13#include "FunctionSpace.h"
16#include <dolfinx/common/types.h>
53std::vector<std::int32_t>
55 int dim, std::span<const std::int32_t> entities,
85 const mesh::Topology& topology,
86 std::array<std::reference_wrapper<const DofMap>, 2> dofmaps,
int dim,
87 std::span<const std::int32_t> entities,
bool remote =
true);
100template <std::
floating_po
int T,
typename U>
109 if (V.element()->is_mixed())
111 throw std::runtime_error(
112 "Cannot locate dofs geometrically for mixed space. Use subspaces.");
116 const std::vector<T> dof_coordinates = V.tabulate_dof_coordinates(
true);
118 using cmdspan3x_t = MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
120 MDSPAN_IMPL_STANDARD_NAMESPACE::extents<
121 std::size_t, 3, MDSPAN_IMPL_STANDARD_NAMESPACE::dynamic_extent>>;
124 cmdspan3x_t x(dof_coordinates.data(), 3, dof_coordinates.size() / 3);
125 const std::vector<std::int8_t> marked_dofs = marker_fn(x);
127 std::vector<std::int32_t> dofs;
128 dofs.reserve(std::count(marked_dofs.begin(), marked_dofs.end(),
true));
129 for (std::size_t i = 0; i < marked_dofs.size(); ++i)
151template <std::
floating_po
int T,
typename U>
165 auto mesh = V0.
mesh();
168 if (mesh != V1.
mesh())
169 throw std::runtime_error(
"Meshes are not the same.");
170 const int tdim = mesh->topology()->dim();
175 throw std::runtime_error(
"Function spaces must have the same element.");
180 using cmdspan3x_t = MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
182 MDSPAN_IMPL_STANDARD_NAMESPACE::extents<
183 std::size_t, 3, MDSPAN_IMPL_STANDARD_NAMESPACE::dynamic_extent>>;
186 cmdspan3x_t x(dof_coordinates.data(), 3, dof_coordinates.size() / 3);
187 const std::vector<std::int8_t> marked_dofs = marker_fn(x);
190 std::shared_ptr<const DofMap> dofmap0 = V0.
dofmap();
192 const int bs0 = dofmap0->bs();
193 std::shared_ptr<const DofMap> dofmap1 = V1.
dofmap();
195 const int bs1 = dofmap1->bs();
197 const int element_bs = dofmap0->element_dof_layout().block_size();
198 assert(element_bs == dofmap1->element_dof_layout().block_size());
201 auto topology = mesh->topology();
203 std::vector<std::array<std::int32_t, 2>> bc_dofs;
204 for (
int c = 0; c < topology->connectivity(tdim, 0)->num_nodes(); ++c)
207 auto cell_dofs0 = dofmap0->cell_dofs(c);
208 auto cell_dofs1 = dofmap1->cell_dofs(c);
211 for (std::size_t i = 0; i < cell_dofs1.size(); ++i)
213 if (marked_dofs[cell_dofs1[i]])
216 for (
int k = 0; k < element_bs; ++k)
218 const int local_pos = element_bs * i + k;
219 const std::div_t pos0 = std::div(local_pos, bs0);
220 const std::div_t pos1 = std::div(local_pos, bs1);
221 const std::int32_t dof_index0
222 = bs0 * cell_dofs0[pos0.quot] + pos0.rem;
223 const std::int32_t dof_index1
224 = bs1 * cell_dofs1[pos1.quot] + pos1.rem;
225 bc_dofs.push_back({dof_index0, dof_index1});
232 std::sort(bc_dofs.begin(), bc_dofs.end());
233 bc_dofs.erase(std::unique(bc_dofs.begin(), bc_dofs.end()), bc_dofs.end());
236 std::array dofs = {std::vector<std::int32_t>(bc_dofs.size()),
237 std::vector<std::int32_t>(bc_dofs.size())};
238 std::transform(bc_dofs.cbegin(), bc_dofs.cend(), dofs[0].begin(),
239 [](
auto dof) { return dof[0]; });
240 std::transform(bc_dofs.cbegin(), bc_dofs.cend(), dofs[1].begin(),
241 [](
auto dof) { return dof[1]; });
257 std::floating_point U = dolfinx::scalar_value_type_t<T>>
263 std::size_t num_owned(
const DofMap& dofmap,
264 std::span<const std::int32_t> dofs)
267 std::int32_t map_size = dofmap.
index_map->size_local();
268 std::int32_t owned_size = bs * map_size;
269 auto it = std::lower_bound(dofs.begin(), dofs.end(), owned_size);
270 return std::distance(dofs.begin(), it);
274 static std::vector<std::int32_t>
275 unroll_dofs(std::span<const std::int32_t> dofs,
int bs)
277 std::vector<std::int32_t> dofs_unrolled(bs * dofs.size());
278 for (std::size_t i = 0; i < dofs.size(); ++i)
279 for (
int k = 0; k < bs; ++k)
280 dofs_unrolled[bs * i + k] = bs * dofs[i] + k;
281 return dofs_unrolled;
302 template <
typename S,
typename X,
304 = std::enable_if_t<std::is_convertible_v<S, T>
305 or std::is_convertible_v<S, std::span<const T>>>>
306 requires 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->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 X::value_type>(V_g_dofs[0])),
422 _dofs1_g(std::forward<typename X::value_type>(V_g_dofs[1])),
423 _owned_indices0(num_owned(*_function_space->dofmap(), _dofs0))
449 return _function_space;
454 std::variant<std::shared_ptr<const Function<T, U>>,
455 std::shared_ptr<const Constant<T>>>
467 std::pair<std::span<const std::int32_t>, std::int32_t>
dof_indices()
const
469 return {_dofs0, _owned_indices0};
483 void set(std::span<T> x, T scale = 1)
const
485 if (std::holds_alternative<std::shared_ptr<
const Function<T, U>>>(_g))
487 auto g = std::get<std::shared_ptr<const Function<T, U>>>(_g);
489 std::span<const T> values = g->x()->array();
490 auto dofs1_g = _dofs1_g.empty() ? std::span(_dofs0) : std::span(_dofs1_g);
491 std::int32_t x_size = x.size();
492 for (std::size_t i = 0; i < _dofs0.size(); ++i)
494 if (_dofs0[i] < x_size)
496 assert(dofs1_g[i] < (std::int32_t)values.size());
497 x[_dofs0[i]] = scale * values[dofs1_g[i]];
501 else if (std::holds_alternative<std::shared_ptr<
const Constant<T>>>(_g))
503 auto g = std::get<std::shared_ptr<const Constant<T>>>(_g);
504 std::vector<T>
value = g->value;
505 int bs = _function_space->dofmap()->bs();
506 std::int32_t x_size = x.size();
507 std::for_each(_dofs0.cbegin(), _dofs0.cend(),
508 [x_size, bs, scale, &
value, &x](
auto dof)
511 x[dof] = scale * value[dof % bs];
520 void set(std::span<T> x, std::span<const T> x0, T scale = 1)
const
522 if (std::holds_alternative<std::shared_ptr<
const Function<T, U>>>(_g))
524 auto g = std::get<std::shared_ptr<const Function<T, U>>>(_g);
526 std::span<const T> values = g->x()->array();
527 assert(x.size() <= x0.size());
528 auto dofs1_g = _dofs1_g.empty() ? std::span(_dofs0) : std::span(_dofs1_g);
529 std::int32_t x_size = x.size();
530 for (std::size_t i = 0; i < _dofs0.size(); ++i)
532 if (_dofs0[i] < x_size)
534 assert(dofs1_g[i] < (std::int32_t)values.size());
535 x[_dofs0[i]] = scale * (values[dofs1_g[i]] - x0[_dofs0[i]]);
539 else if (std::holds_alternative<std::shared_ptr<
const Constant<T>>>(_g))
541 auto g = std::get<std::shared_ptr<const Constant<T>>>(_g);
542 const std::vector<T>&
value = g->value;
543 std::int32_t bs = _function_space->dofmap()->bs();
544 std::for_each(_dofs0.begin(), _dofs0.end(),
545 [&x, &x0, &
value, scale, bs](
auto dof)
547 if (dof < (std::int32_t)x.size())
548 x[dof] = scale * (value[dof % bs] - x0[dof]);
563 if (std::holds_alternative<std::shared_ptr<
const Function<T, U>>>(_g))
565 auto g = std::get<std::shared_ptr<const Function<T, U>>>(_g);
567 std::span<const T> g_values = g->x()->array();
568 auto dofs1_g = _dofs1_g.empty() ? std::span(_dofs0) : std::span(_dofs1_g);
569 for (std::size_t i = 0; i < dofs1_g.size(); ++i)
570 values[_dofs0[i]] = g_values[dofs1_g[i]];
572 else if (std::holds_alternative<std::shared_ptr<
const Constant<T>>>(_g))
574 auto g = std::get<std::shared_ptr<const Constant<T>>>(_g);
576 const std::vector<T>& g_value = g->value;
577 const std::int32_t bs = _function_space->dofmap()->bs();
578 for (std::size_t i = 0; i < _dofs0.size(); ++i)
579 values[_dofs0[i]] = g_value[_dofs0[i] % bs];
591 for (std::size_t i = 0; i < _dofs0.size(); ++i)
593 assert(_dofs0[i] < (std::int32_t)markers.size());
594 markers[_dofs0[i]] =
true;
600 std::shared_ptr<const FunctionSpace<U>> _function_space;
603 std::variant<std::shared_ptr<const Function<T, U>>,
604 std::shared_ptr<const Constant<T>>>
609 std::vector<std::int32_t> _dofs0, _dofs1_g;
612 std::int32_t _owned_indices0 = -1;
Degree-of-freedom map representations and tools.
Constant value which can be attached to a Form.
Definition Constant.h:23
Definition DirichletBC.h:259
void dof_values(std::span< T > values) const
Definition DirichletBC.h:561
void set(std::span< T > x, T scale=1) const
Definition DirichletBC.h:483
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
std::variant< std::shared_ptr< const Function< T, U > >, std::shared_ptr< const Constant< T > > > value() const
Definition DirichletBC.h:456
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:467
std::shared_ptr< const FunctionSpace< U > > function_space() const
Definition DirichletBC.h:447
void set(std::span< T > x, std::span< const T > x0, T scale=1) const
Definition DirichletBC.h:520
~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.
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
void mark_dofs(std::span< std::int8_t > markers) const
Definition DirichletBC.h:589
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:171
int index_map_bs() const
Block size associated with the index_map.
Definition DofMap.cpp:282
This class represents a finite element function space defined by a mesh, a finite element,...
Definition FunctionSpace.h:34
std::shared_ptr< const DofMap > dofmap() const
The dofmap.
Definition FunctionSpace.h:335
std::shared_ptr< const mesh::Mesh< geometry_type > > mesh() const
The mesh.
Definition FunctionSpace.h:323
std::shared_ptr< const FiniteElement< geometry_type > > element() const
The finite element.
Definition FunctionSpace.h:329
std::vector< geometry_type > tabulate_dof_coordinates(bool transpose) const
Tabulate the physical coordinates of all dofs on this process.
Definition FunctionSpace.h:190
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:101
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:186