13#include "FunctionSpace.h"
17#include <dolfinx/common/types.h>
55std::vector<std::int32_t>
57 int dim, std::span<const std::int32_t> entities,
88 std::array<std::reference_wrapper<const DofMap>, 2> dofmaps,
int dim,
89 std::span<const std::int32_t> entities,
bool remote =
true);
102template <std::
floating_po
int T,
typename U>
111 if (V.element()->is_mixed())
113 throw std::runtime_error(
114 "Cannot locate dofs geometrically for mixed space. Use subspaces.");
118 const std::vector<T> dof_coordinates = V.tabulate_dof_coordinates(
true);
120 using cmdspan3x_t = MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
122 MDSPAN_IMPL_STANDARD_NAMESPACE::extents<
123 std::size_t, 3, MDSPAN_IMPL_STANDARD_NAMESPACE::dynamic_extent>>;
126 cmdspan3x_t x(dof_coordinates.data(), 3, dof_coordinates.size() / 3);
127 const std::vector<std::int8_t> marked_dofs = marker_fn(x);
129 std::vector<std::int32_t> dofs;
130 dofs.reserve(std::count(marked_dofs.begin(), marked_dofs.end(),
true));
131 for (std::size_t i = 0; i < marked_dofs.size(); ++i)
153template <std::
floating_po
int T,
typename U>
167 auto mesh = V0.mesh();
170 if (mesh != V1.
mesh())
171 throw std::runtime_error(
"Meshes are not the same.");
172 const int tdim = mesh->topology()->dim();
174 assert(V0.element());
176 if (*V0.element() != *V1.
element())
177 throw std::runtime_error(
"Function spaces must have the same element.");
182 using cmdspan3x_t = MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
184 MDSPAN_IMPL_STANDARD_NAMESPACE::extents<
185 std::size_t, 3, MDSPAN_IMPL_STANDARD_NAMESPACE::dynamic_extent>>;
188 cmdspan3x_t x(dof_coordinates.data(), 3, dof_coordinates.size() / 3);
189 const std::vector<std::int8_t> marked_dofs = marker_fn(x);
192 std::shared_ptr<const DofMap> dofmap0 = V0.dofmap();
194 const int bs0 = dofmap0->bs();
195 std::shared_ptr<const DofMap> dofmap1 = V1.
dofmap();
197 const int bs1 = dofmap1->bs();
199 const int element_bs = dofmap0->element_dof_layout().block_size();
200 assert(element_bs == dofmap1->element_dof_layout().block_size());
203 auto topology = mesh->topology();
205 std::vector<std::array<std::int32_t, 2>> bc_dofs;
206 for (
int c = 0; c < topology->connectivity(tdim, 0)->num_nodes(); ++c)
209 auto cell_dofs0 = dofmap0->cell_dofs(c);
210 auto cell_dofs1 = dofmap1->cell_dofs(c);
213 for (std::size_t i = 0; i < cell_dofs1.size(); ++i)
215 if (marked_dofs[cell_dofs1[i]])
218 for (
int k = 0; k < element_bs; ++k)
220 const int local_pos = element_bs * i + k;
221 const std::div_t pos0 = std::div(local_pos, bs0);
222 const std::div_t pos1 = std::div(local_pos, bs1);
223 const std::int32_t dof_index0
224 = bs0 * cell_dofs0[pos0.quot] + pos0.rem;
225 const std::int32_t dof_index1
226 = bs1 * cell_dofs1[pos1.quot] + pos1.rem;
227 bc_dofs.push_back({dof_index0, dof_index1});
234 std::ranges::sort(bc_dofs);
235 auto [unique_end, range_end] = std::ranges::unique(bc_dofs);
236 bc_dofs.erase(unique_end, range_end);
239 std::array dofs = {std::vector<std::int32_t>(bc_dofs.size()),
240 std::vector<std::int32_t>(bc_dofs.size())};
241 std::ranges::transform(bc_dofs, dofs[0].begin(),
242 [](
auto dof) {
return dof[0]; });
243 std::ranges::transform(bc_dofs, dofs[1].begin(),
244 [](
auto dof) {
return dof[1]; });
258 std::floating_point U = dolfinx::scalar_value_type_t<T>>
264 std::size_t num_owned(
const DofMap& dofmap,
265 std::span<const std::int32_t> dofs)
268 std::int32_t map_size = dofmap.
index_map->size_local();
269 std::int32_t owned_size = bs * map_size;
270 auto it = std::ranges::lower_bound(dofs, owned_size);
271 return std::distance(dofs.begin(), it);
275 static std::vector<std::int32_t>
276 unroll_dofs(std::span<const std::int32_t> dofs,
int bs)
278 std::vector<std::int32_t> dofs_unrolled(bs * dofs.size());
279 for (std::size_t i = 0; i < dofs.size(); ++i)
280 for (
int k = 0; k < bs; ++k)
281 dofs_unrolled[bs * i + k] = bs * dofs[i] + k;
282 return dofs_unrolled;
303 template <
typename S,
typename X>
304 requires(std::is_convertible_v<S, T>
305 || std::is_convertible_v<S, std::span<const T>>)
306 && std::is_convertible_v<std::remove_cvref_t<X>,
307 std::vector<std::int32_t>>
328 template <
typename X>
329 requires std::is_convertible_v<std::remove_cvref_t<X>,
330 std::vector<std::int32_t>>
333 : _function_space(V), _g(g), _dofs0(std::forward<X>(dofs)),
334 _owned_indices0(num_owned(*V->dofmap(), _dofs0))
338 if (g->shape.size() != V->element()->value_shape().size())
340 throw std::runtime_error(
341 "Rank mis-match between Constant and function space in DirichletBC");
344 if (g->value.size() != _function_space->dofmap()->bs())
346 throw std::runtime_error(
347 "Creating a DirichletBC using a Constant is not supported when the "
348 "Constant size is not equal to the block size of the constrained "
349 "(sub-)space. Use a fem::Function to create the fem::DirichletBC.");
352 if (!V->element()->interpolation_ident())
354 throw std::runtime_error(
355 "Constant can be used only with point-evaluation elements");
359 if (
const int bs = V->dofmap()->bs(); bs > 1)
361 _owned_indices0 *= bs;
362 _dofs0 = unroll_dofs(_dofs0, bs);
378 template <
typename X>
379 requires std::is_convertible_v<std::remove_cvref_t<X>,
380 std::vector<std::int32_t>>
383 _dofs0(std::forward<X>(dofs)),
384 _owned_indices0(num_owned(*_function_space->dofmap(), _dofs0))
386 assert(_function_space);
389 if (
const int bs = _function_space->dofmap()->bs(); bs > 1)
391 _owned_indices0 *= bs;
392 _dofs0 = unroll_dofs(_dofs0, bs);
417 template <
typename X>
420 : _function_space(V), _g(g),
421 _dofs0(std::forward<typename std::remove_reference_t<X>::value_type>(
423 _dofs1_g(std::forward<typename std::remove_reference_t<X>::value_type>(
425 _owned_indices0(num_owned(*_function_space->dofmap(), _dofs0))
451 return _function_space;
456 std::variant<std::shared_ptr<const Function<T, U>>,
457 std::shared_ptr<const Constant<T>>>
469 std::pair<std::span<const std::int32_t>, std::int32_t>
dof_indices()
const
471 return {_dofs0, _owned_indices0};
496 void set(std::span<T> x, std::optional<std::span<const T>> x0,
501 auto apply = [&](std::invocable<std::int32_t>
auto set_fn)
504 std::is_same_v<std::invoke_result_t<
decltype(set_fn), std::int32_t>,
507 std::int32_t x_size = x.size();
508 for (std::size_t i = 0; i < _dofs0.size(); ++i)
510 if (_dofs0[i] < x_size)
511 x[_dofs0[i]] = set_fn(i);
517 apply([](std::int32_t) -> T {
return 0; });
521 if (std::holds_alternative<std::shared_ptr<
const Function<T, U>>>(_g))
523 auto g = std::get<std::shared_ptr<const Function<T, U>>>(_g);
525 std::span<const T> values = g->x()->array();
530 auto dofs_g = _dofs1_g.empty() ? std::span(_dofs0) : std::span(_dofs1_g);
534 assert(x.size() <= x0->size());
536 [dofs_g, x0 = *x0, alpha, values,
537 &dofs0 = this->_dofs0](std::int32_t i) -> T
539 assert(dofs_g[i] <
static_cast<std::int32_t
>(values.size()));
540 return alpha * (values[dofs_g[i]] - x0[dofs0[i]]);
546 [dofs_g, values, alpha](std::int32_t i) -> T
548 assert(dofs_g[i] <
static_cast<std::int32_t
>(values.size()));
549 return alpha * values[dofs_g[i]];
553 else if (std::holds_alternative<std::shared_ptr<
const Constant<T>>>(_g))
555 auto g = std::get<std::shared_ptr<const Constant<T>>>(_g);
556 const std::vector<T>&
value = g->value;
557 std::int32_t bs = _function_space->dofmap()->bs();
560 assert(x.size() <= x0->size());
562 [x0 = *x0, alpha, bs, &
value, &dofs0 = _dofs0](std::int32_t i) -> T
565 return alpha * (
value[dof % bs] - x0[dof]);
570 apply([alpha, bs, &
value, &dofs0 = _dofs0](std::int32_t i) -> T
571 {
return alpha *
value[dofs0[i] % bs]; });
592 for (std::int32_t idx : _dofs0)
594 assert(idx < (std::int32_t)markers.size());
601 std::shared_ptr<const FunctionSpace<U>> _function_space;
604 std::variant<std::shared_ptr<const Function<T, U>>,
605 std::shared_ptr<const Constant<T>>>
610 std::vector<std::int32_t> _dofs0, _dofs1_g;
613 std::int32_t _owned_indices0 = -1;
Degree-of-freedom map representations and tools.
Constant value which can be attached to a Form.
Definition Form.h:29
Definition DirichletBC.h:260
DirichletBC(const S &g, X &&dofs, std::shared_ptr< const FunctionSpace< U > > V)
Create a representation of a Dirichlet boundary condition constrained by a scalar- or vector-valued c...
Definition DirichletBC.h:308
DirichletBC & operator=(const DirichletBC &bc)=default
DirichletBC(std::shared_ptr< const Constant< T > > g, X &&dofs, std::shared_ptr< const FunctionSpace< U > > V)
Create a representation of a Dirichlet boundary condition constrained by a fem::Constant.
Definition DirichletBC.h:331
void set(std::span< T > x, std::optional< std::span< const T > > x0, T alpha=1) const
Set entries in an array that are constrained by Dirichlet boundary conditions.
Definition DirichletBC.h:496
std::variant< std::shared_ptr< const Function< T, U > >, std::shared_ptr< const Constant< T > > > value() const
Definition DirichletBC.h:458
DirichletBC(const DirichletBC &bc)=default
DirichletBC(DirichletBC &&bc)=default
std::pair< std::span< const std::int32_t >, std::int32_t > dof_indices() const
Definition DirichletBC.h:469
std::shared_ptr< const FunctionSpace< U > > function_space() const
Definition DirichletBC.h:449
~DirichletBC()=default
Destructor.
DirichletBC(std::shared_ptr< const Function< T, U > > g, X &&dofs)
Create a representation of a Dirichlet boundary condition where the space being constrained is the sa...
Definition DirichletBC.h:381
DirichletBC & operator=(DirichletBC &&bc)=default
Move assignment operator.
void mark_dofs(std::span< std::int8_t > markers) const
Set markers[i] = true if dof i has a boundary condition applied.
Definition DirichletBC.h:590
DirichletBC(std::shared_ptr< const Function< T, U > > g, X &&V_g_dofs, std::shared_ptr< const FunctionSpace< U > > V)
Create a representation of a Dirichlet boundary condition where the space being constrained and the f...
Definition DirichletBC.h:418
Degree-of-freedom map.
Definition DofMap.h:76
std::shared_ptr< const common::IndexMap > index_map
Index map that describes the parallel distribution of the dofmap.
Definition DofMap.h:170
int index_map_bs() const
Block size associated with the index_map.
Definition DofMap.cpp:286
This class represents a finite element function space defined by a mesh, a finite element,...
Definition vtk_utils.h:32
std::shared_ptr< const DofMap > dofmap() const
The dofmap.
Definition FunctionSpace.h:318
std::shared_ptr< const mesh::Mesh< geometry_type > > mesh() const
The mesh.
Definition FunctionSpace.h:306
std::shared_ptr< const FiniteElement< geometry_type > > element() const
The finite element.
Definition FunctionSpace.h:312
std::vector< geometry_type > tabulate_dof_coordinates(bool transpose) const
Tabulate the physical coordinates of all dofs on this process.
Definition FunctionSpace.h:173
Topology stores the topology of a mesh, consisting of mesh entities and connectivity (incidence relat...
Definition Topology.h:45
Finite element method functionality.
Definition assemble_matrix_impl.h:26
std::vector< std::int32_t > locate_dofs_geometrical(const FunctionSpace< T > &V, U marker_fn)
Find degrees of freedom whose geometric coordinate is true for the provided marking function.
Definition DirichletBC.h:103
std::vector< std::int32_t > locate_dofs_topological(const mesh::Topology &topology, const DofMap &dofmap, int dim, std::span< const std::int32_t > entities, bool remote=true)
Find degrees-of-freedom which belong to the provided mesh entities (topological).
Definition DirichletBC.cpp:187