From 00bdee55360e726d75e0dbde1741bae663981201 Mon Sep 17 00:00:00 2001 From: joergbuchwald Date: Mon, 24 Jun 2024 16:47:10 +0200 Subject: [PATCH 1/2] pylinting + tiny bugfixes --- ogs6py/classes/build_tree.py | 6 +++ ogs6py/classes/geo.py | 2 +- ogs6py/classes/linsolvers.py | 4 +- ogs6py/classes/mesh.py | 6 +-- ogs6py/classes/processes.py | 20 ++++++--- ogs6py/classes/python_script.py | 2 +- ogs6py/ogs.py | 78 ++++----------------------------- 7 files changed, 33 insertions(+), 85 deletions(-) diff --git a/ogs6py/classes/build_tree.py b/ogs6py/classes/build_tree.py index 312339d..5ee160d 100644 --- a/ogs6py/classes/build_tree.py +++ b/ogs6py/classes/build_tree.py @@ -51,6 +51,9 @@ def populate_tree(cls, parent, tag, text='', attr=None, overwrite=False): @classmethod def get_child_tag(cls, parent, tag, attr=None, attr_val=None): + """ + search for child tag based on tag and possible attributes + """ q = None for child in parent: if child.tag == tag: @@ -63,6 +66,9 @@ def get_child_tag(cls, parent, tag, attr=None, attr_val=None): @classmethod def get_child_tag_for_type(cls, parent, tag, subtagval, subtag="type"): + """ + search for child tag based on subtag type + """ q = None for child in parent: if child.tag == tag: diff --git a/ogs6py/classes/geo.py b/ogs6py/classes/geo.py index b7e8d67..b40a7de 100644 --- a/ogs6py/classes/geo.py +++ b/ogs6py/classes/geo.py @@ -26,4 +26,4 @@ def add_geometry(self, filename): ---------- filename : `str` """ - geom = self.populate_tree(self.root, "geometry", text=filename, overwrite=True) + self.populate_tree(self.root, "geometry", text=filename, overwrite=True) diff --git a/ogs6py/classes/linsolvers.py b/ogs6py/classes/linsolvers.py index fbf3b89..3c4c987 100644 --- a/ogs6py/classes/linsolvers.py +++ b/ogs6py/classes/linsolvers.py @@ -39,11 +39,11 @@ def add_lin_solver(self, **args): lis : `str` for lis only """ self._convertargs(args) - if not "name" in args: + if "name" not in args: raise KeyError("You need to provide a name for the linear solver.") ls = self.populate_tree(self.lss, 'linear_solver', overwrite=True) self.populate_tree(ls, 'name', text=args['name'], overwrite=True) - if not "kind" in args: + if "kind" not in args: raise KeyError("No kind given. Please specify the linear \ solver library (e.g.: eigen, petsc, lis).") if args['kind'] == "eigen": diff --git a/ogs6py/classes/mesh.py b/ogs6py/classes/mesh.py index 602472d..f075113 100644 --- a/ogs6py/classes/mesh.py +++ b/ogs6py/classes/mesh.py @@ -7,7 +7,6 @@ """ # pylint: disable=C0103, R0902, R0914, R0913 -from lxml import etree as ET from ogs6py.classes import build_tree class Mesh(build_tree.BuildTree): @@ -41,7 +40,7 @@ def add_mesh(self, filename, axially_symmetric=None): elif isinstance(axially_symmetric, str): attr_dict = {"axially_symmetric": axially_symmetric} if self.mesh is not None: - if self.mesh.text is "": + if self.mesh.text == "": self.populate_tree(self.root, "mesh", text=filename, attr=attr_dict, overwrite=True) else: entry = self.mesh.text @@ -57,7 +56,7 @@ def add_mesh(self, filename, axially_symmetric=None): self.geometry = self.root.find("./geometry") self.populate_tree(self.meshes, "mesh", text=entry, attr=mesh0attr_dict) self.populate_tree(self.meshes, "mesh", text=filename, attr=attr_dict) - elif (self.meshes is not None): + elif self.meshes is not None: self.populate_tree(self.meshes, "mesh", text=filename, attr=attr_dict) self.geometry = self.root.find("./geometry") if self.geometry is not None: @@ -65,4 +64,3 @@ def add_mesh(self, filename, axially_symmetric=None): self.geometry = self.root.find("./geometry") else: raise RuntimeError("This should not happpen") - diff --git a/ogs6py/classes/processes.py b/ogs6py/classes/processes.py index 8febd51..f477319 100644 --- a/ogs6py/classes/processes.py +++ b/ogs6py/classes/processes.py @@ -37,7 +37,7 @@ def add_process_variable(self, process_variable="", process_variable_name=""): process_variable_name : `str` """ self.procvars = self.populate_tree(self.process, "process_variables", overwrite=True) - if not process_variable == "": + if process_variable != "": if process_variable_name == "": raise KeyError("process_variable_name missing.") self.procvar[process_variable] = self.populate_tree(self.procvars, @@ -90,7 +90,7 @@ def set_process(self, **args): self.populate_tree(self.process, "specific_body_force", text=" ".join(str(x) for x in args['specific_body_force'])) for key, value in args.items(): if isinstance(value, str): - self.populate_tree(self.process, key, text=args[key]) + self.populate_tree(self.process, key, text=value) def set_constitutive_relation(self, **args): @@ -115,6 +115,9 @@ def set_constitutive_relation(self, **args): def add_bhe_type(self, bhe_type): + """ + Adds a BHE type + """ self.borehole_heat_exchangers = self.populate_tree( self.process, "borehole_heat_exchangers", overwrite=True) self.borehole_heat_exchanger.append(self.populate_tree( @@ -122,8 +125,12 @@ def add_bhe_type(self, bhe_type): self.populate_tree(self.borehole_heat_exchanger[-1], "type", text = bhe_type) def add_bhe_component(self, index=0, **args): + """ + adds a BHE component + """ self._convertargs(args) - if not 'comp_type' in args: + bhe_type = "" + if 'comp_type' not in args: raise KeyError("No BHE component name specified.") bhecomponent = self.populate_tree(self.borehole_heat_exchanger[index], args['comp_type']) if bhecomponent.tag == "borehole": @@ -133,10 +140,9 @@ def add_bhe_component(self, index=0, **args): for element in self.borehole_heat_exchanger[index]: if element.tag == "type": bhe_type = element.text - if bhe_type == "1U" or bhe_type == "2U": - inlet_text = "inlet" - outlet_text = "outlet" - elif bhe_type == "CXA" or bhe_type == "CXC": + inlet_text = "inlet" + outlet_text = "outlet" + if bhe_type in ("CXA", "CXC"): inlet_text = "inner" outlet_text = "outer" inlet = self.populate_tree(bhecomponent, inlet_text) diff --git a/ogs6py/classes/python_script.py b/ogs6py/classes/python_script.py index e066a73..07ef422 100644 --- a/ogs6py/classes/python_script.py +++ b/ogs6py/classes/python_script.py @@ -26,4 +26,4 @@ def set_pyscript(self, filename): ---------- filename : `str` """ - self.populate_tree(self.root, "python_script", text=args['filename'], overwrite=True) + self.populate_tree(self.root, "python_script", text=filename, overwrite=True) diff --git a/ogs6py/ogs.py b/ogs6py/ogs.py index a58767b..f2b4b24 100644 --- a/ogs6py/ogs.py +++ b/ogs6py/ogs.py @@ -19,11 +19,11 @@ import shutil import pandas as pd from lxml import etree as ET -from ogs6py.classes import (display, geo, mesh, python_script, processes, media, timeloop, +from ogs6py.classes import (display, geo, mesh, python_script, processes, media, timeloop, local_coordinate_system, parameters, curves, processvars, linsolvers, nonlinsolvers) import ogs6py.log_parser.log_parser as parser import ogs6py.log_parser.common_ogs_analyses as parse_fcts -from ogs6py.classes.properties import * +from ogs6py.classes.properties import * class OGS: """Class for an OGS6 model. @@ -77,24 +77,6 @@ def __init__(self, **args): else: self.inputfile = None self.root = ET.Element("OpenGeoSysProject") - """if len(self.geo.tree['geometry']['text']) > 0: - self.__dict2xml(self.root, self.geo.tree) - self.__dict2xml(self.root, self.mesh.tree) - if len(self.pyscript.tree['pythonscript']['text']) > 0: - self.__dict2xml(self.root, self.pyscript.tree) - self.__dict2xml(self.root, self.processes.tree) - if len(self.media.tree['media']['children']) > 0: - self.__dict2xml(self.root, self.media.tree) - self.__dict2xml(self.root, self.timeloop.tree) - if len(self.local_coordinate_system.tree['local_coordinate_system']['children']) > 0: - self.__dict2xml(self.root, self.local_coordinate_system.tree) - self.__dict2xml(self.root, self.parameters.tree) - if len(self.curves.tree['curves']['children']) > 0: - self.__dict2xml(self.root, self.curves.tree) - self.__dict2xml(self.root, self.processvars.tree) - self.__dict2xml(self.root, self.nonlinsolvers.tree) - self.__dict2xml(self.root, self.linsolvers.tree)) - self._add_includes(self.root)""" # Reparsing for pretty_print to work properly parse = ET.XMLParser(remove_blank_text=True, huge_tree=True) tree_string = ET.tostring(self.root, pretty_print=True) @@ -117,15 +99,6 @@ def __init__(self, **args): self.nonlinear_solvers = nonlinsolvers.NonLinSolvers(self.tree) self.linear_solvers = linsolvers.LinSolvers(self.tree) - def __dict2xml(self, parent, dictionary): - for entry in dictionary: - self.tag.append(ET.SubElement(parent, dictionary[entry]['tag'])) - self.tag[-1].text = str(dictionary[entry]['text']) - for attr in dictionary[entry]['attr']: - self.tag[-1].set(attr, dictionary[entry]['attr'][attr]) - if len(dictionary[entry]['children']) > 0: - self.__dict2xml(self.tag[-1], dictionary[entry]['children']) - def __replace_blocks_by_includes(self): for i, file in enumerate(self.include_files): parent_element = self.include_elements[i].getparent() @@ -371,7 +344,7 @@ def remove_element(self, xpath, tag = None, text = None): """ root = self._get_root() elements = root.findall(xpath) - if tag == None: + if tag is None: for element in elements: element.getparent().remove(element) else: @@ -576,8 +549,8 @@ def set(self, **args): "compensate_displacement": "./process_variables/process_variable[name='displacement']/compensate_non_equilibrium_initial_residuum", "compensate_all": "./process_variables/process_variable/compensate_non_equilibrium_initial_residuum" } - for arg in args: - self.replace_text(args[arg], xpath=property_db[arg]) + for key, val in args.items(): + self.replace_text(val, xpath=property_db[key]) def restart(self, restart_suffix="_restart", t_initial=None, t_end=None, zero_displacement=False): @@ -768,32 +741,6 @@ def run_model(self, logfile="out.log", path=None, args=None, container_path=None print(line) raise RuntimeError('OGS execution was not successful.') - """def build_tree(self): - self.root = ET.Element("OpenGeoSysProject") - if len(self.geo.tree['geometry']['text']) > 0: - self.__dict2xml(self.root, self.geo.tree) - self.__dict2xml(self.root, self.mesh.tree) - if len(self.pyscript.tree['pythonscript']['text']) > 0: - self.__dict2xml(self.root, self.pyscript.tree) - self.__dict2xml(self.root, self.processes.tree) - if len(self.media.tree['media']['children']) > 0: - self.__dict2xml(self.root, self.media.tree) - self.__dict2xml(self.root, self.timeloop.tree) - if len(self.local_coordinate_system.tree['local_coordinate_system']['children']) > 0: - self.__dict2xml(self.root, self.local_coordinate_system.tree) - self.__dict2xml(self.root, self.parameters.tree) - if len(self.curves.tree['curves']['children']) > 0: - self.__dict2xml(self.root, self.curves.tree) - self.__dict2xml(self.root, self.processvars.tree) - self.__dict2xml(self.root, self.nonlinsolvers.tree) - self.__dict2xml(self.root, self.linsolvers.tree) - self._add_includes(self.root) - # Reparsing for pretty_print to work properly - parse = ET.XMLParser(remove_blank_text=True) - tree_string = ET.tostring(self.root, pretty_print=True) - tree_ = ET.fromstring(tree_string, parser=parse) - self.tree = ET.ElementTree(tree_) - """ def write_input(self, keep_includes=False): """Writes the projectfile to disk @@ -819,16 +766,7 @@ def write_input(self, keep_includes=False): xml_declaration=True, pretty_print=True) return True - raise RuntimeError("Something went wrong") - self.build_tree() - ET.indent(self.tree, space=" ") - if self.verbose is True: - display.Display(self.tree) - self.tree.write(self.prjfile, - encoding="ISO-8859-1", - xml_declaration=True, - pretty_print=True) - return True + raise RuntimeError("No tree has been build.") def parse_out(self, logfile=None, filter=None, maximum_lines=None, reset_index=True): """Parses the logfile @@ -880,7 +818,7 @@ def property_dataframe(self, mediamapping=None): for i in range(numofmedia): multidim_prop[i] = {} ## preprocessing - # write elastic properties to MPL + # write elastic properties to MPL for entry in newtree.findall("./processes/process/constitutive_relation"): medium = self._get_medium_pointer(root, entry.attrib["id"]) parent = medium.find("./phases/phase[type='Solid']/properties") @@ -909,7 +847,7 @@ def property_dataframe(self, mediamapping=None): property_value = newtree.findall(f"./media/medium/{location_pointer[location]}properties/property[parameter_name='{parameter_name}']/parameter_name") for entry in property_value: entry.tag = "value" - entry.text = param_value[0].text + entry.text = param_value[0].text # expand tensors expand_tensors(self, numofmedia, multidim_prop, root, location) expand_van_genuchten(self, numofmedia, root, location) From 84194da5558a70feb7801a3cfdf588e40510a4a5 Mon Sep 17 00:00:00 2001 From: joergbuchwald Date: Mon, 24 Jun 2024 16:59:45 +0200 Subject: [PATCH 2/2] fixes specific body force --- ogs6py/classes/processes.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/ogs6py/classes/processes.py b/ogs6py/classes/processes.py index f477319..33fe976 100644 --- a/ogs6py/classes/processes.py +++ b/ogs6py/classes/processes.py @@ -77,20 +77,29 @@ def set_process(self, **args): raise KeyError("type missing.") if "integration_order" not in args: raise KeyError("integration_order missing.") - if "darcy_gravity" in args: - for i, entry in enumerate(args["darcy_gravity"]): - if entry != 0.0: - self.process_baseentries["darcy_gravity"] = self.populate_tree( + for key, value in args.items(): + if key == "darcy_gravity": + for i, entry in enumerate(args["darcy_gravity"]): + if entry != 0.0: + self.process_baseentries["darcy_gravity"] = self.populate_tree( self.process, "darcy_gravity") - self.populate_tree(self.process_baseentries["darcy_gravity"], + self.populate_tree(self.process_baseentries["darcy_gravity"], "axis_id",text =str(i)) - self.populate_tree(self.process_baseentries["darcy_gravity"], + self.populate_tree(self.process_baseentries["darcy_gravity"], "g",text = str(entry)) - if "specific_body_force" in args: - self.populate_tree(self.process, "specific_body_force", text=" ".join(str(x) for x in args['specific_body_force'])) - for key, value in args.items(): - if isinstance(value, str): - self.populate_tree(self.process, key, text=value) + elif key == "specific_body_force": + if isinstance(args["specific_body_force"], list): + self.populate_tree(self.process, "specific_body_force", + text=" ".join(str(x) for x in args['specific_body_force'])) + else: + self.populate_tree(self.process, "specific_body_force", + text=args["specific_body_force"]) + else: + if isinstance(value, str): + self.populate_tree(self.process, key, text=value) + else: + raise RuntimeError(f"{key} is not of type string") + def set_constitutive_relation(self, **args):