"Fossies" - the Fresh Open Source Software Archive

Member "dune-istl-2.7.1/dune/istl/test/bcrsnormtest.cc" (26 Nov 2020, 2468 Bytes) of package /linux/misc/dune/dune-istl-2.7.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

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