-
Notifications
You must be signed in to change notification settings - Fork 1
/
single_file_run.py
1835 lines (1666 loc) · 107 KB
/
single_file_run.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 numpy as np
import ast
import os
import copy
import datetime as dt
import sys
from itertools import product
from scipy.optimize import curve_fit
class Gaussian_File:
def __init__(self, file_name = "name.inp", keywords = "", nproc=False, mem=False, title="Job Name", oldchk=False, oldchk_file=None, chk=False, chk_name=False,
charge_multiplicity=(0, 1), geom=False, basis_set=False, wfx=False, Field=False):
self.file_name = file_name
self.keywords = keywords
self.nproc = nproc
self.mem = mem
self.title = title
self.oldchk = oldchk
self.oldchk_file = oldchk_file
self.chk = chk
self.chk_name = chk_name
self.charge_multiplicity = charge_multiplicity
self.geom = False
self.basis_set_gaussian = basis_set
self.wfx = wfx
self.Field = Field
class Fchk_File():
"""
This class is used to store the data from a fchk file.
"""
def __init__(self, name = False, e_field = False, energy = False, dipole_moment = False, polarizability = False, hyperpolarizability = False, quadrupole_moment = False):
self.name = name
self.e_field = e_field
self.energy = energy
self.dipole_moment = dipole_moment
self.polarizability = polarizability
self.hyperpolarizability = hyperpolarizability
self.quadrupole_moment = quadrupole_moment
def list_propreties(self, directions):
"""
This function returns a list of propreties of the object if they are present.
The input is a list of directions for which to return the propreties.
"""
map_directions_1 = {"x" : 0, "y" : 1, "z" : 2, "xx" : 0, "yy" : 2, "zz" : 5, "xxx" : 0, "yyy" : 3, "zzz" : 9}
map_dipole = {"x" : 0, "y" : 1, "z" : 2}
map_polarizability = {"xx" : 0, "xy" : 1, "yy" : 2, "xz" : 3, "yz" : 4, "zz" : 5}
map_hyperpolarizability = {"xxx" : 0, "xxy" : 1, "xyy" : 2, "yyy" : 3, "xxz" : 4, "xyz" : 5, "yyz" : 6, "xzz" : 7, "yzz" : 8, "zzz" : 9}
main_directions = [] #if direction x, y or z is specified, it will be saved here, later to print also the xx, yy, zz, xxx, yyy, zzz
secondary_directions = [] #All other directions which will be printed only once
for char in directions: #Separating the directions in the input file
if char.lower() in map_dipole:
main_directions.append(char)
else: secondary_directions.append(char)
new_list = [["Name", "E_Field_X", "E_Field_Y", "E_Field_Z", "Energy", "Dipole_Moment", "Polarizability", "Hyperpolarizability"]]
new_list.append([]) #Creating the list in the form [[*names], [*values]
new_list[1].append(self.name)
new_list[1].extend(self.e_field)
new_list[1].append(self.energy)
if self.dipole_moment:
count = 0
position = new_list[0].index("Dipole_Moment")
del new_list[0][position]
for i in main_directions: #Printing it for all the specified main directions
new_list[0].insert(position + count, "Dipole_Moment_" + i.lower())
new_list[1].append(self.dipole_moment[map_directions_1[i.lower()]])
count += 1
else:
count = 0
position = new_list[0].index("Dipole_Moment")
del new_list[0][position]
for i in main_directions: #Printing it for all the specified main directions
new_list[0].insert(position + count, "Dipole_Moment_" + i.lower())
new_list[1].append(np.NaN)
count += 1
print("Dipole_Moment is not present in the file" + self.name)
if self.polarizability:
count = 0
position = new_list[0].index("Polarizability")
del new_list[0][position]
for i in main_directions: #Printing it for all the specified main directions
new_list[0].insert(position + count, "Polarizability_" + 2*i.lower())
new_list[1].append(self.polarizability[map_directions_1[2*i.lower()]])
count += 1
if secondary_directions: #Printing it for all the specified secondary directions
for i in secondary_directions:
if i.lower() in map_polarizability:
new_list[0].insert(position + count, "Polarizability_" + i.lower())
new_list[1].append(self.polarizability[map_polarizability[i.lower()]])
count += 1
else:
count = 0
position = new_list[0].index("Polarizability")
del new_list[0][position]
for i in main_directions: #Printing it for all the specified main directions
new_list[0].insert(position + count, "Polarizability_" + 2*i.lower())
new_list[1].append(np.NaN)
count += 1
print("Polarizability is not present in the file" + self.name)
if self.hyperpolarizability:
count = 0
position = new_list[0].index("Hyperpolarizability")
del new_list[0][position]
for i in main_directions:
new_list[0].insert(position + count, "Hyperpolarizability_" + 3*i.lower())
new_list[1].append(self.hyperpolarizability[map_directions_1[3*i.lower()]])
count += 1
if secondary_directions:
for i in secondary_directions:
if i.lower() in map_hyperpolarizability:
new_list[0].insert(position + count, "Hyperpolarizability_" + i.lower())
new_list[1].append(self.hyperpolarizability[map_hyperpolarizability[i.lower()]])
count += 1
else:
count = 0
position = new_list[0].index("Hyperpolarizability")
del new_list[0][position]
for i in main_directions:
new_list[0].insert(position + count, "Hyperpolarizability_" + 3*i.lower())
new_list[1].append(np.NaN)
count += 1
print("Hyperpolarizability is not present in the file" + self.name)
return new_list
def read_input_file(path_to_file, extension = ".com"):
from itertools import product
def generate_files_e_field_in_one_direction(path_to_file, c1, c2, c3, start, finish, step, type_coordinates, type_space, extension = ".com", lines = "" ,new_kw = False):
"""
This function will generate files with different electric field all being aligned in one specified direction
c1, c2, c3, coordinates of the direction, it can be either cartesian, spherical or cylindrical
start, finish, step, the range of the electric field
type_coordinates - "cartesian", "spherical" or "cylindrical"
type_space - "linear", "step" or "log"
extension - the extension of the file
lines - Gaussian Input text to be written
new_kw - list of new keywords to be added to the file
more detailed about the function can be found in descripton of the function gen_e_field_direction
"""
c1, c2, c3 = np.longdouble(c1), np.longdouble(c2), np.longdouble(c3)
start, finish = np.longdouble(start), np.longdouble(finish)
if type_space == "linear":
step = int(step)
if type_space == "step":
step = np.longdouble(step)
map_directions = {"0" : "X", "1" : "Y", "2" :"Z"} #Dictionary to map the index of the direction into the letter
#Generate the values of the electric field over the specified direction
e_fields = vary_e_field_in_certain_direction(c1, c2, c3, var_range = [start, finish, step], type_coordinates = type_coordinates, type_space = type_space)
for field in e_fields:
if field[0] == 0 and field[1] == 0 and field[2] == 0:
continue
else: directions = [map_directions[str(i)] for i, x in enumerate(field) if x != 0] #Getting the directions of the electric field to be used in nameing of the files
dont_add_chk_last_file = False #Flag to not add ChkBasis, geom=check, guess=(read), GFInput to the last file, for case when we have only negative values of the electric field
for count, field in enumerate(e_fields): #Looping to creating the files
#Creating the name of the file. File name will have the following format:
#Input_kw M062X_101_Z+0.001900.com
if field[0] == 0 and field[1] == 0 and field[2] == 0:
file_name = path_to_file[:-4] + "_" + "_".join([str(x) + "+0" for x in directions]) + extension
else: file_name = path_to_file[:-4] + "_" + "_".join(["".join((map_directions[str(i)], "+" + "{:.8f}".format(x) if x > 0 else "{:.8f}".format(x))) for i, x, in enumerate(field) if x!= 0]) + extension
with open (file_name, "w") as file: #Creating the file
for line in lines: #Writting line of the file
file.write(line)
if field[0] == 0 and field[1] == 0 and field[2] == 0: #If 0 field, it will not write the field
None
else:
file.write("\n\n" + " ".join(["{:.8e}".format(x) for x in field]) + "\n\n")
change_line_in_file(file_name, "%chk", "%chk=" + os.path.basename(file_name[:-4]) + ".chk") #Adding the checkpoint file name
if new_kw:
if field[0] == 0 and field[1] == 0 and field[2] == 0:
None #Keywords for 0 field will be from the gaussian input file
else: #Changing the keywords for the ones specified in the respective section
change_kw(file_name, new_kw)
if field[0] == 0 and field[1] == 0 and field[2] == 0: #Insering geometry for 0 field
insert_geom(file_name, path_to_geom)
None
else:
if "automatically_update_kw" in kw_without_input_for_function: #Checking the keyword for the field calculation
add_keywords(file_name, *["IOp(3/14=-6)"])
if "update_old_chk" not in kw_without_input_for_function: #Checking the keyword for the field calculation
None
else:
index_update_old_chk = kw_without_input_for_function.index("update_old_chk")
type_old_chk_kw = input_for_function[index_update_old_chk] #Getting info about how we update old chk file
if (0, 0, 0) in e_fields: #Studying the case when we have 0 field
if ("automatically_update_kw" in kw_without_input_for_function) and (type_old_chk_kw[0] == "zero" or type_old_chk_kw[0] == "n-1"):
add_keywords(file_name, *["ChkBasis", "geom=check", "guess=(read)", "GFInput"])
with open(log_file, "a") as log:
log.write("Checked and added if they were missing the following keywords: ChkBasis, geom=check, guess=(read), GFInput to " + file_name + "\n")
else: #Studying the case when we do not have 0 field, and we need to choose a file from the boundary
if count == 0:
if ("_X+" in file_name) or ("_Y+" in file_name) or ("_Z+" in file_name):
#Checking that we have only positive values of the electric field
#If this is the cases, for the first iteration we will not add kw to the file
#Because we will make references to the lower boundary files
#!!!Posible bugs!!!
insert_geom(file_name, path_to_geom)
else:
#If we have negative values of the electric field, we will add the kw to the first file
add_keywords(file_name, *["ChkBasis", "geom=check", "guess=(read)", "GFInput"])
dont_add_chk_last_file = True #Bcz we did not use first file as a reference, we will use the last one
with open(log_file, "a") as log:
log.write("Checked and added if they were missing the following keywords: ChkBasis, geom=check, guess=(read), GFInput to " + file_name + "\n")
else:
if count == (len(e_fields)-1) and dont_add_chk_last_file:
#Checking if we need to add kw to the last file
insert_geom(file_name, path_to_geom)
else:
add_keywords(file_name, *["ChkBasis", "geom=check", "guess=(read)", "GFInput"])
with open(log_file, "a") as log:
log.write("Checked and added if they were missing the following keywords: ChkBasis, geom=check, guess=(read), GFInput to " + file_name + "\n")
insert_geom(file_name, path_to_geom) #Adding geometry to the created file. This function will check if there are kw that dont permit specification of the geometry
def check_double_lines(folder):
#Check if there are two consecutive lines that are whitespace
folder_list = os.listdir(folder)
folder_list = [x for x in folder_list if x.endswith(extension)]
for file in folder_list:
with open(os.path.join(folder, file), "r") as f:
lines = f.readlines()
with open(os.path.join(folder, file), "w") as f:
previous_line = ""
for line in lines:
if (line.strip() == "") and (previous_line.strip() == ""):
previous_line = line.strip()
continue
else: f.write(line)
previous_line = line.strip()
#Making that files end in a double lines
if previous_line == "":
f.write("\n")
else: f.write("\n\n")
def insert_geom(path_to_file, path_to_geom):
"""
This funciton is inserting the geometry into the file
path_to_file - path to the file
path_to_geom - path to the geometry file, if specified "delete" it will not introduce the geometry into the file, and will delete the respective line
The function will automatically check with keywords dont allow for the introduction of the geometry
"""
if path_to_geom == None:
return
with open(path_to_file, "r") as file:
lines = file.readlines()
for line in lines:
#Checking for the keywords that dont allow for the introduction of the geometry
if "geom=check" in line:
path_to_geom = "delete"
break
if "guess=(read)" in line:
path_to_geom = "delete"
break
#deleting the line of geometry if this is necesary
if path_to_geom == "delete":
with open(path_to_file, "r") as file:
lines = file.readlines()
with open(path_to_file, "w") as file:
for line in lines:
if "@" in line.strip():
continue
file.write(line)
return
#introducing the geometry into the file if @ is present
with open(path_to_file, "w") as file:
for line in lines:
if "@" in line.strip():
with open(path_to_geom) as geom_file:
geom = geom_file.readlines()
file.write("".join(geom))
continue
file.write(line)
return
def read_block_in_file(path_to_file, kw_start):
"""
This function will read the block of text between two keywords
"""
with open(path_to_file, "r") as file:
lines = []
record = False
count = 0
for line in file:
if kw_start in line.strip() and count == 0:
record = True
count += 1
continue
if record:
if kw_start in line.strip():
record = False
break
else: lines.append(line.strip())
return lines
path_to_folder = os.path.dirname(path_to_file) #Getting the path to the folder
original_file_name = os.path.basename(path_to_file) #Getting the name of the file
log_file = path_to_file[:-4] + "_WARNINGS_log.txt" #Creating the log file
with open(log_file, "w") as file:
file.write("All the critical errors will be updated here: " + path_to_file + "\n\n")
with open(path_to_file, "r") as file:
#This part extracts the keywords to be used by this function
kw_lines = []
for i, line in enumerate(file):
if line.strip() == "":
line_kw_end = i
break
else: kw_lines.append(line.strip())
keywords = []
#This will get the list of all keywords for this function and their options specified in the input
for line in kw_lines:
keywords.extend(split_text_for_inp(line))
keywords = keywords[1:]
print("--------------------------------------")
with open(log_file, "a") as file:
file.write("Keywords Provided to the input are:\n")
file.write(" ".join([x for x in keywords]) + "\n")
print("Keywords Provided to the input are:")
print(keywords)
with open(path_to_file, "r") as file:
#This part will write the original Gaussian file lines
original_file_lines = []
record_file = False
for line in file:
if "***start_gaussian_file***" in line.strip().lower():
record_file = True
continue
if record_file:
original_file_lines.append(line)
kw_without_input_for_function = [] #Keywords without the their options
input_for_function = [] #Options for the respective kw
for i in keywords:
kw_without_input_for_function.append(i.split("(")[0])
input_for_function.append(get_inp_text(i))
print("list of keywords: ", kw_without_input_for_function)
if "read_geom" in kw_without_input_for_function:
"""
This part will check if the geometry needs to be updated to any of the files, and will extract the path to the geometry
"""
index = kw_without_input_for_function.index("read_geom")
inp = input_for_function[index]
with open(path_to_file, "r") as file:
original_lines = file.readlines()
for line in original_lines:
if "@" in line.strip():
path_to_geom = line.strip()[1:]
break
else: path_to_geom = None
print("\n\n", "path to geom: ", path_to_geom)
if "gen_e_field_direction" in kw_without_input_for_function:
"""
This part will generate the files with different electric field in one direction
inp - is the input for the function gen_e_field_direction, it has the following format:
c1, c2, c3, start, finish, step, type_coordinates, type_space, (not_necessary)extension of the file
"""
index = kw_without_input_for_function.index("gen_e_field_direction")
inp = input_for_function[index]
if inp == None:
print("No input for function gen_e_field_direction")
print("Please specify the input for the function gen_e_field_direction")
print("Syntax: gen_e_field_direction(c1, c2, c3, start, finish, step, type_coordinates, type_space, (not_necessary)extension of the file)")
new_kw = False
if "new_kw" in keywords:
new_kw = read_block_in_file(path_to_file, "kw_e_field_calc")
inp = inp[0].replace(",", " ").split()
generate_files_e_field_in_one_direction(path_to_file, *inp, lines = original_file_lines, new_kw=new_kw)
check_double_lines(path_to_folder)
if "update_old_chk" in kw_without_input_for_function:
"""
This part will check if the files need to be updated with the old checkpoint file
and will find the type of the update
zero - will reference all files to the file with zero field
n-1 - will reference all files to the previous file.
0 field file will be the origin, and from it will start the referencing
also, there is a possibility to reference to a specific file by introducing full name of the file
"""
index = kw_without_input_for_function.index("update_old_chk")
inp = input_for_function[index]
if inp[0] == "n-1":
reference = False
print("All files will be referencing file n-1")
else:
reference = inp[0]
if "zero" in reference.lower():
for file in os.listdir(path_to_folder):
if file.endswith(extension):
if ((("_X+0" + extension) or "_X+0_") in file) or ((("_Y+0" + extension) or "_Y+0_") in file) or ((("_Z+0" + extension) or "_Z+0_") in file):
reference = file
else: reference = inp[0] #Case of introducing file name
if reference:
print("Selected file for reference is: " + reference)
update_oldchk_for_files_in_a_folder(path_to_folder, file_extension=".com", reference_from_input=reference)
if "basis_set" in kw_without_input_for_function:
"""
This function will introduce basis set for the files were it is desired and not contradicted by kw
"""
index = kw_without_input_for_function.index("basis_set")
inp = input_for_function[index]
if inp == None:
print("No input for function basis_set")
print("Please specify the input for the function basis_set")
print("Syntax: basis_set(option)")
print("Possible options are: 'origin' or 'all', or a file name")
basis_set_name = read_block_in_file(path_to_file, "kw_basis_set")
if basis_set_name == [] or basis_set_name == None or basis_set_name[0] == "":
print("No basis set name was specified")
print("Please specify the basis set in the corresponding block")
return
if "origin" in inp: #This will look to introduce basis set to the file with 0 field
for file in os.listdir(path_to_folder):
if file.endswith(extension):
if ((("_X+0" + extension) or "_X+0_") in file) or ((("_Y+0" + extension) or "_Y+0_") in file) or ((("_Z+0" + extension) or "_Z+0_") in file):
print("Adding basis set to file: " + file)
with open(os.path.join(path_to_folder, file), "r") as f:
lines = f.readlines()
with open(os.path.join(path_to_folder, file), "w") as f:
for line in lines:
if "#" in line.strip():
if "/gen" in line.strip()[-4:]: #Adding /gen to the end of first kw line if it has not been introduces
f.write(line)
else: f.write(line.strip() + "/gen\n")
continue
if "ChkBasis" in line.strip(): #Checking if ChkBasis is already in the file. It we have it we cannot introduce the basis set. It will raise a warning.
with open(log_file, "a") as log:
log.write("!!!WARNING!!!\n")
log.write("ChkBasis already in file: " + file + "\n")
f.write(line)
f.write("\n".join(basis_set_name))
f.write("\n\n")
break
elif "all" in inp:
for file in os.listdir(path_to_folder):
if file.endswith(extension):
print("Adding basis set to file: " + file)
with open(os.path.join(path_to_folder, file), "r") as f:
lines = f.readlines()
with open(os.path.join(path_to_folder, file), "w") as f:
for line in lines:
if "#" in line.strip():
if "/gen" in line.strip()[-4:]:
f.write(line)
else: f.write(line.strip() + "/gen\n")
continue
if "ChkBasis" in line.strip():
with open(log_file, "a") as log:
log.write("!!!WARNING!!!\n")
log.write("ChkBasis already in file: " + file + "\n")
log.write("File_name: " + file + "\n")
log.write("Basis set was added")
f.write(line)
f.write("\n".join(basis_set_name))
f.write("\n\n")
else: #This is for the case when we specify the basis set ourselves
with open(os.path.join(path_to_folder, inp[0]), "r") as f:
lines = f.readlines()
with open(os.path.join(path_to_folder, inp[0]), "w") as f:
for line in lines:
if "#" in line.strip():
if "/gen" in line.strip()[-4:]:
f.write(line)
else: f.write(line.strip() + "/gen\n")
continue
f.write(line)
f.write("\n".join(basis_set_name))
f.write("\n\n")
check_double_lines(path_to_folder)
if "change_kw" in kw_without_input_for_function:
"""
This keyword will allow us to make folders with different changed keywords
"""
index = kw_without_input_for_function.index("change_kw")
inp = input_for_function[index]
kw_to_change = inp[0].replace(",", " ").split()[0] #Kw which we want to change is in the 0th index
list_of_new_kw = inp[0].replace(",", " ").split()[1:] #Kws at index 1: are the ones we want to change to
for i in keywords:
if "change_kw" in i: #In new input files we want to delete the change_kw keyword to avoid recursion
str_to_remove_in_change_kw = i
if inp == None:
print("No input for function change_kw")
print("Please specify the input for the function change_kw")
print("Syntax: change_kw(file_name, new_kw)")
return
path_to_folder_minus_one = os.path.dirname(path_to_folder) #Folder where we will create new folders
folder_name = os.path.basename(path_to_folder) #name of the original folder, so that we can modify it for the new inputs
with open(path_to_file, "r") as file:
original_file_lines = file.readlines() #Saving the lines of the original file to copy them into a new folder, but we will delete change_kw
for i in list_of_new_kw: #Looping over the new keywords
new_folder_name = folder_name + "_kw_changed_" + i
try: #Avoiding error if the folder exists
os.mkdir(os.path.join(path_to_folder_minus_one, new_folder_name))
except: FileExistsError
#Name for the new input file
new_input_file_name_folder_i = os.path.join(path_to_folder_minus_one, new_folder_name, original_file_name[:-4] + "_kw_changed_" + i + original_file_name[-4:])
with open(new_input_file_name_folder_i, "w") as file:
#Making a new input file with the changed keyword, and with change_kw deleted to avoid recursion
for line in original_file_lines:
if str_to_remove_in_change_kw in line.strip():
line = line.replace(str_to_remove_in_change_kw, "")
if kw_to_change in line.strip():
line = line.replace(kw_to_change, i)
file.write(line)
check_double_lines(os.path.join(path_to_folder_minus_one, new_folder_name))
print("Now working in folder: ", os.path.join(path_to_folder_minus_one, new_folder_name))
read_input_file(new_input_file_name_folder_i) #Using this function to generate the files in the new folder
print("\n\n")
if "automatically_update_kw" in kw_without_input_for_function:
for file in os.listdir(path_to_folder):
"""
This small section will add NoXCTest kw for the case where it is needed.
If we don't have the keyword CPHF(grid=)
for the cases where the grid is grid=(fine,sg1grid)
we have to add the string (NoXCTest)
So, the keyword
Integral(Grid=sg1grid,Acc2E=14)
should look like
Integral(Grid=sg1grid,NoXCTest,Acc2E=14)
and the same for grid=fine
"""
if file.endswith(extension):
check_lines_for_kw = False
with open(os.path.join(path_to_folder, file), "r") as f:
lines = f.readlines()
introduce_NoXCTest_kw = True #It will be false if we have CPHF keyword
grid_kw_present = False #It will be true if we find grid=fine or grid=sg1grid
for line in lines: #Loop over the lines of the file
if line.startswith("#"):
check_lines_for_kw = True
if check_lines_for_kw: #If we are in the part of the file where we need to check for kws
if ("grid=fine" in line.strip().lower()) or ("grid=sg1grid" in line.strip().lower()):
grid_kw_present = True
if ("cphf") in line.strip().lower():
introduce_NoXCTest_kw = False
if line.strip() == "": #If we have reached the end of the kws part of the file, we generate a list of kw that need to be added
check_lines_for_kw = False
check_lines_for_kw = False
if grid_kw_present and introduce_NoXCTest_kw:
"""
If the CPHF keyword was missing, and if we have grid=fine or grid=sg1grid in the keywords
In this section we add NoXCTest to the list of keywords
"""
with open(os.path.join(path_to_folder, file), "w") as f:
for line in lines:
if "integral" in line.strip().lower():
words = split_text_for_inp(line.strip()) #Getting list of keywords in this line
for i, word in enumerate(words): #Looping over the keywords
if "integral" in word.lower(): #Finding the keyword Integral
words[i] = word[:-1] + ",NoXCTest)" #Adding NoXCTest to the end of options of integral kw
with open(log_file, "a") as log: #Adding info to the log file
log.write("Added NoXCTest kw to file: " + file + "\n")
line = " ".join(words) + "\n" #Making the line from the list of keywords
f.write(line) #Writting the line to the file
if "zip" in kw_without_input_for_function:
"""
Zip function is similar to change_kw, but it will allow us to change multiple keywords at once
it will make all possible combination of the keywords we want to change
Example of the input: zip((cphf(grid=sg1), delete), (sg1, sg2, sg3, sg4), (IOp(9/75=2), IOp(9/75=1)))
the kw in the position 0 of the inner bracket, is the kw we want to change, and the rest are the options we want to change to
it will make all posible combination. For the example above, it will make 16 ppossible combiations
"""
index = kw_without_input_for_function.index("zip")
inp = input_for_function[index]
if inp == None:
print("No input for function zip")
print("Please specify the input for the function zip")
print("Syntax: zip((kw1, change1, change2, ...), (kw2, change1, change2, ...), ...)")
inp = get_inp_text(inp[0])
original_kw = [x.replace(",", " ").split()[0] for x in inp] #Getting list of kw-s that we will be changing
all_kw = [x.replace(",", " ").split() for x in inp] #Getting list to what we will change the kw. It will include the original kw.
for i in keywords:
if "zip" in i: #Deleting the zip kw from the new input files to avoid recursion
str_to_remove_in_zip = i
print("This kw will be removed")
print(str_to_remove_in_zip)
path_to_folder_minus_one = os.path.dirname(path_to_folder) #Path to the folder where new folders will be created
folder_name = os.path.basename(path_to_folder) #Name of the original folder
for new_comb in product(*all_kw): #Looping over all possible combinations
with open(path_to_file, "r") as file:
original_file_lines = file.readlines()
new_folder_name = folder_name + "_kw_changed_" + "_".join(new_comb)
name = ""
not_allowed = "()/" #Not allowed characters in the folder name
for char in new_folder_name: #!!!FOR FUTURE TO CHANGE IT TO A SINGLE LIST COMPREHENSION!!!
if char in not_allowed:
continue
else: name += char
new_folder_name = name
try:
os.mkdir(os.path.join(path_to_folder_minus_one, new_folder_name))
except:
FileExistsError
name_from_kw = "_".join(new_comb)
name_from_kw = "".join([char for char in name_from_kw if char not in "()/"])
new_input_file_name_folder_i = os.path.join(path_to_folder_minus_one, new_folder_name, original_file_name[:-4] + "_kw_changed_" + name_from_kw + original_file_name[-4:])
with open(new_input_file_name_folder_i, "w") as file:
for line in original_file_lines:
#Removing the zip kw
if str_to_remove_in_zip in line.strip():
ind_str = line.find(str_to_remove_in_zip)
line = line[:ind_str] + line[ind_str + len(str_to_remove_in_zip):]
if line.endswith(" "):
line = line[:-1]
if line.startswith(" "):
line = line[1:]
if line.strip() == "":
continue
#Changing the kw
for i, x in enumerate(original_kw):
if x in line.strip():
if new_comb[i] == "delete":
ind_str = line.find(x)
line = line[:ind_str] + line[ind_str + len(x):]
if line.endswith(" "):
line = line[:-1]
if line.startswith(" "):
line = line[1:]
if line.strip() == "":
continue
else: line = line.replace(x, new_comb[i])
file.write(line)
check_double_lines(os.path.join(path_to_folder_minus_one, new_folder_name))
print("Now working in folder: ", os.path.join(path_to_folder_minus_one, new_folder_name))
read_input_file(new_input_file_name_folder_i)
print("\n\n")
return
return
def change_kw(path_to_file, keywords):
"""
This function will change the keywords in a given file to the ones specified
"""
with open(path_to_file, "r") as file:
lines = file.readlines() #Reading the lines of the file
with open(path_to_file, "w") as file:
introducing_new_kw = False
gaussian_file_started = True
count_kw_introduced = 0
for line in lines:
if "#" in line.strip() and gaussian_file_started: #Looking for the section in input where keywords are specified
introducing_new_kw = True
if introducing_new_kw: #Introducing new keywords if the kw section was found
if count_kw_introduced == 0: #Introducing once the words
for i, line in enumerate(keywords):
file.write(line + "\n")
gaussian_file_started = False #Not introducing keywords from Gaussian file
count_kw_introduced += 1
if line.strip() == "": #Looking for the end of section where kw are specified
introducing_new_kw = False
gaussian_file_started = True
if gaussian_file_started: #Introducing all the Gaussian file lines before and after the keywords section
file.write(line)
return
def split_text_for_inp(text):
"""
This function will separate words in the line which has multiple brackets
This: 'update_old_chk(n-1) basis_set(origin) read_geom() new_kw'
Will be separate into this: ['update_old_chk(n-1)', 'basis_set(origin)', 'read_geom()', 'new_kw']
Nested brackets can be used inside the primary brackets
"""
result = ""
parens_open = 0
for char in text:
if char =='(':
parens_open += 1
elif char == ')':
parens_open -= 1
if char == ',' and parens_open == 0:
char = ""
if char == ' ' and parens_open == 0:
char = ";"
result += char
return result.split(";")
def get_inp_text(text):
"""
This will get text inside the brackets of the line. Only the first brackets are used
"""
result = ""
parens_open = 0
to_return = []
for char in text:
if char == ")":
parens_open -= 1
if parens_open > 0:
result += char
if char =='(':
parens_open += 1
if result != "" and parens_open == 0:
to_return.append(result)
result = ""
if len(to_return) == 0:
return None
else: return to_return
#--------------------------------------------------------------------------------------------------------------------------
def create_gaussian_file(file_name, keywords, nproc=False, mem=False, title="Job Name", oldchk=False, oldchk_file=None, chk=False, chk_name=False,
charge_multiplicity=(0, 1), geom=False, basis_set=False, wfx=False, Field=False):
"""
Creating an input file for gaussian calculation
nproc - to specify the number of processos to be used in calculation, mem - to specify the memory to be used in calculation
file_name = name by which the file will be saved
geom - Geometry to be used in calculation
keywords - keywords to be specified to gaussian. A string or a list of word is accepted
title - title to be used in file
oldchk = False, an old checkpoint name needs to be indicated, oldchk_file = Name of the old checkpoint file
chk = False, put True if a chk with the same name as filename needs to be saved, chk_name = Name of the checkpoint file to be used instead
charge_multiplicity = (0, 1) by default, if specified, another charge and multiplicity will be used
basis set = False, if smth else, need to be indicated another basis set in form ["Atom1, Atom2, ...", "Basis Set Used"]. Both values are single strings
wfx = False, A wfx file name needs to be specified
Field = False, if smth else, the directions of field need to be specified
"""
file_gaussian = open(file_name, "w") # Opening the file
if nproc: # Adding the line of nprocessors if the number of processors is specified
file_gaussian.write("%nproc=" + str(nproc) + "\n")
if mem: # Adding the line with memory
file_gaussian.write("%mem=" + mem + "\n")
if oldchk: # Checking if and old checkpoint is to be used in the calculation
if oldchk_file == None:
raise TypeError(
"Old checkpoint file is not specified. Working filename is: " + file_name)
# Adding the line of an old chk to the gaussian input
file_gaussian.write("%oldchk=" + oldchk_file + "\n")
if chk: # Checking if the checkpoint needs to be created
if chk_name: # Case when name of chk is different from file name
file_gaussian.write("%chk=" + chk_name + "\n")
else: # Chk point is the same as file name
file_gaussian.write(
"%chk=" + os.path.basename(file_name[:-4]) + ".chk\n")
# Adding the keywords to the file
if type(keywords) == list: # Checking if keywords are specified as a list
new_line = " ".join(keywords)
file_gaussian.write(new_line + '\n\n')
elif type(keywords) == str: # Checking if keywords are specified as a string
file_gaussian.write(keywords + "\n\n")
else:
# An error for the case of wrong keyword list
raise TypeError(
"keywords need to be a list of keywords or a string. Working filename is: " + file_name)
file_gaussian.write(title + "\n\n") # Adding the title
# Adding charge and multiplicity. By default 0 1
# The bloc of code checks if it was introduced as string, or as a list/tuple.
if isinstance(charge_multiplicity, str):
file_gaussian.write(charge_multiplicity + "\n")
elif isinstance(charge_multiplicity, tuple) or isinstance(charge_multiplicity, list):
file_gaussian.write(" ".join(str(x) for x in charge_multiplicity) + "\n") #Separating values of a list with a underline, and writing to the file.
else: file_gaussian.write("0 1" + "\n") #If they have not been specified, or there is an error, submit it as (0, 1), which is the most usual case.
#This bloc of code checks if the geometry needs to be added to the file.
if geom is not False:
for i in range(geom.shape[0]): # Writting the geometry. Iterating over the lines of the matrix.
file_gaussian.write(" ".join(geom[i]) + "\n")
file_gaussian.write("\n") # Adding a blanc line at the end of file
else:
file_gaussian.write("\n")
#This bloc of code addes an electric field if it has been specified.
if Field is not False: # Adding lines corresponding to the Field
file_gaussian.write(" ".join([str(x) for x in Field]) + "\n\n")
if basis_set: # Adding the lines corresponding to the basis set
if type(basis_set[0]) == list: # Checking if the atoms were saved as a list
file_gaussian.write(" ".join(basis_set[0]) + " 0" + "\n")
elif type(basis_set[0]) == str:
file_gaussian.write(basis_set[0] + " 0" + "\n")
# Writting the basis set in the file
file_gaussian.write(basis_set[1] + "\n" + "****" + "\n\n")
if wfx: # write wfx file
file_gaussian.write(wfx + "\n\n")
file_gaussian.close() # Closing the file
#------------------------------------------------------------------------------------------------------
def generate_input_energy_field_calculation(ndim, type_space, all_the_same = False, **kwargs):
"""
Creating a matrix with values of how each element in the electric field changes
ndim - dimension of matrix
type_space - "linear", "log" or "step". Linear - n values on the range of start to finish
log - logarithmicaly n spaced values on the range start to finish
step - values placed with a specified step from start to finish, finish is not included.
all the same = False, change to a [start, finish, step] for the case when for all the directions the change is the same
**kwargs submit the directions and how the field will be changing in the form of {"Direction" : [start, finish, step], "Direction2": [start, finish, step]}
"""
ndim_l = [3 for i in range(ndim)] #Getting the dimensions of the matrix
matrix = np.zeros(ndim_l, dtype=object) #Creating a matrix. Having dtype = object is crucial here
character_mapping = {"X" : "0", "x" : "0", "Y": "1", "y": "1", "Z" : "2", "z": "2"} #Creating a map of letters into numbers(indexes)
new_dict = {} #To store the new data
for old_key, value in kwargs.items(): #Changing the letters into number
for char, replacement in character_mapping.items(): #iterating over the map created earlier
old_key = old_key.replace(char, replacement) #replacing character
new_dict[old_key] = value
if all_the_same is not False: #The case when we want all directions of electric field to have the same values
if type_space == "linear":
new_array = np.linspace(all_the_same[0], all_the_same[1], all_the_same[2])
if type_space == "log":
new_array = np.logspace(np.log10(all_the_same[0]), np.log10(all_the_same[1]), all_the_same[2])
if type_space == "step":
new_array = np.arange(all_the_same[0], all_the_same[1], all_the_same[2])
for index, element in np.ndenumerate(matrix):
print(index, element)
matrix[index] = new_array
return matrix
for key, value in new_dict.items(): #Changing only the elements of the matrix that were specified in the kwargs
new_key = [int(x) for x in key] #Making a list of integers from the str
if type_space == "linear":
new_array = np.linspace(value[0], value[1], value[2])
if type_space == "log":
new_array = np.logspace(np.log10(value[0]), np.log10(value[1]), value[2])
if type_space == "step":
new_array = np.arange(value[0], value[1] + 10**(-10), value[2])
matrix[tuple(new_key)] = new_array #Assigning the new values
return matrix
#------------------------------------------------------------------------------
def vary_e_field_in_certain_direction(c1, c2, c3, var_range, type_coordinates = "cartesian", type_space = "linear"):
"""
Function to vary the electric field in a certain direction.
c1, c2, c3 - coordinates of the point in the space, where the electric field is to be varied.
format of the coordinates is (x, y, z) for catesian, (r, theta, phi) for spherical, (r, phi, h) for cylindrical
Physics convetion is used. Values are to be given in degrees
The use of (r, theta, phi) denotes radial distance, inclination (or elevation), and azimuth, respectively.
var_range - range of the variation.
type_coordinates - "cartesian", "spherical", "cylindrical". Default is cartesian.
type_space - "linear", "log", "step". Default is linear.
The function will convert the coordinates in the sphericall coordinates.
Then it will vary the radial distance which represents the intensity of the electric field.
"""
def convert_spherical_to_cartesian_coordinates(r, theta, phi):
"""
Convert spherical coordinates to cartesian coordinates.
The use of (r, theta, phi) denotes radial distance, inclination (or elevation), and azimuth, respectively.
θ the angle measured away from the +Z axis
As φ has a range of 360°
θ has a range of 180°, running from 0° to 180°
"""
x = r * np.sin(theta) * np.cos(phi)
y = r * np.sin(theta) * np.sin(phi)
z = r * np.cos(theta)
return x, y, z
def convert_cartesian_to_spherical_coordinates(x, y, z):
"""
Convert cartesian coordinates to spherical coordinates.
The use of (x, y, z) denotes the cartesian coordinates.
"""
r = np.sqrt(x**2 + y**2 + z**2)
theta = np.arccos(z / r)
if int(x) == 0:
phi = np.arctan(np.inf)
else: phi = np.arctan(y / x)
return r, theta, phi
def convert_cylindrical_to_spherical_coordinates(r, phi, h):
"""
Convert cylindrical coordinates to spherical coordinates.
The use of (r, phi, z) denotes radial distance, azimuth angle, and height, respectively.
"""
ro = np.sqrt(r**2 + h**2)
if int(h) == 0:
theta = np.arctan(np.inf)
else: theta = np.arctan(r / h)
return ro, theta, phi
def return_x_y_z(x, y, z): #Function to return the same coordinates
return x, y, z
if type_space == "linear": #Creating the space for the variation
space = np.linspace(var_range[0], var_range[1], var_range[2])
elif type_space == "step":
space = np.arange(var_range[0], var_range[1] + var_range[2], var_range[2])
elif "log" in type_space.lower():
space = []
var_range[1] = int(var_range[1])
var_range[0] = np.float64(var_range[0])
var_range[2] = np.float64(var_range[2])
for i in range(0, var_range[1]+1):
space.append(var_range[0]*(var_range[2]**i))
space = np.array(space)
space2 = -1 * copy.deepcopy(space)
space = np.hstack((space2, [0], space))
else: space = np.linspace(var_range[0], var_range[1], var_range[2])
#Mapping the function to the corresponding conversion of coordinates
map_function = {"cartesian" : convert_cartesian_to_spherical_coordinates, "spherical" : return_x_y_z, "cylindrical" : convert_cylindrical_to_spherical_coordinates}
f = map_function[type_coordinates]
return_vector = []
if type_coordinates == "spherical":
c2 = np.radians(c2) #Converting from degrees to radians
c3 = np.radians(c3)
elif type_coordinates == "cylindrical":
c2 = np.radians(c2)
r, theta, phi = f(c1, c2, c3) #Converting the coordinates to spherical
for i in space: #Obtaining the new values for electric field
r = i #Length of vector is changed
x, y, z = convert_spherical_to_cartesian_coordinates(r, theta, phi) #Obtaining the new cartesian coordinates to be used in the Gaussian input file.
x, y, z = np.round((x, y, z), 10) #Rounding the values to 10 decimal places
if x == 0:
x = abs(x)
if y == 0:
y = abs(y)
if z == 0:
z = abs(z)
return_vector.append((x, y, z)) #Appending the new values to the list
return return_vector
#--------------------------------------------------------------------------
def update_oldchk_for_files_in_a_folder(folder_path, file_extension = ".com", reference_from_input = False):
"""
This function updates the %oldchk= line in the Gaussian input files in a folder.
:param folder_path: the path to the folder containing the Gaussian input files
:param file_extension: the extension of the Gaussian input files
:param reference: the name of the reference file
:return: None
If reference is False, the files are referenced to the file n-1 according to the value of the e field
"""
def change_oldchk_file(file_path, new_oldchk_name): # This function changes the %oldchk= line in a Gaussian input file
old_chk_line = "%oldchk=" # The function takes the path to the file and the name of the new chk file
with open(file_path, "r") as file_gaussian:
lines = file_gaussian.readlines()
for i, line in enumerate(lines):
if old_chk_line in line.lower():
lines[i] = "%oldchk=" + str(new_oldchk_name) + "\n"
with open(file_path, "w") as file_1:
file_1.writelines(lines)
break
file_list = os.listdir(folder_path) # This part of the function sorts the files in the folder by the number in the file name
file_list = [file_name for file_name in file_list if file_name.endswith(file_extension)]
file_list = [(np.float64(x.split("_")[-1][1:-4]), x) for x in file_list] # The number is the last part of the file name, after the last underscore and before the file extension
file_list = sorted(file_list, key=lambda x: x[0]) # The files are sorted by the number
zero_index = None # The index of the file with the number 0 is found
#for i in file_list:
#if np.round(i[0],10) == 0:
#zero_index = file_list.index(i) # The index of the file with the number 0 is found
#break
file_list = [x[1] for x in file_list] # The file names are extracted from the list of tuples
zero_index = None
for file in file_list:
if ((("_X+0" + file_extension) or "_X+0_") in file) or ((("_Y+0" + file_extension) or "_Y+0_") in file) or ((("_Z+0" + file_extension) or "_Z+0_") in file):
zero_index = file_list.index(file) # The index of the file with e_field (0, 0, 0) is found
print("zero_index_file " + file_list[zero_index])
break