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-2021 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 (!eptr || !eptr->isStream())
95  {
97  << "Null or invalid entry" << nl
98  << exit(FatalIOError);
99  }
100  ITstream& is = eptr->stream();
101 
102  if (is.peek().isWord())
103  {
104  const word contentType(is);
105 
106  if (contentType == "constant" || contentType == "uniform")
107  {
108  is >> uniformValue;
109  fld.resize(len);
110  fld = uniformValue;
111  }
112  else if (contentType == "nonuniform")
113  {
114  if (len)
115  {
116  isUniform = false;
117  }
118 
119  is >> static_cast<List<Type>&>(fld);
120  const label lenRead = fld.size();
121  if (len != lenRead)
122  {
123  if
124  (
125  len < lenRead
126  && FieldBase::allowConstructFromLargerSize
127  )
128  {
129  #ifdef FULLDEBUG
131  << "Sizes do not match. Truncating " << lenRead
132  << " entries to " << len << endl;
133  #endif
134 
135  // Truncate the data
136  fld.resize(len);
137  }
138  else
139  {
141  << "size " << lenRead
142  << " is not equal to the expected length " << len
143  << exit(FatalIOError);
144  }
145  }
146  }
147  else
148  {
150  << "Expected keyword 'constant', 'uniform', or 'nonuniform'"
151  << ", found " << contentType
152  << exit(FatalIOError);
153  }
154  }
155  else
156  {
157  // Uniform (constant) field
158  is >> uniformValue;
159  fld.resize(len);
160  fld = uniformValue;
161  }
162 
163  return fld;
164 }
165 
166 
167 template<class Type>
169 (
170  const polyPatch& pp,
171  const word& redirectType,
172  const word& entryName,
173  const dictionary& dict,
174  const bool faceValues
175 )
176 :
177  PatchFunction1<Type>(pp, entryName, dict, faceValues),
178  isUniform_(true), // overwritten by getValue()
179  uniformValue_(Zero), // overwritten by getValue()
180  value_
181  (
182  getValue
183  (
184  dict.findEntry(entryName, keyType::LITERAL),
185  dict,
186  this->size(),
187  isUniform_,
188  uniformValue_
189  )
190  )
191 {}
192 
193 
194 template<class Type>
196 (
197  const polyPatch& pp,
198  const entry* eptr,
199  const word& entryName,
200  const dictionary& dict,
201  const bool faceValues
202 )
203 :
204  PatchFunction1<Type>(pp, entryName, dict, faceValues),
205  isUniform_(true), // overwritten by getValue()
206  uniformValue_(Zero), // overwritten by getValue()
207  value_
208  (
209  getValue
210  (
211  eptr,
212  dict,
213  this->size(),
214  isUniform_,
215  uniformValue_
216  )
217  )
218 {}
219 
220 
221 template<class Type>
223 (
224  const ConstantField<Type>& rhs
225 )
226 :
227  ConstantField<Type>(rhs, rhs.patch())
228 {}
229 
230 
231 template<class Type>
233 (
234  const ConstantField<Type>& rhs,
235  const polyPatch& pp
236 )
237 :
238  PatchFunction1<Type>(rhs, pp),
239  isUniform_(rhs.isUniform_),
240  uniformValue_(rhs.uniformValue_),
241  value_(rhs.value_)
242 {
243  // If sizes are different...
244  value_.resize(this->size(), Zero);
245 
246  if (isUniform_)
247  {
248  value_ = uniformValue_;
249  }
250 }
251 
252 
253 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
254 
255 template<class Type>
257 (
258  const FieldMapper& mapper
259 )
260 {
261  value_.autoMap(mapper);
262 
263  // If originating from single value override just to make sure
264  if (isUniform_)
265  {
266  value_ = uniformValue_;
267  }
268 }
269 
270 
271 template<class Type>
273 (
274  const PatchFunction1<Type>& pf1,
275  const labelList& addr
276 )
277 {
278  const auto& cst = refCast<const ConstantField<Type>>(pf1);
279  value_.rmap(cst.value_, addr);
280 }
281 
282 
283 template<class Type>
285 (
286  Ostream& os
287 ) const
288 {
290 
291  if (isUniform_)
292  {
293  os.writeKeyword(this->name())
294  << word("constant") << token::SPACE << uniformValue_;
295  os.endEntry();
296  }
297  else
298  {
299  value_.writeEntry(this->name(), os);
300  }
301 }
302 
303 
304 // ************************************************************************* //
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:65
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:285
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::entry::isStream
virtual bool isStream() const noexcept
Return true if this entry is a stream.
Definition: entry.H:223
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:369
Foam::token::isWord
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE)
Definition: tokenI.H:609
Foam::Field
Generic templated field type.
Definition: Field.H:63
Foam::ITstream
An input stream of tokens.
Definition: ITstream.H:52
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
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:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam::PatchFunction1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: PatchFunction1.H:60
Foam::PatchFunction1Types::ConstantField::autoMap
virtual void autoMap(const FieldMapper &mapper)
Map (and resize as needed) from self given a mapping object.
Definition: ConstantField.C:257
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::List< label >
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:273
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::patchIdentifier::name
const word & name() const noexcept
The patch name.
Definition: patchIdentifier.H:135
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:340
Foam::uniformValue
Definition: uniformValue.H:50
Foam::ITstream::peek
const token & peek() const
Failsafe peek at what the next read would return,.
Definition: ITstream.C:352