Debian Codegraph @

dune/istl/test/bcrsimplicitbuild.cc

raw
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5
6 #undef NDEBUG // make sure assert works
7
8 #include <dune/common/float_cmp.hh>
9 #include <dune/common/fmatrix.hh>
10 #include <dune/common/exceptions.hh>
11 #include <dune/istl/bcrsmatrix.hh>
12
13 typedef Dune::BCRSMatrix<Dune::FieldMatrix<double,1,1> > ScalarMatrix;
14
15 void buildMatrix(ScalarMatrix& m)
16 {
17 m.entry(0,0) = 1.0; m.entry(0,1) = 1.0; m.entry(0,2) = 1.0; m.entry(0,3) = 1.0;
18 m.entry(1,0) = 1.0; m.entry(1,1) = 1.0; m.entry(1,2) = 1.0;
19 m.entry(2,1) = 1.0; m.entry(2,2) = 1.0; m.entry(2,3) = 1.0;
20 m.entry(3,2) = 1.0; m.entry(3,3) = 1.0; m.entry(3,4) = 1.0;
21 m.entry(4,3) = 1.0; m.entry(4,4) = 1.0; m.entry(4,5) = 1.0;
22 m.entry(5,4) = 1.0; m.entry(5,5) = 1.0; m.entry(5,6) = 1.0;
23 m.entry(6,5) = 1.0; m.entry(6,6) = 1.0; m.entry(6,7) = 1.0;
24 m.entry(7,6) = 1.0; m.entry(7,7) = 1.0; m.entry(7,8) = 1.0;
25 m.entry(8,7) = 1.0; m.entry(8,8) = 1.0; m.entry(8,9) = 1.0;
26 m.entry(9,8) = 1.0; m.entry(9,9) = 1.0;
27 // add some more entries in random order
28 m.entry(7,3) = 1.0;
29 m.entry(6,0) = 1.0;
30 m.entry(3,8) = 1.0;
31 }
32
33 template<typename M>
34 void setMatrix(M& m)
35 {
36 m[0][0] = 1.0; m[0][1] = 1.0; m[0][2] = 1.0; m[0][3] = 1.0;
37 m[1][0] = 1.0; m[1][1] = 1.0; m[1][2] = 1.0;
38 m[2][1] = 1.0; m[2][2] = 1.0; m[2][3] = 1.0;
39 m[3][2] = 1.0; m[3][3] = 1.0; m[3][4] = 1.0;
40 m[4][3] = 1.0; m[4][4] = 1.0; m[4][5] = 1.0;
41 m[5][4] = 1.0; m[5][5] = 1.0; m[5][6] = 1.0;
42 m[6][5] = 1.0; m[6][6] = 1.0; m[6][7] = 1.0;
43 m[7][6] = 1.0; m[7][7] = 1.0; m[7][8] = 1.0;
44 m[8][7] = 1.0; m[8][8] = 1.0; m[8][9] = 1.0;
45 m[9][8] = 1.0; m[9][9] = 1.0;
46 // add some more entries in random order
47 m[7][3] = 1.0;
48 m[6][0] = 1.0;
49 m[3][8] = 1.0;
50 }
51
52 void testZeroSizeImplicitBuild()
53 {
54 ScalarMatrix m(0,0,3,0.1,ScalarMatrix::implicit);
55 ScalarMatrix::CompressionStatistics stats = m.compress();
56
57 ScalarMatrix m1(0,0,0,0,ScalarMatrix::implicit);
58 stats = m1.compress();
59 }
60
61 void testImplicitBuild()
62 {
63 ScalarMatrix m(10,10,3,0.1,ScalarMatrix::implicit);
64 buildMatrix(m);
65 ScalarMatrix::CompressionStatistics stats = m.compress();
66 assert(Dune::FloatCmp::eq(stats.avg,33./10.));
67 assert(stats.maximum == 4);
68 assert(stats.overflow_total == 4);
69 setMatrix(m);
70 ScalarMatrix m1(m);
71 }
72
73 void testImplicitBuildWithInsufficientOverflow()
74 {
75 try {
76 ScalarMatrix m(10,10,1,0,ScalarMatrix::implicit);
77 // add diagonal entries + completely fill the first row with entries
78 // with the current base buffer of 4 * avg, that should be enough to make
79 // compress fail.
80 for (int i = 0; i < 10; ++i)
81 {
82 m.entry(i,i) = 1.0;
83 m.entry(0,i) = 1.0;
84 }
85 m.compress();
86 assert(false && "compress() should have thrown an exception");
87 } catch (const Dune::ImplicitModeCompressionBufferExhausted& e) {
88 // test passed
89 }
90 }
91
92 void testSetterInterface()
93 {
94 ScalarMatrix m;
95 m.setBuildMode(ScalarMatrix::implicit);
96 m.setImplicitBuildModeParameters(3,0.1);
97 m.setSize(10,10);
98 buildMatrix(m);
99 ScalarMatrix::CompressionStatistics stats = m.compress();
100 assert(Dune::FloatCmp::eq(stats.avg,33.0/10.0));
101 assert(stats.maximum == 4);
102 assert(stats.overflow_total == 4);
103 }
104
105 void testDoubleSetSize()
106 {
107 ScalarMatrix m;
108 m.setBuildMode(ScalarMatrix::implicit);
109 m.setImplicitBuildModeParameters(3,0.1);
110 m.setSize(14,14);
111 m.setSize(10,10);
112 buildMatrix(m);
113 ScalarMatrix::CompressionStatistics stats = m.compress();
114 assert(Dune::FloatCmp::eq(stats.avg,33.0/10.0));
115 assert(stats.maximum == 4);
116 assert(stats.overflow_total == 4);
117 }
118
119 int testInvalidBuildModeConstructorCall()
120 {
121 try {
122 ScalarMatrix m(10,10,1,-1.0,ScalarMatrix::random);
123 std::cerr<< "ERROR: Constructor should have thrown an exception!"<<std::endl;
124 return 1;
125 } catch (Dune::BCRSMatrixError& e) {
126 // test passed
127 return 0;
128 }
129 }
130
131 int testNegativeOverflowConstructorCall()
132 {
133 try {
134 ScalarMatrix m(10,10,1,-1.0,ScalarMatrix::implicit);
135 std::cerr<<"ERROR: Constructor should have thrown an exception!"<<std::endl;
136 return 1;
137 } catch (Dune::BCRSMatrixError& e) {
138 // test passed
139 return 0;
140 }
141 }
142
143 int testInvalidSetImplicitBuildModeParameters()
144 {
145 try {
146 ScalarMatrix m;
147 m.setBuildMode(ScalarMatrix::implicit);
148 m.setImplicitBuildModeParameters(1,-1.0);
149 std::cerr<<"ERROR: setImplicitBuildModeParameters() should have thrown an exception!"<<std::endl;
150 return 1;
151 } catch (Dune::BCRSMatrixError& e) {
152 // test passed
153 return 0;
154 }
155 }
156
157 int testSetImplicitBuildModeParametersAfterSetSize()
158 {
159 try {
160 ScalarMatrix m;
161 m.setBuildMode(ScalarMatrix::implicit);
162 m.setImplicitBuildModeParameters(3,0.1);
163 m.setSize(10,10);
164 m.setImplicitBuildModeParameters(4,0.1);
165 std::cerr<<"setImplicitBuildModeParameters() should have thrown an exception!"<<std::endl;
166 return 1;
167 } catch (Dune::InvalidStateException& e) {
168 // test passed
169 return 0;
170 }
171 }
172
173 int testSetSizeWithNonzeroes()
174 {
175 try {
176 ScalarMatrix m;
177 m.setBuildMode(ScalarMatrix::implicit);
178 m.setImplicitBuildModeParameters(3,0.1);
179 m.setSize(10,10,300);
180 std::cerr<<"ERROR: setSize() should have thrown an exception!"<<std::endl;
181 return 1;
182 } catch (Dune::BCRSMatrixError& e) {
183 // test passed
184 return 0;
185 }
186 }
187
188 void testCopyConstructionAndAssignment()
189 {
190 ScalarMatrix m(10,10,3,0.1,ScalarMatrix::implicit);
191 buildMatrix(m);
192 m.compress();
193 ScalarMatrix m2(m);
194 m2 = 3.0;
195 ScalarMatrix m3(m);
196 m3 = m2;
197 ScalarMatrix m4;
198 m4 = m;
199 }
200
201 int testInvalidCopyConstruction()
202 {
203 try {
204 ScalarMatrix m(10,10,3,0.1,ScalarMatrix::implicit);
205 buildMatrix(m);
206 ScalarMatrix m2(m);
207 std::cerr<<"ERROR: copy constructor should have thrown an exception!"<<std::endl;
208 return 1;
209 } catch (Dune::InvalidStateException& e) {
210 // test passed
211 return 0;
212 }
213 }
214
215 int testInvalidCopyAssignment()
216 {
217 ScalarMatrix m(10,10,3,0.1,ScalarMatrix::implicit);
218 buildMatrix(m);
219 int ret=0;
220 // copy incomplete matrix into empty one
221 try {
222 ScalarMatrix m2;
223 m2 = m;
224 std::cerr<<"ERROR: operator=() should have thrown an exception!"<<std::endl;
225 ++ret;
226 } catch (Dune::InvalidStateException& e) {
227 // test passed
228 }
229 // copy incomplete matrix into full one
230 try {
231 ScalarMatrix m2(10,10,3,0.1,ScalarMatrix::implicit);
232 buildMatrix(m2);
233 m2.compress();
234 m2 = m;
235 std::cerr<<"ERROR: operator=() should have thrown an exception!"<<std::endl;
236 ++ret;
237 } catch (Dune::InvalidStateException& e) {
238 // test passed
239 }
240 // copy fully build matrix into half-built one
241 m.compress();
242 try {
243 ScalarMatrix m2(10,10,3,0.1,ScalarMatrix::implicit);
244 buildMatrix(m2);
245 m2 = m;
246 std::cerr<<"ERROR: operator=() should have thrown an exception!"<<std::endl;
247 ++ret;
248 } catch (Dune::InvalidStateException& e) {
249 // test passed
250 }
251 return ret;
252 }
253
254 int testEntryConsistency()
255 {
256 int ret=0;
257 ScalarMatrix m(10,10,3,0.1,ScalarMatrix::implicit);
258 if(!Dune::FloatCmp::eq(static_cast<const double&>(m.entry(0,3)),0.0))
259 ret++;
260 if(!Dune::FloatCmp::eq(static_cast<const double&>(m.entry(7,6)),0.0))
261 ++ret;
262 buildMatrix(m);
263 if(!Dune::FloatCmp::eq(static_cast<const double&>(m.entry(0,3)),1.0))
264 ++ret;
265 if(!Dune::FloatCmp::eq(static_cast<const double&>(m.entry(7,6)),1.0))
266 ++ret;
267 m.entry(4,4) += 3.0;
268 if(!Dune::FloatCmp::eq(static_cast<const double&>(m.entry(4,4)),4.0))
269 ++ret;
270 m.compress();
271 if(!Dune::FloatCmp::eq(static_cast<const double&>(m[0][3]),1.0))
272 ++ret;
273 if(!Dune::FloatCmp::eq(static_cast<const double&>(m[7][6]),1.0))
274 ++ret;
275 if(!Dune::FloatCmp::eq(static_cast<const double&>(m[4][4]),4.0))
276 ++ret;
277 if(ret)
278 std::cerr<<"ERROR: Entries are not consistent"<<std::endl;
279 return ret;
280 }
281
282 int testEntryAfterCompress()
283 {
284 try {
285 ScalarMatrix m(10,10,3,0.1,ScalarMatrix::implicit);
286 buildMatrix(m);
287 m.compress();
288 m.entry(3,3);
289 std::cerr<<"ERROR: entry() should have thrown an exception!"<<std::endl;
290 return 1;
291 } catch (Dune::BCRSMatrixError& e) {
292 // test passed
293 return 0;
294 }
295 }
296
297 int testBracketOperatorBeforeCompress()
298 {
299 try {
300 ScalarMatrix m(10,10,3,0.1,ScalarMatrix::implicit);
301 buildMatrix(m);
302 m[3][3];
303 std::cerr<<"ERROR: operator[]() should have thrown an exception!"<<std::endl;
304 return 1;
305 } catch (Dune::BCRSMatrixError& e) {
306 // test passed
307 return 0;
308 }
309 }
310
311 int testConstBracketOperatorBeforeCompress()
312 {
313 try {
314 ScalarMatrix m(10,10,3,0.1,ScalarMatrix::implicit);
315 buildMatrix(m);
316 const_cast<const ScalarMatrix&>(m)[3][3];
317 std::cerr<<"ERROR: operator[]() should have thrown an exception!"<<std::endl;
318 return 1;
319 } catch (Dune::BCRSMatrixError& e) {
320 // test passed
321 return 0;
322 }
323 }
324
325 void testImplicitMatrixBuilder()
326 {
327 ScalarMatrix m(10,10,3,0.1,ScalarMatrix::implicit);
328 Dune::ImplicitMatrixBuilder<ScalarMatrix> b(m);
329 setMatrix(b);
330 m.compress();
331 setMatrix(m);
332 }
333
334 void testImplicitMatrixBuilderExtendedConstructor()
335 {
336 ScalarMatrix m;
337 Dune::ImplicitMatrixBuilder<ScalarMatrix> b(m,10,10,3,0.1);
338 setMatrix(b);
339 m.compress();
340 setMatrix(m);
341 }
342
343 int main()
344 {
345 int ret=0;
346 try{
347 testImplicitBuild();
348 testImplicitBuildWithInsufficientOverflow();
349 testSetterInterface();
350 testDoubleSetSize();
351 ret+=testInvalidBuildModeConstructorCall();
352 ret+=testNegativeOverflowConstructorCall();
353 ret+=testInvalidSetImplicitBuildModeParameters();
354 ret+=testSetImplicitBuildModeParametersAfterSetSize();
355 ret+=testSetSizeWithNonzeroes();
356 testCopyConstructionAndAssignment();
357 ret+=testInvalidCopyConstruction();
358 ret+=testInvalidCopyAssignment();
359 ret+=testEntryConsistency();
360 ret+=testEntryAfterCompress();
361 ret+=testBracketOperatorBeforeCompress();
362 ret+=testConstBracketOperatorBeforeCompress();
363 testImplicitMatrixBuilder();
364 testImplicitMatrixBuilderExtendedConstructor();
365 testZeroSizeImplicitBuild();
366 }catch(Dune::Exception& e) {
367 std::cerr << e <<std::endl;
368 return 1;
369 }
370 return ret;
371 }