1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
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 \*---------------------------------------------------------------------------*/
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 Foam::Table<Type>::Table(const word& entryName, Istream& is)
33 DataEntry<Type>(entryName),
38 FatalErrorIn("Foam::Table<Type>::Table(const Istream&)")
39 << "Table for entry " << this->name_ << " is invalid (empty)"
40 << nl << exit(FatalError);
46 Foam::Table<Type>::Table(const Table<Type>& tbl)
53 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
56 Foam::Table<Type>::~Table()
60 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
63 Type Foam::Table<Type>::value(const scalar x) const
65 // Return zero if out of bounds
66 if (x < table_[0].first() || x > table_.last().first())
68 return pTraits<Type>::zero;
71 // Find i such that x(i) < x < x(i+1)
73 while ((table_[i+1].first() < x) && (i+1 < table_.size()))
78 // Linear interpolation to find value. Note constructor needed for
79 // Table<label> to convert intermediate scalar back to label.
82 (x - table_[i].first())/(table_[i+1].first() - table_[i].first())
83 * (table_[i+1].second() - table_[i].second())
90 Type Foam::Table<Type>::integrate(const scalar x1, const scalar x2) const
92 // Initialise return value
93 Type sum = pTraits<Type>::zero;
95 // Return zero if out of bounds
96 if ((x1 > table_.last().first()) || (x2 < table_[0].first()))
101 // Find next index greater than x1
103 while ((table_[id1].first() < x1) && (id1 < table_.size()))
108 // Find next index less than x2
109 label id2 = table_.size() - 1;
110 while ((table_[id2].first() > x2) && (id2 >= 1))
115 if ((id1 - id2) == 1)
117 // x1 and x2 lie within 1 interval
118 sum = 0.5*(value(x1) + value(x2))*(x2 - x1);
122 // x1 and x2 cross multiple intervals
124 // Integrate table body
125 for (label i=id1; i<id2; i++)
128 (table_[i].second() + table_[i+1].second())
129 * (table_[i+1].first() - table_[i].first());
133 // Add table ends (partial segments)
137 * (value(x1) + table_[id1].second())
138 * (table_[id1].first() - x1);
140 if (id2 < table_.size() - 1)
143 * (table_[id2].second() + value(x2))
144 * (x2 - table_[id2].first());
152 // * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * * * //
157 // ************************************************************************* //