initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / lagrangian / intermediate / submodels / IO / DataEntry / Table / Table.C
blob98abaf6e67e533db1dfe4d19b0656ba548d9d333
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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 \*---------------------------------------------------------------------------*/
27 #include "Table.H"
29 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
31 template<class Type>
32 Foam::Table<Type>::Table(const word& entryName, Istream& is)
34     DataEntry<Type>(entryName),
35     table_(is)
37     if (!table_.size())
38     {
39         FatalErrorIn("Foam::Table<Type>::Table(const Istream&)")
40             << "Table for entry " << this->name_ << " is invalid (empty)"
41             << nl << exit(FatalError);
42     }
46 template<class Type>
47 Foam::Table<Type>::Table(const Table<Type>& tbl)
49     DataEntry<Type>(tbl),
50     table_(tbl.table_)
54 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
56 template<class Type>
57 Foam::Table<Type>::~Table()
61 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
63 template<class Type>
64 Type Foam::Table<Type>::value(const scalar x) const
66     // Return zero if out of bounds
67     if ((x > table_[table_.size()-1].first()) || (x < table_[0].first()))
68     {
69         return pTraits<Type>::zero;
70     }
72     // Find i such that x(i) < x < x(i+1)
73     label i = 0;
74     while ((table_[i+1].first() < x) && (i+1 < table_.size()))
75     {
76         i++;
77     }
79     // Linear interpolation to find value. Note constructor needed for
80     // Table<label> to convert intermediate scalar back to label.
81     return Type
82     (
83         (x - table_[i].first())/(table_[i+1].first() - table_[i].first())
84       * (table_[i+1].second() - table_[i].second())
85       + table_[i].second()
86     );
90 template<class Type>
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()))
98     {
99         return sum;
100     }
102     // Find start index
103     label id1 = 0;
104     while ((table_[id1].first() < x1) && (id1 < table_.size()))
105     {
106         id1++;
107     }
109     // Find end index
110     label id2 = table_.size() - 1;
111     while ((table_[id2].first() > x2) && (id2 >= 1))
112     {
113         id2--;
114     }
116     // Integrate table body
117     for (label i=id1; i<id2; i++)
118     {
119         sum +=
120             (table_[i].second() + table_[i+1].second())
121           * (table_[i+1].first() - table_[i].first());
122     }
123     sum *= 0.5;
125     // Add table ends (partial segments)
126     if (id1 > 0)
127     {
128         sum += 0.5
129           * (value(x1) + table_[id1].second())
130           * (table_[id1].first() - x1);
131     }
132     if (id2 < table_.size() - 1)
133     {
134         sum += 0.5
135           * (table_[id2].second() + value(x2))
136           * (x2 - table_[id2].first());
137     }
139     return sum;
143 // * * * * * * * * * * * * * *  IOStream operators * * * * * * * * * * * * * //
145 #include "TableIO.C"
148 // ************************************************************************* //