| 1 |
import grass.script as gs |
| 2 |
from grass.gunittest.case import TestCase |
| 3 |
from grass.gunittest.main import test |
| 4 |
|
| 5 |
|
| 6 |
class TestR3GWFlow(TestCase): |
| 7 |
"""Regression tests for the r3.gwflow GRASS module.""" |
| 8 |
|
| 9 |
tmp_3d_rasters = [] |
| 10 |
|
| 11 |
@classmethod |
| 12 |
def setUpClass(cls): |
| 13 |
"""Set up the test environment and create input raster maps.""" |
| 14 |
cls.use_temp_region() |
| 15 |
cls.runModule( |
| 16 |
"g.region", res=25, res3=25, t=75, b=0, n=100, s=0, w=0, e=100, flags="p3" |
| 17 |
) |
| 18 |
|
| 19 |
cls.runModule( |
| 20 |
"r3.mapcalc", |
| 21 |
expression="phead = if(row() == 1 && depth() == 2, 50, 40)", |
| 22 |
overwrite=True, |
| 23 |
) |
| 24 |
cls.runModule( |
| 25 |
"r3.mapcalc", |
| 26 |
expression="status = if(row() == 1 && depth() == 2, 2, 1)", |
| 27 |
overwrite=True, |
| 28 |
) |
| 29 |
cls.runModule( |
| 30 |
"r3.mapcalc", |
| 31 |
expression="well = if(row() == 2 && col() == 2 && depth() == 1, -0.25, 0)", |
| 32 |
overwrite=True, |
| 33 |
) |
| 34 |
cls.runModule("r3.mapcalc", expression="hydcond = 0.00025", overwrite=True) |
| 35 |
cls.runModule("r3.mapcalc", expression="syield = 0.0001", overwrite=True) |
| 36 |
cls.runModule("r3.mapcalc", expression="recharge = 0.0", overwrite=True) |
| 37 |
cls.tmp_3d_rasters.extend( |
| 38 |
["phead", "status", "well", "hydcond", "syield", "recharge"] |
| 39 |
) |
| 40 |
|
| 41 |
cls.runModule( |
| 42 |
"r3.mapcalc", |
| 43 |
expression="status_mixed = if(row() == 1, 2, if(col() > 2, 1, 0))", |
| 44 |
overwrite=True, |
| 45 |
) |
| 46 |
cls.tmp_3d_rasters.append("status_mixed") |
| 47 |
cls.runModule( |
| 48 |
"r3.mapcalc", |
| 49 |
expression="hydcond_variable = 0.0001 + (col() * 0.0001)", |
| 50 |
overwrite=True, |
| 51 |
) |
| 52 |
cls.tmp_3d_rasters.append("hydcond_variable") |
| 53 |
|
| 54 |
@classmethod |
| 55 |
def tearDownClass(cls): |
| 56 |
"""Clean up the test environment by removing created raster maps.""" |
| 57 |
cls.runModule("g.remove", flags="f", type="raster_3d", name=cls.tmp_3d_rasters) |
| 58 |
cls.del_temp_region() |
| 59 |
|
| 60 |
def _run_gwflow( |
| 61 |
self, |
| 62 |
suffix, |
| 63 |
phead="phead", |
| 64 |
status="status", |
| 65 |
hc_x="hydcond", |
| 66 |
hc_y="hydcond", |
| 67 |
hc_z="hydcond", |
| 68 |
yield_="syield", |
| 69 |
output=None, |
| 70 |
**kwargs, |
| 71 |
): |
| 72 |
""" |
| 73 |
Helper function to run r3.gwflow with flexible parameters. |
| 74 |
""" |
| 75 |
if output is None: |
| 76 |
output = f"gw_out_{suffix}" |
| 77 |
|
| 78 |
module_params = { |
| 79 |
"phead": phead, |
| 80 |
"status": status, |
| 81 |
"hc_x": hc_x, |
| 82 |
"hc_y": hc_y, |
| 83 |
"hc_z": hc_z, |
| 84 |
"yield": yield_, |
| 85 |
"output": output, |
| 86 |
"dtime": kwargs.get("dtime", 86400), |
| 87 |
"maxit": kwargs.get("maxit", 500), |
| 88 |
"error": kwargs.get("error", 1e-6), |
| 89 |
"solver": kwargs.get("solver", "cg"), |
| 90 |
} |
| 91 |
|
| 92 |
optional_params = [ |
| 93 |
"sink", |
| 94 |
"recharge", |
| 95 |
"velocity_x", |
| 96 |
"velocity_y", |
| 97 |
"velocity_z", |
| 98 |
"budget", |
| 99 |
] |
| 100 |
for param in optional_params: |
| 101 |
if param in kwargs: |
| 102 |
module_params[param] = kwargs[param] |
| 103 |
|
| 104 |
if "velocity_x" not in kwargs: |
| 105 |
module_params["velocity_x"] = f"vx_{suffix}" |
| 106 |
if "velocity_y" not in kwargs: |
| 107 |
module_params["velocity_y"] = f"vy_{suffix}" |
| 108 |
if "velocity_z" not in kwargs: |
| 109 |
module_params["velocity_z"] = f"vz_{suffix}" |
| 110 |
if "budget" not in kwargs: |
| 111 |
module_params["budget"] = f"budget_{suffix}" |
| 112 |
|
| 113 |
if "flags" in kwargs: |
| 114 |
module_params["flags"] = kwargs["flags"] |
| 115 |
|
| 116 |
self.assertModule("r3.gwflow", **module_params) |
| 117 |
self.assertRaster3dExists(output) |
| 118 |
|
| 119 |
self.tmp_3d_rasters.extend( |
| 120 |
[ |
| 121 |
module_params["budget"], |
| 122 |
module_params["velocity_x"], |
| 123 |
module_params["velocity_y"], |
| 124 |
module_params["velocity_z"], |
| 125 |
output, |
| 126 |
] |
| 127 |
) |
| 128 |
|
| 129 |
def test_gwflow_default_solver(self): |
| 130 |
"""Run basic simulation using default 'cg' solver and verify univariate statistics.""" |
| 131 |
self._run_gwflow("default", sink="well", recharge="recharge") |
| 132 |
expected_stats = { |
| 133 |
"min": 34.347307, |
| 134 |
"max": 50.0, |
| 135 |
"mean": 41.435054, |
| 136 |
"stddev": 2.914188, |
| 137 |
} |
| 138 |
self.assertRaster3dFitsUnivar("gw_out_default", expected_stats, precision=1e-6) |
| 139 |
|
| 140 |
def test_full_matrix_matches_sparse_close(self): |
| 141 |
"""Compare full matrix solver (-f) against sparse solver (cg) for numeric consistency.""" |
| 142 |
self._run_gwflow("cg_compare", solver="cg", sink="well", recharge="recharge") |
| 143 |
self._run_gwflow( |
| 144 |
"f_compare", solver="cholesky", flags="f", sink="well", recharge="recharge" |
| 145 |
) |
| 146 |
|
| 147 |
self.runModule( |
| 148 |
"r3.mapcalc", |
| 149 |
expression=("diffmap = abs(gw_out_f_compare - gw_out_cg_compare)"), |
| 150 |
overwrite=True, |
| 151 |
) |
| 152 |
stats = gs.parse_command("r3.univar", map="diffmap", flags="g") |
| 153 |
self.tmp_3d_rasters.append("diffmap") |
| 154 |
self.assertLess(float(stats["max"]), 1e-2) |
| 155 |
|
| 156 |
def test_gwflow_mixed_boundary_conditions(self): |
| 157 |
"""Validate solution under mixed boundary conditions (Dirichlet, Neumann, inactive).""" |
| 158 |
self._run_gwflow( |
| 159 |
"mixed", status="status_mixed", sink="well", recharge="recharge" |
| 160 |
) |
| 161 |
stats = gs.parse_command("r3.univar", map="gw_out_mixed", flags="g") |
| 162 |
self.assertIsNotNone(float(stats["n"])) |
| 163 |
expected_stats = { |
| 164 |
"min": 40.0, |
| 165 |
"max": 50.0, |
| 166 |
"mean": 41.597213, |
| 167 |
"stddev": 3.328858, |
| 168 |
} |
| 169 |
self.assertRaster3dFitsUnivar("gw_out_mixed", expected_stats, precision=1e-6) |
| 170 |
|
| 171 |
def test_gwflow_variable_hydraulic_conductivity(self): |
| 172 |
"""Check that spatially variable conductivity leads to higher velocity variation.""" |
| 173 |
self._run_gwflow("const_hc", sink="well", recharge="recharge") |
| 174 |
self._run_gwflow( |
| 175 |
"variable_hc", |
| 176 |
hc_x="hydcond_variable", |
| 177 |
hc_y="hydcond_variable", |
| 178 |
hc_z="hydcond_variable", |
| 179 |
sink="well", |
| 180 |
recharge="recharge", |
| 181 |
) |
| 182 |
vstats_const = gs.parse_command("r3.univar", map="vx_const_hc", flags="g") |
| 183 |
vstats_var = gs.parse_command("r3.univar", map="vx_variable_hc", flags="g") |
| 184 |
self.assertGreater(float(vstats_var["stddev"]), float(vstats_const["stddev"])) |
| 185 |
|
| 186 |
|
| 187 |
if __name__ == "__main__": |
| 188 |
test() |