Debian Codegraph @

cclib/method/cda.py

raw
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (c) 2017, the cclib development team
4 #
5 # This file is part of cclib (http://cclib.github.io) and is distributed under
6 # the terms of the BSD 3-Clause License.
7
8 """Charge Decomposition Analysis (CDA)"""
9
10 from __future__ import print_function
11
12 import random
13
14 import numpy
15
16 from cclib.method.fragments import FragmentAnalysis
17
18
19 class CDA(FragmentAnalysis):
20 """Charge Decomposition Analysis (CDA)"""
21
22 def __init__(self, *args):
23
24 # Call the __init__ method of the superclass.
25 super(FragmentAnalysis, self).__init__(logname="CDA", *args)
26
27 def __str__(self):
28 """Return a string representation of the object."""
29 return "CDA of %s" % (self.data)
30
31 def __repr__(self):
32 """Return a representation of the object."""
33 return 'CDA("%s")' % (self.data)
34
35 def calculate(self, fragments, cupdate=0.05):
36 """Perform the charge decomposition analysis.
37
38 Inputs:
39 fragments - list of ccData data objects
40 """
41
42
43 retval = super(CDA, self).calculate(fragments, cupdate)
44 if not retval:
45 return False
46
47 # At this point, there should be a mocoeffs and fooverlaps
48 # in analogy to a ccData object.
49
50 donations = []
51 bdonations = []
52 repulsions = []
53 residuals = []
54
55 if len(self.mocoeffs) == 2:
56 occs = 1
57 else:
58 occs = 2
59
60 # Intialize progress if available.
61 nstep = self.data.homos[0]
62 if len(self.data.homos) == 2:
63 nstep += self.data.homos[1]
64 if self.progress:
65 self.progress.initialize(nstep)
66
67 # Begin the actual method.
68 step = 0
69 for spin in range(len(self.mocoeffs)):
70
71 size = len(self.mocoeffs[spin])
72 homo = self.data.homos[spin]
73
74 if len(fragments[0].homos) == 2:
75 homoa = fragments[0].homos[spin]
76 else:
77 homoa = fragments[0].homos[0]
78
79 if len(fragments[1].homos) == 2:
80 homob = fragments[1].homos[spin]
81 else:
82 homob = fragments[1].homos[0]
83
84 self.logger.info("handling spin unrestricted")
85 if spin == 0:
86 fooverlaps = self.fooverlaps
87 elif spin == 1 and hasattr(self, "fooverlaps2"):
88 fooverlaps = self.fooverlaps2
89
90 offset = fragments[0].nbasis
91
92 self.logger.info("Creating donations, bdonations, and repulsions: array[]")
93 donations.append(numpy.zeros(size, "d"))
94 bdonations.append(numpy.zeros(size, "d"))
95 repulsions.append(numpy.zeros(size, "d"))
96 residuals.append(numpy.zeros(size, "d"))
97
98 for i in range(self.data.homos[spin] + 1):
99
100 # Calculate donation for each MO.
101 for k in range(0, homoa + 1):
102 for n in range(offset + homob + 1, self.data.nbasis):
103 donations[spin][i] += 2 * occs * self.mocoeffs[spin][i,k] \
104 * self.mocoeffs[spin][i,n] * fooverlaps[k][n]
105
106 for l in range(offset, offset + homob + 1):
107 for m in range(homoa + 1, offset):
108 bdonations[spin][i] += 2 * occs * self.mocoeffs[spin][i,l] \
109 * self.mocoeffs[spin][i,m] * fooverlaps[l][m]
110
111 for k in range(0, homoa + 1):
112 for m in range(offset, offset+homob + 1):
113 repulsions[spin][i] += 2 * occs * self.mocoeffs[spin][i,k] \
114 * self.mocoeffs[spin][i, m] * fooverlaps[k][m]
115
116 for m in range(homoa + 1, offset):
117 for n in range(offset + homob + 1, self.data.nbasis):
118 residuals[spin][i] += 2 * occs * self.mocoeffs[spin][i,m] \
119 * self.mocoeffs[spin][i, n] * fooverlaps[m][n]
120
121 step += 1
122 if self.progress and random.random() < cupdate:
123 self.progress.update(step, "Charge Decomposition Analysis...")
124
125 if self.progress:
126 self.progress.update(nstep, "Done.")
127
128 self.donations = donations
129 self.bdonations = bdonations
130 self.repulsions = repulsions
131 self.residuals = residuals
132
133 return True