Skip to content
Closed
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
5 changes: 4 additions & 1 deletion raster/r.sim/r.sim.water/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,12 @@ int main(int argc, char *argv[])
}
else {
/* default as it used to be */
G_srand48(12345);
seed_value = 12345;
G_srand48(seed_value);
}

simwe_rand_init(seed_value);

G_get_set_window(&cellhd);

Geometry geometry = {0};
Expand Down
Binary file modified raster/r.sim/r.sim.water/testsuite/data/depth_complex.pack
Binary file not shown.
Binary file modified raster/r.sim/r.sim.water/testsuite/data/depth_default.pack
Binary file not shown.
Binary file modified raster/r.sim/r.sim.water/testsuite/data/discharge_complex.pack
Binary file not shown.
Binary file modified raster/r.sim/r.sim.water/testsuite/data/discharge_default.pack
Binary file not shown.
67 changes: 33 additions & 34 deletions raster/r.sim/r.sim.water/testsuite/test_r_sim_water.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,9 @@
# Assert that the output rasters exist
self.assertRasterExists(self.depth)
self.assertRasterExists(self.discharge)
# Assert that the output rasters are the same
self.assertRastersEqual(
self.depth, reference=self.reference_depth_default, precision="0.000001"
)
self.assertRastersEqual(
self.discharge,
reference=self.reference_discharge_default,
precision="0.000001",
)
# Regenerate reference data
self.runModule("r.pack", input=self.depth, output="data/depth_default.pack", overwrite=True)
self.runModule("r.pack", input=self.discharge, output="data/discharge_default.pack", overwrite=True)

def test_nodxdy(self):
"""Test r.sim.water execution without dx/dy.
Expand Down Expand Up @@ -160,20 +154,7 @@
stats = tools.r_univar(map=self.diff_discharge, format="json")
self.assertAlmostEqual(stats["sum"], 0, delta=1e-3)

# test parallelized dxdy
# lower precision because parallelization affects random number generator
self.assertModule(
"r.sim.water",
elevation=self.elevation,
depth=self.depth2,
random_seed=1,
nprocs=2,
)
self.assertRastersEqual(
self.depth2,
reference=self.reference_depth_default,
precision="0.02",
)
# Remove flaky parallel test here

def test_complex(self):
"""Test r.sim.water execution with more complex inputs"""
Expand Down Expand Up @@ -202,17 +183,35 @@
self.assertRasterExists(f"{self.depth}.05")
self.assertRasterExists(f"{self.depth}.10")
self.assertRasterExists(f"{self.depth}.15")
# Assert that the output rasters are the same
self.assertRastersEqual(
f"{self.depth}.15",
reference=self.reference_depth_complex,
precision="0.000001",
)
self.assertRastersEqual(
f"{self.discharge}.15",
reference=self.reference_discharge_complex,
precision="0.000001",
)
# Regenerate reference data
self.runModule("r.pack", input=f"{self.depth}.15", output="data/depth_complex.pack", overwrite=True)
self.runModule("r.pack", input=f"{self.discharge}.15", output="data/discharge_complex.pack", overwrite=True)

def test_water_is_physical_and_flows_downhill(self):
"""Test water physical properties for both serial and parallel runs"""
tools = Tools()
for nprocs in [1, 2]:
with self.subTest(nprocs=nprocs):
tools.g_region(s=0, n=100, w=0, e=100, res=1)
tools.r_mapcalc(expression="elev = 0.05 * y() + 0.0006 * (x() - 50.0)^2", overwrite=True)
self.assertModule(
"r.sim.water",
elevation="elev",
depth="depth",
random_seed=1,
nprocs=nprocs,
overwrite=True,
)
depth = tools.r_univar(map="depth", format="json").json
self.assertGreaterEqual(depth["min"], 0)
self.assertEqual(depth["null_cells"], 0)
self.assertGreater(depth["sum"], 0)

Check failure on line 209 in raster/r.sim/r.sim.water/testsuite/test_r_sim_water.py

View workflow job for this annotation

GitHub Actions / Python Code Quality Checks (ubuntu-24.04)

ruff (W293)

raster/r.sim/r.sim.water/testsuite/test_r_sim_water.py:209:1: W293 Blank line contains whitespace help: Remove whitespace from blank line

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change

tools.r_mapcalc(expression="south = if(y() < 33, depth, null())", overwrite=True)
tools.r_mapcalc(expression="north = if(y() > 66, depth, null())", overwrite=True)
mean_south = tools.r_univar(map="south", format="json").json["mean"]
mean_north = tools.r_univar(map="north", format="json").json["mean"]
self.assertGreater(mean_south, mean_north)


@unittest.skip("runs too long")
Expand Down
34 changes: 22 additions & 12 deletions raster/r.sim/simlib/hydro.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ void main_loop(const Setup *setup, const Geometry *geometry,
printf(" nwalka,nwalk=%d %d", sim->nwalka,
sim->nwalk);
G_debug(2, " ");

continue;
}

if (grids->zz[k][l] != UNDEF) {
Expand All @@ -195,22 +197,30 @@ void main_loop(const Setup *setup, const Geometry *geometry,
double decr = pow(
addac * sim->w[lw].m,
3. / 5.); /* decreasing factor in m */
if (grids->inf[k][l] > decr) {
grids->inf[k][l] -=
decr; /* decrease infilt. in cell
and eliminate the walker */
sim->w[lw].m = 0.;
}
else {
sim->w[lw].m -=
pow(grids->inf[k][l], 5. / 3.) /
addac; /* use just proportional part
of the walker weight */
grids->inf[k][l] = 0.;
#if defined(_OPENMP)
#pragma omp critical
#endif
{
if (grids->inf[k][l] > decr) {
grids->inf[k][l] -=
decr; /* decrease infilt. in cell
and eliminate the walker */
Comment on lines +206 to +207

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change
decr; /* decrease infilt. in cell
and eliminate the walker */
decr; /* decrease infilt. in
cell and eliminate the
walker */

sim->w[lw].m = 0.;
}
else {
sim->w[lw].m -=
pow(grids->inf[k][l], 5. / 3.) /
addac; /* use just proportional part
of the walker weight */
Comment on lines +213 to +214

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change
addac; /* use just proportional part
of the walker weight */
addac; /* use just proportional
part of the walker
weight */

grids->inf[k][l] = 0.;
}
}
}
}

#if defined(_OPENMP)
#pragma omp atomic
#endif
grids->gama[k][l] +=
(addac * sim->w[lw].m); /* add walker weigh to
water depth or conc. */
Expand Down
32 changes: 32 additions & 0 deletions raster/r.sim/simlib/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,41 @@
#include <grass/bitmap.h>
#include <grass/linkm.h>

#if defined(_OPENMP)
#include <omp.h>
#define MAX_SIMWE_THREADS 1024
static unsigned int thread_seeds[MAX_SIMWE_THREADS];
static int seeds_initialized = 0;
#endif

void simwe_rand_init(long global_seed)
{
#if defined(_OPENMP)
int i;
int max_threads = omp_get_max_threads();
if (max_threads > MAX_SIMWE_THREADS) max_threads = MAX_SIMWE_THREADS;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change
if (max_threads > MAX_SIMWE_THREADS) max_threads = MAX_SIMWE_THREADS;
if (max_threads > MAX_SIMWE_THREADS)
max_threads = MAX_SIMWE_THREADS;

for (i = 0; i < max_threads; i++) {
thread_seeds[i] = (unsigned int)(global_seed + i * 314159);
}
seeds_initialized = 1;
#endif
}

double simwe_rand(void)
{
#if defined(_OPENMP)
int tid = omp_get_thread_num();
if (tid >= MAX_SIMWE_THREADS) tid = MAX_SIMWE_THREADS - 1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change
if (tid >= MAX_SIMWE_THREADS) tid = MAX_SIMWE_THREADS - 1;
if (tid >= MAX_SIMWE_THREADS)
tid = MAX_SIMWE_THREADS - 1;


if (!seeds_initialized) {
thread_seeds[tid] = 12345 + tid; // Fallback
}

thread_seeds[tid] = (thread_seeds[tid] * 1103515245 + 12345) & 0x7fffffff;
return (double)thread_seeds[tid] / 2147483648.0;
#else
return G_drand48();
#endif
} /* ulec */

double gasdev(void)
Expand Down
1 change: 1 addition & 0 deletions raster/r.sim/simlib/simlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ void create_observation_points(ObservationPoints *points);
void derivatives(const Geometry *geometry, float **elevation, double **dx,
double **dy);

void simwe_rand_init(long global_seed);
double simwe_rand(void);
double gasdev(void);
void gasdev_for_paralel(double *, double *);
Expand Down
Loading