From ef7f80016ccdbae8d60ad08a613fb299e6f75ef4 Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Sat, 23 Aug 2014 17:07:47 +0200 Subject: [PATCH] Some utilities for input --- ocaml/atom.ml | 47 +++++++++++++++++++++++++++++++++++++++++++ ocaml/point3d.ml | 35 ++++++++++++++++++++++++++++++++ ocaml/test_atom.ml | 5 +++++ ocaml/test_point3d.ml | 13 ++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 ocaml/atom.ml create mode 100644 ocaml/point3d.ml create mode 100644 ocaml/test_atom.ml create mode 100644 ocaml/test_point3d.ml diff --git a/ocaml/atom.ml b/ocaml/atom.ml new file mode 100644 index 00000000..b21b6a7d --- /dev/null +++ b/ocaml/atom.ml @@ -0,0 +1,47 @@ +open Core.Std;; + +exception AtomError of string + +module Charge : sig + type t + val to_float : t -> float + 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 + 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 ; +} + +let of_string 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 (String.concat [x; y; z] ?sep:(Some " ")) + } + | _ -> raise (AtomError s) +;; + +let to_string a = + [ Element.to_string a.element ; + Charge.to_string a.charge ; + Point3d.to_string a.coord ] + |> String.concat ?sep:(Some " ") +;; + diff --git a/ocaml/point3d.ml b/ocaml/point3d.ml new file mode 100644 index 00000000..e51003e2 --- /dev/null +++ b/ocaml/point3d.ml @@ -0,0 +1,35 @@ +open Core.Std;; + +type t = { + x : float ; + y : float ; + z : float ; +} + +let of_string s = + let l = s + |> String.split ~on:' ' + |> List.filter ~f:(fun x -> x <> "") + |> List.map ~f:Float.of_string + |> Array.of_list + in + { x = l.(0) ; + y = l.(1) ; + z = l.(2) } +;; + + +let distance2 p1 p2 = + let { x=x1 ; y=y1 ; z=z1 } = p1 + and { x=x2 ; y=y2 ; z=z2 } = p2 in + (x2-.x1)*.(x2-.x1) +. (y2-.y1)*.(y2-.y1) +. (z2-.z1)*.(z2-.z1) +;; + +let distance p1 p2 = sqrt (distance2 p1 p2) +;; + +let to_string p = + let { x=x ; y=y ; z=z } = p in + Printf.sprintf "%f %f %f" x y z +;; + diff --git a/ocaml/test_atom.ml b/ocaml/test_atom.ml new file mode 100644 index 00000000..8e6c019b --- /dev/null +++ b/ocaml/test_atom.ml @@ -0,0 +1,5 @@ +let test_atom = + let line = "C 6.0 1. 2. 3." in + let atom = Atom.of_string line in + print_string (Atom.to_string atom) +;; diff --git a/ocaml/test_point3d.ml b/ocaml/test_point3d.ml new file mode 100644 index 00000000..86c25c69 --- /dev/null +++ b/ocaml/test_point3d.ml @@ -0,0 +1,13 @@ +let test_point3d_1 () = + let input = "7.4950000 -0.1499810 0.5085570" in + let p3d = Point3d.of_string input in + print_string (Point3d.to_string p3d) +;; + +let test_point3d () = + let p1 = Point3d.of_string "1. 2. 3." + and p2 = Point3d.of_string "-2. 1. 1.5" in + Printf.printf "%f\n" (Point3d.distance p1 p2) +;; + +test_point3d ();