1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2009-2010 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
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
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 "uniformInterpolationTable.H"
29 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
32 void Foam::uniformInterpolationTable<Type>::checkTable() const
36 FatalErrorIn("uniformInterpolationTable<Type>::checkTable()")
37 << "Table " << name() << ": must have at least 2 values." << nl
38 << "Table size = " << size() << nl
39 << " min, interval width = " << x0_ << ", " << dx_ << nl
45 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
48 Foam::uniformInterpolationTable<Type>::uniformInterpolationTable
63 IOdictionary dict(io);
65 dict.lookup("data") >> *this;
66 dict.lookup("x0") >> x0_;
67 dict.lookup("dx") >> dx_;
68 dict.readIfPresent("log10", log10_);
69 dict.readIfPresent("bound", bound_);
77 Foam::uniformInterpolationTable<Type>::uniformInterpolationTable
79 const word& tableName,
80 const objectRegistry& db,
81 const dictionary& dict,
82 const bool initialiseOnly
92 false // if used in BCs, could be used by multiple patches
95 x0_(readScalar(dict.lookup("x0"))),
96 dx_(readScalar(dict.lookup("dx"))),
97 log10_(dict.lookupOrDefault<Switch>("log10", false)),
98 bound_(dict.lookupOrDefault<Switch>("bound", false))
102 const scalar xMax = readScalar(dict.lookup("xMax"));
103 const label nIntervals = static_cast<label>(xMax - x0_)/dx_ + 1;
104 this->setSize(nIntervals);
108 dict.lookup("data") >> *this;
115 template <class Type>
116 Foam::uniformInterpolationTable<Type>::uniformInterpolationTable
118 const uniformInterpolationTable& uit
132 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
134 template <class Type>
135 Foam::uniformInterpolationTable<Type>::~uniformInterpolationTable()
139 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
141 template <class Type>
142 Type Foam::uniformInterpolationTable<Type>::interpolate(scalar x) const
146 x = max(min(xMax() - SMALL*dx_, x), x0_);
154 "uniformInterpolationTable<Type>::interpolate(scalar x)"
155 ) << "Supplied value is less than minimum table value:" << nl
156 << "xMin=" << x0_ << ", xMax=" << xMax() << ", x=" << x << nl
164 "uniformInterpolationTable<Type>::interpolate(scalar x)"
165 ) << "Supplied value is greater than maximum table value:" << nl
166 << "xMin=" << x0_ << ", xMax=" << xMax() << ", x=" << x << nl
171 const label i = static_cast<label>((x - x0_)/dx_);
173 const scalar xLo = x0_ + i*dx_;
175 Type fx = (x - xLo)/dx_*(operator[](i+1) - operator[](i)) + operator[](i);
179 Info<< "Table: " << name() << ", x=" << x
180 << ", x_lo=" << xLo << ", x_hi=" << xLo + dx_
181 << ", f(x_lo)=" << operator[](i) << ", f(x_hi)=" << operator[](i+1)
182 << ", f(x)=" << fx << endl;
189 template <class Type>
190 Type Foam::uniformInterpolationTable<Type>::interpolateLog10
201 else if (bound_ && (x <= 0))
209 "uniformInterpolationTable<Type>::interpolateLog10(scalar x)"
210 ) << "Table " << name() << nl
211 << "Supplied value must be greater than 0 when in log10 mode"
212 << nl << "x=" << x << nl << exit(FatalError);
216 return interpolate(x);
220 template <class Type>
221 void Foam::uniformInterpolationTable<Type>::write() const
223 IOdictionary dict(*this);
225 dict.add("data", static_cast<const List<scalar>&>(*this));
230 dict.add("log10", log10_);
234 dict.add("bound", bound_);
237 dict.regIOobject::write();
241 // ************************************************************************* //