Skip to content

Commit 52874e9

Browse files
committed
Bugfix release 0.403 / first ogstools release
1 parent 27ded1e commit 52874e9

19 files changed

+2073
-1001
lines changed

ogs6py/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# -*- coding: utf-8 -*-
22
"""Provide a central version."""
3-
__version__ = "0.402"
3+
__version__ = "0.403"

ogs6py/classes/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# Copyright (c) 2012-2024, OpenGeoSys Community (http://www.opengeosys.org)
2+
# Distributed under a Modified BSD License.
3+
# See accompanying file LICENSE.txt or
4+
# http://www.opengeosys.org/project/license
5+
#
6+
7+
# Author: Joerg Buchwald (Helmholtz Centre for Environmental Research GmbH - UFZ)
8+
19
from . import display
210
from . import mesh
311
from . import geo

ogs6py/classes/build_tree.py

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,77 @@
1-
# -*- coding: utf-8 -*-
21
"""
32
Copyright (c) 2012-2021, OpenGeoSys Community (http://www.opengeosys.org)
43
Distributed under a Modified BSD License.
54
See accompanying file LICENSE or
65
http://www.opengeosys.org/project/license
76
87
"""
8+
9+
10+
from typing import TypeAlias
11+
912
from lxml import etree as ET
1013

