splineInterpolationWeights.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2012-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
28 #include "ListOps.H"
30 
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35 
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 
38 defineTypeNameAndDebug(splineInterpolationWeights, 0);
40 (
41  interpolationWeights,
42  splineInterpolationWeights,
43  word
44 );
45 
46 
47 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
48 
50 (
51  const scalarField& samples,
52  const bool checkEqualDistance
53 )
54 :
55  interpolationWeights(samples),
56  index_(-1)
57 {
58  if (checkEqualDistance && samples_.size() > 2)
59  {
60  const scalar interval = samples_[1]-samples[0];
61  for (label i = 2; i < samples_.size(); i++)
62  {
63  scalar d = samples_[i]-samples[i-1];
64 
65  if (mag(d-interval) > small)
66  {
68  << "Spline interpolation only valid for constant intervals."
69  << nl
70  << "Interval 0-1 : " << interval << nl
71  << "Interval " << i-1 << '-' << i << " : "
72  << d << endl;
73  }
74  }
75  }
76 }
77 
78 
79 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
80 
82 (
83  const scalar t,
84  labelList& indices,
85  scalarField& weights
86 ) const
87 {
88  bool indexChanged = false;
89 
90  // linear interpolation
91  if (samples_.size() <= 2)
92  {
94  (
95  t,
96  indices,
97  weights
98  );
99  }
100 
101  // Check if current timeIndex is still valid
102  if
103  (
104  index_ >= 0
105  && index_ < samples_.size()
106  && (
107  samples_[index_] <= t
108  && (index_ == samples_.size()-1 || t <= samples_[index_+1])
109  )
110  )
111  {
112  // index_ still at correct slot
113  }
114  else
115  {
116  // search for correct index
117  index_ = findLower(samples_, t);
118  indexChanged = true;
119  }
120 
121 
122  // Clamp if outside table
123  if (index_ == -1)
124  {
125  indices.setSize(1);
126  weights.setSize(1);
127 
128  indices[0] = 0;
129  weights[0] = 1;
130  return indexChanged;
131  }
132  else if (index_ == samples_.size()-1)
133  {
134  indices.setSize(1);
135  weights.setSize(1);
136 
137  indices[0] = samples_.size()-1;
138  weights[0] = 1;
139  return indexChanged;
140  }
141 
142 
143 
144  label lo = index_;
145  label hi = index_+1;
146 
147  // weighting
148  scalar mu = (t - samples_[lo])/(samples_[hi] - samples_[lo]);
149 
150  scalar w0 = 0.5*(mu*(-1+mu*(2-mu))); // coeff of lo-1
151  scalar w1 = 0.5*(2+mu*(mu*(-5 + mu*(3)))); // coeff of lo
152  scalar w2 = 0.5*(mu*(1 + mu*(4 + mu*(-3)))); // coeff of hi
153  scalar w3 = 0.5*(mu*mu*(-1 + mu)); // coeff of hi+1
154 
155  if (lo > 0)
156  {
157  if (hi < samples_.size()-1)
158  {
159  // Four points available
160  indices.setSize(4);
161  weights.setSize(4);
162 
163  indices[0] = lo-1;
164  indices[1] = lo;
165  indices[2] = hi;
166  indices[3] = hi+1;
167 
168  weights[0] = w0;
169  weights[1] = w1;
170  weights[2] = w2;
171  weights[3] = w3;
172  }
173  else
174  {
175  // No y3 available. Extrapolate: y3=3*y2-y1
176  indices.setSize(3);
177  weights.setSize(3);
178 
179  indices[0] = lo-1;
180  indices[1] = lo;
181  indices[2] = hi;
182 
183  weights[0] = w0;
184  weights[1] = w1 - w3;
185  weights[2] = w2 + 2*w3;
186  }
187  }
188  else
189  {
190  // No y0 available. Extrapolate: y0=2*y1-y2;
191  if (hi < samples_.size()-1)
192  {
193  indices.setSize(3);
194  weights.setSize(3);
195 
196  indices[0] = lo;
197  indices[1] = hi;
198  indices[2] = hi+1;
199 
200  weights[0] = w1 + 2*w0;
201  weights[1] = w2 - w0;
202  weights[2] = w3;
203  }
204  else
205  {
206  indices.setSize(2);
207  weights.setSize(2);
208 
209  indices[0] = lo;
210  indices[1] = hi;
211 
212  weights[0] = w1 + 2*w0 - w3;
213  weights[1] = w2 - w0 + 2*w3;
214  }
215  }
216 
217  return indexChanged;
218 }
219 
220 
221 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
222 
223 } // End namespace Foam
224 
225 // ************************************************************************* //
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
#define w2
Definition: blockCreate.C:32
splineInterpolationWeights(const scalarField &samples, const bool checkEqualDistance=true)
Construct from components. By default make sure samples are.
Abstract base class for interpolating in 1D.
Macros for easy insertion into run-time selection tables.
Various functions to operate on Lists.
virtual bool valueWeights(const scalar t, labelList &indices, scalarField &weights) const
Calculate weights and indices to calculate t from samples.
#define w0
Definition: blockCreate.C:30
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
#define w3
Definition: blockCreate.C:33
static const char nl
Definition: Ostream.H:260
const dimensionedScalar mu
Atomic mass unit.
defineTypeNameAndDebug(combustionModel, 0)
void setSize(const label)
Reset size of List.
Definition: List.C:281
#define WarningInFunction
Report a warning using Foam::Warning.
virtual bool valueWeights(const scalar t, labelList &indices, scalarField &weights) const
Calculate weights and indices to calculate t from samples.
label findLower(const ListType &, typename ListType::const_reference, const label stary, const BinaryOp &bop)
Find last element < given value in sorted list and return index,.
dimensioned< scalar > mag(const dimensioned< Type > &)
#define w1
Definition: blockCreate.C:31
Namespace for OpenFOAM.