Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / src / OpenFOAM / interpolations / interpolateXY / interpolateXY.C
blobb381f2c95e2410e7c4d1827b1ac14f9630397043
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2004-2010 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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.
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.
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/>.
24 \*---------------------------------------------------------------------------*/
26 #include "interpolateXY.H"
27 #include "primitiveFields.H"
29 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
31 namespace Foam
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 template<class Type>
37 Field<Type> interpolateXY
39     const scalarField& xNew,
40     const scalarField& xOld,
41     const Field<Type>& yOld
44     Field<Type> yNew(xNew.size());
46     forAll(xNew, i)
47     {
48         yNew[i] = interpolateXY(xNew[i], xOld, yOld);
49     }
51     return yNew;
55 template<class Type>
56 Type interpolateXY
58     const scalar x,
59     const scalarField& xOld,
60     const Field<Type>& yOld
63     label n = xOld.size();
65     label lo = 0;
66     for (lo=0; lo<n && xOld[lo]>x; ++lo)
67     {}
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     }
81     label hi = 0;
82     for (hi=0; hi<n && xOld[hi]<x; ++hi)
83     {}
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     }
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     }
118 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
120 } // End namespace Foam
122 // ************************************************************************* //