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
use crate::scf_io::SCF;
use crate::scf_io::{vj_upper_with_ri_v, vj_full_with_ri_v,
vk_full_fromdm_with_ri_v};
use rest_tensors::{
MatrixFull,
MatrixUpper,
};
use rest_tensors::{davidson_solve, DavidsonParams};
use itertools::{iproduct,
};
use tensors::MathMatrix;
use crate::anyhow::{anyhow,Error};
impl SCF {
pub fn stability(&mut self) -> Result< (Vec<bool>, Vec<Vec<Vec<f64>>>) ,Error> {
let spin_channel = self.mol.ctrl.spin_channel;
let print_level = self.mol.ctrl.print_level;
let params = DavidsonParams{tol:1e-4, maxspace:15, ..DavidsonParams::default()};
let mut conv:Vec<bool> = vec![];
let mut e:Vec<f64> = vec![];
let mut v_i:Vec<Vec<f64>> = vec![];
let mut v_e:Vec<Vec<f64>> = vec![];
let mut stable_i = true;
let mut stable_e = true;
let mut stable:Vec<bool> = vec![];
let mut v:Vec<Vec<Vec<f64>>> = vec![];
let stable_str = ["unstable", "stable"];
if spin_channel == 1 {
println!("Start RHF internal stability check");
let (mut g, mut hdiag, mut h_op) = self.generate_g_hop(false).unwrap();
let ov = g.len();
let mut x0 = vec![0.0_f64;ov];
x0.iter_mut().zip(hdiag.iter()).for_each(|(x, hd)| {*x += 1.0 / hd});
(conv, e, v_i) = davidson_solve(h_op, &mut x0, &mut hdiag,
¶ms,
print_level
);
stable_i = e[0] > -1e-5;
println!("RHF internal : {:?}", stable_str[stable_i as usize]);
stable.push(stable_i);
v.push(v_i);
println!("Start RHF external stability check");
let (mut g2, mut hdiag2, mut h_op2) = self.generate_g_hop(true).unwrap();
x0 = vec![0.0_f64;ov];
x0.iter_mut().zip(hdiag2.iter()).for_each(|(x, hd)| {*x += 1.0 / hd});
(conv, e, v_e) = davidson_solve(h_op2, &mut x0, &mut hdiag2,
¶ms,
print_level
);
stable_e = e[0] > -1e-5;
println!("RHF -> UHF : {:?}", stable_str[stable_e as usize]);
stable.push(stable_e);
v.push(v_e);
}
else {
println!("Start UHF internal stability check");
let (mut g, mut hdiag, mut h_op) = self.generate_g_hop(false).unwrap();
let ov = g.len();
let mut x0 = vec![0.0_f64;ov];
x0.iter_mut().zip(hdiag.iter()).for_each(|(x, hd)| {*x += 1.0 / hd});
x0[0] += 0.2; (conv, e, v_i) = davidson_solve(h_op, &mut x0, &mut hdiag,
¶ms,
print_level
);
stable_i = e[0] > -1e-5;
println!("UHF internal : {:?}", stable_str[stable_i as usize]);
stable.push(stable_i);
v.push(v_i);
}
Ok((stable,v))
}
pub fn generate_h_rhf_slow(&mut self) {
let num_basis = self.mol.num_basis;
let num_state = self.mol.num_state;
let num_auxbas = self.mol.num_auxbas;
let spin_channel = self.mol.spin_channel;
let mut occ = self.homo[0] + 1;
let mut vir = num_state - occ;
let mut ov = occ*vir;
let mut moa = &self.eigenvectors[0];
let mut fock = self.hamiltonian[0].to_matrixfull().unwrap();
let mut cv = MatrixFull::from_vec([num_basis,vir],
moa.iter_submatrix(0..num_basis,occ..num_basis).map(|i| *i).collect()
).unwrap();
let mut co = MatrixFull::from_vec([num_basis,occ],
moa.iter_submatrix(0..num_basis,0..occ).map(|i| *i).collect()
).unwrap();
let mut f_co = MatrixFull::new([num_basis, occ], 0.0_f64);
f_co.lapack_dgemm(&mut fock.clone(), &mut co.clone(), 'N', 'N', 1.0, 0.0);
let mut f_cv = MatrixFull::new([num_basis, vir], 0.0_f64);
f_cv.lapack_dgemm(&mut fock.clone(), &mut cv.clone(), 'N', 'N', 1.0, 0.0);
let mut foo = MatrixFull::new([occ,occ], 0.0_f64);
foo.lapack_dgemm(&mut co.clone(), &mut f_co, 'T', 'N', 1.0, 0.0);
let mut fvv = MatrixFull::new([vir,vir], 0.0_f64);
fvv.lapack_dgemm(&mut cv.clone(), &mut f_cv, 'T', 'N', 1.0, 0.0);
let mut fvo = MatrixFull::new([vir,occ], 0.0_f64);
fvo.lapack_dgemm(&mut cv.clone(), &mut f_co.clone(), 'T', 'N', 1.0, 0.0);
let mut g = fvo.data.to_vec();
let mut hdiag = MatrixFull::new([vir, occ], 0.0_f64);
iproduct!(fvv.get_diagonal_terms().unwrap().iter(),
foo.get_diagonal_terms().unwrap().iter()).map(|(v,o)| {(*v,*o)})
.zip(hdiag.data.iter_mut()).for_each(|(vo,h)| {
*h = vo.0 - vo.1
});
let mut hdiagvec = hdiag.data.to_vec();
let mut amat = MatrixFull::new([ov,ov], 0.0_f64);
let mut bmat = MatrixFull::new([ov,ov], 0.0_f64);
for ia in 0..ov {
amat.data[ia*ov+ia] = hdiagvec[ia]
}
if let Some(ri3fn) = &self.ri3fn {
for i_spin in (0..spin_channel) {
ri3fn.iter_auxbas(0..num_auxbas).unwrap().enumerate().for_each(|(i,m)| {
let mut tmp_mu = MatrixFull::from_vec([num_basis, num_basis], m.to_vec()).unwrap();
let mut m_cv = MatrixFull::new([num_basis, vir], 0.0f64);
m_cv.lapack_dgemm(&mut tmp_mu, &mut cv.clone(), 'N', 'N', 1.0, 0.0);
let mut m_co = MatrixFull::new([num_basis, occ], 0.0f64);
m_co.lapack_dgemm(&mut tmp_mu, &mut co.clone(), 'N', 'N', 1.0, 0.0);
let mut l_ov = MatrixFull::new([occ, vir], 0.0f64);
l_ov.lapack_dgemm(&mut co.clone(), &mut m_cv, 'T', 'N', 1.0, 0.0);
let mut l_vv = MatrixFull::new([vir,vir], 0.0f64);
l_vv.lapack_dgemm(&mut cv.clone(), &mut m_cv, 'T', 'N', 1.0, 0.0);
let mut l_oo = MatrixFull::new([occ, occ], 0.0f64);
l_oo.lapack_dgemm(&mut co.clone(), &mut m_co, 'T', 'N', 1.0, 0.0);
let mut l_ov_vec = l_ov.data.to_vec();
let mut l_vv_vec = l_vv.data.to_vec();
let mut l_oo_vec = l_oo.data.to_vec();
for ia in 0..ov {
for jb in 0..ov {
amat.data[jb*ov+ia] += l_ov_vec[ia]*l_ov_vec[jb]*2.0;
bmat.data[jb*ov+ia] += l_ov_vec[ia]*l_ov_vec[jb]*2.0;
}
}
for i in 0..occ {
for j in 0.. occ {
for a in 0..vir {
for b in 0..vir {
let ia = a*occ+i;
let jb = b*occ+j;
let ab = b*vir+a;
let ij = j*occ+i;
let ib = b*occ+i;
let ja = a*occ+j;
amat.data[jb*ov+ia] -= l_vv_vec[ab]*l_oo_vec[ij];
bmat.data[jb*ov+ia] -= l_ov_vec[ja]*l_ov_vec[ib];
}
}
}
}
});
}
amat.formated_output(ov, "full");
bmat.formated_output(ov, "full");
let mut a_plus_b = amat.clone();
a_plus_b.self_add(&mut bmat);
let mut apb_upper = a_plus_b.clone().to_matrixupper();
let (mut v, mut e, n) = apb_upper.to_matrixupperslicemut().lapack_dspevx().unwrap();
println!("eigval {:?}", e.to_vec());
};
}
pub fn generate_g_hop(&mut self, external: bool) -> Result<(Vec<f64>, Vec<f64>,
Box<dyn FnMut(&Vec<f64>) -> Vec<f64> + '_>),
Error
> {
let num_basis = self.mol.num_basis;
let num_state = self.mol.num_state;
let num_auxbas = self.mol.num_auxbas;
let mut _occ:Vec<usize> = vec![0, 0];
let mut _vir:Vec<usize> = vec![0, 0];
let mut ov:Vec<usize> = vec![0, 0];
let spin_channel = self.mol.spin_channel;
let mut cv = [MatrixFull::empty(), MatrixFull::empty()];
let mut co = [MatrixFull::empty(), MatrixFull::empty()];
let mut foo = [MatrixFull::empty(), MatrixFull::empty()];
let mut fvv = [MatrixFull::empty(), MatrixFull::empty()];
let mut fvo = [MatrixFull::empty(), MatrixFull::empty()];
let mut g = vec![vec![], vec![]];
let mut hdiagvec = vec![vec![], vec![]];
let mut factor = 1.0;
if spin_channel == 1 {
factor = 4.0;
} else {
factor = 2.0;
}
for ispin in 0..spin_channel {
_occ[ispin] = self.homo[ispin] + 1;
_vir[ispin] = num_state - _occ[ispin];
ov[ispin] = _occ[ispin]*_vir[ispin];
let occ = _occ[ispin];
let vir = _vir[ispin];
let mut mo = &self.eigenvectors[ispin];
let mut fock = self.hamiltonian[ispin].to_matrixfull().unwrap();
cv[ispin] = MatrixFull::from_vec([num_basis,vir],
mo.iter_submatrix(0..num_basis,occ..num_basis).map(|i| *i).collect()
).unwrap();
co[ispin] = MatrixFull::from_vec([num_basis,occ],
mo.iter_submatrix(0..num_basis,0..occ).map(|i| *i).collect()
).unwrap();
let mut f_co = MatrixFull::new([num_basis, occ], 0.0_f64);
f_co.lapack_dgemm(&mut fock.clone(), &mut co[ispin].clone(), 'N', 'N', 1.0, 0.0);
let mut f_cv = MatrixFull::new([num_basis, vir], 0.0_f64);
f_cv.lapack_dgemm(&mut fock.clone(), &mut cv[ispin].clone(), 'N', 'N', 1.0, 0.0);
foo[ispin] = MatrixFull::new([occ,occ], 0.0_f64);
foo[ispin].lapack_dgemm(&mut co[ispin].clone(), &mut f_co, 'T', 'N', 1.0, 0.0);
fvv[ispin] = MatrixFull::new([vir,vir], 0.0_f64);
fvv[ispin].lapack_dgemm(&mut cv[ispin].clone(), &mut f_cv, 'T', 'N', 1.0, 0.0);
fvo[ispin] = MatrixFull::new([vir,occ], 0.0_f64);
fvo[ispin].lapack_dgemm(&mut cv[ispin].clone(), &mut f_co.clone(), 'T', 'N', 1.0, 0.0);
g[ispin] = fvo[ispin].data.to_vec();
let mut hdiag = MatrixFull::new([vir, occ], 0.0_f64);
iproduct!(fvv[ispin].get_diagonal_terms().unwrap().iter(),
foo[ispin].get_diagonal_terms().unwrap().iter()).map(|(v,o)| {(*v,*o)})
.zip(hdiag.data.iter_mut()).for_each(|(vo,h)| {
*h = vo.0 - vo.1
});
hdiagvec[ispin] = hdiag.data.to_vec();
}
let mut g_all = g.concat();
let mut hdiag_all = hdiagvec.concat();
if !external {
hdiag_all.iter_mut().for_each(|h| *h *= factor);
}
let mut xfac = 1.0;
if spin_channel == 1 { xfac *= 2.0; }
let h_op = move |xvec:&Vec<f64>| -> Vec<f64> {
let mut xmat = [MatrixFull::empty(), MatrixFull::empty()];
let mut sigma = vec![MatrixFull::empty(), MatrixFull::empty()];
let mut sigmavec = [vec![], vec![]];
let mut d1 = vec![MatrixFull::empty(), MatrixFull::empty()];
for ispin in 0..spin_channel {
let xstart = 0 + ov[0]*ispin;
let xend = ov[0] + ov[1]*ispin;
let occ = _occ[ispin];
let vir = _vir[ispin];
xmat[ispin] = MatrixFull::from_vec([vir, occ], xvec[xstart..xend].to_vec()).unwrap();
sigma[ispin] = MatrixFull::new([vir, occ], 0.0_f64);
sigma[ispin].lapack_dgemm(&mut fvv[ispin].clone(), &mut xmat[ispin], 'N', 'N', 1.0 ,0.0);
sigma[ispin].lapack_dgemm( &mut xmat[ispin].clone(), &mut foo[ispin].clone(), 'N', 'N', -1.0 ,1.0);
let mut x_co = MatrixFull::new([vir, num_basis], 0.0);
x_co.lapack_dgemm(&mut xmat[ispin].clone(), &mut co[ispin].clone(), 'N', 'T', 1.0, 0.0);
d1[ispin] = MatrixFull::new([num_basis, num_basis], 0.0);
d1[ispin].lapack_dgemm(&mut cv[ispin].clone(), &mut x_co, 'N', 'N', xfac, 0.0);
let mut d1_t = d1[ispin].transpose();
d1[ispin].self_add( &mut d1_t );
}
let mut vind = self.response_fn_hf(&d1, external);
for ispin in 0..spin_channel {
let occ = _occ[ispin];
let vir = _vir[ispin];
let mut v_co = MatrixFull::new([num_basis, occ], 0.0);
v_co.lapack_dgemm(&mut vind[ispin], &mut co[ispin].clone(), 'N', 'N', 1.0, 0.0);
sigma[ispin].lapack_dgemm(&mut cv[ispin].clone(), &mut v_co.clone(), 'T', 'N', 1.0, 1.0);
sigmavec[ispin] = sigma[ispin].data.to_vec();
}
let mut sigma_all = sigmavec.concat();
if !external {
sigma_all.iter_mut().for_each(|s| *s *= factor);
}
sigma_all
};
Ok((g_all, hdiag_all, Box::new(h_op)))
}
pub fn response_fn_hf(&mut self, dm: &Vec<MatrixFull<f64>>,
external: bool
) -> Vec<MatrixFull<f64>> {
if self.mol.ctrl.spin_channel == 1 {
if external {
let mut vind = self.response_vk_full_with_ri_v( &dm, -0.5);
vind
} else {
let mut vj = self.response_vj_full_with_ri_v( &dm, 1.0);
let mut vk = self.response_vk_full_with_ri_v( &dm, 1.0);
let mut vind = vj.clone();
vind[0] = vj[0].scaled_add(&vk[0], -0.5).unwrap();
vind
}
} else {
if external {
let mut vind = self.response_vk_full_with_ri_v( &dm, -1.0);
vind
} else {
let mut vj = self.response_vj_full_with_ri_v( &dm, 1.0);
let mut vk = self.response_vk_full_with_ri_v( &dm, -1.0);
let mut vind = vk.clone();
vind[0].self_add(&vj[0]);
vind[0].self_add(&vj[1]);
vind[1].self_add(&vj[0]);
vind[1].self_add(&vj[1]);
vind
}
}
}
pub fn response_vj_full_with_ri_v(&mut self, dm: &Vec<MatrixFull<f64>>,
scaling_factor: f64) -> Vec<MatrixFull<f64>> {
let spin_channel = self.mol.spin_channel;
vj_full_with_ri_v(&self.ri3fn, dm, spin_channel, scaling_factor)
}
pub fn response_vj_upper_with_ri_v(&mut self, dm: &Vec<MatrixFull<f64>>,
scaling_factor: f64) -> Vec<MatrixUpper<f64>> {
let spin_channel = self.mol.spin_channel;
vj_upper_with_ri_v(&self.ri3fn, dm, spin_channel, scaling_factor)
}
pub fn response_vk_full_with_ri_v(&mut self, dm: &Vec<MatrixFull<f64>>,
scaling_factor: f64) -> Vec<MatrixFull<f64>> {
let spin_channel = self.mol.spin_channel;
vk_full_fromdm_with_ri_v(&self.ri3fn, dm, spin_channel, scaling_factor)
}
}