/
euorun.py
504 lines (454 loc) · 19 KB
/
euorun.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#!/usr/bin/python
import subprocess
import sys
import os
import findtc as ftc
import database
import job
import system_parameter
def system_equal(sp1, sp2):
# conditions
conditions=[]
# bulk, isolated and heterostructure parameter
conditions.append(sp1.get_system().material_class==sp2.get_system().material_class)
conditions.append(sp1.get_system().name==sp2.get_system().name)
conditions.append(sp1.concentration==sp2.concentration)
# isolated and heterostructure parameter
if sp1.get_system().material_class=='isolated':
conditions.append(sp1.N==sp2.N)
# heterostructure parameter
if sp1.get_system().material_class=='heterostructure':
conditions.append(sp1.N0==sp2.N0)
conditions.append(sp1.n_cr==sp2.n_cr)
conditions.append(sp1.Delta_W==sp2.Delta_W)
return all(conditions)
def database_exists(sp):
if sp.get_system().material_class=='bulk':
bdb=database.bulk_database()
bdb.download()
if bdb.exists(sp.get_system().name, sp.concentration, sp.temperature):
return True
else:
return False
elif sp.get_system().material_class=='isolated':
idb=database.isolated_database()
idb.download()
if idb.exists(sp.get_system().name, sp.N, sp.concentration, sp.temperature):
return True
else:
return False
else:
hdb=database.heterostructure_database()
hdb.download()
if hdb.exists(sp.get_system().name, sp.N, sp.N0, sp.concentration, sp.n_cr, sp.Delta_W, sp.temperature):
return True
else:
return False
def get_worker():
host=database.get_host()
idb=database.isolated_database()
idb.download()
for worker in idb.workers:
if worker.host==host:
return worker
print "Error: get_worker: %s is an unknown working host. Break." % host
exit(1)
class euorun:
def __init__(self, np, material, N=5, M=None, ni=0.01, ncr=None, dW=None, output=None, input=None, initial_input=None, inputFlag=True, isoDeltaFlag=True, updatedbFlag=True, iteration_parameter=None, get_default_iteration_parameter=None, check_database=False, source=None, input_system_name=None, log='run', verbose=True, email='stollenwerk@th.physik.uni-bonn.de', mailcmd='mailx -s'):
# number of nodes
self.np=np
# material name
self.material=material
# number of left layers
self.N=N
# number of right layers
self.M=M
# number of charge carriers in the left system
self.ni=ni
# number of charge carriers in the right system
self.ncr=ncr
# workfunction difference between left and right system
self.dW=dW
# initial input folder
self.initial_input=initial_input
# search automatically for suitable input
self.inputFlag=inputFlag
# add isolated delta values automatically
self.isoDeltaFlag=isoDeltaFlag
# update databases after succesfful runs automatically
self.updatedbFlag=updatedbFlag
# additional parameter (like max2, wr1, etc.)
# user defined parameter (if not defined add nothing)
if iteration_parameter!=None:
self.iteration_parameter=iteration_parameter
else:
self.iteration_parameter=''
# function which gives the default iteration parameter depending on the material
# (only relevant for automatic isodelta runs)
if get_default_iteration_parameter!=None:
self.get_default_iteration_parameter=get_default_iteration_parameter
else:
self.get_default_iteration_parameter=database.get_iteration_parameter
# check database before a run, if it exists don't run again
self.check_database=check_database
# source for searching suitable input ('local', 'remote' or None(=both))
self.source=source
# alternative system name which can serve as an input (only if source!=local)
self.input_system_name=input_system_name
# logfile name
self.log=log
# email address
self.email=email
# send intermediate notifications
self.verbose=verbose
# email command
self.mailcmd=mailcmd
# keep an instance of the system parameter class for later use
self.sp=system_parameter.system_parameter()
# keep an instance of bulk database for later use
self.bdb=database.bulk_database()
self.bdb.download()
# keep an instance of isolated database for later use
self.idb=database.isolated_database()
self.idb.download()
# keep an instance of heterostructure database for later use
self.hdb=database.heterostructure_database()
self.hdb.download()
# get material class
self.material_class=self.sp.get_system_by_name(self.material).material_class
# get mpicmd
self.mpicmd=get_worker().mpicmd
# get name which defines the system
self.name=None
if self.material_class=='bulk':
self.name=self.bdb.get_output(self.material, self.ni)
elif self.material_class=='isolated':
self.name=self.idb.get_output(self.material, self.N, self.ni)
else:
self.name=self.hdb.get_output(self.material, self.N, self.M, self.ni, self.ncr, self.dW)
# set top output folder to current working directory by default
if output==None:
if self.material_class=='bulk':
self.output=self.bdb.get_output(self.material, self.ni)
elif self.material_class=='isolated':
self.output=self.idb.get_output(self.material, self.N, self.ni)
else:
self.output=self.hdb.get_output(self.material, self.N, self.M, self.ni, self.ncr, self.dW)
else:
self.output=output
# set top input search folder to output folder by default
if input==None:
self.input=self.output
else:
self.input=input
# host
self.host=database.get_host()
# write to log file
def write_log(self, message):
f=open("%s.log" % self.log, 'a')
f.write(message)
f.close()
# write to error log file
def write_error_log(self, message):
f=open("%s.err" % self.log, 'a')
f.write(message)
f.close()
# remove log files if they exist
def log_prepare(self):
if os.path.exists("%s.log" % self.log):
os.remove("%s.log" % self.log)
if os.path.exists("%s.err" % self.log):
os.remove("%s.err" % self.log)
# check output folder if results are already there
def run_exists(self, runcmd, output, check_database=True):
if os.path.exists("%s/parameter.cfg" % output):
sp1=system_parameter.system_parameter()
sp1.read_file("%s/parameter.cfg" % output)
if sp1.get_system()==None:
return False
else:
sp2=system_parameter.system_parameter()
sp2.read_cmd(runcmd)
results_exists=os.path.exists("%s/results" % output)
return system_equal(sp1, sp2) and results_exists
elif check_database:
sp=system_parameter.system_parameter()
sp.read_cmd(runcmd)
return database_exists(sp)
else:
return False
def run_bulk(self, t, special_input=None):
# run name
runname="%s, ni=%f, T=%f" % (self.material, self.ni, t)
self.write_log("##############################################\n")
self.write_log("### euorun: %s\n" % runname)
self.write_log("##############################################\n")
# run command
runcmd=self.mpicmd + " -np %i " % self.np
runcmd+=self.sp.get_runcmd_bulk(self.material, self.ni, t)
# add additional parameter
runcmd+=self.iteration_parameter
# add output
runoutput=self.output + self.idb.get_temp_output(t)
runcmd+=" -o %s/" % runoutput
runexists=self.run_exists(runcmd, runoutput, check_database=self.check_database)
if not runexists:
# add special input folder
if special_input!=None:
runcmd+=" -i %s" % (special_input)
# search self.input folder for suitable input folders and add it
else:
if self.inputFlag:
runcmd=database.add_input(runcmd, download_path=self.output+"/download/", path=self.input, source=self.source, input_system_name=self.input_system_name)
# run job
j=job.job(runname, self.log, self.email, [runcmd], logappend=True, verbose=self.verbose, mailcmd=self.mailcmd)
j.run()
# update database
if self.updatedbFlag and not runexists:
self.write_log("* Update bulk database\n")
updatecmd="bulk_remote.py %s" % runoutput
#subprocess.call(updatecmd, shell=True)
j=job.job("update remote bulk database", self.log, self.email, [updatecmd], logappend=True, verbose=False, mailcmd=self.mailcmd)
j.run()
self.write_log("\n")
def run_isolated(self, t, special_input=None):
# run name
runname="%s, N=%i, ni=%f, T=%f" % (self.material, self.N, self.ni, t)
self.write_log("##############################################\n")
self.write_log("### euorun: %s\n" % runname)
self.write_log("##############################################\n")
# run command
runcmd=self.mpicmd + " -np %i " % self.np
runcmd+=self.sp.get_runcmd_isolated(self.material, self.N, self.ni, t)
# add additional parameter
runcmd+=self.iteration_parameter
# add output
runoutput=self.output + self.idb.get_temp_output(t)
runcmd+=" -o %s/" % runoutput
# check if run exists before downloading possible input
runexists=self.run_exists(runcmd, runoutput, check_database=self.check_database)
# run job
if not runexists:
# add special input folder
if special_input!=None:
runcmd+=" -i %s" % (special_input)
# search self.input folder for suitable input folders and add it
else:
if self.inputFlag:
runcmd=database.add_input(runcmd, download_path=self.output+"/download/", path=self.input, source=self.source, input_system_name=self.input_system_name)
j=job.job(runname, self.log, self.email, [runcmd], logappend=True, verbose=self.verbose, mailcmd=self.mailcmd)
j.run()
# update database
if self.updatedbFlag and not runexists:
self.write_log("* Update isolated database\n")
updatecmd="isolated_remote.py %s" % runoutput
#subprocess.call(updatecmd, shell=True)
j=job.job("update remote isolated database", self.log, self.email, [updatecmd], logappend=True, verbose=False, mailcmd=self.mailcmd)
j.run()
self.write_log("\n")
def run_hetero(self, t, special_input=None):
# run name
runname="%s, N=%i, M=%i, ni=%f, ncr=%f, dW=%f, T=%f" % (self.material, self.N, self.M, self.ni, self.ncr, self.dW, t)
self.write_log("##############################################\n")
self.write_log("### euorun: %s\n" % runname)
self.write_log("##############################################\n")
# run command
runcmd=self.mpicmd + " -np %i " % self.np
runcmd+=self.sp.get_runcmd_hetero(self.material, self.N, self.M, self.ni, self.ncr, self.dW, t)
# add additional parameter
runcmd+=self.iteration_parameter
# add output
runoutput=self.output + self.hdb.get_temp_output(t)
runcmd+=" -o %s/" % runoutput
#print "check", runcmd
# check if run not already exist
runexists=self.run_exists(runcmd, runoutput, check_database=self.check_database)
if not runexists:
# add special input folder
if special_input!=None:
runcmd+=" -i %s" % (special_input)
# search self.input folder and/or remote database for suitable input folders and add it
else:
if self.inputFlag:
runcmd=database.add_input(runcmd, download_path=self.output+"/download/", path=self.input, source=self.source, input_system_name=self.input_system_name)
if self.isoDeltaFlag:
######################################################################################
####### add energy shift values for the isolated system constituents #################
######################################################################################
# check is values of energy shifts in the isolated system already exist
#print "check isodeltas:", database.get_isodelta_info(runcmd)
self.write_log("* Check isolated deltas: %s, %s, %s, %s, %s, %s, %s, %s\n" % (database.get_isodelta_info(runcmd)[:-1]))
(exists_left, material_left, N_left, nc_left, exists_right, material_right, N_right, nc_right, temp)=database.get_isodelta_info(runcmd)
# if not start isolated runs
if not exists_left or not exists_right:
if not exists_left:
# get name
runname_left="%s, N=%i, ni=%f, T=%f" % (material_left, N_left, nc_left, t)
self.write_log("* Isolated run necessary: %s\n" % runname_left)
# get run command
runcmd_left=self.mpicmd + " -np %i " % self.np
runcmd_left+=self.sp.get_runcmd_isolated(material_left, N_left, nc_left, t)
# add default additional parameter for iteration
runcmd_left+=self.get_default_iteration_parameter(material_left)
# add output
output_left=self.idb.get_output(material_left, N_left, nc_left)
runoutput_left=output_left + self.idb.get_temp_output(t)
runcmd_left+=" -o " + runoutput_left
# run left system
if not self.run_exists(runcmd_left, runoutput_left):
# add input if existent
runcmd_left=database.add_input(runcmd_left, download_path=output_left+"/download/", path=output_left, source=self.source)
j=job.job(runname_left, self.log, self.email, [runcmd_left], logappend=True, verbose=self.verbose, mailcmd=self.mailcmd)
j.run()
# update database
self.write_log("* Update isolated database\n")
#print "update isolated db"
updatecmd_left="isolated_remote.py %s" % output_left
#subprocess.call(updatecmd_left, shell=True)
j=job.job("update remote isolated database" , self.log, self.email, [updatecmd_left], logappend=True, verbose=False, mailcmd=self.mailcmd)
j.run()
if not exists_right:
# get name
runname_right="%s, N=%i, ni=%f, T=%f" % (material_right, N_right, nc_right, t)
self.write_log("* Isolated run necessary: %s\n" % runname_right)
# get run command
runcmd_right=self.mpicmd + " -np %i " % self.np
runcmd_right+=self.sp.get_runcmd_isolated(material_right, N_right, nc_right, t)
# add default additional parameter for iteration
runcmd_right+=self.get_default_iteration_parameter(material_right)
# add output
output_right=self.idb.get_output(material_right, N_right, nc_right)
runoutput_right=output_right + self.idb.get_temp_output(t)
runcmd_right+=" -o " + runoutput_right
# run right system
if not self.run_exists(runcmd_right, runoutput_right):
# add input if existent
runcmd_right=database.add_input(runcmd_right, download_path=output_right+"/download/", path=output_right, source=self.source)
j=job.job(runname_right, self.log, self.email, [runcmd_right], logappend=True, verbose=self.verbose, mailcmd=self.mailcmd)
j.run()
# update database
#print "update isolated db"
self.write_log("* Update isolated database\n")
updatecmd_right="isolated_remote.py %s" % output_right
#subprocess.call(updatecmd_right, shell=True)
j=job.job("update remote isolated database" , self.log, self.email, [updatecmd_right], logappend=True, verbose=False, mailcmd=self.mailcmd)
j.run()
# add isodeltas
runcmd=database.add_isodeltas(runcmd)
# run heterostructure job
#print "run", runcmd
j=job.job(runname, self.log, self.email, [runcmd], logappend=True, verbose=self.verbose, mailcmd=self.mailcmd)
j.run()
# update database
if self.updatedbFlag and not runexists:
self.write_log("* Update heterostructure database\n")
#print "update heterostructure db"
updatecmd="heterostructure_remote.py %s" % runoutput
#subprocess.call(updatecmd, shell=True)
j=job.job("update remote heterostructure database", self.log, self.email, [updatecmd], logappend=True, verbose=False, mailcmd=self.mailcmd)
j.run()
self.write_log("\n")
def run(self, t, special_input=None):
if self.material_class=='bulk':
self.run_bulk(t, special_input)
elif self.material_class=='isolated':
self.run_isolated(t, special_input)
else:
self.run_hetero(t, special_input)
# temperature sweep
def tempsweep(self, temperatures):
self.log_prepare()
first=True
for t in temperatures:
if first:
self.run(t, special_input=self.initial_input)
else:
self.run(t)
first=False
# send finishing notification
runname=''
if self.material_class=='bulk':
runname="%s, ni=%f, T=" % (self.material, self.ni)
elif self.material_class=='isolated':
runname="%s, N=%i, ni=%f, T=" % (self.material, self.N, self.ni)
else:
runname="%s, N=%i, M=%i, ni=%f, ncr=%f, dW=%f, T=" % (self.material, self.N, self.M, self.ni, self.ncr, self.dW)
for t in temperatures:
runname +=" %f," % t
cmd="echo "" | %s 'Temperature sweep finished: %s on %s.' %s" % (self.mailcmd, runname, self.host, self.email)
subprocess.call(cmd, shell=True)
# extract magnetisation of a successful run
def extractMag(self, t):
magfile=None
if self.material_class=='bulk':
magfile=self.output + self.bdb.get_temp_output(t) + "results/totalmag.dat"
elif self.material_class=='isolated':
magfile=self.output + self.idb.get_temp_output(t) + "results/avmag.dat"
else:
magfile=self.output + self.hdb.get_temp_output(t) + "results/avmag.dat"
# read magnetisation file or download it (in the case of checkdatabase==True)
if os.path.exists(magfile):
return float(database.extractResultValue2ndColumn(magfile))
else:
cmd = "scp stollenw@steinschal-tradigist.th.physik.uni-bonn.de:/users/stollenw/projects/euo/results/%s/%s temp_magfile.dat" % (self.material_class, magfile)
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
proc.communicate()
mag=float(database.extractResultValue2ndColumn("temp_magfile.dat"))
os.remove("temp_magfile.dat")
return mag
# find Tc temperature sweep
def findtc(self, temperatures=None, tsteps=None, deltaM=None):
# prepare logfiles
self.log_prepare()
# default magnetisation precision
if deltaM==None:
deltaM=1.0E-2
# default temperatures steps
if temperatures==None:
temperatures=range(20,301,20)
# default temperatures steps
if tsteps==None:
# default temperature increments
tsteps=[160,80,40,20,10,5,2.5,1,0.1]
# tsteps must contain at least one entry
if len(tsteps)<1:
print "Error: euorun: find_tc: No temperature steps given. Break"
exit(1)
# sort tsteps
tsteps.sort()
# reverse order (decendent)
tsteps.reverse()
(tc, dT, dM)=ftc.findtc(self.run, self.extractMag, (), (self.initial_input,), (), temperatures, tsteps, deltaM)
##############################
# save tc in file and upload it to database
##############################
local_file="%s/tc.dat" % self.output
f=open(local_file, 'w')
f.write("# Curie temperature of %s\n" % self.name )
f.write("# Magnetisation accuracy dM=%f\n" % dM)
f.write("# Temperature accuracy dT=%f\n" % dT)
f.write("# Tc=%f\n" % tc)
f.write("%0.17e\n" % tc)
f.close()
if self.updatedbFlag:
cmd = "scp %s stollenw@steinschal-tradigist.th.physik.uni-bonn.de:/users/stollenw/projects/euo/results/%s/%s/" % (local_file, self.material_class, self.output)
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
proc.communicate()
##############################
# send finishing notification
##############################
runname=''
if self.material_class=='bulk':
runname="%s, ni=%f, Tc=%f" % (self.material, self.ni, tc)
elif self.material_class=='isolated':
runname="%s, N=%i, ni=%f, Tc=%f" % (self.material, self.N, self.ni, tc)
else:
runname="%s, N=%i, M=%i, ni=%f, ncr=%f, dW=%f, Tc=%f" % (self.material, self.N, self.M, self.ni, self.ncr, self.dW, tc)
cmd="echo '' | %s 'Find Tc finished: %s on %s.' %s" % (self.mailcmd, runname, self.host, self.email)
subprocess.call(cmd, shell=True)
def main():
erun=euorun(1, 'Bulk-Heisenberg-Metal', N=1, ni=0.001)
#erun.tempsweep((21,22))
erun.findtc()
if __name__=="__main__":
main()