libMesh
equation_systems_test.C
Go to the documentation of this file.
1 #include <libmesh/dof_map.h>
2 #include <libmesh/elem.h>
3 #include <libmesh/equation_systems.h>
4 #include <libmesh/ghost_point_neighbors.h>
5 #include <libmesh/mesh.h>
6 #include <libmesh/mesh_generation.h>
7 #include <libmesh/mesh_refinement.h>
8 #include <libmesh/remote_elem.h>
9 #include <libmesh/replicated_mesh.h>
10 #include <libmesh/node_elem.h>
11 
12 #include "test_comm.h"
13 #include "libmesh_cppunit.h"
14 
15 
16 using namespace libMesh;
17 
18 // Anonymous namespace to avoid linker conflicts
19 namespace {
20 
21 Number bilinear_test (const Point& p,
22  const Parameters&,
23  const std::string&,
24  const std::string&)
25 {
26  const Real & x = p(0);
27  const Real & y = p(1);
28 
29  return 4*x*y - 3*x + 2*y - 1;
30 }
31 
32 }
33 
34 class EquationSystemsTest : public CppUnit::TestCase {
35 public:
36  CPPUNIT_TEST_SUITE( EquationSystemsTest );
37 
38  CPPUNIT_TEST( testConstruction );
39  CPPUNIT_TEST( testAddSystem );
40  CPPUNIT_TEST( testInit );
41  CPPUNIT_TEST( testPostInitAddSystem );
42  CPPUNIT_TEST( testPostInitAddElem );
43  CPPUNIT_TEST( testReinitWithNodeElem );
44 #if LIBMESH_DIM > 1
45  CPPUNIT_TEST( testRefineThenReinitPreserveFlags );
46 #ifdef LIBMESH_ENABLE_AMR // needs project_solution, even for reordering
47  CPPUNIT_TEST( testRepartitionThenReinit );
48 #endif
49 #endif
50  CPPUNIT_TEST( testDisableDefaultGhosting );
51 
52  CPPUNIT_TEST_SUITE_END();
53 
54 private:
55 
56 public:
57  void setUp()
58  {}
59 
60  void tearDown()
61  {}
62 
63 
64 
66  {
69  }
70 
72  {
75  /*System &sys = */es.add_system<System> ("SimpleSystem");
76  }
77 
78  void testInit()
79  {
82  /*System &sys = */es.add_system<System> ("SimpleSystem");
84  es.init();
85  }
86 
88  {
92  /*System &sys1 = */es.add_system<System> ("SimpleSystem");
93  es.init();
94  /*System &sys2 = */es.add_system<System> ("SecondSystem");
95  es.reinit();
96  }
97 
99  {
102  EquationSystems es(mesh);
103  System &sys1 = es.add_system<System> ("SimpleSystem");
104  sys1.add_variable("u1", FIRST);
105  es.init();
106  System &sys2 = es.add_system<System> ("SecondSystem");
107  sys2.add_variable("u2", FIRST);
108  es.reinit();
109  }
110 
112  {
114 
115  EquationSystems es(mesh);
116  System &sys = es.add_system<System> ("SimpleSystem");
117  sys.add_variable("u", FIRST);
118 
120  es.init();
121 
122  Elem* e = Elem::build(EDGE2).release();
123  e->set_id(mesh.max_elem_id());
124  e->processor_id() = 0;
125  e->set_node(0) = mesh.node_ptr(2);
126  e->set_node(1) = mesh.node_ptr(8);
127  mesh.add_elem(e);
129 
130  es.reinit();
131  }
132 
134  {
136 
138  Elem* node_elem = mesh.add_elem (new NodeElem);
139  node_elem->set_node(0) = mesh.node_ptr(0);
141 
142  EquationSystems es(mesh);
143  System &sys = es.add_system<System> ("SimpleSystem");
144  sys.add_variable("u", CONSTANT, MONOMIAL);
145  es.init();
146  es.reinit();
147  }
148 
150  {
151  // This test requires AMR support since it sets refinement flags.
152 #ifdef LIBMESH_ENABLE_AMR
154  mesh.allow_renumbering(false);
155  EquationSystems es(mesh);
156  System & sys = es.add_system<System> ("SimpleSystem");
157  sys.add_variable("u", FIRST);
159  es.init();
160 
161  Elem * to_refine = mesh.query_elem_ptr(0);
162  if (to_refine)
163  to_refine->set_refinement_flag(Elem::REFINE);
164 
165  MeshRefinement mr(mesh);
166  mr.refine_elements();
168  es.reinit();
169 
170  if (mesh.query_elem_ptr(1))
171  CPPUNIT_ASSERT( mesh.elem_ptr(1)->active() );
172 
173  const Elem * elem = mesh.query_elem_ptr(0);
174  if (elem)
175  {
176  CPPUNIT_ASSERT_EQUAL( Elem::INACTIVE,elem->refinement_flag() );
177 
178  for (unsigned int c=0; c<elem->n_children(); c++)
179  if (elem->child_ptr(c) != remote_elem)
180  CPPUNIT_ASSERT_EQUAL(Elem::JUST_REFINED,
181  elem->child_ptr(c)->refinement_flag());
182  }
183 #endif
184  }
185 
186 
187 
189  {
191  mesh.allow_renumbering(false);
192  EquationSystems es(mesh);
193  System & sys = es.add_system<System> ("SimpleSystem");
194  sys.add_variable("u", FIRST);
196  es.init();
197  sys.project_solution(bilinear_test, NULL, es.parameters);
198 
199  // Force (in parallel) a different partitioning - we'll simply put
200  // everything on rank 0, which hopefully is not what our default
201  // partitioner did!
202  mesh.partition(1);
203 
204  // Make sure the solution is still intact after reinit
205  es.reinit();
206 
207  for (Real x = 0.1; x < 1; x += 0.2)
208  for (Real y = 0.1; y < 1; y += 0.2)
209  {
210  Point p(x,y);
211  LIBMESH_ASSERT_FP_EQUAL(libmesh_real(sys.point_value(0,p)),
212  libmesh_real(bilinear_test(p,es.parameters,"","")),
214  }
215  }
216 
218  {
220  EquationSystems es(mesh);
221 
222  auto n_ghosts = [&mesh]() {
225  };
226 
227  auto n_evaluables = [](System &sys) {
228  return int(std::distance(sys.get_dof_map().algebraic_ghosting_functors_begin(),
229  sys.get_dof_map().algebraic_ghosting_functors_end()));
230  };
231 
232  auto n_couplings = [](System &sys) {
233  return int(std::distance(sys.get_dof_map().coupling_functors_begin(),
234  sys.get_dof_map().coupling_functors_end()));
235  };
236 
237  // One default ghosting functor on the mesh
238  CPPUNIT_ASSERT_EQUAL(n_ghosts(), 1);
239 
240  // Add another functor, making two
241  auto gpn = std::make_shared<GhostPointNeighbors>(mesh);
243  CPPUNIT_ASSERT_EQUAL(n_ghosts(), 2);
244 
245  // Remove the default, leaving just the user functor
246  es.enable_default_ghosting(false);
247  CPPUNIT_ASSERT_EQUAL(n_ghosts(), 1);
248 
249  // Which can be removed too
251  CPPUNIT_ASSERT_EQUAL(n_ghosts(), 0);
252 
253  // Adding a new system shouldn't add any default ghosting if the
254  // EquationSystems disabled it
255  System & sys1 = es.add_system<System>("System1");
256  CPPUNIT_ASSERT_EQUAL(n_ghosts(), 0);
257  CPPUNIT_ASSERT_EQUAL(n_evaluables(sys1), 0);
258  CPPUNIT_ASSERT_EQUAL(n_couplings(sys1), 0);
259 
260  // But if we reenable it then now we should have three functors,
261  // with the default algebraic and coupling functors from sys1.
262  //
263  // We currently iterate over coupling functors manually even when
264  // using them for evaluability... this test will need to change
265  // eventually, when that does.
266  es.enable_default_ghosting(true);
267  CPPUNIT_ASSERT_EQUAL(n_ghosts(), 3);
268  CPPUNIT_ASSERT_EQUAL(n_evaluables(sys1), 1);
269  CPPUNIT_ASSERT_EQUAL(n_couplings(sys1), 1);
270 
271  // Adding a second system with default ghosting reenabled should
272  // give us 2 more functors.
273  System & sys2 = es.add_system<System>("System2");
274  CPPUNIT_ASSERT_EQUAL(n_ghosts(), 5);
275  CPPUNIT_ASSERT_EQUAL(n_evaluables(sys2), 1);
276  CPPUNIT_ASSERT_EQUAL(n_couplings(sys2), 1);
277 
278  // Adding a user functor to evaluables and couplings should add it
279  // to the mesh
282  CPPUNIT_ASSERT_EQUAL(n_ghosts(), 6);
283  CPPUNIT_ASSERT_EQUAL(n_evaluables(sys1), 2);
284  CPPUNIT_ASSERT_EQUAL(n_couplings(sys1), 1);
285 
286  // Unless we say not to.
287  auto gpn3 = std::make_shared<GhostPointNeighbors>(mesh);
288  sys1.get_dof_map().add_coupling_functor(gpn3, /*to_mesh=*/false);
289  CPPUNIT_ASSERT_EQUAL(n_ghosts(), 6);
290  CPPUNIT_ASSERT_EQUAL(n_evaluables(sys1), 2);
291  CPPUNIT_ASSERT_EQUAL(n_couplings(sys1), 2);
292 
293  // Turning off default coupling again should get rid of everything
294  // except the user functors.
295  es.enable_default_ghosting(false);
296  CPPUNIT_ASSERT_EQUAL(n_ghosts(), 1);
297  CPPUNIT_ASSERT_EQUAL(n_evaluables(sys1), 1);
298  CPPUNIT_ASSERT_EQUAL(n_couplings(sys1), 1);
299  CPPUNIT_ASSERT_EQUAL(n_evaluables(sys2), 0);
300  CPPUNIT_ASSERT_EQUAL(n_couplings(sys2), 0);
301  }
302 
303 
304 
305 
306 
307 };
308 
libMesh::DofMap::add_algebraic_ghosting_functor
void add_algebraic_ghosting_functor(GhostingFunctor &evaluable_functor, bool to_mesh=true)
Adds a functor which can specify algebraic ghosting requirements for use with distributed vectors.
Definition: dof_map.C:1862
libMesh::System
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:100
libMesh::MeshBase::ghosting_functors_begin
std::set< GhostingFunctor * >::const_iterator ghosting_functors_begin() const
Beginning of range of ghosting functors.
Definition: mesh_base.h:1113
libMesh::Number
Real Number
Definition: libmesh_common.h:195
libMesh::Elem::child_ptr
const Elem * child_ptr(unsigned int i) const
Definition: elem.h:2567
EquationSystemsTest::tearDown
void tearDown()
Definition: equation_systems_test.C:60
libMesh::MeshBase::ghosting_functors_end
std::set< GhostingFunctor * >::const_iterator ghosting_functors_end() const
End of range of ghosting functors.
Definition: mesh_base.h:1119
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::Elem::JUST_REFINED
Definition: elem.h:1172
EquationSystemsTest::testRepartitionThenReinit
void testRepartitionThenReinit()
Definition: equation_systems_test.C:188
libMesh::MeshRefinement::refine_elements
bool refine_elements()
Only refines the user-requested elements.
Definition: mesh_refinement.C:683
EquationSystemsTest::testInit
void testInit()
Definition: equation_systems_test.C:78
libMesh::libmesh_real
T libmesh_real(T a)
Definition: libmesh_common.h:166
EquationSystemsTest::testRefineThenReinitPreserveFlags
void testRefineThenReinitPreserveFlags()
Definition: equation_systems_test.C:149
libMesh::DofObject::set_id
dof_id_type & set_id()
Definition: dof_object.h:776
libMesh::MeshBase::max_elem_id
virtual dof_id_type max_elem_id() const =0
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::TOLERANCE
static const Real TOLERANCE
Definition: libmesh_common.h:128
EquationSystemsTest::testDisableDefaultGhosting
void testDisableDefaultGhosting()
Definition: equation_systems_test.C:217
libMesh::GhostPointNeighbors
This class implements the default geometry ghosting in libMesh: point neighbors and interior_parent e...
Definition: ghost_point_neighbors.h:36
mesh
MeshBase & mesh
Definition: mesh_communication.C:1257
libMesh::MeshBase::node_ptr
virtual const Node * node_ptr(const dof_id_type i) const =0
libMesh::DofObject::processor_id
processor_id_type processor_id() const
Definition: dof_object.h:829
libMesh::MeshBase::elem_ptr
virtual const Elem * elem_ptr(const dof_id_type i) const =0
libMesh::Elem::active
bool active() const
Definition: elem.h:2345
libMesh::MeshBase::query_elem_ptr
virtual const Elem * query_elem_ptr(const dof_id_type i) const =0
EquationSystemsTest::testPostInitAddRealSystem
void testPostInitAddRealSystem()
Definition: equation_systems_test.C:98
libMesh::ReplicatedMesh
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
Definition: replicated_mesh.h:47
EquationSystemsTest::testAddSystem
void testAddSystem()
Definition: equation_systems_test.C:71
libMesh::MeshRefinement
Implements (adaptive) mesh refinement algorithms for a MeshBase.
Definition: mesh_refinement.h:61
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::MeshBase::remove_ghosting_functor
void remove_ghosting_functor(GhostingFunctor &ghosting_functor)
Removes a functor which was previously added to the set of ghosting functors.
Definition: mesh_base.C:450
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::MeshTools::Generation::build_line
void build_line(UnstructuredMesh &mesh, const unsigned int nx, const Real xmin=0., const Real xmax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
A specialized build_cube() for 1D meshes.
Definition: mesh_generation.C:1480
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::Point
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:38
libMesh::NodeElem
The NodeElem is a point element, generally used as a side of a 1D element.
Definition: node_elem.h:37
libMesh::CONSTANT
Definition: enum_order.h:41
libMesh::EquationSystems::enable_default_ghosting
virtual void enable_default_ghosting(bool enable)
Enable or disable default ghosting functors on the Mesh and on all Systems.
Definition: equation_systems.C:312
EquationSystemsTest::testReinitWithNodeElem
void testReinitWithNodeElem()
Definition: equation_systems_test.C:133
libMesh::Elem::REFINE
Definition: elem.h:1171
EquationSystemsTest::testPostInitAddElem
void testPostInitAddElem()
Definition: equation_systems_test.C:111
libMesh::MONOMIAL
Definition: enum_fe_family.h:39
libMesh::EquationSystems
This is the EquationSystems class.
Definition: equation_systems.h:74
libMesh::Elem::set_node
virtual Node *& set_node(const unsigned int i)
Definition: elem.h:2059
libMesh::EquationSystems::disable_refine_in_reinit
void disable_refine_in_reinit()
Calls to reinit() will not try to coarsen or refine the mesh.
Definition: equation_systems.h:534
libMesh::MeshBase::add_ghosting_functor
void add_ghosting_functor(GhostingFunctor &ghosting_functor)
Adds a functor which can specify ghosting requirements for use on distributed meshes.
Definition: mesh_base.h:1089
libMesh::Elem::refinement_flag
RefinementState refinement_flag() const
Definition: elem.h:2614
distance
Real distance(const Point &p)
Definition: subdomains_ex3.C:50
libMesh::EquationSystems::reinit
virtual void reinit()
Handle any mesh changes and reinitialize all the systems on the updated mesh.
Definition: equation_systems.C:121
EquationSystemsTest
Definition: equation_systems_test.C:34
libMesh::System::point_value
Number point_value(unsigned int var, const Point &p, const bool insist_on_success=true, const NumericVector< Number > *sol=nullptr) const
Definition: system.C:1971
libMesh::Elem::n_children
virtual unsigned int n_children() const =0
EquationSystemsTest::setUp
void setUp()
Definition: equation_systems_test.C:57
libMesh::MeshBase::add_elem
virtual Elem * add_elem(Elem *e)=0
Add elem e to the end of the element array.
libmesh_cppunit.h
EquationSystemsTest::testConstruction
void testConstruction()
Definition: equation_systems_test.C:65
libMesh::Elem
This is the base class from which all geometric element types are derived.
Definition: elem.h:100
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(EquationSystemsTest)
EquationSystemsTest::testPostInitAddSystem
void testPostInitAddSystem()
Definition: equation_systems_test.C:87
libMesh::System::get_dof_map
const DofMap & get_dof_map() const
Definition: system.h:2099
libMesh::Elem::set_refinement_flag
void set_refinement_flag(const RefinementState rflag)
Sets the value of the refinement flag for the element.
Definition: elem.h:2622
libMesh::MeshBase::allow_renumbering
void allow_renumbering(bool allow)
If false is passed in then this mesh will no longer be renumbered when being prepared for use.
Definition: mesh_base.h:1025
libMesh::MeshTools::Generation::build_point
void build_point(UnstructuredMesh &mesh, const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
A specialized build_cube() for 0D meshes.
Definition: mesh_generation.C:1462
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
test_comm.h
libMesh::MeshBase::prepare_for_use
void prepare_for_use(const bool skip_renumber_nodes_and_elements=false, const bool skip_find_neighbors=false)
Prepare a newly ecreated (or read) mesh for use.
Definition: mesh_base.C:318
libMesh::Elem::INACTIVE
Definition: elem.h:1174
libMesh::System::project_solution
void project_solution(FunctionBase< Number > *f, FunctionBase< Gradient > *g=nullptr) const
Projects arbitrary functions onto the current solution.
Definition: system_projection.C:950
libMesh::Elem::build
static std::unique_ptr< Elem > build(const ElemType type, Elem *p=nullptr)
Definition: elem.C:246
libMesh::FIRST
Definition: enum_order.h:42
libMesh::remote_elem
const RemoteElem * remote_elem
Definition: remote_elem.C:57
int
void ErrorVector unsigned int
Definition: adjoints_ex3.C:360
libMesh::Parameters
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition: parameters.h:59
libMesh::EDGE2
Definition: enum_elem_type.h:35
libMesh::MeshBase::partition
virtual void partition(const unsigned int n_parts)
Call the default partitioner (currently metis_partition()).
Definition: mesh_base.C:599
libMesh::DofMap::add_coupling_functor
void add_coupling_functor(GhostingFunctor &coupling_functor, bool to_mesh=true)
Adds a functor which can specify coupling requirements for creation of sparse matrices.
Definition: dof_map.C:1838
libMesh::EquationSystems::parameters
Parameters parameters
Data structure holding arbitrary parameters.
Definition: equation_systems.h:557