From 23695b4edb8aba0a8611ce7f74aeec187892a8ea Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Wed, 29 Oct 2014 16:56:16 +0100 Subject: [PATCH] pretty printing of MOs --- Makefile | 4 +- ocaml/Input_mo_basis.ml | 107 +++++++++++++++++++++++++++++++------ ocaml/MO_label.ml | 29 ++++++++++ ocaml/MO_label.mli | 14 +++++ ocaml/qptypes_generator.ml | 3 ++ ocaml/test_determinants.ml | 12 +++++ ocaml/test_input.ml | 4 +- ocaml/test_mo_label.ml | 5 ++ 8 files changed, 160 insertions(+), 18 deletions(-) create mode 100644 ocaml/MO_label.ml create mode 100644 ocaml/MO_label.mli create mode 100644 ocaml/test_determinants.ml create mode 100644 ocaml/test_mo_label.ml diff --git a/Makefile b/Makefile index cdbe67c5..1a13cfc6 100644 --- a/Makefile +++ b/Makefile @@ -53,7 +53,9 @@ bin/m4: QPACKAGE_ROOT=$$PWD ./scripts/install_m4.sh | tee install_m4.log -ocaml: ocaml/Qptypes.ml curl m4 +ocaml: curl m4 + rm ocaml/Qptypes.ml + $(MAKE) ocaml/Qptypes.ml ocaml/Qptypes.ml: $(info $(BLUE)===== Installing ocaml =====$(BLACK)) diff --git a/ocaml/Input_mo_basis.ml b/ocaml/Input_mo_basis.ml index a5ae3a79..94024aba 100644 --- a/ocaml/Input_mo_basis.ml +++ b/ocaml/Input_mo_basis.ml @@ -5,19 +5,20 @@ open Core.Std;; module Mo_basis : sig type t = { mo_tot_num : MO_number.t ; - mo_label : Non_empty_string.t; - mo_occ : Positive_float.t array; - mo_coef : MO_coef.t array; + mo_label : MO_label.t; + mo_occ : MO_occ.t array; + mo_coef : (MO_coef.t array) array; } with sexp ;; val read : unit -> t val to_string : t -> string + val debug : t -> string end = struct type t = { mo_tot_num : MO_number.t ; - mo_label : Non_empty_string.t; - mo_occ : Positive_float.t array; - mo_coef : MO_coef.t array; + mo_label : MO_label.t; + mo_occ : MO_occ.t array; + mo_coef : (MO_coef.t array) array; } with sexp ;; @@ -25,10 +26,10 @@ end = struct let read_mo_label () = if not (Ezfio.has_mo_basis_mo_label ()) then - Ezfio.set_mo_basis_mo_label "Unknown" + Ezfio.set_mo_basis_mo_label "None" ; Ezfio.get_mo_basis_mo_label () - |> Non_empty_string.of_string + |> MO_label.of_string ;; let read_mo_tot_num () = @@ -52,13 +53,19 @@ end = struct end; (Ezfio.get_mo_basis_mo_occ () ).Ezfio.data |> Ezfio.flattened_ezfio_data - |> Array.map ~f:Positive_float.of_float + |> Array.map ~f:MO_occ.of_float ;; let read_mo_coef () = - (Ezfio.get_mo_basis_mo_coef () ).Ezfio.data + let a = (Ezfio.get_mo_basis_mo_coef () ).Ezfio.data |> Ezfio.flattened_ezfio_data |> Array.map ~f:MO_coef.of_float + in + let mo_tot_num = read_mo_tot_num () |> MO_number.to_int in + let ao_num = (Array.length a)/mo_tot_num in + Array.init mo_tot_num ~f:(fun j -> + Array.sub ~pos:(j*ao_num) ~len:(ao_num) a + ) ;; let read () = @@ -69,6 +76,70 @@ end = struct } ;; + let mo_coef_to_string mo_coef = + let ao_num = Array.length mo_coef.(0) + and mo_tot_num = Array.length mo_coef in + let rec print_five imin imax = + match (imax-imin+1) with + | 1 -> + let header = [ Printf.sprintf " #%15d" (imin+1) ; ] in + let new_lines = + List.init ao_num ~f:(fun i -> + Printf.sprintf " %3d %15.10f " (i+1) + (MO_coef.to_float mo_coef.(imin ).(i)) ) + in header @ new_lines + | 2 -> + let header = [ Printf.sprintf " #%15d %15d" (imin+1) (imin+2) ; ] in + let new_lines = + List.init ao_num ~f:(fun i -> + Printf.sprintf " %3d %15.10f %15.10f" (i+1) + (MO_coef.to_float mo_coef.(imin ).(i)) + (MO_coef.to_float mo_coef.(imin+1).(i)) ) + in header @ new_lines + | 3 -> + let header = [ Printf.sprintf " #%15d %15d %15d" + (imin+1) (imin+2) (imin+3); ] in + let new_lines = + List.init ao_num ~f:(fun i -> + Printf.sprintf " %3d %15.10f %15.10f %15.10f" (i+1) + (MO_coef.to_float mo_coef.(imin ).(i)) + (MO_coef.to_float mo_coef.(imin+1).(i)) + (MO_coef.to_float mo_coef.(imin+2).(i)) ) + in header @ new_lines + | 4 -> + let header = [ Printf.sprintf " #%15d %15d %15d %15d" + (imin+1) (imin+2) (imin+3) (imin+4) ; ] in + let new_lines = + List.init ao_num ~f:(fun i -> + Printf.sprintf " %3d %15.10f %15.10f %15.10f %15.10f" (i+1) + (MO_coef.to_float mo_coef.(imin ).(i)) + (MO_coef.to_float mo_coef.(imin+1).(i)) + (MO_coef.to_float mo_coef.(imin+2).(i)) + (MO_coef.to_float mo_coef.(imin+3).(i)) ) + in header @ new_lines + | 5 -> + let header = [ Printf.sprintf " #%15d %15d %15d %15d %15d" + (imin+1) (imin+2) (imin+3) (imin+4) (imin+5) ; ] in + let new_lines = + List.init ao_num ~f:(fun i -> + Printf.sprintf " %3d %15.10f %15.10f %15.10f %15.10f %15.10f" (i+1) + (MO_coef.to_float mo_coef.(imin ).(i)) + (MO_coef.to_float mo_coef.(imin+1).(i)) + (MO_coef.to_float mo_coef.(imin+2).(i)) + (MO_coef.to_float mo_coef.(imin+3).(i)) + (MO_coef.to_float mo_coef.(imin+4).(i)) ) + in header @ new_lines + | _ -> assert false + in + let rec create_list accu i = + if (i+5 < mo_tot_num) then + create_list ( (print_five i (i+4) |> String.concat ~sep:"\n")::accu ) (i+5) + else + (print_five i (mo_tot_num-1) |> String.concat ~sep:"\n")::accu |> List.rev + in + create_list [] 0 |> String.concat ~sep:"\n\n" + ;; + let to_string b = Printf.sprintf " Label of the molecular orbitals :: @@ -79,9 +150,13 @@ Total number of MOs :: mo_tot_num = %s +MO coefficients :: + +%s " - (Non_empty_string.to_string b.mo_label) + (MO_label.to_string b.mo_label) (MO_number.to_string b.mo_tot_num) + (mo_coef_to_string b.mo_coef) ;; @@ -92,12 +167,14 @@ mo_tot_num = \"%s\" mo_occ = %s mo_coef = %s " - (Non_empty_string.to_string b.mo_label) + (MO_label.to_string b.mo_label) (MO_number.to_string b.mo_tot_num) (b.mo_occ |> Array.to_list |> List.map - ~f:(Positive_float.to_string) |> String.concat ~sep:", " ) - (b.mo_coef |> Array.to_list |> List.map - ~f:(MO_coef.to_string) |> String.concat ~sep:", " ) + ~f:(MO_occ.to_string) |> String.concat ~sep:", " ) + (b.mo_coef |> Array.map + ~f:(fun x-> Array.map ~f:MO_coef.to_string x |> String.concat_array + ~sep:"," ) |> + String.concat_array ~sep:"\n" ) ;; end diff --git a/ocaml/MO_label.ml b/ocaml/MO_label.ml new file mode 100644 index 00000000..0b8b81dd --- /dev/null +++ b/ocaml/MO_label.ml @@ -0,0 +1,29 @@ +open Core.Std;; + +type t = +| Guess +| Canonical +| Natural +| Localized +| None +with sexp +;; + +let to_string = function + | Guess -> "Guess" + | Canonical -> "Canonical" + | Natural -> "Natural" + | Localized -> "Localized" + | None -> "None" +;; + +let of_string s = + match String.lowercase s with + | "guess" -> Guess + | "canonical" -> Canonical + | "natural" -> Natural + | "localized" -> Localized + | "none" -> None + | _ -> failwith "MO_label should be one of: + Guess | Canonical | Natural | Localized | None." +;; diff --git a/ocaml/MO_label.mli b/ocaml/MO_label.mli new file mode 100644 index 00000000..a0702049 --- /dev/null +++ b/ocaml/MO_label.mli @@ -0,0 +1,14 @@ +type t = + | Guess + | Canonical + | Natural + | Localized + | None +with sexp + +(** String representation *) +val to_string : t -> string + +(** Build from string representation *) +val of_string : string -> t + diff --git a/ocaml/qptypes_generator.ml b/ocaml/qptypes_generator.ml index 219d5329..ced370cb 100644 --- a/ocaml/qptypes_generator.ml +++ b/ocaml/qptypes_generator.ml @@ -102,6 +102,9 @@ let input_data = " * MO_coef : float +* MO_occ : float + assert (x >= 0.); + * AO_coef : float * AO_expo : float diff --git a/ocaml/test_determinants.ml b/ocaml/test_determinants.ml new file mode 100644 index 00000000..71c29ed9 --- /dev/null +++ b/ocaml/test_determinants.ml @@ -0,0 +1,12 @@ +open Qptypes;; + +let test_module () = + let mo_tot_num = MO_number.of_int 10 in + let det = + [| 15L ; 7L |] + |> Determinant.of_int64_array (N_int_number.of_int 1) + in + Printf.printf "%s\n" (Determinant.to_string (~mo_tot_num:mo_tot_num) det) +;; + +test_module ();; diff --git a/ocaml/test_input.ml b/ocaml/test_input.ml index 17a2c82c..a5655fcf 100644 --- a/ocaml/test_input.ml +++ b/ocaml/test_input.ml @@ -75,7 +75,7 @@ let test_mo () = Ezfio.set_file "F2.ezfio" ; let b = Input.Mo_basis.read () in - print_endline (Input.Mo_basis.to_string b); + print_endline (Input.Mo_basis.debug b); ;; let test_nucl () = @@ -96,5 +96,5 @@ test_hf ();; test_mo ();; test_nucl (); *) -test_dets();; +test_mo();; diff --git a/ocaml/test_mo_label.ml b/ocaml/test_mo_label.ml new file mode 100644 index 00000000..714ca6e3 --- /dev/null +++ b/ocaml/test_mo_label.ml @@ -0,0 +1,5 @@ +let () = + let m = MO_label.of_string "canonical" in + let s = MO_label.to_string m in + print_string s +;;