libMesh
distort.C
Go to the documentation of this file.
1 #include <libmesh/libmesh.h>
2 #include <libmesh/replicated_mesh.h>
3 #include <libmesh/elem.h>
4 #include <libmesh/mesh_generation.h>
5 #include <libmesh/mesh_modification.h>
6 #include <libmesh/mesh_tools.h>
7 
8 #include "test_comm.h"
9 #include "libmesh_cppunit.h"
10 
11 // C++ includes
12 #include <unordered_set>
13 #include <unordered_map>
14 
15 
16 using namespace libMesh;
17 
18 class DistortTest : public CppUnit::TestCase
19 {
24 public:
25  LIBMESH_CPPUNIT_TEST_SUITE( DistortTest );
26 
27  // 2D tests
28 #if LIBMESH_DIM > 1
29  CPPUNIT_TEST( testDistortQuad );
30 #endif
31 
32  // 3D tests
33 #if LIBMESH_DIM > 2
34  CPPUNIT_TEST( testDistortHex );
35 #endif
36 
37  CPPUNIT_TEST_SUITE_END();
38 
39 protected:
40  // Helper function called by the test implementations, saves a few lines of code.
41  void test_helper_2D(ElemType elem_type)
42  {
43  ReplicatedMesh mesh(*TestCommWorld, /*dim=*/2);
44 
46  /*nx=*/2, /*ny=*/2,
47  /*xmin=*/0., /*xmax=*/1.,
48  /*ymin=*/0., /*ymax=*/1.,
49  elem_type);
50 
51  perturb_and_check(mesh);
52  }
53 
54  // Helper function called by the test implementations in 3D, saves a few lines of code.
55  void test_helper_3D(ElemType elem_type)
56  {
57  ReplicatedMesh mesh(*TestCommWorld, /*dim=*/3);
58 
60  /*nx=*/2, /*ny=*/2, /*nz=*/2,
61  /*xmin=*/0., /*xmax=*/1.,
62  /*ymin=*/0., /*ymax=*/1.,
63  /*zmin=*/0., /*zmax=*/1.,
64  elem_type);
65  perturb_and_check(mesh);
66  }
67 
68  // Code to save node positions, perturb the mesh, and ensure the correct nodes moved.
70  {
71  // Record node positions ahead of time to make sure the correct
72  // ones are moved by distort.
73  std::unordered_map<dof_id_type, Point> pts_before;
74  for (const auto & node : mesh.node_ptr_range())
75  pts_before[node->id()] = *node;
76 
77  std::unordered_set<dof_id_type> boundary_node_ids =
79 
81  /*factor=*/0.1,
82  /*perturb_boundary=*/false);
83 
84  // Make sure the boundary nodes are not perturbed, and the
85  // other nodes are.
86  for (const auto & node : mesh.node_ptr_range())
87  {
88  bool equal = node->absolute_fuzzy_equals(pts_before[node->id()]);
89  CPPUNIT_ASSERT(boundary_node_ids.count(node->id()) ? equal : !equal);
90  }
91  }
92 
93 public:
94  void setUp() {}
95  void tearDown() {}
96  void testDistortQuad() { LOG_UNIT_TEST; test_helper_2D(QUAD4); }
97  void testDistortHex() { LOG_UNIT_TEST; test_helper_3D(HEX8); }
98 };
99 
100 
ElemType
Defines an enum for geometric element types.
CPPUNIT_TEST_SUITE_REGISTRATION(DistortTest)
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
void tearDown()
Definition: distort.C:95
void setUp()
Definition: distort.C:94
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:171
void distort(MeshBase &mesh, const Real factor, const bool perturb_boundary=false)
Randomly perturb the nodal locations.
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.
void test_helper_2D(ElemType elem_type)
Definition: distort.C:41
The libMesh namespace provides an interface to certain functionality in the library.
std::unordered_set< dof_id_type > find_boundary_nodes(const MeshBase &mesh)
Calling this function on a 2D mesh will convert all the elements to triangles.
Definition: mesh_tools.C:517
void testDistortQuad()
Definition: distort.C:96
void testDistortHex()
Definition: distort.C:97
void test_helper_3D(ElemType elem_type)
Definition: distort.C:55
void perturb_and_check(ReplicatedMesh &mesh)
Definition: distort.C:69
void build_cube(UnstructuredMesh &mesh, const unsigned int nx=0, const unsigned int ny=0, const unsigned int nz=0, const Real xmin=0., const Real xmax=1., const Real ymin=0., const Real ymax=1., const Real zmin=0., const Real zmax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
Builds a (elements) cube.