libMesh
Loading...
Searching...
No Matches
fe_rational_map.C
Go to the documentation of this file.
1#include "test_comm.h"
2
3#include <libmesh/dof_map.h>
4#include <libmesh/elem.h>
5#include <libmesh/equation_systems.h>
6#include <libmesh/fe_base.h>
7#include <libmesh/fe_interface.h>
8#include <libmesh/mesh.h>
9#include <libmesh/mesh_generation.h>
10#include <libmesh/numeric_vector.h>
11#include <libmesh/system.h>
12
13#include <vector>
14
15#include "libmesh_cppunit.h"
16
17
18using namespace libMesh;
19
20
21template <ElemType elem_type>
22class RationalMapTest : public CppUnit::TestCase {
23
24private:
25 unsigned int _dim, _nx, _ny, _nz;
27 std::vector<dof_id_type> _dof_indices;
28 std::unique_ptr<Mesh> _mesh;
29 std::unique_ptr<EquationSystems> _es;
30 std::unique_ptr<FEBase> _fe;
31
32protected:
33 std::string libmesh_suite_name;
34
35public:
36 void setUp()
37 {
38 _mesh = std::make_unique<Mesh>(*TestCommWorld);
39 const std::unique_ptr<Elem> test_elem = Elem::build(elem_type);
40 _dim = test_elem->dim();
41 const unsigned int ny = _dim > 1;
42 const unsigned int nz = _dim > 2;
43
44 // Make sure we can handle non-zero weight indices
45 _mesh->add_node_integer("buffer integer1");
46 _mesh->add_node_integer("buffer integer2");
47
48 // Default weight to 1.0 so we don't get NaN from contains_point
49 // checks with a default GhostPointNeighbors ghosting
50 const Real default_weight = 1.0;
51 unsigned char weight_index = cast_int<unsigned char>
52 (_mesh->add_node_datum<Real>("rational_weight", true,
53 &default_weight));
54
55 libmesh_assert_not_equal_to(weight_index, 0);
56
57 _mesh->set_default_mapping_type(RATIONAL_BERNSTEIN_MAP);
58 _mesh->set_default_mapping_data(weight_index);
59
61 1, ny, nz,
62 0., 1., 0., ny, 0., nz,
63 elem_type);
64
65 for (auto elem : _mesh->element_ptr_range())
66 {
67 CPPUNIT_ASSERT_EQUAL(elem->mapping_type(), RATIONAL_BERNSTEIN_MAP);
68 CPPUNIT_ASSERT_EQUAL(elem->mapping_data(), weight_index);
69 }
70
71 // Transform the cube / square into a rotated quarter-annulus,
72 // with central axis at (-.5, 0) and radii ranging from .5 to 1.5
73
74 for (auto node : _mesh->node_ptr_range())
75 {
76 // Workaround for nvc++ bug
77 Node & n = *node;
78
79 Real & x = (*node)(0);
80 Real & y = (*node)(1);
81 n.set_extra_datum<Real>(weight_index, 1);
82 if (y > .6)
83 {
84 y = .5 + x;
85 x = -.5;
86 }
87 else if (y > .4)
88 {
89 y = .5 + x;
90 x = y - .5;
91 n.set_extra_datum<Real>(weight_index, sqrt(Real(2))/2);
92 }
93 }
94
95 _es = std::make_unique<EquationSystems>(*_mesh);
96 System * sys = &(_es->add_system<System> ("SimpleSystem"));
97 sys->add_variable("u", FIRST);
98 _es->init();
99
100 _fe = FEBase::build(_dim, sys->variable_type("u"));
101 _fe->get_xyz();
102 _fe->get_phi();
103 _fe->get_dphi();
104 _fe->get_dphidx();
105#if LIBMESH_DIM > 1
106 _fe->get_dphidy();
107#endif
108#if LIBMESH_DIM > 2
109 _fe->get_dphidz();
110#endif
111
112 auto rng = _mesh->active_local_element_ptr_range();
113 _elem = rng.begin() == rng.end() ? nullptr : *(rng.begin());
114
115 _nx = 10;
116 _ny = (_dim > 1) ? _nx : 0;
117 _nz = (_dim > 2) ? _nx : 0;
118 }
119
120 void tearDown() {}
121
123 {
124 LOG_UNIT_TEST;
125
126 // Handle the "more processors than elements" case
127 if (!_elem)
128 return;
129
130 // These tests require exceptions to be enabled because a
131 // TypeTensor::solve() call down in Elem::contains_point()
132 // actually throws a non-fatal exception for a certain Point which
133 // is not in the Elem. When exceptions are not enabled, this test
134 // simply aborts.
135#ifdef LIBMESH_ENABLE_EXCEPTIONS
136 for (unsigned int j=0; j != _ny+1; ++j)
137 for (unsigned int k=0; k != _nz+1; ++k)
138 {
139 for (int i=-1; i != int(_nx+2); ++i)
140 {
141 Real r = (Real(i)/_nx) + 0.5,
142 theta = (Real(j)/_nx)*pi/2,
143 z = (Real(k)/_nx);
144 Real x = -.5 + r * std::cos(theta),
145 y = r * std::sin(theta);
146 Point p(x,y,z);
147 // Test for false negatives
148 if (i >= 0 && i <= int(_nx))
149 CPPUNIT_ASSERT(_elem->contains_point(p));
150 // Also test for false positives
151 else
152 CPPUNIT_ASSERT(!_elem->contains_point(p));
153 }
154 }
155#endif
156 }
157};
158
159
160#define INSTANTIATE_RATIONALMAP_TEST(elemtype) \
161 class RationalMapTest_##elemtype : public RationalMapTest<elemtype> { \
162 public: \
163 RationalMapTest_##elemtype() : \
164 RationalMapTest<elemtype>() { \
165 if (unitlog->summarized_logs_enabled()) \
166 this->libmesh_suite_name = "RationalMapTest"; \
167 else \
168 this->libmesh_suite_name = "RationalMapTest_" #elemtype; \
169 } \
170 CPPUNIT_TEST_SUITE( RationalMapTest_##elemtype ); \
171 CPPUNIT_TEST( testContainsPoint ); \
172 CPPUNIT_TEST_SUITE_END(); \
173 }; \
174 \
175 CPPUNIT_TEST_SUITE_REGISTRATION( RationalMapTest_##elemtype );
176
void ErrorVector unsigned int
std::unique_ptr< Mesh > _mesh
unsigned int _dim
unsigned int _nz
unsigned int _nx
std::unique_ptr< EquationSystems > _es
std::string libmesh_suite_name
unsigned int _ny
std::vector< dof_id_type > _dof_indices
std::unique_ptr< FEBase > _fe
void set_extra_datum(const unsigned int index, const T value)
Sets the value on this object of the extra datum associated with index, which should have been obtain...
This is the base class from which all geometric element types are derived.
Definition elem.h:96
static std::unique_ptr< Elem > build(const ElemType type, Elem *p=nullptr)
Definition elem.C:442
virtual bool contains_point(const Point &p, Real tol=TOLERANCE) const
Definition elem.C:2784
static std::unique_ptr< FEGenericBase > build(const unsigned int dim, const FEType &type)
Builds a specific finite element type.
A Node is like a Point, but with more information.
Definition node.h:55
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition point.h:40
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition system.h:100
const FEType & variable_type(const unsigned int i) const
Definition system.C:2721
unsigned int add_variable(std::string_view var, const FEType &type, const std::set< subdomain_id_type > *const active_subdomains=nullptr)
Adds the variable var to the list of variables for this system.
Definition system.C:1344
Communicator * TestCommWorld
INSTANTIATE_RATIONALMAP_TEST(EDGE3)
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.
The libMesh namespace provides an interface to certain functionality in the library.
@ RATIONAL_BERNSTEIN_MAP
const Real pi
.
Definition libmesh.h:292
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real