Skip to content

Commit 0a2fe87

Browse files
authored
Add advanced xml input examples, function tests (#990)
* Adding a complex hydraulic fracturing example * Adding integrated test that uses heterogeneous in-situ properties * Adding setters to functions for testing * Adding a simple 1D table function unit test * Adding 2D and 4D table unit tests * Adding a symbolic function unit test * Updating the new heterogeneous hf example * Fixing a bug in the event restart dt calculation * Adding a target to build pygeos using the same python executable as cmake * Updating the pygeos build target * Starting to write a hf tutorial * Modifying the heterogeneous hydrofracture example * Updating the pygeos cmake target * Changing the xml formatting cmake target depend on pygeos build * Updating pygeos git targets to prevent re-building of virtualenv * Formatting new xml files * Changing the name of pygeos to geosx_xml_tools to generalize, avoid future conflicts * Updating numerical methods block in new function example xml files * Adding a script-based entry into the xml formatter, applying it after xml processing * Installing the hdf5_wrapper package into the default virtual environment
1 parent f46215a commit 0a2fe87

File tree

18 files changed

+307
-277
lines changed

18 files changed

+307
-277
lines changed

geosx_xml_tools_package/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build
2+
*.egg-info
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
"""@package pygeos
1+
"""@package geosx_xml_tools
22
A python module that enables advanced xml features for GEOSX.
33
"""

pygeos_package/pygeos/__main__.py renamed to geosx_xml_tools_package/geosx_xml_tools/__main__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
"""Command line tools for pygeos"""
1+
"""Command line tools for geosx_xml_tools"""
22

33
import argparse
4-
from pygeos import xml_processor
4+
from geosx_xml_tools import xml_processor
55

66

77
def main():
8-
"""Entry point for the pygeos console script
8+
"""Entry point for the geosx_xml_tools console script
99
1010
@arg input Input file name
1111
@arg -o/--output Output filename (default = randomly generated string)
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
2+
.. _advanced_xml_features:
3+
4+
###############################################################################
5+
Advanced XML Features (geosx_xml_tools)
6+
###############################################################################
7+
8+
geosx_xml_tools is a python module that enables advanced xml features in GEOSX (parameters, units, symbolic math, etc.), and is used to format xml files.
9+
10+
11+
Setup
12+
=================================
13+
14+
The `geosx_xml_tools` package can be installed using the following command within the GEOSX build directory:
15+
16+
`make geosx_xml_tools`
17+
18+
During the installation step, two console scripts will be created: `preprocess_xml` and `format_xml`.
19+
These will be located within the GEOSX build/bin directory.
20+
Additional things to consider:
21+
22+
- The above make command will create a new virtual python environment to install the package. By default, the source python environment will be the the version used to run the `config-build.py` command. The version of python can be changed by specifying the `PYTHON_POST_EXECUTABLE` variable. To build the environment, the `virtualenv` package must be installed in the source distribution.
23+
- The `geosx_xml_tools` package depends on the `lxml` package. If `lxml` is missing from the parent environment, the install script will attempt to fetch an appropriate version from the internet.
24+
- The package may also be manually installed within an existing python distribution (this required administrative priviliges) via pip: `pip install src/coreComponents/python/modules/geosx_xml_tools_package`. In this case, the console scripts will be located in the python/bin directory
25+
26+
27+
Usage
28+
=================================
29+
30+
geosx_xml_tools can be used via the command-line or can be imported into a python script.
31+
32+
33+
Command-line xml formatting
34+
------------------------------
35+
36+
The following command will update the formatting of an existing xml file in-place:
37+
38+
`format_xml input_file.xml`
39+
40+
To update the formatting for all xml files located within the GEOSX repository, you can run the following command within the GEOSX build directory:
41+
42+
`make geosx_format_all_xml_files`
43+
44+
45+
Command-line xml preprocessing
46+
------------------------------
47+
48+
The following command will read an xml file, process any advanced xml features located within it, and write a new file that can be read by GEOSX:
49+
50+
`preprocess_xml input_file.xml`
51+
52+
The script returns the (randomly generated) name of the new xml file.
53+
Optional arguments for this script include:
54+
55+
- `-o/--output`: The desired name for the output file (otherwise, it is randomly generated)
56+
- `-s/--schema`: The location of a schema to validate the final .xml file
57+
- `-v/--verbose`: Increase module verbosity
58+
59+
For convenience, this script can be embedded within a call to GEOSX:
60+
61+
`srun -n 16 geosx -i \`preprocess_xml input_file.xml\` -x 4 -z 4`
62+
63+
64+
Script-based Example
65+
------------------------------
66+
67+
The geosx_xml_tools module can also be called from within a python script. For example:
68+
69+
.. code-block:: python
70+
71+
from geosx_xml_tools import xml_processor
72+
73+
initial_filename = 'input.xml'
74+
new_filename = output_name = xml_processor.process(initial_filename)
75+
76+
77+
78+
Advanced XML Features
79+
=================================
80+
81+
The xml preprocessor in geosx_xml_tools is designed to take a raw input file, and generate a new file that can be directly read by GEOSX.
82+
The syntax for the advanced XML format is given below.
83+
During the processing the order of operations are:
84+
85+
1) Merging any included XML files into the root structure
86+
2) Substituting in any parameters
87+
3) Evaluating unit strings
88+
4) Evaluating symbolic math
89+
5) Error checking and validation
90+
91+
92+
Including Child XML Files
93+
------------------------------
94+
XML inputs can point to included children (these children can then include grandchildren and so on).
95+
During processing, these are recursively inserted into the root XML structure by the following scheme:
96+
97+
- Merge two objects if:
98+
99+
- At the root level an object with the matching tag exists.
100+
- If the "name" attribute is present and an object with the matching tag and name exists.
101+
- Any preexisting attributes are overwritten by the donor.
102+
- Otherwise append the XML structure with the target.
103+
104+
105+
.. code-block:: xml
106+
107+
<Included>
108+
<File name='/path/to/included_a.xml'/>
109+
<File name='/path/to/included_b.xml'/>
110+
</Included>
111+
112+
113+
114+
Parameters
115+
------------------------------
116+
Parameters are a convenient way to build a configurable and human-readable input XML.
117+
They are defined via a block in the XML structure.
118+
Parameter names may only include upper/lower case letters and underscores (to avoid conflicts with symbolic math).
119+
Parameters may have any value:
120+
121+
- Path to a file
122+
- Numbers
123+
- A symbolic expression
124+
- Other parameters
125+
- Etc.
126+
127+
128+
They can be used in any field within in the XML file (except in Includes) as follows:
129+
130+
- $x_par$ (preferred)
131+
- $x_par
132+
- $:x_par
133+
- $:x_par$
134+
135+
136+
For Example:
137+
138+
.. code-block:: xml
139+
140+
<Parameters>
141+
<Parameter
142+
name="mu"
143+
value="0.005"/>
144+
<Parameter
145+
name="table_root"
146+
value="/path/to/table/root"/>
147+
</Parameters>
148+
149+
<Constitutive>
150+
<CompressibleSinglePhaseFluid
151+
name="water"
152+
defaultDensity="1000"
153+
defaultViscosity="$mu$"
154+
referencePressure="0.0"
155+
referenceDensity="1000"
156+
compressibility="5e-10"
157+
referenceViscosity="$mu$"
158+
viscosibility="0.0"/>
159+
</Constitutive>
160+
161+
<Functions>
162+
<TableFunction
163+
name="flow_rate"
164+
inputVarNames="{time}"
165+
coordinateFiles="{$table_root$/time_flow.geos}"
166+
voxelFile="$table_root$/flow.geos"
167+
interpolation="linear"/>
168+
</Functions>
169+
170+
171+
172+
Units
173+
------------------------------
174+
By default, input values are specified using SI units.
175+
In some cases, it is useful to override this behavior by explicitly specifying the units of the input.
176+
These are specified by appending a valid number with a unit definition in square braces.
177+
The unit manager supports most common units and SI prefixes, using both long- and abbreviated names (e.g.: c, centi, k, kilo, etc.).
178+
Units may include predefined composite units (dyne, N, etc.) or may be built up from sub-units using a python syntax (e.g.: [N], [kg*m/s**2]).
179+
Any (or no) amount of whitespace is allowed between the number and the unit bracket.
180+
181+
182+
Examples:
183+
184+
.. code-block:: xml
185+
186+
<Parameters>
187+
<Parameter name='a' value='2[m]'/>
188+
<Parameter name='b' value='1.2 [cm]'/>
189+
<Parameter name='c' value='1.23e4 [bbl/day]'/>
190+
<Parameter name='d' value='1.23E-4 [km**2]'/>
191+
</Parameters>
192+
193+
194+
195+
Symbolic Math
196+
------------------------------
197+
Input XML files can also include symbolic mathematical expressions.
198+
These are placed within pairs of backticks (\`), and use a python syntax.
199+
Parameters and units are evaluated before symbolic expressions.
200+
Note: symbolic expressions are sanitized before by removing any residual alpha characters, but this can be relaxed if more complicated function are needed.
201+
Also, while symbolic expressions are allowed within parameters, errors may occur if these are used in a way that leads to nested symbolic expressions.
202+
203+
204+
Examples:
205+
206+
.. code-block:: xml
207+
208+
<Parameters>
209+
<Parameter name='a' value='2[m]'/>
210+
<Parameter name='b' value='1.2 [cm]'/>
211+
<Parameter name='c' value='1.23e4 [bbl/day]'/>
212+
<Parameter name='d' value='1.23E-4 [km**2]'/>
213+
</Parameters>
214+
<Geometry>
215+
<Box
216+
name='perf'
217+
xMin='`$a$ - 0.2*$b$`, -1e6, -1e6'
218+
xMax='`$c$**2 / $d$`, 1e6, 1e6' />
219+
</Geometry>
220+
221+
222+
Validation
223+
------------------------------
224+
Unmatched special characters ($, [, \`, etc.) mean that parameters, units, or symbolic math were not specified correctly.
225+
If the code detects these, it will throw an error.
226+
The XML is validated against the input schema to check if all of the requireds field are present, and that input parameters match their expected types.
227+
228+

pygeos_package/pygeos/regex_tools.py renamed to geosx_xml_tools_package/geosx_xml_tools/regex_tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Tools for managing regular expressions in pygeos"""
1+
"""Tools for managing regular expressions in geosx_xml_tools"""
22

33
import re
44

File renamed without changes.
File renamed without changes.

pygeos_package/pygeos/tests/generate_test_xml.py renamed to geosx_xml_tools_package/geosx_xml_tools/tests/generate_test_xml.py

File renamed without changes.

pygeos_package/pygeos/tests/test_manager.py renamed to geosx_xml_tools_package/geosx_xml_tools/tests/test_manager.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import re
33
import os
44
import filecmp
5-
from pygeos import regex_tools, unit_manager, xml_processor
6-
from pygeos.tests import generate_test_xml
5+
from geosx_xml_tools import regex_tools, unit_manager, xml_processor
6+
from geosx_xml_tools.tests import generate_test_xml
77

88

99
# Create an instance of the unit manager
@@ -235,8 +235,9 @@ def setUpClass(cls):
235235
# Create a directory to hold tests
236236
cls.pwd = os.getcwd()
237237
rand_name = xml_processor.generate_random_name(suffix='')[:10]
238-
os.makedirs('./pygeos_tests_%s/included' % (rand_name), exist_ok=True)
239-
os.chdir('./pygeos_tests_%s' % (rand_name))
238+
# os.makedirs('./geosx_xml_tools_tests_%s/included' % (rand_name), exist_ok=True)
239+
os.makedirs('./geosx_xml_tools_tests_%s/included' % (rand_name))
240+
os.chdir('./geosx_xml_tools_tests_%s' % (rand_name))
240241

241242
# Generate test xml files to process
242243
generate_test_xml.generate_test_xml_files('.')

pygeos_package/pygeos/unit_manager.py renamed to geosx_xml_tools_package/geosx_xml_tools/unit_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Tools for managing units in GEOSX"""
22

33
import re
4-
from pygeos import regex_tools
4+
from geosx_xml_tools import regex_tools
55

66

77
class UnitManager():

0 commit comments

Comments
 (0)