libMesh
coupling_matrix_test.C
Go to the documentation of this file.
1 // Ignore unused parameter warnings coming from cppunit headers
2 #include <libmesh/ignore_warnings.h>
3 #include <cppunit/extensions/HelperMacros.h>
4 #include <cppunit/TestCase.h>
5 #include <libmesh/restore_warnings.h>
6 
7 // libmesh includes
8 #include <libmesh/coupling_matrix.h>
9 
10 // THE CPPUNIT_TEST_SUITE_END macro expands to code that involves
11 // std::auto_ptr, which in turn produces -Wdeprecated-declarations
12 // warnings. These can be ignored in GCC as long as we wrap the
13 // offending code in appropriate pragmas. We can't get away with a
14 // single ignore_warnings.h inclusion at the beginning of this file,
15 // since the libmesh headers pull in a restore_warnings.h at some
16 // point. We also don't bother restoring warnings at the end of this
17 // file since it's not a header.
18 #include <libmesh/ignore_warnings.h>
19 
20 using namespace libMesh;
21 
22 class CouplingMatrixTest : public CppUnit::TestCase
23 {
24 public:
25  void setUp() {}
26 
27  void tearDown() {}
28 
29  CPPUNIT_TEST_SUITE(CouplingMatrixTest);
30 
31  CPPUNIT_TEST(testSimpleAPI);
32 
33  CPPUNIT_TEST(testIteratorAPI);
34 
35  CPPUNIT_TEST_SUITE_END();
36 
37 
38 private:
40  {
41  CouplingMatrix cm(2);
42 
43  // Use a constant reference to make sure we test both const and
44  // non-const operator() implementations
45  const CouplingMatrix& cmr = cm;
46 
47  cm(0,1) = 1;
48 
49  bool cm01 = cm(0,1);
50  CPPUNIT_ASSERT_EQUAL(cm01, true);
51 
52  cm(1,0) = 1;
53 
54  for (unsigned i=0; i<2; ++i)
55  for (unsigned j=0; j<2; ++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, (i != j));
61  }
62 
63  cm.resize(8);
64 
65  for (unsigned i=0; i<8; ++i)
66  for (unsigned j=0; j<8; ++j)
67  {
68  bool cmij = cm(i,j);
69  bool cmrij = cmr(i,j);
70  CPPUNIT_ASSERT_EQUAL(cmij, cmrij);
71  CPPUNIT_ASSERT_EQUAL(cmij, false);
72  }
73 
74  // Set some elements true, in a weird order.
75  for (unsigned i=6; i>0; --i)
76  {
77  const unsigned int pi = i + (i > 4);
78  for (unsigned j=0; j<6; ++j)
79  {
80  const unsigned int pj = j + (j > 3);
81  cm(pi, pj) = true;
82  }
83  }
84 
85  // Now the tensor product of {1,2,3,4,6,7} with {0,1,2,3,5,6}
86  // should be 1.
87  for (unsigned i=0; i<8; ++i)
88  for (unsigned j=0; j<8; ++j)
89  {
90  bool cmij = cm(i,j);
91  bool cmrij = cmr(i,j);
92  CPPUNIT_ASSERT_EQUAL(cmij, cmrij);
93  if ((i != 0) && (i != 5) && (j != 4) && (j != 7))
94  {
95  CPPUNIT_ASSERT_EQUAL(cmij, true);
96  }
97  else
98  {
99  CPPUNIT_ASSERT_EQUAL(cmij, false);
100  }
101  }
102 
103  // Set some elements to false.
104  for (unsigned k=0; k<8; ++k)
105  {
106  cm(3, k) = false;
107  cm(k, 0) = false;
108  }
109 
110  // Now the tensor product of {1,2,4,6,7} with {1,2,3,5,6}
111  // should be 1.
112  for (unsigned i=0; i<8; ++i)
113  for (unsigned j=0; j<8; ++j)
114  {
115  bool cmij = cm(i,j);
116  bool cmrij = cmr(i,j);
117  CPPUNIT_ASSERT_EQUAL(cmij, cmrij);
118  if ((i != 0) && (i != 3) && (i != 5) &&
119  (j != 0) && (j != 4) && (j != 7))
120  {
121  CPPUNIT_ASSERT_EQUAL(cmij, true);
122  }
123  else
124  {
125  CPPUNIT_ASSERT_EQUAL(cmij, false);
126  }
127  }
128  }
129 
131  {
132  CouplingMatrix cm(8);
133 
134  // Set some elements true, in a weird order.
135  for (unsigned i=6; i>0; --i)
136  {
137  const unsigned int pi = i + (i > 4);
138  for (unsigned j=0; j<6; ++j)
139  {
140  const unsigned int pj = j + (j > 3);
141  cm(pi, pj) = true;
142  }
143  }
144 
145  // Now the tensor product of {1,2,3,4,6,7} with {0,1,2,3,5,6}
146  // should be 1.
147 
148  // Set some elements to false.
149  for (unsigned k=0; k<8; ++k)
150  {
151  cm(3, k) = false;
152  cm(k, 0) = false;
153  }
154 
155  // Now the tensor product of {1,2,4,6,7} with {1,2,3,5,6}
156  // should be 1.
157  const unsigned int ivals[] = {1,2,4,6,7};
158  const unsigned int non_ivals[] = {0,3,5};
159  const unsigned int jvals[] = {1,2,3,5,6};
160  // const unsigned int non_jvals[] = {0,4,7};
161 
162  const unsigned int isize = sizeof(unsigned int);
163 
164  for (unsigned int pi = 0; pi != sizeof(non_ivals)/isize; ++pi)
165  {
166  unsigned int i = non_ivals[pi];
167  ConstCouplingRow ccr(i,cm);
168  CPPUNIT_ASSERT(ccr.begin() == ccr.end());
169  }
170 
171  for (unsigned int pi = 0; pi != sizeof(ivals)/isize; ++pi)
172  {
173  unsigned int i = ivals[pi];
174  ConstCouplingRow ccr(i,cm);
175 
177 
178  for (unsigned int pj = 0; pj != sizeof(jvals)/isize; ++pj)
179  {
180  CPPUNIT_ASSERT(ccr_it != ccr.end());
181  CPPUNIT_ASSERT_EQUAL(*ccr_it, jvals[pj]);
182  ++ccr_it;
183  }
184 
185  CPPUNIT_ASSERT(ccr_it == ccr.end());
186  }
187  }
188 
189 
190 };
191 
This proxy class acts like a container of indices from a single coupling row.
const_iterator end() const
The libMesh namespace provides an interface to certain functionality in the library.
CPPUNIT_TEST_SUITE_REGISTRATION(CouplingMatrixTest)
const_iterator begin() const
void resize(const unsigned int n)
Resizes the matrix and initializes all entries to be 0.
void ErrorVector unsigned int
Definition: adjoints_ex3.C:360
const Real pi
.
Definition: libmesh.h:172
This class defines a coupling matrix.