-
Notifications
You must be signed in to change notification settings - Fork 38
/
SHARC_ORCA.py
executable file
·5428 lines (4635 loc) · 188 KB
/
SHARC_ORCA.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# ******************************************
#
# SHARC Program Suite
#
# Copyright (c) 2023 University of Vienna
#
# This file is part of SHARC.
#
# SHARC is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# SHARC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# inside the SHARC manual. If not, see <http://www.gnu.org/licenses/>.
#
# ******************************************
# Modules:
# Operating system, isfile and related routines, move files, create directories
import os
import shutil
# External Calls to MOLCAS
import subprocess as sp
# Command line arguments
import sys
# Regular expressions
import re
# debug print for dicts and arrays
import pprint
# sqrt and other math
import math
import cmath
# runtime measurement
import datetime
# copy of arrays of arrays
from copy import deepcopy
# parallel calculations
from multiprocessing import Pool
import time
# hostname
from socket import gethostname
# write debug traces when in pool threads
import traceback
# parse Python literals from input
import ast
import struct
# ======================================================================= #
version = '3.0'
versiondate = datetime.date(2023, 4, 1)
changelogstring = '''
16.05.2018: INITIAL VERSION
- functionality as SHARC_GAUSSIAN.py, minus restricted triplets
- QM/MM capabilities in combination with TINKER
- AO overlaps computed by PyQuante (only up to f functions)
11.09.2018:
- added "basis_per_element", "basis_per_atom", and "hfexchange" keywords
03.10.2018:
Update for Orca 4.1:
- SOC for restricted singlets and triplets
- gradients for restricted triplets
- multigrad features
- orca_fragovl instead of PyQuante
16.10.2018:
Update for Orca 4.1, after revisions:
- does not work with Orca 4.0 or lower (orca_fragovl unavailable, engrad/pcgrad files)
11.10.2020:
- COBRAMM can be used for QM/MM calculations
'''
# ======================================================================= #
# holds the system time when the script was started
starttime = datetime.datetime.now()
# global variables for printing (PRINT gives formatted output, DEBUG gives raw output)
DEBUG = False
PRINT = True
# hash table for conversion of multiplicity to the keywords used in MOLCAS
IToMult = {
1: 'Singlet',
2: 'Doublet',
3: 'Triplet',
4: 'Quartet',
5: 'Quintet',
6: 'Sextet',
7: 'Septet',
8: 'Octet',
9: '9-et',
10: '10-et',
11: '11-et',
12: '12-et',
13: '13-et',
14: '14-et',
15: '15-et',
16: '16-et',
17: '17-et',
18: '18-et',
19: '19-et',
20: '20-et',
21: '21-et',
22: '22-et',
23: '23-et',
'Singlet': 1,
'Doublet': 2,
'Triplet': 3,
'Quartet': 4,
'Quintet': 5,
'Sextet': 6,
'Septet': 7,
'Octet': 8
}
# hash table for conversion of polarisations to the keywords used in MOLCAS
IToPol = {
0: 'X',
1: 'Y',
2: 'Z',
'X': 0,
'Y': 1,
'Z': 2
}
# Number of frozen core orbitals
FROZENS = {'H': 0, 'He': 0,
'Li': 0, 'Be': 0, 'B': 1, 'C': 1, 'N': 1, 'O': 1, 'F': 1, 'Ne': 1,
'Na': 1, 'Mg': 1, 'Al': 5, 'Si': 5, 'P': 5, 'S': 5, 'Cl': 5, 'Ar': 5,
'K': 5, 'Ca': 5,
'Sc': 5, 'Ti': 5, 'V': 5, 'Cr': 5, 'Mn': 5, 'Fe': 5, 'Co': 5, 'Ni': 5, 'Cu': 5, 'Zn': 5,
'Ga': 9, 'Ge': 9, 'As': 9, 'Se': 9, 'Br': 9, 'Kr': 9,
'Rb': 9, 'Sr': 9,
'Y': 14, 'Zr': 14, 'Nb': 14, 'Mo': 14, 'Tc': 14, 'Ru': 14, 'Rh': 14, 'Pd': 14, 'Ag': 14, 'Cd': 14,
'In': 18, 'Sn': 18, 'Sb': 18, 'Te': 18, 'I': 18, 'Xe': 18,
'Cs': 18, 'Ba': 18,
'La': 18,
'Ce': 18, 'Pr': 18, 'Nd': 18, 'Pm': 18, 'Sm': 18, 'Eu': 18, 'Gd': 18, 'Tb': 18, 'Dy': 18, 'Ho': 18, 'Er': 18, 'Tm': 18, 'Yb': 18, 'Lu': 23,
'Hf': 23, 'Ta': 23, 'W': 23, 'Re': 23, 'Os': 23, 'Ir': 23, 'Pt': 23, 'Au': 23, 'Hg': 23,
'Tl': 34, 'Pb': 34, 'Bi': 34, 'Po': 34, 'At': 34, 'Rn': 34,
'Fr': 34, 'Ra': 34,
'Ac': 34,
'Th': 34, 'Pa': 34, 'U': 34, 'Np': 34, 'Pu': 34, 'Am': 34, 'Cm': 34, 'Bk': 34, 'Cf': 34, 'Es': 34, 'Fm': 34, 'Md': 34, 'No': 34, 'Lr': 34,
'Rf': 50, 'Db': 50, 'Sg': 50, 'Bh': 50, 'Hs': 50, 'Mt': 50, 'Ds': 50, 'Rg': 50, 'Cn': 50,
'Nh': 50, 'Fl': 50, 'Mc': 50, 'Lv': 50, 'Ts': 50, 'Og': 50
}
ATOMCHARGE = {'H': 1, 'He': 2,
'Li': 3, 'Be': 4, 'B': 5, 'C': 6, 'N': 7, 'O': 8, 'F': 9, 'Ne': 10,
'Na': 11, 'Mg': 12, 'Al': 13, 'Si': 14, 'P': 15, 'S': 16, 'Cl': 17, 'Ar': 18,
'K': 19, 'Ca': 20,
'Sc': 21, 'Ti': 22, 'V': 23, 'Cr': 24, 'Mn': 25, 'Fe': 26, 'Co': 27, 'Ni': 28, 'Cu': 29, 'Zn': 30,
'Ga': 31, 'Ge': 32, 'As': 33, 'Se': 34, 'Br': 35, 'Kr': 36,
'Rb': 37, 'Sr': 38,
'Y': 39, 'Zr': 40, 'Nb': 41, 'Mo': 42, 'Tc': 43, 'Ru': 44, 'Rh': 45, 'Pd': 46, 'Ag': 47, 'Cd': 48,
'In': 49, 'Sn': 50, 'Sb': 51, 'Te': 52, 'I': 53, 'Xe': 54,
'Cs': 55, 'Ba': 56,
'La': 57,
'Ce': 58, 'Pr': 59, 'Nd': 60, 'Pm': 61, 'Sm': 62, 'Eu': 63, 'Gd': 64, 'Tb': 65, 'Dy': 66, 'Ho': 67, 'Er': 68, 'Tm': 69, 'Yb': 70, 'Lu': 71,
'Hf': 72, 'Ta': 73, 'W': 74, 'Re': 75, 'Os': 76, 'Ir': 77, 'Pt': 78, 'Au': 79, 'Hg': 80,
'Tl': 81, 'Pb': 82, 'Bi': 83, 'Po': 84, 'At': 85, 'Rn': 86,
'Fr': 87, 'Ra': 88,
'Ac': 89,
'Th': 90, 'Pa': 91, 'U': 92, 'Np': 93, 'Pu': 94, 'Am': 95, 'Cm': 96, 'Bk': 97, 'Cf': 98, 'Es': 99, 'Fm': 100, 'Md': 101, 'No': 102, 'Lr': 103,
'Rf': 104, 'Db': 105, 'Sg': 106, 'Bh': 107, 'Hs': 108, 'Mt': 109, 'Ds': 110, 'Rg': 111, 'Cn': 112,
'Nh': 113, 'Fl': 114, 'Mc': 115, 'Lv': 116, 'Ts': 117, 'Og': 118
}
# conversion factors
au2a = 0.529177211
rcm_to_Eh = 4.556335e-6
D2au = 0.393430307
au2eV = 27.2113987622
kcal_to_Eh = 0.0015936011
# =============================================================================================== #
# =============================================================================================== #
# =========================================== general routines ================================== #
# =============================================================================================== #
# =============================================================================================== #
# ======================================================================= #
def readfile(filename):
try:
f = open(filename)
out = f.readlines()
f.close()
except IOError:
print('File %s does not exist!' % (filename))
sys.exit(12)
return out
# ======================================================================= #
def writefile(filename, content):
# content can be either a string or a list of strings
try:
f = open(filename, 'w')
if isinstance(content, list):
for line in content:
f.write(line)
elif isinstance(content, str):
f.write(content)
else:
print('Content %s cannot be written to file!' % (content))
sys.exit(13)
f.close()
except IOError:
print('Could not write to file %s!' % (filename))
sys.exit(14)
# ======================================================================= #
def isbinary(path):
return (re.search(r':.* text', sp.Popen(["file", '-L', path], stdout=sp.PIPE).stdout.read()) is None)
# ======================================================================= #
def eformat(f, prec, exp_digits):
'''Formats a float f into scientific notation with prec number of decimals and exp_digits number of exponent digits.
String looks like:
[ -][0-9]\\.[0-9]*E[+-][0-9]*
Arguments:
1 float: Number to format
2 integer: Number of decimals
3 integer: Number of exponent digits
Returns:
1 string: formatted number'''
s = "% .*e" % (prec, f)
mantissa, exp = s.split('e')
return "%sE%+0*d" % (mantissa, exp_digits + 1, int(exp))
# ======================================================================= #
def measuretime():
'''Calculates the time difference between global variable starttime and the time of the call of measuretime.
Prints the Runtime, if PRINT or DEBUG are enabled.
Arguments:
none
Returns:
1 float: runtime in seconds'''
endtime = datetime.datetime.now()
runtime = endtime - starttime
hours = runtime.seconds // 3600
minutes = runtime.seconds // 60 - hours * 60
seconds = runtime.seconds % 60
print('==> Runtime:\n%i Days\t%i Hours\t%i Minutes\t%i Seconds\n\n' % (runtime.days, hours, minutes, seconds))
total_seconds = runtime.days * 24 * 3600 + runtime.seconds + runtime.microseconds // 1.e6
return total_seconds
# ======================================================================= #
def removekey(d, key):
'''Removes an entry from a dictionary and returns the dictionary.
Arguments:
1 dictionary
2 anything which can be a dictionary keyword
Returns:
1 dictionary'''
if key in d:
r = dict(d)
del r[key]
return r
return d
# ======================================================================= # OK
def containsstring(string, line):
'''Takes a string (regular expression) and another string. Returns True if the first string is contained in the second string.
Arguments:
1 string: Look for this string
2 string: within this string
Returns:
1 boolean'''
a = re.search(string, line)
if a:
return True
else:
return False
# =============================================================================================== #
# =============================================================================================== #
# ============================= iterator routines ============================================== #
# =============================================================================================== #
# =============================================================================================== #
# ======================================================================= #
def itmult(states):
for i in range(len(states)):
if states[i] < 1:
continue
yield i + 1
return
# ======================================================================= #
def itnmstates(states):
for i in range(len(states)):
if states[i] < 1:
continue
for k in range(i + 1):
for j in range(states[i]):
yield i + 1, j + 1, k - i / 2.
return
# =============================================================================================== #
# =============================================================================================== #
# =========================================== print routines ==================================== #
# =============================================================================================== #
# =============================================================================================== #
# ======================================================================= #
def printheader():
'''Prints the formatted header of the log file. Prints version number and version date
Takes nothing, returns nothing.'''
print(starttime, gethostname(), os.getcwd())
if not PRINT:
return
string = '\n'
string += ' ' + '=' * 80 + '\n'
string += '||' + ' ' * 80 + '||\n'
string += '||' + ' ' * 28 + 'SHARC - ORCA - Interface' + ' ' * 28 + '||\n'
string += '||' + ' ' * 80 + '||\n'
string += '||' + ' ' * 14 + 'Authors: Sebastian Mai, Lea Ibele, and Moritz Heindl' + ' ' * 14 + '||\n'
string += '||' + ' ' * 80 + '||\n'
string += '||' + ' ' * (36 - (len(version) + 1) // 2) + 'Version: %s' % (version) + ' ' * (35 - (len(version)) // 2) + '||\n'
lens = len(versiondate.strftime("%d.%m.%y"))
string += '||' + ' ' * (37 - lens // 2) + 'Date: %s' % (versiondate.strftime("%d.%m.%y")) + ' ' * (37 - (lens + 1) // 2) + '||\n'
string += '||' + ' ' * 80 + '||\n'
string += ' ' + '=' * 80 + '\n\n'
print(string)
if DEBUG:
print(changelogstring)
# ======================================================================= #
def printQMin(QMin):
if not PRINT:
return
print('==> QMin Job description for:\n%s' % (QMin['comment']))
string = 'Mode: '
if 'init' in QMin:
string += '\tINIT'
if 'restart' in QMin:
string += '\tRESTART'
if 'samestep' in QMin:
string += '\tSAMESTEP'
if 'newstep' in QMin:
string += '\tNEWSTEP'
string += '\nTasks: '
if 'h' in QMin:
string += '\tH'
if 'soc' in QMin:
string += '\tSOC'
if 'dm' in QMin:
string += '\tDM'
if 'grad' in QMin:
string += '\tGrad'
if 'nacdr' in QMin:
string += '\tNac(ddr)'
if 'nacdt' in QMin:
string += '\tNac(ddt)'
if 'overlap' in QMin:
string += '\tOverlap'
if 'angular' in QMin:
string += '\tAngular'
if 'ion' in QMin:
string += '\tDyson'
if 'dmdr' in QMin:
string += '\tDM-Grad'
if 'socdr' in QMin:
string += '\tSOC-Grad'
if 'theodore' in QMin:
string += '\tTheoDORE'
if 'phases' in QMin:
string += '\tPhases'
print(string)
string = 'States: '
for i in itmult(QMin['states']):
string += '% 2i %7s ' % (QMin['states'][i - 1], IToMult[i])
print(string)
string = 'Charges: '
for i in itmult(QMin['states']):
string += '%+2i %7s ' % (QMin['chargemap'][i], '')
print(string)
string = 'Restricted: '
for i in itmult(QMin['states']):
string += '%5s ' % (QMin['jobs'][QMin['multmap'][i]]['restr'])
print(string)
string = 'Method: \t'
if QMin['template']['no_tda']:
string += 'TD-'
else:
string += 'TDA-'
string += QMin['template']['functional'].split()[0].upper()
string += '/%s' % (QMin['template']['basis'])
parts = []
if QMin['template']['dispersion']:
parts.append(QMin['template']['dispersion'].split()[0].upper())
if QMin['template']['qmmm']:
parts.append('QM/MM')
if len(parts) > 0:
string += '\t('
string += ','.join(parts)
string += ')'
print(string)
string = 'Found Geo'
if 'veloc' in QMin:
string += ' and Veloc! '
else:
string += '! '
string += 'NAtom is %i.\n' % (QMin['natom_orig'])
print(string)
string = 'Geometry in Bohrs (%i atoms):\n' % QMin['natom_orig']
if DEBUG:
for i in range(QMin['natom_orig']):
string += '%2s ' % (QMin['geo_orig'][i][0])
for j in range(3):
string += '% 7.4f ' % (QMin['geo_orig'][i][j + 1])
string += '\n'
else:
for i in range(min(QMin['natom_orig'], 5)):
string += '%2s ' % (QMin['geo_orig'][i][0])
for j in range(3):
string += '% 7.4f ' % (QMin['geo_orig'][i][j + 1])
string += '\n'
if QMin['natom_orig'] > 5:
string += '.. ... ... ...\n'
string += '%2s ' % (QMin['geo_orig'][-1][0])
for j in range(3):
string += '% 7.4f ' % (QMin['geo_orig'][-1][j + 1])
string += '\n'
print(string)
if 'veloc' in QMin and DEBUG:
string = ''
for i in range(QMin['natom_orig']):
string += '%s ' % (QMin['geo_orig'][i][0])
for j in range(3):
string += '% 7.4f ' % (QMin['veloc'][i][j])
string += '\n'
print(string)
if 'grad' in QMin:
string = 'Gradients requested: '
for i in range(1, QMin['nmstates'] + 1):
if i in QMin['grad']:
string += 'X '
else:
string += '. '
string += '\n'
print(string)
print('State map:')
pprint.pprint(QMin['statemap'])
print
for i in sorted(QMin):
if not any([i == j for j in ['h', 'dm', 'soc', 'dmdr', 'socdr', 'theodore', 'geo', 'veloc', 'states', 'comment', 'grad', 'nacdr', 'ion', 'overlap', 'template', 'statemap', 'pointcharges', 'geo_orig', 'qmmm']]):
if not any([i == j for j in ['ionlist']]) or DEBUG:
string = i + ': '
string += str(QMin[i])
print(string)
print('\n')
sys.stdout.flush()
# ======================================================================= #
def printcomplexmatrix(matrix, states):
'''Prints a formatted matrix. Zero elements are not printed, blocks of different mult and MS are delimited by dashes. Also prints a matrix with the imaginary parts, of any one element has non-zero imaginary part.
Arguments:
1 list of list of complex: the matrix
2 list of integers: states specs'''
nmstates = 0
for i in range(len(states)):
nmstates += states[i] * (i + 1)
string = 'Real Part:\n'
string += '-' * (11 * nmstates + nmstates // 3)
string += '\n'
istate = 0
for imult, i, ms in itnmstates(states):
jstate = 0
string += '|'
for jmult, j, ms2 in itnmstates(states):
if matrix[istate][jstate].real == 0.:
string += ' ' * 11
else:
string += '% .3e ' % (matrix[istate][jstate].real)
if j == states[jmult - 1]:
string += '|'
jstate += 1
string += '\n'
if i == states[imult - 1]:
string += '-' * (11 * nmstates + nmstates // 3)
string += '\n'
istate += 1
print(string)
imag = False
string = 'Imaginary Part:\n'
string += '-' * (11 * nmstates + nmstates // 3)
string += '\n'
istate = 0
for imult, i, ms in itnmstates(states):
jstate = 0
string += '|'
for jmult, j, ms2 in itnmstates(states):
if matrix[istate][jstate].imag == 0.:
string += ' ' * 11
else:
imag = True
string += '% .3e ' % (matrix[istate][jstate].imag)
if j == states[jmult - 1]:
string += '|'
jstate += 1
string += '\n'
if i == states[imult - 1]:
string += '-' * (11 * nmstates + nmstates // 3)
string += '\n'
istate += 1
string += '\n'
if imag:
print(string)
# ======================================================================= #
def printgrad(grad, natom, geo):
'''Prints a gradient or nac vector. Also prints the atom elements. If the gradient is identical zero, just prints one line.
Arguments:
1 list of list of float: gradient
2 integer: natom
3 list of list: geometry specs'''
string = ''
iszero = True
for atom in range(natom):
if not DEBUG:
if atom == 5:
string += '...\t...\t ...\t ...\t ...\n'
if 5 <= atom < natom - 1:
continue
string += '%i\t%s\t' % (atom + 1, geo[atom][0])
for xyz in range(3):
if grad[atom][xyz] != 0:
iszero = False
g = grad[atom][xyz]
if isinstance(g, float):
string += '% .5f\t' % (g)
elif isinstance(g, complex):
string += '% .5f\t% .5f\t\t' % (g.real, g.imag)
string += '\n'
if iszero:
print('\t\t...is identical zero...\n')
else:
print(string)
# ======================================================================= #
def printtheodore(matrix, QMin):
string = '%6s ' % 'State'
for i in QMin['template']['theodore_prop']:
string += '%6s ' % i
for i in range(len(QMin['template']['theodore_fragment'])):
for j in range(len(QMin['template']['theodore_fragment'])):
string += ' Om%1i%1i ' % (i + 1, j + 1)
string += '\n' + '-------' * (1 + QMin['template']['theodore_n']) + '\n'
istate = 0
for imult, i, ms in itnmstates(QMin['states']):
istate += 1
string += '%6i ' % istate
for i in matrix[istate - 1]:
string += '%6.4f ' % i.real
string += '\n'
print(string)
# ======================================================================= #
def printQMout(QMin, QMout):
'''If PRINT, prints a summary of all requested QM output values. Matrices are formatted using printcomplexmatrix, vectors using printgrad.
Arguments:
1 dictionary: QMin
2 dictionary: QMout'''
# if DEBUG:
# pprint.pprint(QMout)
if not PRINT:
return
states = QMin['states']
nstates = QMin['nstates']
nmstates = QMin['nmstates']
natom = QMin['natom']
print('\n\n>>>>>>>>>>>>> Results\n')
# Hamiltonian matrix, real or complex
if 'h' in QMin or 'soc' in QMin:
eshift = math.ceil(QMout['h'][0][0].real)
print('=> Hamiltonian Matrix:\nDiagonal Shift: %9.2f' % (eshift))
matrix = deepcopy(QMout['h'])
for i in range(nmstates):
matrix[i][i] -= eshift
printcomplexmatrix(matrix, states)
# Dipole moment matrices
if 'dm' in QMin:
print('=> Dipole Moment Matrices:\n')
for xyz in range(3):
print('Polarisation %s:' % (IToPol[xyz]))
matrix = QMout['dm'][xyz]
printcomplexmatrix(matrix, states)
# Gradients
if 'grad' in QMin:
print('=> Gradient Vectors:\n')
istate = 0
for imult, i, ms in itnmstates(states):
print('%s\t%i\tMs= % .1f:' % (IToMult[imult], i, ms))
printgrad(QMout['grad'][istate], natom, QMin['geo'])
istate += 1
# Overlaps
if 'overlap' in QMin:
print('=> Overlap matrix:\n')
matrix = QMout['overlap']
printcomplexmatrix(matrix, states)
if 'phases' in QMout:
print('=> Wavefunction Phases:\n')
for i in range(nmstates):
print('% 3.1f % 3.1f' % (QMout['phases'][i].real, QMout['phases'][i].imag))
print('\n')
# Spin-orbit coupling derivatives
if 'socdr' in QMin:
print('=> Spin-Orbit Gradient Vectors:\n')
istate = 0
for imult, i, ims in itnmstates(states):
jstate = 0
for jmult, j, jms in itnmstates(states):
print('%s\t%i\tMs= % .1f -- %s\t%i\tMs= % .1f:' % (IToMult[imult], i, ims, IToMult[jmult], j, jms))
printgrad(QMout['socdr'][istate][jstate], natom, QMin['geo'])
jstate += 1
istate += 1
# Dipole moment derivatives
if 'dmdr' in QMin:
print('=> Dipole moment derivative vectors:\n')
istate = 0
for imult, i, msi in itnmstates(states):
jstate = 0
for jmult, j, msj in itnmstates(states):
if imult == jmult and msi == msj:
for ipol in range(3):
print('%s\tStates %i - %i\tMs= % .1f\tPolarization %s:' % (IToMult[imult], i, j, msi, IToPol[ipol]))
printgrad(QMout['dmdr'][ipol][istate][jstate], natom, QMin['geo'])
jstate += 1
istate += 1
# Property matrix (dyson norms)
if 'ion' in QMin and 'prop' in QMout:
print('=> Property matrix:\n')
matrix = QMout['prop']
printcomplexmatrix(matrix, states)
# TheoDORE
if 'theodore' in QMin:
print('=> TheoDORE results:\n')
matrix = QMout['theodore']
printtheodore(matrix, QMin)
sys.stdout.flush()
# =============================================================================================== #
# =============================================================================================== #
# ======================================= Matrix initialization ================================= #
# =============================================================================================== #
# =============================================================================================== #
# ======================================================================= # OK
def makecmatrix(a, b):
'''Initialises a complex axb matrix.
Arguments:
1 integer: first dimension
2 integer: second dimension
Returns;
1 list of list of complex'''
mat = [[complex(0., 0.) for i in range(a)] for j in range(b)]
return mat
# ======================================================================= # OK
def makermatrix(a, b):
'''Initialises a real axb matrix.
Arguments:
1 integer: first dimension
2 integer: second dimension
Returns;
1 list of list of real'''
mat = [[0. for i in range(a)] for j in range(b)]
return mat
# =============================================================================================== #
# =============================================================================================== #
# =========================================== QMout writing ===================================== #
# =============================================================================================== #
# =============================================================================================== #
# ======================================================================= #
def writeQMout(QMin, QMout, QMinfilename):
'''Writes the requested quantities to the file which SHARC reads in. The filename is QMinfilename with everything after the first dot replaced by "out".
Arguments:
1 dictionary: QMin
2 dictionary: QMout
3 string: QMinfilename'''
k = QMinfilename.find('.')
if k == -1:
outfilename = QMinfilename + '.out'
else:
outfilename = QMinfilename[:k] + '.out'
if PRINT:
print('===> Writing output to file %s in SHARC Format\n' % (outfilename))
string = ''
if 'h' in QMin or 'soc' in QMin:
string += writeQMoutsoc(QMin, QMout)
if 'dm' in QMin:
string += writeQMoutdm(QMin, QMout)
if 'grad' in QMin:
string += writeQMoutgrad(QMin, QMout)
if 'overlap' in QMin:
string += writeQMoutnacsmat(QMin, QMout)
if 'socdr' in QMin:
string += writeQMoutsocdr(QMin, QMout)
if 'dmdr' in QMin:
string += writeQMoutdmdr(QMin, QMout)
if 'ion' in QMin:
string += writeQMoutprop(QMin, QMout)
if 'theodore' in QMin or QMin['template']['qmmm']:
string += writeQMoutTHEODORE(QMin, QMout)
if 'phases' in QMin:
string += writeQmoutPhases(QMin, QMout)
if 'grad' in QMin:
if QMin['template']['cobramm']:
writeQMoutgradcobramm(QMin, QMout)
string += writeQMouttime(QMin, QMout)
outfile = os.path.join(QMin['pwd'], outfilename)
writefile(outfile, string)
return
# ======================================================================= #
def writeQMoutsoc(QMin, QMout):
'''Generates a string with the Spin-Orbit Hamiltonian in SHARC format.
The string starts with a ! followed by a flag specifying the type of data. In the next line, the dimensions of the matrix are given, followed by nmstates blocks of nmstates elements. Blocks are separated by a blank line.
Arguments:
1 dictionary: QMin
2 dictionary: QMout
Returns:
1 string: multiline string with the SOC matrix'''
states = QMin['states']
nstates = QMin['nstates']
nmstates = QMin['nmstates']
natom = QMin['natom']
string = ''
string += '! %i Hamiltonian Matrix (%ix%i, complex)\n' % (1, nmstates, nmstates)
string += '%i %i\n' % (nmstates, nmstates)
for i in range(nmstates):
for j in range(nmstates):
string += '%s %s ' % (eformat(QMout['h'][i][j].real, 9, 3), eformat(QMout['h'][i][j].imag, 9, 3))
string += '\n'
string += '\n'
return string
# ======================================================================= #
def writeQMoutdm(QMin, QMout):
'''Generates a string with the Dipole moment matrices in SHARC format.
The string starts with a ! followed by a flag specifying the type of data. In the next line, the dimensions of the matrix are given, followed by nmstates blocks of nmstates elements. Blocks are separated by a blank line. The string contains three such matrices.
Arguments:
1 dictionary: QMin
2 dictionary: QMout
Returns:
1 string: multiline string with the DM matrices'''
states = QMin['states']
nstates = QMin['nstates']
nmstates = QMin['nmstates']
natom = QMin['natom']
string = ''
string += '! %i Dipole Moment Matrices (3x%ix%i, complex)\n' % (2, nmstates, nmstates)
for xyz in range(3):
string += '%i %i\n' % (nmstates, nmstates)
for i in range(nmstates):
for j in range(nmstates):
string += '%s %s ' % (eformat(QMout['dm'][xyz][i][j].real, 9, 3), eformat(QMout['dm'][xyz][i][j].imag, 9, 3))
string += '\n'
# string+='\n'
string += '\n'
return string
# ======================================================================= #
def writeQMoutgrad(QMin, QMout):
'''Generates a string with the Gradient vectors in SHARC format.
The string starts with a ! followed by a flag specifying the type of data. On the next line, natom and 3 are written, followed by the gradient, with one line per atom and a blank line at the end. Each MS component shows up (nmstates gradients are written).
Arguments:
1 dictionary: QMin
2 dictionary: QMout
Returns:
1 string: multiline string with the Gradient vectors'''
states = QMin['states']
nstates = QMin['nstates']
nmstates = QMin['nmstates']
natom = QMin['natom']
string = ''
string += '! %i Gradient Vectors (%ix%ix3, real)\n' % (3, nmstates, natom)
i = 0
for imult, istate, ims in itnmstates(states):
string += '%i %i ! %i %i %i\n' % (natom, 3, imult, istate, ims)
for atom in range(natom):
for xyz in range(3):
string += '%s ' % (eformat(QMout['grad'][i][atom][xyz], 9, 3))
string += '\n'
# string+='\n'
i += 1
string += '\n'
return string
# =================================== #
def writeQMoutgradcobramm(QMin, QMout):
'''Generates a string with the Gradient vectors in SHARC format.
The string starts with a ! followed by a flag specifying the type of data. On the next line, natom and 3 are written, followed by the gradient, with one line per atom and a blank line at the end. Each MS component shows up (nmstates gradients are written).
Arguments:
1 dictionary: QMin
2 dictionary: QMout
Returns:
1 string: multiline string with the Gradient vectors'''
states = QMin['states']
nstates = QMin['nstates']
nmstates = QMin['nmstates']
natom = len(QMout['pcgrad'][0])
string = ''
# string+='! %i Gradient Vectors (%ix%ix3, real)\n' % (3,nmstates,natom)
i = 0
for imult, istate, ims in itnmstates(states):
string += '%i %i ! %i %i %i\n' % (natom, 3, imult, istate, ims)
for atom in range(natom):
for xyz in range(3):
print((eformat(QMout['pcgrad'][i][atom][xyz], 9, 3)), i, atom)
string += '%s ' % (eformat(QMout['pcgrad'][i][atom][xyz], 9, 3))
string += '\n'
# string+='\n'
i += 1
string += '\n'
writefile("grad_charges", string)
# ======================================================================= #
def writeQMoutnacsmat(QMin, QMout):
'''Generates a string with the adiabatic-diabatic transformation matrix in SHARC format.
The string starts with a ! followed by a flag specifying the type of data. In the next line, the dimensions of the matrix are given, followed by nmstates blocks of nmstates elements. Blocks are separated by a blank line.
Arguments:
1 dictionary: QMin
2 dictionary: QMout
Returns:
1 string: multiline string with the transformation matrix'''
states = QMin['states']
nstates = QMin['nstates']
nmstates = QMin['nmstates']
natom = QMin['natom']
string = ''
string += '! %i Overlap matrix (%ix%i, complex)\n' % (6, nmstates, nmstates)
string += '%i %i\n' % (nmstates, nmstates)
for j in range(nmstates):
for i in range(nmstates):
string += '%s %s ' % (eformat(QMout['overlap'][j][i].real, 9, 3), eformat(QMout['overlap'][j][i].imag, 9, 3))
string += '\n'
string += '\n'
return string
# ======================================================================= #
def writeQMoutdmdr(QMin, QMout):
states = QMin['states']
nmstates = QMin['nmstates']
natom = QMin['natom']
string = ''
string += '! %i Dipole moment derivatives (%ix%ix3x%ix3, real)\n' % (12, nmstates, nmstates, natom)
i = 0
for imult, istate, ims in itnmstates(states):
j = 0
for jmult, jstate, jms in itnmstates(states):
for ipol in range(3):
string += '%i %i ! m1 %i s1 %i ms1 %i m2 %i s2 %i ms2 %i pol %i\n' % (natom, 3, imult, istate, ims, jmult, jstate, jms, ipol)
for atom in range(natom):
for xyz in range(3):
string += '%s ' % (eformat(QMout['dmdr'][ipol][i][j][atom][xyz], 12, 3))
string += '\n'
string += ''
j += 1
i += 1
string += '\n'
return string
# ======================================================================= #
def writeQMoutsocdr(QMin, QMout):
states = QMin['states']
nmstates = QMin['nmstates']
natom = QMin['natom']
string = ''