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  LIBMESH_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  LOG_UNIT_TEST;
42 
43  // 1.) Test that build_line() produces a Mesh with spatial_dimension==1
45  MeshTools::Generation::build_line (mesh, /*n_elem=*/2, /*xmin=*/0., /*xmax=*/1., EDGE2);
46  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), mesh.mesh_dimension());
47  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), mesh.spatial_dimension());
48 
49  // 2.) Move the nodes in the y-direction, test that spatial_dimension==2
50  // The spatial dimension is updated during prepare_for_use().
51  for (auto & node : mesh.node_ptr_range())
52  (*node)(1) = (*node)(0) * (*node)(0);
53 
55  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), mesh.mesh_dimension());
56  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.spatial_dimension());
57 
58 
59  // 3.) Move nodes back to zero, check that spatial_dimension is *not* decreased
60  for (auto & node : mesh.node_ptr_range())
61  (*node)(1) = 0.;
62 
64  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), mesh.mesh_dimension());
65  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.spatial_dimension());
66 
67 
68  // 4.) Move z-coordinate of nodes, check that spatial_dimension is now 3.
69 #if LIBMESH_DIM == 3
70  for (auto & node : mesh.node_ptr_range())
71  (*node)(2) = (*node)(0) * (*node)(0);
72 
74  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), mesh.mesh_dimension());
75  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), mesh.spatial_dimension());
76 #endif
77  }
78 
79 
80 
81  void test2D()
82  {
83  LOG_UNIT_TEST;
84 
85  // 1.) Test that build_cube() produces a Mesh with spatial_dimension==2
88  /*nx=*/2, /*ny=*/2,
89  /*xmin=*/0., /*xmax=*/1.,
90  /*ymin=*/0., /*ymax=*/1.,
91  QUAD4);
92  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.spatial_dimension());
93 
94  // 2.) Move the nodes in the z-direction, test that spatial_dimension==3
95  // The spatial dimension is updated during prepare_for_use().
96  for (auto & node : mesh.node_ptr_range())
97  (*node)(2) =
98  (*node)(0) * (*node)(0) +
99  (*node)(1) * (*node)(1);
100 
102  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.mesh_dimension());
103  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), mesh.spatial_dimension());
104 
105 
106  // 3.) Move nodes back to zero, check that spatial_dimension is *not* decreased
107  for (auto & node : mesh.node_ptr_range())
108  (*node)(2) = 0.;
109 
111  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.mesh_dimension());
112  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), mesh.spatial_dimension());
113 
114  // 4.) The user is allowed to set the spatial dimension, make sure
115  // that's honored if it's consistent.
118  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.mesh_dimension());
119  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.spatial_dimension());
120 
121  // 5.) If the user sets a spatial dimension that's not big enough, make sure that
122  // prepare_for_use() increases it.
125  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.mesh_dimension());
126  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.spatial_dimension());
127  }
128 };
129 
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:171
void set_spatial_dimension(unsigned char d)
Sets the "spatial dimension" of the Mesh.
Definition: mesh_base.C:550
void prepare_for_use(const bool skip_renumber_nodes_and_elements, const bool skip_find_neighbors)
Prepare a newly ecreated (or read) mesh for use.
Definition: mesh_base.C:759
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.
unsigned int spatial_dimension() const
Definition: mesh_base.C:543
unsigned int mesh_dimension() const
Definition: mesh_base.C:372
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.
CPPUNIT_TEST_SUITE_REGISTRATION(MeshSpatialDimensionTest)