Commit bb0d6a7b authored by Coon, Ethan's avatar Coon, Ethan
Browse files

fixes bugs in utils, working test_c_c test with dynamically sized matrix

parent 66780564
......@@ -11,7 +11,6 @@ Error codes:
----------------------------------------------------------
"""
from colorama import init
from termcolor import colored
import argparse
import numpy as np
......@@ -25,7 +24,7 @@ def run_comparison(testname, full_message=False):
try:
gold = np.loadtxt(fname_gold, skiprows=1)
except IOError:
print 'ERROR: cannot find gold file "%s", bad testname?'%fname_gold
print('ERROR: cannot find gold file "%s", bad testname?'%fname_gold)
return colored('Error in the gold file ', 'magenta')
# get my file
......@@ -37,7 +36,7 @@ def run_comparison(testname, full_message=False):
try:
mine = np.loadtxt(fname_mine, skiprows=1)
except IOError:
print 'ERROR: cannot find my file "%s", failed test run?'%fname_mine
print('ERROR: cannot find my file "%s", failed test run?'%fname_mine)
return colored('Error in my file ', 'cyan')
# compare
......@@ -45,8 +44,8 @@ def run_comparison(testname, full_message=False):
close = np.allclose(gold, mine)
except ValueError as err:
if full_message:
print ''
print 'Test: "%s" FAILED with error: "%r"'%(testname, err)
print('')
print('Test: "%s" FAILED with error: "%r"'%(testname, err))
return colored('Error in my file size ','yellow')
if close:
......@@ -64,11 +63,11 @@ if __name__ == "__main__":
args = parser.parse_args()
results = []
print 'Comparing tests:',
print('Comparing tests:')
for test in args.testnames:
try:
results.append(run_comparison(test, args.verbose))
except Exception as err:
print err
print(err)
results.append('N/A ')
print ''.join(results)
print(''.join(results))
......@@ -25,15 +25,6 @@ using namespace std::chrono;
namespace ELM {
namespace Utils {
static const int n_months = 12;
static const int n_pfts = 17;
static const int n_max_times = 31 * 24 * 2; // max days per month times hours per
// day * half hour timestep
static const int n_grid_cells = 24;
using MatrixState = MatrixStatic<n_grid_cells, n_pfts>;
using MatrixForc = MatrixStatic<n_max_times,n_grid_cells>;
} // namespace
} // namespace
......@@ -41,11 +32,14 @@ using MatrixForc = MatrixStatic<n_max_times,n_grid_cells>;
int main(int argc, char ** argv)
{
using ELM::Utils::n_months;
using ELM::Utils::n_pfts;
using ELM::Utils::n_grid_cells;
using ELM::Utils::n_max_times;
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
const int n_grid_cells = 24;
#ifdef MPICOMP
int myrank, numprocs;
double mytime, maxtime, mintime, avgtime;
......@@ -68,40 +62,32 @@ int main(int argc, char ** argv)
const double dtime = 1800.0;
// phenology state
ELM::Utils::MatrixState elai;
ELM::Utils::MatrixState esai;
ELM::Utils::Matrix<double> elai(n_grid_cells, n_pfts);
ELM::Utils::Matrix<double> 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);
// forcing state
ELM::Utils::MatrixForc forc_rain;
ELM::Utils::MatrixForc forc_snow;
ELM::Utils::MatrixForc forc_air_temp;
ELM::Utils::Matrix<double> forc_rain(n_max_times,n_grid_cells, 0.);
ELM::Utils::Matrix<double> forc_snow(n_max_times,n_grid_cells, 0.);
ELM::Utils::Matrix<double> forc_air_temp(n_max_times,n_grid_cells, 0.);
ELM::Utils::Matrix<double> forc_irrig(n_max_times,n_grid_cells, 0.);
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::MatrixForc forc_irrig; forc_irrig = 0.;
// output state by the grid cell
// auto qflx_prec_intr = std::array<double,n_grid_cells>();
// auto qflx_irrig = std::array<double,n_grid_cells>();
// auto qflx_prec_grnd = std::array<double,n_grid_cells>();
// auto qflx_snwcp_liq = std::array<double,n_grid_cells>();
// auto qflx_snwcp_ice = std::array<double,n_grid_cells>();
// auto qflx_snow_grnd_patch = std::array<double,n_grid_cells>();
// auto qflx_rain_grnd = std::array<double,n_grid_cells>();
auto qflx_prec_intr = ELM::Utils::MatrixState();
auto qflx_irrig = ELM::Utils::MatrixState();
auto qflx_prec_grnd = ELM::Utils::MatrixState();
auto qflx_snwcp_liq = ELM::Utils::MatrixState();
auto qflx_snwcp_ice = ELM::Utils::MatrixState();
auto qflx_snow_grnd_patch = ELM::Utils::MatrixState();
auto qflx_rain_grnd = ELM::Utils::MatrixState();
auto qflx_prec_intr = ELM::Utils::Matrix<double>(n_grid_cells, n_pfts, 0.);
auto qflx_irrig = ELM::Utils::Matrix<double>(n_grid_cells, n_pfts, 0.);
auto qflx_prec_grnd = ELM::Utils::Matrix<double>(n_grid_cells, n_pfts, 0.);
auto qflx_snwcp_liq = ELM::Utils::Matrix<double>(n_grid_cells, n_pfts, 0.);
auto qflx_snwcp_ice = ELM::Utils::Matrix<double>(n_grid_cells, n_pfts, 0.);
auto qflx_snow_grnd_patch = ELM::Utils::Matrix<double>(n_grid_cells, n_pfts, 0.);
auto qflx_rain_grnd = ELM::Utils::Matrix<double>(n_grid_cells, n_pfts, 0.);
// output state by the pft
auto h2o_can = ELM::Utils::MatrixState(); h2o_can = 0.;
auto h2o_can = ELM::Utils::Matrix<double>(n_grid_cells, n_pfts, 0.0);
#ifdef TRACE
#ifdef DEBUG
std::ofstream soln_file;
soln_file.open("test_CanopyHydrology_kern1_multiple.soln");
......@@ -117,9 +103,9 @@ int main(int argc, char ** argv)
auto start = high_resolution_clock::now();
#ifdef MPICOMP
#ifdef MPICOMP
mytime = MPI_Wtime();
#endif
#endif
// main loop
// -- the timestep loop cannot/should not be parallelized
for (size_t t = 0; t != n_times; ++t) {
......@@ -146,7 +132,7 @@ int main(int argc, char ** argv)
}
}
#ifdef TRACE
#ifdef DEBUG
{
auto min_max = std::minmax_element(h2o_can.begin(), h2o_can.end());
soln_file << std::setprecision(16)
......@@ -154,7 +140,7 @@ int main(int argc, char ** argv)
<< "\t" << *min_max.first
<< "\t" << *min_max.second << std::endl;
}
#endif
#endif
}
auto stop = high_resolution_clock::now();
......
......@@ -24,21 +24,16 @@ using namespace std::chrono;
namespace ELM {
namespace Utils {
static const int n_months = 12;
static const int n_pfts = 17;
using MatrixState = MatrixStatic<n_months, n_pfts>;
static const int n_max_times = 31 * 24 * 2; // max days per month times hours per
// day * half hour timestep
using MatrixForc = MatrixStatic<n_max_times,1>;
} // namespace
} // namespace
int main(int argc, char ** argv)
{
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
#ifdef MPICOMP
int myrank, numprocs;
double mytime, maxtime, mintime, avgtime;
......@@ -47,11 +42,6 @@ int main(int argc, char ** argv)
MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
MPI_Barrier(MPI_COMM_WORLD);
#endif
// 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;
......@@ -66,14 +56,14 @@ int main(int argc, char ** argv)
const double dtime = 1800.0;
// phenology state
ELM::Utils::MatrixState elai;
ELM::Utils::MatrixState esai;
ELM::Utils::Matrix<double> elai(n_months, n_pfts);
ELM::Utils::Matrix<double> esai(n_months, n_pfts);
ELM::Utils::read_phenology("../links/surfacedataWBW.nc", n_months, n_pfts, 0, elai, esai);
// forcing state
ELM::Utils::MatrixForc forc_rain;
ELM::Utils::MatrixForc forc_snow;
ELM::Utils::MatrixForc forc_air_temp;
ELM::Utils::Matrix<double> forc_rain(n_max_times, 1);
ELM::Utils::Matrix<double> forc_snow(n_max_times, 1);
ELM::Utils::Matrix<double> 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;
......@@ -85,16 +75,16 @@ int main(int argc, char ** argv)
double qflx_snow_grnd_patch = 0.;
double qflx_rain_grnd = 0.;
#ifdef TRACE
#ifdef DEBUG
std::ofstream soln_file;
soln_file.open("test_CanopyHydrology_kern1_single.soln");
soln_file << "Timestep, forc_rain, h2ocan, qflx_prec_grnd, qflx_prec_intr" << std::endl;
#endif
auto start = high_resolution_clock::now();
#ifdef MPICOMP
#ifdef MPICOMP
mytime = MPI_Wtime();
#endif
#endif
for(size_t itime = 0; itime < n_times; itime += 1) {
// note this call puts all precip as rain for testing
......@@ -106,9 +96,9 @@ int main(int argc, char ** argv)
qflx_prec_intr, qflx_irrig, qflx_prec_grnd,
qflx_snwcp_liq, qflx_snwcp_ice,
qflx_snow_grnd_patch, qflx_rain_grnd);
#ifdef TRACE
#ifdef DEBUG
soln_file << std::setprecision(16) << itime+1 << "\t" << total_precip << "\t" << h2ocan<< "\t" << qflx_prec_grnd << "\t" << qflx_prec_intr << std::endl;
#endif
#endif
}
......
......@@ -32,15 +32,15 @@ int main(int argc, char ** argv)
std::size_t n_max_times = 31 * 24 * 2;
std::size_t n_levels_snow = 5;
#ifdef MPICOMP
int myrank, numprocs;
double mytime, maxtime, mintime, avgtime;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
MPI_Barrier(MPI_COMM_WORLD);
assert(NUMPROCS == n_ranks && "Compile-time sizes set so that code must be run with 6 mpi processes.");
#endif
#ifdef MPICOMP
int myrank, numprocs;
double mytime, maxtime, mintime, avgtime;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
MPI_Barrier(MPI_COMM_WORLD);
assert(NUMPROCS == n_ranks && "Compile-time sizes set so that code must be run with 6 mpi processes.");
#endif
// fixed magic parameters for now
const int ctype = 1;
......@@ -122,7 +122,7 @@ int main(int argc, char ** argv)
auto frac_sno_eff = ELM::Utils::Vector<double>(n_grid_cells, 0.);
auto frac_sno = ELM::Utils::Vector<double>(n_grid_cells, 0.);
#ifdef TRACE
#ifdef DEBUG
std::ofstream soln_file;
soln_file.open("test_CanopyHydrology_module.soln");
{
......@@ -141,11 +141,11 @@ int main(int argc, char ** argv)
<< "\t" << sum_snow << "\t" << *min_max_snow.first << "\t" << *min_max_snow.second
<< "\t" << avg_frac_sfc << "\t" << *min_max_frac_sfc.first << "\t" << *min_max_frac_sfc.second << std::endl;
}
#endif
#endif
auto start = high_resolution_clock::now();
#ifdef MPICOMP
#ifdef MPICOMP
mytime = MPI_Wtime();
#endif
#endif
// main loop
// -- the timestep loop cannot/should not be parallelized
for (size_t t = 0; t != n_times; ++t) {
......@@ -212,7 +212,7 @@ int main(int argc, char ** argv)
qflx_h2osfc2topsoi[g], frac_h2osfc[g]);
} // end grid cell loop
#ifdef TRACE
#ifdef DEBUG
auto min_max_water = std::minmax_element(h2ocan.begin(), h2ocan.end());
auto sum_water = std::accumulate(h2ocan.begin(), h2ocan.end(), 0.);
......@@ -226,10 +226,11 @@ int main(int argc, char ** argv)
<< t+1 << "\t" << sum_water << "\t" << *min_max_water.first << "\t" << *min_max_water.second
<< "\t" << sum_snow << "\t" << *min_max_snow.first << "\t" << *min_max_snow.second
<< "\t" << avg_frac_sfc << "\t" << *min_max_frac_sfc.first << "\t" << *min_max_frac_sfc.second << std::endl;
#endif
#endif
} // end timestep loop
auto stop = high_resolution_clock::now();
#ifdef MPICOMP
#ifdef MPICOMP
mytime = MPI_Wtime() - mytime;
std::cout <<"Timing from node "<< myrank << " is "<< mytime << "seconds." << std::endl;
......@@ -241,7 +242,7 @@ int main(int argc, char ** argv)
std::cout << "Min: "<< mintime << ", Max: " << maxtime << ", Avg: " <<avgtime << std::endl;
}
MPI_Finalize();
#endif
#endif
auto duration = duration_cast<microseconds>(stop - start);
std::cout << "Time taken by function: "<< duration.count() << " microseconds" << std::endl;
......
......@@ -19,15 +19,17 @@ EXEC_TESTS = CanopyHydrology_kern1_single \
.PHONY: links library test compute
#default: all
default: test
include $(ELM_BASE)/config/Makefile.logo
#all: logo links library $(TESTS)
test: logo links library $(TESTS) $(EXEC_TESTS)
test: debug logo links library $(TESTS) $(EXEC_TESTS)
python ../compare_to_gold.py $(TESTS)
debug:
$(eval CXXFLAGS += -DDEBUG)
CanopyHydrology_kern1_single: test_CanopyHydrology_kern1_single
./test_CanopyHydrology_kern1_single > test_CanopyHydrology_kern1_single.stdout
......@@ -37,10 +39,8 @@ CanopyHydrology_kern1_multiple: test_CanopyHydrology_kern1_multiple
CanopyHydrology_module: test_CanopyHydrology_module
./test_CanopyHydrology_module > test_CanopyHydrology_module.stdout
test_%: %.cpp.o readers.hh utils.hh
$(CXX) -DTRACE -o $@ $< $(NETCDF_FLAGS)
$(CXX) -o $@ $< $(NETCDF_FLAGS)
compute: logo links library $(TEST1)
......@@ -54,4 +54,4 @@ allclean: clean
links:
@echo "making in links"
$(MAKE) -C ../links links
\ No newline at end of file
$(MAKE) -C ../links links
......@@ -40,13 +40,15 @@ class Vector {
const T& operator[](size_t i) const { assert(0 <= i && i < N_); return d_[i]; }
void operator=(T t) {
for (size_t i=0; i!=N_; ++i) {
d_[i] = t;
for (auto& d : *this) {
d = t;
}
}
double const * begin() const { return &(d_[0]); }
double const * end() const { return &(d_[N_]); }
T const * begin() const { return &(d_[0]); }
T const * end() const { return &(d_[N_]); }
T * begin() { return d_; }
T * end() { return &(d_[N_]); }
std::size_t size() const { return N_; }
private:
......@@ -80,17 +82,18 @@ class Matrix {
const Vector<T> operator[](size_t i) const { assert(0 <= i && i < M_); return Vector<T>(N_, &d_.get()[i*N_]); }
void operator=(T t) {
for (std::size_t i=0; i!=NM_; ++i) {
d_.get()[i] = t;
for (auto& d : *this) {
d = t;
}
}
double const * begin() const { return &d_.get()[0]; }
double const * end() const { return &d_.get()[M_-1][N_-1] +1; }
T const * begin() const { return &(d_.get()[0]); }
T const * end() const { return &(d_.get()[NM_]); }
T * begin() { return &(d_.get()[0]); }
T * end() { return &(d_.get()[NM_]); }
private:
std::size_t M_, N_, NM_;
std::shared_ptr<T> d_;
};
......
Markdown is supported
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