-
Notifications
You must be signed in to change notification settings - Fork 10
/
run_NAMD_GOMC.py
3415 lines (3066 loc) · 129 KB
/
run_NAMD_GOMC.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
import argparse
import datetime
import json
import os
import subprocess
import sys
from warnings import warn
import numpy as np
import pandas as pd
# MD/MC simulations (NAMD/GOMC) for the NPT, NVT, GEMC, and GCMC ensembles
# *************************************************
# Import the user variables from the json file (start)
# *************************************************
# *************************************************
# The python arguments that need to be selected to run the simulations (start)
# *************************************************
def _get_args():
arg_parser = argparse.ArgumentParser()
# get the filename with the user required input
arg_parser.add_argument(
"-f",
"--file",
help="Defines the variable inputs file used for the hybrid NAMD/GOMC simulation script. "
"This file (i.e., the user_input_variables_NAMD_GOMC.json file) is required "
"to run the hybrid simulation.",
type=str,
)
arg_parser.add_argument(
"-namd_sims_order",
"--namd_simulation_order",
help="This sets the NAMD simulation to be run in series or parallel. "
"The data is entered only as series or parallel (default = series). "
"This is only relevant for the GEMC ensemble when utilizing two (2) NAMD simulation "
"boxes "
"(i.e., only_use_box_0_for_namd_for_gemc = False --> both box 0 and box 1)."
"The GCMC, NVT, NPT, or the GEMC ensembles when using only one (1) "
"NAMD simulation box "
"(i.e., only_use_box_0_for_namd_for_gemc = True --> only box 0) "
"are always run in series, since there is nothing to run in parallel."
"Note: this feature was added so the user can minimize the load on the GPU "
"by running both NAMD simulations in parallel.",
type=str,
)
parser_arguments = arg_parser.parse_args()
# check to see if the file exists
if parser_arguments.file:
if os.path.exists(parser_arguments.file):
print(
"INFO: Reading data from <{}> file.".format(
parser_arguments.file
)
)
else:
print(
"ERROR: Console file <{}> does not exist!".format(
parser_arguments.file
)
)
sys.exit(1)
# set NAMD simulation order
if (
parser_arguments.namd_simulation_order == "parallel"
or parser_arguments.namd_simulation_order == "series"
):
print(
"INFO: The NAMD simulations shall be run in <{}>.".format(
parser_arguments.namd_simulation_order
)
)
elif (
parser_arguments.namd_simulation_order != "parallel"
and parser_arguments.namd_simulation_order != "series"
):
parser_arguments.namd_simulation_order = "series"
print(
"WARNING: The NAMD simulations are not set to 'parallel' or 'series'. Therefore, "
"the NAMD simulations are being default set to <{}>.".format(
parser_arguments.namd_simulation_order
)
)
print("arg_parser.file = " + str(parser_arguments.file))
print(
"parser_arguments.namd_simulation_order = "
+ str(parser_arguments.namd_simulation_order)
)
return [parser_arguments.file, parser_arguments.namd_simulation_order]
# *************************************************
# The python arguments that need to be selected to run the simulations (end)
# *************************************************
# *************************************************
# Import read and check the user input file for errors (start)
# *************************************************
# import and read the users json file
[json_filename, namd_sim_order] = _get_args()
print("json_filename = " + str(json_filename))
print("namd_sim_order = " + str(namd_sim_order))
# just for testing , need to remove later
manual_testing_filename_input_override = False
if json_filename is None and manual_testing_filename_input_override is True:
json_filename = "user_input_NAMD_GOMC.json"
json_file_data = json.load(
open(json_filename)
) # standard name is "user_input_NAMD_GOMC.json"
json_file_data_keys_list = json_file_data.keys()
# get the total_cycles_namd_gomc_sims variable from the json file
if "total_cycles_namd_gomc_sims" not in json_file_data_keys_list:
raise TypeError("The total_cycles_namd_gomc_sims key is not provided.\n")
total_cycles_namd_gomc_sims = json_file_data["total_cycles_namd_gomc_sims"]
if not isinstance(total_cycles_namd_gomc_sims, int):
raise TypeError(
"The total_cycles_namd_gomc_sims values must be an integer.\n"
)
else:
if total_cycles_namd_gomc_sims < 0:
raise TypeError(
"The total_cycles_namd_gomc_sims values must be an integer "
"greater than or equal to zero (>=0.\n"
)
# get the starting_at_cycle_namd_gomc_sims variable from the json file
if "starting_at_cycle_namd_gomc_sims" not in json_file_data_keys_list:
raise TypeError(
"The starting_at_cycle_namd_gomc_sims key is not provided.\n"
)
starting_at_cycle_namd_gomc_sims = json_file_data[
"starting_at_cycle_namd_gomc_sims"
]
if not isinstance(starting_at_cycle_namd_gomc_sims, int):
raise TypeError(
"The starting_at_cycle_namd_gomc_sims values must be an integer.\n"
)
else:
if starting_at_cycle_namd_gomc_sims < 0:
raise TypeError(
"The starting_at_cycle_namd_gomc_sims values must be an integer "
"greater than or equal to zero (>=0.\n"
)
# get the use_CPU_or_GPU variable from the json file
if "gomc_use_CPU_or_GPU" not in json_file_data_keys_list:
raise TypeError("The gomc_use_CPU_or_GPU key is not provided.\n")
gomc_use_CPU_or_GPU = json_file_data["gomc_use_CPU_or_GPU"]
if not isinstance(gomc_use_CPU_or_GPU, str):
raise TypeError("The gomc_use_CPU_or_GPU is not a string.\n")
if gomc_use_CPU_or_GPU != "CPU" and gomc_use_CPU_or_GPU != "GPU":
raise ValueError("The gomc_use_CPU_or_GPU is the string 'CPU' or 'GPU'.\n")
# get the simulation_type variable from the json file
if "simulation_type" not in json_file_data_keys_list:
raise TypeError("The simulation_type key is not provided.\n")
simulation_type = json_file_data["simulation_type"]
if not isinstance(simulation_type, str):
raise TypeError("The simulation_type is not a string.\n")
if simulation_type not in ["GEMC", "GCMC", "NPT", "NVT"]:
raise ValueError(
"The simulation_type is the string 'GEMC', 'GCMC', 'NPT', or 'NVT'.\n"
)
# get the only_use_box_0_for_namd_for_gemc variable from the json file
if "only_use_box_0_for_namd_for_gemc" not in json_file_data_keys_list:
raise TypeError(
"The only_use_box_0_for_namd_for_gemc key is not provided.\n"
)
only_use_box_0_for_namd_for_gemc = json_file_data[
"only_use_box_0_for_namd_for_gemc"
]
if (
only_use_box_0_for_namd_for_gemc is not True
and only_use_box_0_for_namd_for_gemc is not False
):
raise TypeError(
"The only_use_box_0_for_namd_for_gemc not true or false in the json file.\n"
)
# get the no_core_box_0 variable from the json file
if "no_core_box_0" not in json_file_data_keys_list:
raise TypeError("The no_core_box_0 key is not provided.\n")
no_core_box_0 = json_file_data["no_core_box_0"]
if not isinstance(no_core_box_0, int):
raise TypeError("The no_core_box_0 values must be an interger.\n")
else:
if only_use_box_0_for_namd_for_gemc is False and simulation_type == "GEMC":
if no_core_box_0 < 0:
raise ValueError(
"The no_core_box_{} values must be an interger greater "
"than or equal to zero (>=0)".format(0)
)
# get the no_core_box_1 variable from the json file
if "no_core_box_1" not in json_file_data_keys_list:
raise TypeError("The no_core_box_1 key is not provided.\n")
no_core_box_1 = json_file_data["no_core_box_1"]
if not isinstance(no_core_box_1, int):
raise TypeError("The no_core_box_1 values must be an interger.\n")
else:
if only_use_box_0_for_namd_for_gemc is False and simulation_type == "GEMC":
if no_core_box_1 <= 0:
raise ValueError(
"The no_core_box_{} values must be an interger greater than zero (>0) "
"when using only_use_box_0_for_namd_for_gemc is False and "
'simulation_type == "GEMC" .\n'.format(1)
)
if no_core_box_1 < 0:
raise ValueError(
"The no_core_box_{} values must be an interger greater "
"than or equal to zero (>=0)".format(1)
)
# get the simulation_temp_k variable from the json file
if "simulation_temp_k" not in json_file_data_keys_list:
raise TypeError("The simulation_temp_k key is not provided.\n")
simulation_temp_k = json_file_data["simulation_temp_k"]
if not isinstance(simulation_temp_k, int) and not isinstance(
simulation_temp_k, float
):
raise TypeError(
"The simulation_temp_k values must be an interger or float.\n"
)
else:
if simulation_temp_k <= 0:
raise ValueError(
"The simulation_temp_k value must be an interger or float greater than zero (>0)"
)
# get the simulation_pressure_bar variable from the json file
if "simulation_pressure_bar" not in json_file_data_keys_list:
raise TypeError("The simulation_pressure_bar key is not provided.\n")
simulation_pressure_bar = json_file_data["simulation_pressure_bar"]
if simulation_type in ["NPT"]:
if not isinstance(simulation_pressure_bar, int) and not isinstance(
simulation_pressure_bar, float
):
raise TypeError(
"The simulation_pressure_bar values must be an interger or float.\n"
)
else:
if simulation_pressure_bar < 0:
raise ValueError(
"The simulation_pressure_bar value must be an interger or float greater "
"than or equal to zero (>=0)"
)
elif simulation_type in ["GEMC", "GCMC", "NVT"]:
if (
not isinstance(simulation_pressure_bar, int)
and not isinstance(simulation_pressure_bar, float)
and simulation_pressure_bar is not None
):
raise TypeError(
"The simulation_pressure_bar values must be an interger or float (>=0) or null/None.\n"
)
if simulation_type in ["GCMC"]:
# get the GCMC_ChemPot_or_Fugacity variable from the json file
if "GCMC_ChemPot_or_Fugacity" not in json_file_data_keys_list:
raise TypeError("The GCMC_ChemPot_or_Fugacity key is not provided.\n")
GCMC_ChemPot_or_Fugacity = json_file_data["GCMC_ChemPot_or_Fugacity"]
if not isinstance(GCMC_ChemPot_or_Fugacity, str) or (
GCMC_ChemPot_or_Fugacity != "ChemPot"
and GCMC_ChemPot_or_Fugacity != "Fugacity"
):
raise TypeError(
'The GCMC_ChemPot_or_Fugacity values must be the string "ChemPot" or "Fugacity".\n'
)
# get the GCMC_ChemPot_or_Fugacity_dict variable from the json file
if "GCMC_ChemPot_or_Fugacity_dict" not in json_file_data_keys_list:
raise TypeError(
"The GCMC_ChemPot_or_Fugacity_dict key is not provided.\n"
)
GCMC_ChemPot_or_Fugacity_dict = json_file_data[
"GCMC_ChemPot_or_Fugacity_dict"
]
if not isinstance(GCMC_ChemPot_or_Fugacity_dict, dict):
raise TypeError(
"The GCMC_ChemPot_or_Fugacity_dict values must a dictionary.\n"
)
GCMC_ChemPot_or_Fugacity_dict_to_list = GCMC_ChemPot_or_Fugacity_dict.keys()
if GCMC_ChemPot_or_Fugacity == "ChemPot":
for chempot_i in GCMC_ChemPot_or_Fugacity_dict_to_list:
if not isinstance(
GCMC_ChemPot_or_Fugacity_dict[chempot_i], int
) and not isinstance(
GCMC_ChemPot_or_Fugacity_dict[chempot_i], float
):
raise TypeError(
"The GCMC_ChemPot_or_Fugacity_dict values must be floats or integers.\n"
)
if GCMC_ChemPot_or_Fugacity == "Fugacity":
for fugacity_i in GCMC_ChemPot_or_Fugacity_dict_to_list:
if not isinstance(
GCMC_ChemPot_or_Fugacity_dict[fugacity_i], int
) and not isinstance(
GCMC_ChemPot_or_Fugacity_dict[fugacity_i], float
):
raise TypeError(
"The GCMC_ChemPot_or_Fugacity_dict values must be floats or integers.\n"
)
if GCMC_ChemPot_or_Fugacity_dict[fugacity_i] < 0:
raise ValueError(
"The GCMC_ChemPot_or_Fugacity_dict values when using "
'GCMC_ChemPot_or_Fugacity != "Fugacity", must be floats or integers '
"greater than of equal to zero (>=) .\n"
)
else:
GCMC_ChemPot_or_Fugacity = None
GCMC_ChemPot_or_Fugacity_dict = None
# get the gomc_run_steps variable from the json file
if "gomc_run_steps" not in json_file_data_keys_list:
raise TypeError("The gomc_run_steps key is not provided.\n")
gomc_run_steps = json_file_data["gomc_run_steps"]
if not isinstance(gomc_run_steps, int):
raise TypeError("The gomc_run_steps values must be an interger.\n")
# get the namd_run_steps variable from the json file
if "namd_run_steps" not in json_file_data_keys_list:
raise TypeError("The namd_run_steps key is not provided.\n")
namd_run_steps = json_file_data["namd_run_steps"]
if not isinstance(namd_run_steps, int):
raise TypeError("The namd_run_steps values must be an interger.\n")
# get the namd_minimize_mult_scalar variable from the json file
if "namd_minimize_mult_scalar" not in json_file_data_keys_list:
raise TypeError("The namd_minimize_mult_scalar key is not provided.\n")
namd_minimize_mult_scalar = json_file_data["namd_minimize_mult_scalar"]
if not isinstance(namd_run_steps, int):
raise TypeError(
"The namd_minimize_mult_scalar values must be an interger.\n"
)
# get the set_x_dim_box_0 variable from the json file
if "set_dims_box_0_list" not in json_file_data_keys_list:
raise TypeError("The set_dims_box_0_list key is not provided.\n")
set_dims_box_0_list = json_file_data["set_dims_box_0_list"]
if set_dims_box_0_list is not None:
print_error_text = (
"The set_dims_box_0_list must be a null/None or list and contain three (3) integers or floats"
"greater than zero (>0).\n"
)
if isinstance(set_dims_box_0_list, list):
if len(set_dims_box_0_list) == 3:
for dims_box_i in set_dims_box_0_list:
if (
isinstance(dims_box_i, int) is False
and isinstance(dims_box_i, float) is False
and dims_box_i != None
):
raise TypeError(print_error_text)
else:
if dims_box_i != None:
if dims_box_i <= 0:
raise TypeError(print_error_text)
else:
raise TypeError(print_error_text)
else:
raise TypeError(print_error_text)
elif set_dims_box_0_list is None:
set_dims_box_0_list = [None, None, None]
# get the set_x_dim_box_1 variable from the json file
if "set_dims_box_1_list" not in json_file_data_keys_list:
raise TypeError("The set_dims_box_1_list key is not provided.\n")
set_dims_box_1_list = json_file_data["set_dims_box_1_list"]
if set_dims_box_1_list is not None:
print_error_text = (
"The set_dims_box_1_list must be a null/None or list and contain three (3) integers or floats"
"greater than zero (>0).\n"
)
if isinstance(set_dims_box_1_list, list):
if len(set_dims_box_1_list) == 3:
for dims_box_i in set_dims_box_1_list:
if (
isinstance(dims_box_i, int) is False
and isinstance(dims_box_i, float) is False
and dims_box_i != None
):
raise TypeError(print_error_text)
else:
if dims_box_i != None:
if dims_box_i <= 0:
raise TypeError(print_error_text)
else:
raise TypeError(print_error_text)
else:
raise TypeError(print_error_text)
elif set_dims_box_1_list is None:
set_dims_box_1_list = [None, None, None]
# get the set_angle_box_0_list variable from the json file
if "set_angle_box_0_list" not in json_file_data_keys_list:
raise TypeError("The set_angle_box_0_list key is not provided.\n")
set_angle_box_0_list = json_file_data["set_angle_box_0_list"]
if set_angle_box_0_list is not None:
print_error_text = (
"The set_angle_box_0_list must be a null/None or 90 (in degrees). Currently,"
"only orthogonal boxes are available.\n"
)
if isinstance(set_angle_box_0_list, list):
if len(set_angle_box_0_list) == 3:
for angles_box_i in set_angle_box_0_list:
if (
isinstance(angles_box_i, int) is False
and isinstance(angles_box_i, float) is False
and angles_box_i != None
):
raise TypeError(print_error_text)
else:
if angles_box_i != None:
if angles_box_i != 90:
raise TypeError(print_error_text)
else:
raise TypeError(print_error_text)
else:
raise TypeError(print_error_text)
elif set_angle_box_0_list is None:
set_angle_box_0_list = [None, None, None]
# get the set_angle_box_1_list variable from the json file
if "set_angle_box_1_list" not in json_file_data_keys_list:
raise TypeError("The set_angle_box_1_list key is not provided.\n")
set_angle_box_1_list = json_file_data["set_angle_box_1_list"]
if set_angle_box_1_list is not None:
print_error_text = (
"The set_angle_box_1_list must be a null/None or 90 (in degrees). Currently,"
"only orthogonal boxes are available.\n"
)
if isinstance(set_angle_box_1_list, list):
if len(set_angle_box_1_list) == 3:
for angles_box_i in set_angle_box_1_list:
if (
isinstance(angles_box_i, int) is False
and isinstance(angles_box_i, float) is False
and angles_box_i != None
):
raise TypeError(print_error_text)
else:
if angles_box_i != None:
if angles_box_i != 90:
raise TypeError(print_error_text)
else:
raise TypeError(print_error_text)
else:
raise TypeError(print_error_text)
elif set_angle_box_1_list is None:
set_angle_box_1_list = [None, None, None]
# get the starting_ff_file_list_gomc variable from the json file
if "starting_ff_file_list_gomc" not in json_file_data_keys_list:
raise TypeError("The starting_ff_file_list_gomc key is not provided.\n")
starting_ff_file_list_gomc = json_file_data["starting_ff_file_list_gomc"]
if not isinstance(starting_ff_file_list_gomc, list):
raise TypeError(
"The starting_ff_file_list_gomc values must be a list of strings.\n"
)
else:
for start_ff_gomc_i in starting_ff_file_list_gomc:
if not isinstance(start_ff_gomc_i, str):
raise TypeError(
"The starting_ff_file_list_gomc list values must be strings.\n"
)
# get the starting_ff_file_list_namd variable from the json file
if "starting_ff_file_list_namd" not in json_file_data_keys_list:
raise TypeError("The starting_ff_file_list_namd key is not provided.\n")
starting_ff_file_list_namd = json_file_data["starting_ff_file_list_namd"]
if not isinstance(starting_ff_file_list_namd, list):
raise TypeError(
"The starting_ff_file_list_namd values must be a list of strings.\n"
)
else:
for start_ff_gomc_i in starting_ff_file_list_namd:
if not isinstance(start_ff_gomc_i, str):
raise TypeError(
"The starting_ff_file_list_namd list values must be strings.\n"
)
# get the starting_pdb_box_0_file variable from the json file
if "starting_pdb_box_0_file" not in json_file_data_keys_list:
raise TypeError("The starting_pdb_box_0_file key is not provided.\n")
starting_pdb_box_0_file = json_file_data["starting_pdb_box_0_file"]
if not isinstance(starting_pdb_box_0_file, str):
raise TypeError("The starting_pdb_box_0_file is not a string.\n")
# get the starting_psf_box_0_file variable from the json file
if "starting_psf_box_0_file" not in json_file_data_keys_list:
raise TypeError("The starting_psf_box_0_file key is not provided.\n")
starting_psf_box_0_file = json_file_data["starting_psf_box_0_file"]
if not isinstance(starting_psf_box_0_file, str):
raise TypeError("The starting_psf_box_0_file is not a string.\n")
if simulation_type in ["GCMC", "GEMC"]:
# get the starting_pdb_box_1_file variable from the json file
if "starting_pdb_box_1_file" not in json_file_data_keys_list:
raise TypeError("The starting_pdb_box_1_file key is not provided.\n")
starting_pdb_box_1_file = json_file_data["starting_pdb_box_1_file"]
if not isinstance(starting_pdb_box_0_file, str):
raise TypeError("The starting_pdb_box_1_file is not a string.\n")
# get the starting_psf_box_1_file variable from the json file
if "starting_psf_box_1_file" not in json_file_data_keys_list:
raise TypeError("The starting_psf_box_1_file key is not provided.\n")
starting_psf_box_1_file = json_file_data["starting_psf_box_1_file"]
if not isinstance(starting_psf_box_1_file, str):
raise TypeError("The starting_psf_box_1_file is not a string.\n")
else:
starting_pdb_box_1_file = ""
starting_psf_box_1_file = ""
# get the namd2_bin_directory variable from the json file
# json file specifies relative path to the directory where the namd2 is located
if "namd2_bin_directory" not in json_file_data_keys_list:
raise TypeError("The namd2_bin_directory key is not provided.\n")
namd2_bin_directory = json_file_data["namd2_bin_directory"]
if isinstance(namd2_bin_directory, str) is False:
raise TypeError("The namd2_bin_directory values must be an string.\n")
else:
namd_bin_file = "{}/{}/{}".format(
str(os.getcwd()), namd2_bin_directory, "namd2"
)
# get the gomc_bin_directory variable from the json file
# json file specifies relative path to the directory where the GOMC_XXX_XXX binary files are located
if "gomc_bin_directory" not in json_file_data_keys_list:
raise TypeError("The gomc_bin_directory key is not provided.\n")
gomc_bin_directory = json_file_data["gomc_bin_directory"]
if isinstance(gomc_bin_directory, str) is False:
raise TypeError("The gomc_bin_directory values must be an string.\n")
else:
gomc_bin_file = "{}/{}/GOMC_{}_{}".format(
str(os.getcwd()),
gomc_bin_directory,
gomc_use_CPU_or_GPU,
simulation_type,
)
# *************************************************
# Import read and check the user input file for errors (end)
# *************************************************
# *************************************************
# Potential changable variables in the future (start)
# *************************************************
allowable_error_fraction_vdw_plus_elec = 5 * 10 ** (-3)
allowable_error_fraction_potential = 5 * 10 ** (-3)
max_absolute_allowable_kcal_fraction_vdw_plus_elec = 0.5
gomc_console_blkavg_hist_steps = int(gomc_run_steps)
gomc_rst_coor_ckpoint_steps = int(gomc_run_steps)
if gomc_run_steps / 10 > 500:
gomc_hist_sample_steps = int(500)
elif gomc_run_steps / 10 <= 500:
gomc_hist_sample_steps = int(gomc_run_steps / 10)
namd_rst_dcd_xst_steps = int(namd_run_steps)
namd_console_blkavg_e_and_p_steps = int(namd_run_steps)
# *************************************************
# Potential changable variables in the future (end)
# *************************************************
# *************************************************
# check for existing and create NAMD and GOMC folders (start)
# *************************************************
if os.path.isdir("NAMD") or os.path.isdir("GOMC"):
warn(
"INFORMATION: if the system fails to start (with errors) from the beginning of a simulation, "
"you may need to delete the main GOMC and NAMD folders. "
"The failure to start/restart may be caused by the last simulation not finishing correctly."
)
warn(
"INFORMATION: If the system fails to restart a previous run (with errors), "
"you may need to delete the last subfolders under the main "
"NAMD and GOMC (i.e., folders NAMD = 00000000_a or GOMC = 00000001). "
"The failure to start/restart may be caused by the last simulation not finishing properly."
)
path_namd_runs = "NAMD"
os.makedirs(path_namd_runs, exist_ok=True)
path_gomc_runs = "GOMC"
os.makedirs(path_gomc_runs, exist_ok=True)
# *************************************************
# create NAMD and GOMC folders (end)
# *************************************************
# *************************************************
# create NAMD and GOMC config file templates locations (start)
# *************************************************
path_namd_template = "required_data/config_files/NAMD.conf"
path_gomc_template = (
"required_data/config_files/GOMC_" + str(simulation_type) + ".conf"
)
# *************************************************
# create NAMD and GOMC config file templates locations (end)
# *************************************************
gomc_ff_error_message = "ERROR: The GOMC force field(s) need to be provided as a list of string(s).\n"
namd_ff_error_message = "ERROR: The GOMC force field(s) need to be provided as a list of string(s).\n"
if isinstance(starting_ff_file_list_gomc, list) is False:
raise TypeError(gomc_ff_error_message)
if isinstance(starting_ff_file_list_namd, list) is False:
raise TypeError(namd_ff_error_message)
for gomc_ff_i in starting_ff_file_list_gomc:
if isinstance(gomc_ff_i, str) is False:
raise TypeError(gomc_ff_error_message)
for namd_ff_i in starting_ff_file_list_namd:
if isinstance(namd_ff_i, str) is False:
raise TypeError(namd_ff_error_message)
if simulation_type in ["GCMC"]:
if isinstance(GCMC_ChemPot_or_Fugacity_dict, dict) is False:
raise TypeError(
"ERROR: enter the chemical potential or fugacity data (GCMC_ChemPot_or_Fugacity_dict)"
"as a dictionary when using the GCMC ensemble.\n"
)
if GCMC_ChemPot_or_Fugacity is None:
raise ValueError(
"The GCMC_ChemPot_or_Fugacity can not be None when running the GCMC ensemble.\n"
)
if (
GCMC_ChemPot_or_Fugacity != "ChemPot"
and GCMC_ChemPot_or_Fugacity != "Fugacity"
):
raise ValueError(
"The GCMC_ChemPot_or_Fugacity can either be the string "
'"ChemPot" or "Fugacity" when running the GCMC ensemble.\n'
)
GCMC_ChemPot_or_Fugacity_dict_keys = GCMC_ChemPot_or_Fugacity_dict.keys()
for keys_i in GCMC_ChemPot_or_Fugacity_dict_keys:
if (
isinstance(GCMC_ChemPot_or_Fugacity_dict[keys_i], int) is False
and isinstance(GCMC_ChemPot_or_Fugacity_dict[keys_i], float)
is False
):
raise TypeError(
"The GCMC_ChemPot_or_Fugacity_dict values must be an interger or float.\n"
)
if isinstance(keys_i, str) is False:
raise TypeError(
"The GCMC_ChemPot_or_Fugacity_dict keys must be a string.\n"
)
if (
GCMC_ChemPot_or_Fugacity == "Fugacity"
and GCMC_ChemPot_or_Fugacity_dict[keys_i] < 0
):
raise ValueError(
"When using Fugacity, the GCMC_ChemPot_or_Fugacity values must be "
"greater than or equal to zero (>=0).\n"
)
# check the pressure values and set to atomospheric if not used
if simulation_type in ["NPT"] and (
isinstance(simulation_pressure_bar, float) is False
and isinstance(simulation_pressure_bar, int) is False
):
raise TypeError(
"The simulation pressure needs to be set for the NPT simulation type (int or float). \n"
)
if simulation_type in ["NPT"] and (
isinstance(simulation_pressure_bar, float) is True
or isinstance(simulation_pressure_bar, int) is True
):
if simulation_pressure_bar < 0:
raise ValueError(
"The simulation pressure needs to be set to a positive or zero [(int of float) >=0 bar] "
"value for the NPT simulation type. \n"
)
elif simulation_type in ["NVT", "GEMC", "GCMC"]:
# set pressure to atomospheric as it needs a number and it is not used
simulation_pressure_bar = 1.01325
K_to_kcal_mol = 1.98720425864083 * 10 ** (-3)
python_file_directory = os.getcwd()
log_template_file = open(
"{}/NAMD_GOMC_started_at_cycle_No_{}.log".format(
str(python_file_directory), str(starting_at_cycle_namd_gomc_sims)
),
"w",
)
start_time = datetime.datetime.today()
start_time_s = datetime.datetime.today()
write_log_data = (
"*************************************************\n"
"date and time (start) = {} \n"
"************************************************* \n".format(
str(start_time)
)
)
log_template_file.write(str(write_log_data))
print(str(write_log_data))
write_log_data = (
"*************************************************\n"
+ "namd_bin_file = {} \n"
"************************************************* \n".format(
str(namd_bin_file)
)
)
log_template_file.write(str(write_log_data))
print(str(write_log_data))
write_log_data = (
"*************************************************\n"
+ "gomc_bin_file = {} \n"
"************************************************* \n".format(
str(gomc_bin_file)
)
)
log_template_file.write(str(write_log_data))
print(str(write_log_data))
namd_minimize_steps = int(int(namd_run_steps) * int(namd_minimize_mult_scalar))
if simulation_type in ["GEMC"] and only_use_box_0_for_namd_for_gemc is False:
total_no_cores = no_core_box_0 + no_core_box_1
if no_core_box_1 == 0:
write_log_data = (
"*************************************************\n"
"no_core_box_0 = {} \n"
"WARNING: the number of CPU cores listed for box 1 is zero (0), and should be an "
"interger > 0, or the NAMD simulation for box 1 will not run and the python script "
"will crash \n, no_core_box_1 = {} \n"
"*************************************************"
" \n".format(str(no_core_box_0), str(no_core_box_1))
)
log_template_file.write(str(write_log_data))
warn(str(write_log_data))
else:
write_log_data = (
"*************************************************\n"
"no_core_box_0 = {} \n"
"no_core_box_1 = {} \n"
"*************************************************"
" \n".format(str(no_core_box_0), str(no_core_box_1))
)
log_template_file.write(str(write_log_data))
print(str(write_log_data))
else:
if no_core_box_1 != 0:
write_log_data = (
"*************************************************\n"
"no_core_box_0 = {} .\n"
"WARNING: the number of CPU cores listed for box 1 are not being used, \n"
"no_core_box_1 = {}\n"
"*************************************************"
" \n".format(str(no_core_box_0), str(no_core_box_1))
)
log_template_file.write(str(write_log_data))
warn(str(write_log_data))
no_core_box_1 = 0
total_no_cores = no_core_box_0
# Check total cores vs per box cores
if isinstance(no_core_box_0, int) is False:
write_log_data = (
"*************************************************\n"
"Enter no_core_box_0 as an interger, no_core_box_0 = {} \n"
"************************************************* \n".format(
str(no_core_box_0)
)
)
log_template_file.write(str(write_log_data))
print(str(write_log_data))
if no_core_box_0 == 0:
write_log_data = (
"*************************************************\n"
"Enter no_core_box_0 as a non-zero number, no_core_box_0 = {} \n"
"************************************************* \n".format(
str(no_core_box_0)
)
)
log_template_file.write(str(write_log_data))
raise ValueError(str(write_log_data))
if simulation_type in ["GEMC", "GCMC"]:
if isinstance(no_core_box_1, int) is False:
write_log_data = (
"*************************************************\n"
"Enter no_core_box_1 as an interger, no_core_box_1 = {} \n"
"************************************************* \n".format(
str(no_core_box_1)
)
)
log_template_file.write(str(write_log_data))
print(str(write_log_data))
if no_core_box_1 == 0:
write_log_data = (
"*************************************************\n"
"Enter no_core_box_1 as a non-zero number, no_core_box_1 = {} \n"
"************************************************* \n".format(
str(no_core_box_1)
)
)
log_template_file.write(str(write_log_data))
raise ValueError(str(write_log_data))
def calc_folder_zeros(run_number):
"""
Adds zeros to fill in the left side of the run number to total 10 digits.
Parameters
----------
run_number : int
Simulation run number
Returns
---------
add_number_of_zeros_at_start_run_no_str : str
The zeros to add to the left side of the digit run number (run_number)
to total 10 digits with the run number (run_number)
"""
total_run_no_digits = 10
add_no_zeros_at_start_run_no = int(
total_run_no_digits - len(str(run_number))
)
add_number_of_zeros_at_start_run_no_str = ""
for z in range(0, add_no_zeros_at_start_run_no):
add_number_of_zeros_at_start_run_no_str += "0"
return add_number_of_zeros_at_start_run_no_str
# run the GOMC and NAMD simulations
total_sims_namd_gomc = int(2 * total_cycles_namd_gomc_sims)
starting_sims_namd_gomc = int(2 * starting_at_cycle_namd_gomc_sims)
default_namd_e_titles = [
"ETITLE:",
"TS",
"BOND",
"ANGLE",
"DIHED",
"IMPRP",
"ELECT",
"VDW",
"BOUNDARY",
"MISC",
"KINETIC",
"TOTAL",
"TEMP",
"POTENTIAL",
"TOTAL3",
"TEMPAVG",
"PRESSURE",
"GPRESSURE",
"VOLUME",
"PRESSAVG",
"GPRESSAVG",
]
def get_namd_run_0_pme_dim(box_number):
"""
Gets the number of points for the NAMD PME grid in the x, y, and z-dimensions.
It also provides the run 0 (1st NAMD simulation) run directory.
Parameters
----------
box_number : int
The simulation box number, which can only be 0 or 1
Returns
---------
namd_x_pme_grid_dim : int
The number of points for the NAMD PME grid in the x-dimension.
namd_y_pme_grid_dim : int
The number of points for the NAMD PME grid in the y-dimension.
namd_z_pme_grid_dim : int
The number of points for the NAMD PME grid in the z-dimension.
namd_box_x_run_0_dir : str
The run 0 (1st NAMD simulation) run directory.
"""
if isinstance(box_number, int) is False and (
box_number != 0 and box_number != 1
):
raise ValueError(
"ERROR: Enter an interger of 0 or 1 for box_number "
"in the get_namd_run_0_pme_dim function. \n"
)
namd_first_run_no = int(0)
add_zeros_for_box_x_run_0 = calc_folder_zeros(namd_first_run_no)
if box_number == 0:
namd_box_x_run_0_dir = "{}/{}/{}{}_a".format(
str(python_file_directory),
path_namd_runs,
str(add_zeros_for_box_x_run_0),
str(namd_first_run_no),
)
elif box_number == 1:
namd_box_x_run_0_dir = "{}/{}/{}{}_b".format(
str(python_file_directory),
path_namd_runs,
str(add_zeros_for_box_x_run_0),
str(namd_first_run_no),
)
try:
read_namd_box_x_run_0_log_file = open(
"{}/out.dat".format(namd_box_x_run_0_dir), "r"
).readlines()
for i, line in enumerate(read_namd_box_x_run_0_log_file):
split_line = line.split()
if len(split_line) >= 7:
if (
split_line[0] == "Info:"
and split_line[1] == "PME"
and split_line[2] == "GRID"
and split_line[3] == "DIMENSIONS"
):
namd_x_pme_grid_dim = int(split_line[4])
namd_y_pme_grid_dim = int(split_line[5])
namd_z_pme_grid_dim = int(split_line[6])
except:
namd_x_pme_grid_dim = None
namd_y_pme_grid_dim = None
namd_z_pme_grid_dim = None
return (
namd_x_pme_grid_dim,
namd_y_pme_grid_dim,
namd_z_pme_grid_dim,
namd_box_x_run_0_dir,
)
def get_namd_run_0_fft_filename(box_number):
"""
Provides the run 0 (1st NAMD simulation) FFT filename.
Parameters
----------
box_number : int
The simulation box number, which can only be 0 or 1
Returns
---------
namd_box_x_run_0_fft_namd_filename : str
The run 0 (1st NAMD simulation) FFT filename.
"""
if isinstance(box_number, int) is False and (
box_number != 0 and box_number != 1
):
raise ValueError(
"ERROR: Enter an interger of 0 or 1 for box_number in "
"the get_namd_run_0_pme_dim function. \n"
)
namd_first_run_no = int(0)
add_zeros_for_box_x_run_0 = calc_folder_zeros(namd_first_run_no)
if box_number == 0:
namd_box_x_run_0_dir = "{}/{}/{}{}_a".format(
str(python_file_directory),
path_namd_runs,
str(add_zeros_for_box_x_run_0),
str(namd_first_run_no),