]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add several tests for collective add operations in sparse matrix.
authorMartin Kronbichler <kronbichler@lnm.mw.tum.de>
Wed, 8 Sep 2010 15:38:45 +0000 (15:38 +0000)
committerMartin Kronbichler <kronbichler@lnm.mw.tum.de>
Wed, 8 Sep 2010 15:38:45 +0000 (15:38 +0000)
git-svn-id: https://svn.dealii.org/trunk@21888 0785d39b-7218-0410-832d-ea1e28bc413d

tests/bits/sparse_matrix_add_entries_01.cc [new file with mode: 0644]
tests/bits/sparse_matrix_add_entries_01/cmp/generic [new file with mode: 0644]
tests/bits/sparse_matrix_add_entries_02.cc [new file with mode: 0644]
tests/bits/sparse_matrix_add_entries_02/cmp/generic [new file with mode: 0644]
tests/bits/sparse_matrix_add_entries_03.cc [new file with mode: 0644]
tests/bits/sparse_matrix_add_entries_03/cmp/generic [new file with mode: 0644]

diff --git a/tests/bits/sparse_matrix_add_entries_01.cc b/tests/bits/sparse_matrix_add_entries_01.cc
new file mode 100644 (file)
index 0000000..8a90d59
--- /dev/null
@@ -0,0 +1,117 @@
+//----------------------------  sparse_matrix_entries_01.cc  -----------------
+//    $Id$
+//    Version: $Name$
+//
+//    Copyright (C) 2010 by the deal.II authors
+//
+//    This file is subject to QPL and may not be  distributed
+//    without copyright and license information. Please refer
+//    to the file deal.II/doc/license.html for the  text  and
+//    further information on this license.
+//
+//----------------------------  sparse_matrix_add_entries_01.cc  -------------
+
+
+// check adding elements into a matrix using
+// SparseMatrix::add(row, n_cols, col_indices, values, elide_zero_values,
+//                   col_indices_are_sorted)
+// need to filter out zeros, indices are sorted and zero values should
+// not be elided
+
+#include "../tests.h"
+#include <lac/sparse_matrix.h>
+#include <fstream>
+
+
+void test ()
+{
+                               // set up sparse matrix
+  SparsityPattern sp (5,5,3);
+  for (unsigned int i=0; i<5; ++i)
+    for (unsigned int j=0; j<5; ++j)
+      if ((i+2*j+1) % 3 == 0)
+        sp.add (i,j);
+  sp.compress ();
+
+  SparseMatrix<double> m(sp);
+
+                               // prepare structure with indices and values
+  std::vector<unsigned int> indices (m.m());
+  for (unsigned int i=0; i<m.m(); ++i)
+    indices[i] = i;
+  std::vector<double> values (5);
+
+                                   // try to add entries from the list. Zeros
+                                   // should be filtered out. list is sorted
+  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)
+         values[j] = i*j*.5+.5;
+       else
+         values[j] = 0;
+      m.add(i,m.m(),&indices[0], &values[0], false, true);
+    }
+
+                                   // then make sure we retrieve the same ones
+  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)
+       {
+         Assert (m(i,j) == i*j*.5+.5, ExcInternalError());
+       }
+      else
+       {
+         Assert (m.el(i,j) == 0, ExcInternalError());
+       }
+
+                               // try to add an invalid list of indices to
+                               // first and last row, should throw an
+                               // exception
+  deal_II_exceptions::disable_abort_on_exception();
+  for (unsigned int i=0; i<m.m(); ++i)
+    values[i] = 0.5*i - 1.5;
+  m.add(0,m.m(),&indices[0], &values[0], false, true);
+  m.add(m.m()-1,m.m(),&indices[0], &values[0], false, true);
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+int main ()
+{
+  std::ofstream logfile("sparse_matrix_add_entries_01/output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  try
+    {
+      test ();
+    }
+  catch (std::exception &exc)
+    {
+      deallog << std::endl << std::endl
+             << "----------------------------------------------------"
+             << std::endl;
+      deallog << "Exception on processing: " << std::endl
+             << exc.what() << std::endl
+             << "Aborting!" << std::endl
+             << "----------------------------------------------------"
+             << std::endl;
+
+      return 1;
+    }
+  catch (...)
+    {
+      deallog << std::endl << std::endl
+               << "----------------------------------------------------"
+               << std::endl;
+      deallog << "Unknown exception!" << std::endl
+               << "Aborting!" << std::endl
+               << "----------------------------------------------------"
+               << std::endl;
+      return 1;
+    };
+}
diff --git a/tests/bits/sparse_matrix_add_entries_01/cmp/generic b/tests/bits/sparse_matrix_add_entries_01/cmp/generic
new file mode 100644 (file)
index 0000000..a914d80
--- /dev/null
@@ -0,0 +1,5 @@
+
+DEAL::ExcInvalidIndex(row,col_indices[i])
+DEAL::ExcInvalidIndex(row,col_indices[i])
+DEAL::ExcInvalidIndex(row,col_indices[i])
+DEAL::OK
diff --git a/tests/bits/sparse_matrix_add_entries_02.cc b/tests/bits/sparse_matrix_add_entries_02.cc
new file mode 100644 (file)
index 0000000..9a01f44
--- /dev/null
@@ -0,0 +1,108 @@
+//----------------------------  sparse_matrix_entries_02.cc  -----------------
+//    $Id$
+//    Version: $Name$
+//
+//    Copyright (C) 2010 by the deal.II authors
+//
+//    This file is subject to QPL and may not be  distributed
+//    without copyright and license information. Please refer
+//    to the file deal.II/doc/license.html for the  text  and
+//    further information on this license.
+//
+//----------------------------  sparse_matrix_add_entries_02.cc  -------------
+
+
+// check adding elements into a matrix using
+// SparseMatrix::add(row, n_cols, col_indices, values, elide_zero_values,
+//                   col_indices_are_sorted)
+// need to filter out zeros, indices are sorted and zero values should
+// be elided
+
+#include "../tests.h"
+#include <lac/sparse_matrix.h>
+#include <fstream>
+
+
+void test ()
+{
+                               // set up sparse matrix
+  SparsityPattern sp (5,5,3);
+  for (unsigned int i=0; i<5; ++i)
+    for (unsigned int j=0; j<5; ++j)
+      if ((i+2*j+1) % 3 == 0)
+        sp.add (i,j);
+  sp.compress ();
+
+  SparseMatrix<double> m(sp);
+
+                               // prepare structure with indices and values
+  std::vector<unsigned int> indices (m.m());
+  for (unsigned int i=0; i<m.m(); ++i)
+    indices[i] = i;
+  std::vector<double> values (5);
+
+                                   // try to add entries from the list. Zeros
+                                   // should be filtered out. list is sorted
+  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)
+         values[j] = i*j*.5+.5;
+       else
+         values[j] = 0;
+      m.add(i,m.m(),&indices[0], &values[0], true, true);
+    }
+
+                                   // then make sure we retrieve the same ones
+  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)
+       {
+         Assert (m(i,j) == i*j*.5+.5, ExcInternalError());
+       }
+      else
+       {
+         Assert (m.el(i,j) == 0, ExcInternalError());
+       }
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+int main ()
+{
+  std::ofstream logfile("sparse_matrix_add_entries_02/output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  try
+    {
+      test ();
+    }
+  catch (std::exception &exc)
+    {
+      deallog << std::endl << std::endl
+             << "----------------------------------------------------"
+             << std::endl;
+      deallog << "Exception on processing: " << std::endl
+             << exc.what() << std::endl
+             << "Aborting!" << std::endl
+             << "----------------------------------------------------"
+             << std::endl;
+
+      return 1;
+    }
+  catch (...)
+    {
+      deallog << std::endl << std::endl
+               << "----------------------------------------------------"
+               << std::endl;
+      deallog << "Unknown exception!" << std::endl
+               << "Aborting!" << std::endl
+               << "----------------------------------------------------"
+               << std::endl;
+      return 1;
+    };
+}
diff --git a/tests/bits/sparse_matrix_add_entries_02/cmp/generic b/tests/bits/sparse_matrix_add_entries_02/cmp/generic
new file mode 100644 (file)
index 0000000..0fd8fc1
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::OK
diff --git a/tests/bits/sparse_matrix_add_entries_03.cc b/tests/bits/sparse_matrix_add_entries_03.cc
new file mode 100644 (file)
index 0000000..d0bd800
--- /dev/null
@@ -0,0 +1,109 @@
+//----------------------------  sparse_matrix_entries_03.cc  -----------------
+//    $Id$
+//    Version: $Name$
+//
+//    Copyright (C) 2010 by the deal.II authors
+//
+//    This file is subject to QPL and may not be  distributed
+//    without copyright and license information. Please refer
+//    to the file deal.II/doc/license.html for the  text  and
+//    further information on this license.
+//
+//----------------------------  sparse_matrix_add_entries_03.cc  -------------
+
+
+// check adding elements into a matrix using
+// SparseMatrix::add(row, n_cols, col_indices, values, elide_zero_values,
+//                   col_indices_are_sorted)
+// need to filter out zeros, indices are not sorted and zero values should
+// not be elided
+
+#include "../tests.h"
+#include <lac/sparse_matrix.h>
+#include <fstream>
+
+
+void test ()
+{
+                               // set up sparse matrix
+  SparsityPattern sp (5,5,3);
+  for (unsigned int i=0; i<5; ++i)
+    for (unsigned int j=0; j<5; ++j)
+      if ((i+2*j+1) % 3 == 0)
+        sp.add (i,j);
+  sp.compress ();
+
+  SparseMatrix<double> m(sp);
+
+                               // prepare structure with indices and values
+  std::vector<unsigned int> indices (m.m());
+  for (unsigned int i=0; i<m.m(); ++i)
+    indices[i] = m.m()-1-i;
+  std::vector<double> values (5);
+
+                                   // try to add entries from the list. Zeros
+                                   // need to be filtered out. list is not
+                                   // sorted
+  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)
+         values[m.m()-1-j] = i*j*.5+.5;
+       else
+         values[m.m()-1-j] = 0;
+      m.add(i,m.m(),&indices[0], &values[0], false, false);
+    }
+
+                                   // then make sure we retrieve the same ones
+  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)
+       {
+         Assert (m(i,j) == i*j*.5+.5, ExcInternalError());
+       }
+      else
+       {
+         Assert (m.el(i,j) == 0, ExcInternalError());
+       }
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+int main ()
+{
+  std::ofstream logfile("sparse_matrix_add_entries_03/output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  try
+    {
+      test ();
+    }
+  catch (std::exception &exc)
+    {
+      deallog << std::endl << std::endl
+             << "----------------------------------------------------"
+             << std::endl;
+      deallog << "Exception on processing: " << std::endl
+             << exc.what() << std::endl
+             << "Aborting!" << std::endl
+             << "----------------------------------------------------"
+             << std::endl;
+
+      return 1;
+    }
+  catch (...)
+    {
+      deallog << std::endl << std::endl
+               << "----------------------------------------------------"
+               << std::endl;
+      deallog << "Unknown exception!" << std::endl
+               << "Aborting!" << std::endl
+               << "----------------------------------------------------"
+               << std::endl;
+      return 1;
+    };
+}
diff --git a/tests/bits/sparse_matrix_add_entries_03/cmp/generic b/tests/bits/sparse_matrix_add_entries_03/cmp/generic
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.