version 0.4.1
functionhelper.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2021-2024 The Ikarus Developers mueller@ibb.uni-stuttgart.de
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
9#pragma once
10
11#include <ranges>
12
13#include <dune/common/float_cmp.hh>
14#include <dune/grid/utility/hierarchicsearch.hh>
15
18
19namespace Ikarus::utils {
29template <int size, typename LV>
30void obtainLagrangeGlobalNodePositions(const LV& localView,
31 std::vector<Dune::FieldVector<double, size>>& lagrangeNodeGlobalCoords) {
32 auto fT = [&]([[maybe_unused]] int nodeNumber, Dune::FieldVector<double, size>&& localCoordinate) {
33 lagrangeNodeGlobalCoords.emplace_back(localView.element().geometry().global(localCoordinate));
34 return false;
35 };
36 forEachLagrangeNodePosition(localView, fT);
37}
38
49template <int size, typename Basis>
51 static_assert(Concepts::LagrangeNode<std::remove_cvref_t<decltype(basis.localView().tree().child(0))>>,
52 "globalIndexFromGlobalPosition is only supported for Lagrange basis");
53 constexpr double tol = 1e-8;
54 using LocalView = std::remove_cvref_t<decltype(basis.localView())>;
55 using MultiIndex = typename LocalView::MultiIndex;
56 Dune::HierarchicSearch hSearch(basis.gridView().grid(), basis.gridView().indexSet());
57 const auto& ele = hSearch.findEntity(pos);
58 auto localView = basis.localView();
59 localView.bind(ele);
60 const auto geo = localView.element().geometry();
61 const auto& node = localView.tree();
62 std::optional<std::array<MultiIndex, size>> globalIndices;
63
64 auto fT = [&](int nodeNumber, Dune::FieldVector<double, size>&& localCoordinate) {
65 if (Dune::FloatCmp::eq(geo.global(localCoordinate), pos, tol)) {
66 globalIndices.emplace();
67 for (int j = 0; j < size; j++)
68 globalIndices.value()[j] = localView.index(node.child(j).localIndex(nodeNumber));
69 return true;
70 }
71 return false;
72 };
73 forEachLagrangeNodePosition(localView, fT);
74 if (not globalIndices.has_value())
75 DUNE_THROW(Dune::InvalidStateException, "No Lagrange node found at the given position in the grid.");
76 return globalIndices.value();
77}
78
87template <typename FE>
89 constexpr int dim = FE::Traits::mydim;
90 const auto& element = fe.gridElement();
91 const auto& referenceElement = Dune::referenceElement<double, dim>(element.type());
92 const int numberOfVertices = referenceElement.size(codim);
93
94 auto getPosition = [=](const int i) { return referenceElement.position(i, codim); };
95 return std::views::transform(std::views::iota(0, numberOfVertices), getPosition);
96}
97
105template <typename FE>
108}
109
110} // namespace Ikarus::utils
Contains functions to traverse through a tree to its different nodes.
Several concepts.
auto referenceElementSubEntityPositions(FE &fe, int codim)
A function to obtain the local coordinates of subentities of an FiniteElement.
Definition: functionhelper.hh:88
auto referenceElementVertexPositions(FE &fe)
A function to obtain the local coordinates the vertices of an FiniteElement.
Definition: functionhelper.hh:106
auto globalIndexFromGlobalPosition(const Basis &basis, const Dune::FieldVector< double, size > &pos)
A helper function to obtain the global index from the global positions for a Lagrange node.
Definition: functionhelper.hh:50
void obtainLagrangeGlobalNodePositions(const LV &localView, std::vector< Dune::FieldVector< double, size > > &lagrangeNodeGlobalCoords)
A function to obtain the global positions of the nodes of an element with Lagrangian basis.
Definition: functionhelper.hh:30
void globalIndices(const FiniteElement &fe, std::vector< typename FiniteElement::LocalView::MultiIndex > &globalIndices)
Get the global indices for the provided finite element.
Definition: fehelper.hh:127
Definition: algorithms.hh:17
void forEachLagrangeNodePosition(const LV &localView, F &&f)
A helper function that helps in traversing over the local coordinates of an element and call a user-d...
Definition: traversal.hh:65
def basis(gv, tree)
Definition: basis.py:10
FE class is a base class for all finite elements.
Definition: febase.hh:79
const GridElement & gridElement() const
Get the grid element associated with the local view.
Definition: febase.hh:128
static constexpr int mydim
Dimension of the geometry.
Definition: fetraits.hh:63
Definition: utils/dirichletvalues.hh:32
Concept to check if a node in a basis tree is a Lagrangian node.
Definition: concepts.hh:82