diff --git a/ocaml/Makefile b/ocaml/Makefile index b2783eb9..d7086e39 100644 --- a/ocaml/Makefile +++ b/ocaml/Makefile @@ -1,3 +1,5 @@ +#TODO : Opam auto-installer in makefile + # Check if QPACKAGE_ROOT is defined ifndef QPACKAGE_ROOT diff --git a/ocaml/atom.ml b/ocaml/atom.ml index 1fd9e008..51fc03a1 100644 --- a/ocaml/atom.ml +++ b/ocaml/atom.ml @@ -33,13 +33,13 @@ let of_string s = | [ name; charge; x; y; z ] -> { element = Element.of_string name ; charge = Charge.of_string charge ; - coord = Point3d.of_string (String.concat [x; y; z] ?sep:(Some " ")) + coord = Point3d.of_string (String.concat [x; y; z] ~sep:" ") } | [ name; x; y; z ] -> let e = Element.of_string name in { element = e ; charge = Charge.of_int (Element.charge e); - coord = Point3d.of_string (String.concat [x; y; z] ?sep:(Some " ")) + coord = Point3d.of_string (String.concat [x; y; z] ~sep:" ") } | _ -> raise (AtomError s) ;; diff --git a/ocaml/gto.ml b/ocaml/gto.ml index 38817f97..d56c75ad 100644 --- a/ocaml/gto.ml +++ b/ocaml/gto.ml @@ -91,7 +91,7 @@ let find in_channel element = ;; let read_basis_of_element in_channel element = - find in_channel element ; + ignore (find in_channel element) ; read_basis in_channel ; ;; @@ -100,5 +100,5 @@ let to_string { sym = sym ; lc = lc } = let f (p,c) = Printf.sprintf "( %s, %f )" (Primitive.to_string p) c in Printf.sprintf "[ %s : %s ]" (Symmetry.to_string sym) - (String.concat (List.map ~f:f lc) ?sep:(Some ", ")) + (String.concat (List.map ~f:f lc) ~sep:", ") ;; diff --git a/ocaml/molecule.ml b/ocaml/molecule.ml index b4845f2c..4e6999f1 100644 --- a/ocaml/molecule.ml +++ b/ocaml/molecule.ml @@ -9,7 +9,7 @@ type t = { elec_beta : Positive_int.t ; } -let charge { nuclei ; elec_alpha ; elec_beta } = +let get_charge { nuclei ; elec_alpha ; elec_beta } = let result = Positive_int.(to_int elec_alpha + to_int elec_beta) in let rec nucl_charge = function | a::rest -> Atom.(Charge.to_float a.charge) +. nucl_charge rest @@ -18,12 +18,12 @@ let charge { nuclei ; elec_alpha ; elec_beta } = nucl_charge nuclei -. (Float.of_int result) ;; -let multiplicity m = +let get_multiplicity m = Multiplicity.of_alpha_beta m.elec_alpha m.elec_beta ;; let name m = - let cm = Float.to_int (charge m) in + let cm = Float.to_int (get_charge m) in let c = match cm with | 0 -> "" @@ -32,7 +32,7 @@ let name m = | i when i>1 -> Printf.sprintf " (%d+)" i | i -> Printf.sprintf " (%d-)" (-i) in - let mult = Multiplicity.to_string (multiplicity m) in + let mult = Multiplicity.to_string (get_multiplicity m) in let { nuclei ; elec_alpha ; elec_beta } = m in let rec build_list accu = function | a::rest -> @@ -67,35 +67,47 @@ let to_string m = let n = List.length nuclei in let title = name m in [ Int.to_string n ; title ] @ (List.map ~f:Atom.to_string nuclei) - |> String.concat ?sep:(Some "\n") + |> String.concat ~sep:"\n" ;; -let of_xyz_string s charge' multiplicity' = +let of_xyz_string + ?(charge=0) ?(multiplicity=(Multiplicity.of_int 1)) + s = let l = String.split s ~on:'\n' |> List.filter ~f:(fun x -> x <> "") |> List.map ~f:Atom.of_string in - let ne = ( charge { + let ne = ( get_charge { nuclei=l ; elec_alpha=(Positive_int.of_int 0) ; elec_beta=(Positive_int.of_int 0) } |> Float.to_int - )- charge' + )- charge |> Positive_int.of_int in - let (na,nb) = Multiplicity.to_alpha_beta ne multiplicity' in + let (na,nb) = Multiplicity.to_alpha_beta ne multiplicity in let result = { nuclei = l ; elec_alpha = (Positive_int.of_int na) ; elec_beta = (Positive_int.of_int nb) } in - if ((multiplicity result) <> multiplicity') then + if ((get_multiplicity result) <> multiplicity) then let msg = Printf.sprintf "With %d electrons multiplicity %d is impossible" (Positive_int.to_int ne) - (Multiplicity.to_int multiplicity') + (Multiplicity.to_int multiplicity) in raise (MultiplicityError msg); else () ; result ;; + + +let of_xyz_file + ?(charge=0) ?(multiplicity=(Multiplicity.of_int 1)) + filename = + let (_,buffer) = In_channel.read_all filename + |> String.lsplit2_exn ~on:'\n' in + let (_,buffer) = String.lsplit2_exn buffer ~on:'\n' in + of_xyz_string ~charge:charge ~multiplicity:multiplicity buffer +;; diff --git a/ocaml/test_molecule.ml b/ocaml/test_molecule.ml index d648e9ce..7cd7e838 100644 --- a/ocaml/test_molecule.ml +++ b/ocaml/test_molecule.ml @@ -10,14 +10,18 @@ H 1.0 0.54386314 0.00000000 -0.92559535 " in - try ignore (Molecule.of_xyz_string xyz 1 (Strictly_positive_int.of_int 1)) - with - | Molecule.MultiplicityError _ -> print_string "OK\n" - ; print_string "---\n"; - let m = Molecule.of_xyz_string xyz 0 (Strictly_positive_int.of_int 1) + begin + try ( + ignore (Molecule.of_xyz_string xyz ~multiplicity:(Multiplicity.of_int 2)) ; + print_string "Failed in MultiplicityError\n" ) + with + | Molecule.MultiplicityError _ -> print_string "MultiplicityError OK\n" + end ; + print_string "---\n"; + let m = Molecule.of_xyz_string xyz in print_endline (Molecule.name m) ; - let m = Molecule.of_xyz_string xyz 1 (Strictly_positive_int.of_int 2) + let m = Molecule.of_xyz_string xyz ~charge:1 ~multiplicity:(Multiplicity.of_int 2) in print_endline (Molecule.name m) ; let xyz = @@ -27,9 +31,14 @@ O 1.65102147 0.00000000 -2.35602344 H 0.54386314 0.00000000 -0.92559535 " in - let m = Molecule.of_xyz_string xyz (-2) (Strictly_positive_int.of_int 1) + let m = Molecule.of_xyz_string xyz ~charge:(-2) in print_endline (Molecule.name m) ; + print_endline (Molecule.to_string m); + print_string "---------\n"; + + let m = Molecule.of_xyz_file "c2h6.xyz" in print_string (Molecule.to_string m); + ;; test_molecule ();;