Skip to content

Commit

Permalink
pylinting + tiny bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
joergbuchwald committed Jun 24, 2024
1 parent 1588edc commit 00bdee5
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 85 deletions.
6 changes: 6 additions & 0 deletions ogs6py/classes/build_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion ogs6py/classes/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions ogs6py/classes/linsolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
6 changes: 2 additions & 4 deletions ogs6py/classes/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -57,12 +56,11 @@ 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:
self.geometry.getparent().remove(self.geometry)
self.geometry = self.root.find("./geometry")
else:
raise RuntimeError("This should not happpen")

20 changes: 13 additions & 7 deletions ogs6py/classes/processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand All @@ -115,15 +115,22 @@ 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(
self.borehole_heat_exchangers, "borehole_heat_exchanger"))
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":
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion ogs6py/classes/python_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
78 changes: 8 additions & 70 deletions ogs6py/ogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 00bdee5

Please sign in to comment.