libMesh
coupling_matrix_test.C
Go to the documentation of this file.
1 // libmesh includes
2 #include <libmesh/coupling_matrix.h>
3 
4 #include "libmesh_cppunit.h"
5 
6 
7 using namespace libMesh;
8 
9 class CouplingMatrixTest : public CppUnit::TestCase
10 {
11 public:
12  void setUp() {}
13 
14  void tearDown() {}
15 
16  LIBMESH_CPPUNIT_TEST_SUITE(CouplingMatrixTest);
17 
18  CPPUNIT_TEST(testSimpleAPI);
19 
20  CPPUNIT_TEST(testIteratorAPI);
21 
22  CPPUNIT_TEST_SUITE_END();
23 
24 
25 private:
27  {
28  LOG_UNIT_TEST;
29 
30  CouplingMatrix cm(2);
31 
32  // Use a constant reference to make sure we test both const and
33  // non-const operator() implementations
34  const CouplingMatrix& cmr = cm;
35 
36  cm(0,1) = 1;
37 
38  bool cm01 = cm(0,1);
39  CPPUNIT_ASSERT_EQUAL(cm01, true);
40 
41  cm(1,0) = 1;
42 
43  for (unsigned i=0; i<2; ++i)
44  for (unsigned j=0; j<2; ++j)
45  {
46  bool cmij = cm(i,j);
47  bool cmrij = cmr(i,j);
48  CPPUNIT_ASSERT_EQUAL(cmij, cmrij);
49  CPPUNIT_ASSERT_EQUAL(cmij, (i != j));
50  }
51 
52  cm.resize(8);
53 
54  for (unsigned i=0; i<8; ++i)
55  for (unsigned j=0; j<8; ++j)
56  {
57  bool cmij = cm(i,j);
58  bool cmrij = cmr(i,j);
59  CPPUNIT_ASSERT_EQUAL(cmij, cmrij);
60  CPPUNIT_ASSERT_EQUAL(cmij, false);
61  }
62 
63  // Set some elements true, in a weird order.
64  for (unsigned i=6; i>0; --i)
65  {
66  const unsigned int pi = i + (i > 4);
67  for (unsigned j=0; j<6; ++j)
68  {
69  const unsigned int pj = j + (j > 3);
70  cm(pi, pj) = true;
71  }
72  }
73 
74  // Now the tensor product of {1,2,3,4,6,7} with {0,1,2,3,5,6}
75  // should be 1.
76  for (unsigned i=0; i<8; ++i)
77  for (unsigned j=0; j<8; ++j)
78  {
79  bool cmij = cm(i,j);
80  bool cmrij = cmr(i,j);
81  CPPUNIT_ASSERT_EQUAL(cmij, cmrij);
82  if ((i != 0) && (i != 5) && (j != 4) && (j != 7))
83  {
84  CPPUNIT_ASSERT_EQUAL(cmij, true);
85  }
86  else
87  {
88  CPPUNIT_ASSERT_EQUAL(cmij, false);
89  }
90  }
91 
92  // Set some elements to false.
93  for (unsigned k=0; k<8; ++k)
94  {
95  cm(3, k) = false;
96  cm(k, 0) = false;
97  }
98 
99  // Now the tensor product of {1,2,4,6,7} with {1,2,3,5,6}
100  // should be 1.
101  for (unsigned i=0; i<8; ++i)
102  for (unsigned j=0; j<8; ++j)
103  {
104  bool cmij = cm(i,j);
105  bool cmrij = cmr(i,j);
106  CPPUNIT_ASSERT_EQUAL(cmij, cmrij);
107  if ((i != 0) && (i != 3) && (i != 5) &&
108  (j != 0) && (j != 4) && (j != 7))
109  {
110  CPPUNIT_ASSERT_EQUAL(cmij, true);
111  }
112  else
113  {
114  CPPUNIT_ASSERT_EQUAL(cmij, false);
115  }
116  }
117  }
118 
120  {
121  LOG_UNIT_TEST;
122 
123  CouplingMatrix cm(8);
124 
125  // Set some elements true, in a weird order.
126  for (unsigned i=6; i>0; --i)
127  {
128  const unsigned int pi = i + (i > 4);
129  for (unsigned j=0; j<6; ++j)
130  {
131  const unsigned int pj = j + (j > 3);
132  cm(pi, pj) = true;
133  }
134  }
135 
136  // Now the tensor product of {1,2,3,4,6,7} with {0,1,2,3,5,6}
137  // should be 1.
138 
139  // Set some elements to false.
140  for (unsigned k=0; k<8; ++k)
141  {
142  cm(3, k) = false;
143  cm(k, 0) = false;
144  }
145 
146  // Now the tensor product of {1,2,4,6,7} with {1,2,3,5,6}
147  // should be 1.
148  const unsigned int ivals[] = {1,2,4,6,7};
149  const unsigned int non_ivals[] = {0,3,5};
150  const unsigned int jvals[] = {1,2,3,5,6};
151  // const unsigned int non_jvals[] = {0,4,7};
152 
153  const unsigned int isize = sizeof(unsigned int);
154 
155  for (unsigned int pi = 0; pi != sizeof(non_ivals)/isize; ++pi)
156  {
157  unsigned int i = non_ivals[pi];
158  ConstCouplingRow ccr(i,cm);
159  CPPUNIT_ASSERT(ccr.begin() == ccr.end());
160  }
161 
162  for (unsigned int pi = 0; pi != sizeof(ivals)/isize; ++pi)
163  {
164  unsigned int i = ivals[pi];
165  ConstCouplingRow ccr(i,cm);
166 
168 
169  for (unsigned int pj = 0; pj != sizeof(jvals)/isize; ++pj)
170  {
171  CPPUNIT_ASSERT(ccr_it != ccr.end());
172  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(*ccr_it), jvals[pj]);
173  ++ccr_it;
174  }
175 
176  CPPUNIT_ASSERT(ccr_it == ccr.end());
177  }
178  }
179 
180 
181 };
182 
void resize(const std::size_t n)
Resizes the matrix and initializes all entries to be 0.
This proxy class acts like a container of indices from a single coupling row.
The libMesh namespace provides an interface to certain functionality in the library.
CPPUNIT_TEST_SUITE_REGISTRATION(CouplingMatrixTest)
const_iterator end() const
const_iterator begin() const
void ErrorVector unsigned int
Definition: adjoints_ex3.C:360
const Real pi
.
Definition: libmesh.h:299
This class defines a coupling matrix.