|
| 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 | + |
0 commit comments