queso-0.51.1
TestVectorRealizer_gsl.C
Go to the documentation of this file.
1 #define BOOST_TEST_MODULE
2 #include <boost/test/included/unit_test.hpp>
3 #include <boost/test/floating_point_comparison.hpp>
4 
5 #include <queso/VectorSpace.h>
6 #include <queso/VectorRealizer.h>
7 #include <queso/GslMatrix.h>
8 
9 using namespace QUESO;
10 
11 BOOST_AUTO_TEST_CASE( test_uqGaussianVectorRealizer )
12 {
13  // Initialize
14  MPI_Init(NULL, NULL);
15  uqFullEnvironment env; // Puts random number generator in known state... will generate same sequence each run
16  uqVectorSpace<uqGslVector, uqGslMatrix> imageSpace(env, "test_space", 2, NULL);
17  uqMap eMap(2, 0, env.comm());
18 
19  double tol = 1e-16;
20 
21 
22  // Tests
23 
24  // Test 1: mean = 0, covMatrix = identity
25  uqGslVector expectedValues(env, eMap, 0.0);
26  uqGslMatrix lowerCholCovMatrix(env, eMap, 1.0); // identity
27 
28  int ierr = lowerCholCovMatrix.chol();
29  BOOST_REQUIRE( ierr==0 ); // make sure cholesky succeeded
30 
31  lowerCholCovMatrix.zeroUpper(false); // zero upper triangular
32 
33  uqGaussianVectorRealizer<uqGslVector, uqGslMatrix>* gaussianRealizer =
34  new uqGaussianVectorRealizer<uqGslVector, uqGslMatrix>("test_realizer",
35  imageSpace,
36  expectedValues,
37  lowerCholCovMatrix);
38 
39  uqGslVector myRealization(expectedValues);
40 
41  gaussianRealizer->realization(myRealization);
42  //std::cout << "myRealization = " << std::setprecision(16) << std::scientific << myRealization << "\n";
43 
44  // NOTE: Data generated by running rng (i.e. this is a regression test).
45  // If default seed value or rng algorithm are changed, these asserts will fail.
46  BOOST_REQUIRE_CLOSE(myRealization[0], 2.2285703126720258e-01, tol);
47  BOOST_REQUIRE_CLOSE(myRealization[1], -8.5156524864967331e-01, tol);
48 
49  delete gaussianRealizer;
50 
51  // Test 2: mean = [-2; 1], covMatrix = [2, 1; 1, 2]
52  expectedValues[0] = -2.0; expectedValues[1] = 1.0;
53 
54  lowerCholCovMatrix(0,0) = 2.0; lowerCholCovMatrix(0,1) = 1.0;
55  lowerCholCovMatrix(1,0) = 1.0; lowerCholCovMatrix(1,1) = 2.0;
56 
57  ierr = lowerCholCovMatrix.chol();
58  BOOST_REQUIRE( ierr==0 ); // make sure cholesky succeeded
59 
60  lowerCholCovMatrix.zeroUpper(false); // zero upper triangular
61 
62  gaussianRealizer = new uqGaussianVectorRealizer<uqGslVector, uqGslMatrix>("test_realizer",
63  imageSpace,
64  expectedValues,
65  lowerCholCovMatrix);
66 
67  // Generate realization
68  gaussianRealizer->realization(myRealization);
69  //std::cout << "myRealization = " << std::setprecision(16) << std::scientific << myRealization << "\n";
70 
71  BOOST_REQUIRE_CLOSE(myRealization[0], -1.315078621127142e+00, tol);
72  BOOST_REQUIRE_CLOSE(myRealization[1], 9.780380444774379e-01, tol);
73 
74  delete gaussianRealizer;
75 
76 
77  // Clean up
78  MPI_Finalize();
79 
80 }
BOOST_AUTO_TEST_CASE(simple_test_add)
Definition: TestExample.C:7

Generated on Thu Apr 23 2015 19:26:16 for queso-0.51.1 by  doxygen 1.8.5