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

91 lines
2.2 KiB
OCaml
Raw Normal View History

2014-08-13 17:54:32 +02:00
open Core.Std;;
open Qptypes;;
exception GTO_Read_Failure of string
exception End_Of_Basis
2014-08-13 17:54:32 +02:00
type t =
{ sym : Symmetry.t ;
2014-09-18 17:01:43 +02:00
lc : ((Primitive.t * AO_coef.t) list)
2014-10-25 21:24:21 +02:00
} with sexp
2014-08-13 17:54:32 +02:00
;;
let of_prim_coef_list pc =
let (p,c) = List.hd_exn pc in
let sym = p.Primitive.sym in
let rec check = function
| [] -> `OK
| (p,c)::tl ->
if p.Primitive.sym <> sym then
`Failed
else
check tl
in
match check pc with
| `Failed -> raise (Failure "Failed in of_prim_coef_list")
| `OK ->
{ sym = sym ;
lc = pc
}
;;
let read_one in_channel =
(* Fetch number of lines to read on first line *)
let buffer = input_line in_channel in
if ( (String.strip buffer) = "" ) then
raise End_Of_Basis;
2014-08-13 17:54:32 +02:00
let sym_str = String.sub buffer 0 2 in
let n_str = String.sub buffer 2 ((String.length buffer)-2) in
let sym = Symmetry.of_string (String.strip sym_str) in
let n = Int.of_string (String.strip n_str) in
(* Read all the primitives *)
let rec read_lines result = function
| 0 -> result
| i ->
begin
let line_buffer = input_line in_channel in
let buffer = line_buffer
|> String.split ~on:' '
|> List.filter ~f:(fun x -> x <> "")
in
match buffer with
| [ j ; expo ; coef ] ->
begin
let coef = String.tr ~target:'D' ~replacement:'e' coef
in
2014-10-27 01:14:18 +01:00
let p =
Primitive.of_sym_expo sym
(AO_expo.of_float (Float.of_string expo) )
2014-09-18 17:01:43 +02:00
and c = AO_coef.of_float (Float.of_string coef) in
2014-08-13 17:54:32 +02:00
read_lines ( (p,c)::result) (i-1)
end
| _ -> raise (GTO_Read_Failure line_buffer)
end
in read_lines [] n
2014-10-25 21:24:21 +02:00
|> List.rev
2014-08-13 17:54:32 +02:00
|> of_prim_coef_list
;;
(** Transform the gto to a string *)
2014-10-26 12:46:17 +01:00
let to_string { sym = sym ; lc = lc } =
let result =
Printf.sprintf "%s %3d" (Symmetry.to_string sym) (List.length lc)
in
let rec do_work accu i = function
| [] -> List.rev accu
| (p,c)::tail ->
let p = AO_expo.to_float p.Primitive.expo
and c = AO_coef.to_float c
in
let result =
Printf.sprintf "%3d %16f %16f" i p c
in
do_work (result::accu) (i+1) tail
in
(do_work [result] 1 lc)
|> String.concat ~sep:"\n"
;;