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