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,
87 const mesh::Topology& topology,
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();
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)
267 int bs = dofmap.index_map_bs();
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,
305 = std::enable_if_t<std::is_convertible_v<S, T>
306 or std::is_convertible_v<S, std::span<const T>>>>
307 requires std::is_convertible_v<std::remove_cvref_t<X>,
308 std::vector<std::int32_t>>
329 template <
typename X>
330 requires std::is_convertible_v<std::remove_cvref_t<X>,
331 std::vector<std::int32_t>>
334 : _function_space(V), _g(g), _dofs0(std::forward<X>(dofs)),
335 _owned_indices0(num_owned(*V->dofmap(), _dofs0))
339 if (g->shape.size() != V->value_shape().size())
341 throw std::runtime_error(
342 "Rank mis-match between Constant and function space in DirichletBC");
345 if (g->value.size() != _function_space->dofmap()->bs())
347 throw std::runtime_error(
348 "Creating a DirichletBC using a Constant is not supported when the "
349 "Constant size is not equal to the block size of the constrained "
350 "(sub-)space. Use a fem::Function to create the fem::DirichletBC.");
353 if (!V->element()->interpolation_ident())
355 throw std::runtime_error(
356 "Constant can be used only with point-evaluation elements");
360 if (
const int bs = V->dofmap()->bs(); bs > 1)
362 _owned_indices0 *= bs;
363 _dofs0 = unroll_dofs(_dofs0, bs);
379 template <
typename X>
380 requires std::is_convertible_v<std::remove_cvref_t<X>,
381 std::vector<std::int32_t>>
384 _dofs0(std::forward<X>(dofs)),
385 _owned_indices0(num_owned(*_function_space->dofmap(), _dofs0))
387 assert(_function_space);
390 if (
const int bs = _function_space->dofmap()->bs(); bs > 1)
392 _owned_indices0 *= bs;
393 _dofs0 = unroll_dofs(_dofs0, bs);
418 template <
typename X>
421 : _function_space(V), _g(g),
422 _dofs0(std::forward<typename X::value_type>(V_g_dofs[0])),
423 _dofs1_g(std::forward<typename X::value_type>(V_g_dofs[1])),
424 _owned_indices0(num_owned(*_function_space->dofmap(), _dofs0))
450 return _function_space;
455 std::variant<std::shared_ptr<const Function<T, U>>,
456 std::shared_ptr<const Constant<T>>>
468 std::pair<std::span<const std::int32_t>, std::int32_t>
dof_indices()
const
470 return {_dofs0, _owned_indices0};
495 void set(std::span<T> x, std::optional<std::span<const T>> x0,
498 std::int32_t x_size = x.size();
501 for (std::int32_t idx : _dofs0)
509 if (std::holds_alternative<std::shared_ptr<
const Function<T, U>>>(_g))
511 auto g = std::get<std::shared_ptr<const Function<T, U>>>(_g);
514 = _dofs1_g.empty() ? std::span(_dofs0) : std::span(_dofs1_g);
515 std::span<const T> values = g->x()->array();
518 std::span<const T> _x0 = x0.value();
519 assert(x.size() <= _x0.size());
520 for (std::size_t i = 0; i < _dofs0.size(); ++i)
522 if (_dofs0[i] < x_size)
524 assert(dofs1_g[i] < (std::int32_t)values.size());
525 x[_dofs0[i]] = alpha * (values[dofs1_g[i]] - _x0[_dofs0[i]]);
531 for (std::size_t i = 0; i < _dofs0.size(); ++i)
533 if (_dofs0[i] < x_size)
535 assert(dofs1_g[i] < (std::int32_t)values.size());
536 x[_dofs0[i]] = alpha * values[dofs1_g[i]];
541 else if (std::holds_alternative<std::shared_ptr<
const Constant<T>>>(_g))
543 auto g = std::get<std::shared_ptr<const Constant<T>>>(_g);
544 const std::vector<T>&
value = g->value;
545 std::int32_t bs = _function_space->dofmap()->bs();
548 assert(x.size() <= x0.value().size());
549 std::ranges::for_each(
551 [x_size, &x, x0 = x0.value(), &
value, alpha, bs](
auto dof)
554 x[dof] = alpha * (value[dof % bs] - x0[dof]);
559 std::ranges::for_each(_dofs0,
560 [x_size, bs, alpha, &
value, &x](
auto dof)
563 x[dof] = alpha *
value[dof % bs];
581 for (std::int32_t idx : _dofs0)
583 assert(idx < (std::int32_t)markers.size());
590 std::shared_ptr<const FunctionSpace<U>> _function_space;
593 std::variant<std::shared_ptr<const Function<T, U>>,
594 std::shared_ptr<const Constant<T>>>
599 std::vector<std::int32_t> _dofs0, _dofs1_g;
602 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
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:332
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:495
std::variant< std::shared_ptr< const Function< T, U > >, std::shared_ptr< const Constant< T > > > value() const
Definition DirichletBC.h:457
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:468
std::shared_ptr< const FunctionSpace< U > > function_space() const
Definition DirichletBC.h:448
~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:382
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:309
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:579
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:419
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: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: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