Skip to content

Commit

Permalink
initial commit ng
Browse files Browse the repository at this point in the history
  • Loading branch information
joergbuchwald committed Jun 20, 2024
1 parent de7c5f8 commit 7361c25
Show file tree
Hide file tree
Showing 14 changed files with 612 additions and 988 deletions.
53 changes: 45 additions & 8 deletions ogs6py/classes/build_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
http://www.opengeosys.org/project/license
"""
from lxml import etree as ET

# pylint: disable=C0103, R0902, R0914, R0913
class BuildTree:
""" helper class to create a nested dictionary
representing the xml structure
"""
def __init__(self):
self.tree = {}
def __init__(self, tree):
self.tree = tree

def _get_root(self):
root = self.tree.getroot()
return root

@classmethod
def _convertargs(cls, args):
Expand All @@ -25,12 +30,44 @@ def _convertargs(cls, args):
args[item] = str(value)

@classmethod
def populate_tree(cls, tag, text='', attr=None, children=None):
def populate_tree(cls, parent, tag, text='', attr=None, overwrite=False):
"""
method to create dictionary from an xml entity
"""
if attr is None:
attr = {}
if children is None:
children = {}
return {'tag': tag, 'text': text, 'attr': attr, 'children': children}
q = None
if not tag is None:
if overwrite is True:
for child in parent:
if child.tag == tag:
q = child
if q is None:
q = ET.SubElement(parent, tag)
if not text is None:
q.text = str(text)
if not attr is None:
for key, val in attr.items():
q.set(key, str(val))
return q

@classmethod
def get_child_tag(cls, parent, tag, attr=None, attr_val=None):
q = None
for child in parent:
if child.tag == tag:
if not ((attr is None) and (attr_val is None)):
if child.get(attr) == attr_val:
q = child
else:
q = child
return q

@classmethod
def get_child_tag_for_type(cls, parent, tag, subtagval, subtag="type"):
q = None
for child in parent:
if child.tag == tag:
for subchild in child:
if subchild.tag == subtag:
if subchild.text == subtagval:
q = child
return q
24 changes: 8 additions & 16 deletions ogs6py/classes/curves.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@ class Curves(build_tree.BuildTree):
"""
Class to create the curve section of the project file.
"""
def __init__(self):
self.tree = {
'curves': {
'tag': 'curves',
'text': '',
'attr': {},
'children': {}
}
}
def __init__(self, tree):
self.tree = tree
self.root = self._get_root()
self.curves = self.populate_tree(self.root, "curves", overwrite=True)

def add_curve(self, **args):
"""
Expand All @@ -41,11 +36,8 @@ def add_curve(self, **args):
raise KeyError("No values given.")
if len(args["coords"]) != len(args["values"]):
raise ValueError("Number of time coordinate points differs from number of values")
entries = len(self.tree['curves']['children'])
self.tree['curves']['children']['curve' + str(entries)] = self.populate_tree('curve',
children={})
parameter = self.tree['curves']['children']['curve' + str(entries)]
parameter['children']['name'] = self.populate_tree('name', text=args['name'], children={})
curve = self.populate_tree(self.curves, "curve")
self.populate_tree(curve, "name", args['name'])
coord_str = ""
value_str = ""
for i, coord in enumerate(args["coords"]):
Expand All @@ -55,5 +47,5 @@ def add_curve(self, **args):
if i == (len(args["coords"])-1):
coord_str = coord_str + str(coord)
value_str = value_str + str(args["values"][i])
parameter['children']['coords'] = self.populate_tree('coords', text=coord_str, children={})
parameter['children']['values'] = self.populate_tree('values', text=value_str, children={})
self.populate_tree(curve, 'coords', text=coord_str)
self.populate_tree(curve, 'values', text=value_str)
16 changes: 6 additions & 10 deletions ogs6py/classes/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@ class Geo(build_tree.BuildTree):
"""
Class containing the geometry file.
"""
def __init__(self):
self.tree = {
'geometry': {
'tag': 'geometry',
'text': "",
'attr': {},
'children': {}
}
}
def __init__(self, tree):
self.tree = tree
self.root = self._get_root()
self.populate_tree(self.root, "geometry", overwrite=True)


def add_geom(self, filename):
"""
Expand All @@ -31,4 +27,4 @@ def add_geom(self, filename):
----------
filename : `str`
"""
self.tree['geometry']['text'] = filename
geom = self.populate_tree(self.root, "geometry", text=filename, overwrite=True)
70 changes: 30 additions & 40 deletions ogs6py/classes/linsolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@ class LinSolvers(build_tree.BuildTree):
"""
Class for defining a linear solvers in the project file"
"""
def __init__(self):
self.tree = {
'linear_solvers': {
'tag': 'linear_solvers',
'text': '',
'attr': {},
'children': {}
}
}
def __init__(self, tree):
self.tree = tree
self.root = self._get_root()
self.lss = self.populate_tree(self.root, 'linear_solvers', overwrite=True)

