Nektar++
LibUtilities/BasicUtils/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// Permission is hereby granted, free of charge, to any person obtaining a
15// copy of this software and associated documentation files (the "Software"),
16// to deal in the Software without restriction, including without limitation
17// the rights to use, copy, modify, merge, publish, distribute, sublicense,
18// and/or sell copies of the Software, and to permit persons to whom the
19// Software is furnished to do so, subject to the following conditions:
20//
21// The above copyright notice and this permission notice shall be included
22// in all copies or substantial portions of the Software.
23//
24// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30// DEALINGS IN THE SOFTWARE.
31//
32// Description: Interpolator
33//
34///////////////////////////////////////////////////////////////////////////////
35
36#ifndef LIBUTILITIES_BASICUTILS_INTERPOLATOR_H
37#define LIBUTILITIES_BASICUTILS_INTERPOLATOR_H
38
39#include <functional>
40#include <iostream>
41#include <memory>
42#include <vector>
43
44#include <boost/geometry/geometries/box.hpp>
45#include <boost/geometry/geometries/point.hpp>
46#if BOOST_VERSION == 107700
47#include <boost/geometry/strategies/strategies.hpp>
48#endif
49#include <boost/geometry/index/rtree.hpp>
50
56
58{
59
61{
67};
68
69/// A class that contains algorithms for interpolation between pts fields,
70/// expansions and different meshes
72{
73public:
74 /**
75 * @brief Constructor of the Interpolator class
76 *
77 * @param method interpolation method, defaults to a sensible value if
78 * not set
79 * @param coordId coordinate id along which the interpolation should be
80 * performed
81 * @param filtWidth filter width, required by some algorithms such as eGauss
82 * @param maxPts limit number of considered points
83 *
84 * if method is not specified, the best algorithm is chosen automatically.
85 *
86 * If coordId is not specified, a full 1D/2D/3D interpolation is performed
87 * without
88 * collapsing any coordinate.
89 *
90 * filtWidth must be specified for the eGauss algorithm only.
91 */
92 Interpolator(InterpMethod method = eNoMethod, short int coordId = -1,
93 NekDouble filtWidth = 0.0, int maxPts = 1000)
94 : m_method(method), m_filtWidth(filtWidth), m_maxPts(maxPts),
95 m_coordId(coordId)
96 {
97 }
98
99 /// Compute interpolation weights without doing any interpolation
101 const LibUtilities::PtsFieldSharedPtr ptsInField,
102 LibUtilities::PtsFieldSharedPtr &ptsOutField, bool reuseTree = false);
103
104 /// Interpolate from a pts field to a pts field
106 const LibUtilities::PtsFieldSharedPtr ptsInField,
108
109 /// returns the dimension of the Interpolator.
110 /// Should be higher than the dimensions of the interpolated fields
111 LIB_UTILITIES_EXPORT int GetDim() const;
112
113 /// Returns the filter width
115
116 /// Returns the coordinate id along which the interpolation should be
117 /// performed
119
120 /// Returns the interpolation method used by this interpolator
122
123 /// Returns the input field
125
126 /// Returns the output field
128
129 /// Returns if the weights have already been computed
131
132 /// sets a callback funtion which gets called every time the interpolation
133 /// progresses
134 template <typename FuncPointerT, typename ObjectPointerT>
135 void SetProgressCallback(FuncPointerT func, ObjectPointerT obj)
136 {
138 std::bind(func, obj, std::placeholders::_1, std::placeholders::_2);
139 }
140
141protected:
142 /// input field
144 /// output field
146
147 std::function<void(const int position, const int goal)> m_progressCallback;
148
149private:
151 {
152 public:
153 int idx;
156
157 PtsPoint() : idx(-1), coords(Array<OneD, NekDouble>(3)), dist(1E30){};
158
160 : idx(idx), coords(coords), dist(dist){};
161
162 bool operator<(const PtsPoint &comp) const
163 {
164 return (dist < comp.dist);
165 };
166 };
167
168 /// dimension of this interpolator. Hardcoded to 3
169 static const int m_dim = 3;
170 typedef boost::geometry::model::point<NekDouble, m_dim,
171 boost::geometry::cs::cartesian>
173 typedef std::pair<BPoint, unsigned int> PtsPointPair;
174 typedef boost::geometry::index::rtree<PtsPointPair,
175 boost::geometry::index::rstar<16>>
177
178 /// Interpolation Method
180 /// A tree structure to speed up the neighbour search.
181 /// Note that we fill it with an iterator, so instead of rstar, the
182 /// packing algorithm is used.
183 std::shared_ptr<PtsRtree> m_rtree;
184 /// Interpolation weights for each neighbour.
185 /// Structure: m_weights[physPtIdx][neighbourIdx]
187 /// Indices of the relevant neighbours for each physical point.
188 /// Structure: m_neighInds[ptIdx][neighbourIdx]
190 /// Filter width used for some interpolation algorithms
192 /// Max number of interpolation points
194 /// coordinate id along which the interpolation should be performed
195 short int m_coordId;
196
197 LIB_UTILITIES_EXPORT void CalcW_Gauss(const PtsPoint &searchPt,
198 const NekDouble sigma,
199 const int maxPts = 250);
200
201 LIB_UTILITIES_EXPORT void CalcW_Linear(const PtsPoint &searchPt,
202 int coordId);
203
204 LIB_UTILITIES_EXPORT void CalcW_NNeighbour(const PtsPoint &searchPt);
205
206 LIB_UTILITIES_EXPORT void CalcW_Shepard(const PtsPoint &searchPt,
207 int numPts);
208
209 LIB_UTILITIES_EXPORT void CalcW_Quadratic(const PtsPoint &searchPt,
210 int coordId);
211
213
215 const PtsPoint &searchPt, std::vector<PtsPoint> &neighbourPts,
216 const NekDouble dist, const unsigned int maxPts = 1);
217
219 const PtsPoint &searchPt, std::vector<PtsPoint> &neighbourPts,
220 const unsigned int numPts = 1);
221};
222
223typedef std::shared_ptr<Interpolator> InterpolatorSharedPtr;
224
225} // namespace Nektar::LibUtilities
226
227#endif
#define LIB_UTILITIES_EXPORT
PtsPoint(int idx, Array< OneD, NekDouble > coords, NekDouble dist)
A class that contains algorithms for interpolation between pts fields, expansions and different meshe...
void Interpolate(const LibUtilities::PtsFieldSharedPtr ptsInField, LibUtilities::PtsFieldSharedPtr &ptsOutField)
Interpolate from a pts field to a pts field.
Array< TwoD, NekDouble > m_weights
Interpolation weights for each neighbour. Structure: m_weights[physPtIdx][neighbourIdx].
int GetCoordId() const
Returns the coordinate id along which the interpolation should be performed.
void PrintStatistics()
Returns if the weights have already been computed.
void SetProgressCallback(FuncPointerT func, ObjectPointerT obj)
sets a callback funtion which gets called every time the interpolation progresses
LibUtilities::PtsFieldSharedPtr GetInField() const
Returns the input field.
int m_maxPts
Max number of interpolation points.
Array< TwoD, unsigned int > m_neighInds
Indices of the relevant neighbours for each physical point. Structure: m_neighInds[ptIdx][neighbourId...
LibUtilities::PtsFieldSharedPtr GetOutField() const
Returns the output field.
Interpolator(InterpMethod method=eNoMethod, short int coordId=-1, NekDouble filtWidth=0.0, int maxPts=1000)
Constructor of the Interpolator class.
static const int m_dim
dimension of this interpolator. Hardcoded to 3
int GetDim() const
returns the dimension of the Interpolator. Should be higher than the dimensions of the interpolated f...
std::shared_ptr< PtsRtree > m_rtree
A tree structure to speed up the neighbour search. Note that we fill it with an iterator,...
NekDouble m_filtWidth
Filter width used for some interpolation algorithms.
void CalcW_Linear(const PtsPoint &searchPt, int coordId)
Computes interpolation weights using linear interpolation.
boost::geometry::model::point< NekDouble, m_dim, boost::geometry::cs::cartesian > BPoint
void CalcW_NNeighbour(const PtsPoint &searchPt)
Computes interpolation weights using nearest neighbour interpolation.
LibUtilities::PtsFieldSharedPtr m_ptsInField
input field
void CalcW_Shepard(const PtsPoint &searchPt, int numPts)
Computes interpolation weights using linear interpolation.
NekDouble GetFiltWidth() const
Returns the filter width.
InterpMethod GetInterpMethod() const
Returns the interpolation method used by this interpolator.
void CalcW_Gauss(const PtsPoint &searchPt, const NekDouble sigma, const int maxPts=250)
Computes interpolation weights using gaussian interpolation.
std::function< void(const int position, const int goal)> m_progressCallback
void FindNNeighbours(const PtsPoint &searchPt, std::vector< PtsPoint > &neighbourPts, const unsigned int numPts=1)
Finds the nearest neighbours of a point.
void CalcW_Quadratic(const PtsPoint &searchPt, int coordId)
Computes interpolation weights using quadratic interpolation.
void CalcWeights(const LibUtilities::PtsFieldSharedPtr ptsInField, LibUtilities::PtsFieldSharedPtr &ptsOutField, bool reuseTree=false)
Compute interpolation weights without doing any interpolation.
void FindNeighbours(const PtsPoint &searchPt, std::vector< PtsPoint > &neighbourPts, const NekDouble dist, const unsigned int maxPts=1)
Finds the nearest neighbours of a point.
boost::geometry::index::rtree< PtsPointPair, boost::geometry::index::rstar< 16 > > PtsRtree
short int m_coordId
coordinate id along which the interpolation should be performed
LibUtilities::PtsFieldSharedPtr m_ptsOutField
output field
std::shared_ptr< Interpolator > InterpolatorSharedPtr
std::shared_ptr< PtsField > PtsFieldSharedPtr
Definition: PtsField.h:184
double NekDouble