Skip to content

Commit

Permalink
Merge pull request #212 from DrylandEcology/bugfix_211
Browse files Browse the repository at this point in the history
Better compilation and use in rSOILWAT2

we have three documented and suppressed/ignored problems:
- memory leak (use of a suppression file): #205
- compile warning #208: treating c input as c++
- compile warning #214: variable array length
  • Loading branch information
dschlaep authored Jul 17, 2018
2 parents d90151f + 1f84d4a commit 6684d64
Show file tree
Hide file tree
Showing 36 changed files with 1,139 additions and 147 deletions.
7 changes: 7 additions & 0 deletions .LSAN_suppr.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Suppression of memory leaks
# https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer#suppressions

# These are known leaks that await fixing

# https://github.com/DrylandEcology/SOILWAT2/issues/205
leak:SW_VPD_construct
37 changes: 34 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@
language: cpp

# We want to run the `*_severe` debug and test targets; thus, we need
# to enable ptrace capability for (L/A)SAN which is currently not possible
# with container-based builds on travis-ci
# TODO: revert to `sudo: false` once the following are fixed/made possible
# - https://github.com/google/sanitizers/issues/764
# - https://github.com/travis-ci/travis-ci/issues/9033
sudo: required

matrix:
fast_finish: true

# We want to run the `*_severe` debug and test targets; thus, we need
# g++ >= 4.9 and clang++ >= 3.5
compiler:
- clang
- gcc

addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-4.9
- g++-4.9

before_install:
- if [[ $CXX = g++ ]]; then export CXX="g++-4.9" CC="gcc-4.9"; fi

script:
- export CPPFLAGS=-DSWDEBUG && make cleaner bint_run
- make cleaner cov test_run
# compile and run optimized binary
- make clean bin bint_run
# compile and run debug binary
- make clean bin_debug_severe bint_run
# compile and run (severe) unit tests
- ASAN_OPTIONS=detect_leaks=1 LSAN_OPTIONS=suppressions=.LSAN_suppr.txt make clean test_severe test_run
# determine code coverage of unit tests
- make clean cov test_run

