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
#![allow(unused)]
use std::os::raw::c_int;
use std::ptr;
use std::mem::ManuallyDrop;
mod cint;
use crate::cint::{CINTOpt,CINTdel_optimizer};
pub enum CintType {
Spheric,
Cartesian,
}
pub struct CINTR2CDATA {
c_atm: (*mut i32, usize, usize),
c_bas: (*mut i32, usize, usize),
c_env: (*mut f64, usize, usize),
c_nbas: c_int,
c_natm: c_int,
c_opt: (*mut CINTOpt, usize, usize),
cint_type: CintType,
}
impl CINTR2CDATA {
pub fn new() -> CINTR2CDATA {
CINTR2CDATA {
c_atm: (unsafe {std::ptr::null::<i32>() as *mut i32}, 0,0),
c_bas: (unsafe {std::ptr::null::<i32>() as *mut i32}, 0,0),
c_env: (unsafe {std::ptr::null::<f64>() as *mut f64}, 0,0),
c_opt: (unsafe {std::ptr::null::<CINTOpt>() as *mut CINTOpt}, 0,0),
c_nbas: 0 as c_int,
c_natm: 0 as c_int,
cint_type: CintType::Spheric,
}
}
pub fn set_cint_type(&mut self, ctype: CintType) {
self.cint_type = ctype;
}
pub fn initial_r2c(&mut self,
atm: &Vec<Vec<i32>>, natm:i32,
bas: &Vec<Vec<i32>>, nbas:i32,
mut env: Vec<f64>) {
unsafe {
let r_atm = Vec::from_raw_parts(self.c_atm.0,self.c_atm.1,self.c_atm.2);
let r_bas = Vec::from_raw_parts(self.c_bas.0,self.c_bas.1,self.c_bas.2);
let r_env = Vec::from_raw_parts(self.c_env.0,self.c_env.1,self.c_env.2);
}
env.shrink_to_fit();
let mut env = ManuallyDrop::new(env);
self.c_env = (env.as_mut_ptr(), env.len(), env.capacity());
let mut bas_f: Vec<i32> = vec![];
&mut (0..bas.len()).for_each(|i| {
bas_f.extend(bas[i].clone());
});
bas_f.shrink_to_fit();
let mut bas_f = ManuallyDrop::new(bas_f);
self.c_bas = (bas_f.as_mut_ptr(), bas_f.len(), bas_f.capacity());
let mut atm_f: Vec<i32> = vec![];
&mut (0..atm.len()).for_each(|i| {
atm_f.extend(atm[i].clone());
});
atm_f.shrink_to_fit();
let mut atm_f = ManuallyDrop::new(atm_f);
self.c_atm = (atm_f.as_mut_ptr(), atm_f.len(), atm_f.capacity());
self.c_natm = natm as c_int;
self.c_nbas = nbas as c_int;
self.c_opt = (unsafe {std::ptr::null::<CINTOpt>() as *mut CINTOpt}, 0,0);
}
pub fn final_c2r(&mut self) {
println!("Transfer the ownership of the raw pointers in CINTR2CDATA to Rust");
unsafe {
let r_atm = Vec::from_raw_parts(self.c_atm.0,self.c_atm.1,self.c_atm.2);
let r_bas = Vec::from_raw_parts(self.c_bas.0,self.c_bas.1,self.c_bas.2);
let r_env = Vec::from_raw_parts(self.c_env.0,self.c_env.1,self.c_env.2);
}
self.cint_del_optimizer_rust();
}
pub fn cint_del_optimizer_rust(&mut self) {
unsafe{
CINTdel_optimizer(&mut self.c_opt.0);
}
}
pub fn cint2e_optimizer_rust(&mut self){
self.cint_del_optimizer_rust();
unsafe {
cint::cint2e_optimizer(&mut self.c_opt.0,
self.c_atm.0, self.c_natm,
self.c_bas.0, self.c_nbas,
self.c_env.0);
}
}
pub fn cint1e_ovlp_optimizer_rust(&mut self){
self.cint_del_optimizer_rust();
unsafe {
cint::cint1e_ovlp_optimizer(&mut self.c_opt.0,
self.c_atm.0, self.c_natm,
self.c_bas.0, self.c_nbas,
self.c_env.0);
}
}
pub fn cint1e_nuc_optimizer_rust(&mut self){
self.cint_del_optimizer_rust();
unsafe {
cint::cint1e_nuc_optimizer(&mut self.c_opt.0,
self.c_atm.0, self.c_natm,
self.c_bas.0, self.c_nbas,
self.c_env.0);
}
}
pub fn cint1e_kin_optimizer_rust(&mut self){
self.cint_del_optimizer_rust();
unsafe {
cint::int1e_kin_optimizer(&mut self.c_opt.0,
self.c_atm.0, self.c_natm,
self.c_bas.0, self.c_nbas,
self.c_env.0);
}
}
pub fn cint_cgto_rust(&self, index: i32) -> i32 {
let mut dim: i32;
unsafe {
dim = match self.cint_type {
CintType::Spheric =>cint::CINTcgto_spheric(index as c_int, self.c_bas.0) as i32,
CintType::Cartesian=>cint::CINTcgto_cart(index as c_int, self.c_bas.0) as i32,
};
}
dim
}
pub fn cint_ijkl(&mut self, i:i32,j:i32,k:i32,l:i32) -> Vec<f64> {
let mut di = self.cint_cgto_rust(i);
let mut dj = self.cint_cgto_rust(j);
let mut dk = self.cint_cgto_rust(k);
let mut dl = self.cint_cgto_rust(l);
let mut shls: Vec<c_int> = vec![i as c_int,j as c_int,k as c_int,l as c_int];
shls.shrink_to_fit();
let mut shls = ManuallyDrop::new(shls);
let (c_shls,shls_len,shls_cap) = (shls.as_mut_ptr() as *mut c_int,shls.len(),shls.capacity());
let mut buf: Vec<f64> = [0.0f64].repeat((di*dj*dk*dl) as usize);
buf.shrink_to_fit();
let mut buf = ManuallyDrop::new(buf);
let (c_buf, buf_len, buf_cap) = (buf.as_mut_ptr() as *mut f64, buf.len(), buf.capacity());
let mut new_buf:Vec<f64>;
unsafe {
match self.cint_type {
CintType::Spheric => cint::cint2e_sph(c_buf, c_shls,
self.c_atm.0, self.c_natm,
self.c_bas.0,self.c_nbas,
self.c_env.0,
self.c_opt.0),
CintType::Cartesian => cint::cint2e_cart(c_buf, c_shls,
self.c_atm.0, self.c_natm,
self.c_bas.0,self.c_nbas,
self.c_env.0,
self.c_opt.0),
};
let shls = Vec::from_raw_parts(c_shls, shls_len, shls_cap);
new_buf = Vec::from_raw_parts(c_buf, buf_len, buf_cap);
}
new_buf
}
pub fn cint_ijovlp(&mut self, i:i32,j:i32) -> Vec<f64> {
let mut di: i32 = self.cint_cgto_rust(i);
let mut dj: i32 = self.cint_cgto_rust(j);
let mut shls: Vec<c_int> = vec![i as c_int,j as c_int];
shls.shrink_to_fit();
let mut shls = ManuallyDrop::new(shls);
let (c_shls,shls_len,shls_cap) = (shls.as_mut_ptr() as *mut c_int,shls.len(),shls.capacity());
let mut buf: Vec<f64> = [0.0f64].repeat((di*dj) as usize);
buf.shrink_to_fit();
let mut buf = ManuallyDrop::new(buf);
let (c_buf, buf_len, buf_cap) = (buf.as_mut_ptr() as *mut f64, buf.len(), buf.capacity());
let mut new_buf:Vec<f64>;
unsafe {
match self.cint_type {
CintType::Spheric => cint::cint1e_ovlp_sph(
c_buf, c_shls,
self.c_atm.0, self.c_natm,
self.c_bas.0,self.c_nbas,
self.c_env.0,
self.c_opt.0),
CintType::Cartesian => cint::cint1e_ovlp_cart(
c_buf, c_shls,
self.c_atm.0, self.c_natm,
self.c_bas.0,self.c_nbas,
self.c_env.0,
self.c_opt.0),
};
let shls = Vec::from_raw_parts(c_shls, shls_len, shls_cap);
new_buf = Vec::from_raw_parts(c_buf, buf_len, buf_cap);
}
new_buf
}
pub fn cint_ijnuc(&mut self, i:i32,j:i32) -> Vec<f64> {
let mut di: i32 = self.cint_cgto_rust(i);
let mut dj: i32 = self.cint_cgto_rust(j);
let mut shls: Vec<c_int> = vec![i as c_int,j as c_int];
shls.shrink_to_fit();
let mut shls = ManuallyDrop::new(shls);
let (c_shls,shls_len,shls_cap) = (shls.as_mut_ptr() as *mut c_int,shls.len(),shls.capacity());
let mut buf: Vec<f64> = [0.0f64].repeat((di*dj) as usize);
buf.shrink_to_fit();
let mut buf = ManuallyDrop::new(buf);
let (c_buf, buf_len, buf_cap) = (buf.as_mut_ptr() as *mut f64, buf.len(), buf.capacity());
let mut new_buf:Vec<f64>;
unsafe {
match self.cint_type {
CintType::Spheric => cint::cint1e_nuc_sph(
c_buf, c_shls,
self.c_atm.0, self.c_natm,
self.c_bas.0,self.c_nbas,
self.c_env.0,
self.c_opt.0),
CintType::Cartesian => cint::cint1e_nuc_cart(
c_buf, c_shls,
self.c_atm.0, self.c_natm,
self.c_bas.0,self.c_nbas,
self.c_env.0,
self.c_opt.0),
};
let shls = Vec::from_raw_parts(c_shls, shls_len, shls_cap);
new_buf = Vec::from_raw_parts(c_buf, buf_len, buf_cap);
}
new_buf
}
pub fn cint_ijkin(&mut self, i:i32,j:i32) -> Vec<f64> {
let mut di: i32 = self.cint_cgto_rust(i);
let mut dj: i32 = self.cint_cgto_rust(j);
let mut shls: Vec<c_int> = vec![i as c_int,j as c_int];
shls.shrink_to_fit();
let mut shls = ManuallyDrop::new(shls);
let (c_shls,shls_len,shls_cap) = (shls.as_mut_ptr() as *mut c_int,shls.len(),shls.capacity());
let mut buf: Vec<f64> = [0.0f64].repeat((di*dj) as usize);
buf.shrink_to_fit();
let mut buf = ManuallyDrop::new(buf);
let (c_buf, buf_len, buf_cap) = (buf.as_mut_ptr() as *mut f64, buf.len(), buf.capacity());
let mut new_buf:Vec<f64>;
unsafe {
match self.cint_type {
CintType::Spheric => cint::cint1e_kin_sph(
c_buf, c_shls,
self.c_atm.0, self.c_natm,
self.c_bas.0,self.c_nbas,
self.c_env.0,
self.c_opt.0),
CintType::Cartesian => cint::cint1e_kin_cart(
c_buf, c_shls,
self.c_atm.0, self.c_natm,
self.c_bas.0,self.c_nbas,
self.c_env.0,
self.c_opt.0),
};
let shls = Vec::from_raw_parts(c_shls, shls_len, shls_cap);
new_buf = Vec::from_raw_parts(c_buf, buf_len, buf_cap);
}
new_buf
}
}
pub fn cint2e_sph_rust(mut buf: Vec<f64>, mut shls: Vec<i32>,
c_atm: & *mut c_int, c_natm:c_int,
c_bas: & *mut c_int, c_nbas:c_int,
c_env: & *mut f64,
c_opt: & *mut CINTOpt) -> Vec<f64> {
buf.shrink_to_fit();
let mut buf = ManuallyDrop::new(buf);
let (c_buf, buf_len, buf_cap) = (buf.as_mut_ptr() as *mut f64, buf.len(), buf.capacity());
shls.shrink_to_fit();
let mut shls = ManuallyDrop::new(shls);
let (c_shls,shls_len,shls_cap) = (shls.as_mut_ptr() as *mut c_int,shls.len(),shls.capacity());
let mut new_buf:Vec<f64>;
unsafe {
cint::cint2e_sph(c_buf, c_shls, *c_atm, c_natm, *c_bas, c_nbas, *c_env, *c_opt);
let shls = Vec::from_raw_parts(c_shls, shls_len, shls_cap);
new_buf = Vec::from_raw_parts(c_buf, buf_len, buf_cap);
}
new_buf
}
pub fn cint1e_ovlp_sph_rust(mut buf: Vec<f64>, mut shls: Vec<i32>,
c_atm: & *mut c_int, c_natm:c_int,
c_bas: & *mut c_int, c_nbas:c_int,
c_env: & *mut f64,
c_opt: & *mut CINTOpt) -> Vec<f64> {
buf.shrink_to_fit();
let mut buf = ManuallyDrop::new(buf);
let (c_buf, buf_len, buf_cap) = (buf.as_mut_ptr() as *mut f64, buf.len(), buf.capacity());
shls.shrink_to_fit();
let mut shls = ManuallyDrop::new(shls);
let (c_shls,shls_len,shls_cap) = (shls.as_mut_ptr() as *mut c_int,shls.len(),shls.capacity());
let mut new_buf:Vec<f64>;
unsafe {
cint::cint1e_ovlp_sph(c_buf, c_shls, *c_atm, c_natm, *c_bas, c_nbas, *c_env, *c_opt);
let shls = Vec::from_raw_parts(c_shls, shls_len, shls_cap);
new_buf = Vec::from_raw_parts(c_buf, buf_len, buf_cap);
}
new_buf
}