9
1
mirror of https://github.com/QuantumPackage/qp2.git synced 2024-11-09 06:53:38 +01:00
qp2/ocaml/Atom.ml

50 lines
1.1 KiB
OCaml
Raw Normal View History

2019-03-13 13:02:29 +01:00
open Sexplib.Std
2019-01-25 11:39:31 +01:00
exception AtomError of string
type t =
{ element : Element.t ;
charge : Charge.t ;
coord : Point3d.t ;
} [@@deriving sexp]
(** Read xyz coordinates of the atom *)
let of_string ~units s =
let buffer = s
2019-03-13 13:02:29 +01:00
|> String_ext.split ~on:' '
|> List.filter (fun x -> x <> "")
2019-01-25 11:39:31 +01:00
in
match buffer with
| [ name; charge; x; y; z ] ->
{ element = Element.of_string name ;
charge = Charge.of_string charge ;
2019-03-13 13:02:29 +01:00
coord = Point3d.of_string ~units (String.concat " " [x; y; z] )
2019-01-25 11:39:31 +01:00
}
| [ name; x; y; z ] ->
let e = Element.of_string name in
2024-04-22 13:45:51 +02:00
begin
try
{ element = e ;
charge = Element.to_charge e;
coord = Point3d.of_string ~units (String.concat " " [x; y; z])
}
with
| err -> (Printf.eprintf "name = \"%s\"\nxyz = (%s,%s,%s)\n%!" name x y z ; raise err)
end
2019-01-25 11:39:31 +01:00
| _ -> raise (AtomError s)
let to_string ~units a =
[ Element.to_string a.element ;
Charge.to_string a.charge ;
Point3d.to_string ~units a.coord ]
2019-03-13 13:02:29 +01:00
|> String.concat " "
2019-01-25 11:39:31 +01:00
let to_xyz a =
Printf.sprintf "%-3s %s"
(Element.to_string a.element)
(Point3d.to_string ~units:Units.Angstrom a.coord)