Debian Codegraph @

dune/istl/test/bcrsnormtest.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 #include <complex>
4 #include <memory>
5
6 #include <dune/common/fmatrix.hh>
7 #include <dune/istl/bcrsmatrix.hh>
8
9 template <class V>
10 void checkNormNANVector(V const &v, int line) {
11 if (!std::isnan(v.infinity_norm())) {
12 std::cerr << "error: norm not NaN: infinity_norm() on line " << line
13 << " (type: " << Dune::className(v[0]) << ")" << std::endl;
14 std::exit(-1);
15 }
16 }
17
18 template <class M>
19 void checkNormNANMatrix(M const &v, int line) {
20 if (!std::isnan(v.frobenius_norm())) {
21 std::cerr << "error: norm not NaN: frobenius_norm() on line " << line
22 << " (type: " << Dune::className(v[0][0]) << ")" << std::endl;
23 std::exit(-1);
24 }
25 if (!std::isnan(v.infinity_norm())) {
26 std::cerr << "error: norm not NaN: infinity_norm() on line " << line
27 << " (type: " << Dune::className(v[0][0]) << ")" << std::endl;
28 std::exit(-1);
29 }
30 }
31
32 template <typename T>
33 std::shared_ptr<Dune::BCRSMatrix<Dune::FieldMatrix<T, 2, 2>>> genPattern() {
34 using LocalMatrix = Dune::FieldMatrix<T, 2, 2>;
35 using GlobalMatrix = Dune::BCRSMatrix<LocalMatrix>;
36
37 // Build a 3x3 matrix with sparsity pattern
38 //
39 // +-+
40 // ---
41 // +-+
42 auto m = std::make_shared<GlobalMatrix>(3, 3, GlobalMatrix::random);
43
44 m->setrowsize(0, 2);
45 m->setrowsize(1, 0);
46 m->setrowsize(2, 2);
47 m->endrowsizes();
48
49 m->addindex(0, 0);
50 m->addindex(0, 2);
51 m->addindex(2, 0);
52 m->addindex(2, 2);
53 m->endindices();
54
55 return m;
56 }
57
58 // Make sure that matrices with NaN entries have norm NaN.
59 // See also bug flyspray/FS#1147
60 template <typename T>
61 void test_nan(T const &mynan) {
62 T n(0);
63 {
64 auto m = genPattern<T>();
65 (*m)[0][0] = {{n, n}, {n, mynan}};
66 (*m)[0][2] = n;
67 (*m)[2][0] = n;
68 (*m)[2][2] = n;
69 checkNormNANVector((*m)[0], __LINE__);
70 checkNormNANMatrix(*m, __LINE__);
71 }
72 {
73 auto m = genPattern<T>();
74 (*m)[0][0] = n;
75 (*m)[0][2] = {{n, n}, {n, mynan}};;
76 (*m)[2][0] = n;
77 (*m)[2][2] = n;
78 checkNormNANVector((*m)[0], __LINE__);
79 checkNormNANMatrix(*m, __LINE__);
80 }
81 {
82 auto m = genPattern<T>();
83 (*m)[0][0] = n;
84 (*m)[0][2] = n;
85 (*m)[2][0] = {{n, n}, {n, mynan}};;
86 (*m)[2][2] = n;
87 checkNormNANVector((*m)[2], __LINE__);
88 checkNormNANMatrix(*m, __LINE__);
89 }
90 }
91
92 int main() {
93 {
94 double nan = std::nan("");
95 test_nan(nan);
96 }
97 {
98 std::complex<double> nan(std::nan(""), 17);
99 test_nan(nan);
100 }
101 }