|
| 1 | +############################################################################### |
| 2 | +Python Input Processing: pygeos |
| 3 | +############################################################################### |
| 4 | + |
| 5 | +Pygeos is a python-based module designed to construct input files for GEOSX. |
| 6 | +It allows users to create xml files that include child files, parameters, units, and symbolic math. |
| 7 | +It also includes helper-functions for validating input files and constructing tables. |
| 8 | + |
| 9 | + |
| 10 | +Setup |
| 11 | +================================= |
| 12 | +The pygeos module is located here: `src/coreComponents/python/modules/pygeos_package` . |
| 13 | +The module is compabitable with python 2/3, and its dependencies include the `numpy` and `lxml` packages. |
| 14 | +It can easily be installed using tools such as pip: |
| 15 | + |
| 16 | +`pip install src/coreComponents/python/modules/pygeos_package` |
| 17 | + |
| 18 | + |
| 19 | +Virtual Python Environment |
| 20 | +--------------------------------- |
| 21 | + |
| 22 | +On systems with a shared python installation, we reccomend that users install the package within a virtual python environment. |
| 23 | +A script designed to automatically setup a virtual environment, install the dependencies, and install pygeos is included in `scripts/setupVirtualPythonEnvironment.bash` . |
| 24 | +The options for the script include: |
| 25 | + |
| 26 | +- -p/--python_root : This specifies the root path for the parent python environment |
| 27 | +- -o/--output_path : This specifies location where the virtual environment will be installed |
| 28 | + |
| 29 | +The default settings for the script will build a virtual environment for the python-3.6.4 installation on LC systems, and will place the environment in `~/Python/virtual/geosx` |
| 30 | + |
| 31 | +To use the load the python environment, run the following: `source ~/Python/virtual/geosx/bin/activate` . |
| 32 | +Within the environment, the commands `python` and `pip` will point to the correct versions. |
| 33 | +To exit the virtual environment, run the command: `deactivate` . |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +XML Preprocessing |
| 38 | +================================= |
| 39 | + |
| 40 | +The xml preprocessor is designed to take an raw input file, and generate an new file that can be directly read by GEOSX. |
| 41 | +The syntax for the advanced xml format is given below. |
| 42 | +During the processing the order of operations are: |
| 43 | + |
| 44 | +1) Merging any included xml files into the root structure |
| 45 | +2) Substituting in any parameters |
| 46 | +3) Evaluating unit strings |
| 47 | +4) Evaluating symbolic math |
| 48 | +5) Error checking and validation |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +Use Example |
| 53 | +------------------------------ |
| 54 | + |
| 55 | +`pygeos.preprocessGEOSXML(input, schema='')` is used to process the input xml. |
| 56 | +The name of the newly generated, preprocessed xml file will be returned by this function call. |
| 57 | +By default it will have the name 'prep_' + a randomly generated string. |
| 58 | + |
| 59 | +The following is a example python script that will read process an xml file specified via the command line. |
| 60 | + |
| 61 | +.. code-block:: python |
| 62 | +
|
| 63 | + import sys |
| 64 | + import pygeos |
| 65 | +
|
| 66 | + new_filename = pygeos.preprocessGEOSXML(sys.argv[1]) |
| 67 | + print(new_filename) |
| 68 | +
|
| 69 | +
|
| 70 | +
|
| 71 | +Including Child XML Files |
| 72 | +------------------------------ |
| 73 | +XML inputs can point to included children (these children can then include grandchildren and so on). |
| 74 | +During processing, these are recursively inserted into the root XML structure by the following scheme: |
| 75 | + |
| 76 | +- Merge two objects if: |
| 77 | + - At the root level and an object with the matching tag exists. |
| 78 | + - If the “name” attribute is present and a object with the matching tag and name exist. |
| 79 | + - Any preexisting attributes are overwritten by the donor. |
| 80 | +- Otherwise append the xml structure with the target. |
| 81 | + |
| 82 | + |
| 83 | +.. code-block:: xml |
| 84 | +
|
| 85 | + <Included> |
| 86 | + <File name='/path/to/included_a.xml'/> |
| 87 | + <File name='/path/to/included_b.xml'/> |
| 88 | + </Included> |
| 89 | +
|
| 90 | +
|
| 91 | +
|
| 92 | +Parameters |
| 93 | +------------------------------ |
| 94 | +Parameters are a convenient way to build a configurable and human-readable input xml. |
| 95 | +They are defined via a block in the xml structure. |
| 96 | +Parameter names may only include upper/lower case letters and underscores (to avoid conflicts with symbolic math). |
| 97 | +Parameters may have any value: |
| 98 | + |
| 99 | +- Path to a file |
| 100 | +- Numbers |
| 101 | +- A symbolic expression |
| 102 | +- Other parameters |
| 103 | +- Etc. |
| 104 | + |
| 105 | + |
| 106 | +They can be used in any field within in the xml file (except in Includes) as follows: |
| 107 | + |
| 108 | +- $x_par |
| 109 | +- $:x_par |
| 110 | +- $x_par$ |
| 111 | +- $:x_par$ |
| 112 | + |
| 113 | + |
| 114 | +For Example: |
| 115 | + |
| 116 | +.. code-block:: xml |
| 117 | +
|
| 118 | + <Parameters> |
| 119 | + <Parameter name='x' value='5'/> |
| 120 | + <Parameter name='y' value='5'/> |
| 121 | + </Parameters> |
| 122 | + <Partition> |
| 123 | + <SpatialPartition xPar='$x$' yPar='$y$' zPar='1'/> |
| 124 | + </Partition> |
| 125 | +
|
| 126 | +
|
| 127 | +Units |
| 128 | +------------------------------ |
| 129 | +By default, input values are specified using SI units. |
| 130 | +In some cases, it is useful to override this behavior by explicitly specifying the units of the input. |
| 131 | +These are specified by appending a valid number with a unit definition in square braces. |
| 132 | +The unit manager supports most common units and SI prefixes, using both long- and abbreviated names (e.g.: c, centi, k, kilo, etc.) |
| 133 | +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]. |
| 134 | +Any (or no) amount of whitespace is allowed between the number and the unit bracket. |
| 135 | + |
| 136 | + |
| 137 | +Examples: |
| 138 | + |
| 139 | +.. code-block:: xml |
| 140 | +
|
| 141 | + <Parameters> |
| 142 | + <Parameter name='a' value='2[m]'/> |
| 143 | + <Parameter name='b' value='1.2 [cm]'/> |
| 144 | + <Parameter name='c' value='1.23e4 [bbl/day]'/> |
| 145 | + <Parameter name='d' value='1.23E-4 [km**2]'/> |
| 146 | + </Parameters> |
| 147 | +
|
| 148 | +
|
| 149 | +
|
| 150 | +Symbolic Math |
| 151 | +------------------------------ |
| 152 | +Input xml files can also include symbolic mathematical expressions. |
| 153 | +These are indicated with curly braces, and use a python syntax. |
| 154 | +Parameters and units are evaluated before symbolic expressions. |
| 155 | +Note: symbolic expressions are sanitized by removing any residual alpha characters, but this can be relaxed if more complicated function are needed. |
| 156 | + |
| 157 | + |
| 158 | +Examples: |
| 159 | + |
| 160 | +.. code-block:: xml |
| 161 | +
|
| 162 | + <Parameters> |
| 163 | + <Parameter name='a' value='2[m]'/> |
| 164 | + <Parameter name='b' value='1.2 [cm]'/> |
| 165 | + <Parameter name='c' value='1.23e4 [bbl/day]'/> |
| 166 | + <Parameter name='d' value='1.23E-4 [km**2]'/> |
| 167 | + </Parameters> |
| 168 | + <Nodesets> |
| 169 | + <Nodeset name='perf' xmin='{$a$ - 0.2*$b$} -1e6 -1e6' xmax='{$c$**2 / $d$} 1e6 1e6' /> |
| 170 | + </Nodesets> |
| 171 | +
|
| 172 | +
|
| 173 | +Validation |
| 174 | +------------------------------ |
| 175 | +Unmatched special characters ($, [, }, etc.) mean that parameters, units, or symbolic math were not specified correctly. |
| 176 | +If the code detects these, it will throw an error. |
| 177 | +The XML is validated against the input schema to check if all of the required field are present, and that input parameters match their expected types. |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | +Input Table Generation |
| 182 | +================================= |
| 183 | + |
| 184 | +The pygeos package also includes some tools to convert numpy arrays into GEOSX input tables, and vice-versa. |
| 185 | + |
| 186 | +The following example shows how to write a series of structrued tables using a list of spatial axes and a dictionary of table values: |
| 187 | + |
| 188 | +.. code-block:: python |
| 189 | +
|
| 190 | + import numpy as np |
| 191 | + import pygeos |
| 192 | +
|
| 193 | + # Config |
| 194 | + N = (10, 20, 30) |
| 195 | +
|
| 196 | + # Generate coordinate axes |
| 197 | + # These correspond to each axis of the numpy tables |
| 198 | + # The example function accepts up to four axes, and |
| 199 | + # will assign them the names x, y, z, and t in order |
| 200 | + spatial = [np.linspace(0, 1, N[0]), |
| 201 | + np.linspace(0, 1, N[1]), |
| 202 | + np.linspace(0, 1, N[2])] |
| 203 | +
|
| 204 | + # Generate the property dictionary |
| 205 | + properties = {'random_variable_A': np.randn(N), |
| 206 | + 'random_variable_B': np.randn(N), |
| 207 | + 'random_variable_C': np.randn(N)} |
| 208 | +
|
| 209 | + # Write the tables |
| 210 | + # The files will be written to the current directory, |
| 211 | + # and will have the .txt extension |
| 212 | + pygeos.writeGEOSTable(spatial, properties) |
| 213 | +
|
| 214 | +
|
| 215 | +The following shows how to read the geos tables written in the previous example: |
| 216 | + |
| 217 | +. code-block:: python |
| 218 | + |
| 219 | + import numpy as np |
| 220 | + import pygeos |
| 221 | + |
| 222 | + spatial_names = ['x', 'y', 'z'] |
| 223 | + property_names = ['random_variable_A', 'random_variable_B', 'random_variable_C'] |
| 224 | + |
| 225 | + spatial, properties = pygeos.readGEOSTable(spatial_names, property_names) |
| 226 | + |
| 227 | + |
| 228 | + |
| 229 | + |
0 commit comments