WSL/SLF GitLab Repository

Commit 91a372c2 authored by Mathias Bavay's avatar Mathias Bavay
Browse files

Added some tests for Matrix and Grid2DObjects (the Grid3DObjects tests will have to be implemented)

parent bf27531f
......@@ -90,7 +90,7 @@ class Matrix {
* @param cols number of columns of the matrix
*/
void size(unsigned int& rows, unsigned int& cols) const;
/**
* @brief free the memory and set the matrix dimensions to (0,0)
*/
......@@ -221,11 +221,12 @@ class Matrix {
bool isIdentity() const;
static bool isIdentity(const Matrix& A);
static const double epsilon, epsilon_mtr;
protected:
std::vector<double> vecData;
unsigned int ncols;
unsigned int nrows;
static const double epsilon, epsilon_mtr;
unsigned int findMaxInCol(const unsigned int &col);
unsigned int findMaxInRow(const unsigned int &row);
......
......@@ -32,3 +32,4 @@ ADD_SUBDIRECTORY(meteo_reading_no_interpol)
ADD_SUBDIRECTORY(sun)
ADD_SUBDIRECTORY(dem_reading)
ADD_SUBDIRECTORY(2D_interpolations)
ADD_SUBDIRECTORY(arrays)
## Test arrays
# generate executable
ADD_EXECUTABLE(arrays arrays.cc)
TARGET_LINK_LIBRARIES(arrays ${LIBRARIES})
# add the tests
ADD_TEST(arrays.smoke arrays)
SET_TESTS_PROPERTIES(arrays.smoke PROPERTIES LABELS smoke)
#include <time.h>
#include <meteoio/MeteoIO.h>
using namespace std;
using namespace mio;
bool grid2d(const unsigned int& n) {
cout << "Testing Grid2DObject\n";
bool status = true;
srand((unsigned)time(0));
const double range = 10.;
Coords llcorner("CH1903","");
llcorner.setXY(785425. , 191124., 1400.);
Grid2DObject grid1(n, n, 100, llcorner);
for(unsigned int jj=0; jj<grid1.getNy(); jj++) {
for(unsigned int ii=0; ii<grid1.getNx(); ii++) {
grid1.grid2D(ii,jj) = (double)rand()/(double)RAND_MAX*range;
}
}
Grid2DObject grid2(n, n, 100, llcorner, grid1.grid2D);
grid2.grid2D -= 8.;
grid2.grid2D /= 2.;
grid2.grid2D += 4.;
grid2.grid2D *= 2.;
if(!grid2.grid2D.checkEpsilonEquality(grid1.grid2D, 1e-6)) {
cout << "\terror: basic operations with constants fail!\n";
status=false;
}
grid2.set(n, n, 100, llcorner, 0.);
grid2.grid2D += grid1.grid2D;
grid2.grid2D /= grid1.grid2D;
if(!IOUtils::checkEpsilonEquality(grid2.grid2D.getMean(), 1., 1e-6)) {
cout << "\terror: grids addition or division fails!\n";
status=false;
}
grid2.grid2D *= grid1.grid2D;
if(!grid2.grid2D.checkEpsilonEquality(grid1.grid2D, 1e-6)) {
cout << "\terror: grids multiplication fails!\n";
status=false;
}
grid2.grid2D -= grid1.grid2D;
if(!IOUtils::checkEpsilonEquality(grid2.grid2D.getMean(), 0., 1e-6)) {
cout << "\terror: grids substraction fails!\n";
status=false;
}
return status;
}
bool matrix(const unsigned int& n) {
cout << "Testing Matrix\n";
bool status=true;
Matrix I(n,1.); //build an n*n identity matrix
Matrix m1(n,n);
m1.random(10.);
const double det = m1.det();
const Matrix m1_trans = m1.getT();
const double det_trans = m1_trans.det();
if(!IOUtils::checkEpsilonEquality(det, det_trans, Matrix::epsilon_mtr*fabs(det))) {
cout << "\terror: m1.det != m1T.det\n";
status=false;
}
Matrix m2 = m1-8.;
m2 /= 2.;
m2 += 4.;
m2 *= 2.;
if(m2!=m1) {
cout << "\terror: basic operations with constants fail!\n";
status=false;
}
m2 = m1.getInv();
Matrix m3=m1*m2;
if(m3.isIdentity()!=true) {
cout << "\terror: m1*inv(m1) is NOT identity matrix\n";
status=false;
}
m3 = m1*m1;
if(m2*m3 != m1) {
cout << "\terror when multiplying matrix\n";
status=false;
}
Matrix m4=Matrix::solve(m1,I); //solve m1*X=I
if(m1*m4 != I){
cout << "\terror when solving A*X=B\n";
status=false;
}
Matrix L,U;
if(m1.LU(L,U)==false) {
cout << "\terror: LU decomposition could NOT be computed\n";
status=false;
}
return status;
}
int main() {
const unsigned int n=50;
const bool grid2d_status = grid2d(n);
const bool matrix_status = matrix(n);
if(grid2d_status!=true || matrix_status!=true) throw IOException("Grid/Matrix error", AT);
return 0;
}
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