Skip to content

Commit

Permalink
Merge pull request #144 from keurfonluu/devel
Browse files Browse the repository at this point in the history
Fixes and enhancements
  • Loading branch information
keurfonluu authored Aug 8, 2023
2 parents 779b66c + e987c44 commit 1ab3a85
Show file tree
Hide file tree
Showing 38 changed files with 1,114 additions and 793 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ TOUGH simulation output can also be imported into Python as a list of *namedtupl
- ``toughio-co2tab``: copy file *CO2TAB* to the target directory,
- ``toughio-export``: export TOUGH simulation results to a file for visualization (VTK, VTU, Tecplot or XDMF),
- ``toughio-extract``: extract results from TOUGH main output file and reformat as a TOUGH3 element or connection output file (mostly useful for TOUGH2 output *before* calling ``toughio-export``),
- ``toughio-merge``: merge input file, MESH and/or INCON into a single file (for storage or sharing),
- ``toughio-merge``: merge input file, GENER and/or MESH and/or INCON into a single file,
- ``toughio-save2incon``: convert a *SAVE* file to an *INCON* file (mostly useful to automatically restart a simulation and reset the counters).

Contributing
Expand Down
1 change: 1 addition & 0 deletions doc/source/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ API Reference

io
mesh
run
rpcap
cli

Expand Down
6 changes: 4 additions & 2 deletions doc/source/api/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ Input parameters
Simulation outputs
------------------

.. autofunction:: toughio.read_history

.. autofunction:: toughio.read_output

.. autofunction:: toughio.read_table

.. autofunction:: toughio.write_h5

.. autofunction:: toughio.write_output

.. autofunction:: toughio.register_output
4 changes: 4 additions & 0 deletions doc/source/api/run.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Run
===

.. autofunction:: toughio.run
74 changes: 47 additions & 27 deletions doc/source/guide/input.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ A TOUGH input file is defined as follows:
"connections": dict,
"initial_conditions": dict,
"meshmaker": dict,
"minc": dict,
"chemical_properties": dict,
"non_condensible_gas": list[str],
"start": bool,
Expand Down Expand Up @@ -365,6 +366,8 @@ The keyword ``"type"`` denotes the type of mesh to generate. The following value
- ``"rz2d"``
- ``"rz2dl"``

Note that the keyword ``"angle"`` is only used when ``"type"`` is ``"xyz"``.

If ``"type"`` is set to ``"xyz"``, each dictionary in ``"parameters"`` is defined as follows:

.. code-block::
Expand Down Expand Up @@ -397,6 +400,23 @@ The keyword ``"type"`` denotes here the type of increments to generate. The foll
- ``"layer"``: keyword ``"thicknesses"`` is required


MINC
****

MINC parameters are defined in a separate dictionary from ``"meshmaker"`` by the keyword ``"minc"``, and is organized as follows:

.. code-block::
{
"type": str,
"dual": str,
"n_minc": int,
"where": str,
"parameters": list[float],
"volumes": list[float],
}
TMVOC
*****

Expand Down Expand Up @@ -449,6 +469,33 @@ Non-condensible gases (block NCGAS) can be listed using keyword ``"non_condensib
For TMVOC input files, the argument ``eos="tmvoc"`` **must** be provided to the functions :func:`toughio.read_input` and :func:`toughio.write_input`.


Array dimensions (iTOUGH2)
**************************

iTOUGH2 allows users to provide array dimensions if an array is insufficiently dimensioned. An additional keyword ``"array_dimensions"`` can be used to specify such values, as follows:

.. code-block::
"array_dimensions": {
"n_rocks": int,
"n_times": int,
"n_generators": int,
"n_rates": int,
"n_increment_x": int,
"n_increment_y": int,
"n_increment_z": int,
"n_increment_rad": int,
"n_properties": int,
"n_properties_times": int,
"n_regions": int,
"n_regions_parameters": int,
"n_ltab": int,
"n_rpcap": int,
"n_elements_timbc": int,
"n_timbc": int,
}
TOUGHREACT (flow.inp)
---------------------

Expand Down Expand Up @@ -537,33 +584,6 @@ The permeability of an element called ``"AAA00"`` is defined as follows:
}
Array dimensions (iTOUGH2)
**************************

