forked from sunqm/libcint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatlab_Vmunu.m
209 lines (190 loc) · 5.87 KB
/
matlab_Vmunu.m
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
% dummy test 2
% 10/21/24 Hai
addpath('./utils/')
addpath('./treefun/')
addpath('./data')
clear all
order = 8;
eps = 1e-06;
%%% resolve tree on cgto^2
func2 = @(x,y,z) cgto2func(x,y,z);
opts = struct('balance',true,...
'tol',eps, ...
'checkpts',[0 0 0; 0 -0.757 0.757;0 0.587 0.587], ...
'ifcoeffs',false);
f = treefun3(func2,[-15 15 -15 15 -15 15],order,opts);
% f = treefun3(func,[-0.5 0.5 -0.5 0.5 -0.5 0.5],order,opts);
plot(f,func2);
%%% treefun to bdmk
Norb = 24; %
ndim = 3;
ratio = 0.5/15; % from boxlen to 1
ipoly = 0;
[src,nleafbox,srcleaf,wtsleaf,norder,npbox,nboxes,nlevels,ltree,itree,iptr,centers,boxsize] = treefun2bdmk(f,ndim,ratio,ipoly);
r = h5read('src_h2o_ccpvdz.h5', '/DS1');
npts = numel(r(1,:));
r2 = reshape(r,3,[]);
diff = abs(reshape(src/ratio,3,[]) - r2);
%%% eval cgto
src0 = src/ratio;
func = @(x,y,z) cgtofunc(x,y,z);
fvals0 = squeeze(func(squeeze(src0(1,:,:)),squeeze(src0(2,:,:)),squeeze(src0(3,:,:))));
fvals0 = permute(fvals0,[3 1 2]);
fvals = fvals0;
%%% compute V_ijkl
nd = Norb*(Norb+1)/2;
ikernel = 1;
beta = 6.0d0;
%
% Vijkl = Vijklcomp(Norb,ratio,fvals,nleafbox,srcleaf,wtsleaf,...
% ndim,eps,ikernel,beta,ipoly,norder,npbox, ...
% nboxes,nlevels,ltree,itree,iptr,centers,boxsize);
%
idfname = 'isdf_1e-9.h5'; % 0.0026
% idfname = 'isdf_1e-4.h5'; % 0.4476
info = h5info(idfname);
Np = h5read(idfname, '/Np');
collocation_matrix = h5read(idfname, '/collocation_matrix');
interpolating_points = h5read(idfname, '/interpolating_points');
interpolating_vectors = h5read(idfname, '/interpolating_vectors');
kpts = h5read(idfname, '/kpts');
nkpts_ibz = h5read(idfname, '/nkpts_ibz');
nqpts_ibz = h5read(idfname, '/nqpts_ibz');
qpts = h5read(idfname, '/qpts');
%
collocation_matrix = squeeze(collocation_matrix(1,:,:));
interpolating_vectors = squeeze(interpolating_vectors(1,:,:));
rho_ij_id = zeros(npts,Norb^2);
for i=1:Norb
for j=1:Norb
rho_ij_id(:,(i-1)*Norb+j) = interpolating_vectors...
*(collocation_matrix(i,:).*collocation_matrix(j,:))';
end
end
func = @(x,y,z) cgtofunc(x,y,z);
fvals0 = func(r2(1,:),r2(2,:),r2(3,:));
fvals0 = reshape(fvals0,[npts Norb]);
fvals_ij = zeros(npts,Norb^2);
for i=1:Norb
for j=1:Norb
fvals_ij(:,(i-1)*Norb+j) = fvals0(:,i).*fvals0(:,j);
end
end
%
mydiff = abs(fvals - reshape(fvals0',[Norb npbox nboxes]));
%
nhess = ndim*(ndim+1)/2;
%%% based on fvals for all cGTOs, compute phi_k(r')*phi_l(r'), and singular volume integral
nd = size(interpolating_vectors,2);
phi_kl = zeros(nd,npbox,nboxes);
for k = 1:nd
phi_kl(k,:,:) = reshape(interpolating_vectors(:,k),[npbox nboxes]);
end
pot = zeros(nd,npbox,nboxes);
nd0 = 10;
nchnk = floor(nd/nd0);
% for jchnk = 1:1
for jchnk = 1:nchnk
jidxv = (jchnk-1)*nd0 + (1:nd0);
phi_kl0 = phi_kl(jidxv,:,:);
pot0=zeros(nd0,npbox,nboxes);
grad0=zeros(nd0,ndim,npbox,nboxes);
hess0=zeros(nd0,nhess,npbox,nboxes);
tic
ifpgh=1;
ifpghtarg=0;
ntarg = 100;
targs=zeros(ndim,ntarg); pote=zeros(nd0,ntarg);
grade=zeros(nd0,ndim,ntarg); hesse=zeros(nd0,nhess,ntarg);
timeinfo = zeros(20,1);
[pot0,grad0,hess0,pote,grade,hesse] = ...
bdmk_mex(nd0,ndim,eps,ikernel,beta,ipoly,norder,npbox, ...
nboxes,nlevels,ltree,itree,iptr,centers,boxsize,phi_kl0, ...
ifpgh,pot0,grad0,hess0,ntarg,targs, ...
ifpghtarg,pote,grade,hesse,timeinfo);
pot(jidxv,:,:) = pot0;
end
jchnk = nchnk+1;
jidxv = ((jchnk-1)*nd0+1):nd;
nd0 = numel(jidxv);
phi_kl0 = phi_kl(jidxv,:,:);
pot0=zeros(nd0,npbox,nboxes);
grad0=zeros(nd0,ndim,npbox,nboxes);
hess0=zeros(nd0,nhess,npbox,nboxes);
ifpgh=1;
ifpghtarg=0;
ntarg = 100;
targs=zeros(ndim,ntarg); pote=zeros(nd0,ntarg);
grade=zeros(nd0,ndim,ntarg); hesse=zeros(nd0,nhess,ntarg);
timeinfo = zeros(20,1);
[pot0,grad0,hess0,pote,grade,hesse] = ...
bdmk_mex(nd0,ndim,eps,ikernel,beta,ipoly,norder,npbox, ...
nboxes,nlevels,ltree,itree,iptr,centers,boxsize,phi_kl0, ...
ifpgh,pot0,grad0,hess0,ntarg,targs, ...
ifpghtarg,pote,grade,hesse,timeinfo);
pot(jidxv,:,:) = pot0;
%
phi_ij_leaf = zeros(nd,npbox,nleafbox); % leaf box information
potleaf = zeros(nd,npbox,nleafbox);
jbox = 0;
jbox2 = 0;
for ilev = 0:nlevels
bs = boxsize(ilev+1); % Adjusted for MATLAB indexing (1-based)
ifirstbox = itree(2 * ilev + 1);
ilastbox = itree(2 * ilev + 2);
nbloc = ilastbox - ifirstbox + 1;
for i = 1:nbloc
ibox = ifirstbox + i - 1;
if itree(iptr(4) + ibox - 1) == 0
jbox = jbox + 1;
for ell = 1:npbox
for j = 1:nd
phi_ij_leaf(j, ell, jbox) = phi_kl(j, ell, ibox);
potleaf(j, ell, jbox) = pot(j, ell, ibox);
end
end
end
end
end
%% compute smooth volume integral
% nd = nd0;
Vmunu = zeros(nd,nd);
% ell = 1;
for ell = 1:nd
% (2*L)^2 * potleaf = (2*L)^2 * \int_{-1/2}^{1/2} (phi_k*phi_l)/|r' - r| dV
potleaf_ell = potleaf(ell,:,:)/ratio^2;
for j = 1:nd
% (2*L)^3 * \int_{-1/2}^{1/2} phi_{ij} * pot_{kl} dV
Vmunu(j,ell) = sum(squeeze(phi_ij_leaf(j,:,:).*potleaf_ell).*wtsleaf(:,:),'all')/ratio^3;
end
end
Vijkl = zeros(Norb,Norb,Norb,Norb);
tmpidx = 0;
for i = 1:Norb
for j = 1:Norb
for k = 1:Norb
for l = 1:Norb
for mu = 1:nd
for nu = 1:nd
Vijkl(i,j,k,l) = Vijkl(i,j,k,l) ...
+ Vmunu(mu,nu)*collocation_matrix(i,mu)*collocation_matrix(j,mu) ...
*collocation_matrix(k,nu)*collocation_matrix(l,nu);
end
end
end
end
end
end
save('Vmunu_h2o_ccpvdz.mat','Vmunu')
if exist("Vmunu_h2o_ccpvdz.h5", 'file') ~= 2
h5create("Vmunu_h2o_ccpvdz.h5","/DS1",[nd nd])
h5write("Vmunu_h2o_ccpvdz.h5","/DS1",Vmunu)
h5disp("Vmunu_h2o_ccpvdz.h5")
end
save('ERI_h2o_ccpvdz.mat','Vijkl')
if exist("ERI_h2o_ccpvdz.h5", 'file') ~= 2
h5create("ERI_h2o_ccpvdz.h5","/DS1",[Norb Norb Norb Norb])
h5write("ERI_h2o_ccpvdz.h5","/DS1",Vijkl)
h5disp("ERI_h2o_ccpvdz.h5")
end
keyboard