Implemented #322
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Anthony Scemama 2024-03-16 15:21:40 +01:00
parent fdd6392d57
commit a29c67a738
1 changed files with 86 additions and 54 deletions

View File

@ -8,14 +8,14 @@ open Sexplib.Std
(** Interactive editing of the input. (** Interactive editing of the input.
WARNING WARNING
This file is automatically generated by This file is automatically generated by
`${{QP_ROOT}}/scripts/ezfio_interface/ei_handler.py` `${{QP_ROOT}}/scripts/ezfio_interface/ei_handler.py`
*) *)
(** Keywords used to define input sections *) (** Keywords used to define input sections *)
type keyword = type keyword =
| Ao_basis | Ao_basis
| Determinants_by_hand | Determinants_by_hand
| Electrons | Electrons
@ -37,7 +37,7 @@ let keyword_to_string = function
(** Create the header of the temporary file *) (** Create the header of the temporary file *)
let file_header filename = let file_header filename =
Printf.sprintf " Printf.sprintf "
================================================================== ==================================================================
Quantum Package Quantum Package
@ -47,7 +47,7 @@ Editing file `%s`
" filename " filename
(** Creates the header of a section *) (** Creates the header of a section *)
let make_header kw = let make_header kw =
@ -58,14 +58,14 @@ let make_header kw =
(** Returns the rst string of section [s] *) (** Returns the rst string of section [s] *)
let get s = let get s =
let header = (make_header s) in let header = (make_header s) in
let f (read,to_rst) = let f (read,to_rst) =
match read () with match read () with
| Some text -> header ^ (Rst_string.to_string (to_rst text)) | Some text -> header ^ (Rst_string.to_string (to_rst text))
| None -> "" | None -> ""
in in
let rst = let rst =
try try
begin begin
let open Input in let open Input in
@ -84,27 +84,27 @@ let get s =
end end
with with
| Sys_error msg -> (Printf.eprintf "Info: %s\n%!" msg ; "") | Sys_error msg -> (Printf.eprintf "Info: %s\n%!" msg ; "")
in in
rst rst
(** Applies the changes from the string [str] corresponding to section [s] *) (** Applies the changes from the string [str] corresponding to section [s] *)
let set str s = let set str s =
let header = (make_header s) in let header = (make_header s) in
match String_ext.substr_index ~pos:0 ~pattern:header str with match String_ext.substr_index ~pos:0 ~pattern:header str with
| None -> () | None -> ()
| Some idx -> | Some idx ->
begin begin
let index_begin = idx + (String.length header) in let index_begin = idx + (String.length header) in
let index_end = let index_end =
match ( String_ext.substr_index ~pos:(index_begin+(String.length header)+1) match ( String_ext.substr_index ~pos:(index_begin+(String.length header)+1)
~pattern:"==" str) with ~pattern:"==" str) with
| Some i -> i | Some i -> i
| None -> String.length str | None -> String.length str
in in
let l = index_end - index_begin in let l = index_end - index_begin in
let str = String.sub str index_begin l let str = String.sub str index_begin l
|> Rst_string.of_string |> Rst_string.of_string
in in
let write (of_rst,w) s = let write (of_rst,w) s =
@ -129,28 +129,36 @@ let set str s =
(** Creates the temporary file for interactive editing *) (** Creates the temporary file for interactive editing *)
let create_temp_file ezfio_filename fields = let create_temp_file ?filename ezfio_filename fields =
let temp_filename = Filename.temp_file "qp_edit_" ".rst" in let temp_filename =
match filename with
| None -> Filename.temp_file "qp_edit_" ".rst"
| Some f -> f
in
let () =
match filename with
| None -> at_exit (fun () -> Sys.remove temp_filename)
| _ -> ()
in
begin begin
let oc = open_out temp_filename in let oc = open_out temp_filename in
(file_header ezfio_filename) :: (List.map get fields) (file_header ezfio_filename) :: (List.map get fields)
|> String.concat "\n" |> String.concat "\n"
|> Printf.fprintf oc "%s"; |> Printf.fprintf oc "%s";
close_out oc; close_out oc;
at_exit (fun () -> Sys.remove temp_filename);
temp_filename temp_filename
end end
let run check_only ?ndet ?state ezfio_filename =
let run check_only ?ndet ?state ?read ?write ezfio_filename =
(* Set check_only if the arguments are not empty *) (* Set check_only if the arguments are not empty *)
let check_only = let check_only =
match ndet, state with match ndet, state, read with
| None, None -> check_only | None, None, None -> check_only
| _ -> true | _ -> true
in in
@ -163,7 +171,7 @@ let run check_only ?ndet ?state ezfio_filename =
(* Clean qp_stop status *) (* Clean qp_stop status *)
[ "qpstop" ; "qpkill" ] [ "qpstop" ; "qpkill" ]
|> List.iter (fun f -> |> List.iter (fun f ->
let stopfile = let stopfile =
Filename.concat (Qpackage.ezfio_work ezfio_filename) f Filename.concat (Qpackage.ezfio_work ezfio_filename) f
in in
if Sys.file_exists stopfile then if Sys.file_exists stopfile then
@ -173,7 +181,7 @@ let run check_only ?ndet ?state ezfio_filename =
(* Reorder basis set *) (* Reorder basis set *)
begin begin
match Input.Ao_basis.read() with match Input.Ao_basis.read() with
| Some aos -> | Some aos ->
let ordering = Input.Ao_basis.ordering aos in let ordering = Input.Ao_basis.ordering aos in
let test = Array.copy ordering in let test = Array.copy ordering in
Array.sort compare test ; Array.sort compare test ;
@ -184,7 +192,7 @@ let run check_only ?ndet ?state ezfio_filename =
Input.Ao_basis.write new_aos; Input.Ao_basis.write new_aos;
match Input.Mo_basis.read() with match Input.Mo_basis.read() with
| None -> () | None -> ()
| Some mos -> | Some mos ->
let new_mos = Input.Mo_basis.reorder mos ordering in let new_mos = Input.Mo_basis.reorder mos ordering in
Input.Mo_basis.write new_mos Input.Mo_basis.write new_mos
end end
@ -200,7 +208,7 @@ let run check_only ?ndet ?state ezfio_filename =
begin begin
match state with match state with
| None -> () | None -> ()
| Some range -> | Some range ->
begin begin
Input.Determinants_by_hand.extract_states range Input.Determinants_by_hand.extract_states range
end end
@ -210,14 +218,14 @@ let run check_only ?ndet ?state ezfio_filename =
(* (*
let output = (file_header ezfio_filename) :: ( let output = (file_header ezfio_filename) :: (
List.map get [ List.map get [
Ao_basis ; Ao_basis ;
Mo_basis ; Mo_basis ;
]) ])
in in
String.concat output String.concat output
|> print_string |> print_string
*) *)
let tasks = [ let tasks = [
Nuclei_by_hand ; Nuclei_by_hand ;
Ao_basis; Ao_basis;
@ -230,33 +238,41 @@ let run check_only ?ndet ?state ezfio_filename =
(* Create the temp file *) (* Create the temp file *)
let temp_filename = let temp_filename =
create_temp_file ezfio_filename tasks match read, write with
| None, None -> create_temp_file ezfio_filename tasks
| Some filename, None -> filename
| None, filename -> create_temp_file ?filename ezfio_filename tasks
| x, y -> failwith "read and write options are incompatible"
in in
(* Open the temp file with external editor *)
let editor = let () =
try Sys.getenv "EDITOR" match check_only with
with Not_found -> "vi" | true -> ()
| false ->
begin
(* Open the temp file with external editor *)
let editor =
try Sys.getenv "EDITOR"
with Not_found -> "vi"
in
Printf.sprintf "%s %s" editor temp_filename
|> Sys.command |> ignore
end
in in
match check_only with if write = None then
| true -> () (* Re-read the temp file *)
| false -> let temp_string =
Printf.sprintf "%s %s" editor temp_filename let ic = open_in temp_filename in
|> Sys.command |> ignore let result =
; input_lines ic
|> String.concat "\n"
(* Re-read the temp file *) in
let temp_string = close_in ic;
let ic = open_in temp_filename in result
let result =
input_lines ic
|> String.concat "\n"
in in
close_in ic; List.iter (fun x -> set temp_string x) tasks
result
in
List.iter (fun x -> set temp_string x) tasks
@ -264,7 +280,7 @@ let run check_only ?ndet ?state ezfio_filename =
(** Remove the backup file *) (** Remove the backup file *)
let remove_backup ezfio_filename = let remove_backup ezfio_filename =
let backup_filename = let backup_filename =
Printf.sprintf "%s/work/backup.tar" ezfio_filename Printf.sprintf "%s/work/backup.tar" ezfio_filename
in in
try Sys.remove backup_filename try Sys.remove backup_filename
@ -273,7 +289,7 @@ let remove_backup ezfio_filename =
(** Create a backup file in case of an exception *) (** Create a backup file in case of an exception *)
let create_backup ezfio_filename = let create_backup ezfio_filename =
remove_backup ezfio_filename; remove_backup ezfio_filename;
let backup_filename = let backup_filename =
Printf.sprintf "%s/work/backup.tar" ezfio_filename Printf.sprintf "%s/work/backup.tar" ezfio_filename
in in
try try
@ -289,7 +305,7 @@ let create_backup ezfio_filename =
(** Restore the backup file when an exception occuprs *) (** Restore the backup file when an exception occuprs *)
let restore_backup ezfio_filename = let restore_backup ezfio_filename =
let filename = let filename =
Printf.sprintf "%s/work/backup.tar" ezfio_filename Printf.sprintf "%s/work/backup.tar" ezfio_filename
in in
if Sys.file_exists filename then if Sys.file_exists filename then
@ -312,6 +328,16 @@ let () =
doc="Checks the input data"; doc="Checks the input data";
arg=Without_arg; }}; arg=Without_arg; }};
{{
short='w'; long="write"; opt=Optional;
doc="Writes the qp_edit file to a file\"";
arg=With_arg "<string>"; }};
{{
short='r'; long="read"; opt=Optional;
doc="Reads the file and applies it to the EZFIO\"";
arg=With_arg "<string>"; }};
{{ short='n'; long="ndet"; opt=Optional; {{ short='n'; long="ndet"; opt=Optional;
doc="Truncates the wavefunction to the target number of determinants"; doc="Truncates the wavefunction to the target number of determinants";
arg=With_arg "<int>"; }}; arg=With_arg "<int>"; }};
@ -328,6 +354,12 @@ let () =
end; end;
(* Handle options *) (* Handle options *)
let write =
Command_line.get "write"
in
let read =
Command_line.get "read"
in
let ndet = let ndet =
match Command_line.get "ndet" with match Command_line.get "ndet" with
| None -> None | None -> None
@ -353,7 +385,7 @@ let () =
(* Run the program *) (* Run the program *)
try try
if (not c) then create_backup ezfio_filename; if (not c) then create_backup ezfio_filename;
run c ?ndet ?state ezfio_filename run c ?ndet ?state ?read ?write ezfio_filename
with with
| Failure exc | Failure exc
| Invalid_argument exc -> | Invalid_argument exc ->