libMesh
Loading...
Searching...
No Matches
dirichlet_constraint_values_test.C
Go to the documentation of this file.
1#include <libmesh/dirichlet_boundaries.h>
2#include <libmesh/dof_map.h>
3#include <libmesh/elem.h>
4#include <libmesh/equation_systems.h>
5#include <libmesh/function_base.h>
6#include <libmesh/libmesh.h>
7#include <libmesh/linear_implicit_system.h>
8#include <libmesh/mesh.h>
9#include <libmesh/mesh_generation.h>
10#include <libmesh/mesh_refinement.h>
11#include <libmesh/parallel.h>
12#include <libmesh/periodic_boundary.h>
13#include <libmesh/sparse_matrix.h>
14
15#include "test_comm.h"
16#include "libmesh_cppunit.h"
17
18using namespace libMesh;
19
20namespace
21{
22// build_square's boundary ids
23const boundary_id_type bottom_id = 0, right_id = 1, top_id = 2, left_id = 3;
24
28class RampFunction : public FunctionBase<Number>
29{
30public:
31 RampFunction(const Real scale) : _scale(scale) {}
32
33 virtual std::unique_ptr<FunctionBase<Number>> clone() const override
34 {
35 return std::make_unique<RampFunction>(_scale);
36 }
37
38 virtual Number operator()(const Point & p, const Real time = 0.) override
39 {
40 return _scale * (1 + time) * (1 + p(0) + 2 * p(1));
41 }
42
43 virtual void operator()(const Point & p, const Real time, DenseVector<Number> & output) override
44 {
45 for (auto i : make_range(output.size()))
46 output(i) = (*this)(p, time);
47 }
48
49private:
50 const Real _scale;
51};
52
57std::map<dof_id_type, Number> gather_local_rhs(DofMap & dof_map)
58{
59 std::map<dof_id_type, Number> rhs;
60 const auto & values = dof_map.get_primal_constraint_values();
61 for (const auto & [dof, row] : dof_map.get_dof_constraints())
62 if (dof_map.local_index(dof))
63 {
64 libmesh_ignore(row);
65 const auto it = values.find(dof);
66 rhs[dof] = it == values.end() ? Number(0) : it->second;
67 }
68 return rhs;
69}
70
75std::map<dof_id_type, DofConstraintRow> gather_local_rows(const DofMap & dof_map)
76{
77 std::map<dof_id_type, DofConstraintRow> rows;
78 for (const auto & [dof, row] : dof_map.get_dof_constraints())
79 if (dof_map.local_index(dof))
80 rows[dof] = row;
81 return rows;
82}
83
88void assert_constraints_equal(const std::map<dof_id_type, DofConstraintRow> & rows,
89 const std::map<dof_id_type, Number> & rhs,
90 DofMap & dof_map)
91{
92 const auto other_rows = gather_local_rows(dof_map);
93 const auto other_rhs = gather_local_rhs(dof_map);
94
95 CPPUNIT_ASSERT_EQUAL(rows.size(), other_rows.size());
96 for (const auto & [dof, row] : rows)
97 {
98 const auto it = other_rows.find(dof);
99 CPPUNIT_ASSERT(it != other_rows.end());
100 CPPUNIT_ASSERT_EQUAL(row.size(), it->second.size());
101 for (const auto & [constraining_dof, coef] : row)
102 {
103 const auto jt = it->second.find(constraining_dof);
104 CPPUNIT_ASSERT(jt != it->second.end());
105 CPPUNIT_ASSERT_EQUAL(coef, jt->second);
106 }
107 }
108
109 CPPUNIT_ASSERT_EQUAL(rhs.size(), other_rhs.size());
110 for (const auto & [dof, value] : rhs)
111 CPPUNIT_ASSERT_EQUAL(value, libmesh_map_find(other_rhs, dof));
112}
113}
114
115class DirichletConstraintValuesTest : public CppUnit::TestCase
116{
117public:
119
120#ifdef LIBMESH_ENABLE_DIRICHLET
121# if LIBMESH_DIM > 1 && defined(LIBMESH_ENABLE_PERIODIC)
123# endif
124# ifdef LIBMESH_HAVE_SOLVER
126# endif
127#endif
128
130
131private:
133 {
134 LOG_UNIT_TEST;
135 checkUpdate(4);
136 }
137
148 {
149 LOG_UNIT_TEST;
150
153 auto & sys = es.add_system<ExplicitSystem>("sys");
154 const auto u = sys.add_variable("u", FIRST);
155
156 // The constraint operator below uses the node numbering of
157 // build_line
158 mesh.allow_renumbering(false);
160
161 // Each interior even node is a spline node. Each odd node takes
162 // the mean of its two neighbors. The left node takes a combination
163 // of a new spline node and the next spline node, and the right node
164 // is a spline node. Processor 0 owns all rows of the operator.
165 auto matrix = SparseMatrix<Number>::build(mesh.comm());
166 const bool rank0 = !mesh.comm().rank();
167 matrix->init(11, 6, rank0 ? 11 : 0, rank0 ? 6 : 0, rank0 ? 6 : 0, 0);
168 if (rank0)
169 {
170 matrix->set(0, 0, 0.75);
171 matrix->set(0, 1, 0.25);
172 for (const auto i : make_range(5))
173 {
174 if (i)
175 matrix->set(i*2, i, 1);
176 matrix->set(i*2+1, i, 0.5);
177 matrix->set(i*2+1, i+1, 0.5);
178 }
179 matrix->set(10, 5, 1);
180 }
181 matrix->close();
182 mesh.copy_constraint_rows(*matrix);
184
185 // build_line's left boundary id
186 sys.get_dof_map().add_dirichlet_boundary(DirichletBoundary({0}, {u}, RampFunction(1.)));
187
188 es.init();
189
190 auto & dof_map = sys.get_dof_map();
191 for (const Real time : {0., 0.5, 2.})
192 {
193 sys.time = time;
194 sys.reinit_constraints();
195 const auto rebuilt_rows = gather_local_rows(dof_map);
196 const auto rebuilt_rhs = gather_local_rhs(dof_map);
197
198 sys.reinit_dirichlet_constraint_values();
199 assert_constraints_equal(rebuilt_rows, rebuilt_rhs, dof_map);
200 }
201 }
202
210 void checkUpdate(const unsigned int n)
211 {
213
214 // Keep all neighbors until the periodic boundary is attached. The
215 // periodic boundary needs them.
217 MeshTools::Generation::build_square(mesh, n, n, 0., 1., 0., 1., QUAD9);
218
220 auto & sys = es.add_system<ExplicitSystem>("sys");
221 const auto u = sys.add_variable("u", SECOND, HIERARCHIC);
222 const auto v = sys.add_variable("v", SECOND, LAGRANGE);
223
224 // The mesh is periodic in y. Thus periodic partners meet the
225 // Dirichlet boundaries at the corners.
226 PeriodicBoundary periodic(RealVectorValue(0., 1.));
227 periodic.myboundary = bottom_id;
228 periodic.pairedboundary = top_id;
229 sys.get_dof_map().add_periodic_boundary(periodic);
230
233
234 // Add two overlapping time-dependent boundaries. The first claims u
235 // and v on the left. The second claims u on the left and right.
236 // The first boundary keeps the left side.
237 sys.get_dof_map().add_dirichlet_boundary(
238 DirichletBoundary({left_id}, {u, v}, RampFunction(1.)));
239 sys.get_dof_map().add_dirichlet_boundary(
240 DirichletBoundary({left_id, right_id}, {u}, RampFunction(-3.)));
241
242#ifdef LIBMESH_ENABLE_AMR // We can do more testing here
243 // Refine the elements near the left boundary. Then the hanging
244 // nodes on the refined/unrefined interface reach the Dirichlet and
245 // periodic boundaries.
246 MeshRefinement refinement(mesh);
247 for (auto & elem : mesh.active_element_ptr_range())
248 {
249 if (elem->vertex_average()(0) < 0.3)
250 elem->set_refinement_flag(Elem::REFINE);
251 }
252 refinement.refine_elements();
253#endif // LIBMESH_ENABLE_AMR
254
255 es.init();
256
257 auto & dof_map = sys.get_dof_map();
258
259 for (const Real time : {0., 0.25, 1., 2.5})
260 {
261 sys.time = time;
262
263 sys.reinit_constraints();
264 const auto rebuilt_rows = gather_local_rows(dof_map);
265 const auto rebuilt_rhs = gather_local_rhs(dof_map);
266
267 // The update must give the same result from any state that the
268 // last processing left
269 sys.reinit_dirichlet_constraint_values();
270 assert_constraints_equal(rebuilt_rows, rebuilt_rhs, dof_map);
271 const auto updated_rhs = gather_local_rhs(dof_map);
272
273 // The values must change with time. Otherwise the comparison
274 // above could pass on stale values.
275 bool any_nonzero = false;
276 for (const auto & [dof, value] : updated_rhs)
277 {
278 libmesh_ignore(dof);
279 any_nonzero = any_nonzero || value != Number(0);
280 }
281 TestCommWorld->max(any_nonzero);
282 CPPUNIT_ASSERT(any_nonzero);
283 }
284
285 // Two consecutive updates, with no full rebuild between them, must
286 // also agree with a new full rebuild
287 sys.time = 4.;
288 sys.reinit_dirichlet_constraint_values();
289 sys.reinit_dirichlet_constraint_values();
290 const auto twice_updated_rows = gather_local_rows(dof_map);
291 const auto twice_updated_rhs = gather_local_rhs(dof_map);
292 sys.reinit_constraints();
293 assert_constraints_equal(twice_updated_rows, twice_updated_rhs, dof_map);
294 }
295};
296
void checkUpdate(const unsigned int n)
Checks that System::reinit_dirichlet_constraint_values() gives exactly the result of System::reinit_c...
CPPUNIT_TEST(testUpdateMatchesRebuild)
void testSplineConstraintRows()
Checks that System::reinit_dirichlet_constraint_values() gives exactly the result of System::reinit_c...
CPPUNIT_TEST(testSplineConstraintRows)
LIBMESH_CPPUNIT_TEST_SUITE(DirichletConstraintValuesTest)
processor_id_type rank() const
Defines a dense vector for use in Finite Element-type computations.
This class allows one to associate Dirichlet boundary values with a given set of mesh boundary ids an...
This class handles the numbering of degrees of freedom on a mesh.
Definition dof_map.h:181
DofConstraintValueMap & get_primal_constraint_values()
Definition dof_map.h:2555
This is the EquationSystems class.
virtual void init()
Initialize all the systems.
virtual System & add_system(std::string_view system_type, std::string_view name)
Add the system of type system_type named name to the systems array.
Manages consistently variables, degrees of freedom, and coefficient vectors for explicit systems.
Base class for functors that can be evaluated at a point and (optionally) time.
virtual std::unique_ptr< FunctionBase< Output > > clone() const =0
virtual Output operator()(const Point &p, const Real time=0.)=0
void allow_remote_element_removal(bool allow)
If false is passed in then this mesh will no longer have remote elements deleted when being prepared ...
Definition mesh_base.h:1390
void allow_renumbering(bool allow)
If false is passed in then this mesh will no longer be renumbered when being prepared for use.
Definition mesh_base.h:1367
void prepare_for_use(const bool skip_renumber_nodes_and_elements, const bool skip_find_neighbors)
Prepare a newly created (or read) mesh for use.
Definition mesh_base.C:901
void copy_constraint_rows(const MeshBase &other_mesh)
Copy the constraints from the other mesh to this mesh.
Definition mesh_base.C:2838
Implements (adaptive) mesh refinement algorithms for a MeshBase.
bool refine_elements()
Only refines the user-requested elements.
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition mesh.h:51
const Parallel::Communicator & comm() const
boundary_id_type myboundary
The boundary ID of this boundary and its counterpart.
The definition of a periodic boundary.
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition point.h:41
static std::unique_ptr< SparseMatrix< T > > build(const Parallel::Communicator &comm, const SolverPackage solver_package=libMesh::default_solver_package(), const MatrixBuildType matrix_build_type=MatrixBuildType::AUTOMATIC)
Builds a SparseMatrix<T> using the linear solver package specified by solver_package.
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:1400
CPPUNIT_TEST_SUITE_REGISTRATION(DirichletConstraintValuesTest)
static const boundary_id_type left_id
static const boundary_id_type right_id
Communicator * TestCommWorld
MeshBase & mesh
LIBMESH_DEVICE_INLINE ResultVector row(const TensorLike &T_in, const unsigned int row_index)
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 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.
void scale(MeshBase &mesh, const Real xs, const Real ys=0., const Real zs=0.)
Scales the mesh.
The libMesh namespace provides an interface to certain functionality in the library.
int8_t boundary_id_type
Definition id_types.h:51
void libmesh_ignore(const Args &...)
VectorValue< Real > RealVectorValue
Useful typedefs to allow transparent switching between Real and Complex data types.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...
Definition int_range.h:176
const boundary_id_type top_id
const boundary_id_type bottom_id
static const bool value
Definition xdr_io.C:55