Loading src/scripts/guess_resources.py +98 −5 Original line number Diff line number Diff line Loading @@ -24,10 +24,13 @@ # The script requires python3. import math import re from sys import argv ## \cond __version__ = "0.10.10" int_reg = re.compile(r'[-+]?[0-9]') number_reg = re.compile(r'[-+]?[0-9]\.[0-9]+E?[-+][0-9]{2,5}') ## \endcond ## \brief Main execution code. Loading @@ -41,31 +44,121 @@ def main(): config = parse_arguments() if config['version_mode']: print("guess_resources.py v%s."%__version__) exit(0) elif (config['help_mode']): print_help() else: # Will do something here. if (config['edfb_file'] == ''): print("ERROR: missing scatterer configuration file (--edfb EDFB_FILE).") result = 1 elif (config['geom_file'] == ''): print("ERROR: missing geometry configuration file (--geom GEOM_FILE).") result = 1 else: print("Would finally run the script.") model = scan_model(config['edfb_file'], config['geom_file']) return result ## \brief Parse the command line arguments. # # The script behaviour can be modified through a set of optional arguments. # The purpose of this function is to parse the command line in search for # such arguments and prepare the execution accordingly. # # \returns config: `dict` A dictionary containing the script configuration. def parse_arguments(): config = { 'edfb_file': '', 'geom_file': '', 'help_mode': False, 'version_mode': False } skip_arg = False dict_key = '' for arg in argv[1:]: if (skip_arg): if (dict_key != ''): config[dict_key] = arg dict_key = '' skip_arg = False continue if (arg.startswith("--help")): config['help_mode'] = True elif (arg.startswith("--version")): config['version_mode'] = True elif (arg.startswith("--edfb")): dict_key = 'edfb_file' skip_arg = True elif (arg.startswith("--geom")): dict_key = 'geom_file' skip_arg = True else: raise Exception("Unrecognized argument \'{0:s}\'".format(arg)) return config ## \brief Print a command-line help summary. def print_help(): print("############################################### ") print("# # ") print("# NPTM_code MODEL_MAKER # ") print("# NPTM_code GUESS_RESOURCES # ") print("# # ") print("############################################### ") print(" ") print("Create input files for FORTRAN and C++ code. ") print("Evaluate the resources required by a model. ") print(" ") print("Usage: \"./model_maker.py CONFIG [OPTIONS]\" ") print("Usage: \"./guess_resources.py --edfb EDFB --geom GEOM\" ") print(" ") print("CONFIG must be a valid YAML configuration file. ") print("EDFB and GEOM must be valid NPTM_code configuration files.") print(" ") print("Valid options are: ") print("--help Print this help and exit. ") print("--version Print script version and exit. ") print(" ") ## \brief Scan the calculation model. # # The computational costs depennd the characteristics of the model. This # function scans the model configuration files and returns a model description # dictionary. # # \returns model: `dict` A dictionary containing the model description. def scan_model(edfb_name, geom_name): file_line = "INIT" read_lines = 0 model = { 'app': "", 'nsph': 0, 'ies': 0, 'li': 0, 'le': 0, 'configurations': 0, 'ros': [], 'vec_x': [], 'vec_y': [], 'vec_z': [] } edfb_file = open(edfb_name, "r") file_line = edfb_file.readline() read_lines += 1 iter_values = int_reg.finditer(file_line) if (len(iter_values) != 2): print("ERROR: %s is not a valid scatterer configuration file!"%edfb_name) print(" INVALID LINE: \"%s\""%file_line) print(" at line %d in %s."%(read_lines, file_line)) return None model['nsph'] = int(iter_values[0].group()) model['ies'] = int(iter_values[1].group()) if (model['ies'] > 0): model['app'] = "INCLUSION" else: if model['nsph'] == 1: model['app'] = "SPHERE" else: model['app'] = "CLUSTER" # end if model['ies'] block edfb_file.close() return model ## \brief Exit code (0 for success). exit_code = main() exit(exit_code) Loading
src/scripts/guess_resources.py +98 −5 Original line number Diff line number Diff line Loading @@ -24,10 +24,13 @@ # The script requires python3. import math import re from sys import argv ## \cond __version__ = "0.10.10" int_reg = re.compile(r'[-+]?[0-9]') number_reg = re.compile(r'[-+]?[0-9]\.[0-9]+E?[-+][0-9]{2,5}') ## \endcond ## \brief Main execution code. Loading @@ -41,31 +44,121 @@ def main(): config = parse_arguments() if config['version_mode']: print("guess_resources.py v%s."%__version__) exit(0) elif (config['help_mode']): print_help() else: # Will do something here. if (config['edfb_file'] == ''): print("ERROR: missing scatterer configuration file (--edfb EDFB_FILE).") result = 1 elif (config['geom_file'] == ''): print("ERROR: missing geometry configuration file (--geom GEOM_FILE).") result = 1 else: print("Would finally run the script.") model = scan_model(config['edfb_file'], config['geom_file']) return result ## \brief Parse the command line arguments. # # The script behaviour can be modified through a set of optional arguments. # The purpose of this function is to parse the command line in search for # such arguments and prepare the execution accordingly. # # \returns config: `dict` A dictionary containing the script configuration. def parse_arguments(): config = { 'edfb_file': '', 'geom_file': '', 'help_mode': False, 'version_mode': False } skip_arg = False dict_key = '' for arg in argv[1:]: if (skip_arg): if (dict_key != ''): config[dict_key] = arg dict_key = '' skip_arg = False continue if (arg.startswith("--help")): config['help_mode'] = True elif (arg.startswith("--version")): config['version_mode'] = True elif (arg.startswith("--edfb")): dict_key = 'edfb_file' skip_arg = True elif (arg.startswith("--geom")): dict_key = 'geom_file' skip_arg = True else: raise Exception("Unrecognized argument \'{0:s}\'".format(arg)) return config ## \brief Print a command-line help summary. def print_help(): print("############################################### ") print("# # ") print("# NPTM_code MODEL_MAKER # ") print("# NPTM_code GUESS_RESOURCES # ") print("# # ") print("############################################### ") print(" ") print("Create input files for FORTRAN and C++ code. ") print("Evaluate the resources required by a model. ") print(" ") print("Usage: \"./model_maker.py CONFIG [OPTIONS]\" ") print("Usage: \"./guess_resources.py --edfb EDFB --geom GEOM\" ") print(" ") print("CONFIG must be a valid YAML configuration file. ") print("EDFB and GEOM must be valid NPTM_code configuration files.") print(" ") print("Valid options are: ") print("--help Print this help and exit. ") print("--version Print script version and exit. ") print(" ") ## \brief Scan the calculation model. # # The computational costs depennd the characteristics of the model. This # function scans the model configuration files and returns a model description # dictionary. # # \returns model: `dict` A dictionary containing the model description. def scan_model(edfb_name, geom_name): file_line = "INIT" read_lines = 0 model = { 'app': "", 'nsph': 0, 'ies': 0, 'li': 0, 'le': 0, 'configurations': 0, 'ros': [], 'vec_x': [], 'vec_y': [], 'vec_z': [] } edfb_file = open(edfb_name, "r") file_line = edfb_file.readline() read_lines += 1 iter_values = int_reg.finditer(file_line) if (len(iter_values) != 2): print("ERROR: %s is not a valid scatterer configuration file!"%edfb_name) print(" INVALID LINE: \"%s\""%file_line) print(" at line %d in %s."%(read_lines, file_line)) return None model['nsph'] = int(iter_values[0].group()) model['ies'] = int(iter_values[1].group()) if (model['ies'] > 0): model['app'] = "INCLUSION" else: if model['nsph'] == 1: model['app'] = "SPHERE" else: model['app'] = "CLUSTER" # end if model['ies'] block edfb_file.close() return model ## \brief Exit code (0 for success). exit_code = main() exit(exit_code)