Loading...
Searching...
No Matches
facenormalgridfunction.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
#ifndef DUNE_FUNCTIONS_GRIDFUNCTIONS_FACENORMALGRIDFUNCTION_HH
#define DUNE_FUNCTIONS_GRIDFUNCTIONS_FACENORMALGRIDFUNCTION_HH
#include <type_traits>
#include <optional>
#include <dune/common/exceptions.hh>
#include <dune/common/typeutilities.hh>
#include <dune/common/rangeutilities.hh>
#include <dune/geometry/referenceelements.hh>
#include <dune/functions/gridfunctions/gridfunction.hh>
#include <dune/functions/gridfunctions/gridviewentityset.hh>
namespace Dune::Functions {
namespace Impl {
// Compute closest face to point
template<class ReferenceElement, class Coordinate>
auto closestFaceIndex(const ReferenceElement& re, const Coordinate& x)
{
auto closestFaceIndex = decltype(re.subEntity(0,1,0,1)){};
double closestFaceDistance = std::numeric_limits<double>::max();
for(auto&& faceIndex : Dune::range(re.size(1)))
{
// For a face unit outer normal consider the orthogonal projection
// Px = x + <c-x,n>*n into the face. Then the distance to the face
// is given by |x-Px| = |<c-x,n>||n| = <c-x,n>.
auto normal = re.integrationOuterNormal(faceIndex);
normal /= normal.two_norm();
auto c = re.position(faceIndex,1);
c -= x;
auto faceDistance = (c*normal);
if (faceDistance<closestFaceDistance)
{
closestFaceDistance = faceDistance;
closestFaceIndex = faceIndex;
}
}
return closestFaceIndex;
}
} // end namespace Impl
template<class GV>
class FaceNormalGridFunction
{
public:
using GridView = GV;
using EntitySet = GridViewEntitySet<GridView, 0>;
using Element = typename EntitySet::Element;
using LocalDomain = typename EntitySet::LocalCoordinate;
using Domain = typename EntitySet::GlobalCoordinate;
using Range = typename EntitySet::GlobalCoordinate;
private:
using Traits = Imp::GridFunctionTraits<Range(Domain), EntitySet, DefaultDerivativeTraits, 16>;
class LocalFunction
{
using Geometry = typename Element::Geometry;
static const int dimension = GV::dimension;
public:
void bind(const Element& element)
{
element_ = element;
geometry_.emplace(element_.geometry());
}
void unbind()
{
geometry_.reset();
}
bool bound() const
{
return static_cast<bool>(geometry_);
}
Range operator()(const LocalDomain& x) const
{
auto&& re = Dune::referenceElement(*geometry_);
// Compute reference normal of closest face to given point
auto face = Impl::closestFaceIndex(re, x);
auto localNormal = re.integrationOuterNormal(face);
// Transform reference normal into global unit outer normal using
// covariant Piola transformation
auto normal = Range{};
geometry_->jacobianInverseTransposed(x).mv(localNormal, normal);
normal /= normal.two_norm();
return normal;
}
const Element& localContext() const
{
return element_;
}
friend typename Traits::LocalFunctionTraits::DerivativeInterface derivative(const LocalFunction& t)
{
DUNE_THROW(NotImplemented,"not implemented");
}
private:
std::optional<Geometry> geometry_;
Element element_;
};
public:
FaceNormalGridFunction(const GridView& gridView) :
entitySet_(gridView)
{}
Range operator()(const Domain& x) const
{
DUNE_THROW(NotImplemented,"not implemented");
}
friend typename Traits::DerivativeInterface derivative(const FaceNormalGridFunction& t)
{
DUNE_THROW(NotImplemented,"not implemented");
}
friend LocalFunction localFunction(const FaceNormalGridFunction& t)
{
return LocalFunction{};
}
const EntitySet& entitySet() const
{
return entitySet_;
}
private:
EntitySet entitySet_;
};
} // namespace Dune::Functions
#endif // DUNE_FUNCTIONS_GRIDFUNCTIONS_FACENORMALGRIDFUNCTION_HH
gridfunction.hh
gridviewentityset.hh
Dune::Functions
Definition monomialset.hh:19
Dune::Functions::DefaultDerivativeTraits
Default implementation for derivative traits.
Definition defaultderivativetraits.hh:41
Dune::Functions::FaceNormalGridFunction::EntitySet
GridViewEntitySet< GridView, 0 > EntitySet
Definition facenormalgridfunction.hh:73
Dune::Functions::FaceNormalGridFunction::LocalDomain
typename EntitySet::LocalCoordinate LocalDomain
Definition facenormalgridfunction.hh:76
Dune::Functions::FaceNormalGridFunction::Domain
typename EntitySet::GlobalCoordinate Domain
Definition facenormalgridfunction.hh:77
Dune::Functions::FaceNormalGridFunction::Range
typename EntitySet::GlobalCoordinate Range
Definition facenormalgridfunction.hh:78
Dune::Functions::FaceNormalGridFunction::entitySet
const EntitySet & entitySet() const
Return the stored GridViewEntitySet.
Definition facenormalgridfunction.hh:185
Dune::Functions::FaceNormalGridFunction::derivative
friend Traits::DerivativeInterface derivative(const FaceNormalGridFunction &t)
Not implemented.
Definition facenormalgridfunction.hh:173
Dune::Functions::FaceNormalGridFunction::GridView
GV GridView
Definition facenormalgridfunction.hh:72
Dune::Functions::FaceNormalGridFunction::localFunction
friend LocalFunction localFunction(const FaceNormalGridFunction &t)
Return a local-function associated to FaceNormalGridFunction.
Definition facenormalgridfunction.hh:179
Dune::Functions::FaceNormalGridFunction::FaceNormalGridFunction
FaceNormalGridFunction(const GridView &gridView)
Construct the FaceNormalGridFunction.
Definition facenormalgridfunction.hh:162
Dune::Functions::FaceNormalGridFunction::Element
typename EntitySet::Element Element
Definition facenormalgridfunction.hh:74
Dune::Functions::FaceNormalGridFunction::operator()
Range operator()(const Domain &x) const
Not implemented.
Definition facenormalgridfunction.hh:167
Dune::Functions::GridViewEntitySet
An entity set for all entities of given codim in a grid view.
Definition gridviewentityset.hh:27
Dune::Functions::GridViewEntitySet< GridView, 0 >::Element
GridView::template Codim< codim >::Entity Element
Definition gridviewentityset.hh:36
Dune::Functions::GridViewEntitySet< GridView, 0 >::LocalCoordinate
Element::Geometry::LocalCoordinate LocalCoordinate
Definition gridviewentityset.hh:39
Dune::Functions::GridViewEntitySet< GridView, 0 >::GlobalCoordinate
Element::Geometry::GlobalCoordinate GlobalCoordinate
Definition gridviewentityset.hh:40