10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-06-29 16:34:50 +02:00

Corrected bug in qp_edit determinants

This commit is contained in:
Anthony Scemama 2016-03-04 11:58:22 +01:00
parent 0e4b6c0b53
commit 5dd55ffab9

View File

@ -16,6 +16,7 @@ module Determinants_by_hand : sig
val to_string : t -> string val to_string : t -> string
val to_rst : t -> Rst_string.t val to_rst : t -> Rst_string.t
val of_rst : Rst_string.t -> t option val of_rst : Rst_string.t -> t option
val read_n_int : unit -> N_int_number.t
end = struct end = struct
type t = type t =
{ n_int : N_int_number.t; { n_int : N_int_number.t;
@ -157,58 +158,44 @@ end = struct
let read_psi_det () = let read_psi_det () =
let n_int = let n_int = read_n_int ()
read_n_int () and n_alpha = Ezfio.get_electrons_elec_alpha_num ()
and alpha =
Ezfio.get_electrons_elec_alpha_num ()
|> Elec_alpha_number.of_int |> Elec_alpha_number.of_int
and beta = and n_beta = Ezfio.get_electrons_elec_beta_num ()
Ezfio.get_electrons_elec_beta_num ()
|> Elec_beta_number.of_int |> Elec_beta_number.of_int
in in
if not (Ezfio.has_determinants_psi_det ()) then if not (Ezfio.has_determinants_psi_det ()) then
begin begin
let mo_tot_num = let mo_tot_num = MO_number.get_max () in
MO_number.get_max () let rec build_data accu = function
in | 0 -> accu
let rec build_data accu = function | n -> build_data ((MO_number.of_int ~max:mo_tot_num n)::accu) (n-1)
| 0 -> accu in
| n -> build_data ((MO_number.of_int ~max:mo_tot_num n)::accu) (n-1) let det_a = build_data [] (Elec_alpha_number.to_int n_alpha)
in |> Bitlist.of_mo_number_list n_int
let det_a = and det_b = build_data [] (Elec_beta_number.to_int n_beta)
build_data [] (Elec_alpha_number.to_int alpha) |> Bitlist.of_mo_number_list n_int
|> Bitlist.of_mo_number_list n_int in
and det_b = let data = ( (Bitlist.to_int64_list det_a) @
build_data [] (Elec_beta_number.to_int beta) (Bitlist.to_int64_list det_b) )
|> Bitlist.of_mo_number_list n_int in
in Ezfio.ezfio_array_of_list ~rank:3 ~dim:[| N_int_number.to_int n_int ; 2 ; 1 |] ~data:data
let data = |> Ezfio.set_determinants_psi_det ;
( (Bitlist.to_int64_list det_a) @ end ;
(Bitlist.to_int64_list det_b) ) let n_int = N_int_number.to_int n_int in
in let psi_det_array = Ezfio.get_determinants_psi_det () in
Ezfio.ezfio_array_of_list ~rank:3 ~dim:[| N_int_number.to_int n_int ; 2 ; 1 |] ~data:data let dim = psi_det_array.Ezfio.dim
|> Ezfio.set_determinants_psi_det ; and data = Ezfio.flattened_ezfio psi_det_array
end ; in
let n_int_i = assert (n_int = dim.(0));
N_int_number.to_int n_int in assert (dim.(1) = 2);
let psi_det_array = assert (dim.(2) = (Det_number.to_int (read_n_det ())));
Ezfio.get_determinants_psi_det () List.init dim.(2) ~f:(fun i ->
in Array.sub ~pos:(2*n_int*i) ~len:(2*n_int) data)
let dim = |> List.map ~f:(Determinant.of_int64_array
psi_det_array.Ezfio.dim ~n_int:(N_int_number.of_int n_int)
and data = ~alpha:n_alpha ~beta:n_beta )
Ezfio.flattened_ezfio psi_det_array |> Array.of_list
in
assert (n_int_i = dim.(0));
assert (dim.(1) = 2);
assert (dim.(2) = (Det_number.to_int (read_n_det ())));
let len =
2 * n_int_i
in
Array.init dim.(2) ~f:(fun i ->
Array.sub ~pos:(len * i) ~len data
|> Determinant.of_int64_array ~n_int ~alpha ~beta
)
;; ;;
let write_psi_det ~n_int ~n_det d = let write_psi_det ~n_int ~n_det d =
@ -286,9 +273,13 @@ end = struct
|> String.concat_array ~sep:"\t" |> String.concat_array ~sep:"\t"
in in
Array.init ndet ~f:(fun i -> Array.init ndet ~f:(fun i ->
String.concat [ " " ; Printf.sprintf " %s\n%s\n"
(coefs_string i) ; "\n" ; (coefs_string i)
(Determinant.to_string ~mo_tot_num b.psi_det.(i)) ; "\n" ] (Determinant.to_string ~mo_tot_num:mo_tot_num b.psi_det.(i)
|> String.split ~on:'\n'
|> List.map ~f:(fun x -> " "^x)
|> String.concat ~sep:"\n"
)
) )
|> String.concat_array ~sep:"\n" |> String.concat_array ~sep:"\n"
in in
@ -334,18 +325,18 @@ psi_det = %s
;; ;;
let of_rst r = let of_rst r =
let dets = Rst_string.to_string r let r = Rst_string.to_string r
in in
(* Split into header and determinants data *) (* Split into header and determinants data *)
let idx = String.substr_index_exn dets ~pos:0 ~pattern:"\nDeterminants" let idx = String.substr_index_exn r ~pos:0 ~pattern:"\nDeterminants"
in in
let header = let (header, dets) =
String.prefix dets idx (String.prefix r idx, String.suffix r ((String.length r)-idx) )
in in
(* Handle header *) (* Handle header *)
let header = header let header = r
|> String.split ~on:'\n' |> String.split ~on:'\n'
|> List.filter ~f:(fun line -> |> List.filter ~f:(fun line ->
if (line = "") then if (line = "") then
@ -361,117 +352,91 @@ psi_det = %s
|> String.concat |> String.concat
in in
(* Handle determinants and coefs *) (* Handle determinant coefs *)
let dets_stream = let dets = match ( dets
|> String.split ~on:'\n'
let ipos, jmax = |> List.map ~f:(String.strip)
ref idx, String.length dets ) with
in | _::lines -> lines
let next_line = | _ -> failwith "Error in determinants"
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, psi_det =
let alpha = let psi_coef =
Ezfio.get_electrons_elec_alpha_num () let rec read_coefs accu = function
|> Elec_alpha_number.of_int | [] -> List.rev accu
and beta = | ""::""::tail -> read_coefs accu tail
Ezfio.get_electrons_elec_beta_num () | ""::c::tail ->
|> Elec_beta_number.of_int let c =
and n_int = String.split ~on:'\t' c
N_int_number.get_max () |> List.map ~f:(fun x -> Det_coef.of_float (Float.of_string x))
|> N_int_number.of_int |> Array.of_list
in in
read_coefs (c::accu) tail
let rec read coefs dets_bit = function | _::tail -> read_coefs accu tail
| 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
newdet :: dets_bit
in
read new_coefs new_dets (Stream.peek dets_stream)
end
in
let coefs, dets_bit =
read [] [] (Stream.peek dets_stream)
in
let nstates =
List.hd_exn coefs |> Array.length
in in
let a = let a =
let extract_state i = let buffer =
let i = read_coefs [] dets
i-1 in
let nstates =
List.hd_exn buffer
|> Array.length
in
let extract_state i =
let i =
i-1
in
List.map ~f:(fun x -> Det_coef.to_string x.(i)) buffer
|> String.concat ~sep:" "
in
let rec build_result = function
| 1 -> extract_state 1
| i -> (build_result (i-1))^" "^(extract_state i)
in
build_result nstates
in
"(psi_coef ("^a^"))"
in
(* Handle determinants *)
let psi_det =
let n_alpha = Ezfio.get_electrons_elec_alpha_num ()
|> Elec_alpha_number.of_int
and n_beta = Ezfio.get_electrons_elec_beta_num ()
|> Elec_beta_number.of_int
in
let rec read_dets accu = function
| [] -> List.rev accu
| ""::_::alpha::beta::tail ->
begin
let newdet =
(Bitlist.of_string ~zero:'-' ~one:'+' alpha ,
Bitlist.of_string ~zero:'-' ~one:'+' beta)
|> Determinant.of_bitlist_couple ~alpha:n_alpha ~beta:n_beta
|> Determinant.sexp_of_t
|> Sexplib.Sexp.to_string
in in
List.map ~f:(fun x -> x.(i)) coefs read_dets (newdet::accu) tail
in end
let rec build_result accu = function | _::tail -> read_dets accu tail
| 0 -> accu
| i ->
let new_accu =
(extract_state i) :: accu
in
build_result new_accu (i-1)
in
build_result [] nstates
in in
let new_coefs = let dets =
List.concat a |> Array.of_list List.map ~f:String.rev dets
and new_dets =
Array.of_list dets_bit
in in
new_coefs, new_dets let sze =
List.fold ~init:0 ~f:(fun accu x -> accu + (String.length x)) dets
in
let control =
Gc.get ()
in
Gc.tune ~minor_heap_size:(sze) ~space_overhead:(sze/10)
~max_overhead:100000 ~major_heap_increment:(sze/10) ();
let a =
read_dets [] dets
|> String.concat
in
Gc.set control;
"(psi_det ("^a^"))"
in in
@ -481,16 +446,11 @@ psi_det = %s
and n_int = and n_int =
Printf.sprintf "(n_int %d)" (N_int_number.get_max ()) Printf.sprintf "(n_int %d)" (N_int_number.get_max ())
in in
let s = let s =
[ header ; bitkind ; n_int ; "(psi_coef ())" ; "(psi_det ())"] String.concat [ header ; bitkind ; n_int ; psi_coef ; psi_det]
|> String.concat
in in
let result =
Generic_input_of_rst.evaluate_sexp t_of_sexp s Generic_input_of_rst.evaluate_sexp t_of_sexp s
in
match result with
| Some x -> Some { x with psi_coef ; psi_det }
| None -> None
;; ;;
end end