]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Added some tests for Trilinos collective add and set operations.
authorkronbichler <kronbichler@0785d39b-7218-0410-832d-ea1e28bc413d>
Sat, 22 Nov 2008 14:30:58 +0000 (14:30 +0000)
committerkronbichler <kronbichler@0785d39b-7218-0410-832d-ea1e28bc413d>
Sat, 22 Nov 2008 14:30:58 +0000 (14:30 +0000)
git-svn-id: https://svn.dealii.org/trunk@17682 0785d39b-7218-0410-832d-ea1e28bc413d

tests/trilinos/block_sparse_matrix_add_01.cc [new file with mode: 0644]
tests/trilinos/block_sparse_matrix_set_01.cc [new file with mode: 0644]
tests/trilinos/sparse_matrix_add_01.cc [new file with mode: 0644]
tests/trilinos/sparse_matrix_add_02.cc [new file with mode: 0644]
tests/trilinos/sparse_matrix_set_01.cc [new file with mode: 0644]
tests/trilinos/sparse_matrix_set_02.cc [new file with mode: 0644]

diff --git a/tests/trilinos/block_sparse_matrix_add_01.cc b/tests/trilinos/block_sparse_matrix_add_01.cc
new file mode 100644 (file)
index 0000000..40ee8d5
--- /dev/null
@@ -0,0 +1,167 @@
+//-------------  trilinos_block_sparse_matrix_add_01.cc  -------------------
+//    $Id: 01.cc 17484 2008-11-06 10:21:30Z kronbichler $
+//    Version: $Name$ 
+//
+//    Copyright (C) 2004, 2005, 2008 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.
+//
+//-------------  trilinos_block_sparse_matrix_add_01.cc  -------------------
+
+
+// compare collective adding of elements in a trilinos matrix using
+// TrilinosWrappers::BlockSparseMatrix::add() and a FullMatrix<double> with
+// setting the same elements on an element-by-element level. Use the entries
+// as they would result from a vector-valued problem with a Laplace operator
+// in 1D in each component
+
+#include "../tests.h" 
+#include <base/utilities.h>
+#include <lac/trilinos_block_sparse_matrix.h>
+#include <lac/full_matrix.h>
+#include <fstream>
+#include <iostream>
+
+
+void test ()
+{
+                                   // first set a few entries one-by-one in
+                                   // a small matrix
+  const unsigned int block_size = 16;
+  TrilinosWrappers::SparseMatrix m_small (block_size,block_size,3);
+  for (unsigned int i=0; i<block_size; ++i)
+    for (unsigned int j=0; j<block_size; ++j)
+      if (std::fabs((double)i-j) < 2)
+       {
+         double value;
+         if (i == j) 
+           if (i>0 && i<m_small.m()-1)
+             value = 2.;
+           else 
+             value = 1.;
+         else
+           value = -1.;
+
+         m_small.set (i,j, value);
+       }
+  m_small.compress();
+
+                                  // Then build two matrices consisting of
+                                  // several copies of the small
+                                  // matrix. The matrix m2 will be filled
+                                  // later.
+  TrilinosWrappers::BlockSparseMatrix m, m2;
+  m.reinit(3,2);
+  m2.reinit(3,2);
+  for (unsigned int block_row = 0; block_row<m.n_block_rows(); ++block_row)
+    for (unsigned int block_col = 0; block_col<m.n_block_cols(); ++block_col)
+      m.block(block_row, block_col).reinit(m_small);
+  m.collect_sizes();
+
+                                  // Set the small matrix to zero and fill
+                                  // the second matrix with the sparsity
+                                  // pattern (but not the values)
+  m_small = 0;
+  for (unsigned int block_row = 0; block_row<m.n_block_rows(); ++block_row)
+    for (unsigned int block_col = 0; block_col<m.n_block_cols(); ++block_col)
+      m2.block(block_row, block_col).reinit(m_small);
+  m2.collect_sizes();
+
+                                  // now add the same elements from a full
+                                  // matrix
+  {
+    FullMatrix<double> full_matrix(2*m.n_block_rows(),2*m.n_block_cols());
+    for (unsigned int block_row = 0; block_row<m.n_block_rows(); ++block_row)
+      for (unsigned int block_col = 0; block_col<m.n_block_cols(); ++block_col)
+       {
+         full_matrix(block_row*2,block_col*2) = 1;
+         full_matrix(block_row*2+1,block_col*2+1) = 1.;
+         full_matrix(block_row*2,block_col*2+1) = -1;
+         full_matrix(block_row*2+1,block_col*2) = -1.;
+       }
+
+    std::vector<unsigned int> local_row_indices (2*m.n_block_rows());
+    std::vector<unsigned int> local_col_indices (2*m.n_block_cols());
+
+    for (unsigned int i=0; i<block_size-1; ++i)
+      {
+       for (unsigned int block_row = 0; block_row<m.n_block_rows(); ++block_row)
+         {
+           local_row_indices[2*block_row] = block_row*block_size + i;
+           local_row_indices[2*block_row+1] = block_row*block_size + i + 1;
+         }
+       for (unsigned int block_col = 0; block_col<m.n_block_cols(); ++block_col)
+         {
+           local_col_indices[2*block_col] = block_col*block_size + i;
+           local_col_indices[2*block_col+1] = block_col*block_size + i + 1;
+         }
+
+       m2.add (local_row_indices, local_col_indices, full_matrix);
+      }
+  }
+
+  m2.compress();
+
+                                  // subtract the matrix m from this one,
+                                  // we should get a zero matrix
+  double norm = 0;
+  for (unsigned int block_row = 0; block_row<m.n_block_rows(); ++block_row)
+    for (unsigned int block_col = 0; block_col<m.n_block_cols(); ++block_col)
+      {
+       m2.block(block_row,block_col).add(-1.0, m.block(block_row,block_col));
+
+                                  // calculate the Frobenius norm of the
+                                  // matrix in order to check whether all
+                                  // elements really are zero
+       norm += m2.block(block_row,block_col).frobenius_norm();
+      }
+  Assert (norm == 0, ExcInternalError());
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+int main (int argc,char **argv) 
+{
+  std::ofstream logfile("01/output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10); 
+
+  Utilities::System::MPI_InitFinalize mpi_initialization (argc, argv);
+
+  try
+    {
+      {
+        test ();
+      }
+    }
+  catch (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/block_sparse_matrix_set_01.cc b/tests/trilinos/block_sparse_matrix_set_01.cc
new file mode 100644 (file)
index 0000000..bec2931
--- /dev/null
@@ -0,0 +1,168 @@
+//-------------  trilinos_block_sparse_matrix_add_01.cc  -------------------
+//    $Id: 01.cc 17484 2008-11-06 10:21:30Z kronbichler $
+//    Version: $Name$ 
+//
+//    Copyright (C) 2004, 2005, 2008 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.
+//
+//-------------  trilinos_block_sparse_matrix_add_01.cc  -------------------
+
+
+// compare collective setting elements in a trilinos matrix using
+// TrilinosWrappers::BlockSparseMatrix::set() and a FullMatrix<double> with
+// setting the same elements on an entry-by-entry. Use the entries as they
+// would result from a vector-valued problem with a Laplace operator in 1D
+// in each component
+
+#include "../tests.h" 
+#include <base/utilities.h>
+#include <lac/trilinos_block_sparse_matrix.h>
+#include <lac/full_matrix.h>
+#include <fstream>
+#include <iostream>
+
+
+void test ()
+{
+                                   // first set a few entries one-by-one in
+                                   // a small matrix
+  const unsigned int block_size = 16;
+  TrilinosWrappers::SparseMatrix m_small (block_size,block_size,3);
+  for (unsigned int i=0; i<block_size; ++i)
+    for (unsigned int j=0; j<block_size; ++j)
+      if (std::fabs((double)i-j) < 2)
+       {
+         double value;
+         if (i == j) 
+           value = 1.;            // for this example, set the values to
+                                  // one and not some to two, since we do
+                                  // not insert elements into the sparsity
+                                  // pattern, but only set already present
+                                  // ones.
+         else
+           value = -1.;
+
+         m_small.set (i,j, value);
+       }
+  m_small.compress();
+
+                                  // Then build two matrices consisting of
+                                  // several copies of the small
+                                  // matrix. The matrix m2 will be filled
+                                  // later.
+  TrilinosWrappers::BlockSparseMatrix m, m2;
+  m.reinit(3,2);
+  m2.reinit(3,2);
+  for (unsigned int block_row = 0; block_row<m.n_block_rows(); ++block_row)
+    for (unsigned int block_col = 0; block_col<m.n_block_cols(); ++block_col)
+      m.block(block_row, block_col).reinit(m_small);
+  m.collect_sizes();
+
+                                  // Set the small matrix to zero and fill
+                                  // the second matrix with the sparsity
+                                  // pattern (but not the values)
+  m_small = 0;
+  for (unsigned int block_row = 0; block_row<m.n_block_rows(); ++block_row)
+    for (unsigned int block_col = 0; block_col<m.n_block_cols(); ++block_col)
+      m2.block(block_row, block_col).reinit(m_small);
+  m2.collect_sizes();
+
+                                  // now add the same elements from a full
+                                  // matrix
+  {
+    FullMatrix<double> full_matrix(2*m.n_block_rows(),2*m.n_block_cols());
+    for (unsigned int block_row = 0; block_row<m.n_block_rows(); ++block_row)
+      for (unsigned int block_col = 0; block_col<m.n_block_cols(); ++block_col)
+       {
+         full_matrix(block_row*2,block_col*2) = 1;
+         full_matrix(block_row*2+1,block_col*2+1) = 1.;
+         full_matrix(block_row*2,block_col*2+1) = -1;
+         full_matrix(block_row*2+1,block_col*2) = -1.;
+       }
+
+    std::vector<unsigned int> local_row_indices (2*m.n_block_rows());
+    std::vector<unsigned int> local_col_indices (2*m.n_block_cols());
+
+    for (unsigned int i=0; i<block_size-1; ++i)
+      {
+       for (unsigned int block_row = 0; block_row<m.n_block_rows(); ++block_row)
+         {
+           local_row_indices[2*block_row] = block_row*block_size + i;
+           local_row_indices[2*block_row+1] = block_row*block_size + i + 1;
+         }
+       for (unsigned int block_col = 0; block_col<m.n_block_cols(); ++block_col)
+         {
+           local_col_indices[2*block_col] = block_col*block_size + i;
+           local_col_indices[2*block_col+1] = block_col*block_size + i + 1;
+         }
+
+       m2.set (local_row_indices, local_col_indices, full_matrix);
+      }
+  }
+
+  m2.compress();
+
+                                  // subtract the matrix m from this one,
+                                  // we should get a zero matrix
+  double norm = 0;
+  for (unsigned int block_row = 0; block_row<m.n_block_rows(); ++block_row)
+    for (unsigned int block_col = 0; block_col<m.n_block_cols(); ++block_col)
+      {
+       m2.block(block_row,block_col).add(-1.0, m.block(block_row,block_col));
+
+                                  // calculate the Frobenius norm of the
+                                  // matrix in order to check whether all
+                                  // elements really are zero
+       norm += m2.block(block_row,block_col).frobenius_norm();
+      }
+  Assert (norm == 0, ExcInternalError());
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+int main (int argc,char **argv) 
+{
+  std::ofstream logfile("01/output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10); 
+
+  Utilities::System::MPI_InitFinalize mpi_initialization (argc, argv);
+
+  try
+    {
+      {
+        test ();
+      }
+    }
+  catch (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/sparse_matrix_add_01.cc b/tests/trilinos/sparse_matrix_add_01.cc
new file mode 100644 (file)
index 0000000..91a2a2f
--- /dev/null
@@ -0,0 +1,127 @@
+//-----------------  trilinos_sparse_matrix_add_01.cc  -------------------------
+//    $Id: 01.cc 17484 2008-11-06 10:21:30Z kronbichler $
+//    Version: $Name$ 
+//
+//    Copyright (C) 2004, 2005, 2008 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.
+//
+//-----------------  trilinos_sparse_matrix_add_01.cc  -------------------------
+
+
+// compare collective adding of elements in a trilinos matrix using
+// TrilinosWrappers::SparseMatrix::add() with element-wise setting
+
+#include "../tests.h" 
+#include <base/utilities.h>
+#include <lac/trilinos_sparse_matrix.h>
+#include <fstream>
+#include <iostream>
+
+
+void test (TrilinosWrappers::SparseMatrix &m)
+{
+  TrilinosWrappers::SparseMatrix m2(m.m(), m.n(), m.m()/3+1);
+
+                                   // 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);
+         m2.set (i,j, 0.);
+       }
+
+  m.compress ();
+  m2.compress();
+
+                                  // now add the same elements row-wise
+  {
+    std::vector<unsigned int> 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.add (i, col_indices, col_values);
+      }
+  }
+
+  m2.compress();
+
+                                  // 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();
+  Assert (norm == 0, ExcInternalError());
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+int main (int argc,char **argv) 
+{
+  std::ofstream logfile("01/output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10); 
+
+  Utilities::System::MPI_InitFinalize mpi_initialization (argc, argv);
+
+  try
+    {
+      {
+        TrilinosWrappers::SparseMatrix m (5,5,3);
+        test (m);
+      }
+    }
+  catch (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/sparse_matrix_add_02.cc b/tests/trilinos/sparse_matrix_add_02.cc
new file mode 100644 (file)
index 0000000..c274a6a
--- /dev/null
@@ -0,0 +1,127 @@
+//-----------------  trilinos_sparse_matrix_add_02.cc  -------------------------
+//    $Id: 01.cc 17484 2008-11-06 10:21:30Z kronbichler $
+//    Version: $Name$ 
+//
+//    Copyright (C) 2004, 2005, 2008 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.
+//
+//-----------------  trilinos_sparse_matrix_add_02.cc  -------------------------
+
+
+// compare collective adding of elements in a trilinos matrix using
+// TrilinosWrappers::SparseMatrix::add() 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 "../tests.h" 
+#include <base/utilities.h>
+#include <lac/trilinos_sparse_matrix.h>
+#include <lac/full_matrix.h>
+#include <fstream>
+#include <iostream>
+
+
+void test (TrilinosWrappers::SparseMatrix &m)
+{
+  TrilinosWrappers::SparseMatrix m2(m.m(), m.n(), 3);
+
+                                   // 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);
+         m2.set (i,j, 0.);
+       }
+
+  m.compress ();
+  m2.compress();
+
+                                  // now add the same elements from a full
+                                  // matrix
+  {
+    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<unsigned int> local_indices (2);
+
+    for (unsigned int i=0; i<m.m()-1; ++i)
+      {
+       local_indices[0] = i;
+       local_indices[1] = i+1;
+
+       m2.add (local_indices, local_indices, full_matrix);
+      }
+  }
+
+  m2.compress();
+
+                                  // 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();
+  Assert (norm == 0, ExcInternalError());
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+int main (int argc,char **argv) 
+{
+  std::ofstream logfile("01/output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10); 
+
+  Utilities::System::MPI_InitFinalize mpi_initialization (argc, argv);
+
+  try
+    {
+      {
+        TrilinosWrappers::SparseMatrix m (16,16,3);
+        test (m);
+      }
+    }
+  catch (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/sparse_matrix_set_01.cc b/tests/trilinos/sparse_matrix_set_01.cc
new file mode 100644 (file)
index 0000000..ef16d88
--- /dev/null
@@ -0,0 +1,123 @@
+//-----------------  trilinos_sparse_matrix_set_01.cc  -------------------------
+//    $Id: 01.cc 17484 2008-11-06 10:21:30Z kronbichler $
+//    Version: $Name$ 
+//
+//    Copyright (C) 2004, 2005, 2008 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.
+//
+//-----------------  trilinos_sparse_matrix_set_01.cc  -------------------------
+
+
+// compare collective setting of elements in a trilinos matrix using
+// TrilinosWrappers::SparseMatrix::set() with element-wise setting
+
+#include "../tests.h" 
+#include <base/utilities.h>
+#include <lac/trilinos_sparse_matrix.h>
+#include <fstream>
+#include <iostream>
+
+
+void test (TrilinosWrappers::SparseMatrix &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 ();
+
+  TrilinosWrappers::SparseMatrix m2(m.m(), m.n(), m.n()/3+1);
+
+                                  // now set the same elements row-wise
+   {
+    std::vector<unsigned int> 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();
+
+                                  // 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();
+  Assert (norm == 0, ExcInternalError());
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+int main (int argc,char **argv) 
+{
+  std::ofstream logfile("01/output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10); 
+
+  Utilities::System::MPI_InitFinalize mpi_initialization (argc, argv);
+
+  try
+    {
+      {
+        TrilinosWrappers::SparseMatrix m (5,5,3);
+        test (m);
+      }
+    }
+  catch (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/sparse_matrix_set_02.cc b/tests/trilinos/sparse_matrix_set_02.cc
new file mode 100644 (file)
index 0000000..81fc680
--- /dev/null
@@ -0,0 +1,124 @@
+//-----------------  trilinos_sparse_matrix_set_02.cc  -------------------------
+//    $Id: 01.cc 17484 2008-11-06 10:21:30Z kronbichler $
+//    Version: $Name$ 
+//
+//    Copyright (C) 2004, 2005, 2008 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.
+//
+//-----------------  trilinos_sparse_matrix_set_02.cc  -------------------------
+
+
+// compare collective setting of elements in a trilinos matrix using
+// TrilinosWrappers::SparseMatrix::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 "../tests.h" 
+#include <base/utilities.h>
+#include <lac/trilinos_sparse_matrix.h>
+#include <lac/full_matrix.h>
+#include <fstream>
+#include <iostream>
+
+
+void test (TrilinosWrappers::SparseMatrix &m)
+{
+  TrilinosWrappers::SparseMatrix m2(m.m(), m.n(), 3);
+
+                                   // 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 ();
+                                  // 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<unsigned int> 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();
+
+                                  // 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();
+  Assert (norm == 0, ExcInternalError());
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+int main (int argc,char **argv) 
+{
+  std::ofstream logfile("01/output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10); 
+
+  Utilities::System::MPI_InitFinalize mpi_initialization (argc, argv);
+
+  try
+    {
+      {
+        TrilinosWrappers::SparseMatrix m (16,16,3);
+        test (m);
+      }
+    }
+  catch (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;
+    };
+}

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.