Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dictionaries for parameters and curves and allow advanced data access #58

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions ogs6py/classes/curve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# -*- coding: utf-8 -*-
"""
Copyright (c) 2012-2023, OpenGeoSys Community (http://www.opengeosys.org)
Distributed under a Modified BSD License.
See accompanying file LICENSE or
http://www.opengeosys.org/project/license

"""
from fastcore.utils import *
import numpy as np
from lxml import etree as ET
# pylint: disable=C0103, R0902, R0914, R0913

class Curve:
def __init__(self, xmlobject:object=None) -> None:
self.__dict__ = {}
self.xmlobject = xmlobject
if not self.xmlobject is None:
for curve_property in self.xmlobject:
if curve_property.tag == "name":
self.__dict__[curve_property.tag] = curve_property.text
else:
self.__dict__[curve_property.tag] = curve_property.text.split(" ")
assert(len(self.__dict__["coords"])==len(self.__dict__["values"]))

def __setitem__(self, key, item):
if not key in self.__dict__:
raise RuntimeError("property is not existing")
expression_counter = -1
if key in ["coords", "values"]:
for i, entry in enumerate(item):
item[i] = str(entry)
for curve_property in self.xmlobject:
if curve_property.tag == key:
curve_property.text = ' '.join(item)
else:
for curve_property in self.xmlobject:
if curve_property.tag == key:
curve_property.text = item

def __getitem__(self, key):
if not (key == "xmlobject"):
return self.__dict__[key]

def __repr__(self):
newdict = {}
for k, v in self.__dict__.items():
if not (k == "xmlobject"):
newdict[k] = v
return repr(newdict)

def __len__(self):
return len(self.__dict__)

def __delitem__(self, key):
pass
#del self.__dict__[key]

def clear(self):
return self.__dict__.clear()

def copy(self):
return self.__dict__.copy()

def has_key(self, k):
if not (k == "xmlobject"):
return k in self.__dict__

def update(self, *args, **kwargs):
pass
# return self.__dict__.update(*args, **kwargs)

def keys(self):
newdict = {}
for k, v in self.__dict__.items():
if not (k == "xmlobject"):
newdict[k] = v
return newdict.keys()

def items(self):
newdict = {}
for k, v in self.__dict__.items():
if not (k == "xmlobject"):
newdict[k] = v
return newdict.items()

def pop(self, *args):
pass
#return self.__dict__.pop(*args)

def __cmp__(self, dict_):
return self.__cmp__(self.__dict__, dict_)

def __contains__(self, item):
newdict = {}
for k, v in self.__dict__.items():
if not (k == "xmlobject"):
newdict[k] = v
return item in newdict

def __iter__(self):
newdict = {}
for k, v in self.__dict__.items():
if not (k == "xmlobject"):
newdict[k] = v
return iter(newdict)

def evaluate_values(self,x):
coords = np.array([float(val) for val in self.__dict__["coords"]])
values = np.array([float(val) for val in self.__dict__["values"]])
return np.interp(x, coords, values)
121 changes: 119 additions & 2 deletions ogs6py/classes/curves.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# -*- coding: utf-8 -*-
"""
Copyright (c) 2012-2021, OpenGeoSys Community (http://www.opengeosys.org)
Copyright (c) 2012-2023, OpenGeoSys Community (http://www.opengeosys.org)
Distributed under a Modified BSD License.
See accompanying file LICENSE or
http://www.opengeosys.org/project/license

"""
# pylint: disable=C0103, R0902, R0914, R0913
from lxml import etree as ET
from ogs6py.classes import build_tree
from ogs6py.classes import curve

class Curves(build_tree.BuildTree):
"""
Class to create the curve section of the project file.
"""
def __init__(self):
def __init__(self, xmlobject=None):
self.tree = {
'curves': {
'tag': 'curves',
Expand All @@ -22,6 +24,121 @@ def __init__(self):
'children': {}
}
}
self.xmlobject = xmlobject
if not (xmlobject is None):
for curve_obj in xmlobject:
for curve_property in curve_obj:
if curve_property.tag == "name":
curve_name = curve_property.text
self.__dict__[curve_name] = curve.Curve(curve_obj)

