COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-matrix-inverter.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2013 von Karman Institute for Fluid Dynamics, Belgium
2 //
3 // This software is distributed under the terms of the
4 // GNU Lesser General Public License version 3 (LGPLv3).
5 // See doc/lgpl.txt and doc/gpl.txt for the license text.
6 
7 #define BOOST_TEST_DYN_LINK
8 #define BOOST_TEST_MODULE "Test matrix inverter"
9 
10 #include <boost/test/unit_test.hpp>
11 #include <boost/foreach.hpp>
12 
14 #include <Eigen/SVD>
15 
16 #include "common/Log.hpp"
17 
18 #include "math/MatrixTypes.hpp"
19 #include "math/Checks.hpp"
20 
21 using namespace std;
22 using namespace boost;
23 using namespace cf3;
24 using namespace cf3::common;
25 
26 using namespace cf3::math::Checks;
27 using namespace cf3::math::Consts;
28 
29 using namespace Eigen;
30 
32 
34 {
37  {
38  // uncomment if you want to use arguments to the test executable
39  //int* argc = &boost::unit_test::framework::master_test_suite().argc;
40  //char*** argv = &boost::unit_test::framework::master_test_suite().argv;
41  }
42 
45  {
46  }
47 
49 
51 
52 };
53 
55 
56 BOOST_FIXTURE_TEST_SUITE( MatrixInverter_TestSuite, MatrixInverter_Fixture )
57 
58 
60 BOOST_AUTO_TEST_CASE( SVDInverterTest )
61 {
62  //CFinfo << "testing SVDMatrixInverter " << CFflush;
63  // Exact solution is taken from http://www.mathematics-online.org/inhalt/beispiel/beispiel565/
64  // System Matrix
65  MatrixXd A(4,3);
66  A << 2., -4., 5.,
67  6., 0., 3.,
68  2., -4., 5.,
69  6., 0., 3.;
70 
71  // Right Hand Side
72  RealVector4 b(1., 3., -1., 3.);
73 
74 
75  // Known correct solution
76  RealVector3 xCheck(0.5, 0.25, 0.);
77  RealVector3 tol(1., 1., 1.); // 1% tolerance
78 
79  JacobiSVD<RealMatrix> svd(A, ComputeThinU | ComputeThinV);
80 
81  //CFinfo << svd.singularValues() << CFendl;
82  BOOST_CHECK_CLOSE(svd.singularValues()[0],12.,tol[0]);
83  BOOST_CHECK_CLOSE(svd.singularValues()[1],6.,tol[0]);
84  BOOST_CHECK_CLOSE(svd.singularValues()[2]+1.,0.+1.,tol[0]);
85 
86  RealVector s_inv = svd.singularValues();
87 
88  // zeroize small values below treshold
89  for (Uint i=0; i<3; ++i)
90  s_inv[i] = !is_equal_with_error(s_inv[i]+1.,1.,eps()*10.) ? 1./s_inv[i] : 0.;
91 
92  RealVector3 x = svd.matrixV().leftCols(s_inv.size())
93  * s_inv.asDiagonal()
94  * svd.matrixU().leftCols(s_inv.size()).adjoint()
95  * b;
96 
97  //RealVector3 x = svd.solve(b); // broken in Eigen, because the last singular value is not exactly 0
98 
99  //Check if solution matches
100  for (Uint i=0; i<x.size(); i++)
101  BOOST_CHECK_CLOSE(x[i]+1.,xCheck[i]+1.,tol[i]);
102 }
103 
104 
106 
107 BOOST_AUTO_TEST_SUITE_END()
108 
109 
external boost library namespace
STL namespace.
~MatrixInverter_Fixture()
common tear-down for each test case
MatrixInverter_Fixture()
common setup for each test case
Static functions for mathematical constants.
Definition: Consts.hpp:25
Eigen::Matrix< Real, Eigen::Dynamic, 1 > RealVector
Dynamic sized column vector.
Definition: MatrixTypes.hpp:25
Top-level namespace for coolfluid.
Definition: Action.cpp:18
Static functions for checking Real numbers.
Definition: Checks.hpp:24
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
Eigen::Matrix< Real, 4, 1 > RealVector4
Fixed size 4x1 column vector.
Definition: MatrixTypes.hpp:42
bool is_equal_with_error(const Real &x, const Real &y, const Real &fuzz)
Definition: Checks.hpp:31
Most basic kernel library.
Definition: Action.cpp:19
BOOST_AUTO_TEST_CASE(SVDInverterTest)
Eigen::Matrix< Real, 3, 1 > RealVector3
Fixed size 3x1 column vector.
Definition: MatrixTypes.hpp:41
Send comments to:
COOLFluiD Web Admin