forked from MODFLOW-USGS/modflow6
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(bmi-ems): ExplicitModelSolution for PRT fixed for XMI/API calls (M…
…ODFLOW-USGS#1962) * fix(bmi-ems): ExplicitModelSolution for PRT fixed to work with XMI/API calls * fprettify * add test to make sure PRT runs with API * ruff * more ruff
- Loading branch information
1 parent
ae6cc66
commit c178700
Showing
5 changed files
with
172 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
""" | ||
This test runs the simulations in test_prt_budget.py, but uses the api | ||
to run the PRT test. | ||
""" | ||
|
||
from pathlib import Path | ||
|
||
import pytest | ||
from framework import TestFramework | ||
from modflowapi import ModflowApi | ||
from test_prt_budget import ( | ||
HorizontalCase, | ||
build_mp7_sim, | ||
build_prt_sim, | ||
check_output, | ||
) | ||
|
||
simname = "prt_libmf6" | ||
cases = [simname] | ||
|
||
|
||
def build_models(idx, test): | ||
# build MODFLOW 6 files | ||
ws = test.workspace | ||
name = cases[idx] | ||
|
||
gwf_sim = HorizontalCase.get_gwf_sim( | ||
test.name, test.workspace, test.targets["mf6"] | ||
) | ||
prt_sim = build_prt_sim( | ||
test.name, | ||
test.workspace, | ||
test.workspace / "prt", | ||
test.targets["libmf6"], | ||
) | ||
mp7_sim = build_mp7_sim( | ||
test.name, | ||
test.workspace / "mp7", | ||
test.targets["mp7"], | ||
gwf_sim.get_model(), | ||
) | ||
|
||
return gwf_sim, prt_sim, mp7_sim | ||
|
||
|
||
def api_func(exe, idx, model_ws=None): | ||
name = cases[idx].upper() | ||
if model_ws is None: | ||
model_ws = Path(".") | ||
|
||
output_file_path = model_ws / "mfsim.stdout" | ||
|
||
try: | ||
mf6 = ModflowApi(exe, working_directory=model_ws) | ||
except Exception as e: | ||
print("Failed to load " + exe) | ||
print("with message: " + str(e)) | ||
return False, open(output_file_path).readlines() | ||
|
||
# initialize the model | ||
try: | ||
mf6.initialize() | ||
except: | ||
return False, open(output_file_path).readlines() | ||
|
||
# time loop | ||
current_time = mf6.get_current_time() | ||
end_time = mf6.get_end_time() | ||
|
||
# model time loop | ||
idx = 0 | ||
while current_time < end_time: | ||
# get dt and prepare for non-linear iterations | ||
dt = mf6.get_time_step() | ||
mf6.prepare_time_step(dt) | ||
|
||
# convergence loop | ||
kiter = 0 | ||
mf6.prepare_solve() | ||
has_converged = mf6.solve() | ||
mf6.finalize_solve() | ||
|
||
# finalize time step and update time | ||
mf6.finalize_time_step() | ||
current_time = mf6.get_current_time() | ||
|
||
# increment counter | ||
idx += 1 | ||
|
||
# cleanup | ||
try: | ||
mf6.finalize() | ||
except: | ||
return False, open(output_file_path).readlines() | ||
|
||
# cleanup and return | ||
return True, open(output_file_path).readlines() | ||
|
||
|
||
@pytest.mark.parametrize("idx, name", enumerate(cases)) | ||
def test_mf6model(idx, name, function_tmpdir, targets): | ||
test = TestFramework( | ||
name=name, | ||
workspace=function_tmpdir, | ||
targets=targets, | ||
build=lambda t: build_models(idx, t), | ||
api_func=lambda exe, ws: api_func(exe, idx, ws), | ||
check=lambda t: check_output(idx, t), | ||
) | ||
test.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters