Closed dschlaep closed 1 year ago
@kpalmqui I finally got around to implement the updates to the random number generators that we discussed a couple weeks ago! It appears to work ok except that grid cells do not exactly (but almost exactly) reproduce weather in gridded mode.
This now works as expected: i.e.,
Script to check expectations (requires to manually adjust seed during interactive use):
make clean
#-------------------------------------------------------------------------------
#--- Test nongridded mode ------------------------------------------------------
#-------------------------------------------------------------------------------
#--- * if seed != 0 (output is reproduced among runs) ------
# Set model.in: 3 100 7 (niter, nyrs, seed)
make bint_testing_nongridded
cp -r testing.sagebrush.master/Stepwat_Inputs/Output testing.sagebrush.master/Stepwat_Inputs/Output_i3y100s7_r1
make bint_testing_nongridded
#--- ** weather is exactly identical among runs ------
# Expect no differences
diff -aqr testing.sagebrush.master/Stepwat_Inputs/Output testing.sagebrush.master/Stepwat_Inputs/Output_i3y100s7_r1
#--- ** weather is different among years ------
Rscript -e 'x <- read.csv("testing.sagebrush.master/Stepwat_Inputs/Output/bmassavg.csv")[, c("PPT", "Temp")]; apply(x, 2, sd) > 0'
#--- ** weather is different among iterations ------
Rscript -e 'x <- read.csv("testing.sagebrush.master/Stepwat_Inputs/Output/bmassavg.csv")[, c("StdDev", "StdDev.1")]; apply(x, 2, sd) > 0'
#--- ** weather is different among seeds ------
# Set model.in: 3 100 6 (niter, nyrs, seed) # or any seed other than 0 and 7
make bint_testing_nongridded
# Expect differences
Rscript -e 'x1 <- read.csv("testing.sagebrush.master/Stepwat_Inputs/Output/bmassavg.csv")[, c("PPT", "Temp")]; x0 <- read.csv("testing.sagebrush.master/Stepwat_Inputs/Output_i3y100s7_r1/bmassavg.csv")[, c("PPT", "Temp")]; !isTRUE(all.equal(x1, x0))'
#-------------------------------------------------------------------------------
#--- * if seed == 0 (output cannot be reproduced among runs) ------
# Set model.in: 3 100 0 (niter, nyrs, seed)
make bint_testing_nongridded
cp -r testing.sagebrush.master/Stepwat_Inputs/Output testing.sagebrush.master/Stepwat_Inputs/Output_i3y100s0_r1
make bint_testing_nongridded
#--- ** weather is different among years ------
Rscript -e 'x <- read.csv("testing.sagebrush.master/Stepwat_Inputs/Output/bmassavg.csv")[, c("PPT", "Temp")]; apply(x, 2, sd) > 0'
#--- ** weather is different among iterations ------
Rscript -e 'x <- read.csv("testing.sagebrush.master/Stepwat_Inputs/Output/bmassavg.csv")[, c("StdDev", "StdDev.1")]; apply(x, 2, sd) > 0'
#--- ** weather is different among runs ------
Rscript -e 'x1 <- read.csv("testing.sagebrush.master/Stepwat_Inputs/Output/bmassavg.csv")[, c("PPT", "Temp")]; x0 <- read.csv("testing.sagebrush.master/Stepwat_Inputs/Output_i3y100s0_r1/bmassavg.csv")[, c("PPT", "Temp")]; !isTRUE(all.equal(x1, x0))'
#-------------------------------------------------------------------------------
#--- Test gridded mode ---------------------------------------------------------
#-------------------------------------------------------------------------------
#--- * if seed != 0 (output is reproduced among runs) ------
# Set model.in: 3 100 7 (niter, nyrs, seed)
make bint_testing_gridded
cp -r testing.sagebrush.master/Output testing.sagebrush.master/Output_i3y100s7_r1
make bint_testing_gridded
#--- ** weather is exactly identical among runs and cells ------
# Expect no differences among runs
diff -aqr testing.sagebrush.master/Output testing.sagebrush.master/Output_i3y100s7_r1
# Expect no differences among cells
Rscript -e 'x <- lapply(seq_len(4), function(k) read.csv(paste0("testing.sagebrush.master/Output/g_bmassavg", k - 1, ".csv"))[, c("PPT", "Temp")]); sapply(seq_len(4), function(k) all.equal(x[[k]], x[[1]]))'
#--- ** weather is different among years ------
Rscript -e 'x <- read.csv("testing.sagebrush.master/Output/g_bmass_cell_avg.csv")[, c("PPT", "Temp")]; apply(x, 2, sd) > 0'
#--- ** weather is different among iterations ------
Rscript -e 'x <- lapply(seq_len(4), function(k) read.csv(paste0("testing.sagebrush.master/Output/g_bmassavg", k - 1, ".csv"))[, c("StdDev", "StdDev.1")]); sapply(x, function(xk) apply(xk, 2, sd) > 0)'
#--- ** weather is different among seeds ------
# Set model.in: 3 100 6 (niter, nyrs, seed) # or any seed other than 0 and 7
make bint_testing_gridded
# Expect differences
Rscript -e 'x1 <- read.csv("testing.sagebrush.master/Output/g_bmass_cell_avg.csv")[, c("PPT", "Temp")]; x0 <- read.csv("testing.sagebrush.master/Output_i3y100s7_r1/g_bmass_cell_avg.csv")[, c("PPT", "Temp")]; !isTRUE(all.equal(x1, x0))'
#-------------------------------------------------------------------------------
#--- * if seed == 0 (output cannot be reproduced among runs) ------
# Set model.in: 3 100 0 (niter, nyrs, seed)
make bint_testing_gridded
cp -r testing.sagebrush.master/Output testing.sagebrush.master/Output_i3y100s0_r1
make bint_testing_gridded
#--- ** weather is different among cells ------
Rscript -e 'x <- lapply(seq_len(4), function(k) read.csv(paste0("testing.sagebrush.master/Output/g_bmassavg", k - 1, ".csv"))[, c("PPT", "Temp")]); sapply(seq_len(4)[-1], function(k) !isTRUE(all.equal(x[[k]], x[[1]])))'
#--- ** weather is different among years ------
Rscript -e 'x <- read.csv("testing.sagebrush.master/Output/g_bmass_cell_avg.csv")[, c("PPT", "Temp")]; apply(x, 2, sd) > 0'
#--- ** weather is different among iterations ------
Rscript -e 'x <- lapply(seq_len(4), function(k) read.csv(paste0("testing.sagebrush.master/Output/g_bmassavg", k - 1, ".csv"))[, c("StdDev", "StdDev.1")]); sapply(x, function(xk) apply(xk, 2, sd) > 0)'
#--- ** weather is different among runs ------
Rscript -e 'x1 <- read.csv("testing.sagebrush.master/Output/g_bmass_cell_avg.csv")[, c("PPT", "Temp")]; x0 <- read.csv("testing.sagebrush.master/Output_i3y100s0_r1/g_bmass_cell_avg.csv")[, c("PPT", "Temp")]; !isTRUE(all.equal(x1, x0))'
pull in SOILWAT2 branch "feature_pcg_seeding"
now, a random number sequence can be exactly reproduced ("initstate" and "initseq"), see https://github.com/DrylandEcology/SOILWAT2/pull/327
see also https://github.com/DrylandEcology/SOILWAT2/issues/326
function
RandSeed()
has now two arguments "initstate" and "initseq" -> update STEPWAT2 calls toRandSeed()
with new argumentsnew
set_all_rngs()
to set each STEPWAT2 random number generator to produce sequences of random numbers that are reproducible (if user-provided "seed" is non-zero) andunique among RNGs, iterations, years, and grid cells (most RNGs)
unique among RNGs, iterations, and years but identical among grid cells (weather generator RNG).
A user-provided "seed" of zero produces non-reproducible random number sequences which are non-coinciding among RNGs, iterations, and grid cells