From 11c0013992618b8919c249dd03f27433dcd6b1c6 Mon Sep 17 00:00:00 2001 From: Thomas Applencourt Date: Thu, 2 Apr 2015 11:30:03 +0200 Subject: [PATCH] Autobuild qp_edit.ml --- ocaml/Input.ml | 7 +- scripts/ezfio_interface/ei_handler.py | 159 ++++++++++++++---- .../ezfio_interface/ezfio_generate_ocaml.py | 159 +++++++++++++++++- .../ezfio_interface/qp_edit_template | 29 +--- 4 files changed, 293 insertions(+), 61 deletions(-) rename ocaml/qp_edit.ml => scripts/ezfio_interface/qp_edit_template (88%) diff --git a/ocaml/Input.ml b/ocaml/Input.ml index 49ac3152..01bb54a0 100644 --- a/ocaml/Input.ml +++ b/ocaml/Input.ml @@ -2,15 +2,10 @@ open Qputils;; open Qptypes;; open Core.Std;; - include Input_ao_basis;; -include Input_bielec_integrals;; include Input_bitmasks;; -include Input_cisd_sc2_selected;; include Input_determinants;; include Input_electrons;; -include Input_full_ci;; -include Input_hartree_fock;; include Input_mo_basis;; include Input_nuclei;; - +include Input_auto_generated;; \ No newline at end of file diff --git a/scripts/ezfio_interface/ei_handler.py b/scripts/ezfio_interface/ei_handler.py index 44a2be36..8fad8511 100755 --- a/scripts/ezfio_interface/ei_handler.py +++ b/scripts/ezfio_interface/ei_handler.py @@ -5,14 +5,14 @@ Welcom the ei_handler. We will create all the ezfio related stuff from a EZFIO.cfg file. Usage: - ei_handler.py [--path] [--irpf90] [--ezfio_config] [--ocaml] [--ezfio_default] + ei_handler.py [--path] [--irpf90] [--ezfio_config] [--ocaml] [--ezfio_default] [--global] By default all the option are executed. Options: -h --help --path The path of the `EZFIO.cfg`, by default will look in the ${pwd} - --irpf90 Create the `ezfio_interface.ipf90` + --irpf90 Create the `ezfio_interface.ipf90` who containt all the provider needed (aka all with the `interface: input` parameter) in `${pwd}` @@ -23,6 +23,8 @@ Options: --ezfio_default Create the `${module_lower}_ezfio_interface_default` in `${QPACKAGE_ROOT}/data/ezfio_defaults` needed by the ocaml + --global Create all the stuff who need all the EZFIO.cfg + Format specification : [provider_name] | the name of the provider in irp.f90 doc:{str} | Is the doc @@ -93,7 +95,6 @@ def get_type_dict(): from os import listdir qpackage_root = os.environ['QPACKAGE_ROOT'] - fancy_type_pickle = qpackage_root + "/scripts/ezfio_interface/fancy_type.p" if fancy_type_pickle in listdir(os.getcwd()): @@ -145,7 +146,9 @@ def get_type_dict(): b = r.find('let untouched = "') e = r.find(';;', b) - l_un = [i for i in r[b:e].splitlines() if i.strip().startswith("module")] + l_un = [ + i for i in r[ + b:e].splitlines() if i.strip().startswith("module")] # ~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~ # # q p _ t y p e s _ g e n e r a t e # @@ -294,7 +297,6 @@ def create_ezfio_provider(dict_ezfio_cfg): return [code, ...] """ from ezfio_generate_provider import EZFIO_Provider - dict_code_provider = dict() ez_p = EZFIO_Provider() @@ -487,7 +489,7 @@ def save_ezfio_default(module_lower, str_ezfio_default): f.write(str_ezfio_default) -def create_ocaml_input(dict_ezfio_cfg,module_lower): +def create_ocaml_input(dict_ezfio_cfg, module_lower): # ~#~#~#~# # # I n i t # @@ -600,6 +602,93 @@ def save_ocaml_input(module_lower, str_ocaml_input): f.write(str_ocaml_input) +def create_ocaml_input_global(): + """ + Check for all the EZFIO.cfg get the module lower + then create incule {module_lower}.ml + """ + + # ~#~#~#~# # + # I n i t # + # ~#~#~#~# # + + from os import listdir + from os.path import isdir, join, exists + + mypath = "{0}/src".format(os.environ['QPACKAGE_ROOT']) + + onlyfodler = [f for f in listdir(mypath) if isdir(join(mypath, f))] + + l_module_lower = [f.lower() for f in onlyfodler + if exists("{0}/{1}/EZFIO.cfg".format(mypath, f))] + + # ~#~#~#~#~#~#~#~# # + # C r e a t i o n # + # ~#~#~#~#~#~#~#~# # + + from ezfio_generate_ocaml import EZFIO_ocaml + + qpackage_root = os.environ['QPACKAGE_ROOT'] + path = qpackage_root + "/scripts/ezfio_interface/qp_edit_template" + + with open(path, "r") as f: + template_raw = f.read() + + e = EZFIO_ocaml(l_module_lower=l_module_lower) + + template = template_raw.format(keywords=e.create_qp_keywords(), + keywords_to_string=e.create_qp_keywords_to_string(), + section_to_rst=e.create_qp_section_to_rst(), + write=e.create_qp_write(), + tasks=e.create_qp_tasks()) + + input_auto = e.create_input_auto_generated() + + return (template, input_auto) + + +def save_ocaml_input_auto(str_ocaml_input_global): + """ + Write the str_ocaml_input in + $QPACKAGE_ROOT/ocaml/Input_auto_generated.ml + """ + + path = "{0}/ocaml/Input_auto_generated.ml".format(os.environ['QPACKAGE_ROOT']) + + try: + f = open(path, "r") + except IOError: + old_output = "" + else: + old_output = f.read() + f.close() + + if str_ocaml_input_global != old_output: + with open(path, "w+") as f: + f.write(str_ocaml_input_global) + + +def save_ocaml_qp_edit(str_ocaml_qp_edit): + """ + Write the str_ocaml_qp_edit in + $QPACKAGE_ROOT/ocaml/qp_edit.ml + """ + + path = "{0}/ocaml/qp_edit.ml".format(os.environ['QPACKAGE_ROOT']) + + try: + f = open(path, "r") + except IOError: + old_output = "" + else: + old_output = f.read() + f.close() + + if str_ocaml_qp_edit != old_output: + with open(path, "w+") as f: + f.write(str_ocaml_qp_edit) + + if __name__ == "__main__": arguments = docopt(__doc__) @@ -608,29 +697,30 @@ if __name__ == "__main__": # _|_ | | | |_ # - if not arguments["--path"]: - config_file_path = "EZFIO.cfg" - if "EZFIO.cfg" not in os.listdir(os.getcwd()): - sys.exit(0) - else: - config_file_path = arguments["path"] + if not arguments["--global"]: + if not arguments["--path"]: + config_file_path = "EZFIO.cfg" + if "EZFIO.cfg" not in os.listdir(os.getcwd()): + sys.exit(0) + else: + config_file_path = arguments["path"] - # Get the full path - config_file_path = os.path.expanduser(config_file_path) - config_file_path = os.path.expandvars(config_file_path) - config_file_path = os.path.abspath(config_file_path) + # Get the full path + config_file_path = os.path.expanduser(config_file_path) + config_file_path = os.path.expandvars(config_file_path) + config_file_path = os.path.abspath(config_file_path) - # ~#~#~#~#~#~#~#~#~#~#~#~#~#~# # - # G e t _ m o d u l e _ d i r # - # ~#~#~#~#~#~#~#~#~#~#~#~#~#~# # + # ~#~#~#~#~#~#~#~#~#~#~#~#~#~# # + # G e t _ m o d u l e _ d i r # + # ~#~#~#~#~#~#~#~#~#~#~#~#~#~# # - path_dirname = os.path.dirname(config_file_path) - module = [i for i in path_dirname.split("/") if i][-1] - module_lower = module.lower() + path_dirname = os.path.dirname(config_file_path) + module = [i for i in path_dirname.split("/") if i][-1] + module_lower = module.lower() - # Because we only authorise this right now! - ezfio_dir = module_lower - dict_ezfio_cfg = get_dict_config_file(config_file_path, ezfio_dir) + # Because we only authorise this right now! + ezfio_dir = module_lower + dict_ezfio_cfg = get_dict_config_file(config_file_path, ezfio_dir) # _ # / _ _| _ _ _ ._ _ ._ _. _|_ o _ ._ @@ -644,7 +734,7 @@ if __name__ == "__main__": "--ezfio_config", "--ocaml", "--ezfio_default", - ]]): + "--global"]]): # User changer somme argument, do what he want do_all = False else: @@ -670,13 +760,22 @@ if __name__ == "__main__": # ~#~#~#~#~#~# # O c a m l # # ~#~#~#~#~#~# + if do_all or arguments["--ocaml"]: + str_ocaml_input = create_ocaml_input(dict_ezfio_cfg, module_lower) + save_ocaml_input(module_lower, str_ocaml_input) - str_ocaml_input = create_ocaml_input(dict_ezfio_cfg, module_lower) - save_ocaml_input(module_lower, str_ocaml_input) + # ~#~#~#~#~#~#~#~#~#~#~#~#~ # + # e z f i o _ d e f a u l t # + # ~#~#~#~#~#~#~#~#~#~#~#~#~ # + if do_all or arguments["--ezfio_default"]: + str_ezfio_default = create_ezfio_default(dict_ezfio_cfg) + save_ezfio_default(module_lower, str_ezfio_default) # ~#~#~#~#~#~#~#~#~#~#~#~#~ # # e z f i o _ d e f a u l t # # ~#~#~#~#~#~#~#~#~#~#~#~#~ # - str_ezfio_default = create_ezfio_default(dict_ezfio_cfg) - save_ezfio_default(module_lower, str_ezfio_default) + if do_all or arguments["--global"]: + str_ocaml_qp_edit, str_ocaml_input_auto = create_ocaml_input_global() + save_ocaml_input_auto(str_ocaml_input_auto) + save_ocaml_qp_edit(str_ocaml_qp_edit) diff --git a/scripts/ezfio_interface/ezfio_generate_ocaml.py b/scripts/ezfio_interface/ezfio_generate_ocaml.py index 21da15b7..25b6c55f 100755 --- a/scripts/ezfio_interface/ezfio_generate_ocaml.py +++ b/scripts/ezfio_interface/ezfio_generate_ocaml.py @@ -1,6 +1,7 @@ #!/usr/bin/env python import sys +import os # If type in **kwargs from ei_handler import Type @@ -227,7 +228,8 @@ class EZFIO_ocaml(object): "let write{ "] l_template += [" {0};".format(p) for p in self.l_ezfio_name] l_template += [" } ="] - l_template += [" write_{0:<30} {0};".format(p) for p in self.l_ezfio_name] + l_template += [" write_{0:<30} {0};".format(p) + for p in self.l_ezfio_name] l_template += [";;"] # ~#~#~#~#~#~ # @@ -324,3 +326,158 @@ class EZFIO_ocaml(object): # R e t u r n # # ~#~#~#~#~#~ # return "\n ".join(l_template) + + def create_input_auto_generated(self): + """ + Generate the include of all the Input_module.lower template + """ + + # ~#~#~#~#~#~#~#~ # + # C h e c k i n g # + # ~#~#~#~#~#~#~#~ # + + self.check_if_init(["l_module_lower"], + sys._getframe().f_code.co_name) + + # ~#~#~#~#~#~#~#~#~#~#~#~#~#~# # + # C r e a t e _ t e m pl a t e # + # ~#~#~#~#~#~#~#~#~#~#~#~#~#~# # + + l_template = ['open Qputils;;', + 'open Qptypes;;', + 'open Core.Std;;', + ''] + + for m in self.l_module_lower: + l_template += ["include Input_{0}".format(m)] + + # ~#~#~#~#~#~ # + # R e t u r n # + # ~#~#~#~#~#~ # + + return "\n".join(l_template) + + def create_qp_keywords(self): + """ + Generate keywords template + """ + + # ~#~#~#~#~#~#~#~ # + # C h e c k i n g # + # ~#~#~#~#~#~#~#~ # + + self.check_if_init(["l_module_lower"], + sys._getframe().f_code.co_name) + + # ~#~#~#~#~#~#~#~#~#~#~#~#~#~# # + # C r e a t e _ t e m pl a t e # + # ~#~#~#~#~#~#~#~#~#~#~#~#~#~# # + + l_template = ["| {0}".format(m.capitalize()) + for m in self.l_module_lower] + + # ~#~#~#~#~#~ # + # R e t u r n # + # ~#~#~#~#~#~ # + + return "\n".join(l_template) + + def create_qp_keywords_to_string(self): + """ + Generate keywords to string template + """ + + # ~#~#~#~#~#~#~#~ # + # C h e c k i n g # + # ~#~#~#~#~#~#~#~ # + + self.check_if_init(["l_module_lower"], + sys._getframe().f_code.co_name) + + # ~#~#~#~#~#~#~#~#~#~#~#~#~#~# # + # C r e a t e _ t e m pl a t e # + # ~#~#~#~#~#~#~#~#~#~#~#~#~#~# # + + l_template = ['| {0} -> "{0}"'.format(m.capitalize()) + for m in self.l_module_lower] + + # ~#~#~#~#~#~ # + # R e t u r n # + # ~#~#~#~#~#~ # + + return "\n".join(l_template) + + def create_qp_section_to_rst(self): + """ + Generate section to rst + """ + # ~#~#~#~#~#~#~#~ # + # C h e c k i n g # + # ~#~#~#~#~#~#~#~ # + + self.check_if_init(["l_module_lower"], + sys._getframe().f_code.co_name) + + # ~#~#~#~#~#~#~#~#~#~#~#~#~#~# # + # C r e a t e _ t e m pl a t e # + # ~#~#~#~#~#~#~#~#~#~#~#~#~#~# # + + l_template = [] + for m in self.l_module_lower: + m_cap = m.capitalize() + l_template += [" | {0} ->".format(m_cap), + " f {0}.(read, to_rst)".format(m_cap)] + + # ~#~#~#~#~#~ # + # R e t u r n # + # ~#~#~#~#~#~ # + + return "\n".join(l_template) + + def create_qp_write(self): + """ + Generate write + """ + # ~#~#~#~#~#~#~#~ # + # C h e c k i n g # + # ~#~#~#~#~#~#~#~ # + + self.check_if_init(["l_module_lower"], + sys._getframe().f_code.co_name) + + # ~#~#~#~#~#~#~#~#~#~#~#~#~#~# # + # C r e a t e _ t e m pl a t e # + # ~#~#~#~#~#~#~#~#~#~#~#~#~#~# # + + str_ = " | {0} -> write {0}.(of_rst, write) s" + l_template = [str_.format(m.capitalize()) for m in self.l_module_lower] + + # ~#~#~#~#~#~ # + # R e t u r n # + # ~#~#~#~#~#~ # + + return "\n".join(l_template) + + def create_qp_tasks(self): + """ + Generate taks + """ + # ~#~#~#~#~#~#~#~ # + # C h e c k i n g # + # ~#~#~#~#~#~#~#~ # + + self.check_if_init(["l_module_lower"], + sys._getframe().f_code.co_name) + + # ~#~#~#~#~#~#~#~#~#~#~#~#~#~# # + # C r e a t e _ t e m pl a t e # + # ~#~#~#~#~#~#~#~#~#~#~#~#~#~# # + + l_template = [" {0} ; ".format(m.capitalize()) + for m in self.l_module_lower] + + # ~#~#~#~#~#~ # + # R e t u r n # + # ~#~#~#~#~#~ # + + return "\n".join(l_template) diff --git a/ocaml/qp_edit.ml b/scripts/ezfio_interface/qp_edit_template similarity index 88% rename from ocaml/qp_edit.ml rename to scripts/ezfio_interface/qp_edit_template index df7635e0..8d52acfd 100644 --- a/ocaml/qp_edit.ml +++ b/scripts/ezfio_interface/qp_edit_template @@ -11,27 +11,21 @@ open Core.Std;; (** Keywords used to define input sections *) type keyword = | Ao_basis -| Bielec_integrals -| Cisd_sc2_selected | Determinants | Electrons -| Full_ci -| Hartree_fock | Mo_basis | Nuclei +{keywords} ;; let keyword_to_string = function | Ao_basis -> "AO basis" -| Bielec_integrals -> "Two electron integrals" -| Cisd_sc2_selected -> "CISD (SC)^2" | Determinants -> "Determinants" | Electrons -> "Electrons" -| Full_ci -> "Selected Full-CI" -| Hartree_fock -> "Hartree-Fock" | Mo_basis -> "MO basis" | Nuclei -> "Molecule" +{keywords_to_string} ;; @@ -70,24 +64,17 @@ let get s = begin let open Input in match s with - | Full_ci -> - f Full_ci.(read, to_rst) - | Hartree_fock -> - f Hartree_fock.(read, to_rst) | Mo_basis -> f Mo_basis.(read, to_rst) | Electrons -> f Electrons.(read, to_rst) - | Cisd_sc2_selected -> - f Cisd_sc2_selected.(read, to_rst) | Nuclei -> f Nuclei.(read, to_rst) | Ao_basis -> f Ao_basis.(read, to_rst) - | Bielec_integrals -> - f Bielec_integrals.(read, to_rst) | Determinants -> f Determinants.(read, to_rst) +{section_to_rst} end with | Sys_error msg -> (Printf.eprintf "Info: %s\n%!" msg ; "") @@ -125,11 +112,8 @@ let set str s = in let open Input in match s with - | Hartree_fock -> write Hartree_fock.(of_rst, write) s - | Full_ci -> write Full_ci.(of_rst, write) s +{write} | Electrons -> write Electrons.(of_rst, write) s - | Cisd_sc2_selected -> write Cisd_sc2_selected.(of_rst, write) s - | Bielec_integrals -> write Bielec_integrals.(of_rst, write) s | Determinants -> write Determinants.(of_rst, write) s | Nuclei -> write Nuclei.(of_rst, write) s | Ao_basis -> () (* TODO *) @@ -176,10 +160,7 @@ let run check_only ezfio_filename = Nuclei ; Ao_basis; Electrons ; - Bielec_integrals ; - Hartree_fock ; - Cisd_sc2_selected ; - Full_ci ; +{tasks} Mo_basis; Determinants ; ]