10
0
mirror of https://github.com/QuantumPackage/qp2.git synced 2024-11-04 21:23:56 +01:00
QuantumPackage/ocaml/Input_ao_basis.ml

512 lines
14 KiB
OCaml
Raw Permalink Normal View History

2019-01-25 11:39:31 +01:00
open Qptypes;;
open Qputils;;
2019-03-13 15:49:57 +01:00
open Sexplib.Std;;
2019-01-25 11:39:31 +01:00
module Ao_basis : sig
type t =
{ ao_basis : AO_basis_name.t;
ao_num : AO_number.t ;
ao_prim_num : AO_prim_number.t array;
ao_prim_num_max : AO_prim_number.t;
ao_nucl : Nucl_number.t array;
2021-05-21 16:42:48 +02:00
ao_power : Angmom.Xyz.t array;
2019-01-25 11:39:31 +01:00
ao_coef : AO_coef.t array;
ao_expo : AO_expo.t array;
ao_cartesian : bool;
2020-05-11 11:17:03 +02:00
ao_normalized : bool;
primitives_normalized : bool;
2019-01-25 11:39:31 +01:00
} [@@deriving sexp]
;;
val read : unit -> t option
val to_string : t -> string
val to_basis : t -> Basis.t
val reorder : t -> t
val ordering : t -> int array
val write : t -> unit
val to_md5 : t -> MD5.t
val to_rst : t -> Rst_string.t
end = struct
type t =
{ ao_basis : AO_basis_name.t;
ao_num : AO_number.t ;
ao_prim_num : AO_prim_number.t array;
ao_prim_num_max : AO_prim_number.t;
ao_nucl : Nucl_number.t array;
2021-05-21 16:42:48 +02:00
ao_power : Angmom.Xyz.t array;
2019-01-25 11:39:31 +01:00
ao_coef : AO_coef.t array;
ao_expo : AO_expo.t array;
ao_cartesian : bool;
2020-05-11 11:17:03 +02:00
ao_normalized : bool;
primitives_normalized : bool;
2019-01-25 11:39:31 +01:00
} [@@deriving sexp]
;;
let get_default = Qpackage.get_ezfio_default "ao_basis";;
let read_ao_basis () =
Ezfio.get_ao_basis_ao_basis ()
|> AO_basis_name.of_string
;;
let read_ao_num () =
Ezfio.get_ao_basis_ao_num ()
|> AO_number.of_int
;;
let read_ao_prim_num () =
Ezfio.get_ao_basis_ao_prim_num ()
|> Ezfio.flattened_ezfio
2019-03-13 15:49:57 +01:00
|> Array.map AO_prim_number.of_int
2019-01-25 11:39:31 +01:00
;;
let read_ao_prim_num_max () =
Ezfio.get_ao_basis_ao_prim_num ()
|> Ezfio.flattened_ezfio
2019-03-13 15:49:57 +01:00
|> Array.fold_left (fun x y -> if x>y then x else y) 0
2019-01-25 11:39:31 +01:00
|> AO_prim_number.of_int
;;
let read_ao_nucl () =
let nmax = Nucl_number.get_max () in
Ezfio.get_ao_basis_ao_nucl ()
|> Ezfio.flattened_ezfio
2019-03-13 15:49:57 +01:00
|> Array.map (fun x-> Nucl_number.of_int ~max:nmax x)
2019-01-25 11:39:31 +01:00
;;
let read_ao_power () =
let x = Ezfio.get_ao_basis_ao_power () in
let dim = x.Ezfio.dim.(0) in
let data = Ezfio.flattened_ezfio x in
2019-03-13 15:49:57 +01:00
let result = Array.init dim (fun x -> "") in
2019-01-25 11:39:31 +01:00
for i=1 to dim
do
if (data.(i-1) > 0) then
2019-03-13 15:49:57 +01:00
result.(i-1) <- result.(i-1)^"x"^(string_of_int data.(i-1));
2019-01-25 11:39:31 +01:00
if (data.(dim+i-1) > 0) then
2019-03-13 15:49:57 +01:00
result.(i-1) <- result.(i-1)^"y"^(string_of_int data.(dim+i-1));
2019-01-25 11:39:31 +01:00
if (data.(2*dim+i-1) > 0) then
2019-03-13 15:49:57 +01:00
result.(i-1) <- result.(i-1)^"z"^(string_of_int data.(2*dim+i-1));
2019-01-25 11:39:31 +01:00
done;
2021-05-21 16:42:48 +02:00
Array.map Angmom.Xyz.of_string result
2019-01-25 11:39:31 +01:00
;;
let read_ao_coef () =
Ezfio.get_ao_basis_ao_coef ()
|> Ezfio.flattened_ezfio
2019-03-13 15:49:57 +01:00
|> Array.map AO_coef.of_float
2019-01-25 11:39:31 +01:00
;;
let read_ao_expo () =
Ezfio.get_ao_basis_ao_expo ()
|> Ezfio.flattened_ezfio
2019-03-13 15:49:57 +01:00
|> Array.map AO_expo.of_float
2019-01-25 11:39:31 +01:00
;;
let read_ao_cartesian () =
if not (Ezfio.has_ao_basis_ao_cartesian ()) then
get_default "ao_cartesian"
2019-03-13 15:49:57 +01:00
|> bool_of_string
2019-01-25 11:39:31 +01:00
|> Ezfio.set_ao_basis_ao_cartesian
;
Ezfio.get_ao_basis_ao_cartesian ()
;;
2020-05-11 11:17:03 +02:00
let read_ao_normalized () =
if not (Ezfio.has_ao_basis_ao_normalized()) then
get_default "ao_normalized"
|> bool_of_string
|> Ezfio.set_ao_basis_ao_normalized
;
Ezfio.get_ao_basis_ao_normalized ()
;;
let read_primitives_normalized () =
if not (Ezfio.has_ao_basis_primitives_normalized()) then
get_default "primitives_normalized"
|> bool_of_string
|> Ezfio.set_ao_basis_primitives_normalized
;
Ezfio.get_ao_basis_primitives_normalized ()
;;
2019-01-25 11:39:31 +01:00
let to_long_basis b =
let ao_num = AO_number.to_int b.ao_num in
let gto_array = Array.init (AO_number.to_int b.ao_num)
2019-03-13 15:49:57 +01:00
(fun i ->
2021-05-21 16:42:48 +02:00
let s = Angmom.Xyz.to_symmetry b.ao_power.(i) in
2019-01-25 11:39:31 +01:00
let ao_prim_num = AO_prim_number.to_int b.ao_prim_num.(i) in
2019-03-13 15:49:57 +01:00
let prims = List.init ao_prim_num (fun j ->
2019-01-25 11:39:31 +01:00
let prim = { GaussianPrimitive.sym = s ;
GaussianPrimitive.expo = b.ao_expo.(ao_num*j+i)
}
in
let coef = b.ao_coef.(ao_num*j+i) in
(prim,coef)
) in
Gto.of_prim_coef_list prims
)
in
let rec do_work accu sym gto nucl =
match (sym, gto, nucl) with
| (s::srest, g::grest, n::nrest) ->
do_work ((s,g,n)::accu) srest grest nrest
| ([],[],[]) -> List.rev accu
| _ -> assert false
in
do_work []
(Array.to_list b.ao_power)
(Array.to_list gto_array)
(Array.to_list b.ao_nucl)
;;
let to_basis b =
to_long_basis b
|> Long_basis.to_basis
;;
let to_md5 b =
let short_basis = to_basis b in
Basis.to_md5 short_basis
;;
let write_md5 b =
to_md5 b
|> MD5.to_string
|> Ezfio.set_ao_basis_ao_md5
;;
let write_ao_basis name =
AO_basis_name.to_string name
|> Ezfio.set_ao_basis_ao_basis
;;
let write b =
let { ao_basis ;
ao_num ;
ao_prim_num ;
ao_prim_num_max ;
ao_nucl ;
ao_power ;
ao_coef ;
ao_expo ;
ao_cartesian ;
2020-05-11 11:17:03 +02:00
ao_normalized ;
primitives_normalized ;
2019-01-25 11:39:31 +01:00
} = b
in
write_md5 b ;
write_ao_basis ao_basis;
let ao_num = AO_number.to_int ao_num
and ao_prim_num_max = AO_prim_number.to_int ao_prim_num_max
in
let ao_prim_num =
Array.to_list ao_prim_num
2020-05-25 11:31:28 +02:00
|> list_map AO_prim_number.to_int
2019-01-25 11:39:31 +01:00
in
Ezfio.set_ao_basis_ao_prim_num (Ezfio.ezfio_array_of_list
~rank:1 ~dim:[| ao_num |] ~data:ao_prim_num) ;
let ao_nucl =
Array.to_list ao_nucl
2020-05-25 11:31:28 +02:00
|> list_map Nucl_number.to_int
2019-01-25 11:39:31 +01:00
in
Ezfio.set_ao_basis_ao_nucl(Ezfio.ezfio_array_of_list
~rank:1 ~dim:[| ao_num |] ~data:ao_nucl) ;
let ao_power =
let l = Array.to_list ao_power in
List.concat [
2021-05-21 16:42:48 +02:00
(list_map (fun a -> Positive_int.to_int a.Angmom.Xyz.x) l) ;
(list_map (fun a -> Positive_int.to_int a.Angmom.Xyz.y) l) ;
(list_map (fun a -> Positive_int.to_int a.Angmom.Xyz.z) l) ]
2019-01-25 11:39:31 +01:00
in
Ezfio.set_ao_basis_ao_power(Ezfio.ezfio_array_of_list
~rank:2 ~dim:[| ao_num ; 3 |] ~data:ao_power) ;
Ezfio.set_ao_basis_ao_cartesian(ao_cartesian);
2020-05-11 11:17:03 +02:00
Ezfio.set_ao_basis_ao_normalized(ao_normalized);
Ezfio.set_ao_basis_primitives_normalized(primitives_normalized);
2019-01-25 11:39:31 +01:00
let ao_coef =
Array.to_list ao_coef
2020-05-25 11:31:28 +02:00
|> list_map AO_coef.to_float
2019-01-25 11:39:31 +01:00
in
Ezfio.set_ao_basis_ao_coef(Ezfio.ezfio_array_of_list
~rank:2 ~dim:[| ao_num ; ao_prim_num_max |] ~data:ao_coef) ;
let ao_expo =
Array.to_list ao_expo
2020-05-25 11:31:28 +02:00
|> list_map AO_expo.to_float
2019-01-25 11:39:31 +01:00
in
Ezfio.set_ao_basis_ao_expo(Ezfio.ezfio_array_of_list
~rank:2 ~dim:[| ao_num ; ao_prim_num_max |] ~data:ao_expo) ;
;;
let read () =
if (Ezfio.has_ao_basis_ao_basis ()) then
begin
let result =
{ ao_basis = read_ao_basis ();
ao_num = read_ao_num () ;
ao_prim_num = read_ao_prim_num ();
ao_prim_num_max = read_ao_prim_num_max ();
ao_nucl = read_ao_nucl ();
ao_power = read_ao_power ();
ao_coef = read_ao_coef () ;
ao_expo = read_ao_expo () ;
ao_cartesian = read_ao_cartesian () ;
2020-05-11 11:17:03 +02:00
ao_normalized = read_ao_normalized () ;
primitives_normalized = read_primitives_normalized () ;
2019-01-25 11:39:31 +01:00
}
in
to_md5 result
|> MD5.to_string
|> Ezfio.set_ao_basis_ao_md5 ;
Some result
end
else
None
;;
let ordering b =
let ordered_basis =
to_basis b
|> Long_basis.of_basis
|> Array.of_list
and unordered_basis =
to_long_basis b
|> Array.of_list
in
let find x a =
let rec find x a i =
if i = Array.length a then
find2 x a 0
else
if a.(i) = Some x then
(a.(i) <- None ; i)
else
find x a (i+1)
and find2 (s,g,n) a i =
if i = Array.length a then -1
else
match a.(i) with
| None -> find2 (s,g,n) a (i+1)
| Some (s', g', n') ->
if s <> s' || n <> n' then find2 (s,g,n) a (i+1)
else
2020-05-25 11:31:28 +02:00
let lc = list_map (fun (prim, _) -> prim) g.Gto.lc
and lc' = list_map (fun (prim, _) -> prim) g'.Gto.lc
2019-01-25 11:39:31 +01:00
in
if lc <> lc' then find2 (s,g,n) a (i+1) else (a.(i) <- None ; i)
in
find x a 0
in
2019-03-13 15:49:57 +01:00
let search_array = Array.map (fun i -> Some i) unordered_basis in
Array.map (fun x -> find x search_array) ordered_basis
2019-01-25 11:39:31 +01:00
;;
let of_long_basis long_basis name ao_cartesian =
let ao_num = List.length long_basis |> AO_number.of_int in
let ao_prim_num =
2020-05-25 11:31:28 +02:00
list_map (fun (_,g,_) -> List.length g.Gto.lc
2019-03-13 15:49:57 +01:00
|> AO_prim_number.of_int ) long_basis
2019-01-25 11:39:31 +01:00
|> Array.of_list
and ao_nucl =
2020-05-25 11:31:28 +02:00
list_map (fun (_,_,n) -> n) long_basis
2019-01-25 11:39:31 +01:00
|> Array.of_list
and ao_power =
2020-05-25 11:31:28 +02:00
list_map (fun (x,_,_) -> x) long_basis
2019-01-25 11:39:31 +01:00
|> Array.of_list
in
2019-03-13 15:49:57 +01:00
let ao_prim_num_max = Array.fold_left (fun s x ->
if AO_prim_number.to_int x > s then AO_prim_number.to_int x else s) 0
2019-01-25 11:39:31 +01:00
ao_prim_num
|> AO_prim_number.of_int
in
let gtos =
2020-05-25 11:31:28 +02:00
list_map (fun (_,x,_) -> x) long_basis
2019-01-25 11:39:31 +01:00
in
let create_expo_coef ec =
let coefs =
begin match ec with
2020-05-25 11:31:28 +02:00
| `Coefs -> list_map (fun x->
list_map (fun (_,coef) -> AO_coef.to_float coef) x.Gto.lc ) gtos
| `Expos -> list_map (fun x->
list_map (fun (prim,_) -> AO_expo.to_float
2019-03-13 15:49:57 +01:00
prim.GaussianPrimitive.expo) x.Gto.lc ) gtos
2019-01-25 11:39:31 +01:00
end
in
let rec get_n n accu = function
| [] -> List.rev accu
| h::tail ->
let y =
2019-03-13 15:49:57 +01:00
try List.nth h n
with _ -> 0.
2019-01-25 11:39:31 +01:00
in
get_n n (y::accu) tail
in
let rec build accu = function
| n when n=(AO_prim_number.to_int ao_prim_num_max) -> accu
| n -> build ( accu @ (get_n n [] coefs) ) (n+1)
in
build [] 0
in
let ao_coef = create_expo_coef `Coefs
|> Array.of_list
2019-03-13 15:49:57 +01:00
|> Array.map AO_coef.of_float
2019-01-25 11:39:31 +01:00
and ao_expo = create_expo_coef `Expos
|> Array.of_list
2019-03-13 15:49:57 +01:00
|> Array.map AO_expo.of_float
2019-01-25 11:39:31 +01:00
in
{ ao_basis = name ;
ao_num ; ao_prim_num ; ao_prim_num_max ; ao_nucl ;
2020-05-11 11:17:03 +02:00
ao_power ; ao_coef ; ao_expo ; ao_cartesian ;
ao_normalized = bool_of_string @@ get_default "ao_normalized";
primitives_normalized = bool_of_string @@ get_default "primitives_normalized";
}
2019-01-25 11:39:31 +01:00
;;
let reorder b =
let order = ordering b in
2019-03-13 15:49:57 +01:00
let f a = Array.init (Array.length a) (fun i -> a.(order.(i))) in
2019-01-25 11:39:31 +01:00
let ao_prim_num_max = AO_prim_number.to_int b.ao_prim_num_max
and ao_num = AO_number.to_int b.ao_num in
let ao_coef =
2019-03-13 15:49:57 +01:00
Array.init ao_prim_num_max (fun i ->
f @@ Array.init ao_num (fun j -> b.ao_coef.(i*ao_num + j) )
2019-01-25 11:39:31 +01:00
) |> Array.to_list |> Array.concat
in
let ao_expo =
2019-03-13 15:49:57 +01:00
Array.init ao_prim_num_max (fun i ->
f @@ Array.init ao_num (fun j -> b.ao_expo.(i*ao_num + j) )
2019-01-25 11:39:31 +01:00
) |> Array.to_list |> Array.concat
in
{ b with
ao_prim_num = f b.ao_prim_num ;
ao_nucl = f b.ao_nucl ;
ao_power = f b.ao_power ;
ao_coef ;
ao_expo ;
}
;;
let to_rst b =
let print_sym =
2019-03-13 15:49:57 +01:00
let l = List.init (Array.length b.ao_power) (
2019-01-25 11:39:31 +01:00
fun i -> ( (i+1),b.ao_nucl.(i),b.ao_power.(i) ) )
in
let rec do_work = function
| [] -> []
| (i,n,x)::tail ->
(Printf.sprintf " %5d %6d %-8s\n" i (Nucl_number.to_int n)
2021-05-21 16:42:48 +02:00
(Angmom.Xyz.to_string x)
2019-01-25 11:39:31 +01:00
)::(do_work tail)
in do_work l
2019-03-13 15:49:57 +01:00
|> String.concat ""
2019-01-25 11:39:31 +01:00
in
let short_basis = to_basis b in
Printf.sprintf "
Name of the AO basis ::
ao_basis = %s
Cartesian coordinates (6d,10f,...) ::
ao_cartesian = %s
2020-05-11 11:17:03 +02:00
Use normalized primitive functions ::
primitives_normalized = %s
Use normalized basis functions ::
ao_normalized = %s
2019-01-25 11:39:31 +01:00
Basis set (read-only) ::
%s
======= ========= ===========
Basis Nucleus Symmetries
======= ========= ===========
%s
======= ========= ===========
" (AO_basis_name.to_string b.ao_basis)
2019-03-13 15:49:57 +01:00
(string_of_bool b.ao_cartesian)
2020-05-11 11:17:03 +02:00
(string_of_bool b.primitives_normalized)
(string_of_bool b.ao_normalized)
2019-01-25 11:39:31 +01:00
(Basis.to_string short_basis
2019-03-13 15:49:57 +01:00
|> String_ext.split ~on:'\n'
2020-05-25 11:31:28 +02:00
|> list_map (fun x-> " "^x)
2019-03-13 15:49:57 +01:00
|> String.concat "\n"
2019-01-25 11:39:31 +01:00
) print_sym
|> Rst_string.of_string
;;
let read_rst s =
let s = Rst_string.to_string s
2019-03-13 15:49:57 +01:00
|> String_ext.split ~on:'\n'
2019-01-25 11:39:31 +01:00
in
let rec extract_basis = function
| [] -> failwith "Error in basis set"
| line :: tail ->
2019-03-13 15:49:57 +01:00
let line = String.trim line in
2019-01-25 11:39:31 +01:00
if line = "Basis set (read-only) ::" then
2019-03-13 15:49:57 +01:00
String.concat "\n" tail
2019-01-25 11:39:31 +01:00
else
extract_basis tail
in
extract_basis s
;;
let to_string b =
Printf.sprintf "
2020-05-11 11:17:03 +02:00
ao_basis = %s
ao_num = %s
ao_prim_num = %s
ao_prim_num_max = %s
ao_nucl = %s
ao_power = %s
ao_coef = %s
ao_expo = %s
ao_cartesian = %s
ao_normalized = %s
primitives_normalized = %s
md5 = %s
2019-01-25 11:39:31 +01:00
"
(AO_basis_name.to_string b.ao_basis)
(AO_number.to_string b.ao_num)
2020-05-25 11:31:28 +02:00
(b.ao_prim_num |> Array.to_list |> list_map
2019-03-13 15:49:57 +01:00
(AO_prim_number.to_string) |> String.concat ", " )
2019-01-25 11:39:31 +01:00
(AO_prim_number.to_string b.ao_prim_num_max)
2020-05-25 11:31:28 +02:00
(b.ao_nucl |> Array.to_list |> list_map Nucl_number.to_string |>
2019-03-13 15:49:57 +01:00
String.concat ", ")
2020-05-25 11:31:28 +02:00
(b.ao_power |> Array.to_list |> list_map (fun x->
2021-05-21 16:42:48 +02:00
"("^(Angmom.Xyz.to_string x)^")" )|> String.concat ", ")
2020-05-25 11:31:28 +02:00
(b.ao_coef |> Array.to_list |> list_map AO_coef.to_string
2019-03-13 15:49:57 +01:00
|> String.concat ", ")
2020-05-25 11:31:28 +02:00
(b.ao_expo |> Array.to_list |> list_map AO_expo.to_string
2019-03-13 15:49:57 +01:00
|> String.concat ", ")
(b.ao_cartesian |> string_of_bool)
2020-05-11 11:17:03 +02:00
(b.ao_normalized |> string_of_bool)
(b.primitives_normalized |> string_of_bool)
2019-01-25 11:39:31 +01:00
(to_md5 b |> MD5.to_string )
;;
end