File size: 4,580 Bytes
8f59bb3 | 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 | #!/usr/bin/python
"""The module of the CoulombInt class."""
import numpy as np
from itertools import combinations_with_replacement as cr
import sys
import os
from wf import WF
from skmonaco import mcquad
import time
class CoulombIntDir(object):
"""Computes Coulomb integrals."""
def __init__(self, N=2, **kw):
self.N = N # number of basis functions
self.comb = self.specialCombinations() # four factor products
self._load = kw.get('pload', '/data/users/mklymenko/work_py/mb_project/')
self._save = kw.get('psave', '/data/users/mklymenko/work_py/mb_project/')
self.cint = []
if os.path.isfile('/data/users/mklymenko/work_py/mb_project/coint2.dat'):
os.remove('/data/users/mklymenko/work_py/mb_project/coint2.dat')
try: # try to read integrals from the disk
with open(self._load + "coint2.dat"):
self.cint = np.loadtxt(self._load + "coint2.dat")
except IOError: # there are no integrals on the disk
wfs_x = [] # envelope functions for X valleys
wfs_y = [] # envelope functions for Y valleys
wfs_z = [] # envelope functions for Z valleys
for j in xrange(self.N):
#wfs_x.append(WF(qn=j,
# p='/data/users/mklymenko/science/H2_100/programing/dis/v0/', flag='int', **kw)) # executing the fitting procedure N-times
wfs_y.append(WF(qn=j,
p='/data/users/mklymenko/science/H2_100/programing/dis/v1/', flag='int', **kw)) # executing the fitting procedure N-times
#wfs_z.append(WF(qn=j,
# p='/data/users/mklymenko/science/H2_100/programing/dis/v2/', flag='int', **kw)) # executing the fitting procedure N-times
# computing integrals for all possible combinations stored in self.comb
start_time = time.time()
self.cint = [self.two_el_int(wfs_y[j[0]],wfs_y[j[1]],wfs_y[j[2]],wfs_y[j[3]]) for j in self.comb]
# self.cint = self.two_el_int(wfs_y[0],wfs_y[0],wfs_y[0],wfs_y[0])
# self.cint = [self.overlap_int(wfs_y[j],wfs_y[j]) for j in xrange(self.N)]
# self.cint = self.overlap_int(wfs_y[0],wfs_y[0])
print time.time()- start_time
self.save()
#-----------------------------------
def getInt(self, vec):
i = self.comb.index(tuple(sorted(vec)))
return self.cint[i]
#-----------------------------------
def specialCombinations(self):
"""This function computes the number of possible configurations
of N basis functions into poducts of four functions."""
vec = list(xrange(self.N))
b = []
# for a1 in cr(vec, 2):
# for a2 in cr(vec, 2):
# b.append(a1+a2)
for a in cr(vec, 4):
b.append(a)
return b
#-----------------------------------
@staticmethod
def overlap_int(wf1,wf2):
def integrand(x, p1, p2):
wf1 = p1.get_value([x[0], x[1], x[2]])
wf2 = p2.get_value([x[0], x[1], x[2]])
return wf1*wf2
xl = [-6.5, 0.0, -6.5]
xu = [6.5, 8.9, 6.5]
v, a = mcquad(integrand, xl=xl, xu=xu, npoints=1000000, args=[wf1, wf2])
return v, a
#----------------------------------
@staticmethod
def two_el_int(wf1, wf2, wf3, wf4):
def integrand(x, p1, p2, p3, p4):
x1=np.array([x[0], x[1], x[2]])
x2=np.array([x[3], x[4], x[5]])
wf1 = p1.get_value(x1)
wf2 = p2.get_value(x1)
wf3 = p3.get_value(x2)
wf4 = p4.get_value(x2)
D=np.sqrt(np.sum((x1-x2)**2))
return wf1*wf2*(1/D)*wf3*wf4
xl = [-6.5, 0.0, -6.5, -6.5, 0.0, -6.5]
xu = [6.5, 8.9, 6.5, 6.5, 8.9, 6.5]
v, a = mcquad(integrand, xl=xl, xu=xu, npoints=1000000, args=[wf1, wf2, wf3, wf4])
return v, a
#-----------------------------------
def sum2(self, j, i):
ans = 0
for k in xrange(self.N):
ans+=self.getInt([j,i,k,k])
return ans
def save(self): # fixed number of functions, varied amplitudes and widths
if (self._save != '0'):
p = self._save + "coint2.dat"
np.savetxt(p, self.cint)
else:
sys.exit("Wrong path to save")
if __name__ == "__main__":
c=CoulombIntDir()
print(np.array([c.comb,c.cint]).T)
|