10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-06-21 20:52:18 +02:00

Added Molecule.of_xyz_file

This commit is contained in:
Anthony Scemama 2014-08-26 14:39:23 +02:00
parent 9bffc0883b
commit caafe823be
5 changed files with 45 additions and 22 deletions

View File

@ -1,3 +1,5 @@
#TODO : Opam auto-installer in makefile
# Check if QPACKAGE_ROOT is defined
ifndef QPACKAGE_ROOT

View File

@ -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)
;;

View File

@ -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:", ")
;;

View File

@ -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
;;

View File

@ -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 ();;