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
use std::collections::HashMap;
use std::f64::consts::E;
use rest_tensors::MatrixFull;
use itertools::Itertools;
use libm::log;
use rest_libcint::CINTR2CDATA;
use statrs::statistics::{Statistics, Min};
use std::fmt::format;
use std::cmp::{max, max_by};
use std::vec;
use serde::{Deserialize, Serialize};
use crate::geom_io::{get_mass_charge, GeomCell};
use crate::molecule_io::Molecule;
use super::{Basis4Elem, BasCell};
use super::bse_downloader::{ctrl_element_checker};
use super::bse_downloader::Info;
use crate::constants::ATOM_CONFIGURATION;
use rest_tensors::matrix::matrix_blas_lapack::{_einsum_03, _einsum_03_forvec};
#[derive(Clone, Debug)]
pub struct InfoV2 {
pub elements: HashMap<String, Basis4Elem>,
}
impl InfoV2 {
pub fn new() -> InfoV2 {
InfoV2 {
elements: HashMap::new()
}
}
}
pub fn get_basis_from_mol(mol: &Molecule, atom: &String) -> anyhow::Result<Basis4Elem> {
let mut index = 0;
for item in mol.geom.elem.iter() {
if atom == item {
break;
}
index += 1;
}
Ok(mol.basis4elem[index].clone())
}
pub fn etb_gen_for_atom_list(mol: &Molecule, beta: &f64, required_elem: &Vec<String>) -> InfoV2 {
let mut newbasis = InfoV2::new();
let required_elem_mass_charge = get_mass_charge(required_elem);
let required_elem_charge: Vec<usize> = required_elem_mass_charge.iter().map(|(a, b)| (*b as usize)).collect();
let mut index = 0;
for elem in required_elem {
let nuc_charge = required_elem_charge[index];
let conf = ATOM_CONFIGURATION[nuc_charge];
let mut max_shells = 0;
for shell in conf {
if shell != 0 {
max_shells += 1;
}
}
let basis = get_basis_from_mol(&mol, elem).unwrap();
let ang_array: Vec<usize> = basis.electron_shells.iter().map(|b| b.angular_momentum[0] as usize).collect();
let mut emin_by_l = vec![1.0e99_f64; 8];
let mut emax_by_l = vec![0.0_f64; 8];
let mut l_max = 0;
for b in basis.electron_shells {
let l = b.angular_momentum[0] as usize;
l_max = max(l, l_max);
if l >= (max_shells + 1) {
continue;
}
let es = b.exponents;
let cs = b.coefficients;
let mut es_new = vec![];
for row_num in 0..cs[0].len() {
let mut tmp_max = 0.0;
let mut tmp = cs.iter().map(|col| col[row_num].abs()).collect_vec();
let tmp_max = tmp.clone().max();
if tmp_max > 1.0e-3*CINTR2CDATA::gto_norm(l as std::os::raw::c_int, es[row_num]) {
es_new.push(es[row_num]);
}
}
emax_by_l[l] = emax_by_l[l].max(es_new.clone().max());
emin_by_l[l] = emin_by_l[l].min(es_new.min());
}
let l_max1 = l_max + 1;
emax_by_l = emax_by_l[0..l_max1].to_vec();
emin_by_l = emin_by_l[0..l_max1].to_vec();
let emax_by_l_root: Vec<f64> = emax_by_l.iter().map(|a| a.sqrt()).collect();
let emin_by_l_root: Vec<f64> = emin_by_l.iter().map(|a| a.sqrt()).collect();
let emax = _einsum_03_forvec(&emax_by_l_root, &emax_by_l_root);
let emin = _einsum_03_forvec(&emin_by_l_root, &emin_by_l_root);
let mut emax_by_l_new = vec![];
let mut emin_by_l_new = vec![];
for i in 0..(l_max1*2-1) {
emax_by_l_new.push(emax.get_sub_antidiag_terms(i).unwrap().max());
emin_by_l_new.push(emin.get_sub_antidiag_terms(i).unwrap().min());
}
emax_by_l_new = emax_by_l_new.into_iter().map(|v| 2.0*v).collect(); emin_by_l_new = emin_by_l_new.into_iter().map(|v| 2.0*v).collect(); let mut ns: Vec<f64> = emax_by_l_new.iter().zip(&emin_by_l_new).map(|(max,min)| ((max+min)/min).log(E)/beta.log(E)).collect();
ns = ns.iter().map(|v| v.ceil()).filter(|v| *v > 0.0).collect();
let etb_para: Vec<(usize, usize, f64, f64)> = ns.iter().zip(emin_by_l_new).enumerate().map(|(l, (n, min))|(l, *n as usize, min, *beta)).collect();
let etb_result = etbs_gen(etb_para);
newbasis.elements.insert(elem.clone(), etb_result);
index += 1;
}
newbasis
}
pub fn etbs_gen(input: Vec<(usize, usize, f64, f64)>) -> Basis4Elem {
let mut bas = Basis4Elem {
electron_shells: vec![],
references: None,
global_index: (0,0),
};
bas.electron_shells = input.iter()
.map(|(l, n, alpha, beta)| etb_gen(l, n, alpha, beta)).collect();
bas
}
pub fn etb_gen(l: &usize, n: &usize, alpha: &f64, beta: &f64) -> BasCell {
let mut shell = BasCell {
function_type: Some(String::from("gto")),
region: None,
angular_momentum: vec![*l as i32],
exponents: vec![],
coefficients: vec![vec![1.0_f64; *n]],
};
shell.exponents = (0..*n).rev().map(|i| alpha*beta.powf((i as f64))).collect();
shell
}
pub fn get_etb_elem(mol: &GeomCell, start_atom: &usize) -> Vec<String>{
let elem_list = ctrl_element_checker(mol);
let elem_mass_charge = get_mass_charge(&elem_list);
let elem_charge_list: Vec<usize> = elem_mass_charge.iter().map(|(a, b)| (*b as usize)).collect();
let etb_elem = elem_list.iter().zip(elem_charge_list)
.filter(|(elem, charge)| *charge >= *start_atom)
.map(|(elem, charge)| elem.clone()).collect();
etb_elem
}
#[test]
fn test_einsum_03() {
let mut vec_a = [1.0, 2.0, 3.0];
let mut vec_b = [1.0, 2.0];
let mut mat_c = _einsum_03(&vec_a, &vec_b);
println!("{:?}", mat_c);
}
#[test]
fn test_diag() {
let a = MatrixFull::from_vec([3,3], vec![1,2,3,4,5,6,7,8,9]).unwrap();
let v = a.get_diagonal_terms().unwrap();
println!("{:?}", v);
}
#[test]
fn test_antidiag() {
let a = MatrixFull::from_vec([4,4], vec![1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]).unwrap();
let v = a.get_sub_antidiag_terms(4).unwrap();
println!("{:?}", v);
}
#[test]
fn test_iter() {
let mut a = vec![1,2,3,4,5,6];
a = a.into_iter().map(|v| v*2).collect();
println!("{:?}", a);
}