10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-06-21 12:42:05 +02:00
QCaml/Basis/ERI.ml

248 lines
7.8 KiB
OCaml
Raw Normal View History

2018-01-19 03:14:06 +01:00
(** Electron-electron repulsion integrals *)
2018-01-17 19:09:57 +01:00
open Util
2018-02-02 01:25:10 +01:00
open Constants
2018-02-02 10:10:05 +01:00
open Bigarray
2018-01-17 19:09:57 +01:00
2018-02-10 01:07:34 +01:00
type t = (float, float32_elt, fortran_layout) Bigarray.Genarray.t
2018-02-09 00:37:25 +01:00
2018-03-13 18:24:00 +01:00
module Am = AngularMomentum
2018-02-23 15:49:27 +01:00
module Bs = Basis
2018-02-23 18:41:30 +01:00
module Cs = ContractedShell
2018-02-23 15:49:27 +01:00
module Csp = ContractedShellPair
2018-02-24 23:57:38 +01:00
let cutoff = integrals_cutoff
2018-01-18 23:42:48 +01:00
(** (00|00)^m : Fundamental electron repulsion integral
2018-01-19 03:14:06 +01:00
$ \int \int \phi_p(r1) 1/r_{12} \phi_q(r2) dr_1 dr_2 $
2018-01-17 19:09:57 +01:00
2018-01-19 03:14:06 +01:00
maxm : Maximum total angular momentum
expo_pq_inv : $1./p + 1./q$ where $p$ and $q$ are the exponents of
$\phi_p$ and $\phi_q$
norm_pq_sq : square of the distance between the centers of $\phi_p$
and $\phi_q$
2018-01-17 19:09:57 +01:00
*)
2018-02-02 21:14:13 +01:00
2018-01-17 19:09:57 +01:00
let zero_m ~maxm ~expo_pq_inv ~norm_pq_sq =
2018-01-19 23:31:10 +01:00
let exp_pq = 1. /. expo_pq_inv in
let t = norm_pq_sq *. exp_pq in
2018-02-09 02:01:26 +01:00
let f = two_over_sq_pi *. (sqrt exp_pq) in
let result = boys_function ~maxm t in
let rec aux accu k = function
| 0 -> result.(k) <- result.(k) *. accu
| l ->
begin
result.(k) <- result.(k) *. accu;
let new_accu = -. accu *. exp_pq in
aux new_accu (k+1) (l-1)
end
in
aux f 0 maxm;
result
2018-01-17 19:09:57 +01:00
2018-01-23 19:26:28 +01:00
(** Compute all the integrals of a contracted class *)
2018-01-29 22:48:09 +01:00
let contracted_class_shell_pairs ?schwartz_p ?schwartz_q shell_p shell_q : float Zmap.t =
TwoElectronRR.contracted_class_shell_pairs ~zero_m ?schwartz_p ?schwartz_q shell_p shell_q
2018-01-23 19:26:28 +01:00
2018-01-29 22:48:09 +01:00
let contracted_class_shell_pairs_vec ?schwartz_p ?schwartz_q shell_p shell_q : float Zmap.t =
2018-01-25 01:28:10 +01:00
TwoElectronRRVectorized.contracted_class_shell_pairs ~zero_m ?schwartz_p ?schwartz_q shell_p shell_q
2018-01-31 14:37:51 +01:00
2018-01-19 03:14:06 +01:00
2018-01-24 02:14:37 +01:00
let cutoff2 = cutoff *. cutoff
2018-02-03 23:26:20 +01:00
(*
2018-02-14 19:23:23 +01:00
type n_cls = { n : int ; cls : Zkey.t array }
2018-02-03 23:26:20 +01:00
*)
2018-01-23 19:26:28 +01:00
exception NullIntegral
2018-01-19 17:42:12 +01:00
2018-02-02 10:10:05 +01:00
(*
(** Unique index for integral <ij|kl> *)
let index i j k l =
let f i k =
let (p,r) =
if i <= k then (i,k) else (k,i)
in p+ (r*r-r)/2
in
let p = f i k and q = f j l in
f p q
*)
2018-02-09 00:37:25 +01:00
let of_basis basis =
2018-02-19 16:01:13 +01:00
let to_powers x =
2018-01-19 23:31:10 +01:00
let open Zkey in
2018-02-25 01:40:12 +01:00
match to_powers x with
2018-01-19 23:31:10 +01:00
| Three x -> x
| _ -> assert false
in
2018-03-20 14:11:31 +01:00
let n = Bs.size basis
and shell = Bs.contracted_shells basis
2018-02-09 00:37:25 +01:00
in
2018-01-23 19:26:28 +01:00
2018-02-07 17:07:05 +01:00
2018-01-23 19:26:28 +01:00
(* Pre-compute all shell pairs *)
let shell_pairs =
2018-03-20 14:11:31 +01:00
Csp.of_contracted_shell_array shell
2018-02-07 17:07:05 +01:00
in
2018-01-23 19:26:28 +01:00
(* Pre-compute diagonal integrals for Schwartz *)
2018-02-02 10:10:05 +01:00
let t0 = Unix.gettimeofday () in
2018-01-23 19:26:28 +01:00
let schwartz =
2018-03-15 19:35:10 +01:00
Array.map (fun pair_array -> Array.map (function
| None -> (Zmap.create 0, 0.)
| Some pair ->
let cls =
contracted_class_shell_pairs pair pair
in
(cls, Zmap.fold (fun key value accu -> max (abs_float value) accu) cls 0. )
2018-01-23 19:26:28 +01:00
) pair_array ) shell_pairs
in
2018-01-30 22:36:17 +01:00
let icount = ref 0 in
2018-02-09 00:37:25 +01:00
for i=0 to (Array.length shell) - 1 do
2018-03-13 18:56:28 +01:00
print_int (Cs.index shell.(i)) ; print_newline ();
2018-01-30 22:36:17 +01:00
for j=0 to i do
let schwartz_p, schwartz_p_max = schwartz.(i).(j) in
2018-02-03 23:26:20 +01:00
if (schwartz_p_max >= cutoff) then
icount := !icount + 1;
2018-01-30 22:36:17 +01:00
done;
done;
2018-02-02 10:10:05 +01:00
Printf.printf "%d shell pairs computed in %f seconds\n" !icount (Unix.gettimeofday () -. t0);
2018-02-23 15:49:27 +01:00
(* Group shell pairs by common pairs of atoms *)
2018-02-08 01:00:54 +01:00
(* 4D data initialization *)
2018-02-02 10:10:05 +01:00
let eri_array =
2018-02-10 01:07:34 +01:00
Genarray.create Float32 fortran_layout [| n ; n ; n ; n|]
2018-02-02 10:10:05 +01:00
in
Genarray.fill eri_array 0.;
(* Compute ERIs *)
2018-01-30 22:36:17 +01:00
2018-02-09 00:37:25 +01:00
let t0 = Unix.gettimeofday () in
2018-01-24 02:14:37 +01:00
let inn = ref 0 and out = ref 0 in
2018-02-09 00:37:25 +01:00
for i=0 to (Array.length shell) - 1 do
2018-03-13 18:56:28 +01:00
print_int (Cs.index shell.(i)) ; print_newline ();
2018-01-19 03:14:06 +01:00
for j=0 to i do
2018-01-23 19:26:28 +01:00
let schwartz_p, schwartz_p_max = schwartz.(i).(j) in
try
if (schwartz_p_max < cutoff) then raise NullIntegral;
2018-02-01 17:13:47 +01:00
2018-03-15 19:35:10 +01:00
let shell_p =
match shell_pairs.(i).(j) with
| None -> raise NullIntegral
| Some x -> x
2018-02-09 00:37:25 +01:00
in
2018-02-09 19:41:22 +01:00
let sp =
2018-03-14 14:39:22 +01:00
Csp.shell_pairs shell_p
2018-02-09 19:41:22 +01:00
in
2018-01-23 19:26:28 +01:00
for k=0 to i do
for l=0 to k do
let schwartz_q, schwartz_q_max = schwartz.(k).(l) in
try
2018-02-03 23:26:20 +01:00
if schwartz_p_max *. schwartz_q_max < cutoff2 then
raise NullIntegral;
2018-03-15 19:35:10 +01:00
let shell_q =
match shell_pairs.(k).(l) with
| None -> raise NullIntegral
| Some x -> x
2018-02-03 23:26:20 +01:00
in
2018-03-15 19:35:10 +01:00
2018-02-09 19:41:22 +01:00
let sq =
2018-03-14 14:39:22 +01:00
Csp.shell_pairs shell_q
2018-02-09 19:41:22 +01:00
in
2018-02-03 23:26:20 +01:00
let swap =
2018-02-10 03:37:00 +01:00
Array.length sp > Array.length sq
2018-02-03 23:26:20 +01:00
in
(* Compute all the integrals of the class *)
let cls =
if swap then
2018-02-23 15:02:23 +01:00
if (Array.length sp) + (Array.length sq) < 4 then
2018-02-03 23:26:20 +01:00
contracted_class_shell_pairs ~schwartz_p:schwartz_q ~schwartz_q:schwartz_p shell_q shell_p
2018-01-29 23:51:58 +01:00
else
2018-02-03 23:26:20 +01:00
contracted_class_shell_pairs_vec ~schwartz_p:schwartz_q ~schwartz_q:schwartz_p shell_q shell_p
else
2018-02-23 15:02:23 +01:00
if (Array.length sp) + (Array.length sq) < 4 then
2018-02-03 23:26:20 +01:00
contracted_class_shell_pairs ~schwartz_p ~schwartz_q shell_p shell_q
2018-01-29 23:51:58 +01:00
else
2018-02-03 23:26:20 +01:00
contracted_class_shell_pairs_vec ~schwartz_p ~schwartz_q shell_p shell_q
in
2018-02-14 18:08:43 +01:00
2018-02-03 23:26:20 +01:00
(* Write the data in the output file *)
Array.iteri (fun i_c powers_i ->
2018-03-13 18:56:28 +01:00
let i_c = Cs.index shell.(i) + i_c + 1 in
2018-02-19 16:01:13 +01:00
let xi = to_powers powers_i in
2018-02-03 23:26:20 +01:00
Array.iteri (fun j_c powers_j ->
2018-03-13 18:56:28 +01:00
let j_c = Cs.index shell.(j) + j_c + 1 in
2018-02-19 16:01:13 +01:00
let xj = to_powers powers_j in
2018-02-03 23:26:20 +01:00
Array.iteri (fun k_c powers_k ->
2018-03-13 18:56:28 +01:00
let k_c = Cs.index shell.(k) + k_c + 1 in
2018-02-19 16:01:13 +01:00
let xk = to_powers powers_k in
2018-02-03 23:26:20 +01:00
Array.iteri (fun l_c powers_l ->
2018-03-13 18:56:28 +01:00
let l_c = Cs.index shell.(l) + l_c + 1 in
2018-02-19 16:01:13 +01:00
let xl = to_powers powers_l in
2018-02-03 23:26:20 +01:00
let key =
if swap then
2018-02-25 00:53:09 +01:00
Zkey.of_powers_twelve xk xl xi xj
2018-01-29 23:51:58 +01:00
else
2018-02-25 00:53:09 +01:00
Zkey.of_powers_twelve xi xj xk xl
2018-02-03 23:26:20 +01:00
in
let value =
Zmap.find cls key
in
2018-02-08 01:00:54 +01:00
eri_array.{i_c,k_c,j_c,l_c} <- value;
eri_array.{j_c,k_c,i_c,l_c} <- value;
eri_array.{i_c,l_c,j_c,k_c} <- value;
eri_array.{j_c,l_c,i_c,k_c} <- value;
eri_array.{k_c,i_c,l_c,j_c} <- value;
eri_array.{k_c,j_c,l_c,i_c} <- value;
eri_array.{l_c,i_c,k_c,j_c} <- value;
eri_array.{l_c,j_c,k_c,i_c} <- value;
2018-02-03 23:26:20 +01:00
if (abs_float value > cutoff) then
(inn := !inn + 1;
)
else
out := !out + 1;
2018-03-21 15:01:39 +01:00
) Am.(zkey_array (Singlet (Cs.ang_mom shell.(l))))
) Am.(zkey_array (Singlet (Cs.ang_mom shell.(k))))
) Am.(zkey_array (Singlet (Cs.ang_mom shell.(j))))
) Am.(zkey_array (Singlet (Cs.ang_mom shell.(i))))
2018-02-08 01:00:54 +01:00
with NullIntegral -> ()
2018-01-23 19:26:28 +01:00
done;
2018-01-19 03:14:06 +01:00
done;
2018-01-24 02:14:37 +01:00
with NullIntegral -> ()
2018-01-19 03:14:06 +01:00
done;
2018-01-19 23:31:10 +01:00
done;
2018-02-09 00:37:25 +01:00
Printf.printf "In: %d Out:%d\n" !inn !out ;
2018-02-11 00:05:56 +01:00
Printf.printf "Computed ERIs in %f seconds\n%!" (Unix.gettimeofday () -. t0);
2018-02-09 00:37:25 +01:00
eri_array
2018-02-02 10:10:05 +01:00
2018-02-09 00:37:25 +01:00
(** Write all integrals to a file with the <ij|kl> convention *)
let to_file ~filename eri_array =
let oc = open_out filename in
2018-02-02 10:10:05 +01:00
(* Print ERIs *)
2018-02-09 00:37:25 +01:00
for l_c=1 to (Genarray.nth_dim eri_array 3) do
for k_c=1 to l_c do
for j_c=1 to l_c do
for i_c=1 to k_c do
let value = eri_array.{i_c,j_c,k_c,l_c} in
if (abs_float value > cutoff) then
Printf.fprintf oc " %5d %5d %5d %5d%20.15f\n" i_c j_c k_c l_c value;
done;
done;
2018-02-02 10:10:05 +01:00
done;
done;
2018-01-19 03:14:06 +01:00
close_out oc