From 27d775614f2d16473258055f1e03375b3a60216b Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Mon, 19 Feb 2018 11:24:15 +0100 Subject: [PATCH] Cleaning --- Basis/TwoElectronRRVectorized.ml | 312 +++++++++++++++---------------- Utils/Angular_momentum.ml | 2 +- Utils/Angular_momentum.mli | 15 -- Utils/Powers.ml | 39 ++++ Utils/Powers.mli | 7 + 5 files changed, 196 insertions(+), 179 deletions(-) delete mode 100644 Utils/Angular_momentum.mli create mode 100644 Utils/Powers.ml create mode 100644 Utils/Powers.mli diff --git a/Basis/TwoElectronRRVectorized.ml b/Basis/TwoElectronRRVectorized.ml index e74e8e8..2aca64f 100644 --- a/Basis/TwoElectronRRVectorized.ml +++ b/Basis/TwoElectronRRVectorized.ml @@ -1,6 +1,7 @@ open Util open Lacaml.D open Bigarray +open Powers open Coordinate let cutoff = Constants.cutoff @@ -25,81 +26,67 @@ let hvrr_two_e_vector (angMom_a, angMom_b, angMom_c, angMom_d) map_1d map_2d np nq = - 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 + let angMom_a = Powers.of_int_tuple angMom_a + and angMom_b = Powers.of_int_tuple angMom_b + and angMom_c = Powers.of_int_tuple angMom_c + and angMom_d = Powers.of_int_tuple angMom_d + in + let totAngMom_a = angMom_a.tot + and totAngMom_b = angMom_b.tot + and totAngMom_c = angMom_c.tot + and totAngMom_d = angMom_d.tot + in + + let get_xyz angMom = + match angMom with + | { y=0 ; z=0 ; _ } -> X + | { z=0 ; _ } -> Y + | _ -> Z in (** Vertical recurrence relations *) let rec vrr0_v m angMom_a = function | 0 -> Some zero_m_array.(m) | totAngMom_a -> - let key = Zkey.of_int_tuple (Zkey.Three angMom_a) + let key = Zkey.of_int_tuple (Zkey.Three (Powers.to_int_tuple angMom_a)) in try Zmap.find map_1d.(m) key with | Not_found -> let result = - let am, amm, amxyz, xyz = - match angMom_a with - | (x,0,0) -> (x-1,0,0),(x-2,0,0), x-1, X - | (x,y,0) -> (x,y-1,0),(x,y-2,0), y-1, Y - | (x,y,z) -> (x,y,z-1),(x,y,z-2), z-1, Z - in - if amxyz < 0 then - None - else - begin - let cab = Coordinate.get xyz center_ab in - let v1_top, p1_top = - if abs_float cab < cutoff then - None, - vrr0_v (m+1) am (totAngMom_a-1) - else - vrr0_v m am (totAngMom_a-1), - vrr0_v (m+1) am (totAngMom_a-1) + try + let xyz = get_xyz angMom_a in + let am = + try Powers.decr xyz angMom_a + with Invalid_argument _ -> raise Exit in - let v1_top2, p1_top2 = - if amxyz < 1 then (None,None) else - vrr0_v m amm (totAngMom_a-2), - vrr0_v (m+1) amm (totAngMom_a-2) - in - - let result = Array.make_matrix np nq 0. in - if amxyz < 1 then - begin - let p0 = - match p1_top with - | Some p1_top -> p1_top - | _ -> assert false - in - begin - match v1_top with - | None -> () - | Some v0 -> - Array.iteri (fun l result_l -> - let f0 = -. expo_b.(l) *. expo_inv_p.(l) *. cab - and v0_l = v0.(l) - and result_l = result.(l) - in - Array.iteri (fun k v0_lk -> - result_l.(k) <- v0_lk *. f0) v0_l - ) result - end; - Array.iteri (fun l result_l -> - let expo_inv_p_l = expo_inv_p.(l) - and center_pq_xyz_l = (center_pq xyz).(l) - and result_l = result.(l) - and p0_l = p0.(l) - in - Array.iteri (fun k p0_lk -> - result_l.(k) <- result_l.(k) - +. expo_inv_p_l *. center_pq_xyz_l.(k) *. p0_lk - ) p0_l ) result - end + (* + if amxyz < 0 then + raise Exit else + *) begin + let cab = Coordinate.get xyz center_ab in + let v1_top, p1_top = + if abs_float cab < cutoff then + None, + vrr0_v (m+1) am (totAngMom_a-1) + else + vrr0_v m am (totAngMom_a-1), + vrr0_v (m+1) am (totAngMom_a-1) + in + let v1_top2, p1_top2 = + try + (* + if amxyz < 1 then None, None else + *) + let amm = Powers.decr xyz am in + vrr0_v m amm (totAngMom_a-2), + vrr0_v (m+1) amm (totAngMom_a-2) + with Invalid_argument _ -> None, None + in + + let result = Array.make_matrix np nq 0. in let p0 = match p1_top with | Some p1_top -> p1_top @@ -116,33 +103,48 @@ let hvrr_two_e_vector (angMom_a, angMom_b, angMom_c, angMom_d) Array.iteri (fun k v0_lk -> result_l.(k) <- v0_lk *. f0) v0_l ) result end; - let v1 = - match v1_top2 with - | Some v1_top2 -> v1_top2 - | None -> assert false - in - let v2 = - match p1_top2 with - | Some p1_top2 -> p1_top2 - | None -> assert false - in - Array.iteri (fun l result_l -> - let f = float_of_int amxyz *. expo_inv_p.(l) *. 0.5 - and expo_inv_p_l = expo_inv_p.(l) - and center_pq_xyz_l = (center_pq xyz).(l) - and v1_l = v1.(l) - and v2_l = v2.(l) - and result_l = result.(l) - in - Array.iteri (fun k p0_lk -> - result_l.(k) <- result_l.(k) +. - expo_inv_p_l *. center_pq_xyz_l.(k) *. p0_lk +. - f *. (v1_l.(k) +. v2_l.(k) *. expo_inv_p_l) - ) p0.(l) - ) result - end; - Some result - end + let amxyz = Powers.get xyz am in + if amxyz < 1 then + Array.iteri (fun l result_l -> + let expo_inv_p_l = expo_inv_p.(l) + and center_pq_xyz_l = (center_pq xyz).(l) + and result_l = result.(l) + and p0_l = p0.(l) + in + Array.iteri (fun k p0_lk -> + result_l.(k) <- result_l.(k) + +. expo_inv_p_l *. center_pq_xyz_l.(k) *. p0_lk + ) p0_l ) result + else + begin + let v1 = + match v1_top2 with + | Some v1_top2 -> v1_top2 + | None -> assert false + in + let v2 = + match p1_top2 with + | Some p1_top2 -> p1_top2 + | None -> assert false + in + Array.iteri (fun l result_l -> + let f = float_of_int amxyz *. expo_inv_p.(l) *. 0.5 + and expo_inv_p_l = expo_inv_p.(l) + and center_pq_xyz_l = (center_pq xyz).(l) + and v1_l = v1.(l) + and v2_l = v2.(l) + and result_l = result.(l) + in + Array.iteri (fun k p0_lk -> + result_l.(k) <- result_l.(k) +. + expo_inv_p_l *. center_pq_xyz_l.(k) *. p0_lk +. + f *. (v1_l.(k) +. v2_l.(k) *. expo_inv_p_l) + ) p0.(l) + ) result + end; + Some result + end + with Exit -> None in Zmap.add map_1d.(m) key result; result @@ -153,35 +155,17 @@ let hvrr_two_e_vector (angMom_a, angMom_b, angMom_c, angMom_d) | (i,0) -> vrr0_v m angMom_a totAngMom_a | (_,_) -> - let key = Zkey.of_int_tuple (Zkey.Six (angMom_a, angMom_c)) + let key = Zkey.of_int_tuple (Zkey.Six Powers.(to_int_tuple angMom_a, to_int_tuple angMom_c)) in try Zmap.find map_2d.(m) key with | Not_found -> let result = begin - let am, cm, cmm, axyz, cxyz, xyz = - let (aax, aay, aaz) = angMom_a - and (acx, acy, acz) = angMom_c in - if (acz > 0) then - (aax, aay, aaz-1), - (acx, acy, acz-1), - (acx, acy, acz-2), - aaz, acz, Z - else if (acy > 0) then - (aax, aay-1,aaz), - (acx, acy-1,acz), - (acx, acy-2,acz), - aay,acy, Y - else - (aax-1,aay,aaz), - (acx-1,acy,acz), - (acx-2,acy,acz), - aax,acx, X - in - (* - let result = Array.make_matrix np nq 0. in - *) + let xyz = get_xyz angMom_c in + let cm = Powers.decr xyz angMom_c in + let axyz = Powers.get xyz angMom_a in + let do_compute = ref false in let v1 = let f = -. (Coordinate.get xyz center_cd) in @@ -261,8 +245,16 @@ let hvrr_two_e_vector (angMom_a, angMom_b, angMom_c, angMom_d) end in + let cxyz = Powers.get xyz angMom_c in let p2 = - if cxyz < 2 then p1 else + try + let cmm = + try Powers.decr xyz cm + with Invalid_argument _ -> raise Exit + in + if Powers.get xyz cmm < 0 then + raise Exit; + let fcm = (float_of_int (cxyz-1)) *. 0.5 in let f1 = Array.init nq (fun k -> @@ -371,32 +363,40 @@ let hvrr_two_e_vector (angMom_a, angMom_b, angMom_c, angMom_d) done; Some v3 end + with Exit -> p1 in - if (axyz < 1) || (cxyz < 1) then p2 else - let v = - vrr_v (m+1) am cm (totAngMom_a-1) (totAngMom_c-1) + try + let am = + try Powers.decr xyz angMom_a + with Invalid_argument _ -> raise Exit in - match (p2, v) with - | None, None -> None - | Some p2, None -> Some p2 - | _, Some v -> - begin - let p2 = - match p2 with - | None -> Array.make_matrix np nq 0. - | Some p2 -> p2 - in - for l=0 to np-1 do - let fa = (float_of_int axyz) *. expo_inv_p.(l) *. 0.5 in - let p2_l = p2.(l) - and v_l = v.(l) + if (axyz < 1) || (cxyz < 1) then + raise Exit; + let v = + vrr_v (m+1) am cm (totAngMom_a-1) (totAngMom_c-1) + in + match (p2, v) with + | None, None -> None + | Some p2, None -> Some p2 + | _, Some v -> + begin + let p2 = + match p2 with + | None -> Array.make_matrix np nq 0. + | Some p2 -> p2 in - for k=0 to nq-1 do - p2_l.(k) <- p2_l.(k) -. fa *. expo_inv_q.(k) *. v_l.(k) - done - done; - Some p2 - end + for l=0 to np-1 do + let fa = (float_of_int axyz) *. expo_inv_p.(l) *. 0.5 in + let p2_l = p2.(l) + and v_l = v.(l) + in + for k=0 to nq-1 do + p2_l.(k) <- p2_l.(k) -. fa *. expo_inv_q.(k) *. v_l.(k) + done + done; + Some p2 + end + with Exit -> p2 end in Zmap.add map_2d.(m) key result; result @@ -422,14 +422,9 @@ let hvrr_two_e_vector (angMom_a, angMom_b, angMom_c, angMom_d) end end | 1 -> - let (aax, aay, aaz) = angMom_a in - let ap, xyz = - match angMom_b with - | (_,_,1) -> (aax,aay,aaz+1), Z - | (_,1,_) -> (aax,aay+1,aaz), Y - | (_,_,_) -> (aax+1,aay,aaz), X - in - let f = Coordinate.get xyz center_ab in + let xyz = get_xyz angMom_b in + let ap = Powers.incr xyz angMom_a in + let f = Coordinate.get xyz center_ab in let v1 = match vrr_v 0 ap angMom_c (totAngMom_a+1) totAngMom_c with | Some matrix -> Array.fold_left (fun accu c -> accu +. Array.fold_left (+.) 0. c) 0. matrix @@ -443,20 +438,13 @@ let hvrr_two_e_vector (angMom_a, angMom_b, angMom_c, angMom_d) in v1 +. v2 *. f | _ -> - let (aax, aay, aaz) = angMom_a - and (abx, aby, abz) = angMom_b in - let bxyz, xyz = - match angMom_b with - | (0,0,_) -> abz, Z - | (0,_,_) -> aby, Y - | _ -> abx, X - in + let xyz = get_xyz angMom_b in + let bxyz = Powers.get xyz angMom_b in if (bxyz < 1) then 0. else - let ap, bm = - match xyz with - | X -> (aax+1,aay,aaz),(abx-1,aby,abz) - | Y -> (aax,aay+1,aaz),(abx,aby-1,abz) - | Z -> (aax,aay,aaz+1),(abx,aby,abz-1) + let ap = Powers.incr xyz angMom_a in + let bm = + try Powers.decr xyz angMom_b + with Invalid_argument _ -> raise Exit in let h1 = @@ -482,13 +470,11 @@ let hvrr_two_e_vector (angMom_a, angMom_b, angMom_c, angMom_d) else hrr0_v angMom_a angMom_b angMom_c totAngMom_a totAngMom_b totAngMom_c | (_,_) -> - let (acx, acy, acz) = angMom_c - and (adx, ady, adz) = angMom_d in - let cp, dm, xyz = - match angMom_d with - | (_,0,0) -> (acx+1, acy, acz), (adx-1, ady, adz), X - | (_,_,0) -> (acx, acy+1, acz), (adx, ady-1, adz), Y - | _ -> (acx, acy, acz+1), (adx, ady, adz-1), Z + let xyz = get_xyz angMom_d in + let cp = Powers.incr xyz angMom_c in + let dm = + try Powers.decr xyz angMom_d + with Invalid_argument _ -> raise Exit in let h1 = hrr_v angMom_a angMom_b cp dm totAngMom_a totAngMom_b (totAngMom_c+1) (totAngMom_d-1) diff --git a/Utils/Angular_momentum.ml b/Utils/Angular_momentum.ml index 299619a..5f17a70 100644 --- a/Utils/Angular_momentum.ml +++ b/Utils/Angular_momentum.ml @@ -46,7 +46,7 @@ let of_int = function | 4 -> G | 5 -> H | 6 -> I | 7 -> J | 8 -> K | 9 -> L - | 10 -> M | 11 -> N + | 10 -> M | 11 -> N | 12 -> O | c -> raise (AngularMomentumError (string_of_int c)) diff --git a/Utils/Angular_momentum.mli b/Utils/Angular_momentum.mli deleted file mode 100644 index dd826a5..0000000 --- a/Utils/Angular_momentum.mli +++ /dev/null @@ -1,15 +0,0 @@ -exception AngularMomentumError of string -type t = S | P | D | F | G | H | I | J | K | L | M | N | O -val of_char : char -> t -val to_string : t -> string -val to_char : t -> char -val to_int : t -> int -val of_int : int -> t -type kind = -| Singlet of t -| Doublet of (t*t) -| Triplet of (t*t*t) -| Quartet of (t*t*t*t) - -val n_functions : t -> int -val zkey_array : kind -> Zkey.t array diff --git a/Utils/Powers.ml b/Utils/Powers.ml new file mode 100644 index 0000000..3fd8998 --- /dev/null +++ b/Utils/Powers.ml @@ -0,0 +1,39 @@ +type t = { x: int ; y : int ; z : int ; tot : int } + +let of_int_tuple t = + let result = + match t with + | (x,y,z) -> { x ; y ; z ; tot=x+y+z } + in + assert (result.tot >= 0); + result + +let to_int_tuple { x ; y ; z ; _ } = (x,y,z) + +let get coord t = + match coord with + | Coordinate.X -> t.x + | Coordinate.Y -> t.y + | Coordinate.Z -> t.z + +let incr coord t = + match coord with + | Coordinate.X -> { t with x = t.x+1 ; tot = t.tot+1 } + | Coordinate.Y -> { t with y = t.y+1 ; tot = t.tot+1 } + | Coordinate.Z -> { t with z = t.z+1 ; tot = t.tot+1 } + +let decr coord t = + (* + let test _ = () + *) + let test x = + if x < 1 then + invalid_arg "Angular_momentum.Powers.decr"; + in + match coord with + | Coordinate.X -> (test t.x ; { t with x = t.x-1 ; tot = t.tot-1 }) + | Coordinate.Y -> (test t.y ; { t with y = t.y-1 ; tot = t.tot-1 }) + | Coordinate.Z -> (test t.z ; { t with z = t.z-1 ; tot = t.tot-1 }) + + + diff --git a/Utils/Powers.mli b/Utils/Powers.mli new file mode 100644 index 0000000..da45bde --- /dev/null +++ b/Utils/Powers.mli @@ -0,0 +1,7 @@ +type t = private { x: int ; y : int ; z : int ; tot : int } +val of_int_tuple : int * int * int -> t +val to_int_tuple : t -> int * int * int +val get : Coordinate.axis -> t -> int +val incr : Coordinate.axis -> t -> t +val decr : Coordinate.axis -> t -> t +