Optimization

This commit is contained in:
Anthony Scemama 2018-02-02 01:25:10 +01:00
parent fb8fff6ee1
commit 49c503fa55
9 changed files with 95 additions and 79 deletions

View File

@ -1,4 +1,5 @@
open Util
open Constants
type t = {
expo : float array;

View File

@ -1,6 +1,7 @@
(** Electron-electron repulsion integrals *)
open Util
open Constants
(** (00|00)^m : Fundamental electron repulsion integral
$ \int \int \phi_p(r1) 1/r_{12} \phi_q(r2) dr_1 dr_2 $

View File

@ -1,4 +1,5 @@
open Util
open Constants
type t = {
expo : float;

View File

@ -1,4 +1,5 @@
open Util
open Constants
let cutoff2 = cutoff *. cutoff
let debug = false
@ -12,10 +13,10 @@ let hvrr_two_e (angMom_a, angMom_b, angMom_c, angMom_d)
(expo_b, expo_d)
(expo_inv_p, expo_inv_q)
(center_ab, center_cd, center_pq)
map
map_1d map_2d
=
let totAngMom_a = Angular_momentum.to_int totAngMom_a
(* 2_011_273 *) let totAngMom_a = Angular_momentum.to_int totAngMom_a
and totAngMom_b = Angular_momentum.to_int totAngMom_b
and totAngMom_c = Angular_momentum.to_int totAngMom_c
and totAngMom_d = Angular_momentum.to_int totAngMom_d
@ -38,30 +39,27 @@ let hvrr_two_e (angMom_a, angMom_b, angMom_c, angMom_d)
(** Vertical recurrence relations *)
let rec vrr0 angMom_a totAngMom_a =
if debug then
(* 1_137_164 *) if debug then
Printf.printf "vrr0: %d : %d %d %d\n" totAngMom_a angMom_a.(0) angMom_a.(1) angMom_a.(2);
match totAngMom_a with
| 0 -> zero_m_array
| 0 -> (* 66_288 *) zero_m_array
| _ ->
let maxsze = maxm+1 in
(* 1_070_876 *) let maxsze = maxm+1 in
let key = Zkey.of_int_tuple (Zkey.Three
(angMom_a.(0)+1, angMom_a.(1)+1, angMom_a.(2)+1) )
in
try Zmap.find map key with
try Zmap.find map_1d key with
| Not_found ->
let result =
let am = [| angMom_a.(0) ; angMom_a.(1) ; angMom_a.(2) |]
and amm = [| angMom_a.(0) ; angMom_a.(1) ; angMom_a.(2) |]
in
let am = [| angMom_a.(0) ; angMom_a.(1) ; angMom_a.(2) |] in
let xyz =
match angMom_a with
| [|_;0;0|] -> 0
| [|_;_;0|] -> 1
| _ -> 2
| [|_;0;0|] -> (* 28_336 *) 0
| [|_;_;0|] -> (* 52_221 *) 1
| _ -> (* 87_215 *) 2
in
am.(xyz) <- am.(xyz) - 1;
amm.(xyz) <- amm.(xyz) - 2;
if am.(xyz) < 0 then empty else
let v1 =
vrr0 am (totAngMom_a-1)
@ -69,54 +67,53 @@ let hvrr_two_e (angMom_a, angMom_b, angMom_c, angMom_d)
let f1 = expo_inv_p *. (Coordinate.coord center_pq xyz)
and f2 = expo_b *. expo_inv_p *. (Coordinate.coord center_ab xyz)
in
if amm.(xyz) < 0 then
Array.init (maxsze) (fun m ->
if m = maxm then 0. else (f1 *. v1.(m+1) ) -. f2 *. v1.(m) )
if am.(xyz) < 1 then
Array.init maxsze (fun m ->
(* 544_860 *) if m = maxm then 0. else (f1 *. v1.(m+1) ) -. f2 *. v1.(m) )
else
let f3 = (float_of_int am.(xyz)) *. expo_inv_p *. 0.5 in
let amm = [| angMom_a.(0) ; angMom_a.(1) ; angMom_a.(2) |] in
let () = amm.(xyz) <- amm.(xyz) - 2 in
let v3 =
vrr0 amm (totAngMom_a-2)
in
Array.init (maxsze) (fun m ->
(if m = maxm then 0. else (f1 *. v1.(m+1) ) -. f2 *. v1.(m) )
+. f3 *. (v3.(m) +. if m = maxm then 0. else
Array.init maxsze (fun m ->
(* 484_257 *) (if m = maxm then 0. else
(f1 *. v1.(m+1) ) -. f2 *. v1.(m) )
+. f3 *. (v3.(m) +. if m = maxm then 0. else
expo_inv_p *. v3.(m+1))
)
in Zmap.add map key result;
in Zmap.add map_1d key result;
result
and vrr angMom_a angMom_c totAngMom_a totAngMom_c =
if debug then
(* 11_580_843 *) if debug then
Printf.printf "vrr : %d %d : %d %d %d %d %d %d\n" totAngMom_a totAngMom_c angMom_a.(0) angMom_a.(1) angMom_a.(2) angMom_c.(0) angMom_c.(1) angMom_c.(2);
match (totAngMom_a, totAngMom_c) with
| (i,0) -> if (i>0) then vrr0 angMom_a totAngMom_a else zero_m_array
| (i,0) -> (* 959_629 *) if (i>0) then vrr0 angMom_a totAngMom_a else zero_m_array
| (_,_) ->
let maxsze = maxm+1 in
(* 10_621_214 *) let maxsze = maxm+1 in
let key = Zkey.of_int_tuple (Zkey.Six
((angMom_a.(0)+1, angMom_a.(1)+1, angMom_a.(2)+1),
(angMom_c.(0)+1, angMom_c.(1)+1, angMom_c.(2)+1)) )
in
try Zmap.find map key with
try Zmap.find map_2d key with
| Not_found ->
let result =
let am = [| angMom_a.(0) ; angMom_a.(1) ; angMom_a.(2) |]
and cm = [| angMom_c.(0) ; angMom_c.(1) ; angMom_c.(2) |]
and cmm = [| angMom_c.(0) ; angMom_c.(1) ; angMom_c.(2) |]
and xyz =
let cm = [| angMom_c.(0) ; angMom_c.(1) ; angMom_c.(2) |] in
let xyz =
match angMom_c with
| [|_;0;0|] -> 0
| [|_;_;0|] -> 1
| _ -> 2
| [|_;0;0|] -> (* 321_984 *) 0
| [|_;_;0|] -> (* 612_002 *) 1
| _ -> (* 1_067_324 *) 2
in
am.(xyz) <- am.(xyz) - 1;
cm.(xyz) <- cm.(xyz) - 1;
cmm.(xyz) <- cmm.(xyz) - 2;
if cm.(xyz) < 0 then empty else
let f1 =
-. expo_d *. expo_inv_q *. (Coordinate.coord center_cd xyz)
@ -129,22 +126,28 @@ let hvrr_two_e (angMom_a, angMom_b, angMom_c, angMom_d)
let v1 =
vrr angMom_a cm totAngMom_a (totAngMom_c-1)
in
Array.init (maxsze) (fun m ->
f1 *. v1.(m) -. (if m = maxm then 0. else f2 *. v1.(m+1)) )
Array.init maxsze (fun m ->
(* 9_232_029 *) f1 *. v1.(m) -. (if m = maxm then 0. else f2 *. v1.(m+1)) )
in
let result =
if cmm.(xyz) < 0 then result else
if cm.(xyz) < 1 then result else
let f3 =
(float_of_int cm.(xyz)) *. expo_inv_q *. 0.5
in
if (abs_float f3 < cutoff) && (abs_float (f3 *. expo_inv_q) < cutoff) then result else
(
let cmm = [| angMom_c.(0) ; angMom_c.(1) ; angMom_c.(2) |] in
cmm.(xyz) <- cmm.(xyz) - 2;
let v3 =
vrr angMom_a cmm totAngMom_a (totAngMom_c-2)
in
Array.init (maxsze) (fun m -> result.(m) +.
Array.init maxsze (fun m -> (* 7_322_025 *) result.(m) +.
f3 *. (v3.(m) +. (if m=maxm then 0. else expo_inv_q *. v3.(m+1)) ))
)
in
let result =
let am = [| angMom_a.(0) ; angMom_a.(1) ; angMom_a.(2) |] in
am.(xyz) <- am.(xyz) - 1;
if am.(xyz) lor cm.(xyz) < 0 then result else
let f5 =
(float_of_int angMom_a.(xyz)) *. expo_inv_p *. expo_inv_q *. 0.5
@ -154,10 +157,10 @@ let hvrr_two_e (angMom_a, angMom_b, angMom_c, angMom_d)
vrr am cm (totAngMom_a-1) (totAngMom_c-1)
in
Array.init (maxsze) (fun m ->
result.(m) -. (if m = maxm then 0. else f5 *. v5.(m+1)))
(* 7_963_213 *) result.(m) -. (if m = maxm then 0. else f5 *. v5.(m+1)))
in
result
in Zmap.add map key result;
in Zmap.add map_2d key result;
result
@ -168,13 +171,13 @@ let hvrr_two_e (angMom_a, angMom_b, angMom_c, angMom_d)
and hrr0 angMom_a angMom_b angMom_c
totAngMom_a totAngMom_b totAngMom_c =
if debug then
(* 8_448_486 *) if debug then
Printf.printf "hrr0: %d %d %d : %d %d %d %d %d %d %d %d %d\n" totAngMom_a totAngMom_b totAngMom_c angMom_a.(0) angMom_a.(1) angMom_a.(2) angMom_b.(0) angMom_b.(1) angMom_b.(2) angMom_c.(0) angMom_c.(1) angMom_c.(2);
match totAngMom_b with
| 0 -> (vrr angMom_a angMom_c totAngMom_a totAngMom_c).(0)
| 0 -> (* 0 *) (vrr angMom_a angMom_c totAngMom_a totAngMom_c).(0)
| 1 ->
let xyz = if angMom_b.(0) = 1 then 0 else if angMom_b.(1) = 1 then 1 else 2 in
(* 5_045_008 *) let xyz = if angMom_b.(0) = 1 then 0 else if angMom_b.(1) = 1 then 1 else 2 in
let ap = [| angMom_a.(0) ; angMom_a.(1) ; angMom_a.(2) |] in
ap.(xyz) <- ap.(xyz) + 1;
let v1 =
@ -189,13 +192,13 @@ let hvrr_two_e (angMom_a, angMom_b, angMom_c, angMom_d)
in
v1.(0) +. f2 *. v2.(0)
| _ ->
let ap = [| angMom_a.(0) ; angMom_a.(1) ; angMom_a.(2) |]
(* 3_403_478 *) let ap = [| angMom_a.(0) ; angMom_a.(1) ; angMom_a.(2) |]
and bm = [| angMom_b.(0) ; angMom_b.(1) ; angMom_b.(2) |]
and xyz =
match angMom_b with
| [|_;0;0|] -> 0
| [|_;_;0|] -> 1
| _ -> 2
| [|_;0;0|] -> (* 677_315 *) 0
| [|_;_;0|] -> (* 1_136_646 *) 1
| _ -> (* 1_589_517 *) 2
in
ap.(xyz) <- ap.(xyz) + 1;
bm.(xyz) <- bm.(xyz) - 1;
@ -215,20 +218,22 @@ let hvrr_two_e (angMom_a, angMom_b, angMom_c, angMom_d)
and hrr angMom_a angMom_b angMom_c angMom_d
totAngMom_a totAngMom_b totAngMom_c totAngMom_d =
if debug then
(* 7_738_602 *) if debug then
Printf.printf "hrr : %d %d %d %d : %d %d %d %d %d %d %d %d %d %d %d %d\n" totAngMom_a totAngMom_b totAngMom_c totAngMom_d angMom_a.(0) angMom_a.(1) angMom_a.(2) angMom_b.(0) angMom_b.(1) angMom_b.(2) angMom_c.(0) angMom_c.(1) angMom_c.(2) angMom_d.(0) angMom_d.(1) angMom_d.(2);
match (totAngMom_b, totAngMom_d) with
| (0,0) -> (vrr angMom_a angMom_c totAngMom_a totAngMom_c).(0)
| (_,0) -> hrr0 angMom_a angMom_b angMom_c totAngMom_a totAngMom_b totAngMom_c
| (_,0) -> (* 3_608_781 *) if (totAngMom_b = 0) then
(vrr angMom_a angMom_c totAngMom_a totAngMom_c).(0)
else
hrr0 angMom_a angMom_b angMom_c totAngMom_a totAngMom_b totAngMom_c
| (_,_) ->
let cp = [| angMom_c.(0) ; angMom_c.(1) ; angMom_c.(2) |]
(* 4_130_325 *) let cp = [| angMom_c.(0) ; angMom_c.(1) ; angMom_c.(2) |]
and dm = [| angMom_d.(0) ; angMom_d.(1) ; angMom_d.(2) |]
and xyz =
match angMom_d with
| [|_;0;0|] -> 0
| [|_;_;0|] -> 1
| _ -> 2
| [|_;0;0|] -> (* 1_524_451 *) 0
| [|_;_;0|] -> (* 1_302_937 *) 1
| _ -> (* 1_302_937 *) 2
in
cp.(xyz) <- cp.(xyz) + 1;
dm.(xyz) <- dm.(xyz) - 1;
@ -250,7 +255,7 @@ let hvrr_two_e (angMom_a, angMom_b, angMom_c, angMom_d)
let contracted_class_shell_pairs ~zero_m ?schwartz_p ?schwartz_q shell_p shell_q : float Zmap.t =
let shell_a = shell_p.(0).Shell_pair.shell_a
(* 12850 *) let shell_a = shell_p.(0).Shell_pair.shell_a
and shell_b = shell_p.(0).Shell_pair.shell_b
and shell_c = shell_q.(0).Shell_pair.shell_a
and shell_d = shell_q.(0).Shell_pair.shell_b
@ -305,17 +310,18 @@ let contracted_class_shell_pairs ~zero_m ?schwartz_p ?schwartz_q shell_p shell_q
match Contracted_shell.(totAngMom shell_a, totAngMom shell_b,
totAngMom shell_c, totAngMom shell_d) with
| Angular_momentum.(S,S,S,S) ->
let integral =
(* 14_700 *) let integral =
zero_m_array.(0)
in
contracted_class.(0) <- contracted_class.(0) +. coef_prod *. integral
| _ ->
let d = shell_q.(cd).Shell_pair.j in
let map = Zmap.create (Array.length class_indices) in
(* 15_577 *) let d = shell_q.(cd).Shell_pair.j in
let map_1d = Zmap.create (4*maxm) in
let map_2d = Zmap.create (Array.length class_indices) in
let norm_coef_scale_q = shell_q.(cd).Shell_pair.norm_coef_scale in
let norm_coef_scale =
Array.map (fun v1 ->
Array.map (fun v2 -> v1 *. v2) norm_coef_scale_q
(* 165_245 *) Array.map (fun v2 -> (* 2_011_273 *) v1 *. v2) norm_coef_scale_q
) norm_coef_scale_p
|> Array.to_list
|> Array.concat
@ -323,7 +329,7 @@ let contracted_class_shell_pairs ~zero_m ?schwartz_p ?schwartz_q shell_p shell_q
(* Compute the integral class from the primitive shell quartet *)
class_indices
|> Array.iteri (fun i key ->
let a = Zkey.to_int_array Zkey.Kind_12 key in
(* 2_011_273 *) let a = Zkey.to_int_array Zkey.Kind_12 key in
let (angMomA,angMomB,angMomC,angMomD) =
( [| a.(0) ; a.(1) ; a.(2) |],
[| a.(3) ; a.(4) ; a.(5) |],
@ -363,15 +369,16 @@ let contracted_class_shell_pairs ~zero_m ?schwartz_p ?schwartz_q shell_p shell_q
let norm = norm_coef_scale.(i) in
let integral = chop norm (fun () ->
hvrr_two_e (angMomA, angMomB, angMomC, angMomD)
let coef_prod = coef_prod *. norm in
let integral =
(* 2_011_273 *) hvrr_two_e (angMomA, angMomB, angMomC, angMomD)
(Contracted_shell.totAngMom shell_a, Contracted_shell.totAngMom shell_b,
Contracted_shell.totAngMom shell_c, Contracted_shell.totAngMom shell_d)
(maxm, zero_m_array)
(Contracted_shell.expo shell_b b, Contracted_shell.expo shell_d d)
(shell_p.(ab).Shell_pair.expo_inv, shell_q.(cd).Shell_pair.expo_inv)
(shell_p.(ab).Shell_pair.center_ab, shell_q.(cd).Shell_pair.center_ab, center_pq)
map )
map_1d map_2d
in
contracted_class.(i) <- contracted_class.(i) +. coef_prod *. integral
with NullQuartet -> ()
@ -384,7 +391,7 @@ let contracted_class_shell_pairs ~zero_m ?schwartz_p ?schwartz_q shell_p shell_q
let result =
Zmap.create (Array.length contracted_class)
in
Array.iteri (fun i key -> Zmap.add result key contracted_class.(i)) class_indices;
Array.iteri (fun i key -> (* 1_929_480 *) Zmap.add result key contracted_class.(i)) class_indices;
result
@ -392,7 +399,7 @@ let contracted_class_shell_pairs ~zero_m ?schwartz_p ?schwartz_q shell_p shell_q
(** Computes all the two-electron integrals of the contracted shell quartet *)
let contracted_class ~zero_m shell_a shell_b shell_c shell_d : float Zmap.t =
let shell_p = Shell_pair.create_array ~cutoff shell_a shell_b
(* 0 *) let shell_p = Shell_pair.create_array ~cutoff shell_a shell_b
and shell_q = Shell_pair.create_array ~cutoff shell_c shell_d
in
contracted_class_shell_pairs ~zero_m shell_p shell_q

View File

@ -1,5 +1,6 @@
open Util
let cutoff = Constants.cutoff
let cutoff2 = cutoff *. cutoff
exception NullQuartet
@ -17,7 +18,7 @@ let hvrr_two_e_vector (angMom_a, angMom_b, angMom_c, angMom_d)
(expo_b, expo_d)
(expo_inv_p, expo_inv_q)
(center_ab, center_cd, center_pq)
coef_prod map
coef_prod map_1d map_2d
=
let ncoef = (Array.length coef_prod) in
@ -47,7 +48,7 @@ let hvrr_two_e_vector (angMom_a, angMom_b, angMom_c, angMom_d)
in
let (found, result) =
try (true, Zmap.find map.(m) key) with
try (true, Zmap.find map_1d.(m) key) with
| Not_found -> (false,
let am = [| angMom_a.(0) ; angMom_a.(1) ; angMom_a.(2) |]
and amm = [| angMom_a.(0) ; angMom_a.(1) ; angMom_a.(2) |]
@ -86,7 +87,7 @@ let hvrr_two_e_vector (angMom_a, angMom_b, angMom_c, angMom_d)
)
in
if not found then
Zmap.add map.(m) key result;
Zmap.add map_1d.(m) key result;
result
and vrr_v m angMom_a angMom_c totAngMom_a totAngMom_c =
@ -105,7 +106,7 @@ let hvrr_two_e_vector (angMom_a, angMom_b, angMom_c, angMom_d)
in
let (found, result) =
try (true, Zmap.find map.(m) key) with
try (true, Zmap.find map_2d.(m) key) with
| Not_found -> (false,
let am = [| angMom_a.(0) ; angMom_a.(1) ; angMom_a.(2) |]
and cm = [| angMom_c.(0) ; angMom_c.(1) ; angMom_c.(2) |]
@ -182,7 +183,7 @@ let hvrr_two_e_vector (angMom_a, angMom_b, angMom_c, angMom_d)
)
in
if not found then
Zmap.add map.(m) key result;
Zmap.add map_2d.(m) key result;
result
@ -376,7 +377,8 @@ let contracted_class_shell_pairs ~zero_m ?schwartz_p ?schwartz_q shell_p shell_q
center_pq,coef_prod) -> coef_prod) common
in
(* Compute the integral class from the primitive shell quartet *)
let map = Array.init maxm (fun _ -> Zmap.create (Array.length class_indices)) in
let map_1d = Array.init maxm (fun _ -> Zmap.create (4*maxm)) in
let map_2d = Array.init maxm (fun _ -> Zmap.create (Array.length class_indices)) in
let norm =
let norm_coef_scale_q = shell_q.(0).Shell_pair.norm_coef_scale in
Array.map (fun v1 ->
@ -401,11 +403,10 @@ let contracted_class_shell_pairs ~zero_m ?schwartz_p ?schwartz_q shell_p shell_q
(Contracted_shell.expo shell_b b, d)
(shell_ab.Shell_pair.expo_inv, expo_inv)
(shell_ab.Shell_pair.center_ab, center_cd, center_pq)
coef_prod map
|> Array.map (fun x -> x *. norm.(i) )
coef_prod map_1d map_2d
in
let x = Array.fold_left (+.) 0. integral in
contracted_class.(i) <- contracted_class.(i) +. x
contracted_class.(i) <- contracted_class.(i) +. x *. norm.(i)
) class_indices
) shell_p

View File

@ -43,6 +43,11 @@ doc: qpackage.odocl
$(OCAMLBUILD) $*.p.native -use-ocamlfind $(PKGS)
ln -s $*.p.native $*
%.p.byte: $(MLFILES) $(MLIFILES) $(MLLFILES) $(MLYFILES)
rm -f -- $*
$(OCAMLBUILD) -ocamlc ocamlcp $*.byte -use-ocamlfind $(PKGS)
ln -s $*.byte $*
clean:
rm -rf _build $(ALL_EXE) $(ALL_TESTS) *.native *.byte

View File

@ -1,9 +1,9 @@
open Util
type t =
| Bohr of (float * float * float)
| Angstrom of (float * float * float)
let a0 = Constants.a0
let zero = Bohr (0., 0., 0.)
let of_float_triplet (x,y,z) = function
@ -35,10 +35,10 @@ let (|.) s a =
let to_Angstrom = function
| Angstrom a -> Angstrom a
| Bohr a -> Angstrom (a0 |. Bohr a |> extract_float_tuple)
| Bohr a -> Angstrom (Constants.a0 |. Bohr a |> extract_float_tuple)
let to_Bohr = function
| Angstrom a -> Bohr (1./.a0 |. Angstrom a |> extract_float_tuple)
| Angstrom a -> Bohr (1./.Constants.a0 |. Angstrom a |> extract_float_tuple)
| Bohr a -> Bohr a
let (|-), (|+) =

View File

@ -3,7 +3,7 @@ external erf_float : float -> float = "erf_float_bytecode" "erf_float" [@@unboxe
external erfc_float : float -> float = "erfc_float_bytecode" "erfc_float" [@@unboxed] [@@noalloc]
external gamma_float : float -> float = "gamma_float_bytecode" "gamma_float" [@@unboxed] [@@noalloc]
include Constants
open Constants
let factmax = 150

2
_tags
View File

@ -1,3 +1,3 @@
true: package(str,zarith)
<*.byte> : linkdep(Utils/math_functions.o)
<*.byte> : linkdep(Utils/math_functions.o), custom
<*.native>: linkdep(Utils/math_functions.o)