FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include <lagrangianIntermediate/Table.H>
00027 
00028 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00029 
00030 template<class Type>
00031 Foam::Table<Type>::Table(const word& entryName, Istream& is)
00032 :
00033     DataEntry<Type>(entryName),
00034     table_(is)
00035 {
00036     if (!table_.size())
00037     {
00038         FatalErrorIn("Foam::Table<Type>::Table(const Istream&)")
00039             << "Table for entry " << this->name_ << " is invalid (empty)"
00040             << nl << exit(FatalError);
00041     }
00042 }
00043 
00044 
00045 template<class Type>
00046 Foam::Table<Type>::Table(const Table<Type>& tbl)
00047 :
00048     DataEntry<Type>(tbl),
00049     table_(tbl.table_)
00050 {}
00051 
00052 
00053 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00054 
00055 template<class Type>
00056 Foam::Table<Type>::~Table()
00057 {}
00058 
00059 
00060 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00061 
00062 template<class Type>
00063 Type Foam::Table<Type>::value(const scalar x) const
00064 {
00065     // Return zero if out of bounds
00066     if (x < table_[0].first() || x > table_[table_.size()-1].first())
00067     {
00068         return pTraits<Type>::zero;
00069     }
00070 
00071     // Find i such that x(i) < x < x(i+1)
00072     label i = 0;
00073     while ((table_[i+1].first() < x) && (i+1 < table_.size()))
00074     {
00075         i++;
00076     }
00077 
00078     // Linear interpolation to find value. Note constructor needed for
00079     // Table<label> to convert intermediate scalar back to label.
00080     return Type
00081     (
00082         (x - table_[i].first())/(table_[i+1].first() - table_[i].first())
00083       * (table_[i+1].second() - table_[i].second())
00084       + table_[i].second()
00085     );
00086 }
00087 
00088 
00089 template<class Type>
00090 Type Foam::Table<Type>::integrate(const scalar x1, const scalar x2) const
00091 {
00092     // Initialise return value
00093     Type sum = pTraits<Type>::zero;
00094 
00095     // Return zero if out of bounds
00096     if ((x1 > table_[table_.size()-1].first()) || (x2 < table_[0].first()))
00097     {
00098         return sum;
00099     }
00100 
00101     // Find next index greater than x1
00102     label id1 = 0;
00103     while ((table_[id1].first() < x1) && (id1 < table_.size()))
00104     {
00105         id1++;
00106     }
00107 
00108     // Find next index less than x2
00109     label id2 = table_.size() - 1;
00110     while ((table_[id2].first() > x2) && (id2 >= 1))
00111     {
00112         id2--;
00113     }
00114 
00115     if ((id1 - id2) == 1)
00116     {
00117         // x1 and x2 lie within 1 interval
00118         sum = 0.5*(value(x1) + value(x2))*(x2 - x1);
00119     }
00120     else
00121     {
00122         // x1 and x2 cross multiple intervals
00123 
00124         // Integrate table body
00125         for (label i=id1; i<id2; i++)
00126         {
00127             sum +=
00128                 (table_[i].second() + table_[i+1].second())
00129               * (table_[i+1].first() - table_[i].first());
00130         }
00131         sum *= 0.5;
00132 
00133         // Add table ends (partial segments)
00134         if (id1 > 0)
00135         {
00136             sum += 0.5
00137               * (value(x1) + table_[id1].second())
00138               * (table_[id1].first() - x1);
00139         }
00140         if (id2 < table_.size() - 1)
00141         {
00142             sum += 0.5
00143               * (table_[id2].second() + value(x2))
00144               * (x2 - table_[id2].first());
00145         }
00146     }
00147 
00148     return sum;
00149 }
00150 
00151 
00152 // * * * * * * * * * * * * * *  IOStream operators * * * * * * * * * * * * * //
00153 
00154 #include "TableIO.C"
00155 
00156 
00157 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines