MAST
Multidisciplinary-design Adaptation and Sensitivity Toolkit (MAST)
test_comparisons.h
Go to the documentation of this file.
1 /*
2  * MAST: Multidisciplinary-design Adaptation and Sensitivity Toolkit
3  * Copyright (C) 2013-2020 Manav Bhatia and MAST authors
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #ifndef __mast_test_comparisons_h__
21 #define __mast_test_comparisons_h__
22 
23 // C++ includes
24 #include <iomanip>
25 
26 // MAST includes
27 #include "base/mast_data_types.h"
28 
29 namespace MAST {
30 
31  inline bool
32  is_numerical_zero(const Real v, const Real eps) {
33 
34  return fabs(v) <= eps;
35  }
36 
37 
38  inline bool
39  compare(const Real v1, const Real v2, const Real tol) {
40 
41  const Real
42  eps = 1.0e-7;
43 
44  bool rval = false;
45 
46  // check to see if the values are both small enough
47  // to be zero
48  if (MAST::is_numerical_zero(v1, eps) &&
49  MAST::is_numerical_zero(v2, eps))
50  rval = true;
51  // check to see if the absolute difference is small enough
52  else if (MAST::is_numerical_zero(v1-v2, eps))
53  rval = true;
54  // check to see if the relative difference is small enough
55  else if (fabs(v1) > 0)
56  rval = fabs((v1-v2)/v1) <= tol;
57 
58  return rval;
59  }
60 
61 
62 
63 
64  inline bool
65  compare_value(const Real v0, const Real v, const Real tol) {
66 
67  bool pass = true;
68 
69  if (!MAST::compare(v0, v, tol)) {
70  BOOST_TEST_MESSAGE ("Failed comparison: "
71  << "expected: " << v0<< " , "
72  << "computed: " << v << " : "
73  << "diff: " << v0 - v << " , "
74  << "tol: " << tol);
75  pass = false;
76  }
77 
78  return pass;
79  }
80 
81 
82 
83  inline bool
84  compare_vector(const RealVectorX& v0, const RealVectorX& v, const Real tol) {
85 
86  unsigned int
87  v0_size = (unsigned int) v0.rows();
88  libmesh_assert_equal_to(v0_size, v.size());
89 
90 
91  bool pass = true;
92  for (unsigned int i=0; i<v0_size; i++) {
93  if (!MAST::compare(v0(i), v(i), tol)) {
94  BOOST_TEST_MESSAGE("Failed comparison at i = ("
95  << i << ") : "
96  << "expected: " << v0(i) << " , "
97  << "computed: " << v(i) << " : "
98  << "diff: " << v0(i) - v(i) << " , "
99  << "tol: " << tol);
100  pass = false;
101  }
102  }
103 
104  return pass;
105  }
106 
107 
108 
109 
110  inline bool
111  compare_matrix(const RealMatrixX& m0, const RealMatrixX& m, const Real tol) {
112 
113  unsigned int
114  m0_rows = (unsigned int) m0.rows(),
115  m0_cols = (unsigned int) m0.cols();
116  libmesh_assert_equal_to(m0_rows, m.rows());
117  libmesh_assert_equal_to(m0_cols, m.cols());
118 
119 
120  bool
121  pass = true;
122  for (unsigned int i=0; i<m0_rows; i++) {
123  for (unsigned int j=0; j<m0_cols; j++)
124  if (!MAST::compare(m0(i,j), m(i,j), tol)) {
125  if (pass) {
126  BOOST_TEST_MESSAGE("Failed comparison\n"
127  << std::setw(5) << "i"
128  << std::setw(5) << "j"
129  << std::setw(20) << "expected"
130  << std::setw(20) << "computed"
131  << std::setw(20) << "diff"
132  << std::setw(20) << "tol");
133  }
134  BOOST_TEST_MESSAGE(std::setw(5) << i
135  << std::setw(5) << j
136  << std::setw(20) << m0(i,j)
137  << std::setw(20) << m(i,j)
138  << std::setw(20) << m0(i,j) - m(i,j)
139  << std::setw(20) << tol);
140  pass = false;
141  }
142  }
143 
144  return pass;
145  }
146 
147 }
148 
149 #endif //__mast_test_compasisons_h__
bool compare_vector(const RealVectorX &v0, const RealVectorX &v, const Real tol)
#define eps
bool compare_matrix(const RealMatrixX &m0, const RealMatrixX &m, const Real tol)
libMesh::Real Real
Matrix< Real, Dynamic, Dynamic > RealMatrixX
Matrix< Real, Dynamic, 1 > RealVectorX
bool compare_value(const Real v0, const Real v, const Real tol)
bool is_numerical_zero(const Real v, const Real eps)
bool compare(const Real v1, const Real v2, const Real tol)