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