dyn.csh
Code:
#! /bin/csh -f
#PBS -j oe
#PBS -m e

# ASSUMPTION (1): input files are named dynstrt.inp and dyn.inp
# ASSUMPTION (2): output files are named    dyn.res  dyn.trj  dyn.out
# ASSUMPTION (3): previous restart file read as  dyn.rea
#

# change to subdir where 'qsub' was used
cd $PBS_O_WORKDIR

# assign 'chm' variable to point to CHARMM executable
# 'c35b1' is a cover script for running a variety of c35b1 executables
# this usage is for 8 procs on a single node via shared memory MPICH
set chm = "/v/apps/bin/c35b1 shm 8"

# repeat 'run' times 
@ nrun = 5
@ krun = 1
 while ( $krun <= $nrun )

### alternate form is for MPI versions which don't support "stdin"
if ( -e next.seqno ) then
 $chm < dyn.inp > dyn.out
### $chm dyn.inp > dyn.out
else
 $chm < dynstrt.inp > dyn.out
### $chm dynstrt.inp > dyn.out
endif

set okay = true
# TEST FOR EXISTENCE, THEN NONZERO LENGTH OF OUTPUT FILES
if ( -e dyn.res && -e dyn.trj ) then
 @ res = `wc dyn.res | awk '{print $1}'`
 @ tsz = `ls -s dyn.trj | awk '{print $1}'`
# CHECK FOR EXIT MESSAGE
 @ nrm = `grep ' NORMAL TERMINATION ' dyn.out | wc -l`
 if ( $res > 100 && $tsz > 0 && $nrm == 1 ) then
# SUCCESSFUL RUN; COPY RESTART FILE
  cp dyn.res dyn.rea
# DETERMINE RUN NUMBER
  if ( -e next.seqno ) then
   @ i = `cat next.seqno` 
  else
   @ i = 1
  endif
# NUMBER THE OUTPUT FILES; CHANGE EXTENSIONS TO SUIT APPLICATION
  foreach fil ( out res trj )
   mv dyn.$fil dyn$i.$fil
  end
# COMPRESS TEXT BASED FILES
  gzip dyn$i.out dyn$i.res
# CONDITIONAL END CHECK
  if ( -e last.seqno ) then
    @ l = `cat last.seqno`
    if ( $i == $l ) then
      @ i += 1
      echo $i > next.seqno 
      exit
    endif
  endif
  @ i += 1
  echo $i > next.seqno
 else
# ZERO LENGTH FILE(S)
  set okay = false
 endif
else
# FILE DOESN'T EXIST
 set okay = false
endif

# TEST FOR CHARMM RUN FAILED; CREATE .ERR FILE WITH TIMESTAMP
if ( $okay == true ) then
# SUBMIT THE NEXT JOB; LOGIN TO HEAD NODE OF CLUSTER; REQUIRES SSH KEYS
 if ( $krun == $nrun ) ssh m1 "cd $cwd; ./lobos.com"
 @ krun += 1
else
 set ts = `date +%m%d.%H%M`
 mv dyn.out dyn.err.$ts
 exit(201)
endif

end