1#ifndef DUNE_FEM_LINESEGMENTSAMPLER_HH
2#define DUNE_FEM_LINESEGMENTSAMPLER_HH
12#include <dune/geometry/referenceelements.hh>
14#include <dune/fem/function/localfunction/const.hh>
38 template<
class Gr
idPart >
41 typedef GridPart GridPartType;
43 typedef typename GridPartType::GridType::ctype DomainFieldType;
45 static const int dimDomain = GridPartType::dimensionworld;
46 static const int dimGrid = GridPartType::dimension;
52 static_assert( dimDomain == dimGrid,
"LineSegmentSampler supports only flat grids." );
54 template<
class Vector >
71 : gridPart_(
gridPart ), left_( left ), right_( right )
84 template<
class Gr
idFunction >
85 void operator() (
const GridFunction &f, std::vector< typename GridFunction::RangeType > &samples, std::nothrow_t )
const;
86 template<
class Gr
idFunction >
87 void operator() (
const GridFunction &f, std::vector< typename GridFunction::RangeType > &samples )
const;
98 void samplePoints( std::vector< DomainType > &points )
const;
101 const GridPart &
gridPart ()
const {
return gridPart_; }
104 const GridPart &gridPart_;
105 DomainType left_, right_;
113 template<
class Gr
idPart >
114 template<
class Vector >
115 struct LineSegmentSampler< GridPart >::Reduce
117 Vector
operator() (
const Vector &a,
const Vector &b )
const
120 for(
int k = 0; k < Vector::dimension; ++k )
121 c[ k ] = std::min( a[ k ], b[ k ] );
131 template<
class Gr
idPart >
132 template<
class Gr
idFunction >
134 ::operator() (
const GridFunction &f, std::vector< typename GridFunction::RangeType > &samples,
135 std::nothrow_t)
const
139 typedef typename GridFunction::RangeFieldType RangeFieldType;
141 const int numSamples = samples.size();
143 ds /= DomainFieldType( numSamples - 1 );
145 const RangeFieldType invalid
146 = std::numeric_limits< RangeFieldType >::quiet_NaN();
147 for(
int i = 0; i < numSamples; ++i )
148 samples[ i ] =
typename GridFunction::RangeType( invalid );
150 ConstLocalFunction< GridFunction > lf( f );
151 const IteratorType end = gridPart().template end< 0 >();
152 for( IteratorType it = gridPart().
template begin< 0 >(); it != end; ++it )
154 const EntityType &entity = *it;
155 const typename EntityType::Geometry &geometry = entity.geometry();
161 const int numFaces = refElement.size( 1 );
162 for(
int face = 0; face < numFaces; ++face )
167 const LocalDomainType &refNormal = refElement.integrationOuterNormal( face );
168 geometry.jacobianInverseTransposed( center ).mv( refNormal, normal );
170 const DomainFieldType nds = normal * ds;
171 const DomainFieldType ncl = normal * (geometry.global( center ) - left_);
172 if( std::abs( nds ) > 1e-8 )
175 const DomainFieldType alpha = ncl / nds;
177 upper = std::min( upper, alpha );
179 lower = std::max( lower, alpha );
181 else if( ncl < -1e-8 )
192 const int i_upper = std::min(
int( std::floor( upper + 1e-8 ) ), numSamples - 1 );
193 const int i_lower = std::max(
int( std::ceil( lower - 1e-8 ) ), 0 );
194 for(
int i = i_lower; i <= i_upper; ++i )
197 xi.
axpy( DomainFieldType( i ), ds );
198 lf.evaluate( geometry.local( xi ), samples[ i ] );
204 typedef Reduce< typename GridFunction::RangeType > Op;
205 gridPart().comm().template allreduce< Op >( &(samples[ 0 ]), numSamples );
208 template<
class Gr
idPart >
209 template<
class Gr
idFunction >
211 ::operator() (
const GridFunction &f, std::vector< typename GridFunction::RangeType > &samples )
const
213 typedef typename GridFunction::RangeFieldType RangeFieldType;
215 const int numSamples = samples.size();
219 (*this)(f,samples,std::nothrow);
224 if constexpr ( std::is_floating_point< RangeFieldType >::value )
226 for(
int i = 0; i < numSamples; ++i )
228 for(
int d=0; d<GridFunction::RangeType::dimension; ++d )
230 valid &= ! std::isnan( samples[ i ][ d ] );
236 DUNE_THROW( InvalidStateException,
"LineSegmentSampler could not find all samples." );
240 template<
class Gr
idPart >
244 const int numSamples = points.size();
248 ds /= DomainFieldType( numSamples - 1 );
250 for(
int i = 0; i < numSamples; ++i )
253 points[ i ].axpy( DomainFieldType( i ), ds );
derived_type & axpy(const field_type &a, const DenseVector< Other > &x)
vector space axpy operation ( *this += a x )
Definition: densevector.hh:575
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:281
Implements a vector constructed from a given type representing a field and a compile-time given size.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:484
constexpr auto min
Function object that returns the smaller of the given values.
Definition: hybridutilities.hh:506
Dune namespace.
Definition: alignedallocator.hh:13
Static tag representing a codimension.
Definition: dimension.hh:24
samples values of a discrete function along a given line segment
Definition: linesegmentsampler.hh:40
void samplePoints(std::vector< DomainType > &points) const
returns sampling points
Definition: linesegmentsampler.hh:242
const GridPart & gridPart() const
obtain grid part on which the LineSegmentSampler works
Definition: linesegmentsampler.hh:101
void operator()(const GridFunction &f, std::vector< typename GridFunction::RangeType > &samples, std::nothrow_t) const
sample a given function
Definition: linesegmentsampler.hh:134
LineSegmentSampler(const GridPart &gridPart, const DomainType &left, const DomainType &right)
constructor
Definition: linesegmentsampler.hh:70
Class providing access to the singletons of the reference elements.
Definition: referenceelements.hh:128
static const ReferenceElement & general(const GeometryType &type)
get general reference elements
Definition: referenceelements.hh:156