From 72eb1ce5c38f1a204243320c9b01ed5fdd6b319d Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Thu, 18 Jan 2018 14:56:08 +0100 Subject: [PATCH] Added Angstrom --- Nuclei/Xyz_parser.mly | 8 +-- Utils/Coordinate.ml | 110 +++++++++++++++++++++++++++++------------- Utils/Coordinate.mli | 4 +- run_integrals.ml | 9 +++- 4 files changed, 89 insertions(+), 42 deletions(-) diff --git a/Nuclei/Xyz_parser.mly b/Nuclei/Xyz_parser.mly index 4785570..4d5c4c7 100644 --- a/Nuclei/Xyz_parser.mly +++ b/Nuclei/Xyz_parser.mly @@ -49,10 +49,10 @@ atoms_xyz: atoms_list: | { [] } - | atoms_list WORD SPACE FLOAT SPACE FLOAT SPACE FLOAT EOL { (Element.of_string $2, Coordinate.of_3_floats $4 $6 $8 ) :: $1 } - | atoms_list WORD SPACE FLOAT SPACE FLOAT SPACE FLOAT SPACE EOL { (Element.of_string $2, Coordinate.of_3_floats $4 $6 $8 ) :: $1 } - | atoms_list INTEGER SPACE FLOAT SPACE FLOAT SPACE FLOAT EOL { (Element.of_int $2, Coordinate.of_3_floats $4 $6 $8 ) :: $1 } - | atoms_list INTEGER SPACE FLOAT SPACE FLOAT SPACE FLOAT SPACE EOL { (Element.of_int $2, Coordinate.of_3_floats $4 $6 $8 ) :: $1 } + | atoms_list WORD SPACE FLOAT SPACE FLOAT SPACE FLOAT EOL { (Element.of_string $2, Coordinate.of_3_floats $4 $6 $8 `Angstrom ) :: $1 } + | atoms_list WORD SPACE FLOAT SPACE FLOAT SPACE FLOAT SPACE EOL { (Element.of_string $2, Coordinate.of_3_floats $4 $6 $8 `Angstrom ) :: $1 } + | atoms_list INTEGER SPACE FLOAT SPACE FLOAT SPACE FLOAT EOL { (Element.of_int $2, Coordinate.of_3_floats $4 $6 $8 `Angstrom ) :: $1 } + | atoms_list INTEGER SPACE FLOAT SPACE FLOAT SPACE FLOAT SPACE EOL { (Element.of_int $2, Coordinate.of_3_floats $4 $6 $8 `Angstrom ) :: $1 } diff --git a/Utils/Coordinate.ml b/Utils/Coordinate.ml index 82bd610..9c0dbe6 100644 --- a/Utils/Coordinate.ml +++ b/Utils/Coordinate.ml @@ -1,55 +1,97 @@ -type t = float array +type t = + | Bohr of float array + | Angstrom of float array -let zero = - [| 0. ; 0. ; 0. |] +(** Bohr radius, taken from https://physics.nist.gov/cgi-bin/cuu/Value?bohrrada0 *) +let a0 = + 0.529_177_210_67 -let of_float_triplet (x,y,z) = - [|x;y;z|] +let zero = Bohr [| 0. ; 0. ; 0. |] + +let of_float_triplet (x,y,z) = function +| `Bohr -> Bohr [|x;y;z|] +| `Angstrom -> Angstrom [|x;y;z|] let of_3_floats x y z = - [|x;y;z|] + of_float_triplet (x,y,z) -let to_string x = - (string_of_float x.(0))^" "^(string_of_float x.(1))^" "^(string_of_float x.(2)) +let to_string y = + let result x = + (string_of_float x.(0))^" "^(string_of_float x.(1))^" "^(string_of_float x.(2)) + in + match y with + | Bohr x -> (result x) ^ " Bohr" + | Angstrom x -> (result x) ^ " Angstrom" + + +let to_float_array = function + | Bohr a + | Angstrom a -> a + +let x a = (to_float_array a).(0) +let y a = (to_float_array a).(1) +let z a = (to_float_array a).(2) -let x a = a.(0) -let y a = a.(1) -let z a = a.(2) let coord a = function -| 0 -> a.(0) -| 1 -> a.(1) -| 2 -> a.(2) -| _ -> raise (Invalid_argument "Coordinate") + | 0 -> (to_float_array a).(0) + | 1 -> (to_float_array a).(1) + | 2 -> (to_float_array a).(2) + | _ -> raise (Invalid_argument "Coordinate") -let to_float_array a = a - (** Linear algebra *) -let (|-) a b = - match a,b with - | [|x;y;z|], [|x';y';z'|] -> [| x-.x'; y-.y'; z-.z' |] - | _ -> assert false - - -let (|+) a b = - match a,b with - | [|x;y;z|], [|x';y';z'|] -> [| x+.x'; y+.y'; z+.z' |] - | _ -> assert false - - let (|.) s a = match a with - | [|x;y;z|] -> [| s*.x; s*.y; s*.z |] + | Bohr [|x;y;z|] -> Bohr [| s*.x; s*.y; s*.z |] + | Angstrom [|x;y;z|] -> Angstrom [| s*.x; s*.y; s*.z |] | _ -> assert false +let to_Angstrom = function + | Angstrom a -> Angstrom a + | Bohr a -> Angstrom (a0 |. Bohr a |> to_float_array) -let dot a b = - match a,b with - | [|x;y;z|], [|x';y';z'|] -> x*.x' +. y*.y' +. z*.z' - | _ -> assert false +let to_Bohr = function + | Angstrom a -> Bohr (1./.a0 |. Angstrom a |> to_float_array) + | Bohr a -> Bohr a + +let (|-), (|+) = + let rec op f p q = + match (p, q) with + | (Angstrom a, Angstrom b) -> Angstrom (f a b) + | (Bohr a, Bohr b) -> Bohr (f a b) + | (Angstrom a, Bohr b) -> op f (to_Bohr p) q + | (Bohr a, Angstrom b) -> op f p (to_Bohr q) + in + (op (fun a b -> + match a,b with + | [|x;y;z|], [|x';y';z'|] -> [| x-.x'; y-.y'; z-.z' |] + | _ -> assert false + ) , + op (fun a b -> + match a,b with + | [|x;y;z|], [|x';y';z'|] -> [| x+.x'; y+.y'; z+.z' |] + | _ -> assert false + ) + ) + + +let dot = + let rec op f p q = + match (p, q) with + | (Angstrom a, Angstrom b) + | (Bohr a, Bohr b) -> f a b + | (Angstrom a, Bohr b) -> op f (to_Bohr p) q + | (Bohr a, Angstrom b) -> op f p (to_Bohr q) + in + op (fun a b -> + match a,b with + | [|x;y;z|], [|x';y';z'|] -> x*.x' +. y*.y' +. z*.z' + | _ -> assert false + ) let norm u = sqrt @@ dot u u + diff --git a/Utils/Coordinate.mli b/Utils/Coordinate.mli index e6bd4bb..91a5985 100644 --- a/Utils/Coordinate.mli +++ b/Utils/Coordinate.mli @@ -1,7 +1,7 @@ type t val zero : t -val of_float_triplet : float * float * float -> t -val of_3_floats : float -> float -> float -> t +val of_float_triplet : float * float * float -> [< `Angstrom | `Bohr ] -> t +val of_3_floats : float -> float -> float -> [< `Angstrom | `Bohr ] -> t val to_string : t -> string val x : t -> float val y : t -> float diff --git a/run_integrals.ml b/run_integrals.ml index 7328191..d0ee6e2 100644 --- a/run_integrals.ml +++ b/run_integrals.ml @@ -32,6 +32,11 @@ let run ~coord ~basis = let () = - let usage_msg = "Quantum Chemistry Package. Options available:" in + let usage_msg = "Available options:" in Arg.parse speclist (fun _ -> ()) usage_msg; - run ~coord:!coord_file ~basis:!basis_file + try + run ~coord:!coord_file ~basis:!basis_file + with + | Invalid_argument e -> + (print_string "Error: " ; print_endline e; print_newline () ; Arg.usage speclist usage_msg) + ;