LBPM/tests/TestNetcdf.cpp

124 lines
3.7 KiB
C++
Raw Normal View History

2016-06-27 09:09:14 -05:00
// Test reading/writing netcdf files
2016-02-22 15:26:35 -06:00
#include "IO/netcdf.h"
2021-01-22 09:05:16 -06:00
#include "common/MPI.h"
2016-06-27 09:09:14 -05:00
#include "common/Communication.h"
#include "common/UnitTest.h"
2016-02-22 15:26:35 -06:00
#include "ProfilerApp.h"
2016-06-27 09:09:14 -05:00
void load( const std::string& );
void test_NETCDF( UnitTest& ut )
2016-02-22 15:26:35 -06:00
{
2021-01-22 09:05:16 -06:00
Utilities::MPI comm( MPI_COMM_WORLD );
int rank = comm.getRank();
2016-06-27 09:09:14 -05:00
int nprocx = 2;
int nprocy = 2;
int nprocz = 2;
RankInfoStruct info( rank, nprocx, nprocy, nprocz );
Array<float> data( 4, 5, 6 );
for (size_t i=0; i<data.length(); i++)
2016-06-28 15:00:30 -05:00
data(i) = 120*rank + i;
2016-06-27 09:09:14 -05:00
size_t x = info.ix*data.size(0);
size_t y = info.jy*data.size(1);
size_t z = info.kz*data.size(2);
const char* filename = "test.nc";
2017-08-15 07:36:31 -05:00
std::vector<int> dim = { (int) data.size(0)*nprocx, (int) data.size(1)*nprocy, (int) data.size(2)*nprocz };
2021-01-22 09:05:16 -06:00
int fid = netcdf::open( filename, netcdf::CREATE, comm );
2016-06-27 09:09:14 -05:00
auto dims = netcdf::defDim( fid, {"X", "Y", "Z"}, dim );
2016-06-28 15:00:30 -05:00
netcdf::write( fid, "tmp", dims, data, info );
2016-06-27 09:09:14 -05:00
netcdf::close( fid );
2021-01-22 09:05:16 -06:00
comm.barrier();
2016-06-27 09:09:14 -05:00
// Read the contents of the file we created
fid = netcdf::open( filename, netcdf::READ );
Array<float> tmp = netcdf::getVar<float>( fid, "tmp" );
if ( (int)tmp.size(0)!=dim[0] || (int)tmp.size(1)!=dim[1] || (int)tmp.size(2)!=dim[2] ) {
ut.failure("Array sizes do not match");
return;
}
bool pass = true;
for (size_t i=0; i<data.size(0); i++) {
for (size_t j=0; j<data.size(1); j++) {
for (size_t k=0; k<data.size(2); k++) {
pass = pass && data(i,j,k) == tmp(i+x,j+y,k+z);
}
}
}
if ( pass ) {
ut.passes("write/read simple parallel file");
} else {
ut.failure("write/read simple parallel file");
}
}
inline void print( const std::string& name, const std::vector<size_t> size )
{
printf(" Reading variable %s (%i",name.c_str(),(int)size[0]);
for (size_t i=1; i<size.size(); i++)
printf(",%i",(int)size[i]);
printf(")\n");
}
void load( const std::string& filename )
{
printf("Reading %s\n",filename.c_str());
int fid = netcdf::open( filename, netcdf::READ );
2016-02-22 15:26:35 -06:00
std::vector<std::string> vars = netcdf::getVarNames( fid );
for (size_t i=0; i<vars.size(); i++) {
netcdf::VariableType type = netcdf::getVarType( fid, vars[i] );
2016-06-27 09:09:14 -05:00
print( vars[i], netcdf::getVarDim(fid,vars[i]) );
2016-02-22 15:26:35 -06:00
if ( type == netcdf::STRING )
Array<std::string> tmp = netcdf::getVar<std::string>( fid, vars[i] );
else if ( type == netcdf::SHORT )
Array<short> tmp = netcdf::getVar<short>( fid, vars[i] );
else
Array<double> tmp = netcdf::getVar<double>( fid, vars[i] );
}
std::vector<std::string> attr = netcdf::getAttNames( fid );
for (size_t i=0; i<attr.size(); i++) {
2016-06-27 09:09:14 -05:00
printf(" Reading attribute %s\n",attr[i].c_str());
2016-02-22 15:26:35 -06:00
netcdf::VariableType type = netcdf::getAttType( fid, attr[i] );
if ( type == netcdf::STRING )
Array<std::string> tmp = netcdf::getAtt<std::string>( fid, attr[i] );
else
Array<double> tmp = netcdf::getAtt<double>( fid, attr[i] );
}
netcdf::close( fid );
}
int main(int argc, char **argv)
{
2016-06-27 09:09:14 -05:00
// Initialize MPI
2020-10-08 10:03:42 -05:00
Utilities::startup( argc, argv );
2020-01-28 07:51:32 -06:00
Utilities::MPI comm( MPI_COMM_WORLD );
const int rank = comm.getRank();
2016-06-27 09:09:14 -05:00
UnitTest ut;
2016-02-22 15:26:35 -06:00
PROFILE_START("Main");
2016-06-27 09:09:14 -05:00
// Test reading existing netcdf files
if ( rank==0 ) {
for (int i=1; i<argc; i++)
load( argv[i] );
2016-02-22 15:26:35 -06:00
}
2016-06-27 09:09:14 -05:00
// Test writing/reading netcdf file
test_NETCDF( ut );
2016-02-22 15:26:35 -06:00
2016-06-27 09:09:14 -05:00
// Print the results
ut.report();
int N_errors = ut.NumFailGlobal();
2016-02-22 15:26:35 -06:00
PROFILE_SAVE("TestNetcdf");
2016-06-27 09:09:14 -05:00
// Close MPI
2020-01-28 11:33:36 -06:00
comm.barrier();
2020-10-08 10:03:42 -05:00
Utilities::shutdown();
2016-06-27 09:09:14 -05:00
return N_errors;
2016-02-22 15:26:35 -06:00
}