-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathremote_dacapo.py
197 lines (136 loc) · 4.57 KB
/
remote_dacapo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python
'''
this script will submit a job on the remote system to run dacapo
on a remote queue. This module works with Jacapo and ase3.
you need to have the same versions of ase and Jacapo installed locally
and remotely.
I use exceptions a lot in these modules. Basically anytime a job is
not complete, some kind of exception is raised.
Use this in a script like this:
from ase import *
from ase.calculators.jacapo import *
from htp.remote_dacapo import *
Jacapo.calculate = remote_dacapo
author:: John Kitchin <jkitchin@andrew.cmu.edu>
'''
import exceptions
import commands, os, string, sys, time
from htp.torque import *
from htp.ssh import ssh
from htp.rsync import rsync
from ase import *
from ase.calculators.jacapo import *
from Scientific.IO.NetCDF import NetCDFFile
class DacapoAborted(exceptions.Exception):
def __init__(self,args=None):
self.args = args
def __str__(self):
return string.join(self.args,'')
class DacapoNotFinished(exceptions.Exception):
def __init__(self,args=None):
self.args = args
def __str__(self):
return string.join(self.args,'')
USER = os.environ['USER']
REMOTE_HOST = 'beowulf.cheme.cmu.edu'
if REMOTE_HOST is None:
raise Exception, 'You need to define your REMOTE_HOST'
SERVER = '%s@%s' % (USER, REMOTE_HOST)
def remote_dacapo(self,*args,**kwargs):
'''
this will replace the calculate method of the Jacapo Calculator
and run a job remotely. if the job is done it copies the results
back
'''
CWD = os.getcwd()
wholencfile = self.get_nc()
basepath,ncfile = os.path.split(wholencfile)
atoms = Jacapo.read_atoms(wholencfile)
self = atoms.get_calculator()
if self.get_status() == 'finished':
return True
#the ncfile is what will be used on the remote dir, not the whole path
NCFILE = ncfile
basename,ext = os.path.splitext(NCFILE)
TXTFILE = basename + '.txt'
# now, lets change into the basepath and do everything from there
if basepath != '': #in case basepath is the cwd
os.chdir(basepath)
DACAPOCMDe = 'dacapo.run %s -out %s' % (NCFILE,TXTFILE)
#scriptname for the remote job
JOBFILE = basename + '.job_sh'
job = '''\
#!/bin/tcsh
cd $PBS_O_WORKDIR
dacapo.run %s -out %s
stripnetcdf %s
#end
''' % (NCFILE,TXTFILE,NCFILE)
f = open(JOBFILE,'w')
f.write(job+'\n')
f.close()
os.chmod(JOBFILE,0777)
rc = JOBFILE+'.rc'
# get a queue object
pbs = PBS(SERVER)
#we try the qsub here because exceptions are raised if
#the job has already been submitted or is running.
try:
#this will raise a variety of exceptions that we catch
if hasattr(self,'qsuboptions'):
qsuboptions = self.qsuboptions
else:
qsuboptions = ''
pbs.qsub(QSUB='qsub -j oe %s' % qsuboptions,
jobfiles=[JOBFILE,NCFILE])
except PBS_MemoryExceeded, e:
# we create jobfile.rc so the job gets run with more
# memory next time.
#mem = int(e.mem * 1.25)
#f = open(rc,'a')
#f.write('QSUB_MEM = %i\n' % mem)
#f.close()
os.chdir(CWD)
print 'Caught memory exception'
#print 'next time it should run with %i kb memory' % mem
print 'see rc: ',rc
raise e
except JobDone:
# nothing here to do.
pass
except Exception,e:
# pbs.submit can raise a lot of different exceptions
# we need to change back to CWD, no matter what
# and we re-raise the exception so we can see it
os.chdir(CWD)
print e
raise e
# now check if it finished correctly
atoms = Jacapo.read_atoms(NCFILE)
self = atoms.get_calculator()
#check for dacapo errors
TXTFILE = basename + '.txt'
f = open(TXTFILE,'r')
for line in f:
if 'abort_calc' in line:
f.close()
raise DacapoAborted, line
continue
f.close()
if not ('clexit: exiting the program' in line
or 'PAR: msexit halting Master' in line):
raise DacapoNotFinished, line
stopfile = NCFILE + '.stop'
if os.path.exists(stopfile):
os.unlink(stopfile)
runningfile = NCFILE + '.running'
if os.path.exists(runningfile):
os.unlink(runningfile)
#slave files from parallel runs
import glob
slvpattern = TXTFILE + '.slave*'
for slvf in glob.glob(slvpattern):
print 'deleting %s' % slv
os.unlink(slvf)
os.chdir(CWD)
return 0