10
0
mirror of https://github.com/LCPQ/quantum_package synced 2025-01-10 04:58:25 +01:00

Using streams in determinants

This commit is contained in:
Anthony Scemama 2016-02-19 23:46:16 +01:00
parent e4043cda0d
commit 32208d35f9

View File

@ -340,7 +340,7 @@ psi_det = %s
let idx = String.substr_index_exn r ~pos:0 ~pattern:"\nDeterminants" let idx = String.substr_index_exn r ~pos:0 ~pattern:"\nDeterminants"
in in
let (header, dets) = let (header, dets) =
(String.prefix r idx, String.suffix r ((String.length r)-idx) ) (String.prefix r idx, String.suffix r ((String.length r)-idx-1) )
in in
(* Handle header *) (* Handle header *)
@ -360,41 +360,100 @@ psi_det = %s
|> String.concat |> String.concat
in in
(* Handle determinant coefs *) (* Handle determinants and coefs *)
let dets = match ( dets let dets_stream =
|> String.split ~on:'\n'
|> List.map ~f:(String.strip) let ipos, jmax =
) with ref (-1), String.length dets
| _::lines -> lines in
| _ -> failwith "Error in determinants" let next_line =
Stream.from (fun _ ->
let rec loop line =
let j =
!ipos + 1
in
ipos := j;
if (j < jmax) then
match dets.[j] with
| '\n' -> Some (String.of_char_list @@ List.rev line )
| ' ' -> loop line
| c -> loop (c :: line)
else
None
in loop []
)
in
ignore @@ Stream.next next_line; (* Determinants :: *)
ignore @@ Stream.next next_line; (* *)
Stream.from (fun _ ->
try
begin
let result =
let coefs =
let line =
Stream.next next_line
in
String.split ~on:'\t' line
|> Array.of_list
|> Array.map ~f:(fun x -> Det_coef.of_float @@ Float.of_string x)
in
Some (coefs,
Stream.next next_line |> String.rev,
Stream.next next_line |> String.rev )
in
ignore @@ Stream.next next_line;
result
end
with
| Stream.Failure -> None
)
in in
let psi_coef =
let rec read_coefs accu = function
| [] -> List.rev accu let psi_coef, psi_det =
| "" :: "" :: tail -> read_coefs accu tail
| "" :: c :: tail -> let alpha =
let c = Ezfio.get_electrons_elec_alpha_num ()
String.split ~on:'\t' c |> Elec_alpha_number.of_int
|> List.map ~f:(fun x -> Det_coef.of_float (Float.of_string x)) and beta =
|> Array.of_list Ezfio.get_electrons_elec_beta_num ()
|> Elec_beta_number.of_int
and n_int =
N_int_number.get_max ()
|> N_int_number.of_int
in in
read_coefs (c :: accu) tail
| _ :: tail -> read_coefs accu tail let rec read coefs dets_bit = function
| None -> (List.rev coefs), (List.rev dets_bit)
| Some (c, alpha_str, beta_str) ->
begin
ignore @@ Stream.next dets_stream;
let new_coefs =
c :: coefs
and new_dets =
let newdet =
(Bitlist.of_string_mp alpha_str, Bitlist.of_string_mp beta_str)
|> Determinant.of_bitlist_couple ~n_int ~alpha ~beta
in in
let a = newdet :: dets_bit
let buffer = in
read_coefs [] dets read new_coefs new_dets (Stream.peek dets_stream)
end
in
let coefs, dets_bit =
read [] [] (Stream.peek dets_stream)
in in
let nstates = let nstates =
List.hd_exn buffer List.hd_exn coefs |> Array.length
|> Array.length
in in
let a =
let extract_state i = let extract_state i =
let i = let i =
i-1 i-1
in in
List.map ~f:(fun x -> x.(i)) buffer List.map ~f:(fun x -> x.(i)) coefs
in in
let rec build_result accu = function let rec build_result accu = function
| 0 -> accu | 0 -> accu
@ -406,48 +465,14 @@ psi_det = %s
in in
build_result [] nstates build_result [] nstates
in in
List.concat a let new_coefs =
|> Array.of_list List.concat a |> Array.of_list
and new_dets =
Array.of_list dets_bit
in
new_coefs, new_dets
in in
(*
let dets = match ( dets
|> String.split ~on:'\n'
|> List.map ~f:(String.strip)
) with
| _::lines -> lines
| _ -> failwith "Error in determinants"
in
*)
(* Handle determinants *)
let psi_det =
let alpha = Ezfio.get_electrons_elec_alpha_num ()
|> Elec_alpha_number.of_int
and beta = Ezfio.get_electrons_elec_beta_num ()
|> Elec_beta_number.of_int
and n_int =
N_int_number.get_max ()
|> N_int_number.of_int
in
let rec read_dets accu = function
| [] -> List.rev accu
| ""::_::alpha_str::beta_str::tail ->
begin
let newdet =
(Bitlist.of_string_mp alpha_str, Bitlist.of_string_mp beta_str)
|> Determinant.of_bitlist_couple ~n_int ~alpha ~beta
in
read_dets (newdet::accu) tail
end
| _::tail -> read_dets accu tail
in
let dets =
List.map ~f:String.rev dets
in
read_dets [] dets
|> Array.of_list
in
let bitkind = let bitkind =
Printf.sprintf "(bit_kind %d)" (Lazy.force Qpackage.bit_kind Printf.sprintf "(bit_kind %d)" (Lazy.force Qpackage.bit_kind