Skip to content

Commit b29eb0e

Browse files
committed
Python interfaces to geos from Makutu repo
1 parent 8cc89f5 commit b29eb0e

File tree

19 files changed

+5280
-0
lines changed

19 files changed

+5280
-0
lines changed

pygeos-tools/pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ dependencies = [
2222
"numpy",
2323
"scipy",
2424
"mpi4py",
25+
"vtk",
26+
"pyevtk",
27+
"xmltodict",
28+
"h5py",
2529
]
2630

2731
[tool.mypy]
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
# ------------------------------------------------------------------------------------------------------------
2+
# SPDX-License-Identifier: LGPL-2.1-only
3+
#
4+
# Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
5+
# Copyright (c) 2018-2024 Total, S.A
6+
# Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
7+
# Copyright (c) 2023-2024 Chevron
8+
# Copyright (c) 2019- GEOS/GEOSX Contributors
9+
# Copyright (c) 2019- INRIA project-team Makutu
10+
# All rights reserved
11+
#
12+
# See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13+
# ------------------------------------------------------------------------------------------------------------
14+
15+
import sys
16+
import argparse
17+
18+
class GeosxArgs:
19+
"""
20+
Class containing the main argument in command line for GEOSX
21+
22+
Attributes
23+
----------
24+
cmdline : list of str
25+
List corresponding to a splitted GEOSX submission line
26+
options : dict
27+
Dict containing GEOSX main options
28+
"""
29+
def __init__(self, args=sys.argv.copy()):
30+
"""
31+
Parameters
32+
----------
33+
args : list of str
34+
List corresponding to a splitted submission line with options
35+
"""
36+
self.cmdline = args
37+
self.options = self.optionsParser()
38+
39+
40+
def optionsParser(self, cmdline=[]):
41+
"""
42+
Return a dict with useful geosx options parsed from a list/submission line.
43+
44+
Parameters
45+
----------
46+
cmdline : list of str
47+
List corresponding to a splitted GEOSX submission line
48+
49+
Returns
50+
--------
51+
dict :
52+
Dict containing GEOSX main options
53+
"""
54+
if not cmdline:
55+
cmdline = self.cmdline
56+
desc = "Parser of GEOSX command line"
57+
parser = argparse.ArgumentParser("GEOSX command line parser",
58+
allow_abbrev=False,
59+
description=desc)
60+
61+
parser.add_argument("--input", "-i", "--xml",
62+
type=str, dest="xml", default=None,
63+
help="XML input file")
64+
parser.add_argument("--restart", "-r",
65+
dest="restart", type=str, default=None,
66+
help="Target restart filename")
67+
parser.add_argument("-x-partitions", "-x",
68+
type=int, dest="xpart", default=None,
69+
help="Number of partitions in the x direction.")
70+
parser.add_argument("-y-partitions", "-y",
71+
type=int, dest="ypart", default=None,
72+
help="Number of partitions in the y direction.")
73+
parser.add_argument("-z-partitions", "-z",
74+
type=int, dest="zpart", default=None,
75+
help="Number of partitions in the z direction.")
76+
parser.add_argument("--output", "-o",
77+
type=str, dest="outputdir", default=None,
78+
help="Directory to put the output files")
79+
parser.add_argument("--use-nonblocking", "-b", default=None,
80+
dest="non_blocking", action="store_true",
81+
help="Use non-blocking MPI communication")
82+
parser.add_argument("--name", "-n",
83+
dest="name", default=None,
84+
help="Name of the problem, used for output")
85+
parser.add_argument("--suppress-pinned", "-s",
86+
dest="supp_pinned", action="store_true", default=None,
87+
help="Suppress usage of pinned memory for MPI communication buffers")
88+
parser.add_argument("--timers", "-t",
89+
type=str, dest="timers", default=None,
90+
help="String specifying the type of timer output")
91+
parser.add_argument("--trace-data-migration",
92+
dest="trace_data_mig", action="store_true", default=None,
93+
help="Trace host-device data migration")
94+
parser.add_argument("--pause-for",
95+
dest="pause_for", default=None,
96+
help="Pause geosx for a given number of seconds before starting execution")
97+
98+
return vars(parser.parse_known_args(cmdline)[0])
99+
100+
101+
def updateArg(self, optionName=None, newValue=None):
102+
"""
103+
Update the GEOSX initialization arguments
104+
105+
Parameters
106+
----------
107+
optionName : str
108+
Name of the option to update
109+
newValue : str
110+
New value for the argument to be updated.
111+
112+
Returns
113+
-------
114+
bool : True if the option has been (found and) updated. False otherwise.
115+
"""
116+
if optionName is not None:
117+
if self.options[optionName] != newValue:
118+
self.options.update({optionName: newValue})
119+
return True
120+
121+
return False
122+
123+
124+
def getCommandLine(self):
125+
"""
126+
Return the command line specific to GEOSX initialization
127+
128+
Returns
129+
--------
130+
cl : list of str
131+
List containing all the options requested
132+
"""
133+
cl = [""]
134+
for opt, val in self.options.items():
135+
if val is not None:
136+
ab = GeosxAbbrevOption().getAbbrv(opt)
137+
cl += [ab]
138+
if not isinstance(self.options[opt], bool):
139+
cl += [str(self.options[opt])]
140+
141+
return cl
142+
143+
144+
class GeosxAbbrevOption:
145+
"""
146+
Class containing GEOSX command line options abbreviations
147+
148+
Attributes
149+
----------
150+
abbrvDict : dict
151+
Dict containing lists of abbreviations for GEOSX command line options
152+
"""
153+
def __init__(self):
154+
self.abbrvDict = {"xml": ("--input", "-i"),
155+
"restart": ("--restart", "r"),
156+
"xpart": ("-x-partitions", "-x"),
157+
"ypart": ("-y-partitions", "-y"),
158+
"zpart": ("-z-partitions", "-z"),
159+
"non_blocking": ("--use-non-blocking", "-b"),
160+
"name": ("--name", "-n"),
161+
"supp_pinned": ("--suppress-pinned"),
162+
"outputdir": ("--output", "-o"),
163+
"timers": ("--timers", "-t"),
164+
"trace_data_mig": ("--trace-data-migration"),
165+
"pause_for": ("--pause-for")
166+
}
167+
168+
def getAbbrv(self, optionName=None):
169+
"""
170+
Returns the abbreviation corresponding to the requested option
171+
172+
Parameters
173+
----------
174+
optionName : str
175+
Name of the requested option
176+
177+
Returns
178+
-------
179+
str : Requested abbreviations
180+
"""
181+
return self.abbrvDict[optionName][-1]
182+
183+
184+
def getAllAbbrv(self, optionName=None):
185+
"""
186+
Returns the abbreviation corresponding to the requested option
187+
188+
Parameters
189+
----------
190+
optionName : str
191+
Name of the requested option
192+
193+
Returns
194+
-------
195+
list of str :
196+
Requested abbreviations
197+
"""
198+
try:
199+
return self.abbrvDict[optionName]
200+
201+
except:
202+
return [""]
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# ------------------------------------------------------------------------------------------------------------
2+
# SPDX-License-Identifier: LGPL-2.1-only
3+
#
4+
# Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
5+
# Copyright (c) 2018-2024 Total, S.A
6+
# Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
7+
# Copyright (c) 2023-2024 Chevron
8+
# Copyright (c) 2019- GEOS/GEOSX Contributors
9+
# Copyright (c) 2019- INRIA project-team Makutu
10+
# All rights reserved
11+
#
12+
# See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13+
# ------------------------------------------------------------------------------------------------------------
14+
15+
import os
16+
from xml.etree import cElementTree as ET
17+
import xmltodict
18+
from re import findall
19+
from ..mesh.InternalMesh import InternalMesh
20+
from ..mesh.VtkMesh import VTKMesh
21+
22+
23+
class XML():
24+
def __init__(self, xmlFile):
25+
self.filename = xmlFile
26+
27+
self.tree = ET.parse(xmlFile)
28+
root = self.tree.getroot()
29+
to_string = ET.tostring(root, method='xml')
30+
31+
self.outputs = None
32+
33+
root = xmltodict.parse(to_string, attr_prefix="" ,dict_constructor=dict)
34+
for k, v in root['Problem'].items():
35+
words = findall('[A-Z][^A-Z]*', k)
36+
words[0] = words[0].lower()
37+
attr = ""
38+
for word in words:
39+
attr += word
40+
setattr(self, attr, v)
41+
42+
43+
44+
def updateSolvers(self, solverName, **kwargs):
45+
root = self.tree.getroot()
46+
solver = root.find("./Solvers/"+solverName)
47+
for k, v in kwargs.items():
48+
if k in solver.attrib:
49+
solver.set(k, v)
50+
self.solvers[solverName].update({k:str(v)})
51+
52+
53+
def updateMesh(self, **kwargs):
54+
root = self.tree.getroot()
55+
mesh = root.find("./Mesh//")
56+
for k, v in kwargs.items():
57+
if k in mesh.attrib:
58+
mesh.set(k, v)
59+
self.mesh[mesh.tag].update({k:str(v)})
60+
61+
62+
def updateGeometry(self, boxname, **kwargs):
63+
root = self.tree.getroot()
64+
geometry = root.find("./Geometry//*[@name="+boxname+"]")
65+
66+
for i in len(self.geometry[geometry.tag]):
67+
box = self.geometry[geometry.tag][i]
68+
if boxname == box["name"]:
69+
break
70+
71+
for k, v in kwargs.items():
72+
if k in geometry.attrib:
73+
geometry.set(k, v)
74+
self.geometry[geometry.tag][i].update({k:str(v)})
75+
76+
77+
def getMeshObject(self):
78+
79+
if "InternalMesh" in self.mesh.keys():
80+
#Not working properly for now
81+
return InternalMesh(self)
82+
83+
elif "VTKMesh" in self.mesh.keys():
84+
vtkFile = self.mesh["VTKMesh"]["file"]
85+
if not os.path.isabs(vtkFile):
86+
vtkFile = os.path.join(os.path.split(self.filename)[0], vtkFile)
87+
return VTKMesh(vtkFile)
88+
89+
def getAttribute(self, parentElement, attributeTag):
90+
if parentElement == "root":
91+
pElement = self.tree.find(f"./[@{attributeTag}]")
92+
else:
93+
pElement = self.tree.find(f"./*/{parentElement}/[@{attributeTag}]")
94+
95+
return pElement.get(attributeTag)
96+
97+
98+
def getSolverType(self):
99+
return [k for k in self.solvers.keys() if k[0].isupper()]
100+
101+
102+
def getSourcesAndReceivers(self):
103+
solverType = self.getSolverType()
104+
if len(solverType) > 1:
105+
pass
106+
else:
107+
src = self.getAttribute(f"{solverType[0]}", "sourceCoordinates")
108+
src = eval(src.replace("{", "[").replace("}", "]"))
109+
110+
rcv = self.getAttribute(f"{solverType[0]}", "receiverCoordinates")
111+
rcv = eval(rcv.replace("{", "[").replace("}", "]"))
112+
113+
return src, rcv
114+
115+
116+
def exportToXml(self, filename=None):
117+
if filename is None:
118+
self.tree.write(self.filename)
119+
else:
120+
self.tree.write(filename)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# ------------------------------------------------------------------------------------------------------------
2+
# SPDX-License-Identifier: LGPL-2.1-only
3+
#
4+
# Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
5+
# Copyright (c) 2018-2024 Total, S.A
6+
# Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
7+
# Copyright (c) 2023-2024 Chevron
8+
# Copyright (c) 2019- GEOS/GEOSX Contributors
9+
# Copyright (c) 2019- INRIA project-team Makutu
10+
# All rights reserved
11+
#
12+
# See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13+
# ------------------------------------------------------------------------------------------------------------
14+
15+
"""Input handle"""
16+
17+
from .GeosxArgs import GeosxArgs, GeosxAbbrevOption
18+
from .Xml import XML

0 commit comments

Comments
 (0)