LifeV
lifev/core/testsuite/verify_solution/main.cpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5  Copyright (C) 2014 EPFL, Politecnico di Milano, Emory University
6 
7  This file is part of LifeV.
8 
9  LifeV is free software; you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  LifeV is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with LifeV. If not, see <http://www.gnu.org/licenses/>.
21 
22 *******************************************************************************
23 */
24 //@HEADER
25 
26 /*!
27  @file main.cpp
28  @brief This files contains the testsuite for VerifySolutions
29 
30  @author Simone Deparis <simone.deparis@epfl.ch>
31  @date 12-05-2014
32  @mantainer Simone Deparis <simone.deparis@epfl.ch>
33 
34  */
35 
36 #include <Epetra_ConfigDefs.h>
37 #ifdef EPETRA_MPI
38 #include <mpi.h>
39 #include <Epetra_MpiComm.h>
40 #else
41 #include <Epetra_SerialComm.h>
42 #endif
43 
44 
45 #include <lifev/core/LifeV.hpp>
46 
47 #include "lifev/core/util/VerifySolutions.hpp"
48 #include "lifev/core/array/MapEpetra.hpp"
49 #include "lifev/core/array/VectorEpetra.hpp"
50 
51 #include <cmath>
52 
53 // ===================================================
54 //! Namespaces
55 // ===================================================
56 using namespace LifeV;
57 
58 /* Matlab/Octave code:
59  map = [0:99];
60  v1 = cos(map);
61  v2 = sin(map);
62  v3 = 1./(map+1);
63  v = (v1+v2+v3)/3;
64  norm(v) % => 3.4180
65  u1 = v1-v; u2 = v2-v; u3 = v3-v;
66  C = [u1*u1' u2*u1' u3*u1';
67  u1*u2' u2*u2' u3*u2';
68  u1*u3' u2*u3' u3*u3';];
69  % => C =
70  27.5348 -22.0349 -5.4998
71  -22.0349 27.7940 -5.7591
72  -5.4998 -5.7591 11.2589
73  */
74 // ===================================================
75 //! Main
76 // ===================================================
77 int main (int argc, char** argv)
78 {
79 
80 #ifdef HAVE_MPI
81 
82  MPI_Init ( &argc, &argv );
83 
84  std::cout << "MPI Initialization" << std::endl;
85 
86  bool testSuccess (true);
87 
88 #endif
89  {
90 #ifdef EPETRA_MPI
91  std::shared_ptr<Epetra_Comm> comm ( new Epetra_MpiComm ( MPI_COMM_WORLD ) );
92 #else
93  std::shared_ptr<Epetra_Comm> comm ( new Epetra_SerialComm() );
94 #endif
95 
96  // Build Distributed linear map based on [0:99]
97 
98  Int numGlobalElements (100);
99  MapEpetra linearMap (numGlobalElements, 0, comm);
100 
101  // Build Distributed vectors [cos(index)] [sin(index)] [1/(index+1)]
102  VectorEpetra v1 (linearMap), v2 (linearMap), v3 (linearMap);
103 
104  Int numLocalEntries (linearMap.map (Unique)->NumMyElements () );
105 
106  Int* globalElements (linearMap.map (Unique)->MyGlobalElements () );
107  Int globalIndex (0);
108  for (int i (0); i < numLocalEntries; ++i)
109  {
110  globalIndex = globalElements[i];
111  v1.setCoefficient (globalIndex, cos (globalIndex) );
112  v2.setCoefficient (globalIndex, sin (globalIndex) );
113  v3.setCoefficient (globalIndex, 1. / (globalIndex + 1) );
114  }
115 
116  //! [Example of use of VerifySolutions]
117 
118  // Setting the previously computed values of the reference norm of the mean and the correlation matrix.
119  // This is also the output of the Print method.
120  Real referenceMeanNorm = 3.41795511063503;
121  Epetra_SerialDenseMatrix refM (3, 3);
122  refM (0, 0) = 27.5347957298817;
123  refM (0, 1) = -22.0349495350519;
124  refM (0, 2) = -5.49984619482986;
125  refM (1, 0) = -22.0349495350519;
126  refM (1, 1) = 27.7940200477885;
127  refM (1, 2) = -5.75907051273657;
128  refM (2, 0) = -5.49984619482986;
129  refM (2, 1) = -5.75907051273657;
130  refM (2, 2) = 11.2589167075664;
131 
132  // Instance of the class VerifySolution
133  VerifySolutions verify;
134 
135  // Insert vectors into VerifySolution
136  verify.PushBack (v1);
137  verify.PushBack (v2);
138  verify.PushBack (v3);
139 
140  // Compute the Mean and Correlation matrix
141  verify.ComputeCorrelation();
142 
143  // Tolerance between the error and the errorKnown
144  const LifeV::Real tolerance ( 1e-8 );
145  // Verify that it is the same as pre-computed.
146  bool isMeanOk ( verify.Check ( referenceMeanNorm, tolerance ) );
147  bool isMatrixOk ( verify.Check ( refM, tolerance ) );
148 
149  // Print the reference norm of the mean and the correlation matrix as
150  // pseudo c++ code.
151  if (comm->MyPID() == 0)
152  {
153  verify.Print();
154  }
155 
156  //! [Example of use of VerifySolutions]
157 
158  testSuccess = isMeanOk && isMatrixOk;
159 
160  }
161 
162 #ifdef HAVE_MPI
163 
164  MPI_Finalize();
165 
166  std::cout << "MPI Finalization" << std::endl;
167 
168 #endif
169 
170  if ( testSuccess )
171  {
172  return ( EXIT_SUCCESS );
173  }
174  else
175  {
176  return ( EXIT_FAILURE );
177  }
178 }
VectorEpetra - The Epetra Vector format Wrapper.
bool setCoefficient(const UInt row, const data_type &value, UInt offset=0)
Look for the given global row and set its value.
int32_type Int
Generic integer data.
Definition: LifeV.hpp:188
VectorEpetra(const MapEpetra &map, const MapEpetraType &mapType=Unique, const combineMode_Type combineMode=Add)
Constructor - Using Maps.
void updateInverseJacobian(const UInt &iQuadPt)
Epetra_Import const & importer()
Getter for the Epetra_Import.
Definition: MapEpetra.cpp:394
void Print() const
Print Mean and Correlation matrix as c++ lines ready to be inserted into the test.
void ComputeCorrelation()
Compute the correlation matrix and norm of the mean.
void PushBack(VectorEpetra const &newVector)
Add a vector to the list of stored vectors.
int main(int argc, char **argv)
Definition: dummy.cpp:5
double Real
Generic real data.
Definition: LifeV.hpp:175
This class helps the testsuites in verifying that the solutions do not change from one execution to t...
bool Check(Real referenceMean, Real tol) const
Checks that the current norm of the mean of the vectors is the same as the one provided by the user...