interpolateXY.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011 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 
26 #include "interpolateXY.H"
27 #include "primitiveFields.H"
28 
29 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33 
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 
36 template<class Type>
37 Field<Type> interpolateXY
38 (
39  const scalarField& xNew,
40  const scalarField& xOld,
41  const Field<Type>& yOld
42 )
43 {
44  Field<Type> yNew(xNew.size());
45 
46  forAll(xNew, i)
47  {
48  yNew[i] = interpolateXY(xNew[i], xOld, yOld);
49  }
50 
51  return yNew;
52 }
53 
54 
55 template<class Type>
56 Type interpolateXY
57 (
58  const scalar x,
59  const scalarField& xOld,
60  const Field<Type>& yOld
61 )
62 {
63  label n = xOld.size();
64 
65  label lo = 0;
66  for (lo=0; lo<n && xOld[lo]>x; ++lo)
67  {}
68 
69  label low = lo;
70  if (low < n)
71  {
72  for (label i=low; i<n; ++i)
73  {
74  if (xOld[i] > xOld[lo] && xOld[i] <= x)
75  {
76  lo = i;
77  }
78  }
79  }
80 
81  label hi = 0;
82  for (hi=0; hi<n && xOld[hi]<x; ++hi)
83  {}
84 
85  label high = hi;
86  if (high < n)
87  {
88  for (label i=high; i<n; ++i)
89  {
90  if (xOld[i] < xOld[hi] && xOld[i] >= x)
91  {
92  hi = i;
93  }
94  }
95  }
96 
97 
98  if (lo<n && hi<n && lo != hi)
99  {
100  return yOld[lo]
101  + ((x - xOld[lo])/(xOld[hi] - xOld[lo]))*(yOld[hi] - yOld[lo]);
102  }
103  else if (lo == hi)
104  {
105  return yOld[lo];
106  }
107  else if (lo == n)
108  {
109  return yOld[hi];
110  }
111  else
112  {
113  return yOld[lo];
114  }
115 }
116 
117 
118 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
119 
120 } // End namespace Foam
121 
122 // ************************************************************************* //
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:76
Specialisations of Field<T> for scalar, vector and tensor.
Namespace for OpenFOAM.
label n
#define forAll(list, i)
Definition: UList.H:421
Field< Type > interpolateXY(const scalarField &xNew, const scalarField &xOld, const Field< Type > &yOld)
Definition: interpolateXY.C:38
Interpolates y values from one curve to another with a different x distribution.