Skip to content

Commit

Permalink
Merge pull request #391 from DrylandEcology/feature_includeOutputInTests
Browse files Browse the repository at this point in the history
Feature include output code in tests

- close #85
  • Loading branch information
dschlaep authored Feb 8, 2024
2 parents 98b0328 + d93ad1e commit 32ae7d5
Show file tree
Hide file tree
Showing 21 changed files with 377 additions and 648 deletions.
2 changes: 1 addition & 1 deletion include/SW_Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extern "C" {
/* Global Function Declarations */
/* --------------------------------------------------- */
void SW_MDL_read(SW_MODEL* SW_Model, char *InFiles[], LOG_INFO* LogInfo);
void SW_MDL_construct(Bool newperiod[], TimeInt days_in_month[]);
void SW_MDL_construct(SW_MODEL* SW_Model);
void SW_MDL_deconstruct(void);
void SW_MDL_new_year(SW_MODEL* SW_Model);
void SW_MDL_new_day(SW_MODEL* SW_Model);
Expand Down
13 changes: 12 additions & 1 deletion include/SW_Output.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ extern char const *styp2str[];
/* =================================================== */
/* Global Function Declarations */
/* --------------------------------------------------- */
void SW_OUT_init_ptrs(SW_ALL* sw);
void SW_OUT_init_ptrs(SW_OUTPUT* SW_Output);
void SW_GENOUT_init_ptrs(SW_GEN_OUT *GenOutput);
void SW_OUT_construct(Bool make_soil[], Bool make_regular[],
SW_OUTPUT_POINTERS* SW_OutputPtrs, SW_OUTPUT* SW_Output,
LyrIndex n_layers, SW_GEN_OUT *GenOutput);
Expand All @@ -153,6 +154,13 @@ void SW_OUT_flush(SW_ALL* sw, SW_OUTPUT_POINTERS* SW_OutputPtrs,
LOG_INFO* LogInfo);
void _collect_values(SW_ALL* sw, SW_OUTPUT_POINTERS* SW_OutputPtrs,
Bool bFlush_output, TimeInt tOffset, LOG_INFO* LogInfo);

void SW_OUT_close_files(SW_FILE_STATUS* SW_FileStatus, SW_GEN_OUT* GenOutput,
LOG_INFO* LogInfo);
void SW_OUT_create_files(SW_FILE_STATUS* SW_FileStatus, SW_OUTPUT* SW_Output,
LyrIndex n_layers, char *InFiles[], SW_GEN_OUT* GenOutput,
LOG_INFO* LogInfo);

void _echo_outputs(SW_ALL* sw);
void _echo_all_inputs(SW_ALL* sw);

Expand All @@ -169,6 +177,9 @@ void SW_OUT_set_SXWrequests(OutPeriod timeSteps_SXW[][SW_OUTNPERIODS],
IntUS *used_OUTNPERIODS, SW_OUTPUT *SW_Output, LOG_INFO *LogInfo);
#endif

void SW_GENOUT_deepCopy(SW_GEN_OUT* dest, SW_GEN_OUT* source, SW_OUTPUT* SW_Output, LOG_INFO* LogInfo);



// Functions that format the output in `sw_outstr` for printing
/* --------------------------------------------------- */
Expand Down
6 changes: 4 additions & 2 deletions include/SW_Output_outarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ extern const IntUS ncol_TimeOUT[SW_OUTNPERIODS];
/* --------------------------------------------------- */
void SW_OUT_set_nrow(SW_MODEL* SW_Model, Bool use_OutPeriod[],
size_t nrow_OUT[]);

void SW_OUT_construct_outarray(SW_GEN_OUT *GenOutput, SW_OUTPUT* SW_Output,
LOG_INFO *LogInfo);
void SW_OUT_deconstruct_outarray(SW_GEN_OUT *GenOutput);

#ifdef RSOILWAT
Expand All @@ -66,11 +69,10 @@ void get_outvalleader(SW_MODEL* SW_Model, OutPeriod pd,

#ifdef STEPWAT
void do_running_agg(RealD *p, RealD *psd, size_t k, IntU n, RealD x);
void setGlobalSTEPWAT2_OutputVariables(SW_OUTPUT* SW_Output, SW_GEN_OUT *GenOutput,
LOG_INFO *LogInfo);
#endif



#ifdef __cplusplus
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions include/SW_Output_outtext.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extern "C" {
/* Global Function Declarations */
/* --------------------------------------------------- */
#if defined(SOILWAT)
void SW_OUT_create_files(SW_FILE_STATUS* SW_FileStatus, SW_OUTPUT* SW_Output,
void SW_OUT_create_textfiles(SW_FILE_STATUS* SW_FileStatus, SW_OUTPUT* SW_Output,
LyrIndex n_layers, char *InFiles[], SW_GEN_OUT* GenOutput,
LOG_INFO* LogInfo);

Expand All @@ -47,7 +47,7 @@ void write_headers_to_csv(OutPeriod pd, FILE *fp_reg, FILE *fp_soil,
void find_TXToutputSoilReg_inUse(Bool make_soil[], Bool make_regular[],
SW_OUTPUT* SW_Output, OutPeriod timeSteps[][SW_OUTNPERIODS],
IntUS used_OUTNPERIODS);
void SW_OUT_close_files(SW_FILE_STATUS* SW_FileStatus, SW_GEN_OUT* GenOutput,
void SW_OUT_close_textfiles(SW_FILE_STATUS* SW_FileStatus, SW_GEN_OUT* GenOutput,
LOG_INFO* LogInfo);


Expand Down
9 changes: 5 additions & 4 deletions include/SW_datastructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@


// Array-based output:
#if defined(RSOILWAT) || defined(STEPWAT)
#if defined(RSOILWAT) || defined(STEPWAT) || defined(SWNETCDF)
#define SW_OUTARRAY
#endif

// Text-based output:
#if defined(SOILWAT) || defined(STEPWAT)
#if (defined(SOILWAT) || defined(STEPWAT)) && !defined(SWNETCDF)
#define SW_OUTTEXT
#endif

Expand Down Expand Up @@ -156,6 +156,7 @@ typedef struct {
* printing and summing weekly/monthly values */
Bool newperiod[SW_OUTNPERIODS];
Bool isnorth;
Bool doOutput; /**< Flag to indicate if output should be produced (TRUE) or not (FALSE); set to FALSE for spinup and tests */

int ncSuid[2]; // First element used for domain "s", both used for "xy"

Expand Down Expand Up @@ -1007,7 +1008,7 @@ typedef struct {
Bool print_SW_Output;
char sw_outstr[MAX_LAYERS * OUTSTRLEN];

#if defined(RSOILWAT) || defined(STEPWAT)
#if defined(SW_OUTARRAY)
/** \brief A 2-dim array of pointers to output arrays.
The variable p_OUT used by rSOILWAT2 for output and by STEPWAT2 for
Expand Down Expand Up @@ -1170,7 +1171,7 @@ typedef struct {
void (*pfunc_text)(OutPeriod, SW_ALL*); /* pointer to output routine for text output */
#endif

#if defined(RSOILWAT)
#if defined(RSOILWAT) || defined(SWNETCDF)
void (*pfunc_mem)(OutPeriod, SW_ALL*); /* pointer to output routine for array output */

#elif defined(STEPWAT)
Expand Down
23 changes: 11 additions & 12 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -275,31 +275,30 @@ sources_core := \
$(dir_src)/SW_Flow_lib.c \
$(dir_src)/SW_Flow.c \
$(dir_src)/SW_Carbon.c \
$(dir_src)/SW_Domain.c
$(dir_src)/SW_Domain.c \
$(dir_src)/SW_Output.c \
$(dir_src)/SW_Output_get_functions.c

ifdef SWNETCDF
sources_core += $(dir_src)/SW_netCDF.c
sources_core += $(dir_src)/SW_Output_outarray.c
else
sources_core += $(dir_src)/SW_Output_outtext.c
endif

sources_lib = \
$(sw_sources) \
$(sources_core) \
$(dir_src)/SW_Output.c \
$(dir_src)/SW_Output_get_functions.c
$(sources_core)
objects_lib = $(sources_lib:$(dir_src)/%.c=$(dir_build_sw2)/%.o)


# SOILWAT2-standalone
sources_bin := $(dir_src)/SW_Main.c $(dir_src)/SW_Output_outtext.c
# Code for SOILWAT2-standalone program
sources_bin := $(dir_src)/SW_Main.c
objects_bin := $(sources_bin:$(dir_src)/%.c=$(dir_build_sw2)/%.o)


# Unfortunately, we currently cannot include 'SW_Output.c' because
# - cannot increment expression of enum type (e.g., OutKey, OutPeriod)
# - assigning to 'OutKey' from incompatible type 'int'
# ==> instead, we use 'SW_Output_mock.c' which provides mock versions of the
# public functions (but will result in some compiler warnings)
sources_lib_test := $(sources_core) $(dir_src)/SW_Output_mock.c
# Code for SOILWAT2-test library (include all source code)
sources_lib_test := $(sources_core)
objects_lib_test := $(sources_lib_test:$(dir_src)/%.c=$(dir_build_test)/%.o)


Expand Down
33 changes: 21 additions & 12 deletions src/SW_Control.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static void _end_day(SW_ALL* sw, SW_OUTPUT_POINTERS* SW_OutputPtrs,
LOG_INFO* LogInfo) {
int localTOffset = 1; // tOffset is one when called from this function

if ( !sw->Model.SW_SpinUp.spinup ) {
if ( sw->Model.doOutput ) {
_collect_values(sw, SW_OutputPtrs, swFALSE, localTOffset, LogInfo);
if(LogInfo->stopRun) {
return; // Exit function prematurely due to error

Check warning on line 99 in src/SW_Control.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Control.c#L97-L99

Added lines #L97 - L99 were not covered by tests
Expand Down Expand Up @@ -168,6 +168,11 @@ void SW_ALL_deepCopy(SW_ALL* source, SW_ALL* dest, LOG_INFO* LogInfo)
}

SW_VegEstab_alloc_outptrs(&dest->VegEstab, LogInfo);
if(LogInfo->stopRun) {
return; // Exit function prematurely due to error
}

SW_GENOUT_deepCopy(&dest->GenOutput, &source->GenOutput, dest->Output, LogInfo);
}


Expand Down Expand Up @@ -314,7 +319,8 @@ void SW_CTL_init_ptrs(SW_ALL* sw) {
SW_MKV_init_ptrs(&sw->Markov);
SW_VES_init_ptrs(&sw->VegEstab);
SW_VPD_init_ptrs(&sw->VegProd);
SW_OUT_init_ptrs(sw);
SW_OUT_init_ptrs(sw->Output);
SW_GENOUT_init_ptrs(&sw->GenOutput);
SW_SWC_init_ptrs(&sw->SoilWat);
}

Expand Down Expand Up @@ -428,7 +434,7 @@ void SW_CTL_setup_domain(unsigned long userSUID,
*/
void SW_CTL_setup_model(SW_ALL* sw, SW_OUTPUT_POINTERS* SW_OutputPtrs,
LOG_INFO* LogInfo) {
SW_MDL_construct(sw->Model.newperiod, sw->Model.days_in_month);
SW_MDL_construct(&sw->Model);
SW_WTH_construct(&sw->Weather);

// delay SW_MKV_construct() until we know from inputs whether we need it
Expand Down Expand Up @@ -586,7 +592,7 @@ void SW_CTL_run_current_year(SW_ALL* sw, SW_OUTPUT_POINTERS* SW_OutputPtrs,
#ifdef SWDEBUG
if (debug) swprintf("'SW_CTL_run_current_year': flush output\n");
#endif
if ( !sw->Model.SW_SpinUp.spinup ) {
if ( sw->Model.doOutput ) {
SW_OUT_flush(sw, SW_OutputPtrs, LogInfo);

Check warning on line 596 in src/SW_Control.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Control.c#L596

Added line #L596 was not covered by tests
}

Expand Down Expand Up @@ -628,6 +634,7 @@ void SW_CTL_run_spinup(SW_ALL* sw, LOG_INFO* LogInfo) {
scope = sw->Model.SW_SpinUp.scope,
finalyr = sw->Model.startyr + scope - 1,
*years;
Bool prev_doOut = sw->Model.doOutput;

years = ( TimeInt* )Mem_Malloc( sizeof( TimeInt ) * duration, "SW_CTL_run_spinup()", LogInfo );
if(LogInfo->stopRun) {
Expand All @@ -637,7 +644,7 @@ void SW_CTL_run_spinup(SW_ALL* sw, LOG_INFO* LogInfo) {
#ifdef SWDEBUG
int debug = 0;
#endif

switch ( mode ) {
case 2:
// initialize structured array
Expand Down Expand Up @@ -680,6 +687,8 @@ void SW_CTL_run_spinup(SW_ALL* sw, LOG_INFO* LogInfo) {

sw->Model.startyr = years[0]; // set startyr for spinup

sw->Model.doOutput = swFALSE; // turn output temporarily off

for (yrIdx = 0; yrIdx < duration; yrIdx++) {
*cur_yr = years[ yrIdx ];

Expand All @@ -689,15 +698,16 @@ void SW_CTL_run_spinup(SW_ALL* sw, LOG_INFO* LogInfo) {

SW_CTL_run_current_year(sw, NULL, LogInfo);
if(LogInfo->stopRun) {
free( years );
return; // Exit function prematurely due to error
goto reSet; // Exit function prematurely due to error

Check warning on line 701 in src/SW_Control.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Control.c#L701

Added line #L701 was not covered by tests
}
}

sw->Model.startyr = startyr; // reset startyr to original value

free( years );
reSet: {
sw->Model.startyr = startyr; // reset startyr to original value
sw->Model.doOutput = prev_doOut; // reset doOutput to original value

free( years );
}
}


Expand Down Expand Up @@ -890,14 +900,13 @@ void SW_CTL_run_sw(SW_ALL* sw_template, SW_DOMAIN* SW_Domain, unsigned long ncSu

if ( SW_Domain->SW_SpinUp.spinup ) {
SW_CTL_run_spinup(&local_sw, LogInfo );

Check warning on line 902 in src/SW_Control.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Control.c#L901-L902

Added lines #L901 - L902 were not covered by tests
local_sw.Model.SW_SpinUp.spinup = swFALSE;
}

SW_CTL_main(&local_sw, SW_OutputPtrs, LogInfo);

Check warning on line 905 in src/SW_Control.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Control.c#L905

Added line #L905 was not covered by tests

// Clear local instance of SW_ALL
freeMem: {
SW_CTL_clear_model(swFALSE, &local_sw);
SW_CTL_clear_model(swTRUE, &local_sw);

Check warning on line 909 in src/SW_Control.c

View check run for this annotation

Codecov / codecov/patch

src/SW_Control.c#L908-L909

Added lines #L908 - L909 were not covered by tests
}

(void) SW_Domain;
Expand Down
1 change: 0 additions & 1 deletion src/SW_Main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "include/SW_Site.h"
#include "include/SW_Weather.h"
#include "include/SW_Output.h"
#include "include/SW_Output_outtext.h"
#include "include/SW_Main_lib.h"
#include "include/SW_Domain.h"
#include "include/SW_Model.h"
Expand Down
17 changes: 9 additions & 8 deletions src/SW_Model.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,10 @@ const TimeInt _notime = 0xffff; /* init value for _prev* */
/**
@brief MDL constructor for global variables.
@param[out] newperiod newperiod[] Specifies when a new day/week/month/year
has started
@param[out] days_in_month[] Number of days per month for "current" year
@param[in,out] SW_Model Struct of type SW_MODEL holding basic time information
about the simulation
*/
void SW_MDL_construct(Bool newperiod[], TimeInt days_in_month[]) {
void SW_MDL_construct(SW_MODEL* SW_Model) {
/* =================================================== */
/* note that an initializer that is called during
* execution (better called clean() or something)
Expand All @@ -73,13 +72,16 @@ void SW_MDL_construct(Bool newperiod[], TimeInt days_in_month[]) {
*/
OutPeriod pd;

Time_init_model(days_in_month); // values of time are correct only after Time_new_year()
Time_init_model(SW_Model->days_in_month); // values of time are correct only after Time_new_year()

ForEachOutPeriod(pd)
{
newperiod[pd] = swFALSE;
SW_Model->newperiod[pd] = swFALSE;
}
newperiod[eSW_Day] = swTRUE; // every day is a new day
SW_Model->newperiod[eSW_Day] = swTRUE; // every day is a new day

SW_Model->addtl_yr = 0;
SW_Model->doOutput = swTRUE;
}

/**
Expand Down Expand Up @@ -126,7 +128,6 @@ void SW_MDL_read(SW_MODEL* SW_Model, char *InFiles[], LOG_INFO* LogInfo) {
but if hemisphere occurs first, skip checking for the rest
and assume they're not there.
*/
SW_Model->addtl_yr = 0;
lineno = 0;
while (GetALine(f, inbuf, MAX_FILENAMESIZE)) {
switch(lineno) {
Expand Down
Loading

0 comments on commit 32ae7d5

Please sign in to comment.