Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions autotest/test_particlegroup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import tempfile
from pathlib import Path

import numpy as np

from flopy.modpath import ParticleData, ParticleGroup
Expand Down Expand Up @@ -51,3 +54,57 @@ def test_pgroup_release_data():
f"mp7: pgroup with releaseoption 3 returned "
f"len(releasetimes)={len(pgrd3.releasetimes)}. Should be {nripg3}"
)


def test_pgroup_write_with_release_timing():
"""Test that particle groups can be written to files with all release options."""
# create particles
partlocs = []
partids = []
nrow = 21
for i in range(nrow):
partlocs.append((0, i, 2))
partids.append(i)
pdata = ParticleData(partlocs, structured=True, particleids=partids)

with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = Path(tmpdir)

# Test releaseoption 1 (single release time)
pgrd1 = ParticleGroup(
particlegroupname="PG1",
particledata=pdata,
filename=None, # Write internally to test that path
releasedata=0.0,
)

# Test releaseoption 2 (time range with interval)
nripg2 = 10
ripg2 = 1.0
pgrd2 = ParticleGroup(
particlegroupname="PG2",
particledata=pdata,
filename=None,
releasedata=[nripg2, 0.0, ripg2],
)

# Test releaseoption 3 (explicit list of times).
# this triggers Util2d with None model
nripg3 = 7
pgrd3 = ParticleGroup(
particlegroupname="PG3",
particledata=pdata,
filename=None,
releasedata=[nripg3, np.array([0.0, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0])],
)

# Write each particle group to test all release options
for pg in [pgrd1, pgrd2, pgrd3]:
sim_file = tmpdir / f"{pg.particlegroupname}_test.sim"
with open(sim_file, "w") as f:
# This should not raise an AttributeError
pg.write(f, ws=str(tmpdir))

# Verify file was created and has content
assert sim_file.exists()
assert sim_file.stat().st_size > 0
2 changes: 1 addition & 1 deletion flopy/modpath/mp7particlegroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def write(self, fp=None, ws="."):
fp.write(f"{self.releasetimecount}\n")
# item 31
tp = self.releasetimes
v = Util2d(self, (tp.shape[0],), np.float32, tp, name="temp", locat=0)
v = Util2d(None, (tp.shape[0],), np.float32, tp, name="temp", locat=0)
fp.write(v.string)

# item 32
Expand Down
11 changes: 8 additions & 3 deletions flopy/utils/util_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ def __init__(self, u2d, python=None, fortran=None, array_free_format=None):
self._decimal = None
if array_free_format is not None:
self._freeformat_model = bool(array_free_format)
else:
elif u2d.model is not None:
self._freeformat_model = bool(u2d.model.array_free_format)
else:
# Default to free format when no model is available
self._freeformat_model = True

self.default_float_width = 15
self.default_int_width = 10
Expand Down Expand Up @@ -1868,7 +1871,9 @@ def _decide_how(self):
if self.vtype in [np.int32, np.float32]:
self._how = "constant"
# if a filename was passed in or external path was set
elif self._model.external_path is not None or self.vtype == str:
elif (
self._model is not None and self._model.external_path is not None
) or self.vtype == str:
if self.format.array_free_format:
self._how = "openclose"
else:
Expand Down Expand Up @@ -2710,7 +2715,7 @@ def parse_value(self, value):
if len(value.shape) == 3 and value.shape[0] == 1:
value = value[0]

if self.model.version == "mfusg":
if self.model is not None and self.model.version == "mfusg":
if self.shape != value.shape:
value = np.array([np.squeeze(value)])

Expand Down
Loading