DrylandEcology / STEPWAT2

folder
4 stars 5 forks source link

Reproducibly set random number generators #528

Closed dschlaep closed 1 year ago

dschlaep commented 1 year ago
dschlaep commented 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.

dschlaep commented 1 year ago

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))'