libMesh
mesh_base_test.C
Go to the documentation of this file.
1 
2 #include <libmesh/distributed_mesh.h>
3 #include <libmesh/elem.h>
4 #include <libmesh/mesh.h>
5 #include <libmesh/mesh_generation.h>
6 #include <libmesh/mesh_refinement.h>
7 #include <libmesh/mesh_tools.h>
8 #include <libmesh/replicated_mesh.h>
9 
10 #include "test_comm.h"
11 #include "libmesh_cppunit.h"
12 
13 using namespace libMesh;
14 
15 class MeshBaseTest : public CppUnit::TestCase {
20 public:
21  LIBMESH_CPPUNIT_TEST_SUITE( MeshBaseTest );
22 
23 /* Tests need a 2d mesh */
24 #if LIBMESH_DIM > 1
25  CPPUNIT_TEST( testDistributedMeshVerifyHasNeighborPtrs );
26  CPPUNIT_TEST( testMeshVerifyHasNeighborPtrs );
27  CPPUNIT_TEST( testReplicatedMeshVerifyHasNeighborPtrs );
28  CPPUNIT_TEST( testDistributedMeshVerifyHasCachedElemData );
29  CPPUNIT_TEST( testMeshVerifyHasCachedElemData );
30  CPPUNIT_TEST( testReplicatedMeshVerifyHasCachedElemData );
31  CPPUNIT_TEST( testDistributedMeshVerifyRemovalPreparation );
32  CPPUNIT_TEST( testMeshVerifyRemovalPreparation );
33  CPPUNIT_TEST( testReplicatedMeshVerifyRemovalPreparation );
34  CPPUNIT_TEST( testDistributedMeshVerifyIsPrepared );
35  CPPUNIT_TEST( testMeshVerifyIsPrepared );
36  CPPUNIT_TEST( testReplicatedMeshVerifyIsPrepared );
37 #endif
38 
39  CPPUNIT_TEST_SUITE_END();
40 
41 public:
42 
43  void setUp() {}
44 
45  void tearDown() {}
46 
48  {
54  2, 2,
55  0., 1.,
56  0., 1.,
57  QUAD9);
58 
59  // Break some neighbor links. Of course nobody would do this in
60  // real life, right?
61  Elem * elem0 = mesh.query_elem_ptr(0);
62  if (elem0)
63  for (auto n : elem0->side_index_range())
64  {
65  Elem * neigh = elem0->neighbor_ptr(n);
66  if (!neigh)
67  continue;
68  auto n_neigh = neigh->which_neighbor_am_i(elem0);
69  CPPUNIT_ASSERT(n_neigh != libMesh::invalid_uint);
70 
71  elem0->set_neighbor(n, nullptr);
72  neigh->set_neighbor(n_neigh, nullptr);
73  }
74  }
75 
77  {
78  this->BrokenNeighborMesh(mesh);
81  CPPUNIT_ASSERT(mesh.is_prepared());
82  CPPUNIT_ASSERT(MeshTools::valid_is_prepared(mesh));
83  }
84 
86  {
88  testMeshBaseVerifyHasNeighborPtrs(mesh);
89  }
90 
92  {
94  testMeshBaseVerifyHasNeighborPtrs(mesh);
95  }
96 
98  {
100  testMeshBaseVerifyHasNeighborPtrs(mesh);
101  }
102 
104  {
110  2, 2,
111  0., 1.,
112  0., 1.,
113  QUAD9);
114 
115  // Invalidate the subdomain ids cache
116  Elem * elem0 = mesh.query_elem_ptr(0);
117  if (elem0)
118  elem0->subdomain_id() = 1;
119 
120  // We're unprepared (prepare_for_use() will update that cache) but
121  // we're not marked that way.
122  CPPUNIT_ASSERT(!MeshTools::valid_is_prepared(mesh));
123 
126  CPPUNIT_ASSERT(mesh.is_prepared());
127  CPPUNIT_ASSERT(MeshTools::valid_is_prepared(mesh));
128  }
129 
131  {
133  testMeshBaseVerifyHasCachedElemData(mesh);
134  }
135 
137  {
139  testMeshBaseVerifyHasCachedElemData(mesh);
140  }
141 
143  {
145  testMeshBaseVerifyHasCachedElemData(mesh);
146  }
147 
149  {
155  2, 2,
156  0., 1.,
157  0., 1.,
158  QUAD9);
159 
160  // Remove elements on one side, orphaning 4 nodes and removing one
161  // boundary condition. Remove dangling neighbor pointers too; we
162  // can't even clone a mesh with dangling pointers.
163  for (auto & elem : mesh.element_ptr_range())
164  if (elem->vertex_average()(0) > 0.5)
165  mesh.delete_elem(elem);
166  else
167  elem->set_neighbor(1, nullptr);
168 
169  // We're unprepared (prepare_for_use() will remove those orphaned
170  // nodes and fix the boundary id sets and fix the partitioning of
171  // nodes that might need new owners) but we're not marked that
172  // way.
173  CPPUNIT_ASSERT(!MeshTools::valid_is_prepared(mesh));
174 
179  CPPUNIT_ASSERT(mesh.is_prepared());
180  CPPUNIT_ASSERT(MeshTools::valid_is_prepared(mesh));
181  }
182 
184  {
186  testMeshBaseVerifyRemovalPreparation(mesh);
187  }
188 
190  {
192  testMeshBaseVerifyRemovalPreparation(mesh);
193  }
194 
196  {
198  testMeshBaseVerifyRemovalPreparation(mesh);
199  }
200 
202  {
203  this->BrokenNeighborMesh(mesh);
204 
205  // We're unprepared (prepare_for_use() will restitch those
206  // neighbor pointers) but we're not marked that way.
207  CPPUNIT_ASSERT(!MeshTools::valid_is_prepared(mesh));
208  }
209 
211  {
213  testMeshBaseVerifyIsPrepared(mesh);
214  }
215 
217  {
219  testMeshBaseVerifyIsPrepared(mesh);
220  }
221 
223  {
225  testMeshBaseVerifyIsPrepared(mesh);
226  }
227 }; // End definition of class MeshBaseTest
228 
CPPUNIT_TEST_SUITE_REGISTRATION(MeshBaseTest)
void BrokenNeighborMesh(UnstructuredMesh &mesh)
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
bool is_prepared() const
Definition: mesh_base.C:1057
void testMeshVerifyRemovalPreparation()
void testDistributedMeshVerifyHasNeighborPtrs()
const unsigned int invalid_uint
A number which is used quite often to represent an invalid or uninitialized value for an unsigned int...
Definition: libmesh.h:303
void testMeshBaseVerifyIsPrepared(UnstructuredMesh &mesh)
IntRange< unsigned short > side_index_range() const
Definition: elem.h:2724
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:218
bool valid_is_prepared(const MeshBase &mesh)
A function for testing whether a mesh&#39;s cached is_prepared() setting is not a false positive...
Definition: mesh_tools.C:1256
void testDistributedMeshVerifyHasCachedElemData()
This is the base class from which all geometric element types are derived.
Definition: elem.h:94
MeshBase & mesh
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.
The libMesh namespace provides an interface to certain functionality in the library.
void unset_has_cached_elem_data()
Tells this we have done some operation (e.g.
Definition: mesh_base.h:281
virtual void delete_elem(Elem *e)=0
Removes element e from the mesh.
void unset_has_neighbor_ptrs()
Tells this we have done some operation (e.g.
Definition: mesh_base.h:270
The UnstructuredMesh class is derived from the MeshBase class.
unsigned int which_neighbor_am_i(const Elem *e) const
This function tells you which neighbor e is.
Definition: elem.h:2933
void testMeshBaseVerifyHasCachedElemData(UnstructuredMesh &mesh)
void testReplicatedMeshVerifyRemovalPreparation()
void testMeshBaseVerifyHasNeighborPtrs(UnstructuredMesh &mesh)
void testMeshVerifyHasCachedElemData()
void unset_is_partitioned()
Tells this we have done some operation creating unpartitioned elements.
Definition: mesh_base.h:246
void set_neighbor(const unsigned int i, Elem *n)
Assigns n as the neighbor.
Definition: elem.h:2632
The DistributedMesh class is derived from the MeshBase class, and is intended to provide identical fu...
void testMeshBaseVerifyRemovalPreparation(UnstructuredMesh &mesh)
void complete_preparation()
Definition: mesh_base.C:874
void testMeshVerifyIsPrepared()
const Elem * neighbor_ptr(unsigned int i) const
Definition: elem.h:2612
void testMeshVerifyHasNeighborPtrs()
void testReplicatedMeshVerifyIsPrepared()
virtual const Elem * query_elem_ptr(const dof_id_type i) const =0
subdomain_id_type subdomain_id() const
Definition: elem.h:2588
void testDistributedMeshVerifyIsPrepared()
void unset_has_boundary_id_sets()
Tells this we have done some operation which may have invalidated our cached boundary id sets...
Definition: mesh_base.h:340
void testReplicatedMeshVerifyHasCachedElemData()
void testDistributedMeshVerifyRemovalPreparation()
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition: mesh.h:50
void unset_has_removed_orphaned_nodes()
Tells this we have done some operation (e.g.
Definition: mesh_base.h:318
void testReplicatedMeshVerifyHasNeighborPtrs()