10
0
mirror of https://github.com/QuantumPackage/qp2.git synced 2024-11-14 01:53:49 +01:00
QuantumPackage/ocaml/Atom.ml
2019-01-25 11:39:31 +01:00

45 lines
1.0 KiB
OCaml

open Core
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
|> String.split ~on:' '
|> List.filter ~f:(fun x -> x <> "")
in
match buffer with
| [ name; charge; x; y; z ] ->
{ element = Element.of_string name ;
charge = Charge.of_string charge ;
coord = Point3d.of_string ~units (String.concat [x; y; z] ~sep:" ")
}
| [ name; x; y; z ] ->
let e = Element.of_string name in
{ element = e ;
charge = Element.to_charge e;
coord = Point3d.of_string ~units (String.concat [x; y; z] ~sep:" ")
}
| _ -> raise (AtomError s)
let to_string ~units a =
[ Element.to_string a.element ;
Charge.to_string a.charge ;
Point3d.to_string ~units a.coord ]
|> String.concat ~sep:" "
let to_xyz a =
Printf.sprintf "%-3s %s"
(Element.to_string a.element)
(Point3d.to_string ~units:Units.Angstrom a.coord)