14+
OptionalETElement: TypeAlias = (
15+
ET.Element
16+
) # ToDo this should be Optional[ET.Element]
17+
18+
1119
# pylint: disable=C0103, R0902, R0914, R0913
1220
class BuildTree:
13-
""" helper class to create a nested dictionary
21+
"""helper class to create a nested dictionary
1422
representing the xml structure
1523
"""
16-
def __init__(self, tree):
24+
25+
def __init__(self, tree: ET.ElementTree) -> None:
1726
self.tree = tree
1827

19-
def _get_root(self):
20-
root = self.tree.getroot()
21-
return root
28+
def _get_root(self) -> ET.Element:
29+
return self.tree.getroot()
2230

2331
@classmethod
24-
def _convertargs(cls, args):
32+
def _convertargs(cls, args: dict[str, str]) -> None:
2533
"""
2634
convert arguments that are not lists or dictionaries to strings
2735
"""
2836
for item, value in args.items():
29-
if not isinstance(value, (list, dict)):
37+
if not isinstance(value, list | dict):
3038
args[item] = str(value)
3139

3240
@classmethod
33-
def populate_tree(cls, parent, tag, text=None, attr=None, overwrite=False):
41+
def populate_tree(
42+
cls,
43+
parent: ET.Element,
44+
tag: str,
45+
text: str | None = None,
46+
attr: dict[str, str] | None = None,
47+
overwrite: bool = False,
48+
) -> ET.Element:
3449
"""
3550
method to create dictionary from an xml entity
3651
"""
3752
q = None
38-
if not tag is None:
53+
if tag is not None:
3954
if overwrite is True:
4055
for child in parent:
4156
if child.tag == tag:
4257
q = child
4358
if q is None:
4459
q = ET.SubElement(parent, tag)
45-
if not text is None:
60+
if text is not None:
4661
q.text = str(text)
47-
if not attr is None:
62+
if attr is not None:
4863
for key, val in attr.items():
4964
q.set(key, str(val))
5065
return q
5166

5267
@classmethod
53-
def get_child_tag(cls, parent, tag, attr=None, attr_val=None):
68+
def get_child_tag(
69+
cls,
70+
parent: ET.Element,
71+
tag: str,
72+
attr: dict[str, str] | None = None,
73+
attr_val: str | None = None,
74+
) -> OptionalETElement:
5475
"""
5576
search for child tag based on tag and possible attributes
5677
"""
@@ -65,15 +86,18 @@ def get_child_tag(cls, parent, tag, attr=None, attr_val=None):
6586
return q
6687

6788
@classmethod
68-
def get_child_tag_for_type(cls, parent, tag, subtagval, subtag="type"):
89+
def get_child_tag_for_type(
90+
cls, parent: ET.Element, tag: str, subtagval: str, subtag: str = "type"
91+
) -> OptionalETElement:
6992
"""
7093
search for child tag based on subtag type
7194
"""
7295
q = None
7396
for child in parent:
7497
if child.tag == tag:
7598
for subchild in child:
76-
if subchild.tag == subtag:
77-
if subchild.text == subtagval:
78-
q = child
99+
if (subchild.tag == subtag) and (
100+
subchild.text == subtagval
101+
):
102+
q = child
79103
return q

ogs6py/classes/curves.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1-
# -*- coding: utf-8 -*-
21
"""
3-
Copyright (c) 2012-2021, OpenGeoSys Community (http://www.opengeosys.org)
2+
Copyright (c) 2012-2024, OpenGeoSys Community (http://www.opengeosys.org)
43
Distributed under a Modified BSD License.
54
See accompanying file LICENSE or
65
http://www.opengeosys.org/project/license
76
87
"""
98
# pylint: disable=C0103, R0902, R0914, R0913
10-
from ogs6py.classes import build_tree
9+
from typing import Any
10+
11+
from lxml import etree as ET
12+
13+
from ogstools.ogs6py import build_tree
14+
1115

1216
class Curves(build_tree.BuildTree):
1317
"""
1418
Class to create the curve section of the project file.
1519
"""
16-
def __init__(self, tree):
20+
21+
def __init__(self, tree: ET.ElementTree) -> None:
1722
self.tree = tree
1823
self.root = self._get_root()
1924
self.curves = self.populate_tree(self.root, "curves", overwrite=True)
2025

21-
def add_curve(self, **args):
26+
def add_curve(self, **args: Any) -> None:
2227
"""
2328
Adds a new curve.
2429
@@ -29,23 +34,28 @@ def add_curve(self, **args):
2934
values : `list`
3035
"""
3136
if "name" not in args:
32-
raise KeyError("No curve name given.")
37+
msg = "No curve name given."
38+
raise KeyError(msg)
3339
if "coords" not in args:
34-
raise KeyError("No coordinates given.")
40+
msg = "No coordinates given."
41+
raise KeyError(msg)
3542
if "values" not in args:
36-
raise KeyError("No values given.")
43+
msg = "No values given."
44+
raise KeyError(msg)
3745
if len(args["coords"]) != len(args["values"]):
38-
raise ValueError("Number of time coordinate points differs from number of values")
46+
msg = """Number of time coordinate points differs \
47+
from number of values"""
48+
raise ValueError(msg)
3949
curve = self.populate_tree(self.curves, "curve")
40-
self.populate_tree(curve, "name", args['name'])
50+
self.populate_tree(curve, "name", args["name"])
4151
coord_str = ""
4252
value_str = ""
4353
for i, coord in enumerate(args["coords"]):
44-
if i < (len(args["coords"])-1):
54+
if i < (len(args["coords"]) - 1):
4555
coord_str = coord_str + str(coord) + " "
4656
value_str = value_str + str(args["values"][i]) + " "
47-
if i == (len(args["coords"])-1):
57+
if i == (len(args["coords"]) - 1):
4858
coord_str = coord_str + str(coord)
4959
value_str = value_str + str(args["values"][i])
50-
self.populate_tree(curve, 'coords', text=coord_str)
51-
self.populate_tree(curve, 'values', text=value_str)
60+
self.populate_tree(curve, "coords", text=coord_str)
61+
self.populate_tree(curve, "values", text=value_str)

ogs6py/classes/display.py

Lines changed: 89 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,106 @@
1-
# -*- coding: utf-8 -*-
21
"""
32
Copyright (c) 2012-2021, OpenGeoSys Community (http://www.opengeosys.org)
43
Distributed under a Modified BSD License.
54
See accompanying file LICENSE or
65
http://www.opengeosys.org/project/license
76
87
"""
8+
from contextlib import suppress
9+
10+
from lxml import etree as ET
11+
912
try:
10-
from IPython.display import display, Markdown, Latex
13+
from IPython.display import Markdown, display
14+
1115
verbose = True
1216
except ImportError:
1317
verbose = False
1418

19+
1520
# pylint: disable=C0103, R0902, R0914, R0913
1621
class Display:
17-
""" helper class to create a nested dictionary
22+
"""helper class to create a nested dictionary
1823
representing the xml structure
1924
"""
20-
def __init__(self,tree):
25+
26+
def __init__(self, tree: ET.ElementTree):
2127
if verbose is True:
22-
display(Markdown('## OpenGeoSys project file'))
23-
display(Markdown('### Main Info'))
24-
display(Markdown(f"**Process name:** {tree.find('./processes/process/name').text}"))
25-
display(Markdown(f"**Process type:** {tree.find('./processes/process/type').text}"))
26-
t_end = float(tree.find('./time_loop/processes/process/time_stepping/t_end').text)
27-
t_init = float(tree.find('./time_loop/processes/process/time_stepping/t_initial').text)
28-
display(Markdown(f"**Simulation time:** {t_end-t_init}"))
29-
proc_vars = tree.findall("./process_variables/process_variable")
30-
display(Markdown(f"**Output prefix:** {tree.find('./time_loop/output/prefix').text}"))
31-
display(Markdown('### Boundary conditions'))
32-
for var in proc_vars:
33-
proc_var_entries = var.getchildren()
34-
for entry in proc_var_entries:
35-
if entry.tag == "name":
36-
display(Markdown(f"**Process Variable:** {entry.text}"))
37-
if entry.tag == "initial_condition":
38-
display(Markdown(f" - **Initial Condition:** {entry.text}"))
39-
if entry.tag == "order":
40-
display(Markdown(f" - **Order:** {entry.text}"))
41-
if entry.tag == "boundary_conditions":
42-
bcs = entry.getchildren()
43-
for bc in bcs:
44-
bc_entries = bc.getchildren()
45-
for subentry in bc_entries:
46-
if subentry.tag == "type":
47-
display(Markdown(f" - **Type:** {subentry.text}"))
48-
if subentry.tag == "mesh":
49-
display(Markdown(f" - **Mesh:** {subentry.text}"))
50-
if subentry.tag == "geometrical_set":
51-
display(Markdown(f" - **Geometrical Set:** {subentry.text}"))
52-
if subentry.tag == "geometry":
53-
display(Markdown(f" - **Geometry:** {subentry.text}"))
28+
display(Markdown("## OpenGeoSys project file"))
29+
display(Markdown("### Main Info"))
30+
with suppress(AttributeError):
31+
display(
32+
Markdown(
33+
f"**Process name:** {tree.find('./processes/process/name').text}"
34+
)
35+
)
36+
with suppress(AttributeError):
37+
display(
38+
Markdown(
39+
f"**Process type:** {tree.find('./processes/process/type').text}"
40+
)
41+
)
42+
with suppress(AttributeError):
43+
t_end = float(
44+
tree.find(
45+
"./time_loop/processes/process/time_stepping/t_end"
46+
).text
47+
)
48+
t_init = float(
49+
tree.find(
50+
"./time_loop/processes/process/time_stepping/t_initial"
51+
).text
52+
)
53+
display(Markdown(f"**Simulation time:** {t_end-t_init}"))
54+
with suppress(AttributeError):
55+
proc_vars = tree.findall("./process_variables/process_variable")
56+
with suppress(AttributeError):
57+
display(
58+
Markdown(
59+
f"**Output prefix:** {tree.find('./time_loop/output/prefix').text}"
60+
)
61+
)
62+
display(Markdown("### Boundary conditions"))
63+
for var in proc_vars:
64+
proc_var_entries = var.getchildren()
65+
for entry in proc_var_entries:
66+
if entry.tag == "name":
67+
display(
68+
Markdown(f"**Process Variable:** {entry.text}")
69+
)
70+
if entry.tag == "initial_condition":
71+
display(
72+
Markdown(
73+
f" - **Initial Condition:** {entry.text}"
74+
)
75+
)
76+
if entry.tag == "order":
77+
display(Markdown(f" - **Order:** {entry.text}"))
78+
if entry.tag == "boundary_conditions":
79+
bcs = entry.getchildren()
80+
for bc in bcs:
81+
bc_entries = bc.getchildren()
82+
for subentry in bc_entries:
83+
if subentry.tag == "type":
84+
display(
85+
Markdown(
86+
f" - **Type:** {subentry.text}"
87+
)
88+
)
89+
if subentry.tag == "mesh":
90+
display(
91+
Markdown(
92+
f" - **Mesh:** {subentry.text}"
93+
)
94+
)
95+
if subentry.tag == "geometrical_set":
96+
display(
97+
Markdown(
98+
f" - **Geometrical Set:** {subentry.text}"
99+
)
100+
)
101+
if subentry.tag == "geometry":
102+
display(
103+
Markdown(
104+
f" - **Geometry:** {subentry.text}"
105+
)
106+
)

ogs6py/classes/geo.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1-
# -*- coding: utf-8 -*-
21
"""
3-
Copyright (c) 2012-2021, OpenGeoSys Community (http://www.opengeosys.org)
2+
Copyright (c) 2012-2024, OpenGeoSys Community (http://www.opengeosys.org)
43
Distributed under a Modified BSD License.
54
See accompanying file LICENSE or
65
http://www.opengeosys.org/project/license
76
87
"""
98
# pylint: disable=C0103, R0902, R0914, R0913
10-
from ogs6py.classes import build_tree
9+
from lxml import etree as ET
10+
11+
from ogstools.ogs6py import build_tree
12+
1113

1214
class Geo(build_tree.BuildTree):
1315
"""
1416
Class containing the geometry file.
1517
"""
16-
def __init__(self, tree):
18+
19+
def __init__(self, tree: ET.ElementTree) -> None:
1720
self.tree = tree
1821
self.root = self._get_root()
1922
self.populate_tree(self.root, "geometry", overwrite=True)
2023

21-
def add_geometry(self, filename):
24+
def add_geometry(self, filename: str) -> None:
2225
"""
2326
Adds a geometry file.
2427

0 commit comments

Comments
 (0)