-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.py
executable file
·1139 lines (911 loc) · 45.8 KB
/
io.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
"""
"""
from __future__ import absolute_import
from __future__ import print_function
import six
from six.moves import range
from six.moves import zip
__version__ = '1.0.0'
__author__ = 'Mary Van Vleet'
# Standard Packages
#from __future__ import division
import importlib
import numpy as np
import sys
import json
import os
# Local Packages
from .methods import default, mastiff
############################## Global Variables and Constants ######################################
# Born-Mayer-sISA scaling exponent, as defined in Van Vleet et al. JCTC 2016
_bmsisa_exp_scale = 0.84
# Constraints suffix
_constraints_suffix = '.constraints'
####################################################################################################
####################################################################################################
class Parameters():
'''Read parameters from user input.
References
----------
Attributes
----------
Methods
-------
Known Issues
------------
Units
-----
Atomic units are assumed throughout this module.
'''
def __init__(self, mon1, mon2, inputdir, **kwargs
):
'''Initialize input variables.'''
###########################################################################
###################### Variable Initialization ############################
self.mon1 = mon1
self.mon2 = mon2
self.inputdir = inputdir
###########################################################################
###########################################################################
###########################################################################
##################### Instance Variable Defaults ##########################
# File suffixes
self.drude_suffix = '.drude'
self.exp_suffix = '.exp'
self.indexp_suffix = '.indexp'
self.disp_suffix = '.disp'
self.axes_suffix = '.axes'
self.atomtypes_suffix = '.atomtypes'
self.constraints_suffix = _constraints_suffix
self.ignorecase = False
###########################################################################
###########################################################################
###########################################################################
###################### Initialization Routines ############################
# Overwrite defaults with kwargs
self.__dict__.update(kwargs)
###########################################################################
###########################################################################
return
####################################################################################################
####################################################################################################
def readParameters(self):
'''
Parameters
----------
Returns
-------
'''
self.readAtomtypes()
self.readConstraints(self.constraint_files)
# Exponents: Read in any constraints, from input, scale depending on functional form
if not self.exponent_files: #assign default .exp files if necessary
self.exponent_files = [self.inputdir + mon + self.exp_suffix for mon
in (self.mon1, self.mon2)]
self.exponents = { atom:[params[i]['B'] for i in range(len(params))] for atom,params in
self.params.items()}
if self.exp_source.lower() == 'read':
self.readExponents(self.exponent_files,self.exponents)
elif self.exp_source.lower() == 'ip':
print('exponents from IP!')
self.calculateIPExponents()
if self.separate_induction_exponents:
indexponent_files = [self.inputdir + mon + self.indexp_suffix for mon
in (self.mon1, self.mon2)]
self.induction_exponents = {}
self.readExponents(indexponent_files,self.induction_exponents)
else:
self.induction_exponents = self.exponents
self.readAxes()
self.readCParams()
self.readDrudeCharges()
return
####################################################################################################
####################################################################################################
def readAtomtypes(self):
'''
Parameters
----------
Format
-------
.atomtypes file, whose format is identical to that of a standard .xyz
file except that inclusion of atomic coordinates is not required.
Example:
<number of atoms>
comment line
<atomtype> [X] [Y] [Z]
...
Returns
-------
'''
atomtypes_error_message = '''
The number of atoms listed in {0} is not consistent
between line 1 and the rest of the file!
Number of atoms in {0}...
...according to line 1: {1}
...according to the rest of the file: {2}
({3})
Please fix this inconsistency in
{0}
and re-run POInter.
'''
error_message = '''
The number of atoms listed in your .atomtypes file doesn\'t match that
of the .sapt file!
Number of atoms in monomer {0}...
...according to the .sapt file: {1}
...according to the .atomtypes file: {2}
Please fix
{3}
to list atomtypes for each of the {1} atoms in monomer {0}, and re-run
POInter.
'''
# Read in atomtypes from the .atomtypes file(s)
atomtype_files = [self.inputdir + mon + self.atomtypes_suffix for mon
in (self.mon1, self.mon2)]
natoms = []
atoms = [[],[]]
for i,ifile in enumerate(atomtype_files):
with open(ifile,'r') as f:
data = [line.split() for line in f.readlines()]
natoms.append(int(data[0][0]))
for line in data[2:]:
if line: #ignore blank lines
atoms[i].append(line[0])
if self.ignorecase:
atoms = [ [a.upper() for a in atom] for atom in atoms ]
self.natomtypes1, self.natomtypes2 = natoms #number of atoms according to the .atomtypes file
self.atomtypes1, self.atomtypes2 = atoms
self.natoms1, self.natoms2 = len(self.atoms1), len(self.atoms2) # number of atoms according to the .sapt file:
# Ensure self-consistency in each .atomtypes file
assert self.natomtypes1 == len(self.atomtypes1), atomtypes_error_message.format(
atomtype_files[0],self.natomtypes1,len(self.atomtypes1),self.atomtypes1)
assert self.natomtypes2 == len(self.atomtypes2), atomtypes_error_message.format(
atomtype_files[1],self.natomtypes2,len(self.atomtypes2),self.atomtypes2)
# Ensure number of atoms listed in each .atomtypes file equals the number
# of atoms in each molecule (from the .sapt file)
assert len((self.atoms1)) == len(self.atomtypes1),\
error_message.format(1,self.natoms1,self.natomtypes1,atomtype_files[0])
assert len((self.atoms2)) == len(self.atomtypes2),\
error_message.format(2,self.natoms2,self.natomtypes2,atomtype_files[1])
# Construct list of atomtypes
self.atomtypes = list(set(self.atomtypes1 + self.atomtypes2))
# Construct atomtypes -> atoms hash; this is only needed if we later
# calculate exponents based on IP values
self.atomtypes_to_atoms = { k:v for k,v in zip(self.atomtypes1,self.atoms1) }
self.atomtypes_to_atoms.update({ k:v for k,v in zip(self.atomtypes2,self.atoms2) })
return
####################################################################################################
####################################################################################################
def readExponents(self,exponent_files,exponents):
'''
Parameters
----------
Format
------
Atomtype and associated exponent for each atomtype present in the
force field, with atomtype declarations separated by a newline.
Comment lines are ignored. Units in a.u.
Example:
<atomtype1> <exponent1>
<atomtype2> <exponent2>
...
Returns
-------
'''
format_error = '''Improperly formatted exponents file "{}". Aside from
comments, each line of the .exp file should be in two-column format,
with the first column representing the atomtype and the second column
representing the exponent. Please fix the input error and re-run
POInter.
'''
overide_error = '''{0} attempts to set atomtype {1}'s exponent to
{2}, however elsewhere you have set this exponent to {3}.
Please fix the conflicting exponent definition and re-run POInter.
Some suggestions:
1. Did you set different exponent values between your .exp and
.constraints files?
2. Did you use the Born-Mayer-sISA functional form? If so, values from
the .exp file have been scaled before being read into POInter, and may
differ from the input .exp values.
'''
missing_error = '''You have not defined an exponent parameter for atomtype "{0}".
Please define this parameter in one of your .exp files -- {1}
-- and re-run POInter.
'''
for i,ifile in enumerate(exponent_files):
with open(ifile,'r') as f:
raw_lines = [line.split('#')[0] for line in f.readlines()]
data = [line.split() for line in raw_lines]
for line in data:
if not line or line[0].startswith('#'):
continue #ignore blank lines and comments
assert len(line) == 2, format_error.format(ifile)
# Read in new atomtype exponent, check for conflicting
# values
atom, exponent = line
exponent = [float(exponent)*self.exp_scale]
if self.ignorecase:
atom = atom.upper()
assert not (atom in exponents and
exponents[atom] != exponent),\
overide_error.format(ifile,atom,exponent,exponents[atom])
exponents[atom] = exponent
# Make sure all atomtypes have an exponent parameter defined
for atom in self.atomtypes:
assert atom in exponents,\
missing_error.format(atom,'\n\t\t\t'.join([''] + list(set(exponent_files))))
return
####################################################################################################
####################################################################################################
def calculateIPExponents(self,checkOveride=False):
'''
Parameters
----------
checkOveride : boolean, default False
Ensures that any exponents in the constraints files match
exponents calculated from ionization potential (IP)-based
exponents. False by default.
Returns
-------
None explicitly, though self.exponents is updated
'''
overide_error = '''{0} attempts to set atomtype {1}'s exponent to
{2}, however elsewhere you have set this exponent to {3}.
Please fix the conflicting exponent definition and re-run POInter.
Some suggestions:
1. Did you use the Born-Mayer-IP functional form? If so, values from
the .exp file have been scaled before being read into POInter, and may
differ from the input .exp values.
'''
from .elementdata import Exponent
for atom in self.atomtypes:
element = self.atomtypes_to_atoms[atom]
exponent = Exponent(element)
if atom not in self.exponents:
self.exponents[atom] = [exponent]
elif checkOveride:
# Make sure exponents given in constraints match IP values
assert not self.exponents[atom] != exponent,\
overide_error.format(ifile,atom,exponent,self.exponents[atom])
self.exponents[atom] = exponent
return
####################################################################################################
####################################################################################################
def readConstraints(self,constraints_files):
'''
Parameters
----------
Format
------
Atomtype and associated exponent for each atomtype present in the
force field, with atomtype declarations separated by a newline.
Comment lines are ignored. Units in a.u.
Example:
<atomtype1> <exponent1>
<atomtype2> <exponent2>
...
Returns
-------
'''
format_error = '''Improperly formatted exponents file "{}". Aside from
comments, each line of the .exp file should be in two-column format,
with the first column representing the atomtype and the second column
representing the exponent. Please fix the input error and re-run
POInter.
'''
overide_error = '''{0} attempts to set atomtype {1}'s exponent to
{2}, however elsewhere you have set this exponent to {3}. Please fix
the conflicting exponent definition and re-run POInter.
'''
missing_error = '''You have set atomtype "{0}" as constrained, but
no parameters were found for this atomtype in any of the defined
constraints files: {1}
Constraints *were* found for the following atomtypes:
"{2}"
Please either eliminate atomtype "{0}" from the list of constrained
atomtypes, or change this atomtype to match one of the atomtypes
above, and re-run POInter.
'''
constraints = {}
for ifile in constraints_files:
with open(ifile,'r') as f:
constraints.update(json.load(f))
self.Aparams = [ ] # 4 components; exch, elst, ind, dhf
# Create params dictionary of dictionaries
self.params = {}
for atom in self.constrained_atomtypes:
assert atom in constraints, missing_error.format(
atom,'\n\t\t-- '.join([''] + constraints_files),
'", "'.join(list(constraints.keys())))
constraints[atom]['aniso'] = [ np.array(a) for a in constraints[atom]['aniso']]
constraints[atom]['C'] = np.array(constraints[atom]['C'])
self.params[atom] = []
self.params[atom].append(constraints[atom])
return
####################################################################################################
####################################################################################################
def readAxes(self):
'''
Parameters
----------
Format
------
The .axes file contains two sections: the first section declares
anisotropic atomtypes, and the second defines the local coordinate
system for each anisotropic atomtype.
Format for the first section should be the atomtype followed by the
spherical harmonic moments (y10, y20, etc.) used to describe the
anisotropy of that atomtype. ex:
O_H2O y10 y20
Format for the second section should be the atom index (based on its
position in the .atomtypes file, with indexing from zero) followed by
the axis that is being defined (z or x), the origin of the axis vector
(given as an atom index), and the endpoint of the axis vector (as one
or more atom indices; the terminus of the axis vector will be treated
as the midpoint of all atom indices). For instance, defining the
z-axis for the oxygen atom [index 0] in water as the vector bisecting
the two hydrogen atoms [indices 1 and 2] would be accomplished by the
declaration
0 z 0 1 2
Comment lines and section headers (lines beginning with 'Anisotropic'
or 'Axes') are ignored. The two sections should be separated by a
blank line.
Template:
<atomtype1> [sph_harm1] [sph_harm2] ...
<atomtype2> [sph_harm1] ...
...
<blank line>
<atomtype1_index> <z/x axis> <atomtype1_origin> <atomtype1_endpoint>
<atomtype2_index> <z/x axis> <atomtype2_origin> <atomtype2_endpoint1> [atomtype2_endpoint2] ...
<blank line>
Returns
-------
'''
format_error = '''Improperly formatted axes file "{}". Aside
from comments, each line of the axes subsection file should have at
least four columns representing, respectively, the atom index, axis
(z or x), axis origin (as an atomic index), and axis endpoint (also as
Please fix the input error and re-run POInter.
'''
axes_files = [self.inputdir + mon + self.axes_suffix for mon
in (self.mon1, self.mon2)]
self.anisotropic_atomtypes = []
self.anisotropic_symmetries = {}
self.anisotropic_axes1 = [ [ [],[] ] for i in range(self.natoms1)]
self.anisotropic_axes2 = [ [ [],[] ] for i in range(self.natoms2)]
for imon,ifile in enumerate(axes_files):
with open(ifile,'r') as f:
#TODO: Check that using \r\n vs. \n\n is robust
sections = f.read().split('\n\n')
anisotropic_lines = [line.split('#')[0]
for line in sections[0].split('\n') ]
anisotropic_data = [line.split() for line in anisotropic_lines]
for line in anisotropic_data:
if not line or \
line[0].lower().startswith('aniso'):
#line[0].startswith('#') or
continue #ignore blank lines and comments
self.anisotropic_atomtypes.append(line[0])
self.anisotropic_symmetries[line[0]] = line[1:]
axes_lines = [line.split('#')[0]
for line in sections[1].split('\n') ]
axes_data = [line.split() for line in axes_lines]
for line in axes_data:
# Ignore blank lines, comments, section headers, and isotropic
# atoms (ones without spherical harmonic declarations)
if not line or line[0].lower().startswith('axes') or\
line[0].lower().startswith('atom'):
#line[0].startswith('#') or\
continue
assert len(line) > 3, format_error.format(ifile)
iatom = int(line[0])
iaxis = 0 if line[1] == 'z' else 1 # list x and z axes seperately
axes = self.anisotropic_axes1 if imon == 0 else self.anisotropic_axes2
coords = [ int(i) for i in line[2:] ]
assert not (axes[iatom][iaxis] != [] and axes[iatom][iaxis] != coords),\
'''Conflicting specifications for the {} axis for atom {}
in the file {}. Please only use one axis specification
line per axis per atom.
'''.format(iaxis,iatom,ifile)
axes[iatom][iaxis] = coords
self.anisotropic_atomtypes = list(set(self.anisotropic_atomtypes))
return
####################################################################################################
####################################################################################################
def readCParams(self):
'''
Parameters
----------
Format
------
Atomtype and associated dispersion coefficients for each atomtype present in the
force field, with atomtype declarations separated by a newline.
Comment lines are ignored. Units in a.u.
Example:
<atomtype1> <c6> <c8> <c10> <c12>
<atomtype2> <c6> <c8> <c10> <c12>
...
Returns
-------
'''
format_error = '''Improperly formatted dispersion file "{}". Aside
from comments, each line of the .disp file should be in 5-column
format, with the first column representing the atomtype and the
remaining columns representing the C6-C12 coefficients. Please fix the
input error and re-run POInter.
'''
overide_error = '''{0} attempts to set atomtype {1}'s dispersion
coefficients to {2}, however elsewhere you have set these coefficients
to {3}. Please fix the conflicting parameter declaration(s) and re-run
POInter.
'''
missing_error = '''You have not defined dispersion parameters for atomtype "{0}".
Please define this parameter in one of your .disp files -- {1}
-- and re-run POInter.
'''
dispersion_files = [self.inputdir + mon + self.disp_suffix for mon
in (self.mon1, self.mon2)]
# Read in Cii parameters and convert to Ci parameters. For now,
# only a geometric mean combination rule seems appropriate for
# dispersion parameters, so we take a sqrt here.
assert self.cij_combination_rule == 'geometric'
self.Cparams = {}
for i,ifile in enumerate(dispersion_files):
with open(ifile,'r') as f:
raw_lines = [line.split('#')[0] for line in f.readlines()]
data = [line.split() for line in raw_lines]
for line in data:
if not line or line[0].startswith('#'):
continue #ignore blank lines and comments
assert len(line) == 5, format_error.format(ifile)
# Read in dispersion coefficients for each atomtype, check
# for conflicting values
atom, ci = line[0], line[1:]
# Cn parameters are read in as Cii parameters, but internally
# treated as Ci parameters. To extract Ci
# parameters, we need to take the square root of Cii
ci = np.sqrt([float(i) for i in line[1:]])
if self.ignorecase:
atom = atom.upper()
assert not (atom in self.Cparams and not \
np.allclose(self.Cparams[atom] , ci )),\
overide_error.format(ifile,atom,ci,self.Cparams[atom])
self.Cparams[atom] = ci
# Make sure all atomtypes have a dispersion parameter defined
for atom in self.atomtypes:
assert atom in self.Cparams,\
missing_error.format(atom,'\n\t\t\t'.join([''] + list(set(dispersion_files))))
return
####################################################################################################
####################################################################################################
def readDrudeCharges(self):
'''
Parameters
----------
Format
------
Atomtype and associated Drude charge for each atomtype present in the
force field, with atomtype declarations separated by a newline.
Optionally, atomtype-specific and/or anisotropic spring coefficients may be
set; in this case, the file format is either 3- or 5-column, with the
atomtype-specific spring coefficient (or x, y, and
z local-coordinate spring coefficients, if anisotropic) declared after the
Drude charge. Comment lines are ignored. Units in a.u.
Example:
<atomtype1> <drude_charge1>
<atomtype2> <drude_charge2> [springcon2]
<atomtype3> <drude_charge3> [springcon3_x] [springcon3_y] [springcon3_z]
...
Returns
-------
'''
format_error = '''Improperly formatted Drude file "{}". Aside from
comments, each line of the .drude file should follow one of the
following formats:
Isotropic atomtypes with universal spring constant:
<atomtype1> <drude_charge1>
Isotropic atomtypes with atomtype-specific spring constant:
<atomtype2> <drude_charge2> <springcon2>
Anisotropic atomtypes with atomtype-specific spring constants:
<atomtype3> <drude_charge3> <springcon3_x> <springcon3_y> <springcon3_z>
Please fix the formatting error(s) and re-run POInter.
'''
overide_error = '''{0} attempts to set atomtype {1}'s Drude parameters to
{2}, however elsewhere you have set this parameter to {3}. Please fix
the conflicting parameter definitions and re-run POInter.
'''
missing_error = '''You have not defined a Drude charge for atomtype "{0}".
Please define this parameter in one of your .drude files -- {1}
-- and re-run POInter.
'''
drude_files = [self.inputdir + mon + self.drude_suffix for mon
in (self.mon1, self.mon2)]
self.drudes = {}
for i,ifile in enumerate(drude_files):
with open(ifile,'r') as f:
raw_lines = [line.split('#')[0] for line in f.readlines()]
data = [line.split() for line in raw_lines]
for line in data:
if not line or line[0].startswith('#'):
continue #ignore blank lines and comments
if len(line) == 2:
# Reading atomtype and Drude charge; springcon is
# inferred from self.springcon param
atom = line[0]
charge = float(line[1])
springcon = self.springcon
drudes = [charge, springcon, springcon, springcon]
elif len(line) == 3:
atom = line[0]
charge = float(line[1])
springcon = float(line[2])
drudes = [charge, springcon, springcon, springcon]
elif len(line) == 5:
atom = line[0]
charge = float(line[1])
springcons = [float(coeff) for coeff in line[2:]]
drudes = [charge] + springcons
else:
raise AssertionError(format_error.format(ifile))
# Check for conflicting parameter definitions
if self.ignorecase:
atom = atom.upper()
assert not (atom in self.drudes and not
np.allclose(self.drudes[atom] , drudes)),\
overide_error.format(ifile,atom,drudes,self.drudes[atom])
self.drudes[atom] = drudes
# Make sure all atomtypes have a drude parameter defined
for atom in self.atomtypes:
assert atom in self.drudes,\
missing_error.format(atom,'\n\t\t\t'.join([''] + list(set(drude_files))))
# Put drude charges and spring coefficients as a list of lists for
# each atom in each molecule; this is the format later required by the
# drude_oscillators module
self.springcon1 = []
self.springcon2 = []
self.drude_charges1 = []
self.drude_charges2 = []
for atom in self.atomtypes1:
params = self.drudes[atom]
self.drude_charges1.append(params[0])
self.springcon1.append(params[1:])
for atom in self.atomtypes2:
params = self.drudes[atom]
self.drude_charges2.append(params[0])
self.springcon2.append(params[1:])
self.springcon1 = np.array(self.springcon1)
self.springcon2 = np.array(self.springcon2)
self.drude_charges1 = np.array(self.drude_charges1)
self.drude_charges2 = np.array(self.drude_charges2)
return
####################################################################################################
####################################################################################################
####################################################################################################
####################################################################################################
####################################################################################################
class Energies():
'''Read parameters from user input.
References
----------
Attributes
----------
Methods
-------
Known Issues
------------
Units
-----
Atomic units are assumed throughout this module.
'''
def __init__(self, **kwargs
):
'''Initialize input variables and interaction function tensors.'''
###########################################################################
###################### Variable Initialization ############################
###########################################################################
###########################################################################
###########################################################################
################ Program-Defined Class Variables ##########################
###########################################################################
###########################################################################
###########################################################################
###################### Initialization Routines ############################
self.__dict__ = kwargs
for key in ['ignorecase','ncomponents']:
assert key in self.__dict__
###########################################################################
###########################################################################
return
####################################################################################################
def readEnergies(self,ifile):
'''Read in contents of the qm energy file, creating arrays
to store the qm energy and xyz coordinates of each data point in the
file.
Parameters
----------
None, aside from implicit dependence on energy_file
Returns
-------
None, though initializes values for the following class variables:
natoms[1,2] : int
Number of atoms in each monomer.
atoms[1,2] : list
Atomtype names for each atom in monomer.
ndatpts : int
Number of dimer configurations to fit.
xyz[1,2] : 3darray (ndatpts x natoms[1,2] x 3)
Cartesian positions for each atom in each monomer.
qm_energy : 2darray (ncomponents x ndatpts)
QM energies for each component (exchange etc.) and each dimer
configuration.
r12 : ndarray
Interatomic distances between all atom pairs i and j in
monomers 1 and 2, respectively.
atomtypes : list
List of all unique atomtypes read in through the QM energy file.
'''
print('Reading in information from the QM Energy file.')
try:
with open(ifile,'r') as f:
lines = [line.split() for line in f.readlines()]
# Number of atoms for each monomer
self.natoms1 = int(lines[0][0])
self.natoms2 = int(lines[self.natoms1+1][0])
except ValueError:
print('Error in reading the QM energy file.')
print('Did you switch the order of the parameter and energy files?\n')
raise
else:
# Obtain element names from energy file
self.atoms1 = [ lines[i][0] for i in range(1,self.natoms1+1)]
self.atoms2 = [ lines[i][0] for i in range(self.natoms1+2,self.natoms1+self.natoms2+2)]
if self.ignorecase:
self.atoms1 = [ atom.upper() for atom in self.atoms1 ]
self.atoms2 = [ atom.upper() for atom in self.atoms2 ]
# Obtain geometry arrays from energy_file
nlines = len(lines)
self.ndatpts = lines.count([]) # count number of blank lines
self.xyz1 = np.zeros((self.ndatpts,self.natoms1,3))
self.xyz2 = np.zeros((self.ndatpts,self.natoms2,3))
self.qm_energy = [ [] for i in range(self.ncomponents)]
for i in range(self.ndatpts):
# Monomer 1 geometry array:
for j in range(self.natoms1):
k = int(i*nlines/self.ndatpts+j+1)
self.xyz1[i,j,:] = np.array([float(lines[k][l]) for l in range(1,4)])
# Monomer 2 geometry array:
for j in range(self.natoms2):
k = int(i*nlines/self.ndatpts+j+self.natoms1+2)
self.xyz2[i,j,:] = np.array([float(lines[k][l]) for l in range(1,4)])
# QM Energy array:
j = int(i*nlines/self.ndatpts+self.natoms1+self.natoms2+2)
self.qm_energy[0].append(float(lines[j+1][1])) # exchange
self.qm_energy[1].append(float(lines[j][1])) # electrostatics
self.qm_energy[2].append(float(lines[j+4][1])+\
float(lines[j+5][1])) # induction
self.qm_energy[3].append(float(lines[j+17][1])) # dhf
self.qm_energy[4].append(float(lines[j+7][1])+\
float(lines[j+9][1])) # dispersion
self.qm_energy[6].append(float(lines[j+12][1])) # E1tot+E2tot
self.qm_energy[5] = np.zeros_like(self.qm_energy[6]) # Set residual energy to zero for now
self.qm_energy = np.array([np.array(i) for i in self.qm_energy])
# Use xyz1 and xyz2 arrays to compute the r array
self.r12 = (self.xyz1[:,:,np.newaxis,:] - self.xyz2[:,np.newaxis,:,:])**2
self.r12 = np.sqrt(np.sum(self.r12,axis=-1))
self.r12 = np.swapaxes(np.swapaxes(self.r12,0,2),0,1)
# Add dhf energy to E1tot+E2tot to get the total interaction
# energy:
self.qm_energy[6] = self.qm_energy[3] + self.qm_energy[6]
# Convert QM energies to Hartree from mH
self.qm_energy /= 1000
# Construct a list of all atoms present in the qm energy file
## self.atomtypes = set()
## for xyz in self.atoms1+self.atoms2:
## self.atomtypes.add(xyz)
## self.atomtypes = list(self.atomtypes)
# Use .sapt file to get eff_mu and eff_kt
# TODO: Allow user to specify eff_kt and eff_mu directly
etot_min = np.amin(self.qm_energy[6])
self.eff_kt = -etot_min*self.scale_weighting_temperature
self.eff_mu = 0.0
return
####################################################################################################
####################################################################################################
####################################################################################################
####################################################################################################
####################################################################################################
class Settings(object):
'''Read settings files from user input.
References
----------
Attributes
----------
Methods
-------
Known Issues
------------
Units
-----
Atomic units are assumed throughout this module.
'''
def __init__(self):
# Read in default POInter settings in case user doesn't specify
# certain settings themselves
self.settings_files = [default.__file__]
self.settings = self.get_variables_from_module(default)
# Set up list of recognized settings; these are primarily found in
# methods/default.py, but some additional required/optional arguments
# are explicitly listed below
self.recognized_settings = list(self.settings.keys())
self.required_user_settings = ['mon1','mon2']
self.optional_user_settings = ['energy_file',
'multipole_file1','multipole_file2',
'drude_read_file',
'ofile_prefix', 'ofile_suffix',
'output_file','output_settings_file',
'exponent_files',
'constraint_files']
self.recognized_settings += self.required_user_settings
self.recognized_settings += self.optional_user_settings
def __str__(self):
settings = ['Importing configuration settings from the following files:']
for f in self.settings_files:
settings.append(''.join(['\t',f]))
settings.append('\nCurrent Program Settings:')
template = '{:40s} {}'
kwargs = [ template.format(k,v) for (k,v) in self.settings.items()]
kwargs.sort()
settings += kwargs
settings.append('')
return '\n'.join(settings)
####################################################################################################
####################################################################################################
def getSettings(self,ifiles):
'''Read in settings from a list of custom settings file, as specified by the
user.
'''
sys.dont_write_bytecode = True
for ifile in ifiles:
ifile = ifile.replace('.py','')
config_file = ifile.replace('/','.')
try:
config = importlib.import_module(config_file, package=None)
except ImportError:
try: #attempt to import settings from the default settings library
config_file = '.' + config_file
config = importlib.import_module(config_file, package='pointer.methods')
except ImportError:
raise
self.settings_files.append(config.__file__)
userconfigs = self.get_variables_from_module(config)
self.settings.update(userconfigs)
sys.dont_write_bytecode = False
return self.settings
####################################################################################################
####################################################################################################
def checkSettings(self):
'''