DOLFINx 0.10.0.0
DOLFINx C++ interface
Loading...
Searching...
No Matches
DirichletBC.h
1// Copyright (C) 2007-2024 Michal Habera, Anders Logg, Garth N. Wells, Jørgen
2// S.Dokken and Paul T. Kühner
3//
4// This file is part of DOLFINx (https://www.fenicsproject.org)
5//
6// SPDX-License-Identifier: LGPL-3.0-or-later
7
8#pragma once
9
10#include "Constant.h"
11#include "DofMap.h"
12#include "Function.h"
13#include "FunctionSpace.h"
14#include <algorithm>
15#include <array>
16#include <concepts>
17#include <dolfinx/common/types.h>
18#include <functional>
19#include <memory>
20#include <optional>
21#include <span>
22#include <type_traits>
23#include <utility>
24#include <variant>
25#include <vector>
26
27namespace dolfinx::fem
28{
29
55std::vector<std::int32_t>
56locate_dofs_topological(const mesh::Topology& topology, const DofMap& dofmap,
57 int dim, std::span<const std::int32_t> entities,
58 bool remote = true);
59
86std::array<std::vector<std::int32_t>, 2> locate_dofs_topological(
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);
90
102template <std::floating_point T, typename U>
103std::vector<std::int32_t> locate_dofs_geometrical(const FunctionSpace<T>& V,
104 U marker_fn)
105{
106 // FIXME: Calling V.tabulate_dof_coordinates() is very expensive,
107 // especially when we usually want the boundary dofs only. Add
108 // interface that computes dofs coordinates only for specified cell.
109
110 assert(V.element());
111 if (V.element()->is_mixed())
112 {
113 throw std::runtime_error(
114 "Cannot locate dofs geometrically for mixed space. Use subspaces.");
115 }
116
117 // Compute dof coordinates
118 const std::vector<T> dof_coordinates = V.tabulate_dof_coordinates(true);
119
120 using cmdspan3x_t
121 = md::mdspan<const T, md::extents<std::size_t, 3, md::dynamic_extent>>;
122
123 // Compute marker for each dof coordinate
124 cmdspan3x_t x(dof_coordinates.data(), 3, dof_coordinates.size() / 3);
125 const std::vector<std::int8_t> marked_dofs = marker_fn(x);
126
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)
130 {
131 if (marked_dofs[i])
132 dofs.push_back(i);
133 }
134
135 return dofs;
136}
137
151template <std::floating_point T, typename U>
152std::array<std::vector<std::int32_t>, 2> locate_dofs_geometrical(
153 std::array<std::reference_wrapper<const FunctionSpace<T>>, 2> V,
154 U marker_fn)
155{
156 // FIXME: Calling V.tabulate_dof_coordinates() is very expensive,
157 // especially when we usually want the boundary dofs only. Add
158 // interface that computes dofs coordinates only for specified cell.
159
160 // Get function spaces
161 const FunctionSpace<T>& V0 = V.at(0).get();
162 const FunctionSpace<T>& V1 = V.at(1).get();
163
164 // Get mesh
165 auto mesh = V0.mesh();
166 assert(mesh);
167 assert(V1.mesh());
168 if (mesh != V1.mesh())
169 throw std::runtime_error("Meshes are not the same.");
170 const int tdim = mesh->topology()->dim();
171
172 assert(V0.element());
173 assert(V1.element());
174 if (*V0.element() != *V1.element())
175 throw std::runtime_error("Function spaces must have the same element.");
176
177 // Compute dof coordinates
178 const std::vector<T> dof_coordinates = V1.tabulate_dof_coordinates(true);
179
180 using cmdspan3x_t
181 = md::mdspan<const T, md::extents<std::size_t, 3, md::dynamic_extent>>;
182
183 // Evaluate marker for each dof coordinate
184 cmdspan3x_t x(dof_coordinates.data(), 3, dof_coordinates.size() / 3);
185 const std::vector<std::int8_t> marked_dofs = marker_fn(x);
186
187 // Get dofmaps
188 std::shared_ptr<const DofMap> dofmap0 = V0.dofmap();
189 assert(dofmap0);
190 const int bs0 = dofmap0->bs();
191 std::shared_ptr<const DofMap> dofmap1 = V1.dofmap();
192 assert(dofmap1);
193 const int bs1 = dofmap1->bs();
194
195 const int element_bs = dofmap0->element_dof_layout().block_size();
196 assert(element_bs == dofmap1->element_dof_layout().block_size());
197
198 // Iterate over cells
199 auto topology = mesh->topology();
200 assert(topology);
201 std::vector<std::array<std::int32_t, 2>> bc_dofs;
202 for (int c = 0; c < topology->connectivity(tdim, 0)->num_nodes(); ++c)
203 {
204 // Get cell dofmaps
205 auto cell_dofs0 = dofmap0->cell_dofs(c);
206 auto cell_dofs1 = dofmap1->cell_dofs(c);
207
208 // Loop over cell dofs and add to bc_dofs if marked.
209 for (std::size_t i = 0; i < cell_dofs1.size(); ++i)
210 {
211 if (marked_dofs[cell_dofs1[i]])
212 {
213 // Unroll over blocks
214 for (int k = 0; k < element_bs; ++k)
215 {
216 const int local_pos = element_bs * i + k;
217 const std::div_t pos0 = std::div(local_pos, bs0);
218 const std::div_t pos1 = std::div(local_pos, bs1);
219 const std::int32_t dof_index0
220 = bs0 * cell_dofs0[pos0.quot] + pos0.rem;
221 const std::int32_t dof_index1
222 = bs1 * cell_dofs1[pos1.quot] + pos1.rem;
223 bc_dofs.push_back({dof_index0, dof_index1});
224 }
225 }
226 }
227 }
228
229 // Remove duplicates
230 std::ranges::sort(bc_dofs);
231 auto [unique_end, range_end] = std::ranges::unique(bc_dofs);
232 bc_dofs.erase(unique_end, range_end);
233
234 // Copy to separate array
235 std::array dofs = {std::vector<std::int32_t>(bc_dofs.size()),
236 std::vector<std::int32_t>(bc_dofs.size())};
237 std::ranges::transform(bc_dofs, dofs[0].begin(),
238 [](auto dof) { return dof[0]; });
239 std::ranges::transform(bc_dofs, dofs[1].begin(),
240 [](auto dof) { return dof[1]; });
241
242 return dofs;
243}
244
253template <dolfinx::scalar T, std::floating_point U = dolfinx::scalar_value_t<T>>
255{
256private:
259 std::size_t num_owned(const DofMap& dofmap,
260 std::span<const std::int32_t> dofs)
261 {
262 int bs = dofmap.index_map_bs();
263 std::int32_t map_size = dofmap.index_map->size_local();
264 std::int32_t owned_size = bs * map_size;
265 auto it = std::ranges::lower_bound(dofs, owned_size);
266 return std::distance(dofs.begin(), it);
267 }
268
270 static std::vector<std::int32_t>
271 unroll_dofs(std::span<const std::int32_t> dofs, int bs)
272 {
273 std::vector<std::int32_t> dofs_unrolled(bs * dofs.size());
274 for (std::size_t i = 0; i < dofs.size(); ++i)
275 for (int k = 0; k < bs; ++k)
276 dofs_unrolled[bs * i + k] = bs * dofs[i] + k;
277 return dofs_unrolled;
278 }
279
280public:
298 template <typename S, typename X>
299 requires(std::is_convertible_v<S, T>
300 || std::is_convertible_v<S, std::span<const T>>)
301 && std::is_convertible_v<std::remove_cvref_t<X>,
302 std::vector<std::int32_t>>
303 DirichletBC(const S& g, X&& dofs, std::shared_ptr<const FunctionSpace<U>> V)
304 : DirichletBC(std::make_shared<Constant<T>>(g), dofs, V)
305 {
306 }
307
323 template <typename X>
324 requires std::is_convertible_v<std::remove_cvref_t<X>,
325 std::vector<std::int32_t>>
326 DirichletBC(std::shared_ptr<const Constant<T>> g, X&& dofs,
327 std::shared_ptr<const FunctionSpace<U>> V)
328 : _function_space(V), _g(g), _dofs0(std::forward<X>(dofs))
329 {
330 assert(g);
331 assert(V);
332 if (g->shape.size() != V->element()->value_shape().size())
333 {
334 throw std::runtime_error(
335 "Rank mismatch between Constant and function space in DirichletBC");
336 }
337
338 if (g->value.size() != (std::size_t)_function_space->dofmap()->bs())
339 {
340 throw std::runtime_error(
341 "Creating a DirichletBC using a Constant is not supported when the "
342 "Constant size is not equal to the block size of the constrained "
343 "(sub-)space. Use a fem::Function to create the fem::DirichletBC.");
344 }
345
346 if (!V->element()->interpolation_ident())
347 {
348 throw std::runtime_error(
349 "Constant can be used only with point-evaluation elements");
350 }
351
352 // Unroll _dofs0 if dofmap block size > 1
353 if (const int bs = V->dofmap()->bs(); bs > 1)
354 _dofs0 = unroll_dofs(_dofs0, bs);
355
356 _owned_indices0 = num_owned(*_function_space->dofmap(), _dofs0);
357 }
358
371 template <typename X>
372 requires std::is_convertible_v<std::remove_cvref_t<X>,
373 std::vector<std::int32_t>>
374 DirichletBC(std::shared_ptr<const Function<T, U>> g, X&& dofs)
375 : _function_space(g->function_space()), _g(g),
376 _dofs0(std::forward<X>(dofs))
377 {
378 assert(_function_space);
379
380 // Unroll _dofs0 if dofmap block size > 1
381 if (const int bs = _function_space->dofmap()->bs(); bs > 1)
382 _dofs0 = unroll_dofs(_dofs0, bs);
383
384 _owned_indices0 = num_owned(*_function_space->dofmap(), _dofs0);
385 }
386
408 template <typename X>
409 DirichletBC(std::shared_ptr<const Function<T, U>> g, X&& V_g_dofs,
410 std::shared_ptr<const FunctionSpace<U>> V)
411 : _function_space(V), _g(g),
412 _dofs0(std::forward<typename std::remove_reference_t<X>::value_type>(
413 V_g_dofs[0])),
414 _dofs1_g(std::forward<typename std::remove_reference_t<X>::value_type>(
415 V_g_dofs[1])),
416 _owned_indices0(num_owned(*_function_space->dofmap(), _dofs0))
417 {
418 }
419
422 DirichletBC(const DirichletBC& bc) = default;
423
426 DirichletBC(DirichletBC&& bc) = default;
427
429 ~DirichletBC() = default;
430
433 DirichletBC& operator=(const DirichletBC& bc) = default;
434
437
440 std::shared_ptr<const FunctionSpace<U>> function_space() const
441 {
442 return _function_space;
443 }
444
447 std::variant<std::shared_ptr<const Function<T, U>>,
448 std::shared_ptr<const Constant<T>>>
449 value() const
450 {
451 return _g;
452 }
453
460 std::pair<std::span<const std::int32_t>, std::int32_t> dof_indices() const
461 {
462 return {_dofs0, _owned_indices0};
463 }
464
487 void set(std::span<T> x, std::optional<std::span<const T>> x0,
488 T alpha = 1) const
489 {
490 // set_fn is a lambda which gets evaluated for every index in [0,
491 // _dofs0.size()) and its result is assigned to x[_dofs0[i]].
492 auto apply = [&](std::invocable<std::int32_t> auto set_fn)
493 {
494 static_assert(
495 std::is_same_v<std::invoke_result_t<decltype(set_fn), std::int32_t>,
496 T>);
497
498 std::int32_t x_size = x.size();
499 for (std::size_t i = 0; i < _dofs0.size(); ++i)
500 {
501 if (_dofs0[i] < x_size)
502 x[_dofs0[i]] = set_fn(i);
503 }
504 };
505
506 if (alpha == T(0)) // Optimisation for when alpha == 0
507 {
508 apply([](std::int32_t) -> T { return 0; });
509 return;
510 }
511
512 if (std::holds_alternative<std::shared_ptr<const Function<T, U>>>(_g))
513 {
514 auto g = std::get<std::shared_ptr<const Function<T, U>>>(_g);
515 assert(g);
516 std::span<const T> values = g->x()->array();
517
518 // Extract degrees of freedom associated with g. If g is in a collapsed
519 // sub-space, get the dofs in this space, otherwise the degrees of g is
520 // the same as for x
521 auto dofs_g = _dofs1_g.empty() ? std::span(_dofs0) : std::span(_dofs1_g);
522
523 if (x0)
524 {
525 assert(x.size() <= x0->size());
526 apply(
527 [dofs_g, x0 = *x0, alpha, values,
528 &dofs0 = this->_dofs0](std::int32_t i) -> T
529 {
530 assert(dofs_g[i] < static_cast<std::int32_t>(values.size()));
531 return alpha * (values[dofs_g[i]] - x0[dofs0[i]]);
532 });
533 }
534 else
535 {
536 apply(
537 [dofs_g, values, alpha](std::int32_t i) -> T
538 {
539 assert(dofs_g[i] < static_cast<std::int32_t>(values.size()));
540 return alpha * values[dofs_g[i]];
541 });
542 }
543 }
544 else if (std::holds_alternative<std::shared_ptr<const Constant<T>>>(_g))
545 {
546 auto g = std::get<std::shared_ptr<const Constant<T>>>(_g);
547 const std::vector<T>& value = g->value;
548 std::int32_t bs = _function_space->dofmap()->bs();
549 if (x0)
550 {
551 assert(x.size() <= x0->size());
552 apply(
553 [x0 = *x0, alpha, bs, &value, &dofs0 = _dofs0](std::int32_t i) -> T
554 {
555 auto dof = dofs0[i];
556 return alpha * (value[dof % bs] - x0[dof]);
557 });
558 }
559 else
560 {
561 apply([alpha, bs, &value, &dofs0 = _dofs0](std::int32_t i) -> T
562 { return alpha * value[dofs0[i] % bs]; });
563 }
564 }
565 else
566 {
567 // replace with std::unreachable once C++23 is supported
568 assert(false);
569 }
570 }
571
581 void mark_dofs(std::span<std::int8_t> markers) const
582 {
583 for (std::int32_t idx : _dofs0)
584 {
585 assert(idx < (std::int32_t)markers.size());
586 markers[idx] = true;
587 }
588 }
589
590private:
591 // The function space (possibly a sub function space)
592 std::shared_ptr<const FunctionSpace<U>> _function_space;
593
594 // The function
595 std::variant<std::shared_ptr<const Function<T, U>>,
596 std::shared_ptr<const Constant<T>>>
597 _g;
598
599 // Dof indices (_dofs0) in _function_space and (_dofs1_g) in the space
600 // of _g. _dofs1_g may be empty if _dofs0 can be re-used
601 std::vector<std::int32_t> _dofs0, _dofs1_g;
602
603 // The first _owned_indices in _dofs are owned by this process
604 std::int32_t _owned_indices0 = -1;
605};
606} // namespace dolfinx::fem
Degree-of-freedom map representations and tools.
Constant (in space) value which can be attached to a Form.
Definition Constant.h:22
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:303
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:326
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:487
std::variant< std::shared_ptr< const Function< T, U > >, std::shared_ptr< const Constant< T > > > value() const
Definition DirichletBC.h:449
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:460
std::shared_ptr< const FunctionSpace< U > > function_space() const
Definition DirichletBC.h:440
~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:374
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:581
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:409
Degree-of-freedom map.
Definition DofMap.h:73
std::shared_ptr< const common::IndexMap > index_map
Index map that describes the parallel distribution of the dofmap.
Definition DofMap.h:164
int index_map_bs() const
Block size associated with the index_map.
Definition DofMap.cpp:276
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:361
std::shared_ptr< const mesh::Mesh< geometry_type > > mesh() const
The mesh.
Definition FunctionSpace.h:336
std::shared_ptr< const FiniteElement< geometry_type > > element() const
The finite element.
Definition FunctionSpace.h:342
std::vector< geometry_type > tabulate_dof_coordinates(bool transpose) const
Tabulate the physical coordinates of all dofs on this process.
Definition FunctionSpace.h:208
Definition Function.h:47
Finite element method functionality.
Definition assemble_expression_impl.h:23
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:188
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32