Skip to content

Commit

Permalink
Merge pull request #565 from DrylandEcology/feature_SOILWAT2_devel_v7…
Browse files Browse the repository at this point in the history
….2.0

Updates for SOILWAT2 v7.2.0

(see https://github.com/DrylandEcology/SOILWAT2/releases/tag/v7.2.0)

* Simulation output remains the same as the previous version.

* SOILWAT2 updated handling of warnings and errors (but functionality for STEPWAT2 is identical, i.e., immediate crash on error) ([SOILWAT2 issue 368](DrylandEcology/SOILWAT2#368)).

* SOILWAT2 now offers `sw_random_t` for random numbers and internalizes all uses of `pcg` ([SOILWAT2 issue 373](DrylandEcology/SOILWAT2#373)).

* Obsolete code in DEBUG_MEM* sections is removed ([SOILWAT2 issue 369](DrylandEcology/SOILWAT2#369)).
  • Loading branch information
dschlaep authored Oct 24, 2023
2 parents 6060363 + 10f223f commit 0c5b403
Show file tree
Hide file tree
Showing 21 changed files with 153 additions and 156 deletions.
8 changes: 4 additions & 4 deletions ST_colonization.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,28 +163,28 @@ void initColonization(char* fileName) {
}
if(valuesRead != 4 && valuesRead != 5) {
Mem_Free(tempEvents);
LogError(&LogInfo, LOGFATAL, "Error reading colonization file:\n\tIncorrect"
LogError(&LogInfo, LOGERROR, "Error reading colonization file:\n\tIncorrect"
" number of input arguments.\n\tIs it possible you put a space"
" somewhere? Remember this file cannot contain spaces.");
return;
}
if(fromCell < 0 || toCell > ((grid_Rows * grid_Cols) - 1)) {
Mem_Free(tempEvents);
LogError(&LogInfo, LOGFATAL, "Error reading colonization file:"
LogError(&LogInfo, LOGERROR, "Error reading colonization file:"
"\n\tInvalid cell range ( %d - %d ) specified.", fromCell, toCell);
return;
}
if(startYear < 1 || startYear > SuperGlobals.runModelYears) {
Mem_Free(tempEvents);
LogError(&LogInfo, LOGFATAL, "Error reading colonization file:"
LogError(&LogInfo, LOGERROR, "Error reading colonization file:"
"\n\tInvalid start year (%d) specified.", startYear);
return;
}
// For the species info we need to have a cell loaded.
load_cell(toCell / grid_Cols, toCell % grid_Cols);
if(Species_Name2Index(name) == -1) {
Mem_Free(tempEvents);
LogError(&LogInfo, LOGFATAL, "Error reading colonization file:"
LogError(&LogInfo, LOGERROR, "Error reading colonization file:"
"\n\tUnrecognized species name (%s).", name);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions ST_environs.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include "ST_steppe.h"
#include "ST_globals.h"
#include "sw_src/external/pcg/pcg_basic.h"
#include "sw_src/include/SW_Defines.h"
#include "sw_src/include/rands.h"
#include "sxw.h" // externs `*SXW`
#include "sxw_funcs.h"
Expand All @@ -34,7 +34,7 @@ static void _make_disturbance( void);
/**************************************************************/
/**************************************************************/

pcg32_random_t environs_rng;
sw_random_t environs_rng;


void Env_Generate( void) {
Expand Down
6 changes: 3 additions & 3 deletions ST_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
/* =================================================== */
/* Externed Global Variables */
/* --------------------------------------------------- */
extern pcg32_random_t environs_rng; // defined in ST_environs.c
extern pcg32_random_t resgroups_rng; // defined in ST_resgroups.c
extern pcg32_random_t species_rng; // defined in ST_species.c
extern sw_random_t environs_rng; // defined in ST_environs.c
extern sw_random_t resgroups_rng; // defined in ST_resgroups.c
extern sw_random_t species_rng; // defined in ST_species.c



Expand Down
64 changes: 32 additions & 32 deletions ST_grid.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ char sd_Sep;
int grid_Cells = 0;
Bool UseDisturbances = 0, UseSoils = 0, sd_DoOutput = 0; //these are treated like booleans

pcg32_random_t grid_rng; // Gridded mode's unique RNG.
sw_random_t grid_rng; // Gridded mode's unique RNG.

/**
* \brief The main struct of the gridded mode module.
Expand Down Expand Up @@ -485,7 +485,7 @@ static void _init_grid_files(void)
grid_directories[i] = Str_Dup(Str_TrimLeftQ(buf), &LogInfo);
}
if (i != N_GRID_DIRECTORIES)
LogError(&LogInfo, LOGFATAL, "Invalid files.in");
LogError(&LogInfo, LOGERROR, "Invalid files.in");

for (i = 0; i < N_GRID_FILES; i++)
{
Expand All @@ -494,7 +494,7 @@ static void _init_grid_files(void)
grid_files[i] = Str_Dup(Str_TrimLeftQ(buf), &LogInfo);
}
if (i != N_GRID_FILES)
LogError(&LogInfo, LOGFATAL, "Invalid files.in");
LogError(&LogInfo, LOGERROR, "Invalid files.in");

// opens the log file...
if (!strcmp("stdout", grid_files[GRID_FILE_LOGFILE])){
Expand Down Expand Up @@ -568,7 +568,7 @@ static void _init_soilwat_outputs(char* fileName) {
int cell;
FILE* inFile = fopen(fileName, "r");
if(!inFile) {
LogError(&LogInfo, LOGFATAL, "Could not open SOILWAT2 output cells file."
LogError(&LogInfo, LOGERROR, "Could not open SOILWAT2 output cells file."
"\n\tFile named \"%s\"", fileName);
}

Expand Down Expand Up @@ -1163,11 +1163,11 @@ static void _read_disturbances_in(void)
}

if (num != 11)
LogError(&LogInfo, LOGFATAL, "Invalid %s file line %d wrong",
LogError(&LogInfo, LOGERROR, "Invalid %s file line %d wrong",
grid_files[GRID_FILE_DISTURBANCES], i + 2);
}
if (i != grid_Cells)
LogError(&LogInfo, LOGFATAL, "Invalid %s file wrong number of cells",
LogError(&LogInfo, LOGERROR, "Invalid %s file wrong number of cells",
grid_files[GRID_FILE_DISTURBANCES]);

unload_cell();
Expand Down Expand Up @@ -1223,7 +1223,7 @@ static void _read_soils_in(void){

FILE* f = OpenFile(grid_files[GRID_FILE_SOILS], "r", &LogInfo);
if(!GetALine(f, buf)){ // Throw out the header line.
LogError(&LogInfo, LOGFATAL, "%s file empty.", grid_files[GRID_FILE_SOILS]);
LogError(&LogInfo, LOGERROR, "%s file empty.", grid_files[GRID_FILE_SOILS]);
}

for(i = 0; i < grid_Cells; ++i){
Expand All @@ -1232,17 +1232,17 @@ static void _read_soils_in(void){
load_cell(row, col);

if(!GetALine(f, buf)){
LogError(&LogInfo, LOGFATAL, "Too few lines in %s", grid_files[GRID_FILE_SOILS]);
LogError(&LogInfo, LOGERROR, "Too few lines in %s", grid_files[GRID_FILE_SOILS]);
}
lineReadReturnValue = _read_soil_line(buf, &tempSoil, 0);
if (lineReadReturnValue == SOIL_READ_FAILURE){
LogError(&LogInfo, LOGFATAL, "Error reading %s file.", grid_files[GRID_FILE_SOILS]);
LogError(&LogInfo, LOGERROR, "Error reading %s file.", grid_files[GRID_FILE_SOILS]);
}
/* If _read_soil_line didnt return SUCCESS or FAILURE,
it returned a cell number to copy. */
else if (lineReadReturnValue != SOIL_READ_SUCCESS){
if(lineReadReturnValue > i){
LogError(&LogInfo, LOGFATAL, "%s: Attempted to copy values that have not been read yet.\n"
LogError(&LogInfo, LOGERROR, "%s: Attempted to copy values that have not been read yet.\n"
"\tIf you want to copy a soil make sure you define the layers"
"the FIRST time you use it.", grid_files[GRID_FILE_SOILS]);
}
Expand All @@ -1253,11 +1253,11 @@ static void _read_soils_in(void){
else {
for(j = 1; j < tempSoil.num_layers; ++j){
if(!GetALine(f, buf)){
LogError(&LogInfo, LOGFATAL, "Too few lines in %s", grid_files[GRID_FILE_SOILS]);
LogError(&LogInfo, LOGERROR, "Too few lines in %s", grid_files[GRID_FILE_SOILS]);
}
lineReadReturnValue = _read_soil_line(buf, &tempSoil, j);
if(lineReadReturnValue != SOIL_READ_SUCCESS){
LogError(&LogInfo, LOGFATAL, "Different behavior is specified between layers %d and %d of"
LogError(&LogInfo, LOGERROR, "Different behavior is specified between layers %d and %d of"
" cell %d in file %s. (Perhaps you specified a cell to copy in one"
" but not the other?)", j, j+1, i, grid_files[GRID_FILE_SOILS]);
}
Expand Down Expand Up @@ -1310,7 +1310,7 @@ static int _read_soil_line(char* buf, SoilType* destination, int layer){

if(cellNum > grid_Cells){
LogError(
&LogInfo, LOGFATAL,
&LogInfo, LOGERROR,
"%s: cell number (id=%d) is larger than number of cells in grid (n=%d).",
grid_files[GRID_FILE_SOILS], cellNum, grid_Cells
);
Expand All @@ -1324,7 +1324,7 @@ static int _read_soil_line(char* buf, SoilType* destination, int layer){

if(cellToCopy > grid_Cells){
LogError(
&LogInfo, LOGFATAL,
&LogInfo, LOGERROR,
"%s: cell to copy (id=%d) not present in grid (n=%d).",
grid_files[GRID_FILE_SOILS], cellToCopy, grid_Cells
);
Expand All @@ -1340,7 +1340,7 @@ static int _read_soil_line(char* buf, SoilType* destination, int layer){
if(entriesRead == 16) {
if(layerRead > destination->num_layers){
LogError(
&LogInfo, LOGFATAL,
&LogInfo, LOGERROR,
"%s: cell %d has too many soil layers (%d for max=%d).",
grid_files[GRID_FILE_SOILS], cellNum, layerRead, destination->num_layers
);
Expand Down Expand Up @@ -1437,7 +1437,7 @@ static void _read_spinup_species(void)
copy_cell_col = copy_cell % grid_Cols;

if (num != 3)
LogError(&LogInfo, LOGFATAL, "Invalid %s file", grid_files[GRID_FILE_SPINUP_SPECIES]);
LogError(&LogInfo, LOGERROR, "Invalid %s file", grid_files[GRID_FILE_SPINUP_SPECIES]);

int stringIndex = _get_value_index(buf, ',', 3); //gets us the index of the string that is right after what we just parsed in

Expand All @@ -1452,7 +1452,7 @@ static void _read_spinup_species(void)
continue;
}
else if (do_copy == 1)
LogError(&LogInfo, LOGFATAL,
LogError(&LogInfo, LOGERROR,
"Invalid %s file line %d invalid copy_cell attempt",
grid_files[GRID_FILE_SPINUP_SPECIES], i + 2);

Expand All @@ -1462,7 +1462,7 @@ static void _read_spinup_species(void)
{
num = sscanf(&buf[stringIndex], "%d,", &seeds_Avail);
if (num != 1){
LogError(&LogInfo, LOGFATAL, "Invalid %s file line %d invalid species input",
LogError(&LogInfo, LOGERROR, "Invalid %s file line %d invalid species input",
grid_files[GRID_FILE_SPINUP_SPECIES], i + 2);
}

Expand All @@ -1479,7 +1479,7 @@ static void _read_spinup_species(void)
}

if (i != grid_Cells)
LogError(&LogInfo, LOGFATAL, "Invalid %s file, not enough cells",
LogError(&LogInfo, LOGERROR, "Invalid %s file, not enough cells",
grid_files[GRID_FILE_SPINUP_SPECIES]);

if(!isAnyCellOnForSpinup){
Expand Down Expand Up @@ -1535,13 +1535,13 @@ static void _read_grid_setup(void)
GetALine(f, buf);
i = sscanf(buf, "%d %d", &grid_Rows, &grid_Cols);
if (i != 2)
LogError(&LogInfo, LOGFATAL,
LogError(&LogInfo, LOGERROR,
"Invalid grid setup file (rows/cols line wrong)");

grid_Cells = grid_Cols * grid_Rows;

if (grid_Cells > MAX_CELLS)
LogError(&LogInfo, LOGFATAL,
LogError(&LogInfo, LOGERROR,
"Number of cells in grid exceeds MAX_CELLS defined in ST_defines.h");

/* Allocate the 2d array of cells now that we know how many we need */
Expand All @@ -1550,48 +1550,48 @@ static void _read_grid_setup(void)
GetALine(f, buf);
i = sscanf(buf, "%u", &UseDisturbances);
if (i != 1)
LogError(&LogInfo, LOGFATAL,
LogError(&LogInfo, LOGERROR,
"Invalid grid setup file (disturbances line wrong)");

GetALine(f, buf);
i = sscanf(buf, "%u", &UseSoils);
if (i != 1)
LogError(&LogInfo, LOGFATAL, "Invalid grid setup file (soils line wrong)");
LogError(&LogInfo, LOGERROR, "Invalid grid setup file (soils line wrong)");

GetALine(f, buf);
i = sscanf(buf, "%d", &j);
if (i != 1)
LogError(&LogInfo, LOGFATAL,
LogError(&LogInfo, LOGERROR,
"Invalid grid setup file (seed dispersal line wrong)");
UseSeedDispersal = itob(j);

GetALine(f, buf);
i = sscanf(buf, "%d", &shouldSpinup);
if(i < 1){
LogError(&LogInfo, LOGFATAL, "Invalid grid setup file (Spinup line wrong)");
LogError(&LogInfo, LOGERROR, "Invalid grid setup file (Spinup line wrong)");
}

GetALine(f, buf);
i = sscanf(buf, "%hd", &SuperGlobals.runSpinupYears);
if(i < 1){
LogError(&LogInfo, LOGFATAL, "Invalid grid setup file (Spinup years line wrong)");
LogError(&LogInfo, LOGERROR, "Invalid grid setup file (Spinup years line wrong)");
}

GetALine(f, buf);
i = sscanf(buf, "%u", &writeIndividualFiles);
if(i < 1){
LogError(&LogInfo, LOGFATAL, "Invalid grid setup file (Individual output line wrong)");
LogError(&LogInfo, LOGERROR, "Invalid grid setup file (Individual output line wrong)");
}

GetALine(f, buf);
if (sscanf(buf, "%u", &recordDispersalEvents) != 1) {
LogError(&LogInfo, LOGFATAL,
LogError(&LogInfo, LOGERROR,
"Invalid %s file: seed dispersal events output line\n", grid_files[GRID_FILE_SETUP]);
}

GetALine(f, buf);
if (sscanf(buf, "%d", &outputSDData) != 1) {
LogError(&LogInfo, LOGFATAL, "Invalid grid setup file (Seed Dispersal Data Output\n");
LogError(&LogInfo, LOGERROR, "Invalid grid setup file (Seed Dispersal Data Output\n");
}

CloseFile(&f, &LogInfo);
Expand Down Expand Up @@ -2002,7 +2002,7 @@ static void _separateSOILWAT2DailyOutput(char* fileName, int* cellNumbers) {
FILE* inFile = fopen(fileName, "r");

if(!inFile) {
LogError(&LogInfo, LOGFATAL, "Issue while separating SOILWAT2 output.\n\tFile"
LogError(&LogInfo, LOGERROR, "Issue while separating SOILWAT2 output.\n\tFile"
"\"%s\" not found.", fileName);
}

Expand Down Expand Up @@ -2066,7 +2066,7 @@ static void _separateSOILWAT2MonthlyOutput(char* fileName, int* cellNumbers) {
FILE* inFile = fopen(fileName, "r");

if(!inFile) {
LogError(&LogInfo, LOGFATAL, "Issue while separating SOILWAT2 output.\n\tFile"
LogError(&LogInfo, LOGERROR, "Issue while separating SOILWAT2 output.\n\tFile"
"\"%s\" not found.", fileName);
}

Expand Down Expand Up @@ -2128,7 +2128,7 @@ static void _separateSOILWAT2YearlyOutput(char* fileName, int* cellNumbers) {
FILE* inFile = fopen(fileName, "r");

if(!inFile) {
LogError(&LogInfo, LOGFATAL, "Issue while separating SOILWAT2 output.\n\tFile"
LogError(&LogInfo, LOGERROR, "Issue while separating SOILWAT2 output.\n\tFile"
"\"%s\" not found.", fileName);
}

Expand Down
4 changes: 2 additions & 2 deletions ST_grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/******** These modules are necessary to compile ST_grid.c ********/
#include "ST_stats.h"
#include "ST_defines.h"
#include "sw_src/external/pcg/pcg_basic.h"
#include "sw_src/include/SW_Defines.h"
#include "sxw_vars.h"
#include "sw_src/include/SW_Site.h"
#include "sw_src/include/SW_SoilWater.h"
Expand Down Expand Up @@ -306,7 +306,7 @@ typedef enum
/* =================================================== */
/* Externed Global Variables */
/* --------------------------------------------------- */
extern pcg32_random_t grid_rng;
extern sw_random_t grid_rng;
extern CellType** gridCells;
extern int grid_Rows;
extern int grid_Cols;
Expand Down
4 changes: 2 additions & 2 deletions ST_indivs.c
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ void _delete (IndivType *ndv)

if ((s->est_count > 0 && s->IndvHead == NULL)
|| (s->est_count == 0 && s->IndvHead != NULL))
LogError(&LogInfo, LOGFATAL,
LogError(&LogInfo, LOGERROR,
"PGMR: Indiv Count out of sync in _delete()");

Mem_Free(ndv);
Expand Down Expand Up @@ -489,7 +489,7 @@ void Indiv_SortSize( const byte sorttype,
case SORT_A: cmpfunc = Indiv_CompSize_A; break;
case SORT_D: cmpfunc = Indiv_CompSize_D; break;
default:
LogError(&LogInfo, LOGFATAL,
LogError(&LogInfo, LOGERROR,
"Invalid sort mode in Indiv_SortSize");
}

Expand Down
Loading

0 comments on commit 0c5b403

Please sign in to comment.