Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Interpolator.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File Interpolator.h
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2016 Kilian Lackhove
10 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
11 // Department of Aeronautics, Imperial College London (UK), and Scientific
12 // Computing and Imaging Institute, University of Utah (USA).
13 //
14 // License for the specific language governing rights and limitations under
15 // Permission is hereby granted, free of charge, to any person obtaining a
16 // copy of this software and associated documentation files (the "Software"),
17 // to deal in the Software without restriction, including without limitation
18 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
19 // and/or sell copies of the Software, and to permit persons to whom the
20 // Software is furnished to do so, subject to the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be included
23 // in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
26 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 // DEALINGS IN THE SOFTWARE.
32 //
33 // Description: Interpolator
34 //
35 ///////////////////////////////////////////////////////////////////////////////
36 
37 #ifndef FIELDUTILS_INTERPOLATOR_H
38 #define FIELDUTILS_INTERPOLATOR_H
39 
40 #include <vector>
41 
42 #include <boost/function.hpp>
43 #include <boost/shared_ptr.hpp>
44 
45 #include <boost/geometry.hpp>
46 #include <boost/geometry/geometries/box.hpp>
47 #include <boost/geometry/geometries/point.hpp>
48 #include <boost/geometry/index/rtree.hpp>
49 
50 #include <MultiRegions/ExpList.h>
51 
56 
57 #include "FieldUtilsDeclspec.h"
58 
59 namespace bg = boost::geometry;
60 namespace bgi = boost::geometry::index;
61 
62 namespace Nektar
63 {
64 namespace FieldUtils
65 {
66 
68 {
74 };
75 
76 /// A class that contains algorithms for interpolation between pts fields,
77 /// expansions and different meshes
79 {
80 public:
81  /**
82  * @brief Constructor of the Interpolator class
83  *
84  * @param method interpolation method, defaults to a sensible value if not
85  * set
86  * @param coordId coordinate id along which the interpolation should be
87  * performed
88  * @param filtWidth filter width, required by some algorithms such as eGauss
89  * @param maxPts limit number of considered points
90  *
91  * if method is not specified, the best algorithm is chosen autpomatically.
92  *
93  * If coordId is not specified, a full 1D/2D/3D interpolation is performed
94  * without
95  * collapsing any coordinate.
96  *
97  * filtWidth must be specified for the eGauss algorithm only.
98  */
100  short int coordId = -1,
101  NekDouble filtWidth = 0.0,
102  int maxPts = 1000)
103  : m_method(method), m_filtWidth(filtWidth), m_maxPts(maxPts),
104  m_coordId(coordId){};
105 
106  /// Compute interpolation weights without doing any interpolation
108  const LibUtilities::PtsFieldSharedPtr ptsInField,
109  LibUtilities::PtsFieldSharedPtr &ptsOutField);
110 
111  /// Interpolate from a pts field to a pts field
113  const LibUtilities::PtsFieldSharedPtr ptsInField,
114  LibUtilities::PtsFieldSharedPtr &ptsOutField);
115 
116  /// Interpolate from an expansion to an expansion
118  const std::vector<MultiRegions::ExpListSharedPtr> expInField,
119  std::vector<MultiRegions::ExpListSharedPtr> &expOutField,
120  NekDouble def_value = 0.0);
121 
122  /// Interpolate from an expansion to a pts field
124  const std::vector<MultiRegions::ExpListSharedPtr> expInField,
125  LibUtilities::PtsFieldSharedPtr &ptsOutField,
126  NekDouble def_value = 0.0);
127 
128  /// Interpolate from a pts field to an expansion
130  const LibUtilities::PtsFieldSharedPtr ptsInField,
131  std::vector<MultiRegions::ExpListSharedPtr> &expOutField);
132 
133  /// returns the dimension of the Interpolator.
134  /// Should be higher than the dimensions of the interpolated fields
135  FIELD_UTILS_EXPORT int GetDim() const;
136 
137  /// Returns the filter width
139 
140  /// Returns the coordinate id along which the interpolation should be
141  /// performed
142  FIELD_UTILS_EXPORT int GetCoordId() const;
143 
144  /// Returns the interpolation method used by this interpolator
146 
147  /// Returns the input field
149 
150  /// Returns the output field
152 
153  /// Print statics of the interpolation weights
155 
156  /// sets a callback funtion which gets called every time the interpolation
157  /// progresses
158  template <typename FuncPointerT, typename ObjectPointerT>
159  void SetProgressCallback(FuncPointerT func, ObjectPointerT obj)
160  {
161  m_progressCallback = boost::bind(func, obj, _1, _2);
162  }
163 
164 private:
165  class PtsPoint
166  {
167  public:
168  int idx;
171 
172  PtsPoint() : idx(-1), coords(Array<OneD, NekDouble>(3)), dist(1E30){};
173 
175  : idx(idx), coords(coords), dist(dist){};
176 
177  bool operator<(const PtsPoint &comp) const
178  {
179  return (dist < comp.dist);
180  };
181  };
182 
183  /// dimension of this interpolator. Hardcoded to 3
184  static const int m_dim = 3;
185  typedef bg::model::point<NekDouble, m_dim, bg::cs::cartesian> BPoint;
186  typedef std::pair<BPoint, unsigned int> PtsPointPair;
187  typedef bgi::rtree<PtsPointPair, bgi::rstar<16> > PtsRtree;
188 
189  /// input field
191  /// output field
193  /// input field
194  std::vector<MultiRegions::ExpListSharedPtr> m_expInField;
195  /// output field
196  std::vector<MultiRegions::ExpListSharedPtr> m_expOutField;
197 
198  /// Interpolation Method
200  /// A tree structure to speed up the neighbour search.
201  /// Note that we fill it with an iterator, so instead of rstar, the
202  /// packing algorithm is used.
203  boost::shared_ptr<PtsRtree> m_rtree;
204  /// Interpolation weights for each neighbour.
205  /// Structure: m_weights[physPtIdx][neighbourIdx]
207  /// Indices of the relevant neighbours for each physical point.
208  /// Structure: m_neighInds[ptIdx][neighbourIdx]
210  /// Filter width used for some interpolation algorithms
212  /// Max number of interpolation points
213  int m_maxPts;
214  /// coordinate id along which the interpolation should be performed
215  short int m_coordId;
216 
217  boost::function<void(const int position, const int goal)>
219 
220  FIELD_UTILS_EXPORT void CalcW_Gauss(const PtsPoint &searchPt,
221  const NekDouble sigma,
222  const int maxPts = 250);
223 
224  FIELD_UTILS_EXPORT void CalcW_Linear(const PtsPoint &searchPt, int coordId);
225 
226  FIELD_UTILS_EXPORT void CalcW_NNeighbour(const PtsPoint &searchPt);
227 
228  FIELD_UTILS_EXPORT void CalcW_Shepard(const PtsPoint &searchPt);
229 
230  FIELD_UTILS_EXPORT void CalcW_Quadratic(const PtsPoint &searchPt,
231  int coordId);
232 
234 
235  FIELD_UTILS_EXPORT void FindNeighbours(const PtsPoint &searchPt,
236  std::vector<PtsPoint> &neighbourPts,
237  const NekDouble dist,
238  const unsigned int maxPts = 1);
239 
240  FIELD_UTILS_EXPORT void FindNNeighbours(const PtsPoint &searchPt,
241  std::vector<PtsPoint> &neighbourPts,
242  const unsigned int numPts = 1);
243 };
244 
245 typedef boost::shared_ptr<Interpolator> InterpolatorSharedPtr;
246 static InterpolatorSharedPtr NullInterpolator;
247 }
248 }
249 
250 #endif
std::pair< BPoint, unsigned int > PtsPointPair
Definition: Interpolator.h:186
FIELD_UTILS_EXPORT void CalcW_Gauss(const PtsPoint &searchPt, const NekDouble sigma, const int maxPts=250)
Computes interpolation weights using gaussian interpolation.
bg::model::point< NekDouble, m_dim, bg::cs::cartesian > BPoint
Definition: Interpolator.h:185
A class that contains algorithms for interpolation between pts fields, expansions and different meshe...
Definition: Interpolator.h:78
static const int m_dim
dimension of this interpolator. Hardcoded to 3
Definition: Interpolator.h:184
void SetProgressCallback(FuncPointerT func, ObjectPointerT obj)
sets a callback funtion which gets called every time the interpolation progresses ...
Definition: Interpolator.h:159
FIELD_UTILS_EXPORT void FindNeighbours(const PtsPoint &searchPt, std::vector< PtsPoint > &neighbourPts, const NekDouble dist, const unsigned int maxPts=1)
Finds the neares neighbours of a point.
short int m_coordId
coordinate id along which the interpolation should be performed
Definition: Interpolator.h:215
bgi::rtree< PtsPointPair, bgi::rstar< 16 > > PtsRtree
Definition: Interpolator.h:187
PtsPoint(int idx, Array< OneD, NekDouble > coords, NekDouble dist)
Definition: Interpolator.h:174
FIELD_UTILS_EXPORT InterpMethod GetInterpMethod() const
Returns the interpolation method used by this interpolator.
FIELD_UTILS_EXPORT void Interpolate(const LibUtilities::PtsFieldSharedPtr ptsInField, LibUtilities::PtsFieldSharedPtr &ptsOutField)
Interpolate from a pts field to a pts field.
FIELD_UTILS_EXPORT void CalcW_Linear(const PtsPoint &searchPt, int coordId)
Computes interpolation weights using linear interpolation.
bool operator<(const PtsPoint &comp) const
Definition: Interpolator.h:177
InterpMethod m_method
Interpolation Method.
Definition: Interpolator.h:199
static InterpolatorSharedPtr NullInterpolator
Definition: Interpolator.h:246
FIELD_UTILS_EXPORT NekDouble GetFiltWidth() const
Returns the filter width.
FIELD_UTILS_EXPORT LibUtilities::PtsFieldSharedPtr GetOutField() const
Returns the output field.
LibUtilities::PtsFieldSharedPtr m_ptsInField
input field
Definition: Interpolator.h:190
FIELD_UTILS_EXPORT void CalcW_Shepard(const PtsPoint &searchPt)
Computes interpolation weights using linear interpolation.
boost::shared_ptr< PtsRtree > m_rtree
A tree structure to speed up the neighbour search. Note that we fill it with an iterator, so instead of rstar, the packing algorithm is used.
Definition: Interpolator.h:203
boost::shared_ptr< PtsField > PtsFieldSharedPtr
Definition: PtsField.h:178
FIELD_UTILS_EXPORT void CalcW_NNeighbour(const PtsPoint &searchPt)
Computes interpolation weights using nearest neighbour interpolation.
FIELD_UTILS_EXPORT void CalcWeights(const LibUtilities::PtsFieldSharedPtr ptsInField, LibUtilities::PtsFieldSharedPtr &ptsOutField)
Compute interpolation weights without doing any interpolation.
boost::function< void(const int position, const int goal)> m_progressCallback
Definition: Interpolator.h:218
FIELD_UTILS_EXPORT void PrintStatistics()
Print statics of the interpolation weights.
Array< OneD, Array< OneD, float > > m_weights
Interpolation weights for each neighbour. Structure: m_weights[physPtIdx][neighbourIdx].
Definition: Interpolator.h:206
FIELD_UTILS_EXPORT void FindNNeighbours(const PtsPoint &searchPt, std::vector< PtsPoint > &neighbourPts, const unsigned int numPts=1)
Finds the neares neighbours of a point.
FIELD_UTILS_EXPORT void SetupTree()
int m_maxPts
Max number of interpolation points.
Definition: Interpolator.h:213
Interpolator(InterpMethod method=eNoMethod, short int coordId=-1, NekDouble filtWidth=0.0, int maxPts=1000)
Constructor of the Interpolator class.
Definition: Interpolator.h:99
Array< OneD, Array< OneD, unsigned int > > m_neighInds
Indices of the relevant neighbours for each physical point. Structure: m_neighInds[ptIdx][neighbourId...
Definition: Interpolator.h:209
double NekDouble
LibUtilities::PtsFieldSharedPtr m_ptsOutField
output field
Definition: Interpolator.h:192
FIELD_UTILS_EXPORT int GetDim() const
returns the dimension of the Interpolator. Should be higher than the dimensions of the interpolated f...
FIELD_UTILS_EXPORT LibUtilities::PtsFieldSharedPtr GetInField() const
Returns the input field.
std::vector< MultiRegions::ExpListSharedPtr > m_expOutField
output field
Definition: Interpolator.h:196
FIELD_UTILS_EXPORT void CalcW_Quadratic(const PtsPoint &searchPt, int coordId)
Computes interpolation weights using quadratic interpolation.
#define FIELD_UTILS_EXPORT
boost::shared_ptr< Interpolator > InterpolatorSharedPtr
Definition: Interpolator.h:245
FIELD_UTILS_EXPORT int GetCoordId() const
Returns the coordinate id along which the interpolation should be performed.
NekDouble m_filtWidth
Filter width used for some interpolation algorithms.
Definition: Interpolator.h:211
std::vector< MultiRegions::ExpListSharedPtr > m_expInField
input field
Definition: Interpolator.h:194