iTOUGH2 allows users to provide array dimensions if an array is insufficiently dimensioned. An additional keyword ``"array_dimensions"`` can be used to specify such values, as follows:

.. code-block::
"array_dimensions": {
"n_rocks": int,
"n_times": int,
"n_generators": int,
"n_rates": int,
"n_increment_x": int,
"n_increment_y": int,
"n_increment_z": int,
"n_increment_rad": int,
"n_properties": int,
"n_properties_times": int,
"n_regions": int,
"n_regions_parameters": int,
"n_ltab": int,
"n_rpcap": int,
"n_elements_timbc": int,
"n_timbc": int,
}
TOUGHREACT (solute.inp)
-----------------------

Expand Down
20 changes: 20 additions & 0 deletions tasks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import glob
import os
import shutil
import tarfile

from invoke import task

Expand Down Expand Up @@ -70,3 +71,22 @@ def isort(c):
@task
def format(c):
c.run("invoke isort black docstring")


@task
def tar(c):
patterns = [
"__pycache__",
]

def filter(filename):
for pattern in patterns:
if filename.name.endswith(pattern):
return None

return filename

with tarfile.open("toughio.tar.gz", "w:gz") as tf:
tf.add("toughio", arcname="toughio/toughio", filter=filter)
tf.add("pyproject.toml", arcname="toughio/pyproject.toml")
tf.add("setup.cfg", arcname="toughio/setup.cfg")
2 changes: 0 additions & 2 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
output_eleme = [
toughio.Output(
"element",
None,
float(time),
np.array([f"AAA0{i}" for i in range(10)]),
{
Expand All @@ -91,7 +90,6 @@
output_conne = [
toughio.Output(
"connection",
None,
float(time),
np.array([[f"AAA0{i}", f"AAA0{i}"] for i in range(10)]),
{
Expand Down
32 changes: 32 additions & 0 deletions tests/test_h5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pathlib

import h5py
import helpers

import toughio


def test_h5():
this_dir = pathlib.Path(__file__).parent
path = this_dir / "support_files" / "outputs"
filename = helpers.tempdir("output.h5")

toughio.write_h5(
filename=filename,
elements=path / "OUTPUT_ELEME.csv",
connections=path / "OUTPUT_CONNE.csv",
element_history={"A1912": path / "FOFT_A1912.csv"},
connection_history={"A1912": path / "FOFT_A1912.csv"},
generator_history={"A1162": path / "GOFT_A1162.csv"},
rock_history={"ROCK": path / "ROFT.csv"},
)

with h5py.File(filename, "r") as f:
assert list(f.keys()) == [
"connection_history",
"connections",
"element_history",
"elements",
"generator_history",
"rock_history",
]
35 changes: 33 additions & 2 deletions tests/test_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,8 +767,10 @@ def test_meshm_xyz():
assert helpers.allclose(parameters_ref, parameters, atol=1.0e-4)


@pytest.mark.parametrize("layer", [True, False])
def test_meshm_rz2d(layer):
@pytest.mark.parametrize(
"layer, minc", [(True, False), (False, False), (True, True), (False, True)]
)
def test_meshm_rz2d(layer, minc):
parameters_ref = {
"meshmaker": {
"type": "rz2dl" if layer else "rz2d",
Expand All @@ -795,6 +797,35 @@ def test_meshm_rz2d(layer):
],
}
}

if minc:
parameters_ref["minc"] = {
"type": helpers.random_string(5),
"dual": helpers.random_string(5),
"n_minc": np.random.randint(100) + 1,
"where": helpers.random_string(4),
"parameters": np.random.rand(7),
"volumes": np.random.rand(np.random.randint(100) + 1),
}

parameters = write_read(parameters_ref)

assert helpers.allclose(parameters_ref, parameters, atol=1.0e-4)


def test_minc():
n_volume = np.random.randint(100) + 1

parameters_ref = {
"minc": {
"type": helpers.random_string(5),
"dual": helpers.random_string(5),
"n_minc": np.random.randint(100) + 1,
"where": helpers.random_string(4),
"parameters": np.random.rand(7),
"volumes": np.random.rand(n_volume),
}
}
parameters = write_read(parameters_ref)

assert helpers.allclose(parameters_ref, parameters, atol=1.0e-4)
Expand Down
18 changes: 9 additions & 9 deletions tests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@


@pytest.mark.parametrize(
"filename, filename_ref",
"filename, filename_ref, file_format",
[
("OUTPUT_ELEME.csv", "SAVE.out"),
("OUTPUT_ELEME.tec", "SAVE.out"),
("OUTPUT_ELEME_PETRASIM.csv", "SAVE.out"),
("OUTPUT.out", "SAVE.out"),
("OUTPUT_6.out", "SAVE_6.out"),
("OUTPUT_ELEME.csv", "SAVE.out", "csv"),
("OUTPUT_ELEME.tec", "SAVE.out", "tecplot"),
("OUTPUT_ELEME_PETRASIM.csv", "SAVE.out", "petrasim"),
("OUTPUT.out", "SAVE.out", "tough"),
("OUTPUT_6.out", "SAVE_6.out", "tough"),
],
)
def test_output_eleme(filename, filename_ref):
def test_output_eleme(filename, filename_ref, file_format):
this_dir = os.path.dirname(os.path.abspath(__file__))
filename = os.path.join(this_dir, "support_files", "outputs", filename)
outputs = toughio.read_output(filename)
Expand All @@ -48,10 +48,10 @@ def test_output_eleme(filename, filename_ref):
assert time_ref == output.time
assert (
save.labels.tolist() == output.labels.tolist()
if output.format in {"csv", "petrasim", "tough"}
if file_format in {"csv", "petrasim", "tough"}
else len(output.labels) == 0
)
if output.format != "tough":
if file_format != "tough":
assert keys_ref == sorted(list(output.data))

assert helpers.allclose(save.data["X1"], outputs[-1].data["PRES"])
Expand Down
2 changes: 1 addition & 1 deletion toughio/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.12.2
1.13.0
4 changes: 4 additions & 0 deletions toughio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
register_input,
register_output,
register_table,
write_h5,
write_input,
write_output,
)
Expand All @@ -18,6 +19,7 @@
from ._mesh import register as register_mesh
from ._mesh import write as write_mesh
from ._mesh import write_time_series
from ._run import run

__all__ = [
"Mesh",
Expand All @@ -31,6 +33,7 @@
"read_input",
"read_output",
"read_table",
"write_h5",
"write_input",
"write_output",
"from_meshio",
Expand All @@ -42,6 +45,7 @@
"relative_permeability",
"capillarity",
"convert_labels",
"run",
"_cli",
"__version__",
]
18 changes: 3 additions & 15 deletions toughio/_cli/_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,13 @@ def export(argv=None):
output = output[args.time_step]
else:
output = output[-1]
input_format = output.format
labels = output.labels
else:
input_format = output[-1].format
labels = output[-1].labels
print(" Done!")

# Display warning if mesh is provided but input file format is 'tecplot'
# Continue as if mesh was not provided
if input_format == "tecplot":
with_mesh = False
msg = (
"Cannot use mesh file with Tecplot TOUGH output, inferring dimensionality"
if args.mesh
else "Inferring dimensionality"
)
else:
with_mesh = bool(args.mesh)
msg = "Mesh file not specified, inferring dimensionality"
with_mesh = bool(args.mesh)
msg = "Mesh file not specified, inferring dimensionality"

# Triangulate or voxelize if no mesh
voxelized = False
Expand Down Expand Up @@ -225,7 +213,7 @@ def _get_parser():
parser.add_argument(
"infile",
type=str,
help="TOUGH output file",
help="TOUGH input file",
)

# Mesh file
Expand Down
2 changes: 0 additions & 2 deletions toughio/_cli/_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ def extract(argv=None):

# Read TOUGH output file
output = read_output(args.infile, connection=args.connection)
if output[-1].format != "tough":
raise ValueError(f"Invalid TOUGH output file '{args.infile}'.")

try:
if not args.connection:
Expand Down
Loading

0 comments on commit 1ab3a85

Please sign in to comment.