-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaps.py
executable file
·6056 lines (5273 loc) · 233 KB
/
faps.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""
faps -- Frontend for Automated Adsorption Analysis of Porous Solids.
aka
shpes -- Sorption analysis with a High throughput Python
frontend to Examine binding in Structures
Strucutre adsorption property analysis for high throughput processing. Run
as a script, faps will automatically run complete analysis on a structure.
Sensible defaults are implemented, but calculations can be easily customised.
Faps also provides classes and methods for adapting the simulation or only
doing select parts.
"""
# Turn on keyword expansion to get revision numbers in version strings
# in .hg/hgrc put
# [extensions]
# keyword =
#
# [keyword]
# faps.py =
#
# [keywordmaps]
# Revision = {rev}
try:
__version_info__ = (1, 5, 0, int("$Revision$".strip("$Revision: ")))
except ValueError:
__version_info__ = (1, 5, 0, 0)
__version__ = "%i.%i.%i.%i" % __version_info__
import bz2
import code
try:
import configparser
except ImportError:
import ConfigParser as configparser
import gzip
import mmap
import os
import pickle
import re
import shlex
import shutil
import subprocess
import sys
import tarfile
import textwrap
import time
from copy import copy
from glob import glob
from itertools import count
from logging import warning, debug, error, info, critical
from math import ceil, log
from os import path
import numpy as np
from numpy import pi, cos, sin, sqrt, arccos, arctan2
from numpy import array, identity, dot, cross
from numpy.linalg import norm
from binding_sites.absl import calculate_binding_sites
from config import Options
from elements import WEIGHT, ATOMIC_NUMBER, UFF, VASP_PSEUDO_PREF
from elements import CCDC_BOND_ORDERS, GULP_BOND_ORDERS, OB_BOND_ORDERS, METALS
from elements import COVALENT_RADII, UFF_FULL, QEQ_PARAMS
from eos import peng_robinson
from job_handler import JobHandler
from logo import LOGO
# Global constants
DEG2RAD = pi / 180.0
BOHR2ANG = 0.52917720859
EV2KCAL = 23.060542301389
NAVOGADRO = 6.02214129E23
INFINITY = float('inf')
KCAL_TO_KJ = 4.1868 # Steam tables from openbabel
FASTMC_DEFAULT_GRID_SPACING = 0.1
# ID values for system state
NOT_RUN = 0
RUNNING = 1
FINISHED = 2
UPDATED = 3
SKIPPED = -1
NOT_SUBMITTED = -2
# Possible folder names; need these so that similar_ones_with_underscores are
# not globbed
FOLDER_SUFFIXES = ['gulp', 'gulp_opt', 'gulp_fit', 'siesta', 'vasp', 'egulp',
'repeat', 'fastmc', 'properties', 'absl', 'gromacs']
class PyNiss(object):
"""
PyNiss -- Negotiation of Intermediate System States
A single property calculation for one structure. Instance with a set of
options, then run the job_dispatcher() to begin the calculation. The
calculation will pickle itself, or can be pickled at any time, by calling
dump_state().
"""
def __init__(self, options):
"""
Instance an empty structure in the calculation; The dispatcher should
be called to fill it up with data, as needed.
"""
self.options = options
self.structure = Structure(options.get('job_name'))
self.state = {'init': (NOT_RUN, False),
'ff_opt': (NOT_RUN, False),
'dft': (NOT_RUN, False),
'esp': (NOT_RUN, False),
'charges': (NOT_RUN, False),
'properties': (NOT_RUN, False),
'absl': {},
'gcmc': {}}
self.job_handler = JobHandler(options)
def dump_state(self):
"""Write the .niss file holding the current system state."""
job_name = self.options.get('job_name')
info("Writing state file, %s.niss." % job_name)
os.chdir(self.options.get('job_dir'))
# Don't save the job handler in case it changes
save_handler = self.job_handler
self.job_handler = None
my_niss = open(job_name + ".niss", "wb")
pickle.dump(self, my_niss)
my_niss.close()
# put the job handler back and continue
self.job_handler = save_handler
def re_init(self, new_options):
"""Re initialize simulation (with updated options)."""
if new_options.getbool('update_opts'):
info("Using new options.")
self.options = new_options
self.structure.name = new_options.get('job_name')
else:
# Just update command line stuff
info("Using old options with new command line arguments.")
self.options.args = new_options.args
self.options.options = new_options.options
self.options.cmdopts = new_options.cmdopts
self.structure.name = new_options.get('job_name')
self.status(initial=True)
def job_dispatcher(self):
"""
Run parts explicity specified on the command line or do the next step
in an automated run. Drop to interactive mode, if requested.
"""
# In case options have changed, re-intitialize
self.job_handler = JobHandler(self.options)
if 'status' in self.options.args:
self.status(initial=True)
if self.options.getbool('interactive'):
code_locals = locals()
code_locals.update(globals())
console = code.InteractiveConsole(code_locals)
console.push('import rlcompleter, readline')
console.push('readline.parse_and_bind("tab: complete")')
banner = ("((-----------------------------------------------))\n"
"(( Interactive faps mode ))\n"
"(( ===================== ))\n"
"(( ))\n"
"(( WARNING: mode is designed for devs and ))\n"
"(( experts only! ))\n"
"(( Current simulation is accessed as 'self' and ))\n"
"(( associated methods. Type 'dir()' to see the ))\n"
"(( methods in the local namespace and 'help(x)' ))\n"
"(( for help on any object. ))\n"
"(( Use 'self.dump_state()' to save any changes. ))\n"
"((-----------------------------------------------))")
console.interact(banner=banner)
if self.options.getbool('import'):
info("Importing results from a previous simulation")
self.import_old()
self.dump_state()
if self.state['init'][0] == NOT_RUN:
info("Reading in structure")
# No structure, should get one
self.structure.from_file(
self.options.get('job_name'),
self.options.get('initial_structure_format'),
self.options)
if self.options.getbool('order_atom_types'):
info("Forcing atom re-ordering by types")
self.structure.order_by_types()
self.state['init'] = (UPDATED, False)
self.dump_state()
self.step_force_field()
self.step_dft()
self.step_charges()
if self.options.getbool('qeq_fit'):
if not 'qeq_fit' in self.state and self.state['charges'][0] == UPDATED:
info("QEq parameter fit requested")
self.run_qeq_gulp(fitting=True)
self.dump_state()
self.step_properties()
self.step_gcmc()
self.step_absl()
self.send_to_database()
self.post_summary()
def status(self, initial=False):
"""Print the current status to the terminal."""
valid_states = {NOT_RUN: 'Not run',
RUNNING: 'Running',
FINISHED: 'Finished',
UPDATED: 'Processed',
SKIPPED: 'Skipped',
NOT_SUBMITTED: 'Not submitted'}
if initial:
info("Previous system state reported from .niss file "
"(running jobs may have already finished):")
else:
info("Current system status:")
for step, state in self.state.items():
if step == 'gcmc':
if not state:
info(" * State of GCMC: Not run")
else:
for point, job in state.items():
if job[0] is RUNNING:
info(" * GCMC %s: Running, jobid: %s" %
(point, job[1]))
else:
info(" * GCMC %s: %s" %
(point, valid_states[job[0]]))
elif step == 'absl':
if not state:
info(" * State of ABSL: Not run")
else:
# ABSL used to be multiple jobs, still treat jobid as list
for point, jobs in state.items():
if jobs[0] is RUNNING:
info(" * ABSL %s: Running, jobids: %s" %
(point, ",".join('%s' % x for x in jobs[1])))
else:
info(" * ABSL %s: %s" %
(point, valid_states[jobs[0]]))
elif state[0] is RUNNING:
info(" * State of %s: Running, jobid: %s" % (step, state[1]))
else:
info(" * State of %s: %s" % (step, valid_states[state[0]]))
def send_to_database(self):
"""If using a database, store the results"""
# we can skip if not using a database
if not 'sql' in self.options.get('initial_structure_format'):
return
# extract the database and structure names
db_params = self.options.get('job_name').split('.')
# import this here so sqlalchemy is not required generally
from backend.sql import AlchemyBackend
database = AlchemyBackend(db_params[0])
info("Storing results in database")
database.store_results(db_params[1], int(db_params[2]), self.structure)
debug("Database finished")
def post_summary(self):
"""Summarise any results for GCMC, properties..."""
# Also includes excess calculation if void volume calculated
# N = pV/RT
all_csvs = {}
R_GAS = 8.3144621E25 / NAVOGADRO # A^3 bar K-1 molecule
job_name = self.options.get('job_name')
info("Summary of GCMC results")
info("======= ======= ======= ======= =======")
nguests = len(self.structure.guests)
for idx, guest in enumerate(self.structure.guests):
# Determine whether we can calculate the excess for
# any different probes
void_volume = self.structure.sub_property('void_volume')
he_excess, guest_excess = "", ""
if 1.0 in void_volume:
he_excess = 'He-xs-molc/uc,He-xs-mmol/g,He-xs-v/v,He-xs-wt%,'
if hasattr(guest, 'probe_radius'):
if guest.probe_radius != 1.0 and guest.probe_radius in void_volume:
guest_excess = 'xs-molc/uc,xs-mmol/g,xs-v/v,xs-wt%,'
if hasattr(guest, 'c_v') and guest.c_v:
#TODO(tdaff): Make standard in 2.0
# makes sure that c_v is there and not empty
cv_header = "C_v,stdev,"
else:
cv_header = ""
if hasattr(guest, 'fugacities') and guest.fugacities:
fuga_header = "f/bar,"
else:
fuga_header = ""
# Generate headers separately
csv = ["#T/K,p/bar,molc/uc,mmol/g,stdev,",
"v/v,stdev,wt%,stdev,hoa/kcal/mol,stdev,",
guest_excess, he_excess, cv_header, fuga_header,
",".join("p(g%i)" % gidx for gidx in range(nguests)), "\n"]
info(guest.name)
info("---------------------------------------")
info("molc/uc mmol/g vstp/v hoa T_P")
info("======= ======= ======= ======= =======")
for tp_point in sorted(guest.uptake):
# <N>, sd, supercell
uptake = guest.uptake[tp_point]
uptake = [uptake[0]/uptake[2], uptake[1]/uptake[2]]
hoa = guest.hoa[tp_point]
# uptake in mmol/g
muptake = 1000*uptake[0]/self.structure.weight
muptake_stdev = 1000*uptake[1]/self.structure.weight
# volumetric uptake
vuptake = (guest.molar_volume*uptake[0]/
(6.023E-4*self.structure.volume))
vuptake_stdev = (guest.molar_volume*uptake[1]/
(6.023E-4*self.structure.volume))
# weight percent uptake
wtpc = 100*(1 - self.structure.weight/
(self.structure.weight + uptake[0]*guest.weight))
wtpc_stdev = 100*(1 - self.structure.weight/
(self.structure.weight + uptake[1]*guest.weight))
info("%7.2f %7.2f %7.2f %7.2f %s" % (
uptake[0], muptake, vuptake, hoa[0],
("T=%s" % tp_point[0] +
''.join(['P=%s' % x for x in tp_point[1]]))))
csv.append("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f," % (
tp_point[0], tp_point[1][idx], uptake[0],
muptake, muptake_stdev,
vuptake, vuptake_stdev,
wtpc, wtpc_stdev,
hoa[0], hoa[1]))
if guest_excess:
guest_void = void_volume[guest.probe_radius]
n_bulk = (tp_point[1][idx]*guest_void)/(tp_point[0]*R_GAS)
xs_uptake = uptake[0]-n_bulk
# uptake in mmol/g
muptake = 1000*xs_uptake/self.structure.weight
# volumetric uptake
vuptake = (guest.molar_volume*xs_uptake/
(6.023E-4*self.structure.volume))
# weight percent uptake
wtpc = 100*(1 - self.structure.weight/
(self.structure.weight + xs_uptake*guest.weight))
csv.append("%f,%f,%f,%f," % (
xs_uptake, muptake, vuptake, wtpc,))
if he_excess:
guest_void = void_volume[1.0]
n_bulk = (tp_point[1][idx]*guest_void)/(tp_point[0]*R_GAS)
xs_uptake = uptake[0]-n_bulk
# uptake in mmol/g
muptake = 1000*xs_uptake/self.structure.weight
# volumetric uptake
vuptake = (guest.molar_volume*xs_uptake/
(6.023E-4*self.structure.volume))
# weight percent uptake
wtpc = 100*(1 - self.structure.weight/
(self.structure.weight + xs_uptake*guest.weight))
csv.append("%f,%f,%f,%f," % (
xs_uptake, muptake, vuptake, wtpc,))
if cv_header:
csv.append("%f,%f," % (guest.c_v[tp_point]))
if fuga_header:
try:
csv.append("%f," % (guest.fugacities[tp_point]))
except KeyError:
# Assume it was done without fugacity correction
csv.append("%f," % tp_point[1][idx])
# list all the other guest pressures and start a new line
csv.append(",".join("%f" % x for x in tp_point[1]) + "\n")
csv_filename = '%s-%s.csv' % (job_name, guest.ident)
csv_file = open(csv_filename, 'w')
csv_file.writelines(csv)
csv_file.close()
all_csvs[csv_filename] = "".join(csv)
info("======= ======= ======= ======= =======")
info("Structure properties")
# Internally calculated surface area
surf_area_results = self.structure.surface_area()
if surf_area_results:
info("Summary of faps surface areas")
info("========= ========= ========= =========")
info(" radius/A total/A^2 m^2/cm^3 m^2/g")
info("========= ========= ========= =========")
for probe, area in surf_area_results.items():
vol_area = 1E4*area/self.structure.volume
specific_area = NAVOGADRO*area/(1E20*self.structure.weight)
info("%9.3f %9.2f %9.2f %9.2f" %
(probe, area, vol_area, specific_area))
info("========= ========= ========= =========")
# Messy, but check individual properties that might not be there
# and dump them to the screen
info("weight (u): %f" % self.structure.weight)
if hasattr(self.structure, 'pore_diameter'):
info("pores (A): %f %f %f" % self.structure.pore_diameter)
channel_results = self.structure.sub_property('dimensionality')
if channel_results:
for probe, channels in channel_results.items():
info(("channels %.2f probe: " % probe) +
" ".join("%i" % x for x in channels))
# The table is copied from above as it does some calculating
surf_area_results = self.structure.sub_property('zeo_surface_area')
if surf_area_results:
info("Summary of zeo++ surface areas")
info("========= ========= ========= =========")
info(" radius/A total/A^2 m^2/cm^3 m^2/g")
info("========= ========= ========= =========")
for probe, area in surf_area_results.items():
vol_area = 1E4*area/self.structure.volume
specific_area = NAVOGADRO*area/(1E20*self.structure.weight)
info("%9.3f %9.2f %9.2f %9.2f" %
(probe, area, vol_area, specific_area))
info("========= ========= ========= =========")
info("volume (A^3): %f" % self.structure.volume)
void_volume_results = self.structure.sub_property('void_volume')
if surf_area_results:
info("Summary of zeo++ void volumes")
info("========= ========= ========= =========")
info(" radius/A total/A^3 fraction cm^3/g")
info("========= ========= ========= =========")
for probe, void in void_volume_results.items():
void_fraction = void/self.structure.volume
specific_area = NAVOGADRO*void/(1E24*self.structure.weight)
info("%9.3f %9.2f %9.5f %9.4f" %
(probe, void, void_fraction, specific_area))
info("========= ========= ========= =========")
pxrd = self.structure.sub_property('pxrd')
if pxrd:
info("Summary of PXRD; see properties for cpi file")
for probe, pattern in pxrd.items():
info("%s Powder XRD:" % probe)
plot = [['|']*21]
# 1000 points makes this 75 columns wide
averaged = [sum(pattern[x:x+20])/20.0
for x in range(0, 1000, 20)]
# make peaks horizontal first
peak = max(averaged)
for point in averaged:
height = int(round(15*point/peak))
plot.append([' ']*(15-height) + ['|']*height + ['-'])
# transpose for printing
plot = zip(*plot)
for line in plot:
info(''.join(line))
# Email at the end so everything is in the .flog
self.email(all_csvs)
def email(self, csvs=None):
"""Send an email, if one has not already been sent."""
job_name = self.options.get('job_name')
email_addresses = self.options.gettuple('email')
if email_addresses:
info("Emailing results to %s" % ", ".join(email_addresses))
else:
# nobody to email to, why bother?
return
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Construct an email, thanks documentation!
sender = 'Faps Master <[email protected]>'
outer = MIMEMultipart()
outer['Subject'] = 'Results for faps job on %s' % job_name
outer['To'] = ', '.join(email_addresses)
outer['From'] = sender
outer.preamble = 'This is a MIME multipart message\n'
# Just attach all the csv files
if csvs is not None:
for csv in csvs:
msg = MIMEText(csvs[csv], _subtype='csv')
msg.add_header('Content-Disposition', 'attachment',
filename=csv)
outer.attach(msg)
# Include a cif file
msg_cif = MIMEText("".join(self.structure.to_cif()))
msg_cif.add_header('Content-Disposition', 'attachment',
filename="%s.faps.cif" % job_name)
outer.attach(msg_cif)
# And the flog file
try:
flog = open("%s.flog" % job_name)
msg_flog = MIMEText(flog.read())
flog.close()
msg_flog.add_header('Content-Disposition', 'attachment',
filename="%s.flog" % job_name)
outer.attach(msg_flog)
except IOError:
# Error reading the file, don't care
pass
# Send via local SMTP server
s = smtplib.SMTP('localhost')
s.sendmail(sender, email_addresses, outer.as_string())
s.quit()
def step_force_field(self):
"""Check the force field step of the calculation."""
end_after = False
if 'ff_opt' not in self.state:
self.state['ff_opt'] = (NOT_RUN, False)
if self.state['ff_opt'][0] not in [UPDATED, SKIPPED]:
if self.options.getbool('no_force_field_opt'):
info("Skipping force field optimisation")
self.state['ff_opt'] = (SKIPPED, False)
elif self.state['ff_opt'][0] == RUNNING:
job_state = self.job_handler.jobcheck(self.state['ff_opt'][1])
if not job_state:
info("Queue reports force field optimisation has finished")
self.state['ff_opt'] = (FINISHED, False)
else:
# Still running
info("Force field optimisation still in progress")
end_after = True
if self.state['ff_opt'][0] == NOT_RUN or 'ff_opt' in self.options.args:
jobid = self.run_ff_opt()
sys_argv_strip('ff_opt')
end_after = self.postrun(jobid)
self.dump_state()
if self.state['ff_opt'][0] == FINISHED:
self.structure.update_pos(self.options.get('ff_opt_code'),
options=self.options)
self.state['ff_opt'] = (UPDATED, False)
self.dump_state()
# If postrun is submitted then this script is done!
if end_after:
terminate(0)
def step_dft(self):
"""Check the DFT step of the calculation."""
end_after = False
if self.state['dft'][0] not in [UPDATED, SKIPPED]:
if self.options.getbool('no_dft'):
info("Skipping DFT step completely")
info("Job might fail later if you need the ESP")
self.state['dft'] = (SKIPPED, False)
elif self.state['dft'][0] == RUNNING:
job_state = self.job_handler.jobcheck(self.state['dft'][1])
if not job_state:
info("Queue reports DFT step has finished")
self.state['dft'] = (FINISHED, False)
else:
# Still running
info("DFT still in progress")
end_after = True
if self.state['dft'][0] == NOT_RUN or 'dft' in self.options.args:
jobid = self.run_dft()
sys_argv_strip('dft')
end_after = self.postrun(jobid)
self.dump_state()
if self.state['dft'][0] == FINISHED:
self.structure.update_pos(self.options.get('dft_code'))
self.state['dft'] = (UPDATED, False)
self.dump_state()
# If postrun is submitted then this script is done!
if end_after:
terminate(0)
def step_charges(self):
"""Check the charge step of the calculation."""
end_after = False
if self.state['charges'][0] not in [UPDATED, SKIPPED]:
if self.options.getbool('no_charges'):
info("Skipping charge calculation")
self.state['charges'] = (SKIPPED, False)
elif self.state['charges'][0] == RUNNING:
job_state = self.job_handler.jobcheck(self.state['charges'][1])
if not job_state:
info("Queue reports charge calculation has finished")
self.state['charges'] = (FINISHED, False)
else:
info("Charge calculation still running")
end_after = True
if self.state['charges'][0] == NOT_RUN or 'charges' in self.options.args:
jobid = self.run_charges()
sys_argv_strip('charges')
end_after = self.postrun(jobid)
self.dump_state()
if self.state['charges'][0] == FINISHED:
self.structure.update_charges(self.options.get('charge_method'),
self.options)
self.state['charges'] = (UPDATED, False)
self.dump_state()
# If postrun is submitted then this script is done!
if end_after:
terminate(0)
def step_gcmc(self):
"""Check the GCMC step of the calculation."""
end_after = False
jobids = {}
postrun_ids = []
if self.options.getbool('no_gcmc'):
info("Skipping GCMC simulation")
return
elif not self.state['gcmc'] or 'gcmc' in self.options.args:
# The dictionary is empty before any runs
info("Starting gcmc step")
jobids = self.run_fastmc()
sys_argv_strip('gcmc')
self.dump_state()
for tp_point, jobid in jobids.items():
if jobid is True:
self.state['gcmc'][tp_point] = (FINISHED, False)
elif jobid is False:
self.state['gcmc'][tp_point] = (SKIPPED, False)
else:
info("FastMC job in queue. Jobid: %s" % jobid)
self.state['gcmc'][tp_point] = (RUNNING, jobid)
postrun_ids.append(jobid)
# unfinished GCMCs
end_after = True
else:
# when the loop completes write out the state
self.dump_state()
for tp_point in self.state['gcmc']:
tp_state = self.state['gcmc'][tp_point]
if tp_state[0] == RUNNING:
new_state = self.job_handler.jobcheck(tp_state[1])
if not new_state:
info("Queue reports GCMC %s finished" % (tp_point,))
# need to know we have finished to update below
tp_state = (FINISHED, False)
self.state['gcmc'][tp_point] = tp_state
self.dump_state()
else:
info("GCMC %s still running" % (tp_point,))
# unfinished GCMC so exit later
end_after = True
# any states that need to be updated should have been done by now
if tp_state[0] == FINISHED:
startdir = os.getcwd()
# wooki seems slow to copy output files back
# so we give them a few chances to appear
max_attempts = 6
for attempt_count in range(max_attempts):
time.sleep(attempt_count)
try:
self.structure.update_gcmc(tp_point, self.options)
self.state['gcmc'][tp_point] = (UPDATED, False)
self.dump_state()
break
except IOError:
os.chdir(startdir)
else:
error('OUTPUT file never appeared')
if postrun_ids:
self.postrun(postrun_ids)
if end_after:
info("GCMC run has not finished completely")
terminate(0)
def step_properties(self):
"""Run the properties calculations if required."""
if self.state['properties'][0] not in [UPDATED, SKIPPED]:
if self.options.getbool('no_properties'):
info("Skipping all properties calculations")
self.state['properties'] = (SKIPPED, False)
if self.state['properties'][0] == NOT_RUN or 'properties' in self.options.args:
self.calculate_properties()
self.state['properties'] = (UPDATED, False)
self.dump_state()
def step_absl(self):
"""Check the binding site step of the calculation."""
end_after = False
jobids = {}
postrun_ids = []
# check for old simulations with no absl state
# TODO(tdaff): remove eventually
if 'absl' not in self.state:
self.state['absl'] = {}
if self.options.getbool('no_absl'):
info("Skipping ABSL calculation")
return
elif self.options.getbool('no_gcmc'):
info("no_gcmc requested, can't do ABSL, skipping")
return
elif not self.options.getbool('mc_probability_plot'):
info("No probability plot; Skipping ABSL calculation")
return
elif not self.options.getbool('find_maxima'):
info("No TABASCO maxima; Skipping ABSL calculation")
return
elif not self.state['absl'] or 'absl' in self.options.args:
# The dictionary is empty before any runs
info("Starting absl step")
jobids = self.run_absl()
sys_argv_strip('absl')
self.dump_state()
for tp_point, jobid in jobids.items():
if set(jobid) == set([True]):
self.state['absl'][tp_point] = (FINISHED, False)
elif set(jobid) == set([False]):
self.state['absl'][tp_point] = (SKIPPED, False)
else:
info("ABSL job in queue. Jobid: %s" % jobid)
self.state['absl'][tp_point] = (RUNNING, jobid)
postrun_ids.extend(jobid)
# unfinished ABSL calculations
end_after = True
else:
# when the loop completes write out the state
self.dump_state()
for tp_point in self.state['absl']:
tp_state = self.state['absl'][tp_point]
if tp_state[0] == RUNNING:
new_state = set([self.job_handler.jobcheck(job)
for job in tp_state[1]])
if new_state == set([False]):
info("Queue reports ABSL %s finished" % (tp_point,))
# need to know we have finished to update below
tp_state = (FINISHED, False)
self.state['absl'][tp_point] = tp_state
self.dump_state()
else:
info("ABSL %s still running" % (tp_point,))
# unfinished ABSL so exit later
end_after = True
# any states that need to be updated should have been done by now
if tp_state[0] == FINISHED:
startdir = os.getcwd()
# wooki seems slow to copy output files back
# so we give them a few chances to appear
max_attempts = 6
for attempt_count in range(max_attempts):
time.sleep(attempt_count)
try:
self.structure.update_absl(tp_point, self.options)
self.state['absl'][tp_point] = (UPDATED, False)
self.dump_state()
break
except IOError:
os.chdir(startdir)
else:
#TODO(tdaff): does this matter here?
error('ABSL output never appeared')
if postrun_ids:
self.postrun(postrun_ids)
if end_after:
info("ABSL run has not finished completely")
terminate(0)
def import_old(self):
"""Try and import any data from previous stopped simulation."""
job_name = self.options.get('job_name')
job_dir = self.options.get('job_dir')
try:
self.structure.from_file(
job_name,
self.options.get('initial_structure_format'),
self.options)
warning("Ensure that order_atom_types is on for pre-1.4 data")
if self.options.getbool('order_atom_types'):
info("Forcing atom re-ordering by types")
self.structure.order_by_types()
self.state['init'] = (UPDATED, False)
except IOError:
info("No initial structure found to import")
try:
self.structure.update_pos(self.options.get('ff_opt_code'))
self.state['ff_opt'] = (UPDATED, False)
except IOError:
info("No force field optimised structure found to import")
try:
self.structure.update_pos(self.options.get('dft_code'))
self.state['dft'] = (UPDATED, False)
except IOError:
info("No optimized structure found to import")
try:
self.structure.update_charges(self.options.get('charge_method'),
self.options)
self.state['charges'] = (UPDATED, False)
except IOError:
info("No charges found to import")
# Need to generate supercell here on import so that it is set, and
# is based on the cell from dft, if changed
self.structure.gen_supercell(self.options)
guests = [Guest(x) for x in self.options.gettuple('guests')]
if not same_guests(self.structure.guests, guests):
info("Replacing old guests with %s" % " ".join(guest.ident for
guest in guests))
self.structure.guests = guests
else:
# use existing guests that may have data
debug("Retaining previous guests")
guests = self.structure.guests
temps = self.options.gettuple('mc_temperature', float)
presses = self.options.gettuple('mc_pressure', float)
indivs = self.options.gettuple('mc_state_points', float)
for tp_point in state_points(temps, presses, indivs, len(guests)):
try:
self.structure.update_gcmc(tp_point, self.options)
self.state['gcmc'][tp_point] = (UPDATED, False)
except (IOError, OSError):
info("GCMC point %s not found" % str(tp_point))
# Reset directory at end
os.chdir(job_dir)
def postrun(self, jobid):
"""Determine if we need the job handler to post submit itself."""
# update the job tracker
if jobid is not False and jobid is not True:
if self.options.getbool('run_all'):
debug('Submitting postrun script')
os.chdir(self.options.get('job_dir'))
self.job_handler.postrun(jobid)
return True
else:
debug('Postrun script not submitted')
return False
else:
return False
def run_ff_opt(self):
"""Prepare the system and run the selected force field optimisation."""
ff_opt_code = self.options.get('ff_opt_code')
info("Checking connectivity/types")
if self.structure.check_connectivity():
if self.options.getbool('infer_types_from_bonds'):
self.structure.gen_types_from_bonds()
else:
warning("No types; try with infer_types_from_bonds")
else:
info("Bonds and types, provided")
info("Running a %s calculation" % ff_opt_code)
if ff_opt_code == 'gromacs':
jobid = self.run_optimise_gromacs()
elif ff_opt_code == 'gulp':
jobid = self.run_optimise_gulp()
else:
critical("Unknown force field method: %s" % ff_opt_code)
terminate(91)
if jobid is True:
# job run and finished
self.state['ff_opt'] = (FINISHED, False)
else:
info("Running %s job in queue. Jobid: %s" % (ff_opt_code, jobid))
self.state['ff_opt'] = (RUNNING, jobid)
return jobid
def run_dft(self):
"""Select correct method for running the dft/optim."""
dft_code = self.options.get('dft_code')
info("Running a %s calculation" % dft_code)
if dft_code == 'vasp':
jobid = self.run_vasp()
elif dft_code == 'siesta':
jobid = self.run_siesta()
else:
critical("Unknown dft method: %s" % dft_code)
terminate(92)
# update the job tracker
#if jobid is False:
# self.state['dft'] = (NOT_SUBMITTED, False)
# submission skipped
if jobid is True:
# job run and finished
self.state['dft'] = (FINISHED, False)
else:
info("Running %s job in queue. Jobid: %s" % (dft_code, jobid))
self.state['dft'] = (RUNNING, jobid)
return jobid
def run_charges(self):
"""Select correct charge processing methods."""
chg_method = self.options.get('charge_method')
info("Calculating charges with %s" % chg_method)
if chg_method == 'repeat':
# Get ESP
self.esp_to_cube()
jobid = self.run_repeat()
elif chg_method == 'gulp':
jobid = self.run_qeq_gulp()
elif chg_method == 'egulp':
jobid = self.run_qeq_egulp()
else:
critical("Unknown charge calculation method: %s" % chg_method)
terminate(93)
# update the job tracker
if jobid is True:
# job run and finished
self.state['charges'] = (FINISHED, False)
else:
info("Running %s job in queue. Jobid: %s" % (chg_method, jobid))
self.state['charges'] = (RUNNING, jobid)
return jobid
## Methods for specific codes start here
def run_optimise_gromacs(self):
"""Run GROMACS to do a UFF optimisation."""
job_name = self.options.get('job_name')
optim_code = self.options.get('ff_opt_code')
g_verbose = self.options.getbool('gromacs_verbose')
# Run in a subdirectory
optim_dir = path.join(self.options.get('job_dir'),
'faps_%s_%s' % (job_name, optim_code))
mkdirs(optim_dir)
os.chdir(optim_dir)
debug("Running in %s" % optim_dir)
metal_geom = self.options.get('gromacs_metal_geometry')
gro_file, top_file, itp_file = self.structure.to_gromacs(metal_geom)
# We use default names so we don't have to specify
# anything extra on the command line
filetemp = open('conf.gro', 'w')
filetemp.writelines(gro_file)
filetemp.close()
filetemp = open('topol.top', 'w')
filetemp.writelines(top_file)
filetemp.close()
filetemp = open('topol.itp', 'w')
filetemp.writelines(itp_file)
filetemp.close()
filetemp = open('grompp.mdp', 'w')