10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-11-04 21:24:02 +01:00
quantum_package/ocaml/Atom.ml

45 lines
1.0 KiB
OCaml
Raw Normal View History

2017-08-18 18:28:33 +02:00
open Core
2014-08-23 17:07:47 +02:00
exception AtomError of string
type t =
{ element : Element.t ;
charge : Charge.t ;
coord : Point3d.t ;
2017-08-18 18:28:33 +02:00
} [@@deriving sexp]
2014-08-23 17:07:47 +02:00
(** Read xyz coordinates of the atom *)
let of_string ~units s =
2014-08-23 17:07:47 +02:00
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:" ")
2014-08-23 17:07:47 +02:00
}
2014-08-24 20:00:26 +02:00
| [ 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:" ")
2014-08-24 20:00:26 +02:00
}
2014-08-23 17:07:47 +02:00
| _ -> raise (AtomError s)
2014-08-23 17:07:47 +02:00
let to_string ~units a =
2014-08-23 17:07:47 +02:00
[ Element.to_string a.element ;
Charge.to_string a.charge ;
Point3d.to_string ~units a.coord ]
2014-10-29 22:13:03 +01:00
|> String.concat ~sep:" "
let to_xyz a =
Printf.sprintf "%-3s %s"
(Element.to_string a.element)
(Point3d.to_string ~units:Units.Angstrom a.coord)
2014-08-23 17:07:47 +02:00