diff --git a/ocaml/Input_full_ci.ml b/ocaml/Input_full_ci.ml deleted file mode 100644 index f963ee7e..00000000 --- a/ocaml/Input_full_ci.ml +++ /dev/null @@ -1,153 +0,0 @@ -open Qptypes;; -open Qputils;; -open Core.Std;; - -module Full_ci : sig - type t = - { n_det_max_fci : Det_number_max.t; - pt2_max : PT2_energy.t; - do_pt2_end : bool; - var_pt2_ratio : Normalized_float.t; - } with sexp - ;; - val read : unit -> t option - val write : t-> unit - val to_string : t -> string - val to_rst : t -> Rst_string.t - val of_rst : Rst_string.t -> t option -end = struct - type t = - { n_det_max_fci : Det_number_max.t; - pt2_max : PT2_energy.t; - do_pt2_end : bool; - var_pt2_ratio : Normalized_float.t; - } with sexp - ;; - - let get_default = Qpackage.get_ezfio_default "full_ci";; - - let read_n_det_max_fci () = - if not (Ezfio.has_full_ci_n_det_max_fci ()) then - get_default "n_det_max_fci" - |> Int.of_string - |> Ezfio.set_full_ci_n_det_max_fci - ; - Ezfio.get_full_ci_n_det_max_fci () - |> Det_number_max.of_int - ;; - - let write_n_det_max_fci ndet = - Det_number_max.to_int ndet - |> Ezfio.set_full_ci_n_det_max_fci - ;; - - let read_var_pt2_ratio () = - if not (Ezfio.has_full_ci_var_pt2_ratio ()) then - get_default "var_pt2_ratio" - |> Float.of_string - |> Ezfio.set_full_ci_var_pt2_ratio - ; - Ezfio.get_full_ci_var_pt2_ratio () - |> Normalized_float.of_float - ;; - - let write_var_pt2_ratio ratio = - Normalized_float.to_float ratio - |> Ezfio.set_full_ci_var_pt2_ratio - ;; - - let read_pt2_max () = - if not (Ezfio.has_full_ci_pt2_max ()) then - get_default "pt2_max" - |> Float.of_string - |> Ezfio.set_full_ci_pt2_max - ; - Ezfio.get_full_ci_pt2_max () - |> PT2_energy.of_float - ;; - - let write_pt2_max pt2_max = - PT2_energy.to_float pt2_max - |> Ezfio.set_full_ci_pt2_max - ;; - - let read_do_pt2_end () = - if not (Ezfio.has_full_ci_do_pt2_end ()) then - get_default "do_pt2_end" - |> Bool.of_string - |> Ezfio.set_full_ci_do_pt2_end - ; - Ezfio.get_full_ci_do_pt2_end () - ;; - - let write_do_pt2_end = - Ezfio.set_full_ci_do_pt2_end - ;; - - - let read () = - Some - { n_det_max_fci = read_n_det_max_fci (); - pt2_max = read_pt2_max (); - do_pt2_end = read_do_pt2_end (); - var_pt2_ratio = read_var_pt2_ratio (); - } - ;; - - - let write { n_det_max_fci ; - pt2_max ; - do_pt2_end ; - var_pt2_ratio ; - } = - write_n_det_max_fci n_det_max_fci; - write_pt2_max pt2_max; - write_do_pt2_end do_pt2_end; - write_var_pt2_ratio var_pt2_ratio; - ;; - - let to_string b = - Printf.sprintf " -n_det_max_fci = %s -pt2_max = %s -do_pt2_end = %s -var_pt2_ratio = %s -" - (Det_number_max.to_string b.n_det_max_fci) - (PT2_energy.to_string b.pt2_max) - (Bool.to_string b.do_pt2_end) - (Normalized_float.to_string b.var_pt2_ratio) - ;; - - let to_rst b = - Printf.sprintf " -Stop when the `n_det` > `n_det_max_fci` :: - - n_det_max_fci = %s - -Stop when -E(PT2) < `pt2_max` :: - - pt2_max = %s - -Compute E(PT2) at the end :: - - do_pt2_end = %s - -Target energy ratio variational/(variational+PT2) :: - - var_pt2_ratio = %s -" - (Det_number_max.to_string b.n_det_max_fci) - (PT2_energy.to_string b.pt2_max) - (Bool.to_string b.do_pt2_end) - (Normalized_float.to_string b.var_pt2_ratio) - |> Rst_string.of_string - ;; - - include Generic_input_of_rst;; - let of_rst = of_rst t_of_sexp;; - - -end - - diff --git a/scripts/ezfio_interface/ei_handler.py b/scripts/ezfio_interface/ei_handler.py index 7b631721..3396e6db 100755 --- a/scripts/ezfio_interface/ei_handler.py +++ b/scripts/ezfio_interface/ei_handler.py @@ -414,7 +414,11 @@ def save_ezfio_config(module_lower, str_ezfio_config): f.write(str_ezfio_config) -def create_ocaml_check(dict_ezfio_cfg): +def create_ocaml_input(dict_ezfio_cfg): + + l_provider = [k for k, v in dict_ezfio_cfg.iteritems() if 'default' in v] + l_type = [v["type"] for k, v in dict_ezfio_cfg.iteritems() if 'default' in v] + l_doc = [v["doc"] for k, v in dict_ezfio_cfg.iteritems() if 'default' in v] # ~#~#~#~#~#~#~#~# # # C r e a t i o n # @@ -422,8 +426,42 @@ def create_ocaml_check(dict_ezfio_cfg): from ezfio_generate_ocaml import EZFIO_ocaml + template = ['(* =~=~ *)', + '(* Init *)', + '(* =~=~ *)', + ""] + + template += ["open Qptypes;;", + "open Qputils;;", + "open Core.Std;;", + "", + "module Full_ci : sig"] + + template += [EZFIO_ocaml.create_type(l_provider, l_type)] + + template += [" val read : unit -> t option", + " val write : t-> unit", + " val to_string : t -> string", + " val to_rst : t -> Rst_string.t", + " val of_rst : Rst_string.t -> t option", + "end = struct"] + + template += [EZFIO_ocaml.create_type(l_provider, l_type)] + + template += ['', + ' let get_default = Qpackage.get_ezfio_default "full_ci";;', + ''] + + template += ['(* =~=~=~=~=~=~==~=~=~=~=~=~ *)', + '(* Generate Special Function *)', + '(* =~=~=~==~=~~=~=~=~=~=~=~=~ *)', + ""] + for provider_name, d_val in sorted(dict_ezfio_cfg.iteritems()): + if 'default' not in d_val: + continue + ezfio_dir = d_val["ezfio_dir"] ezfio_name = d_val["ezfio_name"] @@ -433,10 +471,48 @@ def create_ocaml_check(dict_ezfio_cfg): e = EZFIO_ocaml(**d) - template = e.create_read() + template += [e.create_read(), + e.create_write(), + ""] - print template + template += ['(* =~=~=~=~=~=~=~=~=~=~=~=~ *)', + '(* Generate Global Function *)', + '(* =~=~=~=~=~=~=~=~=~=~=~=~ *)', + ""] + template += [EZFIO_ocaml.create_read_global(l_provider), + EZFIO_ocaml.create_write_global(l_provider), + EZFIO_ocaml.create_to_string(l_provider, l_type), + EZFIO_ocaml.create_to_rst(l_provider, l_type, l_doc)] + + template += [" include Generic_input_of_rst;;", + " let of_rst = of_rst t_of_sexp;;", + "", + "end"] + + return "\n".join(template) + + +def save_ocaml_input(module_lower, str_ocaml_input): + """ + Write the str_ocaml_input in + $QPACKAGE_ROOT/ocaml/Input_{0}.ml".format(module_lower) + """ + + path = "{0}/ocaml/Input_{1}.ml".format(os.environ['QPACKAGE_ROOT'], + module_lower) + + try: + f = open(path, "r") + except IOError: + old_output = "" + else: + old_output = f.read() + f.close() + + if str_ocaml_input != old_output: + with open(path, "w") as f: + f.write(str_ocaml_input) def main(): """ @@ -480,7 +556,8 @@ def main(): # O c a m l # # ~#~#~#~#~#~# - _ = create_ocaml_check(dict_ezfio_cfg) + str_ocaml_input = create_ocaml_input(dict_ezfio_cfg) + save_ocaml_input(module_lower, str_ocaml_input) # ~#~#~#~#~#~#~#~# # I R P . f 9 0 # diff --git a/scripts/ezfio_interface/ezfio_generate_ocaml.py b/scripts/ezfio_interface/ezfio_generate_ocaml.py index bbed9685..fc71ddbe 100755 --- a/scripts/ezfio_interface/ezfio_generate_ocaml.py +++ b/scripts/ezfio_interface/ezfio_generate_ocaml.py @@ -17,13 +17,17 @@ class EZFIO_ocaml(object): def Ocaml_type(self): return self.type.ocaml.capitalize() + @property + def ocaml_type(self): + return self.type.ocaml + @property def fancy_type(self): return self.type.fancy def create_read(self): ''' - Take an imput a list of keyword argument + You need to instantiate the EZFIO_ocaml with this keyword argument ezfio_dir = str ezfio_name = str type = Named_tuple(fancy_type, ocaml_type, fortrant_type) @@ -42,19 +46,20 @@ class EZFIO_ocaml(object): # C r e a t e _ t e m pl a t e # # ~#~#~#~#~#~#~#~#~#~#~#~#~#~# # - l_template = ['(* Read snippet for {self.ezfio_name} *)', - 'let read_{self.ezfio_name} () =', - ' if not (Ezfio.has_{self.ezfio_dir}_{self.ezfio_name} ()) then', - ' get_default "{self.ezfio_name}"', - ' |> {self.Ocaml_type}.of_string', - ' |> Ezfio.set_{self.ezfio_dir}_{self.ezfio_name}', - ' ;', - ' Ezfio.get_{self.ezfio_dir}_{self.ezfio_name} ()'] + l_template = [ + '(* Read snippet for {self.ezfio_name} *)', + 'let read_{self.ezfio_name} () =', + ' if not (Ezfio.has_{self.ezfio_dir}_{self.ezfio_name} ()) then', + ' get_default "{self.ezfio_name}"', + ' |> {self.Ocaml_type}.of_string', + ' |> Ezfio.set_{self.ezfio_dir}_{self.ezfio_name}', + ' ;', + ' Ezfio.get_{self.ezfio_dir}_{self.ezfio_name} ()'] if self.fancy_type: - l_template += [" |> {self.fancy_type}.of_{self.Ocaml_type}"] + l_template += [" |> {self.fancy_type}.of_{self.ocaml_type}"] - l_template += [";;;"] + l_template += [";;"] template = "\n ".join(l_template) @@ -70,4 +75,158 @@ class EZFIO_ocaml(object): return template_rendered def create_write(self): - pass + ''' + You need to instantiate the EZFIO_ocaml with this keyword argument + ezfio_dir = str + ezfio_name = str + type = Named_tuple(fancy_type, ocaml_type, fortrant_type) + + Return the read template + ''' + + for i in ["ezfio_dir", "ezfio_name", "type"]: + try: + "exec self.{0}".format(i) + except NameError: + msg = "You need to provide a '{0}' for creating read function" + raise KeyError(msg.format(i)) + + # ~#~#~#~#~#~#~#~#~#~#~#~#~#~# # + # C r e a t e _ t e m pl a t e # + # ~#~#~#~#~#~#~#~#~#~#~#~#~#~# # + + l_template = ['(* Write snippet for {self.ezfio_name} *)'] + + if self.fancy_type: + l_template += ['let write_{self.ezfio_name} var = ', + ' {self.fancy_type}.to_{self.ocaml_type} var', + ' |> Ezfio.set_{self.ezfio_dir}_{self.ezfio_name}'] + else: + l_template += ['let write_{self.ezfio_name} =', + ' Ezfio.set_{self.ezfio_dir}_{self.ezfio_name}'] + + l_template += [';;'] + + template = "\n ".join(l_template) + + # ~#~#~#~#~#~ # + # R e n d e r # + # ~#~#~#~#~#~ # + + template_rendered = template.format(**locals()) + + # ~#~#~#~#~#~ # + # R e t u r n # + # ~#~#~#~#~#~ # + return template_rendered + + @staticmethod + def create_type(l_provider, l_type): + + l_template = ["(* Generate type *)", + "type t = ", + " {"] + + for p, t in zip(l_provider, l_type): + + if t.fancy: + l_template += [" {0:<30} : {1}.t;".format(p, t.fancy)] + else: + l_template += [" {0:<30} : {1};".format(p, t.ocaml)] + l_template += [" } with sexp", + ";;"] + + # ~#~#~#~#~#~ # + # R e t u r n # + # ~#~#~#~#~#~ # + return "\n ".join(l_template) + + @staticmethod + def create_read_global(l_provider): + + l_template = ["(* Read all *)", + "let read() = ", + " Some", + " {"] + l_template += [" {0:<30} = read_{0} ();".format(p) for p in l_provider] + + l_template += [" }", + ";;"] + + # ~#~#~#~#~#~ # + # R e t u r n # + # ~#~#~#~#~#~ # + return "\n ".join(l_template) + + @staticmethod + def create_write_global(l_provider): + + l_template = ["(* Write all *)", + "let write{ "] + l_template += [" {0};".format(p) for p in l_provider] + l_template += [" } ="] + l_template += [" write_{0:<30} {0};".format(p) for p in l_provider] + l_template += [";;"] + + # ~#~#~#~#~#~ # + # R e t u r n # + # ~#~#~#~#~#~ # + return "\n ".join(l_template) + + @staticmethod + def create_to_string(l_provider, l_type): + + l_template = ['(* to_string*)', + 'let to_string b =', + ' Printf.sprintf "'] + + l_template += ["{0} = %s".format(p) for p in l_provider] + l_template += ['"'] + + for p, t in zip(l_provider, l_type): + + if t.fancy: + str_ = t.fancy + else: + str_ = t.ocaml.capitalize() + + l_template += [" ({0}.to_string b.{1})".format(str_, p)] + + l_template += [";;"] + + # ~#~#~#~#~#~ # + # R e t u r n # + # ~#~#~#~#~#~ # + return "\n ".join(l_template) + + @staticmethod + def create_to_rst(l_provider, l_type, l_doc): + + l_template = ['(* to_rst*)', + 'let to_rst b =', + ' Printf.sprintf "'] + + for p, d in zip(l_provider, l_doc): + + l_template += ["{0} ::".format(d), + "", + " {0} = %s".format(p), + ""] + l_template += ['"'] + + for p, t in zip(l_provider, l_type): + + if t.fancy: + str_ = t.fancy + else: + str_ = t.ocaml.capitalize() + + l_template += [" ({0}.to_string b.{1})".format(str_, p)] + + l_template += ["|> Rst_string.of_string", + ";;"] + + # ~#~#~#~#~#~ # + # R e t u r n # + # ~#~#~#~#~#~ # + return "\n ".join(l_template) diff --git a/src/Full_CI/EZFIO.cfg b/src/Full_CI/EZFIO.cfg index 8146ff81..1f701ad8 100644 --- a/src/Full_CI/EZFIO.cfg +++ b/src/Full_CI/EZFIO.cfg @@ -18,8 +18,8 @@ default: true [PT2_max] type: PT2_energy -doc: The selection process stops when the largest PT2 (for all the state is lower - than pt2_max in absolute value) +doc: The selection process stops when the largest PT2 (for all the state) is lower + than pt2_max in absolute value interface: output default: 0.0001