-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdNDP_2.py
1791 lines (1510 loc) · 54.4 KB
/
AdNDP_2.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 os
import sys
import re
import copy
import itertools
import collections
import argparse
import warnings
import shutil
import time
if sys.version_info[0] < 3:
raise Exception("Must be using Python 3 version or higher!")
import numpy as np
import pickle
EXIT_WORDS = {"exit", "quit", "q"}
ADNDP_BASENAME = "AdNDP.in"
DISTANCE_BASENAME = "Distance.in"
RESID_BASENAME = "Resid.data"
ALPHA_SEP = " ******* Alpha spin orbitals *******"
BETA_SEP = " ******* Beta spin orbitals *******"
def matrix_recalculations(
atom_centers,
matrix,
partition,
matrix_indexes,
partition_indexes,
partition_is_dst=False,
only_first=False
):
for idx_i, atom_center_i in enumerate(atom_centers):
p_i_start, p_i_end = partition_indexes[idx_i]
m_i_start, m_i_end = matrix_indexes[atom_center_i]
for idx_j, atom_center_j in enumerate(atom_centers):
p_j_start, p_j_end = partition_indexes[idx_j]
m_j_start, m_j_end = matrix_indexes[atom_center_j]
if partition_is_dst:
partition[p_i_start:p_i_end, p_j_start:p_j_end] = (
matrix[m_i_start:m_i_end, m_j_start:m_j_end]
)
else:
matrix[m_i_start:m_i_end, m_j_start:m_j_end] = (
partition[p_i_start:p_i_end, p_j_start:p_j_end]
)
# In some cases there should happen recalculation only on first index
if only_first:
break
def separate_alpha_beta(nbo_path, mo_path, adndp_path, distance_path):
alpha_dir = os.path.abspath("alpha")
beta_dir = os.path.abspath("beta")
if not os.path.exists(alpha_dir):
os.mkdir(alpha_dir)
if not os.path.exists(beta_dir):
os.mkdir(beta_dir)
nbo_basename = os.path.basename(nbo_path)
mo_basename = os.path.basename(mo_path)
adndp_basename = os.path.basename(adndp_path)
distance_basename = os.path.basename(distance_path)
alpha_mo_path = os.path.join(alpha_dir, mo_basename)
beta_mo_path = os.path.join(beta_dir, mo_basename)
alpha_nbo_path = os.path.join(alpha_dir, nbo_basename)
beta_nbo_path = os.path.join(beta_dir, nbo_basename)
alpha_adndp_path = os.path.join(alpha_dir, adndp_basename)
beta_adndp_path = os.path.join(beta_dir, adndp_basename)
alpha_distance_path = os.path.join(alpha_dir, distance_basename)
beta_distance_path = os.path.join(beta_dir, distance_basename)
shutil.copyfile(mo_path, alpha_mo_path)
shutil.copyfile(mo_path, beta_mo_path)
shutil.copyfile(adndp_path, alpha_adndp_path)
shutil.copyfile(adndp_path, beta_adndp_path)
shutil.copyfile(distance_path, alpha_distance_path)
shutil.copyfile(distance_path, beta_distance_path)
with open(alpha_distance_path, "a") as stream:
stream.write("\nalpha")
with open(beta_distance_path, "a") as stream:
stream.write("\nbeta")
alpha_beta_started = False
in_alpha_part = True
with (
open(alpha_nbo_path, "w")
) as a_stream, (
open(beta_nbo_path, "w")
) as b_stream, (
open(nbo_path, "r")
) as nbo_stream:
for line in nbo_stream.readlines():
if not alpha_beta_started:
if not line.startswith(ALPHA_SEP):
a_stream.write(line)
b_stream.write(line)
continue
alpha_beta_started = True
if in_alpha_part:
if not line.startswith(BETA_SEP):
a_stream.write(line)
continue
in_alpha_part = False
b_stream.write(line)
class LogsReader(object):
def __init__(self, nbo_path, mo_path):
self._nbo_path = nbo_path
self._mo_path = mo_path
@property
def nbo_path(self):
return self._nbo_path
@property
def mo_path(self):
return self._mo_path
def nbo_readlines(self):
with open(self.nbo_path, "r") as stream:
for line in stream:
yield line
def mo_readlines(self):
with open(self.mo_path, "r") as stream:
for line in stream:
yield line
def create_adndp(self):
# amount of atoms
amount_of_atoms = None
# Valence electron pairs
valence_electron_pairs = None
# Total electron pairs
total_electro_pairs = None
# Total amount of basis functions
total_basis_funcs = None
# Basis functions per atom
basis_funcs_per_atom = None
lines_queue = collections.deque(self.nbo_readlines())
while lines_queue:
line = lines_queue.popleft()
if amount_of_atoms is None and "NAtoms" in line:
amount_of_atoms = int(line.split()[1])
if (
valence_electron_pairs is None
and line.startswith(" Valence")
):
valence_electron_pairs = int(line[51:55]) // 2
if total_basis_funcs is None and "basis functions," in line:
total_basis_funcs = int(line[:7])
if (
total_electro_pairs is None
and "alpha electrons" in line
and "beta electrons" in line
):
total_electro_pairs = (int(line[:7]) + int(line[24:32])) // 2
if (
line.startswith(
" NAO Atom No lang Type(AO) Occupancy"
)
or line.startswith(" NAO Atom No lang Type(AO)")
):
# Pop one line (for some reason?)
lines_queue.popleft()
# Basis functions per atom
basis_funcs_per_atom = [0 for n in range(amount_of_atoms)]
for idx in range(amount_of_atoms):
counter = 0
while len(lines_queue.popleft()) > 2:
counter += 1
basis_funcs_per_atom[idx] = counter
basis_funcs_per_atom = basis_funcs_per_atom or []
thresholds = [0.0 for _ in basis_funcs_per_atom]
return AdNDPContent(
self.nbo_path,
self.mo_path,
amount_of_atoms or 0,
valence_electron_pairs or 0,
total_electro_pairs or 0,
total_basis_funcs or 0,
basis_funcs_per_atom,
thresholds
)
def find_residual_density(self, system, spin):
if system == "OS":
for line in self.nbo_readlines():
if (
"alpha electrons" not in line
or "beta electrons" not in line
):
continue
line_parts = line.split()
if spin == "A":
return int(line_parts[0])
return int(line_parts[3])
return None
alpha = None
beta = None
for line in self.nbo_readlines():
if "alpha electrons" in line:
alpha = int(line[:7])
beta = int(line[26:32])
if alpha > beta:
return alpha + beta
break
elif " Number of Alpha electrons" in line:
alpha = int(line[34:])
elif " Number of Beta electrons" in line:
beta = int(line[34:])
if alpha > beta:
return alpha + beta
break
return None
def get_distince_matris(self, amount_of_atoms):
some_calc = (
-(-amount_of_atoms // 5)
* (amount_of_atoms + 1)
- 5
* ((-(-amount_of_atoms // 5) - 1) / 2)
* (-(-amount_of_atoms // 5)) + 1
)
dist = []
dist_matrix_started = False
dist_matrix_counter = 0
for line in self.nbo_readlines():
if not dist_matrix_started:
if not line.startswith(
" Distance matrix (angstroms)"
):
continue
dist_matrix_started = True
dist_matrix_counter += 1
if (
dist_matrix_counter >= 2
and not (line.startswith(" "))
and dist_matrix_counter <= some_calc
):
dist.append(list(map(float, line[12:].split())))
return dist
def get_dmnao(self):
# TNV reading DMNAO from nbo.out
dmnao = []
dmnao_enabled = False
dmnao_counter = 0
for line in self.nbo_readlines():
if (
line.startswith(" NAO")
or line.startswith(" NAO")
):
dmnao_enabled = True
dmnao_counter = 0
if not dmnao_enabled:
continue
dmnao_counter += 1
if dmnao_counter >= 3:
if len(line) > 1:
dmnao.append(list(map(float, line[16:].split())))
else:
dmnao_enabled = False
return dmnao
def get_dmao(self):
dmao = []
dmao_enabled = False
dmao_counter = 0
for line in self.nbo_readlines():
if (
line.startswith(" AO")
or line.startswith(" AO")
):
dmao_enabled = True
dmao_counter = 0
if not dmao_enabled:
continue
dmao_counter += 1
if dmao_counter < 3:
continue
if len(line) > 1:
if re.search("(\d)(?:\-{1})(\d)", line) is not None:
line = re.sub("(\d)(?:\-{1})(\d)", r"\1 -\2", line)
dmao.append(list(map(float, line[16:].split())))
else:
dmao_enabled = False
return dmao
class AdNDPContent(object):
def __init__(
self,
nbo_path,
mo_path,
amount_of_atoms,
valence_pairs,
total_pairs,
total_basis_funcs,
basis_funcs_per_atom,
thresholds
):
self.nbo_path = nbo_path
self.mo_path = mo_path
self.amount_of_atoms = amount_of_atoms
self.valence_pairs = valence_pairs
self.total_pairs = total_pairs
self.total_basis_funcs = total_basis_funcs
self.basis_funcs_per_atom = basis_funcs_per_atom
self.thresholds = thresholds
self._indexes_d_for = None
self._log_reader = None
self._distince_matris = None
@property
def log_reader(self):
if self._log_reader is None:
self._log_reader = LogsReader(self.nbo_path, self.mo_path)
return self._log_reader
def get_residual_density(self, system, spin):
residual_density = self.log_reader.find_residual_density(system, spin)
if residual_density is None:
residual_density = 2 * self.total_pairs
return residual_density
@property
def distince_matris(self):
if self._distince_matris is None:
self._distince_matris = self.log_reader.get_distince_matris(
self.amount_of_atoms
)
return self._distince_matris
@property
def indexes_d_for(self):
if self._indexes_d_for is None:
self._indexes_d_for = [
(
sum(self.basis_funcs_per_atom[:idx]),
sum(self.basis_funcs_per_atom[:idx + 1])
)
for idx in range(self.amount_of_atoms)
]
return self._indexes_d_for
def create_distance(self, mode, resid_save, spin):
return DistanceContent(
copy.deepcopy(self.thresholds), mode, resid_save, spin
)
def save_to_file(self, adndp_path):
joined_bea = "\n".join(
str(value) for value in self.basis_funcs_per_atom
)
joined_thresholds = "\n".join(
str(value) for value in self.thresholds
)
adndp_content = (
"NBO filename\n"
f"{self.nbo_path}\n"
"Number of atoms\n"
f"{self.amount_of_atoms}\n"
"Amount of valence electronic pairs\n"
f"{self.valence_pairs}\n"
"Total amount of electronic pairs\n"
f"{self.total_pairs}\n"
"Total amount of basis functions\n"
f"{self.total_basis_funcs}\n"
"Amount of basis functions on each atom\n"
f"{joined_bea}\n"
"Occupation number thresholds\n"
f"{joined_thresholds}\n"
"CMO filename\n"
f"{self.mo_path}\n"
)
with open(adndp_path, "w") as stream:
stream.write(adndp_content)
@classmethod
def from_file(cls, adndp_path):
# TNV AdNDP.in reading + generating Resid.Dens.
with open(adndp_path, "r") as stream:
adndp_lines = stream.readlines()
nbo_path = adndp_lines[1].strip()
mo_path = adndp_lines[-1].strip()
amount_of_atoms = int(adndp_lines[3])
valence_pairs = int(adndp_lines[5])
total_pairs = int(adndp_lines[7])
total_basis_funcs = int(adndp_lines[9])
basis_funcs_per_atom = []
for idx in range(amount_of_atoms):
basis_funcs_per_atom.append(int(adndp_lines[idx + 11]))
thresholds = []
for idx in range(amount_of_atoms):
thresholds.append(float(adndp_lines[idx + amount_of_atoms + 12]))
# TNV input warning
if sum(basis_funcs_per_atom) != total_basis_funcs:
print("WARNING! Number of total basis functions is wrong!")
for threshold in thresholds:
if threshold >= 2:
print("WARNING! Thresholds can not be higher or equal 2!")
return cls(
nbo_path,
mo_path,
amount_of_atoms,
valence_pairs,
total_pairs,
total_basis_funcs,
basis_funcs_per_atom,
thresholds
)
class DistanceContent(object):
def __init__(self, thresholds, mode, resid_save, spin):
system = "OS"
core_threshold = 0.99
if spin == "0":
system = "CS"
core_threshold = 1.999
self.thresholds = thresholds
self.mode = mode
self.resid_save = resid_save
self.spin = spin
self.system = system
self.core_threshold = core_threshold
def fill_thresholds_from_adndp(self, adndp_content):
thresholds_len = len(self.thresholds)
if thresholds_len >= adndp_content.amount_of_atoms:
return
for _ in range(adndp_content.amount_of_atoms - thresholds_len):
self.thresholds.append(0)
def save_to_file(self, distance_path):
joined_dist = " ".join(str(threshold) for threshold in self.thresholds)
distance_content = (
f"{joined_dist}\n"
"Mode(LD-Late Depleting, FC-\"Found-Cut\", LDFC-hybrid): LD\n"
"Save Residual Density Matrix: T\n"
)
with open(distance_path, "w+") as stream:
stream.write(distance_content)
@classmethod
def from_file(cls, distance_path):
with open(distance_path, "r") as stream:
distance_lines = collections.deque(stream.readlines())
thresholds = list(map(float, distance_lines.popleft().split()))
mode = distance_lines.popleft()[54:-1]
resid_save = distance_lines.popleft()[30:-1]
spin = "0"
if distance_lines:
distance_lines.popleft()
ghost = distance_lines.popleft()
if len(ghost) > 1:
if ghost.startswith("alpha"):
spin = "A"
else:
spin = "B"
return cls(thresholds, mode, resid_save, spin)
class AdNDPAnalysis(object):
def __init__(self, use_residual_file, work_dir=None):
if not work_dir:
work_dir = os.getcwd()
work_dir = os.path.abspath(work_dir)
distance_path = os.path.join(work_dir, DISTANCE_BASENAME)
adndp_path = os.path.join(work_dir, ADNDP_BASENAME)
residual_path = os.path.join(work_dir, RESID_BASENAME)
adndp_content = AdNDPContent.from_file(adndp_path)
distance_content = DistanceContent.from_file(distance_path)
distance_content.fill_thresholds_from_adndp(adndp_content)
self._work_dir = work_dir
self._adndp_content = adndp_content
self._distance_content = distance_content
self._residual_path = residual_path
self._use_residual_file = use_residual_file
# Attributes filled dynamically on demand
self._residual_density = None # Change value during processing
# Reshaped distance matrix
self._distince_matris_mod = None
self._dmnao = None
self._dmnao_mod = None # Change value during processing
self._dmnao_mod_indexes = None
self._dmao = None
self._dmao_mod = None # Change value during processing
self._visual = None # Change value during processing
def analyse(self):
mode = self._distance_content.mode
if mode == "LDFC":
self._bonding_search_ldfc()
elif mode == "FC":
self._bonding_search_fc()
elif mode == "LD":
self._bonding_search_ld()
@property
def resid_save_from_distance(self):
return self._distance_content.resid_save == "T"
def store_resid_to_file(self, resid_path=None):
if not resid_path:
resid_path = self._residual_path
with open(resid_path, "wb") as stream:
pickle.dump(
(self.dmnao_mod, self.residual_density),
stream
)
@property
def work_dir(self):
return self._work_dir
@property
def amount_of_atoms(self):
return self._adndp_content.amount_of_atoms
@property
def total_basis_funcs(self):
return self._adndp_content.total_basis_funcs
@property
def basis_funcs_per_atom(self):
return self._adndp_content.basis_funcs_per_atom
@property
def total_pairs(self):
return self._adndp_content.total_pairs
@property
def valence_pairs(self):
return self._adndp_content.valence_pairs
@property
def log_reader(self):
return self._adndp_content.log_reader
@property
def visual(self):
if self._visual is None:
self._visual = [[] for _ in range(self.amount_of_atoms)]
return self._visual
@property
def distince_matris_mod(self):
if self._distince_matris_mod is None:
distince_matris = self.distince_matris
amount_of_atoms = self.amount_of_atoms
if amount_of_atoms < 5:
modified = copy.deepcopy(distince_matris)
self._distince_matris_mod = modified
return modified
modified = []
for idx in range(amount_of_atoms):
counter = 0
while distince_matris[idx][-1] != 0:
counter += 1
counted_idx = (
idx
+ amount_of_atoms
* counter
- 5
* int(((counter + 1) / 2) * counter)
)
for item in distince_matris[counted_idx]:
distince_matris[idx].append(item)
modified.append(distince_matris[idx])
self._distince_matris_mod = modified
return self._distince_matris_mod
@property
def core_threshold(self):
return self._distance_content.core_threshold
@property
def distince_matris(self):
return self._adndp_content.distince_matris
def get_residual_density(self):
if self._residual_density is not None:
return self._residual_density
if self._use_residual_file:
self._load_resid()
return self._residual_density
self._residual_density = self._adndp_content.get_residual_density(
self._distance_content.system,
self._distance_content.spin
)
return self._residual_density
def set_residual_density(self, value):
self._residual_density = value
residual_density = property(get_residual_density, set_residual_density)
def _load_resid(self):
dmnao_mod = None
residual_density = None
with open(self._residual_path, "rb") as stream:
dmnao_mod, residual_density = pickle.load(stream)
self._dmnao_mod = dmnao_mod
self._residual_density = residual_density
@property
def dmnao(self):
if self._dmnao is not None:
return self._dmnao
if self._use_residual_file:
raise ValueError((
"BUG: Tried to use 'dmnao' when residual file"
" should be used as source for 'dmnao_mod'."
))
self._dmnao = self.log_reader.get_dmnao()
return self._dmnao
@property
def dmnao_mod(self):
if self._dmnao_mod is not None:
return self._dmnao_mod
if self._use_residual_file:
self._load_resid()
return self._dmnao_mod
# Reshaping of DMNAO
total_basis_funcs = self.total_basis_funcs
dmnao = self.dmnao
dmnao_mod = dmnao[:total_basis_funcs].copy()
for idx in range(1, (-(-total_basis_funcs // 8))):
dmnao_mod = np.concatenate(
(
dmnao_mod,
dmnao[
idx * total_basis_funcs:
(idx + 1) * total_basis_funcs
]
),
axis=1
)
self._dmnao_mod = dmnao_mod
return self._dmnao_mod
@property
def dmnao_mod_indexes(self):
if self._dmnao_mod_indexes is None:
# Creates indexes of block of i-th atom in DMNAO matrix
amount_of_atoms = self.amount_of_atoms
basis_funcs_per_atom = self.basis_funcs_per_atom
self._dmnao_mod_indexes = [
(
sum(basis_funcs_per_atom[:idx]),
sum(basis_funcs_per_atom[:idx + 1])
)
for idx in range(amount_of_atoms)
]
return self._dmnao_mod_indexes
def dmnao_mod_has_proper_shape(self):
dmnao_mod = self.dmnao_mod
return len(dmnao_mod) == len(dmnao_mod[0])
@property
def dmao(self):
if self._dmao is None:
self._dmao = self.log_reader.get_dmao()
return self._dmao
@property
def dmao_mod(self):
if self._dmao_mod is None:
# Reshaping of NAOAO
total_basis_funcs = self.total_basis_funcs
dmao = self.dmao
dmao_mod = dmao[:total_basis_funcs].copy()
columns_count = len(dmao[0])
ao_count = (
(int(len(dmao) / total_basis_funcs)-1)
* columns_count + len(dmao[-1])
)
for idx in range(1, (-(-ao_count // columns_count))):
dmao_mod = np.concatenate(
(
dmao_mod,
dmao[
idx * total_basis_funcs:
(idx + 1) * total_basis_funcs
]
),
axis=1
)
self._dmao_mod = dmao_mod
return self._dmao_mod
def dmao_mod_has_proper_shape(self):
dmao_mod = self.dmao_mod
return len(dmao_mod) == len(dmao_mod[0])
def combinations(self, centers):
"""Creates all possible combinations of N centers.
Not modifying method.
"""
amount_of_atoms = self.amount_of_atoms
dist_thresholds = self._distance_content.thresholds
center_treshold = dist_thresholds[centers - 1]
if center_treshold == 0:
return list(
itertools.combinations(range(amount_of_atoms), centers)
)
distince_matris_mod = self.distince_matris_mod
modified = []
for center_combination in itertools.combinations(
range(amount_of_atoms), centers
):
add_combination = True
for pos_j, pos_i in itertools.combinations(center_combination, 2):
if distince_matris_mod[pos_i][pos_j] > center_treshold:
add_combination = False
break
if add_combination:
modified.append(center_combination)
return modified
def _indexes_and_dim_for_atom_centers(self, atom_centers):
basis_funcs_per_atom = self.basis_funcs_per_atom
indexes_partition = []
prev_part = 0
dim = 0
for idx, atom_center in enumerate(atom_centers):
atom_value = basis_funcs_per_atom[atom_center]
dim += atom_value
new_part = prev_part + atom_value
indexes_partition.append((prev_part, new_part))
prev_part = new_part
return indexes_partition, dim
def _search_bonding(self, centers, atom_centers, resid="Y", core="N"):
"""Searching for 'centers'c-2e bonds on 'atom_centers' centers.
If Resid='Y', Density matrix (D_FOR) will be changed.
#If Core='Y', will searching for 1c-2e core orbitals with ON>1.99|e|.
Returns truple (ON, wavefunction in NAO basis set)
"""
dmnao_mod = self.dmnao_mod
dmnao_mod_indexes = self.dmnao_mod_indexes
indexes_partition, dim = (
self._indexes_and_dim_for_atom_centers(atom_centers)
)
partition = np.zeros(dim ** 2).reshape(dim, dim)
matrix_recalculations(
atom_centers,
dmnao_mod,
partition,
dmnao_mod_indexes,
indexes_partition,
partition_is_dst=True,
)
ans = np.linalg.eig(partition)
if core == "N":
if resid == "Y":
print((
f"Occupancy of {centers}c-2e"
f" bond on {str([n+1 for n in atom_centers])}"
f" atom(s) is {np.real(max(ans[0]))}"
))
occupancy = np.real(max(ans[0]))
wave_function = ans[1][:, np.argmax(ans[0])]
if resid == "N":
return (occupancy, wave_function)
partition = (
partition
- occupancy
* wave_function
* np.transpose(wave_function[np.newaxis])
)
matrix_recalculations(
atom_centers,
dmnao_mod,
partition,
dmnao_mod_indexes,
indexes_partition,
partition_is_dst=False,
)
return (occupancy, wave_function)
if max(ans[0]) < 1.99:
return (0, 0)
if not atom_centers:
return ans
print((
f"Occupancy of Core {str(centers)}c-2e"
f" bond on {str([n+1 for n in atom_centers])}"
f"atom(s) is {np.real(max(ans[0]))}"
))
occupancy = np.real(max(ans[0]))
wave_function = ans[1][:, np.argmax(ans[0])]
partition = (
partition
- occupancy
* wave_function
* np.transpose(wave_function[np.newaxis])
)
matrix_recalculations(
atom_centers,
dmnao_mod,
partition,
dmnao_mod_indexes,
indexes_partition,
partition_is_dst=False,
only_first=True
)
return (occupancy, wave_function)
def _analysis_bonding(self, centers, atom_centers, resid="Y", core="N"):
"""Searching for 'centers'c-2e bonds on 'atom_centers' centers.
If resid='Y', Density matrix (D_FOR) will be changed.
If core='Y', will searching for 1c-2e core orbitals with ON>1.99|e|.
Returns truple (ON, wavefunction in NAO basis set).
"""
core_threshold = self.core_threshold
thresholds = self._adndp_content.thresholds
dmnao_mod = self.dmnao_mod
dmnao_mod_indexes = self.dmnao_mod_indexes
indexes_partition, dim = (
self._indexes_and_dim_for_atom_centers(atom_centers)
)
partition = np.zeros(dim ** 2).reshape(dim, dim)
matrix_recalculations(
atom_centers,
dmnao_mod,
partition,
dmnao_mod_indexes,
indexes_partition,
partition_is_dst=True,
)
ans = np.linalg.eig(partition)
if not atom_centers:
return ans
if core == "N" and max(ans[0]) >= (2.0 - thresholds[centers - 1]):
wave_function = ans[1][:, np.argmax(ans[0])]
occupancy = np.real(max(ans[0]))
if resid == "N":
return (occupancy, wave_function)
if resid == "Y":
print((
f"FC: Occupancy of {centers}c-2e"
f" bond on {str([n + 1 for n in atom_centers])}"
f" atom(s) is {np.real(max(ans[0]))}"
))
partition = (
partition
- occupancy
* wave_function
* np.transpose(wave_function[np.newaxis])
)
matrix_recalculations(
atom_centers,
dmnao_mod,
partition,
dmnao_mod_indexes,
indexes_partition,
partition_is_dst=False,
)
return (occupancy, wave_function)
if core != "Y":
return (0, 0)
if max(ans[0]) < core_threshold:
return (0, 0)
print((
f"Occupancy of Core {str(centers)}c-2e"
f" bond on {str([n+1 for n in atom_centers])}"
f" atom(s) is {np.real(max(ans[0]))}"
))
occupancy = np.real(max(ans[0]))
wave_function = ans[1][:, np.argmax(ans[0])]
partition = (
partition
- occupancy
* wave_function
* np.transpose(wave_function[np.newaxis])
)
matrix_recalculations(
atom_centers,
dmnao_mod,
partition,
dmnao_mod_indexes,
indexes_partition,
partition_is_dst=False,
only_first=True
)
return (occupancy, wave_function)
def core_cut(self):
"""Searching for all core orbitals in molecule (will not visualized)"""
pairs_diff = self.total_pairs - self.valence_pairs
core_found = 0
while core_found < pairs_diff:
for comb in self.combinations(1):
occupancy, _wave_function = self._analysis_bonding(
1, comb, "Y", "Y"
)
self.residual_density -= occupancy
if occupancy != 0:
core_found += 1
if core_found >= pairs_diff:
break
def _bonding_search_fc(self):
visual = self.visual
thresholds = self._adndp_content.thresholds
for idx in range(self.amount_of_atoms):