10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-06-26 23:22:18 +02:00
quantum_package/ocaml/Atom.ml

57 lines
1.3 KiB
OCaml
Raw Normal View History

2014-08-23 17:07:47 +02:00
open Core.Std;;
exception AtomError of string
module Charge : sig
type t
val to_float : t -> float
2014-08-27 16:38:13 +02:00
val to_int : t -> int
2014-08-23 17:07:47 +02:00
val to_string: t -> string
val of_float : float -> t
val of_int : int -> t
val of_string: string -> t
end = struct
type t = float
let to_float x = x
2014-08-27 16:38:13 +02:00
let to_int x = Float.to_int x
2014-08-23 17:07:47 +02:00
let to_string x = Float.to_string (to_float x)
let of_float x = x
let of_int i = Float.of_int i
let of_string s = Float.of_string s
end
type t =
{ element : Element.t ;
charge : Charge.t ;
coord : Point3d.t ;
}
2014-09-18 17:01:43 +02:00
(** Read xyz coordinates of the atom with unit u *)
let of_string u 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 ;
2014-09-18 17:01:43 +02:00
coord = Point3d.of_string u (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 = Charge.of_int (Element.charge e);
2014-09-18 17:01:43 +02:00
coord = Point3d.of_string u (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)
;;
let to_string a =
[ Element.to_string a.element ;
Charge.to_string a.charge ;
Point3d.to_string a.coord ]
2014-08-24 20:00:26 +02:00
|> String.concat ?sep:(Some " ")
2014-08-23 17:07:47 +02:00
;;