1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 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, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
29 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 Foam::Table<Type>::Table(const word& entryName, Istream& is)
34 DataEntry<Type>(entryName),
39 FatalErrorIn("Foam::Table<Type>::Table(const Istream&)")
40 << "Table for entry " << this->name_ << " is invalid (empty)"
41 << nl << exit(FatalError);
47 Foam::Table<Type>::Table(const Table<Type>& tbl)
54 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
57 Foam::Table<Type>::~Table()
61 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
64 Type Foam::Table<Type>::value(const scalar x) const
66 // Return zero if out of bounds
67 if (x < table_[0].first() || x > table_[table_.size()-1].first())
69 return pTraits<Type>::zero;
72 // Find i such that x(i) < x < x(i+1)
74 while ((table_[i+1].first() < x) && (i+1 < table_.size()))
79 // Linear interpolation to find value. Note constructor needed for
80 // Table<label> to convert intermediate scalar back to label.
83 (x - table_[i].first())/(table_[i+1].first() - table_[i].first())
84 * (table_[i+1].second() - table_[i].second())
91 Type Foam::Table<Type>::integrate(const scalar x1, const scalar x2) const
93 // Initialise return value
94 Type sum = pTraits<Type>::zero;
96 // Return zero if out of bounds
97 if ((x1 > table_[table_.size()-1].first()) || (x2 < table_[0].first()))
102 // Find next index greater than x1
104 while ((table_[id1].first() < x1) && (id1 < table_.size()))
109 // Find next index less than x2
110 label id2 = table_.size() - 1;
111 while ((table_[id2].first() > x2) && (id2 >= 1))
116 if ((id1 - id2) == 1)
118 // x1 and x2 lie within 1 interval
119 sum = 0.5*(value(x1) + value(x2))*(x2 - x1);
123 // x1 and x2 cross multiple intervals
125 // Integrate table body
126 for (label i=id1; i<id2; i++)
129 (table_[i].second() + table_[i+1].second())
130 * (table_[i+1].first() - table_[i].first());
134 // Add table ends (partial segments)
138 * (value(x1) + table_[id1].second())
139 * (table_[id1].first() - x1);
141 if (id2 < table_.size() - 1)
144 * (table_[id2].second() + value(x2))
145 * (x2 - table_[id2].first());
153 // * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * * * //
158 // ************************************************************************* //