Commit c835cf5f authored by Ethan Coon's avatar Ethan Coon
Browse files

reorganizes test directories a bit, also adds gold standard for single and...

reorganizes test directories a bit, also adds gold standard for single and multiple tests and adds scripts to compare solution.  to run, now: cd tests/tests_c; make clean; make; make test
parent f3c9a2c0
"""Compares a test result to the gold standard.
Error codes:
----------------------------------------------------------
G | Error in the gold file -- bad testname?
D | Error in my file -- test did not complete?
E | Error in my file size -- test did not complete.
* | Unknown error
F | Failing test
. | Passing test
----------------------------------------------------------
"""
import argparse
import numpy as np
import os
TOL = 1.e-5
def run_comparison(testname):
fname_gold = os.path.join('..', 'tests_gold', '%s.stdout'%testname)
try:
gold = np.loadtxt(fname_gold, skiprows=1)
except IOError:
print 'ERROR: cannot find gold file "%s", bad testname?'%fname_gold
return 'G'
fname_mine = '%s.stdout'%testname
try:
mine = np.loadtxt(fname_mine, skiprows=1)
except IOError:
print 'ERROR: cannot find my file "%s", failed test run?'%fname_mine
return 'D'
try:
close = np.allclose(gold, mine)
except ValueError:
return 'E'
if close:
return '.'
else:
return 'F'
if __name__ == "__main__":
parser = argparse.ArgumentParser(__doc__)
parser.add_argument('testnames', nargs='+',
help='Name of tests to run')
args = parser.parse_args()
results = []
print 'Comparing tests:',
for test in args.testnames:
try:
results.append(run_comparison(test))
except Exception as err:
print err
results.append('*')
print ''.join(results)
......@@ -7,36 +7,14 @@
#include <cstring>
#include <vector>
#include <iostream>
#include <iomanip>
#include <numeric>
#include "utils.hh"
#include "readers.hh"
using namespace std;
#include "CanopyHydrology.hh"
#define handle_error( status, what ) \
do { \
if ( status ) \
{ \
std::cout \
<< __FILE__ \
<< ':' \
<< __LINE__ \
<< ':' \
<< what \
<< " failed with rc = " \
<< status \
<< ':' \
<< nc_strerror( status ) \
<< '\n' ; \
abort() ; \
} \
} while ( 0 )
int main(int argc, char ** argv)
{
// dimensions
......@@ -60,14 +38,14 @@ int main(int argc, char ** argv)
// phenology state
ELM::Utils::Matrix<> elai(n_grid_cells, n_pfts);
ELM::Utils::Matrix<> esai(n_grid_cells, n_pfts);
ELM::Utils::read_phenology("links/surfacedataWBW.nc", n_months, n_pfts, 0, elai, esai);
ELM::Utils::read_phenology("links/surfacedataBRW.nc", n_months, n_pfts, n_months, elai, esai);
ELM::Utils::read_phenology("../links/surfacedataWBW.nc", n_months, n_pfts, 0, elai, esai);
ELM::Utils::read_phenology("../links/surfacedataBRW.nc", n_months, n_pfts, n_months, elai, esai);
// forcing state
ELM::Utils::Matrix<> forc_rain(n_max_times, n_grid_cells);
ELM::Utils::Matrix<> forc_snow(n_max_times, n_grid_cells);
ELM::Utils::Matrix<> forc_air_temp(n_max_times, n_grid_cells);
const int n_times = ELM::Utils::read_forcing("links/forcing", n_max_times, n_grid_cells, forc_rain, forc_snow, forc_air_temp);
const int n_times = ELM::Utils::read_forcing("../links/forcing", n_max_times, 0, n_grid_cells, forc_rain, forc_snow, forc_air_temp);
ELM::Utils::Matrix<> forc_irrig(n_max_times, n_grid_cells); forc_irrig = 0.;
......@@ -85,7 +63,8 @@ int main(int argc, char ** argv)
std::cout << "Time\t Total Canopy Water\t Min Water\t Max Water" << std::endl;
auto min_max = std::minmax_element(h2o_can.begin(), h2o_can.end());
std::cout << 0 << "\t" << std::accumulate(h2o_can.begin(), h2o_can.end(), 0.)
std::cout << std::setprecision(16)
<< 0 << "\t" << std::accumulate(h2o_can.begin(), h2o_can.end(), 0.)
<< "\t" << *min_max.first
<< "\t" << *min_max.second << std::endl;
......@@ -112,7 +91,8 @@ int main(int argc, char ** argv)
}
auto min_max = std::minmax_element(h2o_can.begin(), h2o_can.end());
std::cout << t << "\t" << std::accumulate(h2o_can.begin(), h2o_can.end(), 0.)
std::cout << std::setprecision(16)
<< t+1 << "\t" << std::accumulate(h2o_can.begin(), h2o_can.end(), 0.)
<< "\t" << *min_max.first
<< "\t" << *min_max.second << std::endl;
......
#include <netcdf.h>
#include <array>
#include <sstream>
#include <iterator>
#include <exception>
#include <string>
#include <stdlib.h>
#include <cstring>
#include <vector>
#include <iostream>
#include <iomanip>
#include "utils.hh"
#include "readers.hh"
#include "CanopyHydrology.hh"
int main(int argc, char ** argv)
{
// dimensions
const int n_months = 12;
const int n_pfts = 17;
const int n_max_times = 31 * 24 * 2; // max days per month times hours per
// day * half hour timestep
// fixed magic parameters for now
const int ctype = 1;
const int ltype = 1;
const bool urbpoi = false;
const bool do_capsnow = false;
const int frac_veg_nosno = 1;
int n_irrig_steps_left = 0;
const double dewmx = 0.1;
const double dtime = 1800.0;
// phenology state
ELM::Utils::Matrix<> elai(n_months, n_pfts);
ELM::Utils::Matrix<> esai(n_months, n_pfts);
ELM::Utils::read_phenology("../links/surfacedataWBW.nc", n_months, n_pfts, 0, elai, esai);
// forcing state
ELM::Utils::Matrix<> forc_rain(n_max_times, 1);
ELM::Utils::Matrix<> forc_snow(n_max_times, 1);
ELM::Utils::Matrix<> forc_air_temp(n_max_times, 1);
const int n_times = ELM::Utils::read_forcing("../links/forcing", n_max_times, 6, 1, forc_rain, forc_snow, forc_air_temp);
double h2ocan = 0.0;
double qflx_prec_intr = 0.;
double qflx_irrig = 0.;
double qflx_prec_grnd = 0.;
double qflx_snwcp_liq = 0.;
double qflx_snwcp_ice = 0.;
double qflx_snow_grnd_patch = 0.;
double qflx_rain_grnd = 0.;
std::cout << "Timestep, forc_rain, h2ocan, qflx_prec_grnd, qflx_prec_intr" << std::endl;
for(size_t itime = 0; itime < n_times; itime += 1) {
// note this call puts all precip as rain for testing
double total_precip = forc_rain(itime, 0) + forc_snow(itime, 0);
ELM::CanopyHydrologyKern1(dtime, total_precip, 0., 0.,
ltype, ctype, urbpoi, do_capsnow,
elai(5,7), esai(5,7), dewmx, frac_veg_nosno,
h2ocan, n_irrig_steps_left,
qflx_prec_intr, qflx_irrig, qflx_prec_grnd,
qflx_snwcp_liq, qflx_snwcp_ice,
qflx_snow_grnd_patch, qflx_rain_grnd);
std::cout << std::setprecision(16) << itime+1 << "\t" << total_precip << "\t" << h2ocan<< "\t" << qflx_prec_grnd << "\t" << qflx_prec_intr << std::endl;
}
return 0;
}
SRCDIR = ../src/fortran/
CSRCDIR = ../src/cpp/
OBJECT = ../src/
SRCDIR = ../../src/fortran/
CSRCDIR = ../../src/cpp/
OBJECT = ../../src/
include $(OBJECT)config/Makefile.config
......@@ -21,16 +21,17 @@ default: all
all: links library $(TESTS)
test: $(EXEC_TESTS)
echo "done"
@echo "done"
CanopyHydrology_kern1_single: test_CanopyHydrology_kern1_single readers.hh utils.hh
CanopyHydrology_kern1_single: test_CanopyHydrology_kern1_single
./test_CanopyHydrology_kern1_single &> test_CanopyHydrology_kern1_single.stdout
CanopyHydrology_kern1_multiple: test_CanopyHydrology_kern1_multiple readers.hh utils.hh
./test_CanopyHydrology_kern1_multiple
CanopyHydrology_kern1_multiple: test_CanopyHydrology_kern1_multiple
./test_CanopyHydrology_kern1_multiple &> test_CanopyHydrology_kern1_multiple.stdout
test_%: %.o
test_%: %.o readers.hh utils.hh
$(LINKER) $(FCFLAGS) -L$(OBJECT)fortran -lelm -L$(NETCDF_ROOT)/lib -lnetcdf -L$(STD_LIB_ROOT)/lib -lc++ $< -o $@
......@@ -45,7 +46,7 @@ allclean:
links:
@echo "making in links"
$(MAKE) -C links links
$(MAKE) -C ../links links
library:
$(MAKE) -C $(OBJECT) all
......@@ -87,13 +87,13 @@ void read_phenology(const std::string& fname,
// -----------------------------------------------------------------------------
template<typename Matrix_t>
int read_forcing(const std::string& fname,
size_t n_times, size_t n_grid_cells,
size_t n_times, size_t start_grid_cell, size_t n_grid_cells,
Matrix_t& rain, Matrix_t& snow, Matrix_t& temp) {
size_t min_ntimes = n_times;
for (size_t lcv_gc=0; lcv_gc!=n_grid_cells; ++lcv_gc) {
std::stringstream fname_full;
fname_full << fname << lcv_gc+1 << ".nc";
fname_full << fname << lcv_gc+1+start_grid_cell << ".nc";
int ncid = -1;
auto status = nc_open(fname_full.str().c_str(), NC_NOWRITE, &ncid);
......
......@@ -6,9 +6,9 @@ program CanopyHydrology_kern1_multiple
bool => shr_kind_bool
implicit none
character (len = *), dimension(2),parameter :: SURFDAT_FILE_NAME = (/"links/surfacedataWBW.nc", "links/surfacedataBRW.nc"/)
character (len = *), dimension(2),parameter :: SURFDAT_FILE_NAME = (/"../links/surfacedataWBW.nc", "../links/surfacedataBRW.nc"/)
character (len = 100) :: FORCDAT_FILE_NAME
character (len = *), parameter :: FORCDAT_BASE = "links/forcing"
character (len = *), parameter :: FORCDAT_BASE = "../links/forcing"
integer(i4) :: frac_veg_nosno = 1
integer(i4), parameter :: npfts=17, nmonths=12, ngrcs=24
......
......@@ -7,8 +7,8 @@ program CanopyHydrology_kern1_single
implicit none
character (len = *), parameter :: SURFDAT_FILE_NAME = "links/surfacedataWBW.nc"
character (len = *), parameter :: FORCDAT_FILE_NAME = "links/forcing8.nc"
character (len = *), parameter :: SURFDAT_FILE_NAME = "../links/surfacedataWBW.nc"
character (len = *), parameter :: FORCDAT_FILE_NAME = "../links/forcing7.nc"
integer(i4), parameter :: npfts=17, nmonths=12
integer(i4) :: ltype=1, ctype=1
......
SRCDIR = ../src/fortran/
OBJECT = ../src/
SRCDIR = ../../src/fortran/
OBJECT = ../../src/
include $(OBJECT)config/Makefile.config
FCFLAGS += -I$(SRCDIR) -I$(SRCDIR)$(ELM_UTILS_DIR) -I$(NETCDF_ROOT)/include
......@@ -18,15 +18,14 @@ default: all
all: links library $(TESTS)
test: $(EXEC_TESTS)
echo "done"
python ../compare_to_gold.py $(TESTS)
CanopyHydrology_kern1_single: test_CanopyHydrology_kern1_single
./test_CanopyHydrology_kern1_single
./test_CanopyHydrology_kern1_single &> test_CanopyHydrology_kern1_single.stdout
CanopyHydrology_kern1_multiple: test_CanopyHydrology_kern1_multiple
./test_CanopyHydrology_kern1_multiple
./test_CanopyHydrology_kern1_multiple &> test_CanopyHydrology_kern1_multiple.stdout
test_%: %.o
......@@ -44,7 +43,7 @@ allclean:
links:
@echo "making in links"
$(MAKE) -C links links
$(MAKE) -C ../links links
library:
$(MAKE) -C $(OBJECT) all
This diff is collapsed.
This diff is collapsed.
#include <netcdf.h>
#include <array>
#include <sstream>
#include <iterator>
#include <exception>
#include <string>
#include <stdlib.h>
#include <cstring>
#include <vector>
#include <iostream>
using namespace std;
#include "CanopyHydrology.hh"
#define handle_error( status, what ) \
do { \
if ( status ) \
{ \
std::cout \
<< __FILE__ \
<< ':' \
<< __LINE__ \
<< ':' \
<< what \
<< " failed with rc = " \
<< status \
<< ':' \
<< nc_strerror( status ) \
<< '\n' ; \
abort() ; \
} \
} while ( 0 )
int main(int argc, char ** argv)
{
int ctype = 1;
const int nmonths = 12;
const int npfts = 17;
bool urbpoi = false,do_capsnow=false;
const double dtime = 1800.0;
double elai = 0, esai = 0, dewmx = 0, forc_snow = 0, forc_rain = 0, qflx_prec_intr = 0, qflx_irrig = 0, qflx_prec_grnd = 0, qflx_snwcp_liq = 0, qflx_snwcp_ice = 0, qflx_snow_grnd_patch = 0, qflx_rain_grnd = 0,irrig_rate = 0 ,h2ocan = 0;
int frac_veg_nosno = 1;
int ipft,itime ;
size_t ntimes=0;
const int ltype = 1;
double monthly_lai[nmonths][npfts] = {};
double monthly_sai[nmonths][npfts] = {};
int n_irrig_steps_left = 0;
double * total_precip = NULL;
int ncid= 0, varid= 0, dimid= 0, status= 0;
/*! use netcdf*/
/*! This will be the netCDF ID for the file and data variable.*/
status = nc_open("links/surfacedataWBW.nc", NC_NOWRITE, &ncid);
handle_error( status, "nc_create" ) ;
status = nc_inq_varid(ncid, "MONTHLY_LAI", &varid);
handle_error( status, "nc_inq_varid" ) ;
array<size_t,4> start{ nmonths,npfts, 1, 1 };
array<size_t,4> count{ 0,0,0,0 };
status = nc_get_vara_double(ncid, varid, count.data(), start.data(), *monthly_lai);
handle_error( status, "nc_get_vara_double monthly_lai" ) ;
status = nc_inq_varid(ncid, "MONTHLY_SAI", &varid);
handle_error( status, "nc_inq_varid" ) ;
/*!do ipft=1,npfts */
/*! print *, monthly_lai(ipft,12), monthly_lai(ipft,3), monthly_lai(ipft,6) */
/*!end do */
ipft = 1;
std::cout << "[ " << monthly_lai[12][ipft] << " , " << monthly_lai[3][ipft] << " , " << monthly_lai[6][ipft] << " ]" << std::endl;
status = nc_get_vara_double(ncid, varid, count.data(), start.data(), *monthly_sai);
handle_error( status, "nc_get_vara_double monthly_sai" ) ;
status = nc_close(ncid);
handle_error( status, "nc_close1" ) ;
status = nc_open("links/forcing7.nc", NC_NOWRITE, &ncid);
handle_error( status, "nc_create" ) ;
status = nc_inq_dimid(ncid, "time", &dimid);
handle_error( status, "nc_inq_dimid" ) ;
status = nc_inq_dimlen(ncid, dimid, &ntimes );
handle_error( status, "nc_inq_dimlen" ) ;
array<size_t,3> start3{ntimes, 1, 1};
array<size_t,3> count3{ 0,0,0 };
total_precip = (double*)malloc(ntimes*1*1*sizeof(double));
status = nc_inq_varid(ncid,"PRECTmms", &varid);
handle_error( status, "nc_inq_varid" ) ;
status = nc_get_vara_double(ncid, varid, count3.data(), start3.data(), total_precip);
handle_error( status, "nc_get_vara_double total_precip" ) ;
status = nc_close(ncid);
handle_error( status, "nc_close2" ) ;
//cout.precision(10);
h2ocan = 0.0;
for(itime = 0; itime < ntimes; itime += 1){
forc_rain = total_precip[itime];
forc_snow = 0.0;
dewmx = 0.1;
elai = monthly_lai[5][7];
esai = monthly_sai[5][7];
frac_veg_nosno = 1;
irrig_rate = 0.0;
n_irrig_steps_left = 0;
urbpoi = false;
do_capsnow = false;
ELM::CanopyHydrologyKern1(dtime, forc_rain, forc_snow, irrig_rate,
ltype, ctype, urbpoi, do_capsnow,
elai, esai, dewmx, frac_veg_nosno,
h2ocan, n_irrig_steps_left,
qflx_prec_intr, qflx_irrig, qflx_prec_grnd,
qflx_snwcp_liq, qflx_snwcp_ice,
qflx_snow_grnd_patch, qflx_rain_grnd);
//printf("[ %d,%E,%4.4f,%E,%E]\n",itime, forc_rain, h2ocan, qflx_prec_grnd, qflx_prec_intr);
std::cout << "[ " << itime << " , " << forc_rain<< " , " << h2ocan<< " , " << qflx_prec_grnd<< " , " << qflx_prec_intr << " ]" << std::endl;
}
return 0;
}
../tests/links
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment