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 = MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
121 const T,
122 MDSPAN_IMPL_STANDARD_NAMESPACE::extents<
123 std::size_t, 3, MDSPAN_IMPL_STANDARD_NAMESPACE::dynamic_extent>>;
124
125 // Compute marker for each dof coordinate
126 cmdspan3x_t x(dof_coordinates.data(), 3, dof_coordinates.size() / 3);
127 const std::vector<std::int8_t> marked_dofs = marker_fn(x);
128
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)
132 {
133 if (marked_dofs[i])
134 dofs.push_back(i);
135 }
136
137 return dofs;
138}
139
153template <std::floating_point T, typename U>
154std::array<std::vector<std::int32_t>, 2> locate_dofs_geometrical(
155 const std::array<std::reference_wrapper<const FunctionSpace<T>>, 2>& V,
156 U marker_fn)
157{
158 // FIXME: Calling V.tabulate_dof_coordinates() is very expensive,
159 // especially when we usually want the boundary dofs only. Add
160 // interface that computes dofs coordinates only for specified cell.
161
162 // Get function spaces
163 const FunctionSpace<T>& V0 = V.at(0).get();
164 const FunctionSpace<T>& V1 = V.at(1).get();
165
166 // Get mesh
167 auto mesh = V0.mesh();
168 assert(mesh);
169 assert(V1.mesh());
170 if (mesh != V1.mesh())
171 throw std::runtime_error("Meshes are not the same.");
172 const int tdim = mesh->topology()->dim();
173
174 assert(V0.element());
175 assert(V1.element());
176 if (*V0.element() != *V1.element())
177 throw std::runtime_error("Function spaces must have the same element.");
178
179 // Compute dof coordinates
180 const std::vector<T> dof_coordinates = V1.tabulate_dof_coordinates(true);
181
182 using cmdspan3x_t = MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
183 const T,
184 MDSPAN_IMPL_STANDARD_NAMESPACE::extents<
185 std::size_t, 3, MDSPAN_IMPL_STANDARD_NAMESPACE::dynamic_extent>>;
186
187 // Evaluate marker for each dof coordinate
188 cmdspan3x_t x(dof_coordinates.data(), 3, dof_coordinates.size() / 3);
189 const std::vector<std::int8_t> marked_dofs = marker_fn(x);
190
191 // Get dofmaps
192 std::shared_ptr<const DofMap> dofmap0 = V0.dofmap();
193 assert(dofmap0);
194 const int bs0 = dofmap0->bs();
195 std::shared_ptr<const DofMap> dofmap1 = V1.dofmap();
196 assert(dofmap1);
197 const int bs1 = dofmap1->bs();
198
199 const int element_bs = dofmap0->element_dof_layout().block_size();
200 assert(element_bs == dofmap1->element_dof_layout().block_size());
201
202 // Iterate over cells
203 auto topology = mesh->topology();
204 assert(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)
207 {
208 // Get cell dofmaps
209 auto cell_dofs0 = dofmap0->cell_dofs(c);
210 auto cell_dofs1 = dofmap1->cell_dofs(c);
211
212 // Loop over cell dofs and add to bc_dofs if marked.
213 for (std::size_t i = 0; i < cell_dofs1.size(); ++i)
214 {
215 if (marked_dofs[cell_dofs1[i]])
216 {
217 // Unroll over blocks
218 for (int k = 0; k < element_bs; ++k)
219 {
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});
228 }
229 }
230 }
231 }
232
233 // Remove duplicates
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);
237
238 // Copy to separate array
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]; });
245
246 return dofs;
247}
248
257template <dolfinx::scalar T,
258 std::floating_point U = dolfinx::scalar_value_type_t<T>>
260{
261private:
264 std::size_t num_owned(const DofMap& dofmap,
265 std::span<const std::int32_t> dofs)
266 {
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);
272 }
273
275 static std::vector<std::int32_t>
276 unroll_dofs(std::span<const std::int32_t> dofs, int bs)
277 {
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;
283 }
284
285public:
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>>
308 DirichletBC(const S& g, X&& dofs, std::shared_ptr<const FunctionSpace<U>> V)
309 : DirichletBC(std::make_shared<Constant<T>>(g), dofs, V)
310 {
311 }
312
328 template <typename X>
329 requires std::is_convertible_v<std::remove_cvref_t<X>,
330 std::vector<std::int32_t>>
331 DirichletBC(std::shared_ptr<const Constant<T>> g, X&& dofs,
332 std::shared_ptr<const FunctionSpace<U>> V)
333 : _function_space(V), _g(g), _dofs0(std::forward<X>(dofs)),
334 _owned_indices0(num_owned(*V->dofmap(), _dofs0))
335 {
336 assert(g);
337 assert(V);
338 if (g->shape.size() != V->element()->value_shape().size())
339 {
340 throw std::runtime_error(
341 "Rank mis-match between Constant and function space in DirichletBC");
342 }
343
344 if (g->value.size() != _function_space->dofmap()->bs())
345 {
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.");
350 }
351
352 if (!V->element()->interpolation_ident())
353 {
354 throw std::runtime_error(
355 "Constant can be used only with point-evaluation elements");
356 }
357
358 // Unroll _dofs0 if dofmap block size > 1
359 if (const int bs = V->dofmap()->bs(); bs > 1)
360 {
361 _owned_indices0 *= bs;
362 _dofs0 = unroll_dofs(_dofs0, bs);
363 }
364 }
365
378 template <typename X>
379 requires std::is_convertible_v<std::remove_cvref_t<X>,
380 std::vector<std::int32_t>>
381 DirichletBC(std::shared_ptr<const Function<T, U>> g, X&& dofs)
382 : _function_space(g->function_space()), _g(g),
383 _dofs0(std::forward<X>(dofs)),
384 _owned_indices0(num_owned(*_function_space->dofmap(), _dofs0))
385 {
386 assert(_function_space);
387
388 // Unroll _dofs0 if dofmap block size > 1
389 if (const int bs = _function_space->dofmap()->bs(); bs > 1)
390 {
391 _owned_indices0 *= bs;
392 _dofs0 = unroll_dofs(_dofs0, bs);
393 }
394 }
395
417 template <typename X>
418 DirichletBC(std::shared_ptr<const Function<T, U>> g, X&& V_g_dofs,
419 std::shared_ptr<const FunctionSpace<U>> V)
420 : _function_space(V), _g(g),
421 _dofs0(std::forward<typename std::remove_reference_t<X>::value_type>(
422 V_g_dofs[0])),
423 _dofs1_g(std::forward<typename std::remove_reference_t<X>::value_type>(
424 V_g_dofs[1])),
425 _owned_indices0(num_owned(*_function_space->dofmap(), _dofs0))
426 {
427 }
428
431 DirichletBC(const DirichletBC& bc) = default;
432
435 DirichletBC(DirichletBC&& bc) = default;
436
438 ~DirichletBC() = default;
439
442 DirichletBC& operator=(const DirichletBC& bc) = default;
443
446
449 std::shared_ptr<const FunctionSpace<U>> function_space() const
450 {
451 return _function_space;
452 }
453
456 std::variant<std::shared_ptr<const Function<T, U>>,
457 std::shared_ptr<const Constant<T>>>
458 value() const
459 {
460 return _g;
461 }
462
469 std::pair<std::span<const std::int32_t>, std::int32_t> dof_indices() const
470 {
471 return {_dofs0, _owned_indices0};
472 }
473
496 void set(std::span<T> x, std::optional<std::span<const T>> x0,
497 T alpha = 1) const
498 {
499 // set_fn is a lambda which gets evaluated for every index in [0,
500 // _dofs0.size()) and its result is assigned to x[_dofs0[i]].
501 auto apply = [&](std::invocable<std::int32_t> auto set_fn)
502 {
503 static_assert(
504 std::is_same_v<std::invoke_result_t<decltype(set_fn), std::int32_t>,
505 T>);
506
507 std::int32_t x_size = x.size();
508 for (std::size_t i = 0; i < _dofs0.size(); ++i)
509 {
510 if (_dofs0[i] < x_size)
511 x[_dofs0[i]] = set_fn(i);
512 }
513 };
514
515 if (alpha == T(0)) // Optimisation for when alpha == 0
516 {
517 apply([](std::int32_t) -> T { return 0; });
518 return;
519 }
520
521 if (std::holds_alternative<std::shared_ptr<const Function<T, U>>>(_g))
522 {
523 auto g = std::get<std::shared_ptr<const Function<T, U>>>(_g);
524 assert(g);
525 std::span<const T> values = g->x()->array();
526
527 // Extract degrees of freedom associated with g. If g is in a collapsed
528 // sub-space, get the dofs in this space, otherwise the degrees of g is
529 // the same as for x
530 auto dofs_g = _dofs1_g.empty() ? std::span(_dofs0) : std::span(_dofs1_g);
531
532 if (x0)
533 {
534 assert(x.size() <= x0->size());
535 apply(
536 [dofs_g, x0 = *x0, alpha, values,
537 &dofs0 = this->_dofs0](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]] - x0[dofs0[i]]);
541 });
542 }
543 else
544 {
545 apply(
546 [dofs_g, values, alpha](std::int32_t i) -> T
547 {
548 assert(dofs_g[i] < static_cast<std::int32_t>(values.size()));
549 return alpha * values[dofs_g[i]];
550 });
551 }
552 }
553 else if (std::holds_alternative<std::shared_ptr<const Constant<T>>>(_g))
554 {
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();
558 if (x0)
559 {
560 assert(x.size() <= x0->size());
561 apply(
562 [x0 = *x0, alpha, bs, &value, &dofs0 = _dofs0](std::int32_t i) -> T
563 {
564 auto dof = dofs0[i];
565 return alpha * (value[dof % bs] - x0[dof]);
566 });
567 }
568 else
569 {
570 apply([alpha, bs, &value, &dofs0 = _dofs0](std::int32_t i) -> T
571 { return alpha * value[dofs0[i] % bs]; });
572 }
573 }
574 else
575 {
576 // replace with std::unreachable once C++23 is supported
577 assert(false);
578 }
579 }
580
590 void mark_dofs(std::span<std::int8_t> markers) const
591 {
592 for (std::int32_t idx : _dofs0)
593 {
594 assert(idx < (std::int32_t)markers.size());
595 markers[idx] = true;
596 }
597 }
598
599private:
600 // The function space (possibly a sub function space)
601 std::shared_ptr<const FunctionSpace<U>> _function_space;
602
603 // The function
604 std::variant<std::shared_ptr<const Function<T, U>>,
605 std::shared_ptr<const Constant<T>>>
606 _g;
607
608 // Dof indices (_dofs0) in _function_space and (_dofs1_g) in the space
609 // of _g. _dofs1_g may be empty if _dofs0 can be re-used
610 std::vector<std::int32_t> _dofs0, _dofs1_g;
611
612 // The first _owned_indices in _dofs are owned by this process
613 std::int32_t _owned_indices0 = -1;
614};
615} // namespace dolfinx::fem
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
Definition XDMFFile.h:31
Topology stores the topology of a mesh, consisting of mesh entities and connectivity (incidence relat...
Definition Topology.h:45
Definition types.h:20
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