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
use std::collections::HashMap;
use std::f64::consts::PI;
use super::bragg;
use super::bse;
use super::parameters;
use statrs::function::gamma;
pub fn radial_grid_kk(num_points: usize) -> (Vec<f64>, Vec<f64>) {
let n = num_points as i32;
let mut rws: Vec<_> = (1..=n).map(|i| kk_r_w(i, n)).collect();
rws.reverse();
rws.iter().cloned().unzip()
}
fn kk_r_w(i: i32, n: i32) -> (f64, f64) {
let pi = std::f64::consts::PI;
let angle = ((i as f64) * pi) / ((n + 1) as f64);
let s = angle.sin();
let c = angle.cos();
let t1 = ((n + 1 - 2 * i) as f64) / ((n + 1) as f64);
let t2 = 2.0 / pi;
let x = t1 + t2 * c * s * (1.0 + (2.0 / 3.0) * s * s);
let f = 1.0 / 2.0_f64.ln();
let r = f * (2.0 / (1.0 - x)).ln();
let w = r * r * (f / (1.0 - x)) * (16.0 * s * s * s * s) / (3.0 * (n as f64) + 3.0);
(r, w)
}
pub fn radial_grid_lmg_bse(
basis_set: &str,
radial_precision: f64,
proton_charge: i32,
) -> (Vec<f64>, Vec<f64>) {
let (alpha_min, alpha_max) = bse::ang_min_and_max(basis_set, proton_charge as usize);
radial_grid_lmg(alpha_min, alpha_max, radial_precision, proton_charge)
}
pub fn radial_grid_lmg(
alpha_min: HashMap<usize, f64>,
alpha_max: f64,
radial_precision: f64,
proton_charge: i32,
) -> (Vec<f64>, Vec<f64>) {
let r_inner = get_r_inner(radial_precision, alpha_max * 2.0);
let mut h = std::f64::MAX;
let mut r_outer: f64 = 0.0;
let mut v: Vec<_> = alpha_min.into_iter().collect();
v.sort_by(|x, y| x.0.cmp(&y.0));
for (l, a) in v {
if a > 0.0 {
r_outer = r_outer.max(get_r_outer(
radial_precision,
a,
l,
4.0 * bragg::get_bragg_angstrom(proton_charge),
));
assert!(r_outer > r_inner);
h = h.min(get_h(radial_precision, l, 0.1 * (r_outer - r_inner)));
}
}
assert!(r_outer > h);
let c = r_inner / (h.exp() - 1.0);
let num_points = ((1.0 + (r_outer / c)).ln() / h) as usize;
let mut rs = Vec::new();
let mut ws = Vec::new();
for i in 1..=num_points {
let r = c * (((i as f64) * h).exp() - 1.0);
rs.push(r);
ws.push((r + c) * r * r * h);
}
(rs, ws)
}
pub fn radial_grid_gc2nd (n: usize) -> (Vec<f64>, Vec<f64>) {
let ln2 = 1.0 / f64::ln(2.0);
let fac = (16.0 / 3.0) / ((n+1) as f64);
let mut x1 = vec![];
let mut xi = vec![];
for i in 1..n+1 {
x1.push((i as f64)*PI / ((n+1) as f64));
};
for i in 1..n+1 {
let a = ((n as isize)-1-2*((i-1) as isize)) as f64;
let b = (1.0+2.0/3.0*f64::sin(x1[i-1]).powf(2.0))*f64::sin(2.0*x1[i-1]) / PI;
xi.push(
a / ((n+1) as f64) + b
);
}; let xi_rev: Vec<&f64> = xi.iter().rev().collect();
let xi_new: Vec<f64> = xi.iter().zip(xi_rev.iter()).map(|(xi, xi_rev)| (xi - *xi_rev) / 2.0 ).collect();
let r: Vec<f64> = xi_new.iter().map(|xi_new| 1.0 - f64::ln(1.0 + xi_new) * ln2).collect();
let w: Vec<f64> = xi_new.iter().zip(x1.iter()).map(|(xi_new, x1)| fac * f64::sin(*x1).powf(4.0) * ln2 / (1.0 + xi_new)).collect();
return (r,w);
}
pub fn radial_grid_treutler(n: usize) -> (Vec<f64>, Vec<f64>) {
let mut r: Vec<f64> = vec![];
let mut w: Vec<f64> = vec![];
let step = PI / ((n+1) as f64);
let ln2 = 1.0 / f64::ln(2.0);
for i in 1..n+1 {
let x = f64::cos((i as f64)*step);
r.push(-ln2*(1.0+x).powf(0.6)*f64::ln((1.0-x)/2.0));
w.push(step*f64::sin((i as f64)*step)*ln2*(1.0+x).powf(0.6)*(-0.6/(1.0+x)*f64::ln((1.0-x)/2.0)+1.0/(1.0-x)))
}
let r_rev: Vec<f64> = r.into_iter().rev().collect();
let w_rev: Vec<f64> = w.into_iter().rev().collect();
return (r_rev, w_rev)
}
fn get_r_inner(max_error: f64, alpha_inner: f64) -> f64 {
let d = 1.9;
let mut r = d - (1.0 / max_error).ln();
r *= 2.0 / 3.0;
r = r.exp() / alpha_inner;
r = r.sqrt();
r
}
fn get_r_outer(max_error: f64, alpha_outer: f64, l: usize, guess: f64) -> f64 {
let m = (2 * l) as f64;
let mut r_old = std::f64::MAX;
let mut step = 0.5;
let mut sign = 1.0;
let mut r = guess;
while (r_old - r).abs() > parameters::SMALL {
let c = gamma::gamma((m + 3.0) / 2.0);
let a = (alpha_outer * r * r).powf((m + 1.0) / 2.0);
let e = (-alpha_outer * r * r).exp();
let f = c * a * e;
let sign_old = sign;
sign = if f > max_error { 1.0 } else { -1.0 };
if r < 0.0 {
sign = 1.0
}
if sign * sign_old < 0.0 {
step *= 0.1;
}
r_old = r;
r += sign * step;
}
r
}
fn get_h(max_error: f64, l: usize, guess: f64) -> f64 {
let m = (2 * l) as f64;
let mut h_old = std::f64::MAX;
let mut h = guess;
let mut step = 0.1 * guess;
let mut sign = -1.0;
let pi = std::f64::consts::PI;
while (h_old - h).abs() > parameters::SMALL {
let c0 = 4.0 * (2.0 as f64).sqrt() * pi;
let cm = gamma::gamma(3.0 / 2.0) / gamma::gamma((m + 3.0) / 2.0);
let p0 = 1.0 / h;
let e0 = (-pi * pi / (2.0 * h)).exp();
let pm = (pi / h).powf(m / 2.0);
let rd0 = c0 * p0 * e0;
let f = cm * pm * rd0;
let sign_old = sign;
sign = if f > max_error { -1.0 } else { 1.0 };
if h < 0.0 {
sign = 1.0
}
if sign * sign_old < 0.0 {
step *= 0.1;
}
h_old = h;
h += sign * step;
}
h
}