OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

Table.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "Table.H"
00028 
00029 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00030 
00031 template<class Type>
00032 Foam::Table<Type>::Table(const word& entryName, Istream& is)
00033 :
00034     DataEntry<Type>(entryName),
00035     table_(is)
00036 {
00037     if (!table_.size())
00038     {
00039         FatalErrorIn("Foam::Table<Type>::Table(const Istream&)")
00040             << "Table for entry " << this->name_ << " is invalid (empty)"
00041             << nl << exit(FatalError);
00042     }
00043 }
00044 
00045 
00046 template<class Type>
00047 Foam::Table<Type>::Table(const Table<Type>& tbl)
00048 :
00049     DataEntry<Type>(tbl),
00050     table_(tbl.table_)
00051 {}
00052 
00053 
00054 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00055 
00056 template<class Type>
00057 Foam::Table<Type>::~Table()
00058 {}
00059 
00060 
00061 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00062 
00063 template<class Type>
00064 Type Foam::Table<Type>::value(const scalar x) const
00065 {
00066     // Return zero if out of bounds
00067     if ((x > table_[table_.size()-1].first()) || (x < table_[0].first()))
00068     {
00069         return pTraits<Type>::zero;
00070     }
00071 
00072     // Find i such that x(i) < x < x(i+1)
00073     label i = 0;
00074     while ((table_[i+1].first() < x) && (i+1 < table_.size()))
00075     {
00076         i++;
00077     }
00078 
00079     // Linear interpolation to find value. Note constructor needed for
00080     // Table<label> to convert intermediate scalar back to label.
00081     return Type
00082     (
00083         (x - table_[i].first())/(table_[i+1].first() - table_[i].first())
00084       * (table_[i+1].second() - table_[i].second())
00085       + table_[i].second()
00086     );
00087 }
00088 
00089 
00090 template<class Type>
00091 Type Foam::Table<Type>::integrate(const scalar x1, const scalar x2) const
00092 {
00093     // Initialise return value
00094     Type sum = pTraits<Type>::zero;
00095 
00096     // Return zero if out of bounds
00097     if ((x1 > table_[table_.size()-1].first()) || (x2 < table_[0].first()))
00098     {
00099         return sum;
00100     }
00101 
00102     // Find start index
00103     label id1 = 0;
00104     while ((table_[id1].first() < x1) && (id1 < table_.size()))
00105     {
00106         id1++;
00107     }
00108 
00109     // Find end index
00110     label id2 = table_.size() - 1;
00111     while ((table_[id2].first() > x2) && (id2 >= 1))
00112     {
00113         id2--;
00114     }
00115 
00116     // Integrate table body
00117     for (label i=id1; i<id2; i++)
00118     {
00119         sum +=
00120             (table_[i].second() + table_[i+1].second())
00121           * (table_[i+1].first() - table_[i].first());
00122     }
00123     sum *= 0.5;
00124 
00125     // Add table ends (partial segments)
00126     if (id1 > 0)
00127     {
00128         sum += 0.5
00129           * (value(x1) + table_[id1].second())
00130           * (table_[id1].first() - x1);
00131     }
00132     if (id2 < table_.size() - 1)
00133     {
00134         sum += 0.5
00135           * (table_[id2].second() + value(x2))
00136           * (x2 - table_[id2].first());
00137     }
00138 
00139     return sum;
00140 }
00141 
00142 
00143 // * * * * * * * * * * * * * *  IOStream operators * * * * * * * * * * * * * //
00144 
00145 #include "TableIO.C"
00146 
00147 
00148 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd