From 2f3370817206dc49d370acd44739b3ba4c2deee3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Buchwald?= Date: Thu, 4 Jul 2024 15:01:03 +0200 Subject: [PATCH] preserve tag order --- ogs6py/classes/geo.py | 2 +- ogs6py/classes/timeloop.py | 8 ++++---- ogs6py/ogs.py | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/ogs6py/classes/geo.py b/ogs6py/classes/geo.py index b40a7de..0dac5dc 100644 --- a/ogs6py/classes/geo.py +++ b/ogs6py/classes/geo.py @@ -16,7 +16,7 @@ class Geo(build_tree.BuildTree): def __init__(self, tree): self.tree = tree self.root = self._get_root() - + self.populate_tree(self.root, "geometry", overwrite=True) def add_geometry(self, filename): """ diff --git a/ogs6py/classes/timeloop.py b/ogs6py/classes/timeloop.py index c22d0e6..716dbdf 100644 --- a/ogs6py/classes/timeloop.py +++ b/ogs6py/classes/timeloop.py @@ -17,6 +17,7 @@ def __init__(self, tree): self.tree = tree self.root = self._get_root() self.time_loop = self.populate_tree(self.root, 'time_loop', overwrite=True) + self.gpc = self.populate_tree(self.time_loop, "global_process_coupling", overwrite=True) self.processes = self.populate_tree(self.time_loop, 'processes', overwrite=True) self.output = self.populate_tree(self.time_loop, 'output', overwrite=True) @@ -341,10 +342,9 @@ def add_global_process_coupling(self, **args): local_coupling_processes_max_iter : `str` """ self._convertargs(args) - gpc = self.populate_tree(self.time_loop, "global_process_coupling", overwrite=True) if "max_iter" in args: - self.populate_tree(gpc, "max_iter", text=args['max_iter'], overwrite=True) - convergence_criteria = self.populate_tree(gpc, "convergence_criteria", overwrite=True) + self.populate_tree(self.gpc, "max_iter", text=args['max_iter'], overwrite=True) + convergence_criteria = self.populate_tree(self.gpc, "convergence_criteria", overwrite=True) if "convergence_type" not in args: raise KeyError("No convergence criterion given. \ Specify convergence_type.") @@ -372,7 +372,7 @@ def add_global_process_coupling(self, **args): if "local_coupling_processes" in args: if "local_coupling_processes_max_iter" not in args: raise KeyError("local_coupling_processes_max_iter parameter is missing") - lcp = self.populate_tree(gpc, "local_coupling_processes") + lcp = self.populate_tree(self.gpc, "local_coupling_processes") self.populate_tree(lcp, "max_iter", text=args['local_coupling_processes_max_iter']) for name in args["local_coupling_processes"]: self.populate_tree(lcp, "process_name", text=name) diff --git a/ogs6py/ogs.py b/ogs6py/ogs.py index f2b4b24..160a8ec 100644 --- a/ogs6py/ogs.py +++ b/ogs6py/ogs.py @@ -144,6 +144,19 @@ def _get_root(self, remove_blank_text=False, remove_comments=False): self.include_elements.append(child) return root + def _remove_empty_elements(self): + root = self._get_root() + empty_text_list = ["./geometry"] + empty_el_list = ["./time_loop/global_process_coupling"] + for element in empty_text_list: + entry = root.find(element) + if entry.text == "": + self.remove_element(element) + for element in empty_el_list: + entry = root.find(element) + if len(entry.getchildren()) == 0: + self.remove_element(element) + @classmethod def _get_parameter_pointer(cls, root, name, xpath): params = root.findall(xpath) @@ -750,6 +763,7 @@ def write_input(self, keep_includes=False): keep_includes : `boolean`, optional """ if not self.tree is None: + self._remove_empty_elements() if keep_includes is True: self.__replace_blocks_by_includes() root = self.tree.getroot()