9
1
mirror of https://github.com/QuantumPackage/qp2.git synced 2024-11-09 06:53:38 +01:00
qp2/ocaml/Input_nuclei_by_hand.ml

242 lines
6.2 KiB
OCaml
Raw Normal View History

2019-01-25 11:39:31 +01:00
open Qptypes;;
open Qputils;;
2019-03-13 15:49:57 +01:00
open Sexplib.Std;;
2019-01-25 11:39:31 +01:00
module Nuclei_by_hand : sig
type t =
{ nucl_num : Nucl_number.t ;
nucl_label : Element.t array;
nucl_charge : Charge.t array;
nucl_coord : Point3d.t array;
} [@@deriving sexp]
;;
val read : unit -> t option
val write : t -> unit
val to_string : t -> string
val to_atom_list : t -> Atom.t list
val to_rst : t -> Rst_string.t
val of_rst : Rst_string.t -> t option
end = struct
type t =
{ nucl_num : Nucl_number.t ;
nucl_label : Element.t array;
nucl_charge : Charge.t array;
nucl_coord : Point3d.t array;
} [@@deriving sexp]
;;
let get_default = Qpackage.get_ezfio_default "nuclei";;
let read_nucl_num () =
let nmax = Nucl_number.get_max () in
Nucl_number.of_int ~max:nmax nmax
;;
let write_nucl_num n =
Nucl_number.to_int n
|> Ezfio.set_nuclei_nucl_num
;;
let read_nucl_label () =
Ezfio.get_nuclei_nucl_label ()
|> Ezfio.flattened_ezfio
2019-03-13 15:49:57 +01:00
|> Array.map Element.of_string
2019-01-25 11:39:31 +01:00
;;
let write_nucl_label ~nucl_num labels =
let nucl_num =
Nucl_number.to_int nucl_num
in
let labels =
Array.to_list labels
2020-05-25 11:31:28 +02:00
|> list_map Element.to_string
2019-01-25 11:39:31 +01:00
in
Ezfio.ezfio_array_of_list ~rank:1
~dim:[| nucl_num |] ~data:labels
|> Ezfio.set_nuclei_nucl_label
;;
let read_nucl_charge () =
Ezfio.get_nuclei_nucl_charge ()
|> Ezfio.flattened_ezfio
2019-03-13 15:49:57 +01:00
|> Array.map Charge.of_float
2019-01-25 11:39:31 +01:00
;;
let write_nucl_charge ~nucl_num charges =
let nucl_num =
Nucl_number.to_int nucl_num
in
let charges =
Array.to_list charges
2020-05-25 11:31:28 +02:00
|> list_map Charge.to_float
2019-01-25 11:39:31 +01:00
in
Ezfio.ezfio_array_of_list ~rank:1
~dim:[| nucl_num |] ~data:charges
|> Ezfio.set_nuclei_nucl_charge
;;
let read_nucl_coord () =
let nucl_num = Nucl_number.to_int (read_nucl_num ()) in
let raw_data =
Ezfio.get_nuclei_nucl_coord()
|> Ezfio.flattened_ezfio
in
2023-02-08 13:12:29 +01:00
let zero = Point3d.of_string ~units:Units.Bohr "0. 0. 0." in
2019-03-13 15:49:57 +01:00
let result = Array.make nucl_num zero in
2019-01-25 11:39:31 +01:00
for i=0 to (nucl_num-1)
do
result.(i) <- Point3d.({ x=raw_data.(i);
y=raw_data.(nucl_num+i);
z=raw_data.(2*nucl_num+i); });
done;
result
;;
let write_nucl_coord ~nucl_num coord =
let nucl_num =
Nucl_number.to_int nucl_num
in
let coord = Array.to_list coord in
let coord =
2020-05-25 11:31:28 +02:00
(list_map (fun x-> x.Point3d.x) coord) @
(list_map (fun x-> x.Point3d.y) coord) @
(list_map (fun x-> x.Point3d.z) coord)
2019-01-25 11:39:31 +01:00
in
Ezfio.ezfio_array_of_list ~rank:2
~dim:[| nucl_num ; 3 |] ~data:coord
|> Ezfio.set_nuclei_nucl_coord
;;
let read () =
if (Ezfio.has_nuclei_nucl_num ()) then
Some
{ nucl_num = read_nucl_num ();
nucl_label = read_nucl_label () ;
nucl_charge = read_nucl_charge ();
nucl_coord = read_nucl_coord ();
}
else
None
;;
let write { nucl_num ;
nucl_label ;
nucl_charge ;
nucl_coord ;
} =
write_nucl_num nucl_num ;
write_nucl_label ~nucl_num:nucl_num nucl_label;
write_nucl_charge ~nucl_num:nucl_num nucl_charge;
write_nucl_coord ~nucl_num:nucl_num nucl_coord;
;;
let to_atom_list b =
let rec loop accu (coord, charge, label) = function
| -1 -> accu
| i ->
let atom =
{ Atom.element = label.(i) ;
Atom.charge = charge.(i) ;
Atom.coord = coord.(i) ;
}
in
loop (atom::accu) (coord, charge, label) (i-1)
in
loop [] (b.nucl_coord, b.nucl_charge, b.nucl_label)
( (Nucl_number.to_int b.nucl_num) - 1)
;;
let to_string b =
Printf.sprintf "
nucl_num = %s
nucl_label = %s
nucl_charge = %s
nucl_coord = %s
"
(Nucl_number.to_string b.nucl_num)
2020-05-25 11:31:28 +02:00
(b.nucl_label |> Array.to_list |> list_map
2019-03-13 15:49:57 +01:00
(Element.to_string) |> String.concat ", " )
2020-05-25 11:31:28 +02:00
(b.nucl_charge |> Array.to_list |> list_map
2019-03-13 15:49:57 +01:00
(Charge.to_string) |> String.concat ", " )
2020-05-25 11:31:28 +02:00
(b.nucl_coord |> Array.to_list |> list_map
2019-03-13 15:49:57 +01:00
(Point3d.to_string ~units:Units.Bohr) |> String.concat "\n" )
2019-01-25 11:39:31 +01:00
;;
let to_rst b =
let nucl_num = Nucl_number.to_int b.nucl_num in
let text =
( Printf.sprintf " %d\n "
nucl_num
) :: (
2019-03-13 15:49:57 +01:00
List.init nucl_num (fun i->
2019-10-24 13:55:38 +02:00
Printf.sprintf " %-3s %3d %s"
2019-01-25 11:39:31 +01:00
(b.nucl_label.(i) |> Element.to_string)
(b.nucl_charge.(i) |> Charge.to_int )
(b.nucl_coord.(i) |> Point3d.to_string ~units:Units.Angstrom) )
2019-03-13 15:49:57 +01:00
) |> String.concat "\n"
2019-01-25 11:39:31 +01:00
in
Printf.sprintf "
Nuclear coordinates in xyz format (Angstroms) ::
%s
" text
|> Rst_string.of_string
;;
let of_rst s =
let l = Rst_string.to_string s
2019-03-13 15:49:57 +01:00
|> String_ext.split ~on:'\n'
2019-01-25 11:39:31 +01:00
in
(* Find lines containing the xyz data *)
let rec extract_begin = function
2019-03-13 15:49:57 +01:00
| [] -> raise Not_found
2019-01-25 11:39:31 +01:00
| line::tail ->
2019-03-13 15:49:57 +01:00
let line = String.trim line in
2019-01-25 11:39:31 +01:00
if (String.length line > 3) &&
2019-03-13 15:49:57 +01:00
(String.sub line ((String.length line)-2) 2 = "::") then
2019-01-25 11:39:31 +01:00
tail
else
extract_begin tail
in
(* Create a list of Atom.t *)
let nmax = Nucl_number.get_max () in
let atom_list =
match (extract_begin l) with
| _ :: nucl_num :: title :: lines ->
begin
let nucl_num = nucl_num
2019-03-13 15:49:57 +01:00
|> String.trim
|> int_of_string
2019-01-25 11:39:31 +01:00
|> Nucl_number.of_int ~max:nmax
and lines = Array.of_list lines
in
2019-03-13 15:49:57 +01:00
List.init (Nucl_number.to_int nucl_num) (fun i ->
2023-02-08 13:12:29 +01:00
Atom.of_string ~units:Units.Angstrom lines.(i))
2019-01-25 11:39:31 +01:00
end
| _ -> failwith "Error in xyz format"
in
(* Create the Nuclei.t data structure *)
let result =
{ nucl_num = List.length atom_list
|> Nucl_number.of_int ~max:nmax;
2020-05-25 11:31:28 +02:00
nucl_label = list_map (fun x ->
2019-03-13 15:49:57 +01:00
x.Atom.element) atom_list |> Array.of_list ;
2020-05-25 11:31:28 +02:00
nucl_charge = list_map (fun x ->
2019-03-13 15:49:57 +01:00
x.Atom.charge ) atom_list |> Array.of_list ;
2020-05-25 11:31:28 +02:00
nucl_coord = list_map (fun x ->
2019-03-13 15:49:57 +01:00
x.Atom.coord ) atom_list |> Array.of_list ;
2019-01-25 11:39:31 +01:00
}
in Some result
;;
end