1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2008-2009 OpenCFD Ltd.
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 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
32 Foam::label Foam::interpolationLookUpTable<Type>::index
34 const List<scalar>& indices,
43 for (int j = i + 1; j < dim_.size(); j++)
52 max(label((indices[i] - min_[i])/delta_[i]), 0),
59 label iLastdim = dim_.size() - 1;
60 totalIndex += Foam::min
64 label((indices[iLastdim] - min_[iLastdim])/delta_[iLastdim]),
76 Foam::label Foam::interpolationLookUpTable<Type>::index
87 label((indice - min_[i])/delta_[i]),
98 bool Foam::interpolationLookUpTable<Type>::checkRange
100 const scalar lookUpValue,
101 const label interfield
104 if (lookUpValue >= min_[interfield] && lookUpValue <= max_[interfield])
116 Foam::scalar Foam::interpolationLookUpTable<Type>::interpolate
120 const scalar lookUpValue,
122 const label interfield
127 List<scalarField>::operator[](interfield).operator[](hi)
128 != List<scalarField>::operator[](interfield).operator[](lo)
133 List<scalarField>::operator[](ofield).operator[](lo)
135 List<scalarField>::operator[](ofield).operator[](hi)
136 - List<scalarField>::operator[](ofield).operator[](lo)
140 - List<scalarField>::operator[](interfield).operator[](lo)
143 List<scalarField>::operator[](interfield).operator[](hi)
144 - List<scalarField>::operator[](interfield).operator[](lo)
151 return List<scalarField>::operator[](ofield).operator[](lo);
157 void Foam::interpolationLookUpTable<Type>::dimensionTable()
159 min_.setSize(entries_.size());
160 dim_.setSize(entries_.size());
161 delta_.setSize(entries_.size());
162 max_.setSize(entries_.size());
163 entryIndices_.setSize(entries_.size());
164 outputIndices_.setSize(output_.size());
170 dim_[i] = readLabel(entries_[i].lookup("N"));
171 max_[i] = readScalar(entries_[i].lookup("max"));
172 min_[i] = readScalar(entries_[i].lookup("min"));
173 delta_[i] = (max_[i] - min_[i])/dim_[i];
174 tableDim *= dim_[i] + 1;
175 fieldIndices_.insert(entries_[i].lookup("name"), index);
176 entryIndices_[i] = index;
182 fieldIndices_.insert(output_[i].lookup("name"), index);
183 outputIndices_[i] = index;
187 List<scalarField>& internal = *this;
189 internal.setSize(entries_.size() + output_.size());
191 interpOutput_.setSize(entries_.size() + output_.size());
195 internal[i].setSize(tableDim);
201 void Foam::interpolationLookUpTable<Type>::readTable
203 const word& instance,
219 control.lookup("fields") >> entries_;
220 control.lookup("output") >> output_;
221 control.lookup("values") >> *this;
227 if (this->size() == 0)
231 "Foam::interpolationLookUpTable<Type>::readTable()"
232 ) << "table is empty" << nl << exit(FatalError);
237 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
240 Foam::interpolationLookUpTable<Type>::interpolationLookUpTable()
243 fileName_("fileNameIsUndefined")
248 Foam::interpolationLookUpTable<Type>::interpolationLookUpTable
250 const fileName& fn, const word& instance, const fvMesh& mesh
265 readTable(instance, mesh);
270 Foam::interpolationLookUpTable<Type>::interpolationLookUpTable
272 const interpolationLookUpTable& interpTable
275 List<scalarField>(interpTable),
276 fileName_(interpTable.fileName_),
277 entryIndices_(interpTable.entryIndices_),
278 outputIndices_(interpTable.outputIndices_),
279 dim_(interpTable.dim_),
280 min_(interpTable.min_),
281 delta_(interpTable.delta_),
282 max_(interpTable.max_),
285 interpOutput_(interpTable.interpOutput_)
290 Foam::interpolationLookUpTable<Type>::interpolationLookUpTable
292 const dictionary& dict
296 fileName_(fileName(dict.lookup("fileName")).expand()),
301 entries_(dict.lookup("fields")),
302 output_(dict.lookup("output")),
312 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
315 void Foam::interpolationLookUpTable<Type>::check() const
317 // check order in the first dimension.
318 scalar prevValue = List<scalarField>::operator[](0).operator[](0);
320 for (int j = 1; j < dim_.size(); j++)
325 for (label i = 1; i < dim_[0]; i++)
328 const scalar currValue =
329 List<scalarField>::operator[](0).operator[](index);
331 // avoid duplicate values (divide-by-zero error)
332 if (currValue <= prevValue)
336 "Foam::interpolationLookUpTable<Type>::checkOrder() const"
337 ) << "out-of-order value: " << currValue
338 << " at index " << index << nl << exit(FatalError);
340 prevValue = currValue;
346 void Foam::interpolationLookUpTable<Type>::write
350 const word& instance,
366 control.writeHeader(os);
368 os.writeKeyword("fields");
369 os << entries_ << token::END_STATEMENT << nl;
371 os.writeKeyword("output");
372 os << output_ << token::END_STATEMENT << nl;
374 if (this->size() == 0)
378 "Foam::interpolationTable<Type>::write()"
379 ) << "table is empty" << nl << exit(FatalError);
381 os.writeKeyword("values");
382 os << *this << token::END_STATEMENT << nl;
386 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
390 Foam::interpolationLookUpTable<Type>::operator[](const label i)
393 label n = this->size();
399 "Foam::interpolationLookUpTable<Type>::operator[](const label)"
400 ) << "table has (" << n << ") columns" << nl << exit(FatalError);
406 "Foam::interpolationLookUpTable<Type>::operator[](const label)"
407 ) << "index (" << ii << ") underflow" << nl << exit(FatalError);
413 "Foam::interpolationLookUpTable<Type>::operator[](const label)"
414 ) << "index (" << ii << ") overflow" << nl << exit(FatalError);
417 return List<scalarField>::operator[](ii);
422 const Foam::scalarField&
423 Foam::interpolationLookUpTable<Type>::operator[](const label i) const
426 label n = this->size();
432 "Foam::interpolationLookUpTable<Type>::operator[]"
433 "(const label) const"
434 ) << "table has (" << n << ") columns" << nl << exit(FatalError);
440 "Foam::interpolationLookUpTable<Type>::operator[]"
441 "(const label) const"
442 ) << "index (" << ii << ") underflow" << nl << exit(FatalError);
449 "Foam::interpolationLookUpTable<Type>::operator[]"
450 "(const label) const"
451 ) << "index (" << ii << ") overflow" << nl
455 return List<scalarField>::operator[](ii);
460 bool Foam::interpolationLookUpTable<Type>::found(const word& fieldName) const
462 return fieldIndices_.found(fieldName);
467 const Foam::scalarList&
468 Foam::interpolationLookUpTable<Type>::lookUp(const scalar retvals)
470 const label lo = index(retvals);
472 return interpOutput_;
477 void Foam::interpolationLookUpTable<Type>::findHi
483 forAll(outputIndices_,j)
486 label ofield = outputIndices_[j];
487 scalar baseValue = List<scalarField>::operator[](ofield).operator[](lo);
489 forAll(entryIndices_,i)
491 if (checkRange(retvals, entryIndices_[i]))
495 label hi = Foam::min(lo + dim, (*this)[0].size() - 1);
497 tmp += interpolate(lo, hi, retvals, ofield, entryIndices_[i])
500 interpOutput_[entryIndices_[i]] = retvals;
504 interpOutput_[outputIndices_[j]] = tmp;
509 // ************************************************************************* //