FreeFOAM The Cross-Platform CFD Toolkit
Table.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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.
20 
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/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
27 
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
30 template<class Type>
31 Foam::Table<Type>::Table(const word& entryName, Istream& is)
32 :
33  DataEntry<Type>(entryName),
34  table_(is)
35 {
36  if (!table_.size())
37  {
38  FatalErrorIn("Foam::Table<Type>::Table(const Istream&)")
39  << "Table for entry " << this->name_ << " is invalid (empty)"
40  << nl << exit(FatalError);
41  }
42 }
43 
44 
45 template<class Type>
47 :
48  DataEntry<Type>(tbl),
49  table_(tbl.table_)
50 {}
51 
52 
53 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
54 
55 template<class Type>
57 {}
58 
59 
60 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
61 
62 template<class Type>
63 Type Foam::Table<Type>::value(const scalar x) const
64 {
65  // Return zero if out of bounds
66  if (x < table_[0].first() || x > table_[table_.size()-1].first())
67  {
68  return pTraits<Type>::zero;
69  }
70 
71  // Find i such that x(i) < x < x(i+1)
72  label i = 0;
73  while ((table_[i+1].first() < x) && (i+1 < table_.size()))
74  {
75  i++;
76  }
77 
78  // Linear interpolation to find value. Note constructor needed for
79  // Table<label> to convert intermediate scalar back to label.
80  return Type
81  (
82  (x - table_[i].first())/(table_[i+1].first() - table_[i].first())
83  * (table_[i+1].second() - table_[i].second())
84  + table_[i].second()
85  );
86 }
87 
88 
89 template<class Type>
90 Type Foam::Table<Type>::integrate(const scalar x1, const scalar x2) const
91 {
92  // Initialise return value
93  Type sum = pTraits<Type>::zero;
94 
95  // Return zero if out of bounds
96  if ((x1 > table_[table_.size()-1].first()) || (x2 < table_[0].first()))
97  {
98  return sum;
99  }
100 
101  // Find next index greater than x1
102  label id1 = 0;
103  while ((table_[id1].first() < x1) && (id1 < table_.size()))
104  {
105  id1++;
106  }
107 
108  // Find next index less than x2
109  label id2 = table_.size() - 1;
110  while ((table_[id2].first() > x2) && (id2 >= 1))
111  {
112  id2--;
113  }
114 
115  if ((id1 - id2) == 1)
116  {
117  // x1 and x2 lie within 1 interval
118  sum = 0.5*(value(x1) + value(x2))*(x2 - x1);
119  }
120  else
121  {
122  // x1 and x2 cross multiple intervals
123 
124  // Integrate table body
125  for (label i=id1; i<id2; i++)
126  {
127  sum +=
128  (table_[i].second() + table_[i+1].second())
129  * (table_[i+1].first() - table_[i].first());
130  }
131  sum *= 0.5;
132 
133  // Add table ends (partial segments)
134  if (id1 > 0)
135  {
136  sum += 0.5
137  * (value(x1) + table_[id1].second())
138  * (table_[id1].first() - x1);
139  }
140  if (id2 < table_.size() - 1)
141  {
142  sum += 0.5
143  * (table_[id2].second() + value(x2))
144  * (x2 - table_[id2].first());
145  }
146  }
147 
148  return sum;
149 }
150 
151 
152 // * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * * * //
153 
154 #include "TableIO.C"
155 
156 
157 // ************************ vim: set sw=4 sts=4 et: ************************ //