Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(remove_model): remove_model method fix and tests (#1916) #1945

Merged
merged 2 commits into from
Sep 14, 2023
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
13 changes: 12 additions & 1 deletion autotest/regression/test_mf6.py
Original file line number Diff line number Diff line change
Expand Up @@ -3085,7 +3085,7 @@ def test028_create_tests_sfr(function_tmpdir, example_data_path):
delc=5000.0,
top=top,
botm=botm,
#idomain=idomain,
idomain=idomain,
filename=f"{model_name}.dis",
)
strt = testutils.read_std_array(os.path.join(pth, "strt.txt"), "float")
Expand Down Expand Up @@ -3989,6 +3989,17 @@ def test006_2models_different_dis(function_tmpdir, example_data_path):
assert gnc_data[0][1] == (0, 0)
assert gnc_data[0][2] == (0, 1, 1)

# test remove_model
sim2.remove_model(model_name_2)
sim2.write_simulation()
success, buff = sim2.run_simulation()
assert success
sim3 = MFSimulation.load(sim_ws=sim.sim_path)
assert sim3.get_model(model_name_1) is not None
assert sim3.get_model(model_name_2) is None
assert len(sim3.name_file.models.get_data()) == 1
assert sim3.name_file.exchanges.get_data() is None

sim.delete_output_files()


Expand Down
47 changes: 45 additions & 2 deletions autotest/test_mf6.py
Original file line number Diff line number Diff line change
Expand Up @@ -2244,14 +2244,14 @@ def test_multi_model(function_tmpdir):


@requires_exe("mf6")
def test_namefile_creation(tmpdir):
def test_namefile_creation(function_tmpdir):
test_ex_name = "test_namefile"
# build MODFLOW 6 files
sim = MFSimulation(
sim_name=test_ex_name,
version="mf6",
exe_name="mf6",
sim_ws=str(tmpdir),
sim_ws=str(function_tmpdir),
)

tdis_rc = [(6.0, 2, 1.0), (6.0, 3, 1.0), (6.0, 3, 1.0), (6.0, 3, 1.0)]
Expand Down Expand Up @@ -2293,3 +2293,46 @@ def test_namefile_creation(tmpdir):
except flopy.mf6.mfbase.FlopyException:
ex_happened = True
assert ex_happened


def test_remove_model(function_tmpdir, example_data_path):
# load a multi-model simulation
sim_ws = str(example_data_path / "mf6" / "test006_2models_mvr")
sim = MFSimulation.load(sim_ws=sim_ws, exe_name="mf6")

# original simulation should contain models:
# - 'parent', with files named 'model1.ext'
# - 'child', with files named 'model2.ext'
assert len(sim.model_names) == 2
assert "parent" in sim.model_names
assert "child" in sim.model_names

# remove the child model
sim.remove_model("child")

# simulation should now only contain the parent model
assert len(sim.model_names) == 1
assert "parent" in sim.model_names

# write simulation input files
sim.set_sim_path(function_tmpdir)
sim.write_simulation()

# there should be no input files for the child model
files = list(function_tmpdir.glob("*"))
assert not any("model2" in f.name for f in files)

# there should be no model or solver entry for the child model in the simulation namefile
lines = open(function_tmpdir / "mfsim.nam").readlines()
lines = [l.lower().strip() for l in lines]
assert not any("model2" in l for l in lines)
assert not any("child" in l for l in lines)

# there should be no exchanges either
exg_index = 0
for i, l in enumerate(lines):
if "begin exchanges" in l:
exg_index = i
elif exg_index > 0:
assert "end exchanges" in l
break
31 changes: 28 additions & 3 deletions flopy/mf6/mfsimbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -2183,11 +2183,36 @@ def remove_model(self, model_name):
Model name to remove from simulation

"""
# Remove model
# remove model
del self._models[model_name]

# TODO: Fully implement this
# Update simulation name file
# remove from solution group block
self._remove_from_all_solution_groups(model_name)

# remove from models block
models_recarray = self.name_file.models.get_data()
if models_recarray is not None:
new_records = []
for record in models_recarray:
if len(record) <= 2 or record[2] != model_name:
new_records.append(tuple(record))
self.name_file.models.set_data(new_records)

# remove from exchanges block
exch_recarray = self.name_file.exchanges.get_data()
if exch_recarray is not None:
new_records = []
for record in exch_recarray:
model_in_record = False
if len(record) > 2:
for item in list(record)[2:]:
if item == model_name:
model_in_record = True
if not model_in_record:
new_records.append(tuple(record))
if len(new_records) == 0:
new_records = None
self.name_file.exchanges.set_data(new_records)

def is_valid(self):
"""
Expand Down