libMesh
dof_map_test.C
Go to the documentation of this file.
1 #include <libmesh/equation_systems.h>
2 #include <libmesh/mesh.h>
3 #include <libmesh/mesh_generation.h>
4 #include <libmesh/elem.h>
5 #include <libmesh/dof_map.h>
6 
7 #include "test_comm.h"
8 #include "libmesh_cppunit.h"
9 
10 
11 using namespace libMesh;
12 
13 #ifdef LIBMESH_ENABLE_CONSTRAINTS
14 // This class is used by testConstraintLoopDetection
16 {
17 private:
18 
20 
21 public:
22 
23  MyConstraint( System & sys ) : Constraint(), _sys(sys) {}
24 
25  virtual ~MyConstraint() {}
26 
27  void constrain()
28  {
29  {
30  const dof_id_type constrained_dof_index = 0;
31  DofConstraintRow constraint_row;
32  constraint_row[1] = 1.0;
33  _sys.get_dof_map().add_constraint_row( constrained_dof_index, constraint_row, 0., true);
34  }
35  {
36  const dof_id_type constrained_dof_index = 1;
37  DofConstraintRow constraint_row;
38  constraint_row[0] = 1.0;
39  _sys.get_dof_map().add_constraint_row( constrained_dof_index, constraint_row, 0., true);
40  }
41  }
42 };
43 #endif
44 
45 
46 class DofMapTest : public CppUnit::TestCase {
47 public:
48  CPPUNIT_TEST_SUITE( DofMapTest );
49 
50  CPPUNIT_TEST( testDofOwnerOnEdge3 );
51 #if LIBMESH_DIM > 1
52  CPPUNIT_TEST( testDofOwnerOnQuad9 );
53  CPPUNIT_TEST( testDofOwnerOnTri6 );
54 #endif
55 #if LIBMESH_DIM > 2
56  CPPUNIT_TEST( testDofOwnerOnHex27 );
57 #endif
58 
59 #if defined(LIBMESH_ENABLE_CONSTRAINTS) && defined(LIBMESH_ENABLE_EXCEPTIONS) && LIBMESH_DIM > 1
60  CPPUNIT_TEST( testConstraintLoopDetection );
61 #endif
62 
63  CPPUNIT_TEST_SUITE_END();
64 
65 private:
66 
67 public:
68  void setUp()
69  {}
70 
71  void tearDown()
72  {}
73 
74  void testDofOwner(const ElemType elem_type)
75  {
77 
79  System &sys = es.add_system<System> ("SimpleSystem");
80  sys.add_variable("u", THIRD, HIERARCHIC);
81 
82  const unsigned int n_elem_per_side = 3;
83  const std::unique_ptr<Elem> test_elem = Elem::build(elem_type);
84  const unsigned int ymax = test_elem->dim() > 1;
85  const unsigned int zmax = test_elem->dim() > 2;
86  const unsigned int ny = ymax * n_elem_per_side;
87  const unsigned int nz = zmax * n_elem_per_side;
88 
90  n_elem_per_side,
91  ny,
92  nz,
93  0., 1.,
94  0., ymax,
95  0., zmax,
96  elem_type);
97 
98  es.init();
99 
100  DofMap & dof_map = sys.get_dof_map();
101  for (dof_id_type id = 0; id != dof_map.n_dofs(); ++id)
102  {
103  const processor_id_type pid = dof_map.dof_owner(id);
104  CPPUNIT_ASSERT(dof_map.first_dof(pid) <= id);
105  CPPUNIT_ASSERT(id < dof_map.end_dof(pid));
106  }
107  }
108 
109 
110 
111  void testDofOwnerOnEdge3() { testDofOwner(EDGE3); }
112  void testDofOwnerOnQuad9() { testDofOwner(QUAD9); }
113  void testDofOwnerOnTri6() { testDofOwner(TRI6); }
114  void testDofOwnerOnHex27() { testDofOwner(HEX27); }
115 
116 #if defined(LIBMESH_ENABLE_CONSTRAINTS) && defined(LIBMESH_ENABLE_EXCEPTIONS)
118  {
120 
121  EquationSystems es(mesh);
122  System & sys = es.add_system<System> ("SimpleSystem");
123  sys.add_variable("u", FIRST);
124 
125  MyConstraint my_constraint(sys);
126  sys.attach_constraint_object(my_constraint);
127 
128  MeshTools::Generation::build_square (mesh,4,4,-1., 1.,-1., 1., QUAD4);
129 
130  // Tell the dof_map to check for constraint loops
131  DofMap & dof_map = sys.get_dof_map();
132  dof_map.set_error_on_constraint_loop(true);
133 
134  CPPUNIT_ASSERT_THROW_MESSAGE("Constraint loop not detected", es.init(), libMesh::LogicError);
135  }
136 #endif
137 
138 };
139 
libMesh::System
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:100
libMesh::dof_id_type
uint8_t dof_id_type
Definition: id_types.h:67
MyConstraint::constrain
void constrain()
Constraint function.
Definition: dof_map_test.C:27
libMesh::Mesh
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition: mesh.h:50
libMesh::EquationSystems::add_system
virtual System & add_system(const std::string &system_type, const std::string &name)
Add the system of type system_type named name to the systems array.
Definition: equation_systems.C:345
libMesh::DofMap::n_dofs
dof_id_type n_dofs() const
Definition: dof_map.h:625
MyConstraint::MyConstraint
MyConstraint(System &sys)
Definition: dof_map_test.C:23
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::MeshTools::Generation::build_cube
void build_cube(UnstructuredMesh &mesh, const unsigned int nx=0, const unsigned int ny=0, const unsigned int nz=0, const Real xmin=0., const Real xmax=1., const Real ymin=0., const Real ymax=1., const Real zmin=0., const Real zmax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
Builds a (elements) cube.
Definition: mesh_generation.C:298
libMesh::LogicError
A class to represent the internal "this should never happen" errors, to be thrown by "libmesh_error()...
Definition: libmesh_exceptions.h:38
MyConstraint
Definition: dof_map_test.C:15
mesh
MeshBase & mesh
Definition: mesh_communication.C:1257
DofMapTest::tearDown
void tearDown()
Definition: dof_map_test.C:71
libMesh::DofMap::first_dof
dof_id_type first_dof(const processor_id_type proc) const
Definition: dof_map.h:650
DofMapTest::setUp
void setUp()
Definition: dof_map_test.C:68
libMesh::System::attach_constraint_object
void attach_constraint_object(Constraint &constrain)
Register a user object for imposing constraints.
Definition: system.C:1809
DofMapTest::testDofOwnerOnHex27
void testDofOwnerOnHex27()
Definition: dof_map_test.C:114
libMesh::DofMap::set_error_on_constraint_loop
void set_error_on_constraint_loop(bool error_on_constraint_loop)
Definition: dof_map.C:250
libMesh::MeshTools::Generation::build_square
void build_square(UnstructuredMesh &mesh, const unsigned int nx, const unsigned int ny, const Real xmin=0., const Real xmax=1., const Real ymin=0., const Real ymax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
A specialized build_cube() for 2D meshes.
Definition: mesh_generation.C:1501
libMesh::HEX27
Definition: enum_elem_type.h:49
libMesh::System::add_variable
unsigned int add_variable(const std::string &var, const FEType &type, const std::set< subdomain_id_type > *const active_subdomains=nullptr)
Adds the variable var to the list of variables for this system.
Definition: system.C:1069
libMesh::QUAD4
Definition: enum_elem_type.h:41
TestCommWorld
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:111
libMesh::EquationSystems::init
virtual void init()
Initialize all the systems.
Definition: equation_systems.C:96
libMesh::processor_id_type
uint8_t processor_id_type
Definition: id_types.h:104
DofMapTest::testDofOwnerOnQuad9
void testDofOwnerOnQuad9()
Definition: dof_map_test.C:112
libMesh::HIERARCHIC
Definition: enum_fe_family.h:37
libMesh::EquationSystems
This is the EquationSystems class.
Definition: equation_systems.h:74
libMesh::System::Constraint
Abstract base class to be used for system constraints.
Definition: system.h:167
libMesh::TRI6
Definition: enum_elem_type.h:40
DofMapTest::testConstraintLoopDetection
void testConstraintLoopDetection()
Definition: dof_map_test.C:117
DofMapTest
Definition: dof_map_test.C:46
libMesh::DofMap
This class handles the numbering of degrees of freedom on a mesh.
Definition: dof_map.h:176
libmesh_cppunit.h
libMesh::EDGE3
Definition: enum_elem_type.h:36
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(DofMapTest)
DofMapTest::testDofOwnerOnEdge3
void testDofOwnerOnEdge3()
Definition: dof_map_test.C:111
libMesh::THIRD
Definition: enum_order.h:44
libMesh::QUAD9
Definition: enum_elem_type.h:43
libMesh::System::get_dof_map
const DofMap & get_dof_map() const
Definition: system.h:2099
DofMapTest::testDofOwner
void testDofOwner(const ElemType elem_type)
Definition: dof_map_test.C:74
DofMapTest::testDofOwnerOnTri6
void testDofOwnerOnTri6()
Definition: dof_map_test.C:113
test_comm.h
MyConstraint::_sys
System & _sys
Definition: dof_map_test.C:19
libMesh::DofMap::add_constraint_row
void add_constraint_row(const dof_id_type dof_number, const DofConstraintRow &constraint_row, const Number constraint_rhs, const bool forbid_constraint_overwrite)
Adds a copy of the user-defined row to the constraint matrix, using an inhomogeneous right-hand-side ...
Definition: dof_map_constraints.C:1364
libMesh::Elem::build
static std::unique_ptr< Elem > build(const ElemType type, Elem *p=nullptr)
Definition: elem.C:246
libMesh::DofMap::dof_owner
processor_id_type dof_owner(const dof_id_type dof) const
Definition: dof_map.h:701
MyConstraint::~MyConstraint
virtual ~MyConstraint()
Definition: dof_map_test.C:25
libMesh::DofConstraintRow
std::map< dof_id_type, Real, std::less< dof_id_type >, Threads::scalable_allocator< std::pair< const dof_id_type, Real > > > DofConstraintRow
A row of the Dof constraint matrix.
Definition: dof_map.h:97
libMesh::FIRST
Definition: enum_order.h:42
libMesh::ElemType
ElemType
Defines an enum for geometric element types.
Definition: enum_elem_type.h:33
libMesh::DofMap::end_dof
dof_id_type end_dof(const processor_id_type proc) const
Definition: dof_map.h:692