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>
31 RampFunction(
const Real scale) : _scale(
scale) {}
33 virtual std::unique_ptr<FunctionBase<Number>>
clone()
const override
35 return std::make_unique<RampFunction>(_scale);
40 return _scale * (1 + time) * (1 + p(0) + 2 * p(1));
46 output(i) = (*this)(p, time);
57std::map<dof_id_type, Number> gather_local_rhs(
DofMap & dof_map)
59 std::map<dof_id_type, Number> rhs;
61 for (
const auto & [dof, row] : dof_map.get_dof_constraints())
62 if (dof_map.local_index(dof))
65 const auto it = values.find(dof);
66 rhs[dof] = it == values.end() ?
Number(0) : it->second;
75std::map<dof_id_type, DofConstraintRow> gather_local_rows(
const DofMap & dof_map)
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))
88void assert_constraints_equal(
const std::map<dof_id_type, DofConstraintRow> & rows,
89 const std::map<dof_id_type, Number> & rhs,
92 const auto other_rows = gather_local_rows(dof_map);
93 const auto other_rhs = gather_local_rhs(dof_map);
95 CPPUNIT_ASSERT_EQUAL(rows.size(), other_rows.size());
96 for (
const auto & [dof, row] : rows)
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)
103 const auto jt = it->second.find(constraining_dof);
104 CPPUNIT_ASSERT(jt != it->second.end());
105 CPPUNIT_ASSERT_EQUAL(coef, jt->second);
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));
120#ifdef LIBMESH_ENABLE_DIRICHLET
121# if LIBMESH_DIM > 1 && defined(LIBMESH_ENABLE_PERIODIC)
124# ifdef LIBMESH_HAVE_SOLVER
167 matrix->init(11, 6, rank0 ? 11 : 0, rank0 ? 6 : 0, rank0 ? 6 : 0, 0);
170 matrix->set(0, 0, 0.75);
171 matrix->set(0, 1, 0.25);
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);
179 matrix->set(10, 5, 1);
186 sys.get_dof_map().add_dirichlet_boundary(
DirichletBoundary({0}, {u}, RampFunction(1.)));
190 auto & dof_map = sys.get_dof_map();
191 for (
const Real time : {0., 0.5, 2.})
194 sys.reinit_constraints();
195 const auto rebuilt_rows = gather_local_rows(dof_map);
196 const auto rebuilt_rhs = gather_local_rhs(dof_map);
198 sys.reinit_dirichlet_constraint_values();
199 assert_constraints_equal(rebuilt_rows, rebuilt_rhs, dof_map);
229 sys.get_dof_map().add_periodic_boundary(periodic);
237 sys.get_dof_map().add_dirichlet_boundary(
239 sys.get_dof_map().add_dirichlet_boundary(
242#ifdef LIBMESH_ENABLE_AMR
247 for (
auto & elem :
mesh.active_element_ptr_range())
249 if (elem->vertex_average()(0) < 0.3)
257 auto & dof_map = sys.get_dof_map();
259 for (
const Real time : {0., 0.25, 1., 2.5})
263 sys.reinit_constraints();
264 const auto rebuilt_rows = gather_local_rows(dof_map);
265 const auto rebuilt_rhs = gather_local_rhs(dof_map);
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);
275 bool any_nonzero =
false;
276 for (
const auto & [dof,
value] : updated_rhs)
282 CPPUNIT_ASSERT(any_nonzero);
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);
void testUpdateMatchesRebuild()
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.
DofConstraintValueMap & get_primal_constraint_values()
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 ...
void allow_renumbering(bool allow)
If false is passed in then this mesh will no longer be renumbered when being prepared for use.
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.
void copy_constraint_rows(const MeshBase &other_mesh)
Copy the constraints from the other mesh to this mesh.
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.
const Parallel::Communicator & comm() const
boundary_id_type myboundary
The boundary ID of this boundary and its counterpart.
boundary_id_type pairedboundary
The definition of a periodic boundary.
A Point defines a location in LIBMESH_DIM dimensional Real space.
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.
CPPUNIT_TEST_SUITE_REGISTRATION(DirichletConstraintValuesTest)
static const boundary_id_type left_id
static const boundary_id_type right_id
Communicator * TestCommWorld
LIBMESH_DEVICE_INLINE ResultVector row(const TensorLike &T_in, const unsigned int row_index)
The libMesh namespace provides an interface to certain functionality in the library.
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...
const boundary_id_type top_id
const boundary_id_type bottom_id