after_success:
- bash <(curl -s https://codecov.io/bash) || echo "Codecov did not collect coverage reports"
- make cleaner
# clean up
- make clean
674 changes: 674 additions & 0 deletions LICENSE.md

Large diffs are not rendered by default.

46 changes: 34 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ You can help us in different ways:
[pull request](https://github.com/DrylandEcology/SOILWAT2/pulls)


__Compilation__:
* Requirements:
- the `gcc` or `clang/llvm` toolchains
- `gcc >= v4.9` and `clang >= v3.3` for the `*_severe` test/debug targets
- POSIX- or GNU-compliant `make`
- On Windows OS: an installation of `cygwin`
* Build with `make` (see `make help` to print information about all
available targets)


__SOILWAT2 code is used as part of three applications__:
* stand-alone (code flag `SOILWAT` is defined if neither `STEPWAT` nor
`RSOILWAT` exist),
Expand All @@ -75,25 +85,35 @@ __Tests, documentation, and code__ form a trinity
* Note: `SOILWAT2` is written in C whereas `GoogleTest` is a C++ framework.
* Run unit tests locally on the command-line with
```
make test # compiles the unit-test binary/executable (with `-DSWDEBUG`)
make test_run # executes the unit-test binary
make cleaner
make test test_run # compiles and executes the unit-tests
make test_severe test_run # compiles/executes with strict/severe flags
make clean # cleans build artifacts
```
* Development/feature branches can only be merged into master if they pass
all checks
all checks on `appveyor` and `travis` continuous integration servers, i.e.,
run the following locally to prepare a pull-request or commit to be reviewed
```
make clean bin_debug_severe bint_run
make clean test_severe test_run
```
* Note that you may want/need to exclude known memory leaks from severe
testing (see https://github.com/DrylandEcology/SOILWAT2/issues/205):
```
ASAN_OPTIONS=detect_leaks=1 LSAN_OPTIONS=suppressions=.LSAN_suppr.txt make clean test_severe test_run
```
* Informal and local integration tests example:
1. Before coding, run `testing/` and produce reference output
```
git checkout master
make cleaner bint_run
make bin bint_run
cp -r testing/Output testing/Output_ref
```
2. Develop your code and keep "testing/Output_ref" locally, i.e., don't
include "testing/Output_ref" in commits
3. Regularly, e.g., before finalizing a commit, check that new code produces
identical output (that is unless you work on output...)
```
make cleaner bint_run
make bin bint_run
diff testing/Output/ testing/Output_ref/ -qs
```
Expand All @@ -116,19 +136,21 @@ __Tests, documentation, and code__ form a trinity
...
}
```
* Clean, compile and run SOILWAT2-standalone in debugging mode with, e.g.,
* Clean, compile and run optimized SOILWAT2-standalone in debugging mode
with, e.g.,
```
make cleaner bint_run CPPFLAGS=-DSWDEBUG
make bin bint_run CPPFLAGS=-DSWDEBUG
```
* Alternatively, you can use the pre-configured debugging targets
`bin_debug` and `bind`, for instance, with
* Alternatively and potentially preferably, you can use the pre-configured
debugging targets
`bin_debug` and `bin_debug_severe`, for instance, with
```
make cleaner bind bint_run
make bin_debug_severe bint_run
```
* If **valgrind** is installed, then you can call the target `bind_valgrind`
(see description in `makefile`) with
```
make cleaner bind_valgrind
make bind_valgrind
```
<br>
Expand Down
48 changes: 29 additions & 19 deletions SW_Carbon.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,40 @@
* @date 23 January 2017
*/
#ifndef CARBON
#define CARBON /**< A macro that only lets the variables in @f SW_Carbon.h be defined once. */
#define CARBON /**< A macro that only lets the variables in @f SW_Carbon.h be defined once. */

#include "SW_Defines.h"
#include "SW_Defines.h"


/**
* @brief The main structure holding all CO2-related data.
*/
typedef struct {
int
use_wue_mult, /**< A boolean integer indicating if WUE multipliers should be calculated. */
use_bio_mult; /**< A boolean integer indicating if biomass multipliers should be calculated. */
#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief The main structure holding all CO2-related data.
*/
typedef struct {
int
use_wue_mult, /**< A boolean integer indicating if WUE multipliers should be calculated. */
use_bio_mult; /**< A boolean integer indicating if biomass multipliers should be calculated. */

char
scenario[64]; /**< A 64-char array holding the scenario name for which we are extracting CO2 data from the carbon.in file. */
char
scenario[64]; /**< A 64-char array holding the scenario name for which we are extracting CO2 data from the carbon.in file. */

double
ppm[MAX_NYEAR]; /**< A 1D array holding atmospheric CO2 concentration values (units ppm) that are indexed by calendar year. Is typically only populated for the years that are being simulated. `ppm[index]` is the CO2 value for the calendar year `index + 1` */
double
ppm[MAX_NYEAR]; /**< A 1D array holding atmospheric CO2 concentration values (units ppm) that are indexed by calendar year. Is typically only populated for the years that are being simulated. `ppm[index]` is the CO2 value for the calendar year `index + 1` */

} SW_CARBON;
} SW_CARBON;

/* Function Declarations */
void SW_CBN_construct(void);
void SW_CBN_deconstruct(void);
void SW_CBN_read(void);
void calculate_CO2_multipliers(void);


#ifdef __cplusplus
}
#endif

/* Function Declarations */
void SW_CBN_construct(void);
void SW_CBN_deconstruct(void);
void SW_CBN_read(void);
void calculate_CO2_multipliers(void);
#endif
9 changes: 9 additions & 0 deletions SW_Control.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#ifndef SW_CONTROL_H
#define SW_CONTROL_H

#ifdef __cplusplus
extern "C" {
#endif

void SW_CTL_init_model(const char *firstfile);
void SW_CTL_clear_model(Bool full_reset);
void SW_CTL_obtain_inputs(void);
Expand All @@ -29,4 +33,9 @@ void SW_CTL_run_current_year(void);
void SW_CTL_SetMemoryRefs(void);
#endif


#ifdef __cplusplus
}
#endif

#endif
11 changes: 10 additions & 1 deletion SW_Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
/********************************************************/

#ifndef SOILW_DEF_H
#define SOILW_DEF_H
#define SOILW_DEF_H

#include <math.h> /* for atan() in tanfunc() below */
#include "generic.h"

#ifdef __cplusplus
extern "C" {
#endif

/* Not sure if this parameter is variable or a consequence of algebra,
* but it's different in the FORTRAN version than in the ELM doc.
* If deemed to need changing, might as well recompile rather than
Expand Down Expand Up @@ -150,4 +154,9 @@ typedef enum { eF, /* file management */
eOUT /* output */
} ObjType;


#ifdef __cplusplus
}
#endif

#endif
11 changes: 10 additions & 1 deletion SW_Files.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
/********************************************************/
/********************************************************/
#ifndef SW_FILES_H
#define SW_FILES_H
#define SW_FILES_H

#ifdef __cplusplus
extern "C" {
#endif

#define SW_NFILES 23

Expand Down Expand Up @@ -43,4 +47,9 @@ void SW_CSV_F_INIT(const char *s);
void SW_F_SetMemoryRefs(void);
#endif


#ifdef __cplusplus
}
#endif

#endif
15 changes: 15 additions & 0 deletions SW_Flow.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
#ifndef SW_FLOW_H
#define SW_FLOW_H


#ifdef __cplusplus
extern "C" {
#endif

void SW_FLW_construct(void);
void SW_FLW_deconstruct(void);
void SW_Water_Flow(void);


#ifdef __cplusplus
}
#endif

#endif
9 changes: 9 additions & 0 deletions SW_Flow_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
#ifndef SW_WATERSUBS_H
#define SW_WATERSUBS_H

#ifdef __cplusplus
extern "C" {
#endif

/* Standing crop can only intercept so much precip
* This is the limiter used inside stdcrop_intercepted()
*/
Expand Down Expand Up @@ -179,4 +183,9 @@ void soil_temperature_today(double *ptr_dTime, double deltaX, double sT1, double
int nRgr, double sTempR[], double oldsTempR[], double vwcR[], double wpR[], double fcR[],
double bDensityR[], double csParam1, double csParam2, double shParam, Bool *ptr_stError);


#ifdef __cplusplus
}
#endif

#endif
4 changes: 2 additions & 2 deletions SW_Main_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
/* --------------------------------------------------- */

/* see generic.h and filefuncs.h for more info on these vars */
char inbuf[1024]; /* buffer used by input statements */
char inbuf[MAX_FILENAMESIZE]; /* buffer used by input statements */
char errstr[MAX_ERROR]; /* used to compose an error msg */
FILE *logfp; /* file handle for logging messages */
int logged; /* boolean: true = we logged a msg */
Expand All @@ -62,7 +62,7 @@ void usage(void) {
sw_error(0, "%s", s1);
}

char _firstfile[1024];
char _firstfile[MAX_FILENAMESIZE];

void init_args(int argc, char **argv) {
/* =================================================== */
Expand Down
10 changes: 10 additions & 0 deletions SW_Markov.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#ifndef SW_MARKOV_H
#define SW_MARKOV_H

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {

/* pointers to arrays of probabilities for each day saves some space */
Expand Down Expand Up @@ -42,4 +46,10 @@ void SW_MKV_today(TimeInt doy, RealD *tmax, RealD *tmin, RealD *rain);
void SW_MKV_SetMemoryRefs( void);
#endif


#ifdef __cplusplus
}
#endif

#endif

9 changes: 9 additions & 0 deletions SW_Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#include "Times.h"
#include "SW_Defines.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
TimeInt /* controlling dates for model run */
startyr, /* beginning year for model run */
Expand Down Expand Up @@ -54,4 +58,9 @@ void SW_MDL_deconstruct(void);
void SW_MDL_new_year(void);
void SW_MDL_new_day(void);


#ifdef __cplusplus
}
#endif

#endif
2 changes: 2 additions & 0 deletions SW_Output.c
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,8 @@ int SW_OUT_read_onekey(OutKey k, OutSum sumtype, char period[], int first,
last = 366;
#ifndef RSOILWAT
strcpy(period, "YR");
#else
if (period) {} // avoid `-Wunused-parameter`
#endif

} else if ((k == eSW_AllVeg || k == eSW_ET || k == eSW_AllWthr || k == eSW_AllH2O))
Expand Down
Loading

0 comments on commit 6684d64

Please sign in to comment.