ConstantField.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2018-2020 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "ConstantField.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class Type>
34 (
35  const polyPatch& pp,
36  const word& entryName,
37  const Type& uniformValue,
38  const dictionary& dict,
39  const bool faceValues
40 )
41 :
42  PatchFunction1<Type>(pp, entryName, dict, faceValues),
43  isUniform_(true),
44  uniformValue_(uniformValue),
45  value_(this->size(), uniformValue_)
46 {}
47 
48 
49 template<class Type>
51 (
52  const polyPatch& pp,
53  const word& entryName,
54  const bool isUniform,
55  const Type& uniformValue,
56  const Field<Type>& fieldValues,
57  const dictionary& dict,
58  const bool faceValues
59 )
60 :
61  PatchFunction1<Type>(pp, entryName, dict, faceValues),
62  isUniform_(isUniform),
63  uniformValue_(uniformValue),
64  value_(fieldValues)
65 {
66  if (fieldValues.size() != this->size())
67  {
69  << "Supplied field size " << fieldValues.size()
70  << " is not equal to the number of "
71  << (faceValues ? "faces" : "points") << ' '
72  << this->size() << " of patch " << pp.name() << nl
73  << exit(FatalIOError);
74  }
75 }
76 
77 
78 template<class Type>
81 (
82  const entry* eptr,
83  const dictionary& dict,
84  const label len,
85  bool& isUniform,
86  Type& uniformValue
87 )
88 {
89  isUniform = true;
91 
93 
94  if (len)
95  {
96  if (!eptr || !eptr->isStream())
97  {
99  << "Null or invalid entry" << nl
100  << exit(FatalIOError);
101  }
102  ITstream& is = eptr->stream();
103 
104  // Read first token
105  token firstToken(is);
106 
107  if (firstToken.isWord())
108  {
109  if
110  (
111  firstToken.wordToken() == "uniform"
112  || firstToken.wordToken() == "constant"
113  )
114  {
115  is >> uniformValue;
116  fld.setSize(len);
117  fld = uniformValue;
118  }
119  else if (firstToken.wordToken() == "nonuniform")
120  {
121  List<Type>& list = fld;
122  is >> list;
123  isUniform = false;
124 
125  const label currentSize = fld.size();
126  if (currentSize != len)
127  {
128  if
129  (
130  len < currentSize
131  && FieldBase::allowConstructFromLargerSize
132  )
133  {
134  #ifdef FULLDEBUG
136  << "Sizes do not match. "
137  << "Re-sizing " << currentSize
138  << " entries to " << len
139  << endl;
140  #endif
141 
142  // Resize (shrink) the data
143  fld.setSize(len);
144  }
145  else
146  {
148  << "size " << fld.size()
149  << " is not equal to the given value of " << len
150  << exit(FatalIOError);
151  }
152  }
153  }
154  else
155  {
156  isUniform = false;
158  << "Expected keyword 'uniform', 'nonuniform' or 'constant'"
159  << ", found " << firstToken.wordToken()
160  << exit(FatalIOError);
161  }
162  }
163  else
164  {
165  is.putBack(firstToken);
166  is >> uniformValue;
167  fld.setSize(len);
168  fld = uniformValue;
169  }
170  }
171 
172  return fld;
173 }
174 
175 
176 template<class Type>
178 (
179  const polyPatch& pp,
180  const word& redirectType,
181  const word& entryName,
182  const dictionary& dict,
183  const bool faceValues
184 )
185 :
186  PatchFunction1<Type>(pp, entryName, dict, faceValues),
187  isUniform_(true), // overwritten by getValue()
188  uniformValue_(Zero), // overwritten by getValue()
189  value_
190  (
191  getValue
192  (
193  dict.findEntry(entryName, keyType::LITERAL),
194  dict,
195  this->size(),
196  isUniform_,
197  uniformValue_
198  )
199  )
200 {}
201 
202 
203 template<class Type>
205 (
206  const polyPatch& pp,
207  const entry* eptr,
208  const word& entryName,
209  const dictionary& dict,
210  const bool faceValues
211 )
212 :
213  PatchFunction1<Type>(pp, entryName, dict, faceValues),
214  isUniform_(true), // overwritten by getValue()
215  uniformValue_(Zero), // overwritten by getValue()
216  value_
217  (
218  getValue
219  (
220  eptr,
221  dict,
222  this->size(),
223  isUniform_,
224  uniformValue_
225  )
226  )
227 {}
228 
229 
230 template<class Type>
232 (
233  const ConstantField<Type>& rhs
234 )
235 :
236  ConstantField<Type>(rhs, rhs.patch())
237 {}
238 
239 
240 template<class Type>
242 (
243  const ConstantField<Type>& rhs,
244  const polyPatch& pp
245 )
246 :
247  PatchFunction1<Type>(rhs, pp),
248  isUniform_(rhs.isUniform_),
249  uniformValue_(rhs.uniformValue_),
250  value_(rhs.value_)
251 {
252  // If sizes are different...
253  value_.resize(this->size(), Zero);
254 
255  if (isUniform_)
256  {
257  value_ = uniformValue_;
258  }
259 }
260 
261 
262 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
263 
264 template<class Type>
266 (
267  const FieldMapper& mapper
268 )
269 {
270  value_.autoMap(mapper);
271 
272  // If originating from single value override just to make sure
273  if (isUniform_)
274  {
275  value_ = uniformValue_;
276  }
277 }
278 
279 
280 template<class Type>
282 (
283  const PatchFunction1<Type>& pf1,
284  const labelList& addr
285 )
286 {
287  const auto& cst = refCast<const ConstantField<Type>>(pf1);
288  value_.rmap(cst.value_, addr);
289 }
290 
291 
292 template<class Type>
294 (
295  Ostream& os
296 ) const
297 {
299 
300  if (isUniform_)
301  {
302  os.writeKeyword(this->name())
303  << word("constant") << token::SPACE << uniformValue_
304  << token::END_STATEMENT << nl;
305  }
306  else
307  {
308  value_.writeEntry(this->name(), os);
309  }
310 }
311 
312 
313 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
ConstantField.H
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::entry::stream
virtual ITstream & stream() const =0
Return token stream, if entry is a primitive entry.
Foam::PatchFunction1Types::ConstantField::writeData
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition: ConstantField.C:294
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::PatchFunction1Types::ConstantField
Templated function that returns a constant value.
Definition: ConstantField.H:58
writeData
const bool writeData(pdfDictionary.get< bool >("writeData"))
Foam::FieldMapper
Abstract base class to hold the Field mapping addressing and weights.
Definition: FieldMapper.H:49
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::entry::isStream
virtual bool isStream() const
Return true if this entry is a stream.
Definition: entry.H:212
Foam::Field
Generic templated field type.
Definition: Field.H:63
Foam::ITstream
An input stream of tokens.
Definition: ITstream.H:55
Foam::PatchFunction1Types::ConstantField::ConstantField
ConstantField(const polyPatch &pp, const word &entryName, const Type &uniformValue, const dictionary &dict=dictionary::null, const bool faceValues=true)
Construct from a uniform value.
Definition: ConstantField.C:34
Foam::polyPatch
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:68
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
fld
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< ' ';}gmvFile<< nl;for(const word &name :lagrangianScalarNames){ IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputLagrangian.H:23
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::PatchFunction1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: PatchFunction1.H:59
Foam::Ostream::writeKeyword
virtual Ostream & writeKeyword(const keyType &kw)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:57
Foam::PatchFunction1Types::ConstantField::autoMap
virtual void autoMap(const FieldMapper &mapper)
Map (and resize as needed) from self given a mapping object.
Definition: ConstantField.C:266
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::List< label >
Foam::Istream::putBack
void putBack(const token &tok)
Put back token.
Definition: Istream.C:53
Foam::PatchFunction1Types::ConstantField::rmap
virtual void rmap(const PatchFunction1< Type > &pf1, const labelList &addr)
Reverse map the given PatchFunction1 onto this PatchFunction1.
Definition: ConstantField.C:282
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
IOWarningInFunction
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
Definition: messageStream.H:315
Foam::uniformValue
Definition: uniformValue.H:50
Foam::patchIdentifier::name
const word & name() const
The patch name.
Definition: patchIdentifier.H:134