Debian Codegraph @

dune/istl/test/matrixtest.hh

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 #ifndef DUNE_ISTL_TEST_MATRIXTEST_HH
6 #define DUNE_ISTL_TEST_MATRIXTEST_HH
7
8 /** \file
9 * \brief Infrastructure for testing the dune-istl matrix interface
10 *
11 * This file contains various methods that test parts of the dune-istl matrix interface.
12 * They only test very general features, i.e., features that every dune-istl matrix should
13 * have.
14 *
15 * At the same time, these tests should help to define what the dune-istl matrix interface
16 * actually is. They may not currently define the entire interface, but they are a lower
17 * bound: any feature that is tested here is part of the dune-istl matrix interface.
18 */
19
20 #include <dune/common/exceptions.hh>
21 #include <dune/common/test/iteratortest.hh>
22
23 namespace Dune
24 {
25 /** \brief Test whether the given type is default- and copy-constructible */
26 template <typename Matrix>
27 void testMatrixConstructibility()
28 {
29 static_assert(std::is_default_constructible<Matrix>::value, "Matrix type is not default constructible");
30 static_assert(std::is_copy_constructible<Matrix>::value, "Matrix type is not copy constructible");
31 }
32
33 template <typename Matrix, typename DomainVector, typename RangeVector>
34 void testMatrixVectorProducts(const Matrix& matrix, const DomainVector& domain, const RangeVector& range)
35 {
36 // Make mutable copies of the vectors
37 DomainVector domainMutable = domain;
38 RangeVector rangeMutable = range;
39
40 matrix.mv(domain,rangeMutable);
41 matrix.umv(domain,rangeMutable);
42 matrix.mmv(domain,rangeMutable);
43 matrix.usmv(1.0,domain,rangeMutable);
44 matrix.mtv(range,domainMutable);
45 matrix.umtv(range,domainMutable);
46 matrix.mmtv(range,domainMutable);
47 matrix.usmtv(1.0,range,domainMutable);
48 matrix.umhv(domain,rangeMutable);
49 matrix.mmhv(domain,rangeMutable);
50 matrix.usmhv(1.0,range,domainMutable);
51 }
52
53 /** \brief Test whether a given type implements all the norms required from a dune-istl matrix
54 */
55 template <typename Matrix>
56 void testNorms(const Matrix& m)
57 {
58 using field_type = typename Matrix::field_type;
59 using real_type = typename FieldTraits<field_type>::real_type;
60
61 // frobenius_norm
62 static_assert(std::is_same<decltype(m.frobenius_norm()),real_type>::value, "'frobenius_norm' does not return 'real_type'");
63 if (m.frobenius_norm() < 0.0)
64 DUNE_THROW(RangeError, "'frobenius_norm' returns negative value");
65
66 // frobenius_norm2
67 static_assert(std::is_same<decltype(m.frobenius_norm2()),real_type>::value, "'frobenius_norm2' does not return 'real_type'");
68 if (m.frobenius_norm2() < 0.0)
69 DUNE_THROW(RangeError, "'frobenius_norm2' returns negative value");
70
71 // infinity_norm
72 static_assert(std::is_same<decltype(m.infinity_norm()),real_type>::value, "'infinity_norm' does not return 'real_type'");
73 if (m.infinity_norm() < 0.0)
74 DUNE_THROW(RangeError, "'infinity_norm' returns negative value");
75
76 // infinity_norm_real
77 static_assert(std::is_same<decltype(m.infinity_norm_real()),real_type>::value, "'infinity_norm_real' does not return 'real_type'");
78 if (m.infinity_norm_real() < 0.0)
79 DUNE_THROW(RangeError, "'infinity_norm_real' returns negative value");
80 }
81
82 template <typename Matrix>
83 void testVectorSpaceOperations(const Matrix& m)
84 {
85 // Make a mutable copy of the argument
86 Matrix mMutable = m;
87
88 // operator+=
89 mMutable += m;
90 // operator-=
91 mMutable -= m;
92
93 // operator*=
94 mMutable *= 0.5;
95
96 // operator/=
97 mMutable /= 0.5;
98
99 // axpy -- not sure whether that really is an interface method
100 //mMutable.axpy(0.5,m);
101 }
102
103 } // namespace Dune
104
105 #endif