def add_lin_solver(self, **args):
"""
Expand All @@ -37,54 +32,49 @@ def add_lin_solver(self, **args):
max_iteration_step : `int`, optional
scaling : `str`, optional
1 or 0
error_tolerance : `float`
prefix : `str`, optional
required for petsc solver
parameters : `str` for petsc only
lis : `str` for lis only
"""
self._convertargs(args)
if not "name" in args:
raise KeyError("You need to provide a name for the linear solver.")
if not args['name'] in self.tree['linear_solvers']['children']:
self.tree['linear_solvers']['children'][
args['name']] = self.populate_tree('linear_solver', children={})
linear_solver = self.tree['linear_solvers']['children'][
args['name']]['children']
if not 'name' in linear_solver:
linear_solver['name'] = self.populate_tree('name', text=args['name'],children={})
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:
raise KeyError("No kind given. Please specify the linear \
solver library (e.g.: eigen, petsc, lis).")
if not "solver_type" in args:
raise KeyError("No solver_type given.")
if args['kind'] == "eigen":
linear_solver['eigen'] = self.populate_tree('eigen', children={})
linear_solver['eigen']['children']['solver_type'] = self.populate_tree(
'solver_type', text=args['solver_type'], children={})
eigen = self.populate_tree(ls, 'eigen', overwrite=True)
self.populate_tree(eigen, 'solver_type', text=args['solver_type'], overwrite=True)
if "precon_type" in args:
linear_solver['eigen']['children']['precon_type'] = self.populate_tree(
'precon_type', text=args['precon_type'], children={})
self.populate_tree(eigen, 'precon_type', text=args['precon_type'], overwrite=True)
if "max_iteration_step" in args:
linear_solver['eigen']['children']['max_iteration_step'] = self.populate_tree(
'max_iteration_step', text=args['max_iteration_step'], children={})
self.populate_tree(eigen, 'max_iteration_step', text=args['max_iteration_step'], overwrite=True)
if "error_tolerance" in args:
linear_solver['eigen']['children']['error_tolerance'] = self.populate_tree(
'error_tolerance', text=args['error_tolerance'], children={})
self.populate_tree(eigen, 'error_tolerance', text=args['error_tolerance'], overwrite=True)
if "scaling" in args:
linear_solver['eigen']['children']['scaling'] = self.populate_tree(
'scaling', text=args['scaling'], children={})
self.populate_tree(eigen, 'scaling', text=args['scaling'], overwrite=True)
elif args['kind'] == "lis":
string = (f"-i {args['solver_type']} -p {args['precon_type']}"
if "lis" in args:
lis_string = args["lis"]
else:
lis_string = (f"-i {args['solver_type']} -p {args['precon_type']}"
f" -tol {args['error_tolerance']}"
f" -maxiter {args['max_iteration_step']}")
linear_solver['lis'] = self.populate_tree('lis', text=string, children={})
self.populate_tree(ls, 'lis', text=lis_string, overwrite=True)
elif args['kind'] == "petsc":
if 'prefix' not in args:
KeyError("No prefix given.")
prefix = args['prefix']
linear_solver['petsc'] = self.populate_tree('petsc', children={})
linear_solver['petsc']['children'][
'prefix'] = self.populate_tree('prefix', text=prefix, children={})
string = (f"-{prefix}_ksp_type {args['solver_type']} -{prefix}_pc_type"
petsc = self.populate_tree(ls, 'petsc', overwrite=True)
prefix = ""
if "prefix" in args:
self.populate_tree(petsc, 'prefix', args['prefix'], overwrite=True)
prefix = args['prefix']
if "parameters" in args:
self.populate_tree(petsc, 'parameters', args['parameters'], overwrite=True)
else:
petsc_string = (f"-{prefix}_ksp_type {args['solver_type']} -{prefix}_pc_type"
f" {args['precon_type']} -{prefix}_ksp_rtol {args['error_tolerance']}"
f" -{prefix}_ksp_max_it {args['max_iteration_step']}")
linear_solver['petsc']['children'][
'parameters'] = self.populate_tree('parameters', text=string, children={})
self.populate_tree(petsc, 'parameters', petsc_string, overwrite=True)
32 changes: 7 additions & 25 deletions ogs6py/classes/local_coordinate_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@ class LocalCoordinateSystem(build_tree.BuildTree):
"""
Class for defining a local coordinate system in the project file"
"""
def __init__(self):
self.tree = {
'local_coordinate_system': {
'tag': 'local_coordinate_system',
'text': "",
'attr': {},
'children': {}
}
}
def __init__(self, tree):
self.tree = tree
self.root = self._get_root()
self.lcs = self.populate_tree(self.root, "local_coordinate_system", overwrite=True)

def add_basis_vec(self, **args):
"""
Expand All @@ -39,21 +34,8 @@ def add_basis_vec(self, **args):
self._convertargs(args)
if "basis_vector_0" not in args:
raise KeyError("no vector given")
self.tree['local_coordinate_system']['children'] = {
'basis_vector_0': {
'tag': 'basis_vector_0',
'text': args["basis_vector_0"],
'attr': {},
'children': {}}}
self.populate_tree(self.lcs, "basis_vector_0", text=args["basis_vector_0"])
if "basis_vector_1" in args:
self.tree['local_coordinate_system']['children']['basis_vector_1'] = {
'tag': 'basis_vector_1',
'text': args["basis_vector_1"],
'attr': {},
'children': {}}
self.populate_tree(self.lcs, "basis_vector_1", text=args["basis_vector_1"])
if "basis_vector_2" in args:
self.tree['local_coordinate_system']['children']['basis_vector_2'] = {
'tag': 'basis_vector_2',
'text': args["basis_vector_2"],
'attr': {},
'children': {}}
self.populate_tree(self.lcs, "basis_vector_2", text=args["basis_vector_2"])
Loading

0 comments on commit 7361c25

Please sign in to comment.