mirror of
https://github.com/LCPQ/quantum_package
synced 2025-05-05 22:54:59 +02:00
Some utilities for input
This commit is contained in:
parent
0cdae777d5
commit
ef7f80016c
47
ocaml/atom.ml
Normal file
47
ocaml/atom.ml
Normal file
@ -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 " ")
|
||||
;;
|
||||
|
35
ocaml/point3d.ml
Normal file
35
ocaml/point3d.ml
Normal file
@ -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
|
||||
;;
|
||||
|
5
ocaml/test_atom.ml
Normal file
5
ocaml/test_atom.ml
Normal file
@ -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)
|
||||
;;
|
13
ocaml/test_point3d.ml
Normal file
13
ocaml/test_point3d.ml
Normal file
@ -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 ();
|
Loading…
x
Reference in New Issue
Block a user