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 #include <iostream>
42 
43 #include <boost/geometry/geometries/box.hpp>
44 #include <boost/geometry/geometries/point.hpp>
45 #include <boost/geometry/index/rtree.hpp>
46 
47 #include <boost/function.hpp>
48 #include <boost/shared_ptr.hpp>
49 
50 #include <MultiRegions/ExpList.h>
51 
56 
57 #include "FieldUtilsDeclspec.h"
58 
59 namespace Nektar
60 {
61 namespace FieldUtils
62 {
63 
65 {
71 };
72 
73 /// A class that contains algorithms for interpolation between pts fields,
74 /// expansions and different meshes
76 {
77 public:
78  /**
79  * @brief Constructor of the Interpolator class
80  *
81  * @param method interpolation method, defaults to a sensible value if not
82  * set
83  * @param coordId coordinate id along which the interpolation should be
84  * performed
85  * @param filtWidth filter width, required by some algorithms such as eGauss
86  * @param maxPts limit number of considered points
87  *
88  * if method is not specified, the best algorithm is chosen autpomatically.
89  *
90  * If coordId is not specified, a full 1D/2D/3D interpolation is performed
91  * without
92  * collapsing any coordinate.
93  *
94  * filtWidth must be specified for the eGauss algorithm only.
95  */
97  short int coordId = -1,
98  NekDouble filtWidth = 0.0,
99  int maxPts = 1000)
100  : m_method(method), m_filtWidth(filtWidth), m_maxPts(maxPts),
101  m_coordId(coordId){};
102 
103  /// Compute interpolation weights without doing any interpolation
105  const LibUtilities::PtsFieldSharedPtr ptsInField,
106  LibUtilities::PtsFieldSharedPtr &ptsOutField);
107 
108  /// Interpolate from a pts field to a pts field
110  const LibUtilities::PtsFieldSharedPtr ptsInField,
111  LibUtilities::PtsFieldSharedPtr &ptsOutField);
112 
113  /// Interpolate from an expansion to an expansion
115  const std::vector<MultiRegions::ExpListSharedPtr> expInField,
116  std::vector<MultiRegions::ExpListSharedPtr> &expOutField,
117  NekDouble def_value = 0.0);
118 
119  /// Interpolate from an expansion to a pts field
121  const std::vector<MultiRegions::ExpListSharedPtr> expInField,
122  LibUtilities::PtsFieldSharedPtr &ptsOutField,
123  NekDouble def_value = 0.0);
124 
125  /// Interpolate from a pts field to an expansion
127  const LibUtilities::PtsFieldSharedPtr ptsInField,
128  std::vector<MultiRegions::ExpListSharedPtr> &expOutField);
129 
130  /// returns the dimension of the Interpolator.
131  /// Should be higher than the dimensions of the interpolated fields
132  FIELD_UTILS_EXPORT int GetDim() const;
133 
134  /// Returns the filter width
136 
137  /// Returns the coordinate id along which the interpolation should be
138  /// performed
139  FIELD_UTILS_EXPORT int GetCoordId() const;
140 
141  /// Returns the interpolation method used by this interpolator
143 
144  /// Returns the input field
146 
147  /// Returns the output field
149 
150  /// Print statics of the interpolation weights
152 
153  /// sets a callback funtion which gets called every time the interpolation
154  /// progresses
155  template <typename FuncPointerT, typename ObjectPointerT>
156  void SetProgressCallback(FuncPointerT func, ObjectPointerT obj)
157  {
158  m_progressCallback = boost::bind(func, obj, _1, _2);
159  }
160 
161 private:
162  class PtsPoint
163  {
164  public:
165  int idx;
168 
169  PtsPoint() : idx(-1), coords(Array<OneD, NekDouble>(3)), dist(1E30){};
170 
172  : idx(idx), coords(coords), dist(dist){};
173 
174  bool operator<(const PtsPoint &comp) const
175  {
176  return (dist < comp.dist);
177  };
178  };
179 
180  /// dimension of this interpolator. Hardcoded to 3
181  static const int m_dim = 3;
182  typedef boost::geometry::model::point<NekDouble, m_dim, boost::geometry::cs::cartesian> BPoint;
183  typedef std::pair<BPoint, unsigned int> PtsPointPair;
184  typedef boost::geometry::index::rtree<PtsPointPair, boost::geometry::index::rstar<16> > PtsRtree;
185 
186  /// input field
188  /// output field
190  /// input field
191  std::vector<MultiRegions::ExpListSharedPtr> m_expInField;
192  /// output field
193  std::vector<MultiRegions::ExpListSharedPtr> m_expOutField;
194 
195  /// Interpolation Method
197  /// A tree structure to speed up the neighbour search.
198  /// Note that we fill it with an iterator, so instead of rstar, the
199  /// packing algorithm is used.
200  boost::shared_ptr<PtsRtree> m_rtree;
201  /// Interpolation weights for each neighbour.
202  /// Structure: m_weights[physPtIdx][neighbourIdx]
204  /// Indices of the relevant neighbours for each physical point.
205  /// Structure: m_neighInds[ptIdx][neighbourIdx]
207  /// Filter width used for some interpolation algorithms
209  /// Max number of interpolation points
210  int m_maxPts;
211  /// coordinate id along which the interpolation should be performed
212  short int m_coordId;
213 
214  boost::function<void(const int position, const int goal)>
216 
217  FIELD_UTILS_EXPORT void CalcW_Gauss(const PtsPoint &searchPt,
218  const NekDouble sigma,
219  const int maxPts = 250);
220 
221  FIELD_UTILS_EXPORT void CalcW_Linear(const PtsPoint &searchPt, int coordId);
222 
223  FIELD_UTILS_EXPORT void CalcW_NNeighbour(const PtsPoint &searchPt);
224 
225  FIELD_UTILS_EXPORT void CalcW_Shepard(const PtsPoint &searchPt, int numPts);
226 
227  FIELD_UTILS_EXPORT void CalcW_Quadratic(const PtsPoint &searchPt,
228  int coordId);
229 
231 
232  FIELD_UTILS_EXPORT void FindNeighbours(const PtsPoint &searchPt,
233  std::vector<PtsPoint> &neighbourPts,
234  const NekDouble dist,
235  const unsigned int maxPts = 1);
236 
237  FIELD_UTILS_EXPORT void FindNNeighbours(const PtsPoint &searchPt,
238  std::vector<PtsPoint> &neighbourPts,
239  const unsigned int numPts = 1);
240 };
241 
242 typedef boost::shared_ptr<Interpolator> InterpolatorSharedPtr;
243 static InterpolatorSharedPtr NullInterpolator;
244 }
245 }
246 
247 #endif
Array< TwoD, unsigned int > m_neighInds
Indices of the relevant neighbours for each physical point. Structure: m_neighInds[ptIdx][neighbourId...
Definition: Interpolator.h:206
std::pair< BPoint, unsigned int > PtsPointPair
Definition: Interpolator.h:183
FIELD_UTILS_EXPORT void CalcW_Gauss(const PtsPoint &searchPt, const NekDouble sigma, const int maxPts=250)
Computes interpolation weights using gaussian interpolation.
A class that contains algorithms for interpolation between pts fields, expansions and different meshe...
Definition: Interpolator.h:75
static const int m_dim
dimension of this interpolator. Hardcoded to 3
Definition: Interpolator.h:181
void SetProgressCallback(FuncPointerT func, ObjectPointerT obj)
sets a callback funtion which gets called every time the interpolation progresses ...
Definition: Interpolator.h:156
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:212
PtsPoint(int idx, Array< OneD, NekDouble > coords, NekDouble dist)
Definition: Interpolator.h:171
boost::geometry::index::rtree< PtsPointPair, boost::geometry::index::rstar< 16 > > PtsRtree
Definition: Interpolator.h:184
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:174
InterpMethod m_method
Interpolation Method.
Definition: Interpolator.h:196
static InterpolatorSharedPtr NullInterpolator
Definition: Interpolator.h:243
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:187
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:200
boost::geometry::model::point< NekDouble, m_dim, boost::geometry::cs::cartesian > BPoint
Definition: Interpolator.h:182
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:215
FIELD_UTILS_EXPORT void PrintStatistics()
Print statics of the interpolation weights.
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:210
Interpolator(InterpMethod method=eNoMethod, short int coordId=-1, NekDouble filtWidth=0.0, int maxPts=1000)
Constructor of the Interpolator class.
Definition: Interpolator.h:96
double NekDouble
LibUtilities::PtsFieldSharedPtr m_ptsOutField
output field
Definition: Interpolator.h:189
FIELD_UTILS_EXPORT int GetDim() const
returns the dimension of the Interpolator. Should be higher than the dimensions of the interpolated f...
Array< TwoD, float > m_weights
Interpolation weights for each neighbour. Structure: m_weights[physPtIdx][neighbourIdx].
Definition: Interpolator.h:203
FIELD_UTILS_EXPORT LibUtilities::PtsFieldSharedPtr GetInField() const
Returns the input field.
std::vector< MultiRegions::ExpListSharedPtr > m_expOutField
output field
Definition: Interpolator.h:193
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:242
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:208
std::vector< MultiRegions::ExpListSharedPtr > m_expInField
input field
Definition: Interpolator.h:191
FIELD_UTILS_EXPORT void CalcW_Shepard(const PtsPoint &searchPt, int numPts)
Computes interpolation weights using linear interpolation.