]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add tests. 16732/head
authorWolfgang Bangerth <bangerth@colostate.edu>
Thu, 7 Mar 2024 21:56:15 +0000 (14:56 -0700)
committerWolfgang Bangerth <bangerth@colostate.edu>
Fri, 8 Mar 2024 03:37:43 +0000 (20:37 -0700)
tests/trilinos_tpetra/sparse_matrix_set_01.cc [new file with mode: 0644]
tests/trilinos_tpetra/sparse_matrix_set_01.output [new file with mode: 0644]
tests/trilinos_tpetra/sparse_matrix_set_02.cc [new file with mode: 0644]
tests/trilinos_tpetra/sparse_matrix_set_02.output [new file with mode: 0644]

diff --git a/tests/trilinos_tpetra/sparse_matrix_set_01.cc b/tests/trilinos_tpetra/sparse_matrix_set_01.cc
new file mode 100644 (file)
index 0000000..7b6aaab
--- /dev/null
@@ -0,0 +1,133 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2004 - 2018 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE.md at
+// the top level directory of deal.II.
+//
+// ---------------------------------------------------------------------
+
+
+
+// compare collective setting of elements in a trilinos matrix using
+// LinearAlgebra::TpetraWrappers::SparseMatrix<double>::set() with element-wise
+// setting
+
+#include <deal.II/base/utilities.h>
+
+#include <deal.II/lac/trilinos_tpetra_sparse_matrix.h>
+
+#include <iostream>
+
+#include "../tests.h"
+
+
+void
+test(LinearAlgebra::TpetraWrappers::SparseMatrix<double> &m)
+{
+  // first set a few entries one-by-one
+  for (unsigned int i = 0; i < m.m(); ++i)
+    for (unsigned int j = 0; j < m.n(); ++j)
+      if ((i + 2 * j + 1) % 3 == 0)
+        m.set(i, j, i * j * .5 + .5);
+
+  m.compress(VectorOperation::insert);
+
+  LinearAlgebra::TpetraWrappers::SparseMatrix<double> m2(m.m(),
+                                                         m.n(),
+                                                         m.n() / 3 + 1);
+
+  // now set the same elements row-wise
+  {
+    std::vector<types::global_dof_index> col_indices(m.n() / 3 + 1);
+    std::vector<double>                  col_values(m.n() / 3 + 1);
+    for (unsigned int i = 0; i < m.m(); ++i)
+      {
+        unsigned int col_index = 0;
+        // count the number of elements in this
+        // row
+        for (unsigned int j = 0; j < m.n(); ++j)
+          if ((i + 2 * j + 1) % 3 == 0)
+            ++col_index;
+
+        col_indices.resize(col_index);
+        col_values.resize(col_index);
+        col_index = 0;
+
+        // extract column values
+        for (unsigned int j = 0; j < m.n(); ++j)
+          if ((i + 2 * j + 1) % 3 == 0)
+            {
+              col_indices[col_index] = j;
+              col_values[col_index]  = i * j * .5 + .5;
+              col_index++;
+            }
+
+        m2.set(i, col_indices, col_values);
+      }
+  }
+
+  m2.compress(VectorOperation::insert);
+
+  // subtract the matrix m from this one,
+  // we should get a zero matrix.
+  m2.add(-1.0, m);
+  // Calculate the Frobenius norm of the
+  // matrix in order to check whether all
+  // elements really are zero.
+  double norm = m2.frobenius_norm();
+  AssertThrow(norm == 0, ExcInternalError());
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+int
+main(int argc, char **argv)
+{
+  initlog();
+
+  Utilities::MPI::MPI_InitFinalize mpi_initialization(
+    argc, argv, testing_max_num_threads());
+
+  try
+    {
+      {
+        LinearAlgebra::TpetraWrappers::SparseMatrix<double> m(5U, 5U, 3U);
+        test(m);
+      }
+    }
+  catch (const std::exception &exc)
+    {
+      std::cerr << std::endl
+                << std::endl
+                << "----------------------------------------------------"
+                << std::endl;
+      std::cerr << "Exception on processing: " << std::endl
+                << exc.what() << std::endl
+                << "Aborting!" << std::endl
+                << "----------------------------------------------------"
+                << std::endl;
+
+      return 1;
+    }
+  catch (...)
+    {
+      std::cerr << std::endl
+                << std::endl
+                << "----------------------------------------------------"
+                << std::endl;
+      std::cerr << "Unknown exception!" << std::endl
+                << "Aborting!" << std::endl
+                << "----------------------------------------------------"
+                << std::endl;
+      return 1;
+    };
+}
diff --git a/tests/trilinos_tpetra/sparse_matrix_set_01.output b/tests/trilinos_tpetra/sparse_matrix_set_01.output
new file mode 100644 (file)
index 0000000..0fd8fc1
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::OK
diff --git a/tests/trilinos_tpetra/sparse_matrix_set_02.cc b/tests/trilinos_tpetra/sparse_matrix_set_02.cc
new file mode 100644 (file)
index 0000000..e09c408
--- /dev/null
@@ -0,0 +1,131 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2004 - 2018 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE.md at
+// the top level directory of deal.II.
+//
+// ---------------------------------------------------------------------
+
+
+
+// compare collective setting of elements in a trilinos matrix using
+// LinearAlgebra::TpetraWrappers::SparseMatrix<double>::set() and a
+// FullMatrix<double> with setting the same elements on an element-by-element
+// level. Use the entries as they would result from a Laplace operator in 1D.
+
+#include <deal.II/base/utilities.h>
+
+#include <deal.II/lac/full_matrix.h>
+#include <deal.II/lac/trilinos_tpetra_sparse_matrix.h>
+
+#include <iostream>
+
+#include "../tests.h"
+
+
+void
+test(LinearAlgebra::TpetraWrappers::SparseMatrix<double> &m)
+{
+  LinearAlgebra::TpetraWrappers::SparseMatrix<double> m2(m.m(), m.n(), 3U);
+
+  // first set a few entries one-by-one and
+  // initialize the sparsity pattern for m2
+  for (unsigned int i = 0; i < m.m(); ++i)
+    for (unsigned int j = 0; j < m.n(); ++j)
+      if (std::fabs((double)i - j) < 2)
+        {
+          double value;
+          if (i == j)
+            if (i > 0 && i < m.m() - 1)
+              value = 2.;
+            else
+              value = 1.;
+          else
+            value = -1.;
+
+          m.set(i, j, value);
+        }
+  m.compress(VectorOperation::insert);
+  // now add the same elements from a full
+  // matrix (if the same element is set
+  // more than once, Trilinos adds them)
+  {
+    FullMatrix<double> full_matrix(2, 2);
+    full_matrix(0, 0) = full_matrix(1, 1) = 1.;
+    full_matrix(0, 1) = full_matrix(1, 0) = -1.;
+    std::vector<types::global_dof_index> local_indices(2);
+
+    for (unsigned int i = 0; i < m.m() - 1; ++i)
+      {
+        local_indices[0] = i;
+        local_indices[1] = i + 1;
+
+        m2.set(local_indices, local_indices, full_matrix);
+      }
+  }
+
+  m2.compress(VectorOperation::insert);
+
+  // subtract the matrix m from this one,
+  // we should get a zero matrix
+  m2.add(-1.0, m);
+  // calculate the Frobenius norm of the
+  // matrix in order to check whether all
+  // elements really are zero
+  double norm = m2.frobenius_norm();
+  AssertThrow(norm == 0, ExcInternalError());
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+int
+main(int argc, char **argv)
+{
+  initlog();
+
+  Utilities::MPI::MPI_InitFinalize mpi_initialization(
+    argc, argv, testing_max_num_threads());
+
+  try
+    {
+      {
+        LinearAlgebra::TpetraWrappers::SparseMatrix<double> m(16U, 16U, 3U);
+        test(m);
+      }
+    }
+  catch (const std::exception &exc)
+    {
+      std::cerr << std::endl
+                << std::endl
+                << "----------------------------------------------------"
+                << std::endl;
+      std::cerr << "Exception on processing: " << std::endl
+                << exc.what() << std::endl
+                << "Aborting!" << std::endl
+                << "----------------------------------------------------"
+                << std::endl;
+
+      return 1;
+    }
+  catch (...)
+    {
+      std::cerr << std::endl
+                << std::endl
+                << "----------------------------------------------------"
+                << std::endl;
+      std::cerr << "Unknown exception!" << std::endl
+                << "Aborting!" << std::endl
+                << "----------------------------------------------------"
+                << std::endl;
+      return 1;
+    };
+}
diff --git a/tests/trilinos_tpetra/sparse_matrix_set_02.output b/tests/trilinos_tpetra/sparse_matrix_set_02.output
new file mode 100644 (file)
index 0000000..0fd8fc1
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::OK

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.