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