Skip to content

Commit

Permalink
Merge pull request #171 from keurfonluu/devel
Browse files Browse the repository at this point in the history
Support flag option for OFT blocks
  • Loading branch information
keurfonluu authored Aug 19, 2024
2 parents 3d6476d + 321e9b1 commit 589f304
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 37 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ jobs:
python-version: '3.9'
- name: Install OSMesa VTK
run: |
sudo apt update
sudo apt install libosmesa6-dev libgl1-mesa-dev retry --option="APT::Acquire::Retries=3"
pip install https://github.com/pyvista/pyvista-wheels/raw/main/vtk-osmesa-9.1.0-cp39-cp39-linux_x86_64.whl
pip install vtk-osmesa==9.3.0 --index-url https://gitlab.kitware.com/api/v4/projects/13/packages/pypi/simple
- name: Install libGLU for pygmsh
run: |
sudo apt update
sudo apt install libglu1
- name: Install toughio
run: |
Expand Down
6 changes: 6 additions & 0 deletions doc/source/api/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ Input parameters
Simulation outputs
------------------

.. autoclass:: toughio.ElementOutput
:members:

.. autoclass:: toughio.ConnectionOutput
:members:

.. autofunction:: toughio.read_output

.. autofunction:: toughio.read_table
Expand Down
12 changes: 11 additions & 1 deletion tests/test_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,20 @@ def test_hyste(write_read):
)
def test_oft(write_read, oft, n):
parameters_ref = {
oft: [helpers.random_string(n) for _ in range(np.random.randint(10) + 1)]
oft: [
helpers.random_string(n),
helpers.random_string(n),
{"label": helpers.random_string(n)},
{"label": helpers.random_string(n), "flag": np.random.randint(10)},
]
}
parameters = write_read(parameters_ref)

if write_read == write_read_tough:
for i, v in enumerate(parameters_ref[oft]):
if not isinstance(v, dict):
parameters_ref[oft][i] = {"label": v}

assert helpers.allclose(parameters_ref, parameters)


Expand Down
2 changes: 1 addition & 1 deletion toughio/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.15.0
1.15.1
12 changes: 9 additions & 3 deletions toughio/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@
"MOMOP": "50S",
"TIMES": {1: "5d,5d,10f,10f", 2: ",".join(8 * ["10f"])},
"HYSTE": ",".join(3 * ["5d"]),
"FOFT": {5: "5s", 6: "6s", 7: "7s", 8: "8s", 9: "9s"},
"COFT": {5: "10s", 6: "12s", 7: "14s", 8: "16s", 9: "18s"},
"GOFT": {5: "5s", 6: "6s", 7: "7s", 8: "8s", 9: "9s"},
"FOFT": {5: "5s,5s,5d", 6: "6s,4s,5d", 7: "7s,3s,5d", 8: "8s,2s,5d", 9: "9s,1s,5d"},
"COFT": {
5: "10s,10s,5d",
6: "12s,8s,5d",
7: "14s,6s,5d",
8: "16s,4s,5d",
9: "18s,2s,5d",
},
"GOFT": {5: "5s,5s,5d", 6: "6s,4s,5d", 7: "7s,3s,5d", 8: "8s,2s,5d", 9: "9s,1s,5d"},
"ROFT": "5s,5s",
"GENER": {
0: ",".join(4 * ["14f"]),
Expand Down
5 changes: 5 additions & 0 deletions toughio/_io/input/tough/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@
"eps": 1.0e-6,
}

histories = {
"label": None,
"flag": None,
}

generators = {
"label": None,
"name": None,
Expand Down
7 changes: 6 additions & 1 deletion toughio/_io/input/tough/_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,12 @@ def _read_oft(f, oft, label_length):
while True:
if line.strip():
data = read_record(line, fmt[label_length])
history[key].append(label_format.format(data[0]))
tmp = {"label": label_format.format(data[0])}

if data[2] is not None:
tmp["flag"] = data[2]

history[key].append(tmp)

else:
break
Expand Down
57 changes: 35 additions & 22 deletions toughio/_io/input/tough/_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -1056,43 +1056,56 @@ def _write_hyste(parameters, space_between_values):
return out


@block("FOFT", multi=True)
def _write_foft(parameters, space_between_values):
"""Write FOFT block data."""
def _write_oft(oft, parameters, space_between_values):
"""Write FOFT, COFT, and GOFT blocks data."""
from ._common import histories

keys = {
"FOFT": "element",
"COFT": "connection",
"GOFT": "generator",
}

# Formats
fmt = block_to_format["FOFT"]
fmt = block_to_format[oft]
fmt = str2format(fmt[5])

values = [x for x in parameters["element_history"]]
out = write_record(values, fmt, space_between_values, multi=True)
out = []
for oft in parameters[f"{keys[oft]}_history"]:
data = deepcopy(histories)

if isinstance(oft, dict):
data.update(oft)

else:
data["label"] = oft

values = [
data["label"],
None,
data["flag"],
]
out += write_record(values, fmt, space_between_values)

return out


@block("FOFT", multi=True)
def _write_foft(parameters, space_between_values):
"""Write FOFT block data."""
return _write_oft("FOFT", parameters, space_between_values)


@block("COFT", multi=True)
def _write_coft(parameters, space_between_values):
"""Write COFT block data."""
# Format
fmt = block_to_format["COFT"]
fmt = str2format(fmt[5])

values = [x for x in parameters["connection_history"]]
out = write_record(values, fmt, space_between_values, multi=True)

return out
return _write_oft("COFT", parameters, space_between_values)


@block("GOFT", multi=True)
def _write_goft(parameters, space_between_values):
"""Write GOFT block data."""
# Format
fmt = block_to_format["GOFT"]
fmt = str2format(fmt[5])

values = [x for x in parameters["generator_history"]]
out = write_record(values, fmt, space_between_values, multi=True)

return out
return _write_oft("GOFT", parameters, space_between_values)


@block("ROFT", multi=True)
Expand Down
17 changes: 11 additions & 6 deletions toughio/_io/output/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,17 @@ def to_output(file_type, labels_order, headers, times, labels, data):
"Found duplicate connections. Fixing outputs by summing duplicate connections."
)

for output in outputs:
output.labels = list(connections)
output.data = {
k: np.array([v[idx].sum() for idx in connections.values()])
for k, v in output.data.items()
}
outputs = [
ConnectionOutput(
time=output.time,
data={
k: np.array([v[idx].sum() for idx in connections.values()])
for k, v in output.data.items()
},
labels=list(connections),
)
for output in outputs
]

if file_type == "element" and labels_order is not None:
outputs = [output[labels_order] for output in outputs]
Expand Down

0 comments on commit 589f304

Please sign in to comment.