]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Test ConstraintMatrix::distribute_local_to_global.
authorwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Mon, 15 Mar 2004 16:42:42 +0000 (16:42 +0000)
committerwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Mon, 15 Mar 2004 16:42:42 +0000 (16:42 +0000)
git-svn-id: https://svn.dealii.org/trunk@8743 0785d39b-7218-0410-832d-ea1e28bc413d

tests/bits/Makefile
tests/bits/dof_constraints_01.cc [new file with mode: 0644]
tests/bits/dof_constraints_02.cc [new file with mode: 0644]
tests/results/i686-pc-linux-gnu+gcc3.2/bits/dof_constraints_01.output [new file with mode: 0644]
tests/results/i686-pc-linux-gnu+gcc3.2/bits/dof_constraints_02.output [new file with mode: 0644]

index 449742f13d907dba598a5129aea794c41c18e58a..bc0db51e8de5e6f4782baafba9eb0ab5527c71fe 100644 (file)
@@ -52,7 +52,8 @@ tests_x = anna_? \
          solver_selector \
          deal_solver_* \
          vector_* \
-         subdomain_ids_*
+         subdomain_ids_* \
+         dof_constraints_*
 
 ifeq ($(USE_CONTRIB_PETSC),yes)
   tests_x += petsc_*
diff --git a/tests/bits/dof_constraints_01.cc b/tests/bits/dof_constraints_01.cc
new file mode 100644 (file)
index 0000000..b19a949
--- /dev/null
@@ -0,0 +1,166 @@
+//----------------------------  dof_constraints_01.cc  ---------------------------
+//    $Id$
+//    Version: $Name$ 
+//
+//    Copyright (C) 2004 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.
+//
+//----------------------------  dof_constraints_01.cc  ---------------------------
+
+
+// check DoFConstraints::distribute_local_to_global for matrices
+
+#include "../tests.h"
+#include <lac/sparsity_pattern.h>
+#include <lac/sparse_matrix.h>
+#include <lac/vector.h>
+#include <grid/tria.h>
+#include <grid/tria_accessor.h>
+#include <grid/tria_iterator.h>
+#include <grid/grid_tools.h>
+#include <grid/grid_generator.h>
+#include <dofs/dof_handler.h>
+#include <dofs/dof_accessor.h>
+#include <dofs/dof_tools.h>
+#include <dofs/dof_constraints.h>
+#include <fe/fe_q.h>
+#include <fstream>
+#include <iostream>
+
+
+template <int dim>
+void test ()
+{
+  deallog << dim << "D" << std::endl;
+  
+  Triangulation<dim> triangulation;
+  GridGenerator::hyper_cube (triangulation);
+
+                                   // refine the mesh in a random way so as to
+                                   // generate as many hanging node
+                                   // constraints as possible
+//  triangulation.refine_global (4-dim);
+  triangulation.refine_global (1);
+  for (unsigned int i=0; i<1; ++i)
+    {
+      typename Triangulation<dim>::active_cell_iterator
+        cell = triangulation.begin_active();
+      for (unsigned int index=0; cell != triangulation.end(); ++cell, ++index)
+        if (index % (3*dim) == 0)
+          cell->set_refine_flag();
+      triangulation.execute_coarsening_and_refinement ();
+    }
+  deallog << "Number of cells: " << triangulation.n_active_cells() << std::endl;
+  
+                                   // set up a DoFHandler and compute hanging
+                                   // node constraints
+  FE_Q<dim> fe(1);
+  DoFHandler<dim> dof_handler (triangulation);
+  dof_handler.distribute_dofs (fe);
+  deallog << "Number of dofs: " << dof_handler.n_dofs() << std::endl;
+
+  ConstraintMatrix constraints;
+  DoFTools::make_hanging_node_constraints (dof_handler, constraints);
+  constraints.close ();
+  deallog << "Number of constraints: " << constraints.n_constraints() << std::endl;
+
+                                   // then set up a sparsity pattern and two
+                                   // matrices on top of it
+  SparsityPattern sparsity (dof_handler.n_dofs(),
+                            dof_handler.n_dofs(),
+                            dof_handler.max_couplings_between_dofs());
+  DoFTools::make_sparsity_pattern (dof_handler, sparsity);
+  constraints.condense (sparsity);
+  SparseMatrix<double> A(sparsity), B(sparsity);
+
+                                   // then fill the two matrices by setting up
+                                   // bogus matrix entries and (1) writing
+                                   // them into the matrix and condensing away
+                                   // hanging node constraints later on, or
+                                   // (2) distributing them right away
+  std::vector<unsigned int> local_dofs (fe.dofs_per_cell);
+  FullMatrix<double> local_matrix (fe.dofs_per_cell, fe.dofs_per_cell);
+  for (typename DoFHandler<dim>::active_cell_iterator
+         cell = dof_handler.begin_active();
+       cell != dof_handler.end(); ++cell)
+    {
+      cell->get_dof_indices (local_dofs);
+      local_matrix.clear ();
+      for (unsigned int i=0; i<fe.dofs_per_cell; ++i)
+        for (unsigned int j=0; j<fe.dofs_per_cell; ++j)
+          local_matrix(i,j) = (i+1.)*(j+1.)*(local_dofs[i]+1.)*(local_dofs[j]+1.);
+
+                                       // copy local to global by ourselves
+      for (unsigned int i=0; i<fe.dofs_per_cell; ++i)
+        for (unsigned int j=0; j<fe.dofs_per_cell; ++j)
+          A.add (local_dofs[i], local_dofs[j], local_matrix(i,j));
+
+                                       // or let other functions do that
+      constraints.distribute_local_to_global (local_matrix, local_dofs, B);
+
+      break;
+    }
+
+                                   // now condense away constraints from A
+  constraints.condense (A);
+
+                                   // we haven't yet set the diagonal entries
+                                   // for constrained nodes. we can do so at
+                                   // will, since these values don't matter
+                                   // anyway
+  for (unsigned int i=0; i<dof_handler.n_dofs(); ++i)
+    if (constraints.is_constrained(i))
+      B.set(i,i,A(i,i));
+  
+                                   // now comes the check: we subtract B from
+                                   // A, and make sure that the result is zero
+  A.add_scaled (-1., B);
+  deallog << "|A|=" << A.frobenius_norm() << std::endl;
+  deallog << "|B|=" << B.frobenius_norm() << std::endl;
+  Assert (A.frobenius_norm() < 1e-12*B.frobenius_norm(),
+          ExcInternalError());
+}
+
+
+
+int main ()
+{
+  std::ofstream logfile("dof_constraints_01.output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+
+  try
+    {
+//       test<1> ();
+//       test<2> ();
+      test<3> ();
+    }
+  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/bits/dof_constraints_02.cc b/tests/bits/dof_constraints_02.cc
new file mode 100644 (file)
index 0000000..10e7039
--- /dev/null
@@ -0,0 +1,156 @@
+//----------------------------  dof_constraints_02.cc  ---------------------------
+//    $Id$
+//    Version: $Name$ 
+//
+//    Copyright (C) 2004 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.
+//
+//----------------------------  dof_constraints_02.cc  ---------------------------
+
+
+// check DoFConstraints::distribute_local_to_global for vectors
+
+#include "../tests.h"
+#include <lac/vector.h>
+#include <grid/tria.h>
+#include <grid/tria_accessor.h>
+#include <grid/tria_iterator.h>
+#include <grid/grid_tools.h>
+#include <grid/grid_generator.h>
+#include <dofs/dof_handler.h>
+#include <dofs/dof_accessor.h>
+#include <dofs/dof_tools.h>
+#include <dofs/dof_constraints.h>
+#include <fe/fe_q.h>
+#include <fstream>
+#include <iostream>
+
+
+template <int dim>
+void test ()
+{
+  deallog << dim << "D" << std::endl;
+  
+  Triangulation<dim> triangulation;
+  GridGenerator::hyper_cube (triangulation);
+
+                                   // refine the mesh in a random way so as to
+                                   // generate as many hanging node
+                                   // constraints as possible
+  triangulation.refine_global (4-dim);
+  for (unsigned int i=0; i<11-2*dim; ++i)
+    {
+      typename Triangulation<dim>::active_cell_iterator
+        cell = triangulation.begin_active();
+      for (unsigned int index=0; cell != triangulation.end(); ++cell, ++index)
+        if (index % (3*dim) == 0)
+          cell->set_refine_flag();
+      triangulation.execute_coarsening_and_refinement ();
+    }
+  deallog << "Number of cells: " << triangulation.n_active_cells()
+          << std::endl;
+  
+                                   // set up a DoFHandler and compute hanging
+                                   // node constraints
+  FE_Q<dim> fe(1);
+  DoFHandler<dim> dof_handler (triangulation);
+  dof_handler.distribute_dofs (fe);
+  deallog << "Number of dofs: " << dof_handler.n_dofs() << std::endl;
+
+  ConstraintMatrix constraints;
+  DoFTools::make_hanging_node_constraints (dof_handler, constraints);
+  constraints.close ();
+  deallog << "Number of constraints: " << constraints.n_constraints() << std::endl;
+
+                                   // then set up two vectors
+  Vector<double> A(dof_handler.n_dofs()), B(dof_handler.n_dofs());
+
+                                   // then fill the two vectors by setting up
+                                   // bogus matrix entries and (1) writing
+                                   // them into the vector and condensing away
+                                   // hanging node constraints later on, or
+                                   // (2) distributing them right away
+  std::vector<unsigned int> local_dofs (fe.dofs_per_cell);
+  Vector<double> local_vector (fe.dofs_per_cell);
+  for (typename DoFHandler<dim>::active_cell_iterator
+         cell = dof_handler.begin_active();
+       cell != dof_handler.end(); ++cell)
+    {
+      cell->get_dof_indices (local_dofs);
+      local_vector.clear ();
+      for (unsigned int i=0; i<fe.dofs_per_cell; ++i)
+        local_vector(i) = (i+1.)*(local_dofs[i]+1.);
+
+                                       // copy local to global by ourselves
+      for (unsigned int i=0; i<fe.dofs_per_cell; ++i)
+        A(local_dofs[i]) += local_vector(i);
+
+                                       // or let other functions do that
+      constraints.distribute_local_to_global (local_vector, local_dofs, B);
+
+      break;
+    }
+
+                                   // now condense away constraints from A
+  constraints.condense (A);
+
+                                   // we haven't yet set the entries
+                                   // for constrained nodes. we can do so at
+                                   // will, since these values don't matter
+                                   // anyway
+  for (unsigned int i=0; i<dof_handler.n_dofs(); ++i)
+    if (constraints.is_constrained(i))
+      B(i) = A(i);
+  
+                                   // now comes the check: we subtract B from
+                                   // A, and make sure that the result is zero
+  A -= B;
+  deallog << "|A|=" << A.l2_norm() << std::endl;
+  deallog << "|B|=" << B.l2_norm() << std::endl;
+  Assert (A.l2_norm() < 1e-12*B.l2_norm(),
+          ExcInternalError());
+}
+
+
+
+int main ()
+{
+  std::ofstream logfile("dof_constraints_02.output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+
+  try
+    {
+      test<1> ();
+      test<2> ();
+      test<3> ();
+    }
+  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/results/i686-pc-linux-gnu+gcc3.2/bits/dof_constraints_01.output b/tests/results/i686-pc-linux-gnu+gcc3.2/bits/dof_constraints_01.output
new file mode 100644 (file)
index 0000000..5bc3146
--- /dev/null
@@ -0,0 +1,7 @@
+
+DEAL::3D
+DEAL::Number of cells: 15
+DEAL::Number of dofs: 46
+DEAL::Number of constraints: 12
+DEAL::|A|=0.00000
+DEAL::|B|=8796.84
diff --git a/tests/results/i686-pc-linux-gnu+gcc3.2/bits/dof_constraints_02.output b/tests/results/i686-pc-linux-gnu+gcc3.2/bits/dof_constraints_02.output
new file mode 100644 (file)
index 0000000..8151a75
--- /dev/null
@@ -0,0 +1,19 @@
+
+DEAL::1D
+DEAL::Number of cells: 115
+DEAL::Number of dofs: 116
+DEAL::Number of constraints: 0
+DEAL::|A|=0.00000
+DEAL::|B|=4.12311
+DEAL::2D
+DEAL::Number of cells: 1009
+DEAL::Number of dofs: 1301
+DEAL::Number of constraints: 459
+DEAL::|A|=0.00000
+DEAL::|B|=18.8149
+DEAL::3D
+DEAL::Number of cells: 1023
+DEAL::Number of dofs: 1745
+DEAL::Number of constraints: 881
+DEAL::|A|=0.00000
+DEAL::|B|=93.6590

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.