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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011 OpenFOAM Foundation
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "interpolateXY.H"
29 #include "primitiveFields.H"
30 
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35 
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 
38 template<class Type>
39 Field<Type> interpolateXY
40 (
41  const scalarField& xNew,
42  const scalarField& xOld,
43  const Field<Type>& yOld
44 )
45 {
46  Field<Type> yNew(xNew.size());
47 
48  forAll(xNew, i)
49  {
50  yNew[i] = interpolateXY(xNew[i], xOld, yOld);
51  }
52 
53  return yNew;
54 }
55 
56 
57 template<class Type>
58 Type interpolateXY
59 (
60  const scalar x,
61  const scalarField& xOld,
62  const Field<Type>& yOld
63 )
64 {
65  label n = xOld.size();
66 
67  label lo = 0;
68  for (lo=0; lo<n && xOld[lo]>x; ++lo)
69  {}
70 
71  label low = lo;
72  if (low < n)
73  {
74  for (label i=low; i<n; ++i)
75  {
76  if (xOld[i] > xOld[lo] && xOld[i] <= x)
77  {
78  lo = i;
79  }
80  }
81  }
82 
83  label hi = 0;
84  for (hi=0; hi<n && xOld[hi]<x; ++hi)
85  {}
86 
87  label high = hi;
88  if (high < n)
89  {
90  for (label i=high; i<n; ++i)
91  {
92  if (xOld[i] < xOld[hi] && xOld[i] >= x)
93  {
94  hi = i;
95  }
96  }
97  }
98 
99 
100  if (lo<n && hi<n && lo != hi)
101  {
102  return yOld[lo]
103  + ((x - xOld[lo])/(xOld[hi] - xOld[lo]))*(yOld[hi] - yOld[lo]);
104  }
105  else if (lo == hi)
106  {
107  return yOld[lo];
108  }
109  else if (lo == n)
110  {
111  return yOld[hi];
112  }
113  else
114  {
115  return yOld[lo];
116  }
117 }
118 
119 
120 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
121 
122 } // End namespace Foam
123 
124 // ************************************************************************* //
Foam::interpolateXY
Field< Type > interpolateXY(const scalarField &xNew, const scalarField &xOld, const Field< Type > &yOld)
Definition: interpolateXY.C:40
primitiveFields.H
Specialisations of Field<T> for scalar, vector and tensor.
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::Field
Generic templated field type.
Definition: Field.H:63
interpolateXY.H
Interpolates y values from one curve to another with a different x distribution.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
x
x
Definition: LISASMDCalcMethod2.H:52