KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
checks.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Pooyan Dadvand
11 //
12 //
13 
14 // System includes
15 #include <cstring>
16 #include <limits>
17 #include <cmath> // std::abs for double
18 
19 // External includes
20 
21 // Project includes
22 #include "includes/exception.h"
23 
24 #if !defined(KRATOS_CHECKS_H_INCLUDED )
25 #define KRATOS_CHECKS_H_INCLUDED
26 
31 #define KRATOS_CHECK(IsTrue) if(!(IsTrue)) KRATOS_ERROR << "Check failed because " << #IsTrue << " is not true" << std::endl;
32 #define KRATOS_CHECK_IS_FALSE(IsFalse) if(IsFalse) KRATOS_ERROR << "Check failed because " << #IsFalse << " is not false" << std::endl;
33 
34 #define KRATOS_CHECK_EQUAL(a,b) if(!((a) == (b))) KRATOS_ERROR << "Check failed because " << #a << " is not equal to " << #b
35 #define KRATOS_CHECK_NOT_EQUAL(a,b) if((a) == (b)) KRATOS_ERROR << "Check failed because " << #a << " is equal to " << #b
36 
37 #define KRATOS_CHECK_STRING_EQUAL(a,b) if(a.compare(b) != 0) KRATOS_ERROR << "Check failed because \"" << a << "\" is not equal to \"" << b << "\"" << std::endl;
38 #define KRATOS_CHECK_STRING_NOT_EQUAL(a,b) if(a.compare(b) == 0) KRATOS_ERROR << "Check failed because \"" << a << "\" is equal to \"" << b << "\"" << std::endl;
39 
40 #define KRATOS_CHECK_C_STRING_EQUAL(a,b) if((strcmp(a,b) != 0)) KRATOS_ERROR << "Check failed because \"" << a << "\" is not equal to \"" << b << "\"" << std::endl;
41 #define KRATOS_CHECK_C_STRING_NOT_EQUAL(a,b) if((strcmp(a,b) == 0)) KRATOS_ERROR << "Check failed because \"" << a << "\" is equal to \"" << b << "\"" << std::endl;
42 
43 #define KRATOS_CHECK_LESS(a,b) if(!(a < b)) KRATOS_ERROR << "Check failed because " << #a << " is greater than or equal to " << #b << std::endl;
44 #define KRATOS_CHECK_LESS_EQUAL(a,b) if(!(a <= b)) KRATOS_ERROR << "Check failed because " << #a << " is greater than " << #b << std::endl;
45 
46 #define KRATOS_CHECK_GREATER(a,b) if(!(a > b)) KRATOS_ERROR << "Check failed because " << #a << " is less than or equal to " << #b
47 #define KRATOS_CHECK_GREATER_EQUAL(a,b) if(!(a >= b)) KRATOS_ERROR << "Check failed because " << #a << " is less than " << #b
48 
49 #define KRATOS_CHECK_STRING_CONTAIN_SUB_STRING(TheString, SubString) if (TheString.find(SubString) == std::string::npos ) \
50 KRATOS_ERROR << "The string \"" << SubString << "\" was not found in the given string" << std::endl;
51 
52 #define KRATOS_CHECK_NEAR(a,b, tolerance) if(!(std::abs(a - b) <= tolerance)) KRATOS_ERROR << "Check failed because " << #a << " = " << a << \
53 " is not near to " << #b << " = " << b << " within the tolerance " << tolerance
54 #define KRATOS_CHECK_RELATIVE_NEAR(a,b, tolerance) if(!(std::abs(b) <= std::numeric_limits<double>::epsilon())) { KRATOS_ERROR_IF(!(std::abs((a - b)/b) <= tolerance)) << "Check failed because " << #a << " = " << a << \
55 " is not near to " << #b << " = " << b << " within the relative tolerance " << tolerance << std::endl; } else {KRATOS_CHECK_NEAR(a,b,tolerance);}
56 #define KRATOS_CHECK_DOUBLE_EQUAL(a,b) KRATOS_CHECK_NEAR(a,b,std::numeric_limits<double>::epsilon())
57 
58 #define KRATOS_CHECK_VECTOR_NEAR(a, b, tolerance) { \
59 KRATOS_ERROR_IF_NOT(a.size() == b.size()) \
60 << "Check failed because vector arguments do not have the same size:" \
61 << std::endl \
62 << "First argument has size " << a.size() << ", " \
63 << "second argument has size " << b.size() << "." << std::endl; \
64 for (std::size_t _i = 0; _i < a.size(); _i++) { \
65  KRATOS_ERROR_IF( !(std::abs(a[_i] - b[_i]) <= tolerance) ) \
66  << "Check failed because vector " << #a << " with values" << std::endl \
67  << a << std::endl \
68  << "Is not near vector " << #b << " with values" << std::endl \
69  << b << std::endl \
70  << "Mismatch found in component " << _i << ":" << std::endl \
71  << a[_i] << " not near " << b[_i] \
72  << " within tolerance " << tolerance << "." << std::endl; \
73 } \
74 }
75 #define KRATOS_CHECK_VECTOR_RELATIVE_NEAR(a, b, tolerance) { \
76 KRATOS_ERROR_IF_NOT(a.size() == b.size()) \
77 << "Check failed because vector arguments do not have the same size:" \
78 << std::endl \
79 << "First argument has size " << a.size() << ", " \
80 << "second argument has size " << b.size() << "." << std::endl; \
81 for (std::size_t _i = 0; _i < a.size(); _i++) { \
82  if (std::abs(b[_i]) > std::numeric_limits<double>::epsilon()) { \
83  KRATOS_ERROR_IF( !(std::abs((a[_i] - b[_i])/b[_i]) <= tolerance) ) \
84  << "Check failed because vector " << #a << " with values" << std::endl \
85  << a << std::endl \
86  << "Is not near vector " << #b << " with values" << std::endl \
87  << b << std::endl \
88  << "Mismatch found in component " << _i << ":" << std::endl \
89  << a[_i] << " not near " << b[_i] \
90  << " within relative tolerance " << tolerance << "." << std::endl; \
91  } else { \
92  KRATOS_ERROR_IF( !(std::abs(a[_i] - b[_i]) <= tolerance) ) \
93  << "Check failed because vector " << #a << " with values" << std::endl \
94  << a << std::endl \
95  << "Is not near vector " << #b << " with values" << std::endl \
96  << b << std::endl \
97  << "Mismatch found in component " << _i << ":" << std::endl \
98  << a[_i] << " not near " << b[_i] \
99  << " within tolerance " << tolerance << "." << std::endl; \
100  } \
101 } \
102 }
103 #define KRATOS_CHECK_VECTOR_EQUAL(a, b) KRATOS_CHECK_VECTOR_NEAR(a,b,std::numeric_limits<double>::epsilon())
104 
105 #define KRATOS_CHECK_MATRIX_NEAR(a, b, tolerance) { \
106 KRATOS_ERROR_IF_NOT((a.size1() == b.size1()) && (a.size2() == b.size2())) \
107 << "Check failed because matrix arguments do not have the same dimensions:" \
108 << std::endl \
109 << "First argument has dimensions (" << a.size1() << "," << a.size2() << "), " \
110 << "second argument has dimensions (" << b.size1() << "," << b.size2() << ")." \
111 << std::endl; \
112 for (std::size_t _i = 0; _i < a.size1(); _i++) { \
113  for (std::size_t _j = 0; _j < a.size2(); _j++) { \
114  KRATOS_ERROR_IF( !(std::abs(a(_i,_j) - b(_i,_j)) <= tolerance) ) \
115  << "Check failed because matrix " << #a << " with values" << std::endl \
116  << a << std::endl \
117  << "Is not near matrix " << #b << " with values" << std::endl \
118  << b << std::endl \
119  << "Mismatch found in component (" << _i << "," << _j << "): " << std::endl \
120  << a(_i,_j) << " not near " << b(_i,_j) \
121  << " within tolerance " << tolerance << "." << std::endl; \
122  } \
123 } \
124 }
125 #define KRATOS_CHECK_MATRIX_RELATIVE_NEAR(a, b, tolerance) { \
126 KRATOS_ERROR_IF_NOT((a.size1() == b.size1()) && (a.size2() == b.size2())) \
127 << "Check failed because matrix arguments do not have the same dimensions:" \
128 << std::endl \
129 << "First argument has dimensions (" << a.size1() << "," << a.size2() << "), " \
130 << "second argument has dimensions (" << b.size1() << "," << b.size2() << ")." \
131 << std::endl; \
132 for (std::size_t _i = 0; _i < a.size1(); _i++) { \
133  for (std::size_t _j = 0; _j < a.size2(); _j++) { \
134  if (std::abs(b(_i,_j)) > std::numeric_limits<double>::epsilon()) { \
135  KRATOS_ERROR_IF( !(std::abs((a(_i,_j) - b(_i,_j))/b(_i,_j)) <= tolerance) ) \
136  << "Check failed because matrix " << #a << " with values" << std::endl \
137  << a << std::endl \
138  << "Is not near matrix " << #b << " with values" << std::endl \
139  << b << std::endl \
140  << "Mismatch found in component (" << _i << "," << _j << "): " << std::endl \
141  << a(_i,_j) << " not near " << b(_i,_j) \
142  << " within relative tolerance " << tolerance << "." << std::endl; \
143  } else { \
144  KRATOS_ERROR_IF( !(std::abs(a(_i,_j) - b(_i,_j)) <= tolerance) ) \
145  << "Check failed because matrix " << #a << " with values" << std::endl \
146  << a << std::endl \
147  << "Is not near matrix " << #b << " with values" << std::endl \
148  << b << std::endl \
149  << "Mismatch found in component (" << _i << "," << _j << "): " << std::endl \
150  << a(_i,_j) << " not near " << b(_i,_j) \
151  << " within tolerance " << tolerance << "." << std::endl; \
152  } \
153 } \
154 } \
155 }
156 #define KRATOS_CHECK_MATRIX_EQUAL(a, b) KRATOS_CHECK_MATRIX_NEAR(a,b,std::numeric_limits<double>::epsilon())
157 
158 #define KRATOS_CHECK_EXCEPTION_IS_THROWN(TheStatement, TheErrorMessage) \
159 try { \
160  TheStatement; \
161  KRATOS_ERROR << #TheStatement << " exited without throwing an error." << std::endl; \
162 } catch (Kratos::Exception& e) { \
163  if ( std::string(e.what()).find( TheErrorMessage ) == std::string::npos ) \
164  KRATOS_ERROR \
165  << "Test Failed: " << #TheStatement \
166  << " did not throw the expected error." << std::endl \
167  << "Expected:" << std::endl << TheErrorMessage << std::endl \
168  << "Got:" << std::endl << e.what() << std::endl; \
169 }
170 
171 #define KRATOS_CHECK_VARIABLE_IN_NODAL_DATA(TheVariable, TheNode) \
172  KRATOS_ERROR_IF_NOT(TheNode.SolutionStepsDataHas(TheVariable)) \
173  << "Missing " << TheVariable.Name() << " variable in solution step data for node " \
174  << TheNode.Id() << "." << std::endl;
175 
176 #define KRATOS_CHECK_DOF_IN_NODE(TheVariable, TheNode) \
177  KRATOS_ERROR_IF_NOT(TheNode.HasDofFor(TheVariable)) \
178  << "Missing Degree of Freedom for " << TheVariable.Name() \
179  << " in node " << TheNode.Id() << "." << std::endl;
180 
181 #ifdef KRATOS_DEBUG
182 #define KRATOS_DEBUG_CHECK(IsTrue) KRATOS_CHECK(IsTrue)
183 #define KRATOS_DEBUG_CHECK_IS_FALSE(IsFalse) KRATOS_CHECK_IS_FALSE(IsFalse)
184 
185 #define KRATOS_DEBUG_CHECK_EQUAL(a,b) KRATOS_CHECK_EQUAL(a,b)
186 #define KRATOS_DEBUG_CHECK_NOT_EQUAL(a,b) KRATOS_CHECK_NOT_EQUAL(a,b)
187 
188 #define KRATOS_DEBUG_CHECK_C_STRING_EQUAL(a,b) KRATOS_CHECK_C_STRING_EQUAL(a,b)
189 #define KRATOS_DEBUG_CHECK_C_STRING_NOT_EQUAL(a,b) KRATOS_CHECK_C_STRING_NOT_EQUAL(a,b)
190 
191 #define KRATOS_DEBUG_CHECK_LESS(a,b) KRATOS_CHECK_LESS(a,b)
192 #define KRATOS_DEBUG_CHECK_LESS_EQUAL(a,b) KRATOS_CHECK_LESS_EQUAL(a,b)
193 
194 #define KRATOS_DEBUG_CHECK_GREATER(a,b) KRATOS_CHECK_GREATER(a,b)
195 #define KRATOS_DEBUG_CHECK_GREATER_EQUAL(a,b) KRATOS_CHECK_GREATER_EQUAL(a,b)
196 
197 #define KRATOS_DEBUG_CHECK_STRING_CONTAIN_SUB_STRING(TheString, SubString) KRATOS_CHECK_STRING_CONTAIN_SUB_STRING(TheString, SubString)
198 
199 #define KRATOS_DEBUG_CHECK_NEAR(a,b, tolerance) KRATOS_CHECK_NEAR(a,b, tolerance)
200 #define KRATOS_DEBUG_CHECK_DOUBLE_EQUAL(a,b) KRATOS_CHECK_DOUBLE_EQUAL(a,b)
201 
202 #define KRATOS_DEBUG_CHECK_VECTOR_NEAR(a, b, tolerance) KRATOS_CHECK_VECTOR_NEAR(a, b, tolerance)
203 #define KRATOS_DEBUG_CHECK_VECTOR_EQUAL(a, b) KRATOS_CHECK_VECTOR_EQUAL(a, b)
204 
205 #define KRATOS_DEBUG_CHECK_MATRIX_NEAR(a, b, tolerance) KRATOS_CHECK_MATRIX_NEAR(a, b, tolerance)
206 #define KRATOS_DEBUG_CHECK_MATRIX_EQUAL(a, b) KRATOS_CHECK_MATRIX_EQUAL(a, b)
207 
208 #define KRATOS_DEBUG_CHECK_EXCEPTION_IS_THROWN(TheStatement, TheErrorMessage) KRATOS_CHECK_EXCEPTION_IS_THROWN(TheStatement, TheErrorMessage)
209 
210 #define KRATOS_DEBUG_CHECK_VARIABLE_IN_NODAL_DATA(TheVariable, TheNode) KRATOS_CHECK_VARIABLE_IN_NODAL_DATA(TheVariable, TheNode)
211 #define KRATOS_DEBUG_CHECK_DOF_IN_NODE(TheVariable, TheNode) KRATOS_CHECK_DOF_IN_NODE(TheVariable, TheNode)
212 
213 #else
214 #define KRATOS_DEBUG_CHECK(IsTrue) if(false) KRATOS_CHECK(IsTrue)
215 #define KRATOS_DEBUG_CHECK_IS_FALSE(IsFalse) if(false) KRATOS_CHECK_IS_FALSE(IsFalse)
216 
217 #define KRATOS_DEBUG_CHECK_EQUAL(a,b) if(false) KRATOS_CHECK_EQUAL(a,b)
218 #define KRATOS_DEBUG_CHECK_NOT_EQUAL(a,b) if(false) KRATOS_CHECK_NOT_EQUAL(a,b)
219 
220 #define KRATOS_DEBUG_CHECK_C_STRING_EQUAL(a,b) if(false) KRATOS_CHECK_C_STRING_EQUAL(a,b)
221 #define KRATOS_DEBUG_CHECK_C_STRING_NOT_EQUAL(a,b) if(false) KRATOS_CHECK_C_STRING_NOT_EQUAL(a,b)
222 
223 #define KRATOS_DEBUG_CHECK_LESS(a,b) if(false) KRATOS_CHECK_LESS(a,b)
224 #define KRATOS_DEBUG_CHECK_LESS_EQUAL(a,b) if(false) KRATOS_CHECK_LESS_EQUAL(a,b)
225 
226 #define KRATOS_DEBUG_CHECK_GREATER(a,b) if(false) KRATOS_CHECK_GREATER(a,b)
227 #define KRATOS_DEBUG_CHECK_GREATER_EQUAL(a,b) if(false) KRATOS_CHECK_GREATER_EQUAL(a,b)
228 
229 #define KRATOS_DEBUG_CHECK_STRING_CONTAIN_SUB_STRING(TheString, SubString) if(false) KRATOS_CHECK_STRING_CONTAIN_SUB_STRING(TheString, SubString)
230 
231 #define KRATOS_DEBUG_CHECK_NEAR(a,b, tolerance) if(false) KRATOS_CHECK_NEAR(a,b, tolerance)
232 #define KRATOS_DEBUG_CHECK_DOUBLE_EQUAL(a,b) if(false) KRATOS_CHECK_DOUBLE_EQUAL(a,b)
233 
234 #define KRATOS_DEBUG_CHECK_VECTOR_NEAR(a, b, tolerance) if (false) KRATOS_CHECK_VECTOR_NEAR(a, b, tolerance)
235 #define KRATOS_DEBUG_CHECK_VECTOR_EQUAL(a, b) if (false) KRATOS_CHECK_VECTOR_EQUAL(a, b)
236 
237 #define KRATOS_DEBUG_CHECK_MATRIX_NEAR(a, b, tolerance) if (false) KRATOS_CHECK_MATRIX_NEAR(a, b, tolerance)
238 #define KRATOS_DEBUG_CHECK_MATRIX_EQUAL(a, b) if (false) KRATOS_CHECK_MATRIX_EQUAL(a, b)
239 
240 #define KRATOS_DEBUG_CHECK_EXCEPTION_IS_THROWN(TheStatement, TheErrorMessage) if(false) KRATOS_CHECK_EXCEPTION_IS_THROWN(TheStatement, TheErrorMessage)
241 
242 #define KRATOS_DEBUG_CHECK_VARIABLE_IN_NODAL_DATA(TheVariable, TheNode) if(false) KRATOS_CHECK_VARIABLE_IN_NODAL_DATA(TheVariable, TheNode)
243 #define KRATOS_DEBUG_CHECK_DOF_IN_NODE(TheVariable, TheNode) if(false) KRATOS_CHECK_DOF_IN_NODE(TheVariable, TheNode)
244 #endif
246 
248 
249 #endif // KRATOS_CHECKS_H_INCLUDED defined