libMesh
spatial_dimension_test.C
Go to the documentation of this file.
1 #include <libmesh/equation_systems.h>
2 #include <libmesh/mesh_generation.h>
3 #include <libmesh/node.h>
4 #include <libmesh/replicated_mesh.h>
5 
6 #include "test_comm.h"
7 #include "libmesh_cppunit.h"
8 
9 
10 using namespace libMesh;
11 
12 class MeshSpatialDimensionTest : public CppUnit::TestCase
13 {
18 public:
19  CPPUNIT_TEST_SUITE( MeshSpatialDimensionTest );
20 
21 #if LIBMESH_DIM > 1
22  CPPUNIT_TEST( test1D );
23 #endif
24 #if LIBMESH_DIM > 2
25  CPPUNIT_TEST( test2D );
26 #endif
27 
28  CPPUNIT_TEST_SUITE_END();
29 
30 public:
31  void setUp()
32  {
33  }
34 
35  void tearDown()
36  {
37  }
38 
39  void test1D()
40  {
41  // 1.) Test that build_line() produces a Mesh with spatial_dimension==1
43  MeshTools::Generation::build_line (mesh, /*n_elem=*/2, /*xmin=*/0., /*xmax=*/1., EDGE2);
44  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), mesh.mesh_dimension());
45  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), mesh.spatial_dimension());
46 
47  // 2.) Move the nodes in the y-direction, test that spatial_dimension==2
48  // The spatial dimension is updated during prepare_for_use().
49  for (auto & node : mesh.node_ptr_range())
50  (*node)(1) = (*node)(0) * (*node)(0);
51 
53  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), mesh.mesh_dimension());
54  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.spatial_dimension());
55 
56 
57  // 3.) Move nodes back to zero, check that spatial_dimension is *not* decreased
58  for (auto & node : mesh.node_ptr_range())
59  (*node)(1) = 0.;
60 
62  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), mesh.mesh_dimension());
63  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.spatial_dimension());
64 
65 
66  // 4.) Move z-coordinate of nodes, check that spatial_dimension is now 3.
67 #if LIBMESH_DIM == 3
68  for (auto & node : mesh.node_ptr_range())
69  (*node)(2) = (*node)(0) * (*node)(0);
70 
72  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), mesh.mesh_dimension());
73  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), mesh.spatial_dimension());
74 #endif
75  }
76 
77 
78 
79  void test2D()
80  {
81  // 1.) Test that build_cube() produces a Mesh with spatial_dimension==2
84  /*nx=*/2, /*ny=*/2,
85  /*xmin=*/0., /*xmax=*/1.,
86  /*ymin=*/0., /*ymax=*/1.,
87  QUAD4);
88  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.spatial_dimension());
89 
90  // 2.) Move the nodes in the z-direction, test that spatial_dimension==3
91  // The spatial dimension is updated during prepare_for_use().
92  for (auto & node : mesh.node_ptr_range())
93  (*node)(2) =
94  (*node)(0) * (*node)(0) +
95  (*node)(1) * (*node)(1);
96 
98  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.mesh_dimension());
99  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), mesh.spatial_dimension());
100 
101 
102  // 3.) Move nodes back to zero, check that spatial_dimension is *not* decreased
103  for (auto & node : mesh.node_ptr_range())
104  (*node)(2) = 0.;
105 
107  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.mesh_dimension());
108  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), mesh.spatial_dimension());
109 
110  // 4.) The user is allowed to set the spatial dimension, make sure
111  // that's honored if it's consistent.
114  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.mesh_dimension());
115  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.spatial_dimension());
116 
117  // 5.) If the user sets a spatial dimension that's not big enough, make sure that
118  // prepare_for_use() increases it.
121  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.mesh_dimension());
122  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.spatial_dimension());
123  }
124 };
125 
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
MeshSpatialDimensionTest
Definition: spatial_dimension_test.C:12
mesh
MeshBase & mesh
Definition: mesh_communication.C:1257
libMesh::MeshBase::mesh_dimension
unsigned int mesh_dimension() const
Definition: mesh_base.C:135
libMesh::ReplicatedMesh
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
Definition: replicated_mesh.h:47
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::node_ptr_range
virtual SimpleRange< node_iterator > node_ptr_range()=0
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
libMesh::QUAD4
Definition: enum_elem_type.h:41
TestCommWorld
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:111
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(MeshSpatialDimensionTest)
libMesh::MeshBase::spatial_dimension
unsigned int spatial_dimension() const
Definition: mesh_base.C:159
MeshSpatialDimensionTest::test1D
void test1D()
Definition: spatial_dimension_test.C:39
MeshSpatialDimensionTest::test2D
void test2D()
Definition: spatial_dimension_test.C:79
libmesh_cppunit.h
MeshSpatialDimensionTest::tearDown
void tearDown()
Definition: spatial_dimension_test.C:35
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
MeshSpatialDimensionTest::setUp
void setUp()
Definition: spatial_dimension_test.C:31
libMesh::MeshBase::set_spatial_dimension
void set_spatial_dimension(unsigned char d)
Sets the "spatial dimension" of the Mesh.
Definition: mesh_base.C:166
libMesh::EDGE2
Definition: enum_elem_type.h:35