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.
expect.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 <cmath>
16 #include <limits>
17 #include <cstring>
18 
19 // External includes
20 
21 // Project includes
22 #include "includes/exception.h"
23 
24 #pragma once
25 
30 #define KRATOS_EXPECT_TRUE(IsTrue) if(!(IsTrue)) KRATOS_ERROR << "Check failed because " << #IsTrue << " is not true" << std::endl;
31 #define KRATOS_EXPECT_FALSE(IsFalse) if(IsFalse) KRATOS_ERROR << "Check failed because " << #IsFalse << " is not false" << std::endl;
32 
33 #define KRATOS_EXPECT_EQ(a,b) if(!((a) == (b))) KRATOS_ERROR << "Check failed because " << #a << " is not equal to " << #b
34 #define KRATOS_EXPECT_NE(a,b) if((a) == (b)) KRATOS_ERROR << "Check failed because " << #a << " is equal to " << #b
35 
36 // #define KRATOS_EXPECT_STREQ(a,b) if(a.compare(b) != 0) KRATOS_ERROR << "Check failed because \"" << a << "\" is not equal to \"" << b << "\"" << std::endl;
37 // #define KRATOS_EXPECT_STRNE(a,b) if(a.compare(b) == 0) KRATOS_ERROR << "Check failed because \"" << a << "\" is equal to \"" << b << "\"" << std::endl;
38 
39 #define KRATOS_EXPECT_STREQ(a,b) if((strcmp(a,b) != 0)) KRATOS_ERROR << "Check failed because \"" << a << "\" is not equal to \"" << b << "\"" << std::endl;
40 #define KRATOS_EXPECT_STRNE(a,b) if((strcmp(a,b) == 0)) KRATOS_ERROR << "Check failed because \"" << a << "\" is equal to \"" << b << "\"" << std::endl;
41 
42 #define KRATOS_EXPECT_LT(a,b) if(!(a < b)) KRATOS_ERROR << "Check failed because " << #a << " is greater than or equal to " << #b << std::endl;
43 #define KRATOS_EXPECT_LE(a,b) if(!(a <= b)) KRATOS_ERROR << "Check failed because " << #a << " is greater than " << #b << std::endl;
44 
45 #define KRATOS_EXPECT_GT(a,b) if(!(a > b)) KRATOS_ERROR << "Check failed because " << #a << " is less than or equal to " << #b
46 #define KRATOS_EXPECT_GE(a,b) if(!(a >= b)) KRATOS_ERROR << "Check failed because " << #a << " is less than " << #b
47 
48 #define KRATOS_EXPECT_HAS_SUBSTRING(TheString, SubString) if (TheString.find(SubString) == std::string::npos ) \
49 KRATOS_ERROR << "The string \"" << SubString << "\" was not found in the given string" << std::endl;
50 
51 #define KRATOS_EXPECT_NEAR(a,b, tolerance) if(!(std::abs(a - b) <= tolerance)) KRATOS_ERROR << "Check failed because " << #a << " = " << a << \
52 " is not near to " << #b << " = " << b << " within the tolerance " << tolerance
53 #define KRATOS_EXPECT_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 << \
54 " is not near to " << #b << " = " << b << " within the relative tolerance " << tolerance << std::endl; } else {KRATOS_EXPECT_NEAR(a,b,tolerance);}
55 #define KRATOS_EXPECT_DOUBLE_EQ(a,b) KRATOS_EXPECT_NEAR(a,b,std::numeric_limits<double>::epsilon())
56 
57 #define KRATOS_EXPECT_VECTOR_NEAR(a, b, tolerance) { \
58 KRATOS_ERROR_IF_NOT(a.size() == b.size()) \
59 << "Check failed because vector arguments do not have the same size:" \
60 << std::endl \
61 << "First argument has size " << a.size() << ", " \
62 << "second argument has size " << b.size() << "." << std::endl; \
63 for (std::size_t _i = 0; _i < a.size(); _i++) { \
64  KRATOS_ERROR_IF( !(std::abs(a[_i] - b[_i]) <= tolerance) ) \
65  << "Check failed because vector " << #a << " with values" << std::endl \
66  << a << std::endl \
67  << "Is not near vector " << #b << " with values" << std::endl \
68  << b << std::endl \
69  << "Mismatch found in component " << _i << ":" << std::endl \
70  << a[_i] << " not near " << b[_i] \
71  << " within tolerance " << tolerance << "." << std::endl; \
72 } \
73 }
74 
75 #define KRATOS_EXPECT_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 
104 #define KRATOS_EXPECT_VECTOR_EQ(a, b) KRATOS_EXPECT_VECTOR_NEAR(a,b,std::numeric_limits<double>::epsilon())
105 
106 #define KRATOS_EXPECT_MATRIX_NEAR(a, b, tolerance) { \
107 KRATOS_ERROR_IF_NOT((a.size1() == b.size1()) && (a.size2() == b.size2())) \
108 << "Check failed because matrix arguments do not have the same dimensions:" \
109 << std::endl \
110 << "First argument has dimensions (" << a.size1() << "," << a.size2() << "), " \
111 << "second argument has dimensions (" << b.size1() << "," << b.size2() << ")." \
112 << std::endl; \
113 for (std::size_t _i = 0; _i < a.size1(); _i++) { \
114  for (std::size_t _j = 0; _j < a.size2(); _j++) { \
115  KRATOS_ERROR_IF( !(std::abs(a(_i,_j) - b(_i,_j)) <= tolerance) ) \
116  << "Check failed because matrix " << #a << " with values" << std::endl \
117  << a << std::endl \
118  << "Is not near matrix " << #b << " with values" << std::endl \
119  << b << std::endl \
120  << "Mismatch found in component (" << _i << "," << _j << "): " << std::endl \
121  << a(_i,_j) << " not near " << b(_i,_j) \
122  << " within tolerance " << tolerance << "." << std::endl; \
123  } \
124 } \
125 }
126 
127 #define KRATOS_EXPECT_MATRIX_RELATIVE_NEAR(a, b, tolerance) { \
128 KRATOS_ERROR_IF_NOT((a.size1() == b.size1()) && (a.size2() == b.size2())) \
129 << "Check failed because matrix arguments do not have the same dimensions:" \
130 << std::endl \
131 << "First argument has dimensions (" << a.size1() << "," << a.size2() << "), " \
132 << "second argument has dimensions (" << b.size1() << "," << b.size2() << ")." \
133 << std::endl; \
134 for (std::size_t _i = 0; _i < a.size1(); _i++) { \
135  for (std::size_t _j = 0; _j < a.size2(); _j++) { \
136  if (std::abs(b(_i,_j)) > std::numeric_limits<double>::epsilon()) { \
137  KRATOS_ERROR_IF( !(std::abs((a(_i,_j) - b(_i,_j))/b(_i,_j)) <= tolerance) ) \
138  << "Check failed because matrix " << #a << " with values" << std::endl \
139  << a << std::endl \
140  << "Is not near matrix " << #b << " with values" << std::endl \
141  << b << std::endl \
142  << "Mismatch found in component (" << _i << "," << _j << "): " << std::endl \
143  << a(_i,_j) << " not near " << b(_i,_j) \
144  << " within relative tolerance " << tolerance << "." << std::endl; \
145  } else { \
146  KRATOS_ERROR_IF( !(std::abs(a(_i,_j) - b(_i,_j)) <= tolerance) ) \
147  << "Check failed because matrix " << #a << " with values" << std::endl \
148  << a << std::endl \
149  << "Is not near matrix " << #b << " with values" << std::endl \
150  << b << std::endl \
151  << "Mismatch found in component (" << _i << "," << _j << "): " << std::endl \
152  << a(_i,_j) << " not near " << b(_i,_j) \
153  << " within tolerance " << tolerance << "." << std::endl; \
154  } \
155 } \
156 } \
157 }
158 
159 #define KRATOS_EXPECT_MATRIX_EQ(a, b) KRATOS_EXPECT_MATRIX_NEAR(a,b,std::numeric_limits<double>::epsilon())
160 
161 #define KRATOS_EXPECT_EXCEPTION_IS_THROWN(TheStatement, TheErrorMessage) \
162 try { \
163  TheStatement; \
164  KRATOS_ERROR << #TheStatement << " exited without throwing an error." << std::endl; \
165 } catch (Kratos::Exception& e) { \
166  if ( std::string(e.what()).find( TheErrorMessage ) == std::string::npos ) \
167  KRATOS_ERROR \
168  << "Test Failed: " << #TheStatement \
169  << " did not throw the expected error." << std::endl \
170  << "Expected:" << std::endl << TheErrorMessage << std::endl \
171  << "Got:" << std::endl << e.what() << std::endl; \
172 }
173 
174 #define KRATOS_EXPECT_VARIABLE_IN_NODAL_DATA(TheVariable, TheNode) \
175  KRATOS_ERROR_IF_NOT(TheNode.SolutionStepsDataHas(TheVariable)) \
176  << "Missing " << TheVariable.Name() << " variable in solution step data for node " \
177  << TheNode.Id() << "." << std::endl;
178 
179 #define KRATOS_EXPECT_DOF_IN_NODE(TheVariable, TheNode) \
180  KRATOS_ERROR_IF_NOT(TheNode.HasDofFor(TheVariable)) \
181  << "Missing Degree of Freedom for " << TheVariable.Name() \
182  << " in node " << TheNode.Id() << "." << std::endl;
183 
184 #ifdef KRATOS_DEBUG
185 #define KRATOS_DEBUG_EXCEPT_EXCEPTION_IS_THROWN(TheStatement, TheErrorMessage) KRATOS_EXPECT_EXCEPTION_IS_THROWN(TheStatement, TheErrorMessage)
186 #else
187 #define KRATOS_DEBUG_EXCEPT_EXCEPTION_IS_THROWN(TheStatement, TheErrorMessage) if(false) KRATOS_EXPECT_EXCEPTION_IS_THROWN(TheStatement, TheErrorMessage)
188 #endif
189