def __checkcurve(self, dictionary):
required = ["coords", "values"]
optional = ["name"]
for k, v in dictionary.items():
if not k in (required+optional):
raise RuntimeError(f"{k} is not a valid property field for a curve.")
for entry in required:
if not entry in dictionary:
raise RuntimeError(f"{entry} is required for creating a curve.")

def __setitem__(self, key, item):
if not isinstance(item, dict):
raise RuntimeError("Item must be a dictionary")
if len(item) == 0:
self.__delitem__(key)
return
self.__checkcurve(item)
if key in self.__dict__:
self.__delitem__(key)
curve_obj = ET.SubElement(self.xmlobject, "curve")
assert(len(item["coords"])==len(item["values"]))
for i, entry in enumerate(item["coords"]):
item["coords"][i] = str(entry)
item["values"][i] = str(item["values"][i])
q = ET.SubElement(curve_obj, "name")
q.text = key
q = ET.SubElement(curve_obj, "coords")
q.text = ' '.join(item["coords"])
q = ET.SubElement(curve_obj, "values")
q.text = ' '.join(item["values"])
return curve_obj


def __getitem__(self, key):
if not (key in ["tree", "xmlobject"]):
return self.__dict__[key]

def __repr__(self):
newdict = {}
for k, v in self.__dict__.items():
if not (k in ["tree", "name", "xmlobject"]):
newdict[k] = v
return repr(newdict)

def __len__(self):
return len(self.__dict__)

def __delitem__(self, key):
obj = self.__dict__[key].xmlobject
obj.getparent().remove(obj)
del self.__dict__[key]

def clear(self):
return self.__dict__.clear()

def copy(self):
return self.__dict__.copy()

def has_key(self, k):
if not (k in ["tree","xmlobject"]):
return k in self.__dict__

def update(self, *args, **kwargs):
pass
# return self.__dict__.update(*args, **kwargs)

def keys(self):
newdict = {}
for k, v in self.__dict__.items():
if not (k in ["tree","xmlobject"]):
newdict[k] = v
return newdict.keys()

def values(self):
newdict = {}
for k, v in self.__dict__.items():
if not (k in ["tree", "xmlobject"]):
newdict[k] = v
return newdict.values()

def items(self):
newdict = {}
for k, v in self.__dict__.items():
if not (k in ["tree", "xmlobject"]):
newdict[k] = v
return newdict.items()

def pop(self, *args):
pass
#return self.__dict__.pop(*args)

def __cmp__(self, dict_):
return self.__cmp__(self.__dict__, dict_)

def __contains__(self, item):
newdict = {}
for k, v in self.__dict__.items():
if not (k in ["tree", "xmlobject"]):
newdict[k] = v
return item in newdict

def __iter__(self):
newdict = {}
for k, v in self.__dict__.items():
if not (k in ["tree", "xmlobject"]):
newdict[k] = v
return iter(newdict)

def add_curve(self, **args):
"""
Expand Down
18 changes: 17 additions & 1 deletion ogs6py/classes/local_coordinate_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
http://www.opengeosys.org/project/license

"""
import numpy as np
# pylint: disable=C0103, R0902, R0914, R0913
from ogs6py.classes import build_tree

class LocalCoordinateSystem(build_tree.BuildTree):
"""
Class for defining a local coordinate system in the project file"
"""
def __init__(self):
def __init__(self, xmlobject=None):
self.tree = {
'local_coordinate_system': {
'tag': 'local_coordinate_system',
Expand All @@ -22,6 +23,21 @@ def __init__(self):
'children': {}
}
}
self.xmlobject = xmlobject
self.R = None
if not self.xmlobject is None:
basis_vectors = self.xmlobject.getchildren()
dim = len(basis_vectors)
self.R = np.zeros((dim,dim))
basis_vector_names = []
for vec in basis_vectors:
basis_vector_names.append(vec.text)
basis_vector_values = []
for vec in basis_vector_names:
basis_vector_values.append(np.fromstring(self.xmlobject.getparent().find(f"./parameters/parameter[name='{vec}']/values").text, sep=' '))
for i in range(dim):
for j in range(dim):
self.R[i,j] = basis_vector_values[i][j]

def add_basis_vec(self, **args):
"""
Expand Down
Loading