DUNE-FEM (unstable)

linesegmentsampler.hh
1#ifndef DUNE_FEM_LINESEGMENTSAMPLER_HH
2#define DUNE_FEM_LINESEGMENTSAMPLER_HH
3
4#include <limits>
5#include <vector>
6#include <cmath>
7#include <type_traits>
8#include <new>
9
10#if __GNUC__ >= 13
11#pragma GCC diagnostic push
12#pragma GCC diagnostic ignored "-Wdangling-reference"
13#endif
14
16
17#include <dune/geometry/referenceelements.hh>
18
19#include <dune/fem/function/localfunction/const.hh>
20
21namespace Dune
22{
23
24 namespace Fem
25 {
26
27 // LineSegmentSampler
28 // ------------------
29
43 template< class GridPart >
45 {
46 typedef GridPart GridPartType;
47
48 typedef typename GridPartType::GridType::ctype DomainFieldType;
49
50 static const int dimDomain = GridPartType::dimensionworld;
51 static const int dimGrid = GridPartType::dimension;
52
55
56 private:
57 static_assert( dimDomain == dimGrid, "LineSegmentSampler supports only flat grids." );
58
59 template< class Vector >
60 struct Reduce;
61
63
64 public:
75 LineSegmentSampler ( const GridPart &gridPart, const DomainType &left, const DomainType &right )
76 : gridPart_( gridPart ), left_( left ), right_( right )
77 {}
78
89 template< class GridFunction >
90 void operator() ( const GridFunction &f, std::vector< typename GridFunction::RangeType > &samples, std::nothrow_t ) const;
91 template< class GridFunction >
92 void operator() ( const GridFunction &f, std::vector< typename GridFunction::RangeType > &samples ) const;
93
103 void samplePoints( std::vector< DomainType > &points ) const;
104
106 const GridPart &gridPart () const { return gridPart_; }
107
108 private:
109 const GridPart &gridPart_;
110 DomainType left_, right_;
111 };
112
113
114
115 // LineSegmentSampler::Reduce
116 // --------------------------
117
118 template< class GridPart >
119 template< class Vector >
120 struct LineSegmentSampler< GridPart >::Reduce
121 {
122 Vector operator() ( const Vector &a, const Vector &b ) const
123 {
124 Vector c;
125 for( int k = 0; k < Vector::dimension; ++k )
126 c[ k ] = std::min( a[ k ], b[ k ] );
127 return c;
128 }
129 };
130
131
132
133 // Implementation of LineSegmentSampler
134 // ------------------------------------
135
136 template< class GridPart >
137 template< class GridFunction >
139 ::operator() ( const GridFunction &f, std::vector< typename GridFunction::RangeType > &samples,
140 std::nothrow_t) const
141 {
142 typedef typename GridPartType::template Codim< 0 >::IteratorType IteratorType;
143 typedef typename GridPartType::template Codim< 0 >::EntityType EntityType;
144 typedef typename GridFunction::RangeFieldType RangeFieldType;
145
146 const int numSamples = samples.size();
147 DomainType ds = right_ - left_;
148 ds /= DomainFieldType( numSamples - 1 );
149
150 const RangeFieldType invalid
151 = std::numeric_limits< RangeFieldType >::quiet_NaN();
152 for( int i = 0; i < numSamples; ++i )
153 samples[ i ] = typename GridFunction::RangeType( invalid );
154
155 ConstLocalFunction< GridFunction > lf( f );
156 const IteratorType end = gridPart().template end< 0 >();
157 for( IteratorType it = gridPart().template begin< 0 >(); it != end; ++it )
158 {
159 const EntityType &entity = *it;
160 const typename EntityType::Geometry &geometry = entity.geometry();
161
162 DomainFieldType lower = std::numeric_limits< DomainFieldType >::min();
163 DomainFieldType upper = std::numeric_limits< DomainFieldType >::max();
164
165 auto &refElement = ReferenceElements::general( geometry.type() );
166 const int numFaces = refElement.size( 1 );
167 for( int face = 0; face < numFaces; ++face )
168 {
169 const LocalDomainType &center = refElement.position( face, 1 );
170
171 DomainType normal;
172 const LocalDomainType &refNormal = refElement.integrationOuterNormal( face );
173 geometry.jacobianInverseTransposed( center ).mv( refNormal, normal );
174
175 const DomainFieldType nds = normal * ds;
176 const DomainFieldType ncl = normal * (geometry.global( center ) - left_);
177 if( std::abs( nds ) > 1e-8 )
178 {
179 // ds is not parallel to the face
180 const DomainFieldType alpha = ncl / nds;
181 if( nds > 0 )
182 upper = std::min( upper, alpha );
183 else
184 lower = std::max( lower, alpha );
185 }
186 else if( ncl < -1e-8 )
187 {
188 // ds is parallel to the face and the line lies on the outside
191 }
192 }
193
194 if( lower <= upper )
195 {
196 lf.bind( entity );
197 const int i_upper = std::min( int( std::floor( upper + 1e-8 ) ), numSamples - 1 );
198 const int i_lower = std::max( int( std::ceil( lower - 1e-8 ) ), 0 );
199 for( int i = i_lower; i <= i_upper; ++i )
200 {
201 DomainType xi = left_;
202 xi.axpy( DomainFieldType( i ), ds );
203 lf.evaluate( geometry.local( xi ), samples[ i ] );
204 }
205 lf.unbind();
206 }
207 }
208
209 typedef Reduce< typename GridFunction::RangeType > Op;
210 gridPart().comm().template allreduce< Op >( &(samples[ 0 ]), numSamples );
211 }
212
213 template< class GridPart >
214 template< class GridFunction >
216 ::operator() ( const GridFunction &f, std::vector< typename GridFunction::RangeType > &samples ) const
217 {
218 typedef typename GridFunction::RangeFieldType RangeFieldType;
219
220 const int numSamples = samples.size();
221 if( numSamples < 2 )
222 DUNE_THROW( InvalidStateException, "LineSegmentSampler cannot sample less than 2 points." );
223
224 (*this)(f,samples,std::nothrow);
225
226 bool valid = true;
227
228 // only use isnan when field type is a floating point
229 if constexpr ( std::is_floating_point< RangeFieldType >::value )
230 {
231 for( int i = 0; i < numSamples; ++i )
232 {
233 for( int d=0; d<GridFunction::RangeType::dimension; ++d )
234 {
235 valid &= ! std::isnan( samples[ i ][ d ] );
236 }
237 }
238 }
239
240 if( !valid )
241 DUNE_THROW( InvalidStateException, "LineSegmentSampler could not find all samples." );
242 }
243
244
245 template< class GridPart >
247 :: samplePoints ( std::vector< DomainType > &points ) const
248 {
249 const int numSamples = points.size();
250 if( numSamples < 2 )
251 DUNE_THROW( InvalidStateException, "LineSegmentSampler cannot sample less than 2 points." );
252 DomainType ds = right_ - left_;
253 ds /= DomainFieldType( numSamples - 1 );
254
255 for( int i = 0; i < numSamples; ++i )
256 {
257 points[ i ] = left_;
258 points[ i ].axpy( DomainFieldType( i ), ds );
259 }
260 }
261
262 } // namespace Fem
263
264} // namespace Dune
265
266#if __GNUC__ >= 13
267#pragma GCC diagnostic pop
268#endif
269
270#endif // #ifndef DUNE_FEM_LINESEGMENTSAMPLER_HH
constexpr derived_type & axpy(const field_type &a, const DenseVector< Other > &x)
vector space axpy operation ( *this += a x )
Definition: densevector.hh:576
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:375
Implements a vector constructed from a given type representing a field and a compile-time given size.
#define DUNE_THROW(E,...)
Definition: exceptions.hh:314
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:485
constexpr auto min
Function object that returns the smaller of the given values.
Definition: hybridutilities.hh:507
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:45
void samplePoints(std::vector< DomainType > &points) const
returns sampling points
Definition: linesegmentsampler.hh:247
const GridPart & gridPart() const
obtain grid part on which the LineSegmentSampler works
Definition: linesegmentsampler.hh:106
void operator()(const GridFunction &f, std::vector< typename GridFunction::RangeType > &samples, std::nothrow_t) const
sample a given function
Definition: linesegmentsampler.hh:139
LineSegmentSampler(const GridPart &gridPart, const DomainType &left, const DomainType &right)
constructor
Definition: linesegmentsampler.hh:75
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
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Apr 23, 22:35, 2025)