10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-06-27 07:32:15 +02:00
quantum_package/ocaml/qp_edit.ml

206 lines
4.6 KiB
OCaml
Raw Normal View History

2014-10-26 12:46:17 +01:00
open Qputils;;
open Qptypes;;
open Core.Std;;
2014-11-03 19:33:06 +01:00
let file_header filename = Printf.sprintf
2014-10-29 00:12:45 +01:00
"
==================================================================
Quantum Package
==================================================================
Editing file `%s`
" filename
2014-10-26 12:46:17 +01:00
type keyword =
| Ao_basis
| Bielec_integrals
2014-10-28 17:16:51 +01:00
| Cisd_sc2
| Determinants
| Electrons
| Full_ci
| Hartree_fock
| Mo_basis
2014-10-29 18:55:31 +01:00
| Nuclei
2014-10-26 12:46:17 +01:00
;;
let keyword_to_string = function
2014-10-28 17:16:51 +01:00
| Ao_basis -> "AO basis"
2014-10-26 12:46:17 +01:00
| Bielec_integrals -> "Two electron integrals"
2014-10-28 17:16:51 +01:00
| Cisd_sc2 -> "CISD (SC)^2"
| Determinants -> "Determinants"
| Electrons -> "Electrons"
| Full_ci -> "Selected Full-CI"
| Hartree_fock -> "Hartree-Fock"
| Mo_basis -> "MO basis"
2014-10-29 18:55:31 +01:00
| Nuclei -> "Molecule"
2014-10-26 12:46:17 +01:00
;;
let make_header kw =
let s = keyword_to_string kw in
let l = String.length s in
2014-10-29 00:12:45 +01:00
"\n\n"^s^"\n"^(String.init l ~f:(fun _ -> '='))^"\n\n"
2014-10-26 12:46:17 +01:00
;;
2014-10-28 17:16:51 +01:00
let get s =
2014-10-29 22:13:03 +01:00
let header = (make_header s)
2014-11-12 17:17:44 +01:00
and rst = let open Input in
match s with
2014-10-28 17:16:51 +01:00
| Full_ci ->
2014-11-12 17:17:44 +01:00
Full_ci.(to_rst (read ()))
2014-10-28 17:16:51 +01:00
| Hartree_fock ->
2014-11-12 17:17:44 +01:00
Hartree_fock.(to_rst (read ()))
2014-10-28 17:16:51 +01:00
| Mo_basis ->
2014-11-12 17:17:44 +01:00
Mo_basis.(to_rst (read ()))
2014-10-29 22:13:03 +01:00
| Electrons ->
2014-11-12 17:17:44 +01:00
Electrons.(to_rst (read ()))
2014-10-29 22:13:03 +01:00
| Determinants ->
2014-11-12 17:17:44 +01:00
Determinants.(to_rst (read ()))
2014-10-29 22:13:03 +01:00
| Cisd_sc2 ->
2014-11-12 17:17:44 +01:00
Cisd_sc2.(to_rst (read ()))
2014-10-29 18:55:31 +01:00
| Nuclei ->
2014-11-12 17:17:44 +01:00
Nuclei.(to_rst (read ()))
2014-10-29 22:13:03 +01:00
| Ao_basis ->
2014-11-12 17:17:44 +01:00
Ao_basis.(to_rst (read ()))
2014-10-29 22:13:03 +01:00
| Bielec_integrals ->
2014-11-12 17:17:44 +01:00
Bielec_integrals.(to_rst (read ()))
2014-10-29 22:13:03 +01:00
in header^(Rst_string.to_string rst)
2014-10-26 12:46:17 +01:00
;;
2014-11-03 19:33:06 +01:00
let set str s =
let header = (make_header s) in
let index_begin = String.substr_index_exn ~pos:0 ~pattern:header str in
let index_begin = index_begin + (String.length header) in
let index_end =
match ( String.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 ~pos:index_begin ~len:l str
|> Rst_string.of_string
in
2014-11-12 17:17:44 +01:00
let write (of_rst,w) =
match of_rst str with
| Some data -> w data
| None -> ()
in
let open Input in
match s with
| Hartree_fock -> write Hartree_fock.(of_rst, write)
| Full_ci -> write Full_ci.(of_rst, write)
| Electrons -> write Electrons.(of_rst, write)
| Cisd_sc2 -> write Cisd_sc2.(of_rst, write)
| Bielec_integrals -> write Bielec_integrals.(of_rst, write)
| Determinants -> write Determinants.(of_rst, write)
| Nuclei -> write Nuclei.(of_rst, write)
| Ao_basis -> () (* TODO *)
| Mo_basis -> () (* TODO *)
2014-11-03 19:33:06 +01:00
;;
2014-10-28 17:16:51 +01:00
let create_temp_file ezfio_filename fields =
2014-11-03 19:33:06 +01:00
let temp_filename = Filename.temp_file "qp_edit_" ".rst" in
Out_channel.with_file temp_filename ~f:(fun out_channel ->
(file_header ezfio_filename) :: (List.map ~f:get fields)
|> String.concat ~sep:"\n"
|> Out_channel.output_string out_channel
);
temp_filename
2014-10-26 12:46:17 +01:00
;;
let run ezfio_filename =
(* Open EZFIO *)
if (not (Sys.file_exists_exn ezfio_filename)) then
failwith (ezfio_filename^" does not exists");
Ezfio.set_file ezfio_filename;
2014-11-03 19:33:06 +01:00
(*
let output = (file_header ezfio_filename) :: (
2014-10-28 17:16:51 +01:00
List.map ~f:get [
Ao_basis ;
Mo_basis ;
])
in
2014-10-26 12:46:17 +01:00
String.concat output
|> print_string
2014-11-03 19:33:06 +01:00
*)
let tasks = [
Nuclei ;
2014-11-04 00:39:10 +01:00
Electrons ;
2014-11-03 19:33:06 +01:00
Bielec_integrals ;
2014-11-12 10:34:54 +01:00
Hartree_fock ;
2014-11-03 22:42:39 +01:00
Cisd_sc2 ;
2014-11-12 10:34:54 +01:00
Full_ci ;
Determinants ;
2014-11-03 19:33:06 +01:00
]
in
(* Create the temp file *)
let temp_filename = create_temp_file ezfio_filename tasks in
(* Open the temp file with external editor *)
let editor =
match Sys.getenv "EDITOR" with
| Some editor -> editor
| None -> "vi"
in
let command = Printf.sprintf "%s %s" editor temp_filename in
Sys.command_exn command;
(* Re-read the temp file *)
let temp_string =
In_channel.with_file temp_filename ~f:(fun in_channel ->
In_channel.input_all in_channel)
in
List.iter ~f:(fun x -> set temp_string x) tasks;
(* Remove temp_file *)
Sys.remove temp_filename;
2014-10-26 12:46:17 +01:00
;;
let spec =
let open Command.Spec in
empty
(*
+> flag "i" (optional string)
~doc:"Prints input data"
+> flag "o" (optional string)
~doc:"Prints output data"
*)
+> anon ("ezfio_file" %: string)
;;
let command =
Command.basic
~summary: "Quantum Package command"
~readme:(fun () ->
"
Edit input data
")
spec
(* (fun i o ezfio_file () -> *)
(*fun ezfio_file () ->
try
run ezfio_file
with
| _ msg -> print_string ("\n\nError\n\n"^msg^"\n\n")
*)
(fun ezfio_file () -> run ezfio_file)
;;
let () =
Command.run command
;;