Coverage for fixtures\mf6_modelrun_fixture.py: 100%
22 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-08 13:27 +0200
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-08 13:27 +0200
1from pathlib import Path
3import numpy as np
4from pandas import isnull
6import imod
9def assert_simulation_can_run(
10 simulation: imod.mf6.Modflow6Simulation, discretization_name: str, modeldir: Path
11):
12 """
13 Runs the simulation and asserts that computed heads are not NaN.
14 """
16 # Run simulation
17 simulation.write(
18 modeldir, binary=True
19 ) # write binary to create the disv.disv.grb file needed for postprocessing
20 simulation.write(modeldir, binary=False) # write into human readable files
22 simulation.run()
24 # get flowmodel name
25 flow_model_names = [
26 m for m in simulation if type(simulation[m]) is imod.mf6.GroundwaterFlowModel
27 ]
28 for flowmodel in flow_model_names:
29 # read the output generated by modflow
30 dis_outputfile = (
31 modeldir / flowmodel / f"{discretization_name}.{discretization_name}.grb"
32 )
33 head = imod.mf6.out.open_hds(
34 modeldir / flowmodel / f"{flowmodel}.hds",
35 dis_outputfile,
36 )
38 # filter output on idomain
39 idomain = simulation[flowmodel].domain
40 head = head.reindex_like(idomain, "nearest", 1e-5)
41 head = head.where(idomain == 1, other=0)
43 # Test that heads are not nan
44 assert not np.any(isnull(head.values))
47def assert_model_can_run(
48 model: imod.mf6.GroundwaterFlowModel, discretization_name: str, modeldir: Path
49):
50 """
51 This function creates a simulation given a flow model, and tries to run the simulation.
52 solver parameters and time discretization are given default values that might not work for every model.
53 """
55 simulation = imod.mf6.Modflow6Simulation("simulation_name")
56 simulation["GWF_1"] = model
57 # Define solver settings
58 simulation["solver"] = imod.mf6.Solution(
59 modelnames=["GWF_1"],
60 print_option="summary",
61 csv_output=False,
62 no_ptc=True,
63 outer_dvclose=1.0e-4,
64 outer_maximum=500,
65 under_relaxation=None,
66 inner_dvclose=1.0e-4,
67 inner_rclose=0.001,
68 inner_maximum=100,
69 linear_acceleration="cg",
70 scaling_method=None,
71 reordering_method=None,
72 relaxation_factor=0.97,
73 )
74 # Collect time discretization
75 simulation.create_time_discretization(
76 additional_times=["2000-01-01", "2000-01-02", "2000-01-03", "2000-01-04"]
77 )
79 assert_simulation_can_run(simulation, discretization_name, modeldir)