Skip to content

Commit

Permalink
Merge pull request #226 from DrylandEcology/feature_vegestab
Browse files Browse the repository at this point in the history
Resurrect "vegetation establishment"

* `SOILWAT2` is updated to v6.7.0 which fixed vegetation establishment.

* Functionality to calculate and output establishment/recruitment of species now works and is covered by tests (issue #225, @dschlaep). Note that this functionality assesses yearly the chances of species to recruit/establish based on simulated daily conditions; however, establishment/recruitment outcomes are not utilized to inform the simulation.
  • Loading branch information
dschlaep authored Jan 13, 2023
2 parents 652d59c + b08ea05 commit bd7552e
Show file tree
Hide file tree
Showing 23 changed files with 79 additions and 37 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: rSOILWAT2
Version: 5.3.3
Version: 5.4.0
Title: An Ecohydrological Ecosystem-Scale Water Balance Simulation Model
Description: Access to the C-based SOILWAT2 v6.6.0 and functionality for
Description: Access to the C-based SOILWAT2 v6.7.0 and functionality for
SQLite-database of weather data.
Authors@R: c(
person(
Expand Down
18 changes: 18 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# rSOILWAT2 v5.4.0
* `SOILWAT2` is updated to v6.7.0 which fixed vegetation establishment.
* This version produces identical simulation output
as the previous release under default values
(i.e., vegetation establishment is turned off).

* Functionality to calculate and output establishment/recruitment of species
now works and is covered by tests (issue #225, @dschlaep).
Note that this functionality assesses yearly the chances of
species to recruit/establish based on simulated daily conditions;
however, establishment/recruitment outcomes are not utilized to inform the
simulation.

## Changes to interface
* Class `swEstabSpecies` gains new slot `"vegType"` to specify vegetation type
of a species establishment parameters (issue #225, @dschlaep).


# rSOILWAT2 v5.3.3
* This version produces identical simulation output as the previous release.
* `get_soiltemp()` now correctly locates soil temperature output for all
Expand Down
1 change: 1 addition & 0 deletions R/I_swEstab.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#' @name swEstabSpecies-class
#' @export
setClass("swEstabSpecies", slot = c(fileName = "character", Name = "character",
vegType = "integer",
estab_lyrs = "integer", barsGERM = "numeric", barsESTAB = "numeric",
min_pregerm_days = "integer", max_pregerm_days = "integer",
min_wetdays_for_germ = "integer", max_drydays_postgerm = "integer",
Expand Down
16 changes: 15 additions & 1 deletion data-raw/prepare_testInput_objects.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dir_in <- file.path("inst", "extdata")
dir_backup <- sub("extdata", "extdata_copy", dir_in, fixed = TRUE)
dir_out <- file.path("tests", "test_data")

tests <- 1:5
tests <- 1:6
examples <- paste0("example", tests)


Expand Down Expand Up @@ -119,6 +119,20 @@ for (it in seq_along(tests)) {
substr(fin[line], 1, 3) <- "-45"
writeLines(fin, con = ftemp)

# example6: vegetation establishment
ftemp <- file.path(dir_in, examples[6], "Input", "estab.in")
fin <- readLines(ftemp)
line <- grep("calculate and output establishment", fin, fixed = TRUE)
stopifnot(length(line) == 1, line > 0, line < length(fin))
substr(fin[line], 1, 1) <- "1"
writeLines(fin, con = ftemp)

ftemp <- file.path(dir_in, examples[6], "Input", "outsetup.in")
fin <- readLines(ftemp)
line <- grep("establishment results", fin, fixed = TRUE)
stopifnot(length(line) == 1, line > 0, line < length(fin))
fin[line] <- sub("OFF", "AVG", fin[line], fixed = TRUE)
writeLines(fin, con = ftemp)



Expand Down
Binary file modified data/sw_exampleData.rda
Binary file not shown.
5 changes: 3 additions & 2 deletions inst/extdata/example1/Input/estab.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# soil moisture and timing parameters required for the
# species to establish in a given year.
# There is no limit to the number of files in the list.
# File names with paths relative to the SOILWAT2 working directory (-d flag)

Input/bouteloua.estab
Input/bromus.estab
Input/estab/bouteloua.estab
Input/estab/bromus.estab
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
bogr # 4-char name of species
bogr # 4-char name of species
3 # Vegetation type of species (0, trees; 1, shrubs; 2, forbs; 3 grasses)
# soil layer parameters
2 # number of layers affecting establishment
10. # SWP (bars) requirement for germination (top layer)
15. # SWP (bars) requirement for establishment (average of top layers)
# timing parameters in days
60 # first possible day of germination
180 # last possible day of germination
180 # last possible day of germination
2 # min number of consecutive "wet" days for germination to occur
40 # max number of consecutive "dry" days after germination allowing estab
40 # max number of consecutive "dry" days after germination allowing estab
5 # min number of consecutive "wet" days after germination before establishment
15 # min number of days between germination and establishment
75 # max number of days between germination and establishment
15 # min number of days between germination and establishment
75 # max number of days between germination and establishment
# temperature parameters in C
5. # min temp threshold for germination
20. # max temp threshold for germination
0. # min temp threshold for establishment
20. # max temp threshold for establishment
5. # min temp threshold for germination
20. # max temp threshold for germination
0. # min temp threshold for establishment
20. # max temp threshold for establishment
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
brte # 4-char name of species
3 # Vegetation type of species (0, trees; 1, shrubs; 2, forbs; 3 grasses)
# soil layer parameters
3 # number of layers affecting establishment - 45 cm Harris and Hulbert
10. # SWP (bars) requirement for germination (top layer) - Harris
Expand All @@ -7,12 +8,12 @@ brte # 4-char name of species
200 # first possible day of germination - Hulbert and Harris
365 # last possible day of germination - Hulbert
6 # min number of consecutive "wet" days for germination to occur - Hurlbert and Harris
45 # max number of consecutive "dry" days after germination allowing estab - Harris (longtime)
45 # max number of consecutive "dry" days after germination allowing estab - Harris (longtime)
6 # min number of consecutive "wet" days after germination before establishment - Harris
15 # min number of days between germination and establishment - Hulbert
90 # max number of days between germination and establishment - Harris and Hulbert
# temperature parameters in C
10. # min temp threshold for germination - Hulbert and Harris
30. # max temp threshold for germination - Hulbert and Harris
3. # min temp threshold for establishment - Harris
30. # max temp threshold for establishment - Harris and Hulbert
30. # max temp threshold for establishment - Harris and Hulbert
23 changes: 8 additions & 15 deletions src/rSW_Output.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ void onSet_SW_OUT(SEXP OUT) {
int *use, *timePeriods, *sumtype, *first_orig, *last_orig;
// mykey and myobj are currently unused:
// int *mykey, *myobj;
char
stub[10],
msg[200]; // message to print
char msg[200]; // message to print
#ifdef RSWDEBUG
int debug = 0;
#endif
Expand Down Expand Up @@ -89,19 +87,14 @@ void onSet_SW_OUT(SEXP OUT) {
last_orig = INTEGER(GET_SLOT(OUT, install("last_orig")));
PROTECT(outfile = GET_SLOT(OUT, install("outfile")));

if (use[eSW_Estab]) {
sumtype[eSW_Estab] = eSW_Sum;
first_orig[eSW_Estab] = 1;
timePeriods[eSW_Estab + 0 * SW_OUTNKEYS] = eSW_Year;
ForEachOutPeriod(i) {
timePeriods[eSW_Estab + i * SW_OUTNKEYS] = eSW_NoTime;
}
last_orig[eSW_Estab] = 366;
}

ForEachOutKey(k) {
msg_type = SW_OUT_read_onekey(k, sumtype[k], stub, first_orig[k],
last_orig[k], msg);
msg_type = SW_OUT_read_onekey(
k,
sumtype[k],
first_orig[k],
last_orig[k],
msg
);

if (msg_type > 0) {
LogError(logfp, msg_type, "%s", msg);
Expand Down
23 changes: 18 additions & 5 deletions src/rSW_VegEstab.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,23 @@ SEXP onGet_SW_VES(void) {
SEXP VES;
SEXP use;
SEXP count;

PROTECT(swEstab = MAKE_CLASS("swEstab"));
PROTECT(VES = NEW_OBJECT(swEstab));
PROTECT(use = NEW_LOGICAL(1));

PROTECT(count = NEW_INTEGER(1));
INTEGER(count)[0] = SW_VegEstab.count;

PROTECT(use = NEW_LOGICAL(1));
LOGICAL(use)[0] = SW_VegEstab.use;

SET_SLOT(VES, install("count"), count);
SET_SLOT(VES, install("useEstab"), use);
if (SW_VegEstab.use)

if (SW_VegEstab.use) {
onGet_SW_VES_spps(VES);
}

UNPROTECT(4);
return VES;
}
Expand All @@ -71,6 +79,7 @@ void onSet_SW_VES(SEXP VES) {

PROTECT(use = GET_SLOT(VES,install("useEstab")));
PROTECT(count = GET_SLOT(VES,install("count")));

if (LOGICAL(use)[0] == FALSE) {
//LogError(logfp, LOGNOTE, "Establishment not used.\n");
SW_VegEstab.use = FALSE;
Expand All @@ -82,7 +91,7 @@ void onSet_SW_VES(SEXP VES) {
} else {
SW_VegEstab.use = TRUE;
for (i = 0; i < nSPPS; i++)
onSet_SW_VES_spp(VES, i);
onSet_SW_VES_spp(VES, i); // sets `SW_VegEstab.count` incrementally
}
}

Expand All @@ -100,11 +109,12 @@ void onSet_SW_VES(SEXP VES) {
void onGet_SW_VES_spps(SEXP SPP) {
int i;
SW_VEGESTAB_INFO *v;
SEXP fileName, name, estab_lyrs, barsGERM, barsESTAB, min_pregerm_days, max_pregerm_days, min_wetdays_for_germ, max_drydays_postgerm, min_wetdays_for_estab, min_days_germ2estab,
SEXP fileName, name, vegType, estab_lyrs, barsGERM, barsESTAB, min_pregerm_days, max_pregerm_days, min_wetdays_for_germ, max_drydays_postgerm, min_wetdays_for_estab, min_days_germ2estab,
max_days_germ2estab, min_temp_germ, max_temp_germ, min_temp_estab, max_temp_estab;

PROTECT(fileName = allocVector(STRSXP,SW_VegEstab.count));
PROTECT(name = allocVector(STRSXP,SW_VegEstab.count));
PROTECT(vegType = NEW_INTEGER(SW_VegEstab.count));
PROTECT(estab_lyrs = NEW_INTEGER(SW_VegEstab.count));
PROTECT(barsGERM = allocVector(REALSXP,SW_VegEstab.count));
PROTECT(barsESTAB = allocVector(REALSXP,SW_VegEstab.count));
Expand All @@ -124,6 +134,7 @@ void onGet_SW_VES_spps(SEXP SPP) {
v = SW_VegEstab.parms[i];
SET_STRING_ELT(fileName, i, mkChar(v->sppFileName));
SET_STRING_ELT(name, i, mkChar(v->sppname));
INTEGER(vegType)[i] = v->vegType;
INTEGER(estab_lyrs)[i] = v->estab_lyrs;
REAL(barsGERM)[i] = v->bars[0];
REAL(barsESTAB)[i] = v->bars[1];
Expand All @@ -141,6 +152,7 @@ void onGet_SW_VES_spps(SEXP SPP) {
}
SET_SLOT(SPP, install("fileName"), fileName);
SET_SLOT(SPP, install("Name"), name);
SET_SLOT(SPP, install("vegType"), vegType);
SET_SLOT(SPP, install("estab_lyrs"), estab_lyrs);
SET_SLOT(SPP, install("barsGERM"), barsGERM);
SET_SLOT(SPP, install("barsESTAB"), barsESTAB);
Expand All @@ -156,7 +168,7 @@ void onGet_SW_VES_spps(SEXP SPP) {
SET_SLOT(SPP, install("min_temp_estab"), min_temp_estab);
SET_SLOT(SPP, install("max_temp_estab"), max_temp_estab);

UNPROTECT(16);
UNPROTECT(17);
}

void onSet_SW_VES_spp(SEXP SPP, IntU i) {
Expand All @@ -167,6 +179,7 @@ void onSet_SW_VES_spp(SEXP SPP, IntU i) {
count = _new_species();
v = SW_VegEstab.parms[count];

v->vegType = INTEGER(GET_SLOT(SPP, install("vegType")))[i];
v->estab_lyrs = INTEGER(GET_SLOT(SPP, install("estab_lyrs")))[i];
v->bars[SW_GERM_BARS] = REAL(GET_SLOT(SPP, install("barsGERM")))[i];
v->bars[SW_ESTAB_BARS] = REAL(GET_SLOT(SPP, install("barsESTAB")))[i];
Expand Down
Binary file modified tests/test_data/Ex1_input.rds
Binary file not shown.
Binary file modified tests/test_data/Ex1_output.rds
Binary file not shown.
Binary file modified tests/test_data/Ex2_input.rds
Binary file not shown.
Binary file modified tests/test_data/Ex3_input.rds
Binary file not shown.
Binary file modified tests/test_data/Ex3_output.rds
Binary file not shown.
Binary file modified tests/test_data/Ex4_input.rds
Binary file not shown.
Binary file modified tests/test_data/Ex4_output.rds
Binary file not shown.
Binary file modified tests/test_data/Ex5_input.rds
Binary file not shown.
Binary file modified tests/test_data/Ex5_output.rds
Binary file not shown.
Binary file added tests/test_data/Ex6_input.rds
Binary file not shown.
Binary file added tests/test_data/Ex6_output.rds
Binary file not shown.
Binary file added tests/test_data/Ex6_weather.rds
Binary file not shown.

0 comments on commit bd7552e

Please sign in to comment.