libMesh
spatial_dimension_test.C
Go to the documentation of this file.
1 // Ignore unused parameter warnings coming from cppunit headers
2 #include <libmesh/ignore_warnings.h>
3 #include <cppunit/extensions/HelperMacros.h>
4 #include <cppunit/TestCase.h>
5 #include <libmesh/restore_warnings.h>
6 
7 #include <libmesh/equation_systems.h>
8 #include <libmesh/mesh_generation.h>
9 #include <libmesh/node.h>
10 #include <libmesh/replicated_mesh.h>
11 
12 #include "test_comm.h"
13 
14 // THE CPPUNIT_TEST_SUITE_END macro expands to code that involves
15 // std::auto_ptr, which in turn produces -Wdeprecated-declarations
16 // warnings. These can be ignored in GCC as long as we wrap the
17 // offending code in appropriate pragmas. We can't get away with a
18 // single ignore_warnings.h inclusion at the beginning of this file,
19 // since the libmesh headers pull in a restore_warnings.h at some
20 // point. We also don't bother restoring warnings at the end of this
21 // file since it's not a header.
22 #include <libmesh/ignore_warnings.h>
23 
24 using namespace libMesh;
25 
26 class MeshSpatialDimensionTest : public CppUnit::TestCase
27 {
32 public:
33  CPPUNIT_TEST_SUITE( MeshSpatialDimensionTest );
34 
35  CPPUNIT_TEST( test1D );
36  CPPUNIT_TEST( test2D );
37 
38  CPPUNIT_TEST_SUITE_END();
39 
40 public:
41  void setUp()
42  {
43  }
44 
45  void tearDown()
46  {
47  }
48 
49  void test1D()
50  {
51  // 1.) Test that build_line() produces a Mesh with spatial_dimension==1
53  MeshTools::Generation::build_line (mesh, /*n_elem=*/2, /*xmin=*/0., /*xmax=*/1., EDGE2);
54  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), mesh.mesh_dimension());
55  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), mesh.spatial_dimension());
56 
57  // 2.) Move the nodes in the y-direction, test that spatial_dimension==2
58  // The spatial dimension is updated during prepare_for_use().
59  for (auto & node : mesh.node_ptr_range())
60  (*node)(1) = (*node)(0) * (*node)(0);
61 
62  mesh.prepare_for_use();
63  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), mesh.mesh_dimension());
64  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.spatial_dimension());
65 
66 
67  // 3.) Move nodes back to zero, check that spatial_dimension is *not* decreased
68  for (auto & node : mesh.node_ptr_range())
69  (*node)(1) = 0.;
70 
71  mesh.prepare_for_use();
72  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), mesh.mesh_dimension());
73  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.spatial_dimension());
74 
75 
76  // 4.) Move z-coordinate of nodes, check that spatial_dimension is now 3.
77 #if LIBMESH_DIM == 3
78  for (auto & node : mesh.node_ptr_range())
79  (*node)(2) = (*node)(0) * (*node)(0);
80 
81  mesh.prepare_for_use();
82  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), mesh.mesh_dimension());
83  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), mesh.spatial_dimension());
84 #endif
85  }
86 
87 
88 
89  void test2D()
90  {
91  // 1.) Test that build_cube() produces a Mesh with spatial_dimension==2
94  /*nx=*/2, /*ny=*/2,
95  /*xmin=*/0., /*xmax=*/1.,
96  /*ymin=*/0., /*ymax=*/1.,
97  QUAD4);
98  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.spatial_dimension());
99 
100  // 2.) Move the nodes in the z-direction, test that spatial_dimension==3
101  // The spatial dimension is updated during prepare_for_use().
102  for (auto & node : mesh.node_ptr_range())
103  (*node)(2) =
104  (*node)(0) * (*node)(0) +
105  (*node)(1) * (*node)(1);
106 
107  mesh.prepare_for_use();
108  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.mesh_dimension());
109  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), mesh.spatial_dimension());
110 
111 
112  // 3.) Move nodes back to zero, check that spatial_dimension is *not* decreased
113  for (auto & node : mesh.node_ptr_range())
114  (*node)(2) = 0.;
115 
116  mesh.prepare_for_use();
117  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.mesh_dimension());
118  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), mesh.spatial_dimension());
119 
120  // 4.) The user is allowed to set the spatial dimension, make sure
121  // that's honored if it's consistent.
122  mesh.set_spatial_dimension(2);
123  mesh.prepare_for_use();
124  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.mesh_dimension());
125  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.spatial_dimension());
126 
127  // 5.) If the user sets a spatial dimension that's not big enough, make sure that
128  // prepare_for_use() increases it.
129  mesh.set_spatial_dimension(1);
130  mesh.prepare_for_use();
131  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.mesh_dimension());
132  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), mesh.spatial_dimension());
133  }
134 };
135 
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:28
void set_spatial_dimension(unsigned char d)
Sets the "spatial dimension" of the Mesh.
Definition: mesh_base.C:164
virtual SimpleRange< node_iterator > node_ptr_range() libmesh_override
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 prepare_for_use(const bool skip_renumber_nodes_and_elements=false, const bool skip_find_neighbors=false)
Prepare a newly created (or read) mesh for use.
Definition: mesh_base.C:174
unsigned int spatial_dimension() const
Definition: mesh_base.C:157
unsigned int mesh_dimension() const
Definition: mesh_base.C:148
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)