libMesh
type_vector_test.h
Go to the documentation of this file.
1 #ifndef __type_vector_test_h__
2 #define __type_vector_test_h__
3 
4 #include <libmesh/type_vector.h>
5 
6 #include <cppunit/extensions/HelperMacros.h>
7 #include <cppunit/TestCase.h>
8 
9 #define TYPEVECTORTEST \
10  CPPUNIT_TEST( testNorm ); \
11  CPPUNIT_TEST( testNormSq ); \
12  \
13  CPPUNIT_TEST( testValue ); \
14  CPPUNIT_TEST( testZero ); \
15  \
16  CPPUNIT_TEST( testEquality ); \
17  CPPUNIT_TEST( testInEquality ); \
18  CPPUNIT_TEST( testAssignment ); \
19  \
20  CPPUNIT_TEST( testScalarMult ); \
21  CPPUNIT_TEST( testScalarDiv ); \
22  CPPUNIT_TEST( testScalarMultAssign ); \
23  CPPUNIT_TEST( testScalarDivAssign ); \
24  \
25  CPPUNIT_TEST( testVectorAdd ); \
26  CPPUNIT_TEST( testVectorAddScaled ); \
27  CPPUNIT_TEST( testVectorSub ); \
28  CPPUNIT_TEST( testVectorMult ); \
29  CPPUNIT_TEST( testVectorAddAssign ); \
30  CPPUNIT_TEST( testVectorSubAssign ); \
31  \
32  CPPUNIT_TEST( testNormBase ); \
33  CPPUNIT_TEST( testNormSqBase ); \
34  CPPUNIT_TEST( testValueBase ); \
35  CPPUNIT_TEST( testZeroBase ); \
36  \
37  CPPUNIT_TEST( testEqualityBase ); \
38  CPPUNIT_TEST( testInEqualityBase ); \
39  CPPUNIT_TEST( testAssignmentBase ); \
40  \
41  CPPUNIT_TEST( testScalarMultBase ); \
42  CPPUNIT_TEST( testScalarDivBase ); \
43  CPPUNIT_TEST( testScalarMultAssignBase ); \
44  CPPUNIT_TEST( testScalarDivAssignBase ); \
45  \
46  CPPUNIT_TEST( testVectorAddBase ); \
47  CPPUNIT_TEST( testVectorAddScaledBase ); \
48  CPPUNIT_TEST( testVectorSubBase ); \
49  CPPUNIT_TEST( testVectorMultBase ); \
50  CPPUNIT_TEST( testVectorAddAssignBase ); \
51  CPPUNIT_TEST( testVectorSubAssignBase );
52 
53 
54 using namespace libMesh;
55 
56 template <class DerivedClass>
57 class TypeVectorTestBase : public CppUnit::TestCase {
58 
59 protected:
60  typedef typename DerivedClass::value_type T;
61 
62 private:
63  DerivedClass *m_1_1_1, *m_n1_1_n1;
64  TypeVector<T> *basem_1_1_1, *basem_n1_1_n1;
65 
66 public:
67  virtual void setUp()
68  {
69  m_1_1_1 = new DerivedClass(1,1,1);
70  m_n1_1_n1 = new DerivedClass(-1,1,-1);
71 
72  basem_1_1_1 = m_1_1_1;
73  basem_n1_1_n1 = m_n1_1_n1;
74  }
75 
76  virtual void tearDown()
77  {
78  delete m_1_1_1;
79  delete m_n1_1_n1;
80  }
81 
82  void testValue()
83  {
84  CPPUNIT_ASSERT_EQUAL( T(1), (*m_1_1_1)(0));
85  CPPUNIT_ASSERT_EQUAL( T(1), (*m_1_1_1)(1));
86  CPPUNIT_ASSERT_EQUAL( T(1), (*m_1_1_1)(2));
87 
88  CPPUNIT_ASSERT_EQUAL( T(-1), (*m_n1_1_n1)(0));
89  CPPUNIT_ASSERT_EQUAL( T(1) , (*m_n1_1_n1)(1));
90  CPPUNIT_ASSERT_EQUAL( T(-1), (*m_n1_1_n1)(2));
91  }
92 
93  void testZero()
94  {
95  DerivedClass avector(1,1,1);
96  avector.zero();
97 
98  CPPUNIT_ASSERT_EQUAL( T(0), avector(0));
99  CPPUNIT_ASSERT_EQUAL( T(0), avector(1));
100  CPPUNIT_ASSERT_EQUAL( T(0), avector(2));
101  }
102 
103  void testNorm()
104  {
105  CPPUNIT_ASSERT_DOUBLES_EQUAL( std::sqrt(3.0) , m_1_1_1->norm() , TOLERANCE*TOLERANCE );
106  }
107 
108  void testNormSq()
109  {
110  CPPUNIT_ASSERT_DOUBLES_EQUAL( 3.0 , m_1_1_1->norm_sq() , TOLERANCE*TOLERANCE );
111  }
112 
114  {
115  CPPUNIT_ASSERT( (*m_1_1_1) == (*m_1_1_1) );
116  CPPUNIT_ASSERT( !((*m_1_1_1) == (*m_n1_1_n1)) );
117  }
118 
120  {
121  CPPUNIT_ASSERT( !((*m_1_1_1) != (*m_1_1_1)) );
122  CPPUNIT_ASSERT( (*m_1_1_1) != (*m_n1_1_n1) );
123  }
124 
126  {
127  DerivedClass avector = (*m_1_1_1);
128 
129  CPPUNIT_ASSERT_EQUAL( T(1), (avector)(0) );
130  CPPUNIT_ASSERT_EQUAL( T(1), (avector)(1) );
131  CPPUNIT_ASSERT_EQUAL( T(1), (avector)(2) );
132  }
133 
134  void testScalarInit() // Not valid for all derived classes!
135  {
136  DerivedClass avector = 0;
137  CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0 , libmesh_real(avector(0)) , TOLERANCE*TOLERANCE );
138  CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0 , libmesh_real(avector(1)) , TOLERANCE*TOLERANCE );
139  CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0 , libmesh_real(avector(2)) , TOLERANCE*TOLERANCE );
140 
141  DerivedClass bvector = 2.0;
142  CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0 , libmesh_real(bvector(0)) , TOLERANCE*TOLERANCE );
143  CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0 , libmesh_real(bvector(1)) , TOLERANCE*TOLERANCE );
144  CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0 , libmesh_real(bvector(2)) , TOLERANCE*TOLERANCE );
145  }
146 
148  {
149  CPPUNIT_ASSERT_DOUBLES_EQUAL( 5.0 , libmesh_real(((*m_1_1_1)*5.0)(0)) , TOLERANCE*TOLERANCE );
150  CPPUNIT_ASSERT_DOUBLES_EQUAL( 5.0 , libmesh_real(((*m_1_1_1)*5.0)(1)) , TOLERANCE*TOLERANCE );
151  CPPUNIT_ASSERT_DOUBLES_EQUAL( 5.0 , libmesh_real(((*m_1_1_1)*5.0)(2)) , TOLERANCE*TOLERANCE );
152  }
153 
155  {
156  CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0/5.0 , libmesh_real(((*m_1_1_1)/5.0)(0)) , TOLERANCE*TOLERANCE );
157  CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0/5.0 , libmesh_real(((*m_1_1_1)/5.0)(1)) , TOLERANCE*TOLERANCE );
158  CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0/5.0 , libmesh_real(((*m_1_1_1)/5.0)(2)) , TOLERANCE*TOLERANCE );
159  }
160 
162  {
163  DerivedClass avector(1,1,1);
164  avector*=5.0;
165 
166  CPPUNIT_ASSERT_DOUBLES_EQUAL( 5.0 , libmesh_real(avector(0)) , TOLERANCE*TOLERANCE );
167  CPPUNIT_ASSERT_DOUBLES_EQUAL( 5.0 , libmesh_real(avector(1)) , TOLERANCE*TOLERANCE );
168  CPPUNIT_ASSERT_DOUBLES_EQUAL( 5.0 , libmesh_real(avector(2)) , TOLERANCE*TOLERANCE );
169  }
170 
172  {
173  DerivedClass avector(1.0,1.0,1.0);
174  avector/=5.0;
175 
176  CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0/5.0 , libmesh_real(avector(0)) , TOLERANCE*TOLERANCE );
177  CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0/5.0 , libmesh_real(avector(1)) , TOLERANCE*TOLERANCE );
178  CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0/5.0 , libmesh_real(avector(2)) , TOLERANCE*TOLERANCE );
179  }
180 
182  {
183  CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0 , libmesh_real(((*m_1_1_1)+(*m_n1_1_n1))(0)) , TOLERANCE*TOLERANCE );
184  CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0 , libmesh_real(((*m_1_1_1)+(*m_n1_1_n1))(1)) , TOLERANCE*TOLERANCE );
185  CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0 , libmesh_real(((*m_1_1_1)+(*m_n1_1_n1))(2)) , TOLERANCE*TOLERANCE );
186  }
187 
189  {
190  DerivedClass avector(1,1,1);
191  avector.add_scaled((*m_1_1_1),0.5);
192 
193  CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.5 , libmesh_real(avector(0)) , TOLERANCE*TOLERANCE );
194  CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.5 , libmesh_real(avector(1)) , TOLERANCE*TOLERANCE );
195  CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.5 , libmesh_real(avector(2)) , TOLERANCE*TOLERANCE );
196  }
197 
199  {
200  CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0 , libmesh_real(((*m_1_1_1)-(*m_n1_1_n1))(0)) , TOLERANCE*TOLERANCE );
201  CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0 , libmesh_real(((*m_1_1_1)-(*m_n1_1_n1))(1)) , TOLERANCE*TOLERANCE );
202  CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0 , libmesh_real(((*m_1_1_1)-(*m_n1_1_n1))(2)) , TOLERANCE*TOLERANCE );
203  }
204 
206  {
207  CPPUNIT_ASSERT_DOUBLES_EQUAL( -1.0 , libmesh_real((*m_1_1_1)*(*m_n1_1_n1)) , TOLERANCE*TOLERANCE );
208  }
209 
211  {
212  DerivedClass avector(1,1,1);
213  avector+=(*m_1_1_1);
214 
215  CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0 , libmesh_real(avector(0)) , TOLERANCE*TOLERANCE );
216  CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0 , libmesh_real(avector(1)) , TOLERANCE*TOLERANCE );
217  CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0 , libmesh_real(avector(2)) , TOLERANCE*TOLERANCE );
218  }
219 
221  {
222  DerivedClass avector(1,1,1);
223  avector-=(*m_n1_1_n1);
224 
225  CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0 , libmesh_real(avector(0)) , TOLERANCE*TOLERANCE );
226  CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0 , libmesh_real(avector(1)) , TOLERANCE*TOLERANCE );
227  CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0 , libmesh_real(avector(2)) , TOLERANCE*TOLERANCE );
228  }
229 
231  {
232  CPPUNIT_ASSERT_EQUAL( T(1), (*basem_1_1_1)(0));
233  CPPUNIT_ASSERT_EQUAL( T(1), (*basem_1_1_1)(1));
234  CPPUNIT_ASSERT_EQUAL( T(1), (*basem_1_1_1)(2));
235 
236  CPPUNIT_ASSERT_EQUAL( T(-1), (*basem_n1_1_n1)(0));
237  CPPUNIT_ASSERT_EQUAL( T(1 ), (*basem_n1_1_n1)(1));
238  CPPUNIT_ASSERT_EQUAL( T(-1), (*basem_n1_1_n1)(2));
239  }
240 
242  {
243  TypeVector<T> avector((*basem_1_1_1));
244  avector.zero();
245 
246  CPPUNIT_ASSERT_EQUAL( T(0), avector(0));
247  CPPUNIT_ASSERT_EQUAL( T(0), avector(1));
248  CPPUNIT_ASSERT_EQUAL( T(0), avector(2));
249  }
250 
252  {
253  CPPUNIT_ASSERT_DOUBLES_EQUAL( std::sqrt(3.0) , basem_1_1_1->norm() , TOLERANCE*TOLERANCE );
254  }
255 
257  {
258  CPPUNIT_ASSERT_DOUBLES_EQUAL( 3.0 , basem_1_1_1->norm_sq() , TOLERANCE*TOLERANCE );
259  }
260 
262  {
263  CPPUNIT_ASSERT( (*basem_1_1_1) == (*basem_1_1_1) );
264  CPPUNIT_ASSERT( !((*basem_1_1_1) == (*basem_n1_1_n1)) );
265  }
266 
268  {
269  CPPUNIT_ASSERT( !((*basem_1_1_1) != (*basem_1_1_1)) );
270  CPPUNIT_ASSERT( (*basem_1_1_1) != (*basem_n1_1_n1) );
271  }
272 
274  {
275  TypeVector<T> avector = (*m_1_1_1);
276 
277  CPPUNIT_ASSERT_EQUAL( T(1), (avector)(0) );
278  CPPUNIT_ASSERT_EQUAL( T(1), (avector)(1) );
279  CPPUNIT_ASSERT_EQUAL( T(1), (avector)(2) );
280  }
281 
283  {
284  CPPUNIT_ASSERT_DOUBLES_EQUAL( 5.0 , libmesh_real(((*basem_1_1_1)*5.0)(0)) , TOLERANCE*TOLERANCE );
285  CPPUNIT_ASSERT_DOUBLES_EQUAL( 5.0 , libmesh_real(((*basem_1_1_1)*5.0)(1)) , TOLERANCE*TOLERANCE );
286  CPPUNIT_ASSERT_DOUBLES_EQUAL( 5.0 , libmesh_real(((*basem_1_1_1)*5.0)(2)) , TOLERANCE*TOLERANCE );
287  }
288 
290  {
291  CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0/5.0 , libmesh_real(((*basem_1_1_1)/5.0)(0)) , TOLERANCE*TOLERANCE );
292  CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0/5.0 , libmesh_real(((*basem_1_1_1)/5.0)(1)) , TOLERANCE*TOLERANCE );
293  CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0/5.0 , libmesh_real(((*basem_1_1_1)/5.0)(2)) , TOLERANCE*TOLERANCE );
294  }
295 
297  {
298  TypeVector<T> avector(*m_1_1_1);
299  avector*=5.0;
300 
301  CPPUNIT_ASSERT_DOUBLES_EQUAL( 5.0 , libmesh_real(avector(0)) , TOLERANCE*TOLERANCE );
302  CPPUNIT_ASSERT_DOUBLES_EQUAL( 5.0 , libmesh_real(avector(1)) , TOLERANCE*TOLERANCE );
303  CPPUNIT_ASSERT_DOUBLES_EQUAL( 5.0 , libmesh_real(avector(2)) , TOLERANCE*TOLERANCE );
304  }
305 
307  {
308  TypeVector<T> avector(*m_1_1_1);
309  avector/=5.0;
310 
311  CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0/5.0 , libmesh_real(avector(0)) , TOLERANCE*TOLERANCE );
312  CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0/5.0 , libmesh_real(avector(1)) , TOLERANCE*TOLERANCE );
313  CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0/5.0 , libmesh_real(avector(2)) , TOLERANCE*TOLERANCE );
314  }
315 
317  {
318  CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0 , libmesh_real(((*basem_1_1_1)+(*basem_n1_1_n1))(0)) , TOLERANCE*TOLERANCE );
319  CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0 , libmesh_real(((*basem_1_1_1)+(*basem_n1_1_n1))(1)) , TOLERANCE*TOLERANCE );
320  CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0 , libmesh_real(((*basem_1_1_1)+(*basem_n1_1_n1))(2)) , TOLERANCE*TOLERANCE );
321  }
322 
324  {
325  TypeVector<T> avector(*m_1_1_1);
326  avector.add_scaled((*basem_1_1_1),0.5);
327 
328  CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.5 , libmesh_real(avector(0)) , TOLERANCE*TOLERANCE );
329  CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.5 , libmesh_real(avector(1)) , TOLERANCE*TOLERANCE );
330  CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.5 , libmesh_real(avector(2)) , TOLERANCE*TOLERANCE );
331  }
332 
334  {
335  CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0 , libmesh_real(((*basem_1_1_1)-(*basem_n1_1_n1))(0)) , TOLERANCE*TOLERANCE );
336  CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0 , libmesh_real(((*basem_1_1_1)-(*basem_n1_1_n1))(1)) , TOLERANCE*TOLERANCE );
337  CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0 , libmesh_real(((*basem_1_1_1)-(*basem_n1_1_n1))(2)) , TOLERANCE*TOLERANCE );
338  }
339 
341  {
342  CPPUNIT_ASSERT_DOUBLES_EQUAL( -1.0 , libmesh_real((*basem_1_1_1)*(*basem_n1_1_n1)) , TOLERANCE*TOLERANCE );
343  }
344 
346  {
347  TypeVector<T> avector(*m_1_1_1);
348  avector+=(*basem_1_1_1);
349 
350  CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0 , libmesh_real(avector(0)) , TOLERANCE*TOLERANCE );
351  CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0 , libmesh_real(avector(1)) , TOLERANCE*TOLERANCE );
352  CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0 , libmesh_real(avector(2)) , TOLERANCE*TOLERANCE );
353  }
354 
356  {
357  TypeVector<T> avector(*m_1_1_1);
358  avector-=(*basem_n1_1_n1);
359 
360  CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0 , libmesh_real(avector(0)) , TOLERANCE*TOLERANCE );
361  CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0 , libmesh_real(avector(1)) , TOLERANCE*TOLERANCE );
362  CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0 , libmesh_real(avector(2)) , TOLERANCE*TOLERANCE );
363  }
364 };
365 
366 #endif // #ifdef __type_vector_test_h__
T libmesh_real(T a)
virtual void setUp()
void add_scaled(const TypeVector< T2 > &, const T)
Add a scaled value to this vector without creating a temporary.
Definition: type_vector.h:624
TypeVector< T > * basem_n1_1_n1
virtual void tearDown()
Real norm() const
Definition: type_vector.h:909
static const Real TOLERANCE
The libMesh namespace provides an interface to certain functionality in the library.
DerivedClass * m_n1_1_n1
void zero()
Set all entries of the vector to 0.
Definition: type_vector.h:918
This class defines a vector in LIBMESH_DIM dimensional space of type T.
Definition: tensor_tools.h:30
Real norm_sq() const
Definition: type_vector.h:940
DerivedClass::value_type T