-
Notifications
You must be signed in to change notification settings - Fork 10
/
combine_data_NAMD_GOMC.py
2720 lines (2454 loc) · 108 KB
/
combine_data_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 glob
import json
import os
import subprocess
import sys
from warnings import warn
import numpy as np
import pandas as pd
import scipy as sp
# combines the data from the MD/MC simulations (NAMD/GOMC) for the NPT, NVT, GEMC, and GCMC ensembles
# or just combines the log files for the NAMD-only or GOMC-only simulations.
# *************************************************
# 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="str. Defines the variable inputs to the hybrid NAMD/GOMC, NAMD-only, or GOMC-only "
"log or consol output combining scipt.",
type=str,
)
# name the file folder in which the data will be combined.
arg_parser.add_argument(
"-w",
"--write_folder_name",
help="str. Defines the folder name in which the files containing the output variables "
"will be combined and written in.",
type=str,
)
# name the file folder in which the data will be combined.
arg_parser.add_argument(
"-o",
"--overwrite",
help="bool (True, true, T, t, False, false, F, or f). Overwrites the folder in which "
"the output variables will be combined and added in, if the file already exists.",
type=str,
default="False",
)
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:
raise FileNotFoundError(
"ERROR: Console file <{}> does not exist!".format(
parser_arguments.file
)
)
else:
raise IOError(
"ERROR: The user input file was not specified as -f or --file"
)
# set check if overwrite is set to true, if not set to False
if parser_arguments.overwrite in ["False", "false", "F", "f"]:
print("INFO: By default, the combining folder will not be overwritten.")
parser_arguments.overwrite = False
elif parser_arguments.overwrite in ["True", "true", "T", "t"]:
print("INFO: The combining folder will be overwritten.")
parser_arguments.overwrite = True
else:
raise TypeError("ERROR: -o or --overwrite flag is not a boolean.")
# set check if the combining data file folder name is provided
if isinstance(parser_arguments.write_folder_name, str) is True:
print(
"INFO: The combining folder is named <{}>.".format(
parser_arguments.write_folder_name
)
)
else:
raise TypeError(
"ERROR: The -w or --write_folder_name flag was not provided. This flag defines "
"the folder name which is created and will contain the combined data."
)
# check to see if the folder exists
if (
os.path.exists(parser_arguments.write_folder_name)
and parser_arguments.overwrite is False
):
raise IOError(
"ERROR: The file folder <{}> already exists. If you want to overwrite it, set the "
"-o or --overwrite flag as True.".format(
parser_arguments.write_folder_name
)
)
elif (
os.path.exists(parser_arguments.write_folder_name)
and parser_arguments.overwrite is True
):
print(
"INFO: The combining folder <{}> will be overwritten."
"".format(parser_arguments.write_folder_name)
)
return [
parser_arguments.file,
parser_arguments.write_folder_name,
parser_arguments.overwrite,
]
# *************************************************
# 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, write_folder_name, overwrite_folder] = _get_args()
print("arg_parser.file = {}".format(json_filename))
print("parser_arguments.write_folder_name = {}".format(write_folder_name))
print("parser_arguments.overwrite = {}".format(overwrite_folder))
json_file_data = json.load(open(json_filename))
# json_file_data = json.load(open("user_input_combine_data_NAMD_GOMC.json"))
json_file_data_keys_list = json_file_data.keys()
# *************************************************
# Changeable variables (start)
# *************************************************
# 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 only_use_box_0_for_namd_for_gemc variable from the json file
# string 'Hybrid', 'NAMD-only', 'GOMC-only' , 'Hybrid need to be in the directory
# with the 'NAMD' and 'GOMC' directories
if "simulation_engine_options" not in json_file_data_keys_list:
raise TypeError("The simulation_engine_options key is not provided.\n")
simulation_engine_options = json_file_data["simulation_engine_options"]
if simulation_engine_options not in ["Hybrid", "GOMC-only", "NAMD-only"]:
raise TypeError(
'The simulation_engine_options are only "Hybrid", "GOMC-only", or "NAMD-only", '
"which are entered in the json file.\n"
)
# get the gomc_or_namd_only_log_filename variable from the json file
if "gomc_or_namd_only_log_filename" not in json_file_data_keys_list:
raise TypeError("The gomc_or_namd_only_log_filename key is not provided.\n")
gomc_or_namd_only_log_filename = json_file_data[
"gomc_or_namd_only_log_filename"
]
if not isinstance(gomc_or_namd_only_log_filename, str):
raise TypeError(
"The gomc_or_namd_only_log_filename must be entered as a string in the json file.\n"
)
# get the combine_namd_dcd_file variable from the json file
if "combine_namd_dcd_file" not in json_file_data_keys_list:
raise TypeError("The combine_namd_dcd_file key is not provided.\n")
combine_namd_dcd_file = json_file_data["combine_namd_dcd_file"]
if combine_namd_dcd_file is not True and combine_namd_dcd_file is not False:
raise TypeError(
"The combine_namd_dcd_file not true or false in the json file.\n"
)
# get the combine_gomc_dcd_file variable from the json file
if "combine_gomc_dcd_file" not in json_file_data_keys_list:
raise TypeError("The combine_gomc_dcd_file key is not provided.\n")
combine_gomc_dcd_file = json_file_data["combine_gomc_dcd_file"]
if combine_gomc_dcd_file is not True and combine_gomc_dcd_file is not False:
raise TypeError(
"The combine_gomc_dcd_file not true or false in the json file.\n"
)
# get the combine_dcd_files_cycle_freq variable from the json file
if "combine_dcd_files_cycle_freq" not in json_file_data_keys_list:
raise TypeError("The combine_dcd_files_cycle_freq key is not provided.\n")
combine_dcd_files_cycle_freq = json_file_data["combine_dcd_files_cycle_freq"]
if not isinstance(combine_dcd_files_cycle_freq, int):
raise TypeError(
"The combine_dcd_files_cycle_freq values must be an integer.\n"
)
else:
if combine_dcd_files_cycle_freq < 1:
raise TypeError(
"The combine_dcd_files_cycle_freq values must be an integer "
"greater than or equal to zero (>=1.\n"
)
# get the get_initial_gomc_dcd variable from the json file
if "get_initial_gomc_dcd" not in json_file_data_keys_list:
raise TypeError("The get_initial_gomc_dcd key is not provided.\n")
get_initial_gomc_dcd = json_file_data["get_initial_gomc_dcd"]
if get_initial_gomc_dcd is not True and get_initial_gomc_dcd is not False:
raise TypeError(
"The get_initial_gomc_dcd not true or false in the json file.\n"
)
# get the rel_path_to_combine_binary_catdcd variable from the json file
if "rel_path_to_combine_binary_catdcd" not in json_file_data_keys_list:
raise TypeError(
"The rel_path_to_combine_binary_catdcd key is not provided.\n"
)
rel_path_to_combine_binary_catdcd = json_file_data[
"rel_path_to_combine_binary_catdcd"
]
if not isinstance(rel_path_to_combine_binary_catdcd, str):
raise TypeError(
"The rel_path_to_combine_binary_catdcd must be entered as a string in the json file.\n"
)
warn(
"If the combining file fails without a set reason/error, please check the "
'"simulation_engine_options", "out.dat", and the "rel_path_to_combine_binary_catdcd" variables. '
"The user needs to ensure that these are proper paths and file types, since these variables "
"are currently only checked to confirm they are strings."
)
# *************************************************
# Changable variables (end)
# *************************************************
# *************************************************
# NAMD and GOMC folders name and create combined_data folder(start)
# *************************************************
python_file_directory = os.getcwd()
path_namd_runs = "NAMD"
path_gomc_runs = "GOMC"
path_combined_data_folder = write_folder_name
os.makedirs(path_combined_data_folder, exist_ok=True)
full_path_to_namd_data_folder = (
python_file_directory + "/" + str(path_namd_runs)
)
full_path_to_gomc_data_folder = (
python_file_directory + "/" + str(path_gomc_runs)
)
full_path_to_combined_data_folder = (
python_file_directory + "/" + str(path_combined_data_folder)
)
# *************************************************
# create NAMD and GOMC folders (end)
# *************************************************
K_to_kcal_mol = 1.98720425864083 * 10 ** (-3)
# get the number of directories in each NAMD or GOMC folder. Note: for the NAMD simulations, '
# the data is split between the liquid box (dir ending in '_a'), and the vapor box (dir ending in '_b')
if simulation_engine_options == "Hybrid":
if (
simulation_type in ["GEMC"]
and only_use_box_0_for_namd_for_gemc is False
):
namd_directory_a_list = []
namd_directory_b_list = []
total_namd_directory_list = sorted(
os.listdir(python_file_directory + "/" + str(path_namd_runs))
)
no_total_namd_directory_list = len(total_namd_directory_list)
for i in range(0, len(total_namd_directory_list)):
namd_directory_iter = str(total_namd_directory_list[i])
if namd_directory_iter[-2:] == "_a":
namd_directory_a_list.append(namd_directory_iter)
elif namd_directory_iter[-2:] == "_b":
namd_directory_b_list.append(namd_directory_iter)
namd_directory_a_list = sorted(namd_directory_a_list)
namd_directory_b_list = sorted(namd_directory_b_list)
no_namd_directory_a = len(namd_directory_a_list)
no_namd_directory_b = len(namd_directory_b_list)
if (
no_namd_directory_a + no_namd_directory_b
!= no_total_namd_directory_list
):
warn(
"WARNING: The total NAMD directories does not match the '..._a (NAMD liq box"
"..._b (NAMD vap box)' directories"
)
else:
namd_directory_a_list = []
total_namd_directory_list = sorted(
os.listdir(python_file_directory + "/" + str(path_namd_runs))
)
no_total_namd_directory_list = len(total_namd_directory_list)
for i in range(0, len(total_namd_directory_list)):
namd_directory_iter = str(total_namd_directory_list[i])
if namd_directory_iter[-2:] == "_a":
namd_directory_a_list.append(namd_directory_iter)
namd_directory_a_list = sorted(namd_directory_a_list)
no_namd_directory_a = len(namd_directory_a_list)
if no_namd_directory_a != no_total_namd_directory_list:
print(
"WARNING: The total NAMD directories does not match the '..._a (NAMD liq box)' directories"
)
gomc_directory_list = sorted(
os.listdir(python_file_directory + "/" + str(path_gomc_runs))
)
no_gomc_directory = len(gomc_directory_list)
# confirm there are the same number of directories for GOMC and NAMD (vap and liq , [i.e., _a and _b])
try:
no_namd_directory_b
if (
no_gomc_directory != no_namd_directory_a
or no_gomc_directory != no_namd_directory_b
):
warn(
"WARNING: The total NAMD directories and GOMC directories does not match!"
)
except:
if no_gomc_directory != no_namd_directory_a:
warn(
"WARNING: The total NAMD directories and GOMC directories does not match!"
)
if (
simulation_type in ["GEMC"]
and only_use_box_0_for_namd_for_gemc is False
):
try:
namd_interation = str(namd_directory_b_list[namd_interation])
except:
raise ValueError(
"Box 1 data does not exist for NAMD, maybe use "
"only_use_box_0_for_namd_for_gemc == True"
)
# get the total number of simulation = 2 * number of cycles or 1 * number of GOMC directories
total_sims_namd_gomc = int(2 * no_gomc_directory)
# set all the filenames and paths
if simulation_engine_options in ["Hybrid", "NAMD-only"]:
namd_box_0_data_filename = "{}/{}".format(
full_path_to_combined_data_folder, "NAMD_data_box_0.txt"
)
if (
simulation_type in ["GEMC"]
and only_use_box_0_for_namd_for_gemc is False
):
namd_box_1_data_filename = "{}/{}".format(
full_path_to_combined_data_folder, "NAMD_data_box_1.txt"
)
namd_box_0_data_density_filename = "{}/{}".format(
full_path_to_combined_data_folder, "NAMD_data_density_box_0.txt"
)
if (
simulation_type in ["GEMC"]
and only_use_box_0_for_namd_for_gemc is False
):
namd_box_1_data_density_filename = "{}/{}".format(
full_path_to_combined_data_folder, "NAMD_data_density_box_1.txt"
)
if simulation_engine_options in ["Hybrid", "GOMC-only"]:
gomc_box_0_data_filename = "{}/{}".format(
full_path_to_combined_data_folder, "GOMC_data_box_0.txt"
)
gomc_box_0_energies_stat_filename = "{}/{}".format(
full_path_to_combined_data_folder, "GOMC_Energies_Stat_box_0.txt"
)
gomc_box_0_energies_stat_kcal_per_mol_filename = "{}/{}".format(
full_path_to_combined_data_folder,
"GOMC_Energies_Stat_kcal_per_mol_box_0.txt",
)
if simulation_type in ["GEMC", "GCMC"]:
gomc_box_1_data_filename = "{}/{}".format(
full_path_to_combined_data_folder, "GOMC_data_box_1.txt"
)
gomc_box_1_energies_stat_filename = "{}/{}".format(
full_path_to_combined_data_folder, "GOMC_Energies_Stat_box_1.txt"
)
gomc_box_1_energies_stat_kcal_per_mol_filename = "{}/{}".format(
full_path_to_combined_data_folder,
"GOMC_Energies_Stat_kcal_per_mol_box_1.txt",
)
gomc_box_0_data_file = open(gomc_box_0_data_filename, "w")
if simulation_type in ["GEMC"]:
gomc_box_1_data_file = open(gomc_box_1_data_filename, "w")
gomc_box_0_hist_filename = "{}/{}".format(
full_path_to_combined_data_folder, "GOMC_hist_data_box_0.txt"
)
if simulation_type in ["GCMC"]:
gomc_box_0_hist_file = open(gomc_box_0_hist_filename, "w")
if simulation_engine_options in ["Hybrid", "NAMD-only"]:
namd_box_0_data_file = open(namd_box_0_data_filename, "w")
if (
simulation_type in ["GEMC"]
and only_use_box_0_for_namd_for_gemc is False
):
namd_box_1_data_file = open(namd_box_1_data_filename, "w")
e_values_namd_box_1_density_list = []
if simulation_engine_options in ["Hybrid"]:
combined_box_0_filename_only = "combined_NAMD_GOMC_data_box_0.txt"
combined_box_1_filename_only = "combined_NAMD_GOMC_data_box_1.txt"
elif simulation_engine_options in ["GOMC-only"]:
combined_box_0_filename_only = "combined_GOMC_data_box_0.txt"
combined_box_1_filename_only = "combined_GOMC_data_box_1.txt"
elif simulation_engine_options in ["NAMD-only"]:
combined_box_0_filename_only = "combined_NAMD_data_box_0.txt"
combined_box_1_filename_only = "combined_NAMD_data_box_1.txt"
combined_box_0_data_filename = "{}/{}".format(
full_path_to_combined_data_folder, combined_box_0_filename_only
)
combined_box_1_data_filename = "{}/{}".format(
full_path_to_combined_data_folder, combined_box_1_filename_only
)
if simulation_engine_options in ["Hybrid"]:
# starting point for the GOMC dist dicts for GCMC only
if simulation_type in ["GCMC"]:
full_path_dist_files_only_run_0_list = glob.glob(
"{}/{}/{}/{}".format(
python_file_directory,
path_gomc_runs,
gomc_directory_list[0],
"*dis1a.dat",
)
)
no_dist_files_only_run_0_list = len(
full_path_dist_files_only_run_0_list
)
dict_of_current_dist_dicts = {}
for i in range(0, no_dist_files_only_run_0_list):
dict_of_current_dist_dicts.update({i + 1: {}})
def get_namd_log_data(
read_namd_box_x_log_file,
namd_box_x_data_file,
run_no,
e_values_namd_box_x_density_list=None,
e_titles_namd_box_x_iteration=None,
e_titles_namd_box_x_density_iteration=None,
):
"""
Extracts the NAMD log data from every run (energy and state properties),
combining it into a list while adding the system density,
and also keeping the exact output lines from NAMD.
Parameters
----------
read_namd_box_x_log_file : readable opened file with .readlines()
The read loaded namd log file with .readlines().
namd_box_x_data_file : writeable opened file
The writeable opened file, which is used to write the combined
and compact data from the NAMD log file.
run_no : int
Simulation run number
e_values_namd_box_x_density_list : list, default = []
The NAMD log data output with the density added.
If the default value (empty list) is not used, the
provided list will be appended. This needs provided not
reading 1st NAMD simulation.
e_titles_namd_box_x_iteration : list, default = None
The titles of the NAMD energy and state data from the systems log file.
This needs provided not reading 1st NAMD simulation.
e_titles_namd_box_x_density_iteration : list, default = None
The titles of the NAMD energy and state data from the systems log file,
adding a # sybol on the first entry and 'DENSITY' at the end of the
list. This needs provided not reading 1st NAMD simulation.
Returns
---------
e_titles_namd_box_x_iteration : list
The titles of the NAMD energy and state data from the systems log file.
e_titles_namd_box_x_density_iteration : list
The titles of the NAMD energy and state data from the systems log file,
adding a # sybol on the first entry and 'DENSITY' at the end of the
list. This needs provided not reading 1st NAMD simulation.
e_values_namd_box_x_iteration : list
The NAMD log data output with the density added.
e_values_namd_box_x_density_list : list
The reorganized and combined NAMD log data from the current NAMD run,
and the past runs that were entered via the flag ().
e_property_values_namd_box_x_list list:
The NAMD log data from the current NAMD run, which does not
include the calculated density value. This includes the first item
in the list being 'ENERGY:'.
Notes
--------
The timesteps are rescaled so that the NAMD and GOMC data are added
properly in order.
The 2 timestep 0 values will appear if system is minimized.
The minimization timesteps are rescaled to 0 for the actual run start,
not minimization.
"""
error_for_bad_namd_input_file_data = (
"ERROR: This NAMD file output file does not contain all "
"the required information. The Energy titles/values "
"are missing from the NAMD output/log file(s) in "
"run number {}.".format(run_no)
)
if e_values_namd_box_x_density_list is None:
e_values_namd_box_x_density_list = []
else:
e_values_namd_box_x_density_list
for i, line in enumerate(read_namd_box_x_log_file):
split_line = line.split()
if line.startswith("Info:") is True:
if split_line[0] == "Info:" and len(split_line) >= 6:
if (
split_line[1] == "TOTAL"
and split_line[2] == "MASS"
and split_line[3] == "="
):
namd_sim_total_mass_box_x_amu = float(split_line[4])
if split_line[0] == "Info:" and len(split_line) >= 5:
if (
split_line[1] == "ENERGY"
and split_line[2] == "OUTPUT"
and split_line[3] == "STEPS"
):
namd_sim_e_output_steps_box_x = float(split_line[4])
# note NAMD energy units in kcal/mol (no modifications required)
# generate energy file data for box X
if run_no == 0:
get_e_namd_box_x_titles = True
else:
get_e_namd_box_x_titles = False
e_property_values_namd_box_x_list = []
for i, line in enumerate(read_namd_box_x_log_file):
if (
line.startswith("ETITLE:") is True
and get_e_namd_box_x_titles is True
):
namd_box_x_data_file.write("%s" % read_namd_box_x_log_file[i])
e_titles_namd_box_x_iteration = read_namd_box_x_log_file[i]
e_titles_namd_box_x_iteration = (
e_titles_namd_box_x_iteration.split()
)
e_titles_namd_box_x_iteration = e_titles_namd_box_x_iteration[:]
e_titles_namd_box_x_density_iteration = read_namd_box_x_log_file[i]
e_titles_namd_box_x_density_iteration = (
e_titles_namd_box_x_density_iteration.split()
)
e_titles_namd_box_x_density_iteration = (
e_titles_namd_box_x_density_iteration[1:]
)
e_titles_namd_box_x_density_iteration.append("DENSITY")
if get_e_namd_box_x_titles is True:
get_e_namd_box_x_titles = False
if line.startswith("ENERGY:") is True:
e_values_namd_box_x_iteration = read_namd_box_x_log_file[i]
e_values_namd_box_x_iteration = (
e_values_namd_box_x_iteration.split()
)
e_values_namd_box_x_iteration = e_values_namd_box_x_iteration[:]
for y in range(0, len(e_titles_namd_box_x_iteration)):
if e_titles_namd_box_x_iteration[y] == "TS":
e_values_namd_box_x_iteration[y] = str(
int(
int(e_values_namd_box_x_iteration[y]) + current_step
)
)
array_ts_location = y
elif e_titles_namd_box_x_iteration[y] == "VOLUME":
e_volume_data_namd_box_x_iteration = float(
e_values_namd_box_x_iteration[y]
)
e_density_data_namd_box_x_iteration = (
1.6605402
* namd_sim_total_mass_box_x_amu
/ e_volume_data_namd_box_x_iteration
)
if int(e_values_namd_box_x_iteration[array_ts_location]) >= 0:
namd_box_x_data_file.write(
"%s \n" % str("\t ".join(e_values_namd_box_x_iteration))
)
e_property_values_namd_box_x_list.append(
e_values_namd_box_x_iteration
)
e_values_namd_box_x_density_iteration = (
e_values_namd_box_x_iteration[1:]
)
e_values_namd_box_x_density_iteration.append(
e_density_data_namd_box_x_iteration
)
e_values_namd_box_x_density_list.append(
e_values_namd_box_x_density_iteration
)
if (
e_titles_namd_box_x_iteration is None
or e_titles_namd_box_x_density_iteration is None
) and run_no != 0:
warn(
"enter the e_titles_namd_box_x_iteration variable in the get_namd_log_data, as "
" it is not in the NAMD log file to read."
)
else:
e_titles_namd_box_x_iteration = e_titles_namd_box_x_iteration
e_titles_namd_box_x_density_iteration = (
e_titles_namd_box_x_density_iteration
)
# add a pound symbol (#) before some of the titles
if str(e_titles_namd_box_x_density_iteration[0][0]) != "#":
e_titles_namd_box_x_density_iteration[0] = "#" + str(
e_titles_namd_box_x_density_iteration[0]
)
try:
isinstance(e_titles_namd_box_x_iteration, list)
isinstance(e_titles_namd_box_x_density_iteration, list)
isinstance(e_values_namd_box_x_iteration, list)
isinstance(e_values_namd_box_x_density_list, list)
isinstance(e_property_values_namd_box_x_list, list)
except:
raise ValueError(error_for_bad_namd_input_file_data)
return (
e_titles_namd_box_x_iteration,
e_titles_namd_box_x_density_iteration,
e_values_namd_box_x_iteration,
e_values_namd_box_x_density_list,
e_property_values_namd_box_x_list,
)
def get_gomc_hist_data(read_gomc_box_0_hist_file, gomc_box_0_hist_file, run_no):
"""
This reads in the current simulation histogram (hist) data
and appends it add to a histrogram file, which was created
when the first GOMC simulation was read. In the end, this
function writes and combines all the hist data from all
the GOMC runs.
Parameters
----------
read_gomc_box_0_hist_file : readable opened file with .readlines()
The read loaded GOMC hist file with .readlines().
gomc_box_0_hist_file : writeable opened file
The writeable opened file, which is used to write the combined
and compact data from the GOMC hist files.
run_no : int
Simulation run number
"""
# create the combined histogram
for i, line in enumerate(read_gomc_box_0_hist_file):
# if the first iteration for the GOMC hist file print the 1st line
if run_no == 1 and i == 0:
gomc_box_0_hist_file.write("%s" % read_gomc_box_0_hist_file[i])
elif run_no >= 1 and i >= 1:
gomc_box_0_hist_file.write("%s" % read_gomc_box_0_hist_file[i])
def get_gomc_dist_data(read_gomc_box_0_dist_file, current_dist_dict):
"""
This reads in the current simulation distribution (dist) data
and adds it to the current dist data dictionary which is also
and input variable.
Parameters
----------
read_gomc_box_0_dist_file : readable opened file with .readlines()
The read loaded GOMC dist file with .readlines().
current_dist_dict : the current dist dictionary while reading
thru all the files. This is an empty dictionary when the first GOMC
simulation is started (i.e. simulation 1). The data is added to the
dictionary every time the next GOMC distribution (dist) data is
input here and the new data is read (see current_dist_dict
definition in the return variables).
Returns
--------
current_dist_dict : the updated current dist dictionary after reading
and adding the current simulations distribution (dist) data.
"""
# create the combined distribution dictionary
for i, line in enumerate(read_gomc_box_0_dist_file):
split_line = line.split()
new_key = split_line[0]
new_value = split_line[1]
if new_key in current_dist_dict.keys():
current_value = current_dist_dict[new_key]
new_value = str(int(current_value) + int(new_value))
current_dist_dict.update({new_key: new_value})
else:
current_dist_dict.update({new_key: new_value})
return current_dist_dict
def get_gomc_log_data(
read_gomc_box_x_log_file,
gomc_box_x_data_file,
run_no,
box_no,
e_stat_values_gomc_box_x_list=None,
e_stat_values_gomc_kcal_per_mol_box_x_list=None,
):
"""
Extracts the GOMC log data from every run (energy and state properties),
combining it into a list while adding the system density,
and also keeping the exact output lines from NAMD.
Parameters
----------
read_gomc_box_x_log_file : readable opened file with .readlines()
The read loaded namd log file with .readlines().
gomc_box_x_data_file : writeable opened file
The writeable opened file, which is used to write the combined
and compact data from the GOMC log file.
run_no : int
Simulation run number
box_no : int
The simulation box number, which can only be 0 or 1
e_stat_values_gomc_box_x_list : list, default = []
The GOMC log data (energy and stat values) output in the
standard GOMC units (energy = K and density = kg/m^3).
If the default value (empty list) is not used, the
provided list will be appended. This needs provided not
reading 1st GOMC simulation.
e_stat_values_gomc_kcal_per_mol_box_x_list : list, default = None
The GOMC log data (energy and stat values) output in the
NAMD units (energy = kcal/mol and density = g/cm^3).
If the default value (empty list) is not used, the
provided list will be appended. This needs provided not
reading 1st GOMC simulation.
Returns
--------
e_titles_gomc_box_x_iteration : list
The energy titles of the GOMC energy data from the systems,
log file in GOMC units (energy = K and density = kg/m^3).
e_stat_titles_gomc_box_x_iter_list : list
The energy titles of the GOMC energy data from the systems
log file in GOMC units (energy = K and density = kg/m^3).
e_titles_kcal_per_mol_stat_titles_gomc_box_x_iter_list :
The scaled (NAMD units: energy = kcal/mol and density = g/cm^3 ),
reorgainized, and combined titles from the GOMC log file.
e_stat_values_gomc_box_x_iteration_list :
The GOMC log file data output from this iteration
(GOMC units:energy = K and density = kg/m^3).
e_stat_values_gomc_kcal_per_mol_box_x_list :
The scaled (NAMD units: energy = kcal/mol and density = g/cm^3 ),
reorgainized, and combined data from the GOMC log file data output
from the flag input and whatever is added in this iteration.
Notes
--------
The timesteps are rescaled so that the NAMD and GOMC data are added
properly in order.
"""
error_for_bad_gomc_input_file_data = (
"ERROR: This GOMC file output file does not contain all "
"the required information. The Energy and Stat titles/values "
"are missing from the GOMC output/log file in "
"run number {} in box {}.".format(run_no, box_no)
)
if e_stat_values_gomc_box_x_list is None:
e_stat_values_gomc_box_x_list = []
else:
e_stat_values_gomc_box_x_list
if e_stat_values_gomc_kcal_per_mol_box_x_list is None:
e_stat_values_gomc_kcal_per_mol_box_x_list = []
else:
e_stat_values_gomc_kcal_per_mol_box_x_list
energy_string_label = "ENER_{}:".format(str(box_no))
stat_string_label = "STAT_{}:".format((box_no))
get_e_gomc_box_x_titles = True
get_stat_box_x_titles = True
stat_values_box_x_list = []
for i, line in enumerate(read_gomc_box_x_log_file):
if (
line.startswith("ETITLE:") is True
and get_e_gomc_box_x_titles is True
):
if run_no == 1:
gomc_box_x_data_file.write("%s" % read_gomc_box_x_log_file[i])
e_titles_gomc_box_x_iteration = read_gomc_box_x_log_file[i]
e_titles_gomc_box_x_iteration = (
e_titles_gomc_box_x_iteration.split()
)
e_titles_gomc_box_x_iteration = e_titles_gomc_box_x_iteration[:]
if get_e_gomc_box_x_titles is True:
get_e_gomc_box_x_titles = False
if line.startswith(energy_string_label) is True:
e_values_gomc_box_x_iteration_list = []
e_values_kcal_per_mol_gomc_box_x_iteration_list = []
e_values_gomc_box_x_iteration = read_gomc_box_x_log_file[i]
e_values_gomc_box_x_iteration = (
e_values_gomc_box_x_iteration.split()
)
for j in range(0, len(e_values_gomc_box_x_iteration)):
if e_titles_gomc_box_x_iteration[j] == "ETITLE:":
e_values_gomc_box_x_iteration_list.append(
e_values_gomc_box_x_iteration[j]
)
e_values_kcal_per_mol_gomc_box_x_iteration_list.append(
e_values_gomc_box_x_iteration[j]
)
elif e_titles_gomc_box_x_iteration[j] == "STEP":
e_values_gomc_box_x_iteration_list.append(
str(
int(
int(e_values_gomc_box_x_iteration[j])
+ current_step
)
)
)
e_values_kcal_per_mol_gomc_box_x_iteration_list.append(
str(
int(
int(e_values_gomc_box_x_iteration[j])
+ current_step
)
)
)
else:
e_values_gomc_box_x_iteration_list.append(
str(float(e_values_gomc_box_x_iteration[j]) * 1)
)
e_values_kcal_per_mol_gomc_box_x_iteration_list.append(
str(
float(e_values_gomc_box_x_iteration[j])
* K_to_kcal_mol
)
)
gomc_box_x_data_file.write(
"%s \n" % str("\t ".join(e_values_gomc_box_x_iteration_list))
)
# GOMC_box_X_Energies_Stat_file
if line.startswith("STITLE:") is True and get_stat_box_x_titles is True:
if run_no == 1:
gomc_box_x_data_file.write("%s" % read_gomc_box_x_log_file[i])
stat_titles_box_x_iteration = read_gomc_box_x_log_file[i]
stat_titles_box_x_iteration = stat_titles_box_x_iteration.split()
stat_titles_box_x_iteration = stat_titles_box_x_iteration[:]
if get_stat_box_x_titles is True:
get_stat_box_x_titles = False
if line.startswith(stat_string_label) is True:
stat_values_box_x_iteration_list = []
stat_values_box_x_iteration = read_gomc_box_x_log_file[i]
stat_values_box_x_iteration = stat_values_box_x_iteration.split()
for j in range(0, len(stat_values_box_x_iteration)):
if stat_titles_box_x_iteration[j] == "STITLE:":
stat_values_box_x_iteration_list.append(
stat_values_box_x_iteration[j]
)
elif stat_titles_box_x_iteration[j] == "STEP":
stat_values_box_x_iteration_list.append(
str(
int(
int(stat_values_box_x_iteration[j])
+ current_step
)
)
)
# stat_values_box_x_iteration_list.append(int(int(stat_values_box_x_iteration[j]) \
# + current_step))
else:
stat_values_box_x_iteration_list.append(
str(float(stat_values_box_x_iteration[j]))
)
gomc_box_x_data_file.write(
"%s \n" % str("\t ".join(stat_values_box_x_iteration_list))
)
stat_values_box_x_list.append(stat_values_box_x_iteration_list)
# combine the energy and stat data (ENER_X and STAT_X) into 1 line and remove ENER_X and STAT_X) labels
e_stat_values_gomc_box_x_iteration_list = (
e_values_gomc_box_x_iteration_list[1:]
)
e_stat_values_gomc_kcal_per_mol_box_x_iteration_list = (
e_values_kcal_per_mol_gomc_box_x_iteration_list[1:]
)
for z in range(2, len(stat_values_box_x_iteration_list)):
e_stat_values_gomc_box_x_iteration_list.append(
stat_values_box_x_iteration_list[z]
)
if stat_titles_box_x_iteration[z] == "TOT_DENSITY":
e_stat_values_gomc_kcal_per_mol_box_x_iteration_list.append(
str(float(stat_values_box_x_iteration_list[z]) / 10**3)
)
else:
e_stat_values_gomc_kcal_per_mol_box_x_iteration_list.append(
stat_values_box_x_iteration_list[z]
)
e_stat_values_gomc_box_x_list.append(
e_stat_values_gomc_box_x_iteration_list
)
e_stat_values_gomc_kcal_per_mol_box_x_list.append(
e_stat_values_gomc_kcal_per_mol_box_x_iteration_list
)
# combine the energy and stat titles (ENER_X and STAT_X) into 1 line and remove ENER_X and STAT_X) labels
try:
e_stat_titles_gomc_box_x_iter_list = e_titles_gomc_box_x_iteration[1:]
except:
raise ValueError(error_for_bad_gomc_input_file_data)
try:
e_titles_kcal_per_mol_stat_titles_gomc_box_x_iter_list = (
e_titles_gomc_box_x_iteration[1:]
)
except:
raise ValueError(error_for_bad_gomc_input_file_data)
try:
isinstance(stat_titles_box_x_iteration, list)
except:
raise ValueError(error_for_bad_gomc_input_file_data)
for z in range(2, len(stat_titles_box_x_iteration)):
e_stat_titles_gomc_box_x_iter_list.append(
stat_titles_box_x_iteration[z]
)
e_titles_kcal_per_mol_stat_titles_gomc_box_x_iter_list.append(
stat_titles_box_x_iteration[z]
)
# add a pound symbol (#) before some of the titles
if str(e_stat_titles_gomc_box_x_iter_list[0][0]) != "#":
e_stat_titles_gomc_box_x_iter_list[0] = "#" + str(
e_stat_titles_gomc_box_x_iter_list[0]
)
if (
str(e_titles_kcal_per_mol_stat_titles_gomc_box_x_iter_list[0][0])
!= "#"