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
57namespace Nektar
58{
59namespace LibUtilities
60{
61
63{
69};
70
71/// A class that contains algorithms for interpolation between pts fields,
72/// expansions and different meshes
74{
75public:
76 /**
77 * @brief Constructor of the Interpolator class
78 *
79 * @param method interpolation method, defaults to a sensible value if
80 * not set
81 * @param coordId coordinate id along which the interpolation should be
82 * performed
83 * @param filtWidth filter width, required by some algorithms such as eGauss
84 * @param maxPts limit number of considered points
85 *
86 * if method is not specified, the best algorithm is chosen automatically.
87 *
88 * If coordId is not specified, a full 1D/2D/3D interpolation is performed
89 * without
90 * collapsing any coordinate.
91 *
92 * filtWidth must be specified for the eGauss algorithm only.
93 */
94 Interpolator(InterpMethod method = eNoMethod, short int coordId = -1,
95 NekDouble filtWidth = 0.0, int maxPts = 1000)
96 : m_method(method), m_filtWidth(filtWidth), m_maxPts(maxPts),
97 m_coordId(coordId)
98 {
99 }
100
101 /// Compute interpolation weights without doing any interpolation
103 const LibUtilities::PtsFieldSharedPtr ptsInField,
104 LibUtilities::PtsFieldSharedPtr &ptsOutField, bool reuseTree = false);
105
106 /// Interpolate from a pts field to a pts field
108 const LibUtilities::PtsFieldSharedPtr ptsInField,
110
111 /// returns the dimension of the Interpolator.
112 /// Should be higher than the dimensions of the interpolated fields
113 LIB_UTILITIES_EXPORT int GetDim() const;
114
115 /// Returns the filter width
117
118 /// Returns the coordinate id along which the interpolation should be
119 /// performed
121
122 /// Returns the interpolation method used by this interpolator
124
125 /// Returns the input field
127
128 /// Returns the output field
130
131 /// Returns if the weights have already been computed
133
134 /// sets a callback funtion which gets called every time the interpolation
135 /// progresses
136 template <typename FuncPointerT, typename ObjectPointerT>
137 void SetProgressCallback(FuncPointerT func, ObjectPointerT obj)
138 {
140 std::bind(func, obj, std::placeholders::_1, std::placeholders::_2);
141 }
142
143protected:
144 /// input field
146 /// output field
148
149 std::function<void(const int position, const int goal)> m_progressCallback;
150
151private:
153 {
154 public:
155 int idx;
158
159 PtsPoint() : idx(-1), coords(Array<OneD, NekDouble>(3)), dist(1E30){};
160
162 : idx(idx), coords(coords), dist(dist){};
163
164 bool operator<(const PtsPoint &comp) const
165 {
166 return (dist < comp.dist);
167 };
168 };
169
170 /// dimension of this interpolator. Hardcoded to 3
171 static const int m_dim = 3;
172 typedef boost::geometry::model::point<NekDouble, m_dim,
173 boost::geometry::cs::cartesian>
175 typedef std::pair<BPoint, unsigned int> PtsPointPair;
176 typedef boost::geometry::index::rtree<PtsPointPair,
177 boost::geometry::index::rstar<16>>
179
180 /// Interpolation Method
182 /// A tree structure to speed up the neighbour search.
183 /// Note that we fill it with an iterator, so instead of rstar, the
184 /// packing algorithm is used.
185 std::shared_ptr<PtsRtree> m_rtree;
186 /// Interpolation weights for each neighbour.
187 /// Structure: m_weights[physPtIdx][neighbourIdx]
189 /// Indices of the relevant neighbours for each physical point.
190 /// Structure: m_neighInds[ptIdx][neighbourIdx]
192 /// Filter width used for some interpolation algorithms
194 /// Max number of interpolation points
196 /// coordinate id along which the interpolation should be performed
197 short int m_coordId;
198
199 LIB_UTILITIES_EXPORT void CalcW_Gauss(const PtsPoint &searchPt,
200 const NekDouble sigma,
201 const int maxPts = 250);
202
203 LIB_UTILITIES_EXPORT void CalcW_Linear(const PtsPoint &searchPt,
204 int coordId);
205
206 LIB_UTILITIES_EXPORT void CalcW_NNeighbour(const PtsPoint &searchPt);
207
208 LIB_UTILITIES_EXPORT void CalcW_Shepard(const PtsPoint &searchPt,
209 int numPts);
210
211 LIB_UTILITIES_EXPORT void CalcW_Quadratic(const PtsPoint &searchPt,
212 int coordId);
213
215
217 const PtsPoint &searchPt, std::vector<PtsPoint> &neighbourPts,
218 const NekDouble dist, const unsigned int maxPts = 1);
219
221 const PtsPoint &searchPt, std::vector<PtsPoint> &neighbourPts,
222 const unsigned int numPts = 1);
223};
224
225typedef std::shared_ptr<Interpolator> InterpolatorSharedPtr;
226
227} // namespace LibUtilities
228} // namespace Nektar
229
230#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:190
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
double NekDouble