forked from rickardcronholm/SAMC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemonC_exec.py
261 lines (234 loc) · 9.65 KB
/
daemonC_exec.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/python
# python script that identifies egslog, in a specific directory, and executes a series of commands if the tarfile is newer than the last run of the script
# The main funcitonof the script is to initiate Monte Carlo simulations using DOSXYZnrc
#
# Copyright [2016] [Rickard Cronholm] Licensed under the
# Educational Community License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may
# obtain a copy of the License at
#
#http://www.osedu.org/licenses/ECL-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS"
# BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing
# permissions and limitations under the License.
#
import os
import sys
import datetime
import shutil
import fnmatch
import time
import samcUtil
import re
import fileinput
import glob
import subprocess
def find(pattern, path, compareDir, exclude):
result = []
for root, dirs, files in os.walk(path):
for name in files:
if fnmatch.fnmatch(name, pattern):
if not (fnmatch.fnmatch(name, '*w*') or fnmatch.fnmatch(name, '*egsrun*')):
if name.find('_') > 0:
timeStamp = name[0:name.find('_')]
if os.path.exists(os.path.join(compareDir, timeStamp)):
if not exclude in root:
# check consistensy with beamDir
# get BEAMDIR
fb = open(os.path.join(timeStamp, timeStamp +
'.beamDir'), 'r')
beamDir = fb.readline().rstrip()
fb.close()
if beamDir in root:
result.append(os.path.join(root, name))
return result
# get current time
now = datetime.datetime.now()
newTime = now.strftime("%y%m%d%H%M%S")
# change dir to where the script is
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
# check if lock file exists, otherwise create it
lockFile = 'Dc.lock'
if os.path.isfile(lockFile):
print 'Busy, quitting'
sys.exit(0)
else:
flock = open(lockFile, 'w')
flock.write('Busy\n')
flock.close()
# get variables from confFile
confFile = 'samc.conf'
with open(confFile) as f:
confCont = f.readlines()
whatToGet = ['daemonC.timeFile', 'common.EGS_HOME', 'daemonC.dosxyznrc',
'daemonC.nBatch', 'daemonB.beamPegs', 'daemonC.ncaseMultiplier',
'daemonC.dosPegs']
# generate struct
cv = samcUtil.struct()
# read variables
cv = samcUtil.getConfVars(cv, whatToGet, confCont)
# convert types
cv = samcUtil.typeCast(cv, ['nBatch'], [int])
nBatchBeam = int(samcUtil.getVariable(confFile, 'daemonB.nBatch'))
'''
timeFile = samcUtil.getVariable(confFile,'daemonC.timeFile')
EGS_HOME = samcUtil.getVariable(confFile,'common.EGS_HOME')
dosxyznrc = samcUtil.getVariable(confFile,'daemonC.dosxyznrc')
nBatch = int(samcUtil.getVariable(confFile,'daemonC.nBatch'))
dosPegs = samcUtil.getVariable(confFile,'daemonC.dosPegs')
beamPegs = samcUtil.getVariable(confFile,'daemonB.beamPegs')
ncaseMultiple = float(samcUtil.getVariable(confFile,'daemonC.ncaseMultiplier'))
logFile = samcUtil.getVariable(confFile,'daemonC.logFile')
nBatchBeam = int(samcUtil.getVariable(confFile,'daemonB.nBatch'))
'''
# get time of last run
f = open(cv.timeFile, 'r')
oldTime = f.readline().rstrip()
print 'This was last run at {0}'.format(oldTime)
f.close()
# get list of egslst files
fileList = find('*.egslst', cv.EGS_HOME, dname, cv.dosxyznrc)
egslogList = []
# get time of modification of file
for i in range(0, len(fileList)):
try:
fileTime = os.path.getmtime(fileList[i])
fileTime = time.localtime(fileTime)
fileTime = time.strftime("%y%m%d%H%M%S", fileTime)
# if more recent than last run, append to list
if fileTime > oldTime:
egslogList.append(fileList[i])
except OSError:
pass
# quit if no egslog found
if len(egslogList) == 0:
print 'Nothing to do, exiting'
samcUtil.writeTimeToFile(cv.timeFile, newTime)
os.remove(lockFile) # remove lockFile
sys.exit()
# in a for loop: addphsp, cleanup and initiate DOSXYZnrc simulation
for i in range(0, len(egslogList)):
thisBeamDir, thisFile = os.path.split(egslogList[i])
thisFile, fileExtension = os.path.splitext(thisFile)
timeStamp = thisFile[0:thisFile.find('_')]
print 'timeStamp: ', timeStamp
sys.stdout.flush()
# get number of batchJobs for thisFile
dirList = os.listdir(thisBeamDir)
batchJobs = 0
for file1 in dirList: # file the files
if fnmatch.fnmatch(file1, thisFile + '*.IAEAphsp'): # match the wildcard
batchJobs += 1
# addphsp
addFile = os.path.join(thisBeamDir, thisFile)
subprocess.call(['addphsp', addFile, addFile, str(batchJobs), '1', '1', '1'])
#os.system('addphsp %(addFile)s %(addFile)s %(batchJobs)s 1 1 1' % locals())
# check consistensy of checksum
chksum = os.popen("./checksumTest.py %(addFile)s" % locals()).read().rstrip()
if chksum == 'False': # rerun BEAM simulation
removeFiles = []
removeTypes = [addFile + '*_w*', addFile + '*phsp*',
addFile + '*header']
for name in removeTypes:
removeFiles.append(glob.glob(name))
for name in removeFiles:
os.remove(name)
'''
removeFiles = addFile+'*_w*' # remove tempFiles
os.system('rm %(removeFiles)s' % locals()) # cleanup
removeFiles = addFile+'*phsp*' # remove phsp
os.system('rm %(removeFiles)s' % locals()) # cleanup
removeFiles = addFile+'*header' # remove header
os.system('rm %(removeFiles)s' % locals()) # cleanup
'''
# get BEAMDIR
fb = open(os.path.join(timeStamp, timeStamp + '.beamDir'), 'r')
beamDir = fb.readline().rstrip()
HEN_HOUSE = os.environ.get('HEN_HOUSE')
exb = 'scripts/run_user_code_batch'
exbCall = ''.join([HEN_HOUSE, exb])
subprocess.call([exbCall, beamDir, thisFile, cv.beamPegs,
'='.join(['p', str(cv.nBatchBeam)])])
#os.system('%(exbCall)s %(beamDir)s %(thisFile)s %(cv.beamPegs)s p=%(cv.nBatchBeam)s' % locals())
# log files for which checksum != filesize
fchksum = open('chksum.log', 'a')
fchksum.write('{0}\n'.format(timeStamp))
fchksum.close()
continue # skip to next
# if bs file found
if os.path.isfile('/'.join([dname,timeStamp,thisFile+'.bs'])):
# if phspType = 4 modify egsinp and rerun to get egs mode egslst
with open('.'.join([addFile, 'egsinp'])) as inF:
for line in inF:
if 'phspType=4' in line:
oldWATCH = line.rstrip()
# syntesize new IWATCH line
newWATCH = '0, 0, 4, 1' + oldWATCH[oldWATCH.index('4')
+ 1:len(oldWATCH)]
for line in fileinput.input('.'.join([addFile, 'egsinp']), inplace=1):
print line.replace(oldWATCH, newWATCH).rstrip()
# rerun simulation
# get BEAMDIR
fb = open(os.path.join(timeStamp, timeStamp +
'.beamDir'), 'r')
beamDir = fb.readline().rstrip()
HEN_HOUSE = os.environ.get('HEN_HOUSE')
ex = 'scripts/run_user_code'
exCall = ''.join([HEN_HOUSE, ex])
subprocess.call([exCall, beamDir, thisFile, cv.beamPegs])
#os.system('%(exCall)s %(beamDir)s %(thisFile)s %(cv.beamPegs)s' % locals())
break
shutil.move('.'.join([addFile, 'egslst']),
os.path.join(dname, timeStamp, ''))
removeFiles = glob.glob(addFile + '*_w*')
for name in removeFiles:
os.remove(name)
# initate DOSXYZnrc simulation
dosxyzFile = thisFile[0:thisFile.rfind('_')] + '_DOSXYZnrc'
# set medSurr to AIR
lookup = 'AIR'
f = open(os.path.sep.join([cv.EGS_HOME, cv.dosxyznrc, dosxyzFile + '.egsinp']),
'r')
for line in f:
if re.search('phant', line):
phant = line.rstrip()
break
f.close()
with open(phant) as myPhant:
for num, line in enumerate(myPhant, 1):
if lookup in line:
thisMed = num - 1
for line in fileinput.input(os.path.sep.join([cv.EGS_HOME, cv.dosxyznrc,
dosxyzFile + '.egsinp']), inplace=1):
print line.replace("_medSurr_", str(thisMed)).rstrip()
# multiply nCase
print ''.join([addFile, '.1.IAEAheader'])
lookup = 'PARTICLES:'
breakNext = False
f = open(''.join([addFile, '.1.IAEAheader']), 'r')
for line in f:
if breakNext:
nPart = float(line.rstrip().lstrip())
break
if re.search(lookup, line):
breakNext = True
f.close()
nCase = int(cv.ncaseMultiplier * nPart)
for line in fileinput.input('/'.join([cv.EGS_HOME, cv.dosxyznrc,
dosxyzFile + '.egsinp']), inplace=1):
print line.replace("_ncase_", str(nCase)).rstrip()
# initialize DOSXYZnrc simulation
HEN_HOUSE = os.environ.get('HEN_HOUSE')
exb = 'scripts/run_user_code_batch'
exbCall = ''.join([HEN_HOUSE, exb])
subprocess.call([exbCall, cv.dosxyznrc, dosxyzFile, cv.dosPegs,
'='.join(['p', str(cv.nBatch)])])
#os.system('%(exbCall)s %(cv.dosxyznrc)s %(dosxyzFile)s %(cv.dosPegs)s p=%(cv.nBatch)s' % locals())
# update time of last run
samcUtil.writeTimeToFile(cv.timeFile, newTime)
os.remove(lockFile) # remove lockFile