Debian Codegraph @

dune/istl/test/multitypeblockvectortest.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 * \file
7 * \brief Test the MultiTypeBlockVector data structure
8 */
9 #include <iostream>
10 #include <complex>
11
12 #include <dune/common/exceptions.hh>
13 #include <dune/common/fvector.hh>
14 #include <dune/common/indices.hh>
15 #include <dune/common/float_cmp.hh>
16
17 #include <dune/istl/bvector.hh>
18 #include <dune/istl/multitypeblockvector.hh>
19 #include <dune/istl/test/vectortest.hh>
20
21 using namespace Dune;
22
23 template<typename... Args>
24 void testMultiVector(const MultiTypeBlockVector<Args...>& multiVector)
25 {
26 // Test whether the vector exports 'size_type', and whether that is an integer
27 using size_type = typename MultiTypeBlockVector<Args...>::size_type;
28 static_assert(std::numeric_limits<size_type>::is_integer, "size_type is not an integer!");
29
30 // Test whether we can use std::tuple_element
31 {
32 // Do std::tuple_element and operator[] return the same type?
33 using TupleElementType = typename std::tuple_element<0, MultiTypeBlockVector<Args...> >::type;
34 using BracketType = decltype(multiVector[Indices::_0]);
35
36 // As the return type of const operator[], BracketType will always
37 // be a const reference. We cannot simply strip the const and the &,
38 // because entries of a MultiTypeBlockVector can be references themselves.
39 // Therefore, always add const& to the result of std::tuple_element as well.
40 constexpr bool sameType = std::is_same_v<const TupleElementType&,BracketType>;
41 static_assert(sameType, "std::tuple_element does not provide the type of the 0th MultiTypeBlockVector entry!");
42 }
43
44 // test operator<<
45 std::cout << multiVector << std::endl;
46
47 // test method 'count'
48 std::cout << "multi vector has " << multiVector.N() << " first level blocks" << std::endl;
49
50 static_assert(MultiTypeBlockVector<Args...>::size()==2, "Method MultiTypeBlockVector::size() returned wrong value!");
51
52 if (multiVector.N() != 2)
53 DUNE_THROW(Exception, "Method MultiTypeBlockVector::N returned wrong value!");
54
55 if (multiVector.dim() != 11)
56 DUNE_THROW(Exception, "Method MultiTypeBlockVector::dim returned wrong value!");
57
58 // Test copy construction
59 auto multiVector2 = multiVector;
60
61 // Test assignment operator
62 multiVector2 = multiVector;
63
64 // Test operator+=
65 testVectorSpaceOperations(multiVector);
66
67 // Test assignment from scalar
68 multiVector2 = (double)0.5;
69 multiVector2 = (int)2;
70 multiVector2 = (float)0.5;
71
72 // Test the various vector norms
73 testNorms(multiVector2);
74
75 // Test operator*
76 std::cout << multiVector * multiVector2 << std::endl;
77
78 // Test method 'dot'
79 std::cout << multiVector.dot(multiVector2) << std::endl;
80 }
81
82 int main(int argc, char** argv) try
83 {
84 using namespace Indices;
85
86 MultiTypeBlockVector<BlockVector<FieldVector<double,3> >, BlockVector<FieldVector<double,1> > > multiVector;
87
88 multiVector[_0] = {{1,0,0},
89 {0,1,0},
90 {0,0,1}};
91
92 multiVector[_1] = {3.14, 42};
93
94 testMultiVector(multiVector);
95
96 // create a "shallow" copy
97 MultiTypeBlockVector<BlockVector<FieldVector<double,3> >&, BlockVector<FieldVector<double,1> >& >
98 multiVectorRef(multiVector[_0], multiVector[_1]);
99
100 multiVectorRef[_0][0][0] = 5.0;
101
102 if (!FloatCmp::eq(multiVectorRef[_0][0][0], multiVector[_0][0][0]))
103 DUNE_THROW(Exception, "Modifying an entry of the referencing vector failed!");
104
105 testMultiVector(multiVectorRef);
106
107 // test field_type (std::common_type) for some combinations
108 static_assert ( std::is_same_v<typename MultiTypeBlockVector<BlockVector<FieldVector<double,3> >, BlockVector<FieldVector<double,1> > >::field_type,
109 double > );
110 static_assert ( std::is_same_v<typename MultiTypeBlockVector<BlockVector<FieldVector<float,3> >, BlockVector<FieldVector<float,1> > >::field_type,
111 float > );
112 static_assert ( std::is_same_v<typename MultiTypeBlockVector<BlockVector<FieldVector<float,3> >, BlockVector<FieldVector<double,1> > >::field_type,
113 double > );
114 static_assert ( std::is_same_v<typename MultiTypeBlockVector<BlockVector<FieldVector<double,3> >, BlockVector<FieldVector<std::complex<double>,1> > >::field_type,
115 std::complex<double> > );
116
117 return 0;
118 }
119 catch (Dune::Exception& e)
120 {
121 std::cerr << "DUNE reported an exception: " << e << std::endl;
122 return 1;
123 }
124 catch (std::exception& e)
125 {
126 std::cerr << "C++ reported an exception: " << e.what() << std::endl;
127 return 2;
128 } catch (...)
129 {
130 std::cerr << "Unknown exception encountered!" << std::endl;
131 return 3;
132 }