Skip to content

Commit

Permalink
Update code to match latest submodule
Browse files Browse the repository at this point in the history
- Update the use flag `inputsProvideSWRCp` over `site_has_swrcpMineralSoil`
- New helper function to `onSet_SW_SIT_tranp()`
	* Move the setting of transpiration from `onSet_SW_SIT()`
	* Set after we read soil information
  • Loading branch information
N1ckP3rsl3y committed Dec 18, 2024
1 parent 5751a4a commit db350c7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
8 changes: 8 additions & 0 deletions src/rSW_Control.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ void rSW_CTL_obtain_inputs(Bool from_files, SEXP InputData, SEXP weatherList, LO
return; // Exit function prematurely due to error
}

onSet_SW_SIT_transp(GET_SLOT(InputData, install("site")), LogInfo);
#ifdef RSWDEBUG
if (debug) sw_printf(" > 'swrc/ptf-type'");
#endif
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
}

if (isNull(weatherList)) {
weatherList = GET_SLOT(InputData, install("weatherHistory"));
}
Expand Down
42 changes: 24 additions & 18 deletions src/rSW_Site.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ static void onSet_SW_SWRCp(SEXP SW_SWRCp, LOG_INFO* LogInfo) {
i = 0;
}

if (isMineral && !v->site_has_swrcpMineralSoil) {
if (isMineral && !v->inputsProvideSWRCp) {
return;
}

Expand All @@ -265,6 +265,9 @@ static void onSet_SW_SWRCp(SEXP SW_SWRCp, LOG_INFO* LogInfo) {
}

}

v->site_has_swrcpMineralSoil =
(Bool) (isMineral && v->inputsProvideSWRCp);
}


Expand Down Expand Up @@ -476,7 +479,7 @@ SEXP onGet_SW_SIT(void) {
setAttrib(swrc_flags, R_NamesSymbol, swrc_names);

PROTECT(has_swrcp = NEW_LOGICAL(1));
LOGICAL(has_swrcp)[0] = v->site_has_swrcpMineralSoil;
LOGICAL(has_swrcp)[0] = v->inputsProvideSWRCp;


// Fill all slots of `SW_SIT`
Expand All @@ -500,7 +503,6 @@ SEXP onGet_SW_SIT(void) {
}

void onSet_SW_SIT(SEXP SW_SIT, LOG_INFO* LogInfo) {
int i;
SW_SITE *v = &SoilWatRun.Site;
SW_MODEL *m = &SoilWatRun.Model;

Expand All @@ -515,20 +517,14 @@ void onSet_SW_SIT(SEXP SW_SIT, LOG_INFO* LogInfo) {
SEXP SoilTemperatureConstants_use;
SEXP SoilTemperatureConstants;
SEXP SoilDensityInputType;
SEXP TranspirationRegions;
SEXP swrc_flags, has_swrcp;

int *p_transp; // ideally `LyrIndex` so that same type as `_TranspRgnBounds`, but R API INTEGER() is signed

#ifdef RSWDEBUG
int debug = 0;
#endif

MyFileName = SoilWatDomain.SW_PathInputs.txtInFiles[eSite];

LyrIndex r; /* transp region definition number */
Bool too_many_regions = FALSE;

#ifdef RSWDEBUG
if (debug) sw_printf("'onSet_SW_SIT':");
#endif
Expand Down Expand Up @@ -640,28 +636,38 @@ void onSet_SW_SIT(SEXP SW_SIT, LOG_INFO* LogInfo) {
strcpy(v->site_ptf_name, CHAR(STRING_ELT(swrc_flags, 1)));
v->site_ptf_type = encode_str2ptf(v->site_ptf_name);
PROTECT(has_swrcp = GET_SLOT(SW_SIT, install("has_swrcp")));
v->site_has_swrcpMineralSoil = LOGICAL(has_swrcp)[0];
v->inputsProvideSWRCp = LOGICAL(has_swrcp)[0];

#ifdef RSWDEBUG
if (debug) sw_printf(" > 'swrc/ptf-type'");
#endif
UNPROTECT(13);
}

void onSet_SW_SIT_transp(SEXP SW_SIT, LOG_INFO* LogInfo) {
SW_SITE *v = &SoilWatRun.Site;
SEXP TranspirationRegions;

Bool too_many_regions = FALSE;
LyrIndex r; /* transp region definition number */
int lyr;
int i;
int *p_transp; // ideally `LyrIndex` so that same type as `_TranspRgnBounds`, but R API INTEGER() is signed

PROTECT(TranspirationRegions = GET_SLOT(SW_SIT, install("TranspirationRegions")));
PROTECT(TranspirationRegions = GET_SLOT(SW_SIT, install("TranspirationRegions")));
p_transp = INTEGER(TranspirationRegions);
v->n_transp_rgn = nrows(TranspirationRegions);
if (MAX_TRANSP_REGIONS < v->n_transp_rgn) {
too_many_regions = TRUE;
} else {
for (i = 0; i < v->n_transp_rgn; i++) {
v->TranspRgnBounds[p_transp[i + v->n_transp_rgn * 0] - 1] = p_transp[i + v->n_transp_rgn * 1] - 1;
lyr = p_transp[i + v->n_transp_rgn * 1] - 1;
v->TranspRgnBounds[p_transp[i + v->n_transp_rgn * 0] - 1] = lyr;
v->TranspRgnDepths[i] = v->soils.depths[lyr];
}
}
if (too_many_regions) {
LogError(LogInfo, LOGERROR, "siteparam.in : Number of transpiration regions"
" exceeds maximum allowed (%d > %d)\n", v->n_transp_rgn, MAX_TRANSP_REGIONS);

UNPROTECT(14); // Unprotect the fourteen protected variables before exiting
UNPROTECT(1); // Unprotect the fourteen protected variables before exiting
return; // Exit function prematurely due to error
}
#ifdef RSWDEBUG
Expand All @@ -673,7 +679,7 @@ void onSet_SW_SIT(SEXP SW_SIT, LOG_INFO* LogInfo) {
if (v->TranspRgnBounds[r - 1] >= v->TranspRgnBounds[r]) {
LogError(LogInfo, LOGERROR, "siteparam.in : Discontinuity/reversal in transpiration regions.\n");

UNPROTECT(14); // Unprotect the fourteen protected variables before exiting
UNPROTECT(1); // Unprotect the fourteen protected variables before exiting
return; // Exit function prematurely due to error
}
}
Expand All @@ -682,5 +688,5 @@ void onSet_SW_SIT(SEXP SW_SIT, LOG_INFO* LogInfo) {
if (debug) sw_printf(" ... done. \n");
#endif

UNPROTECT(14);
UNPROTECT(1);
}
1 change: 1 addition & 0 deletions src/rSW_Site.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
/* --------------------------------------------------- */
SEXP onGet_SW_SIT(void);
void onSet_SW_SIT(SEXP SW_SIT, LOG_INFO* LogInfo);
void onSet_SW_SIT_transp(SEXP SW_SIT, LOG_INFO* LogInfo);
SEXP onGet_SW_SOILS(void);
void onSet_SW_SOILS(SEXP SW_SOILS, LOG_INFO* LogInfo);

0 comments on commit db350c7

Please sign in to comment.