]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Allow ConstraintMatrix::distribute_local_to_global with ChunkSparseMatrix.
authorkronbichler <kronbichler@0785d39b-7218-0410-832d-ea1e28bc413d>
Thu, 29 Nov 2012 16:05:54 +0000 (16:05 +0000)
committerkronbichler <kronbichler@0785d39b-7218-0410-832d-ea1e28bc413d>
Thu, 29 Nov 2012 16:05:54 +0000 (16:05 +0000)
git-svn-id: https://svn.dealii.org/trunk@27703 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/include/deal.II/lac/chunk_sparse_matrix.h
deal.II/source/lac/constraint_matrix.cc
tests/deal.II/constraints_local_to_global_chunk.cc [new file with mode: 0644]
tests/deal.II/constraints_local_to_global_chunk/cmp/generic [new file with mode: 0644]

index 4784340b14a3373f0fb942328a4b85087f27b812..7f47346c8e68f0936b38e15a935a6ad0849be87c 100644 (file)
@@ -378,6 +378,31 @@ public:
             const unsigned int j,
             const number value);
 
+  /**
+   * Add an array of values given by
+   * <tt>values</tt> in the given
+   * global matrix row at columns
+   * specified by col_indices in the
+   * sparse matrix.
+   *
+   * The optional parameter
+   * <tt>elide_zero_values</tt> can be
+   * used to specify whether zero
+   * values should be added anyway or
+   * these should be filtered away and
+   * only non-zero data is added. The
+   * default value is <tt>true</tt>,
+   * i.e., zero values won't be added
+   * into the matrix.
+   */
+  template <typename number2>
+  void add (const unsigned int  row,
+            const unsigned int  n_cols,
+            const unsigned int *col_indices,
+            const number2      *values,
+            const bool          elide_zero_values = true,
+            const bool          col_indices_are_sorted = false);
+
   /**
    * Multiply the entire matrix by a
    * fixed factor.
@@ -1277,13 +1302,31 @@ void ChunkSparseMatrix<number>::add (const unsigned int i,
 
   Assert (cols != 0, ExcNotInitialized());
 
-  const unsigned int index = compute_location(i,j);
-  Assert ((index != ChunkSparsityPattern::invalid_entry) ||
-          (value == 0.),
-          ExcInvalidIndex(i,j));
-
   if (value != 0.)
-    val[index] += value;
+    {
+      const unsigned int index = compute_location(i,j);
+      Assert ((index != ChunkSparsityPattern::invalid_entry),
+              ExcInvalidIndex(i,j));
+
+      val[index] += value;
+    }
+}
+
+
+
+template <typename number>
+template <typename number2>
+inline
+void ChunkSparseMatrix<number>::add (const unsigned int row,
+                                     const unsigned int n_cols,
+                                     const unsigned int *col_indices,
+                                     const number2      *values,
+                                     const bool          elide_zero_values,
+                                     const bool          col_indices_are_sorted)
+{
+  // TODO: could be done more efficiently...
+  for (unsigned int col=0; col<n_cols; ++col)
+    add(row, col_indices[col], static_cast<number>(values[col]));
 }
 
 
index b652bbe942e5b39353f79438772cf3c2677652a3..aa0e4f72fa6b909dac681f570ed5452b1159f1cc 100644 (file)
@@ -21,6 +21,7 @@
 #include <deal.II/lac/block_vector.h>
 #include <deal.II/lac/block_sparse_matrix.h>
 #include <deal.II/lac/sparse_matrix_ez.h>
+#include <deal.II/lac/chunk_sparse_matrix.h>
 #include <deal.II/lac/block_sparse_matrix_ez.h>
 #include <deal.II/lac/parallel_vector.h>
 #include <deal.II/lac/parallel_block_vector.h>
@@ -2373,7 +2374,10 @@ BLOCK_MATRIX_VECTOR_FUNCTIONS(BlockSparseMatrix<float>,  BlockVector<double>);
 
 MATRIX_FUNCTIONS(SparseMatrixEZ<double>);
 MATRIX_FUNCTIONS(SparseMatrixEZ<float>);
+MATRIX_FUNCTIONS(ChunkSparseMatrix<double>);
+MATRIX_FUNCTIONS(ChunkSparseMatrix<float>);
 MATRIX_VECTOR_FUNCTIONS(SparseMatrixEZ<float>,  Vector<float>);
+MATRIX_VECTOR_FUNCTIONS(ChunkSparseMatrix<float>, Vector<float>);
 
 // BLOCK_MATRIX_FUNCTIONS(BlockSparseMatrixEZ<double>);
 // BLOCK_MATRIX_VECTOR_FUNCTIONS(BlockSparseMatrixEZ<float>,  Vector<float>);
diff --git a/tests/deal.II/constraints_local_to_global_chunk.cc b/tests/deal.II/constraints_local_to_global_chunk.cc
new file mode 100644 (file)
index 0000000..5638055
--- /dev/null
@@ -0,0 +1,125 @@
+//------------------  constraints_local_to_global_chunk.cc  ------------------
+//    $Id$
+//    Version: $Name$ 
+//
+//    Copyright (C) 2012 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.
+//
+//------------------  constraints_local_to_global_chunk.cc  ------------------
+
+
+// this function tests the correctness of the implementation of
+// ConstraintMatrix::distribute_local_to_global for ChunkSparseMatrix by
+// comparing the results with a sparse matrix. As a test case, we use a square
+// mesh that is refined once globally and then the first cell is refined
+// adaptively.
+
+#include "../tests.h"
+
+#include <deal.II/base/function.h>
+#include <deal.II/base/logstream.h>
+#include <deal.II/lac/full_matrix.h>
+#include <deal.II/lac/chunk_sparse_matrix.h>
+#include <deal.II/lac/sparse_matrix.h>
+#include <deal.II/grid/tria.h>
+#include <deal.II/grid/grid_generator.h>
+#include <deal.II/grid/tria_accessor.h>
+#include <deal.II/grid/grid_refinement.h>
+#include <deal.II/dofs/dof_accessor.h>
+#include <deal.II/dofs/dof_tools.h>
+#include <deal.II/lac/constraint_matrix.h>
+#include <deal.II/fe/fe_q.h>
+#include <deal.II/numerics/vector_tools.h>
+#include <deal.II/lac/compressed_simple_sparsity_pattern.h>
+
+#include <fstream>
+#include <iostream>
+#include <complex>
+
+std::ofstream logfile("constraints_local_to_global_chunk/output");
+
+template <int dim>
+void test (unsigned int chunk_size)
+{
+  Triangulation<dim> tria;
+  GridGenerator::hyper_cube (tria);
+  tria.begin()->face(0)->set_boundary_indicator(1);
+  tria.refine_global(1);
+  tria.begin_active()->set_refine_flag();
+  tria.execute_coarsening_and_refinement();
+
+  FE_Q<dim> fe (1);
+  DoFHandler<dim> dof (tria);
+  dof.distribute_dofs(fe);
+
+  ConstraintMatrix constraints;
+  DoFTools::make_hanging_node_constraints (dof, constraints);
+  VectorTools::interpolate_boundary_values (dof, 1, ZeroFunction<dim>(),
+                                           constraints);
+  constraints.close();
+
+  SparsityPattern sparsity;
+  ChunkSparsityPattern chunk_sparsity;
+  {
+    CompressedSimpleSparsityPattern csp (dof.n_dofs(), dof.n_dofs());
+    DoFTools::make_sparsity_pattern (dof, csp, constraints, false);
+    sparsity.copy_from (csp);
+    chunk_sparsity.copy_from (csp, chunk_size);
+  }
+  SparseMatrix<double> sparse (sparsity);
+  ChunkSparseMatrix<double> chunk_sparse (chunk_sparsity);
+
+  FullMatrix<double> local_mat (fe.dofs_per_cell, fe.dofs_per_cell);
+  std::vector<unsigned int> local_dof_indices (fe.dofs_per_cell);
+
+                               // loop over cells, fill local matrix with
+                               // random values, insert both into sparse and
+                               // full matrix. Make some random entries equal
+                               // to zero
+  typename DoFHandler<dim>::active_cell_iterator
+    cell = dof.begin_active(), endc = dof.end();
+  unsigned int counter = 0;
+  for ( ; cell != endc; ++cell)
+    {
+      for (unsigned int i=0; i<fe.dofs_per_cell; ++i)
+       for (unsigned int j=0; j<fe.dofs_per_cell; ++j, ++counter)
+         if (counter % 42 == 0)
+           local_mat(i,j) = 0;
+         else
+           local_mat (i,j) = (double)rand() / RAND_MAX;
+      cell->get_dof_indices (local_dof_indices);
+      constraints.distribute_local_to_global (local_mat, local_dof_indices,
+                                             sparse);
+      constraints.distribute_local_to_global (local_mat, local_dof_indices,
+                                             chunk_sparse);
+    }
+
+                               // now check that the entries are indeed the
+                               // same
+  double frobenius = 0.;
+  for (unsigned int i=0; i<sparse.m(); ++i)
+    for (unsigned int j=0; j<sparse.n(); ++j)
+      frobenius += numbers::NumberTraits<double>::abs_square(sparse.el(i,j) -
+                                                             chunk_sparse.el(i,j));
+  deallog << "Difference between chunk and sparse matrix: "
+         << std::sqrt(frobenius) << std::endl;
+}
+
+
+int main () 
+{
+  deallog << std::setprecision (2);
+  logfile << std::setprecision (2);
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-14);
+
+  test<2>(1);
+  test<2>(2);
+  test<2>(5);
+}
+
diff --git a/tests/deal.II/constraints_local_to_global_chunk/cmp/generic b/tests/deal.II/constraints_local_to_global_chunk/cmp/generic
new file mode 100644 (file)
index 0000000..52403f3
--- /dev/null
@@ -0,0 +1,4 @@
+
+DEAL::Difference between chunk and sparse matrix: 0
+DEAL::Difference between chunk and sparse matrix: 0
+DEAL::Difference between chunk and sparse matrix: 0

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.