1
2 #include <config.h>
3
4 #include <iostream>
5
6 #include <dune/common/bitsetvector.hh>
7 #include <dune/common/fmatrix.hh>
8 #include <dune/common/dynmatrix.hh>
9 #include <dune/common/test/testsuite.hh>
10 #include <dune/common/tuplevector.hh>
11
12 #include <dune/istl/bcrsmatrix.hh>
13 #include <dune/istl/multitypeblockmatrix.hh>
14 #include <dune/istl/foreach.hh>
15 #include <dune/istl/matrixindexset.hh>
16
17
18
19
20 using namespace Dune;
21
testFlatVectorForEach()22 TestSuite testFlatVectorForEach()
23 {
24 TestSuite t;
25
26 // mix up some types
27
28 FieldVector<double,3> f3;
29 FieldVector<double,1> f1;
30
31 DynamicVector<FieldVector<double,3>> d3;
32
33 std::vector<FieldVector<double,1>> v1;
34
35 d3.resize(5);
36 v1.resize(5);
37
38 using MTBV = MultiTypeBlockVector<DynamicVector<FieldVector<double,3>>,std::vector<FieldVector<double,1>>>;
39
40 MTBV v;
41
42 v[Indices::_0] = d3;
43 v[Indices::_1] = v1;
44
45 int entries = 0;
46
47 auto countEntres = [&](auto&& entry, auto&& index){
48 entries++;
49 };
50
51 auto s = flatVectorForEach(v,countEntres);
52
53 t.check( entries == 20 );
54 t.check( s == 20 );
55
56 return t;
57 }
58
59
testFlatVectorForEachBitSetVector()60 TestSuite testFlatVectorForEachBitSetVector()
61 {
62 TestSuite t;
63
64 int entries = 0;
65
66 auto countEntres = [&](auto&& entry, auto&& index){
67 entries++;
68 };
69
70 BitSetVector<2> bitSetVector;
71 bitSetVector.resize(10);
72
73 auto s = flatVectorForEach(bitSetVector,countEntres);
74
75 t.check( entries == 20 );
76 t.check( s == 20 );
77
78 return t;
79 }
80
81
testFlatMatrixForEachStatic()82 TestSuite testFlatMatrixForEachStatic()
83 {
84 TestSuite t;
85
86 FieldMatrix<double,3,3> F33;
87 FieldMatrix<double,3,1> F31;
88 FieldMatrix<double,1,3> F13;
89 FieldMatrix<double,1,1> F11;
90
91 BCRSMatrix<FieldMatrix<double,3,3>> B33;
92 BCRSMatrix<FieldMatrix<double,3,1>> B31;
93 BCRSMatrix<FieldMatrix<double,1,3>> B13;
94 BCRSMatrix<FieldMatrix<double,1,1>> B11;
95
96 MatrixIndexSet mis;
97 mis.resize(3,3);
98
99 // set some indices ( skip one row for the top left block )
100 mis.add(0,0);
101 mis.add(2,1);
102
103 mis.exportIdx(B33);
104
105 mis.add(1,1);
106
107 mis.exportIdx(B31);
108 mis.exportIdx(B13);
109 mis.exportIdx(B11);
110
111 using Row0 = MultiTypeBlockVector<BCRSMatrix<FieldMatrix<double,3,3>>,BCRSMatrix<FieldMatrix<double,3,1>>>;
112 using Row1 = MultiTypeBlockVector<BCRSMatrix<FieldMatrix<double,1,3>>,BCRSMatrix<FieldMatrix<double,1,1>>>;
113
114 using MTMatrix = MultiTypeBlockMatrix<Row0,Row1>;
115
116 MTMatrix M;
117 M[Indices::_0][Indices::_0] = B33;
118 M[Indices::_0][Indices::_1] = B31;
119 M[Indices::_1][Indices::_0] = B13;
120 M[Indices::_1][Indices::_1] = B11;
121
122
123 int entries = 0;
124
125 auto [ rows , cols ] = flatMatrixForEach( M, [&](auto&& /*entry*/, auto&& rowIndex, auto&& colIndex){
126
127 entries++;
128
129 });
130
131 t.check( entries == 39 , " wrong number of entries ");
132 t.check( rows == 12 , " wrong number of rows ");
133 t.check( cols == 12 , " wrong number of cols ");
134 return t;
135 }
136
137
138
testFlatMatrixForEachDynamic()139 TestSuite testFlatMatrixForEachDynamic()
140 {
141 TestSuite t;
142
143 DynamicMatrix<double> F33(3,3);
144
145
146 BCRSMatrix<DynamicMatrix<double>> B;
147
148
149 MatrixIndexSet mis;
150 mis.resize(3,3);
151
152 // set some entries and leave one line empty on purpose
153 mis.add(0,0);
154 mis.add(1,1);
155
156 mis.exportIdx(B);
157
158 B[0][0] = F33;
159 B[1][1] = F33;
160
161
162 int entries = 0;
163
164 auto [ rows , cols ] = flatMatrixForEach( B, [&](auto&& /*entry*/, auto&& rowIndex, auto&& colIndex){
165
166 entries++;
167
168 });
169
170 t.check( entries == 18 , " wrong number of entries ");
171 t.check( rows == 9 , " wrong number of rows ");
172 t.check( cols == 9 , " wrong number of cols ");
173 return t;
174 }
175
176
177
main(int argc,char ** argv)178 int main(int argc, char** argv)
179 {
180 TestSuite t;
181
182 t.subTest(testFlatVectorForEach());
183 t.subTest(testFlatVectorForEachBitSetVector());
184 t.subTest(testFlatMatrixForEachStatic());
185 t.subTest(testFlatMatrixForEachDynamic());
186
187 return t.exit();
188 }
189