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-2022 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 word& entryName,
83  const entry* eptr,
84  const dictionary& dict,
85  const label len,
86  bool& isUniform,
87  Type& uniformValue
88 )
89 {
90  isUniform = true;
91  uniformValue = Zero;
92 
93  Field<Type> fld;
94 
95  if (!eptr)
96  {
97  if (entryName == dict.dictName())
98  {
99  // The containing dictionary *IS* the Function1 entry
100  // Eg,
101  // uniformValue { type constant; value 1.2; }
102 
103  dict.readEntry("value", uniformValue);
104 
105  fld.resize(len);
106  fld = uniformValue;
107  return fld;
108  }
109  else
110  {
112  << "Null entry" << nl
113  << exit(FatalIOError);
114  }
115  }
116  else if (!eptr->isStream())
117  {
118  // Dictionary format. Eg,
119  // key { type constant; value 1.2; }
120 
121  dict.readEntry("value", uniformValue);
122 
123  fld.resize(len);
124  fld = uniformValue;
125  return fld;
126  }
127 
128  ITstream& is = eptr->stream();
129 
130  if (is.peek().isWord())
131  {
132  const word contentType(is);
133 
134  if (contentType == "constant" || contentType == "uniform")
135  {
136  is >> uniformValue;
137  fld.resize(len);
138  fld = uniformValue;
139  }
140  else if (contentType == "nonuniform")
141  {
142  if (len)
143  {
144  isUniform = false;
145  }
146 
147  is >> static_cast<List<Type>&>(fld);
148  const label lenRead = fld.size();
149  if (len != lenRead)
150  {
151  if
152  (
153  len < lenRead
155  )
156  {
157  #ifdef FULLDEBUG
159  << "Sizes do not match. Truncating " << lenRead
160  << " entries to " << len << endl;
161  #endif
162 
163  // Truncate the data
164  fld.resize(len);
165  }
166  else
167  {
169  << "size " << lenRead
170  << " is not equal to the expected length " << len
171  << exit(FatalIOError);
172  }
173  }
174  }
175  else
176  {
178  << "Expected keyword 'constant', 'uniform', or 'nonuniform'"
179  << ", found " << contentType
180  << exit(FatalIOError);
181  }
182  }
183  else
184  {
185  // Uniform (constant) field
186  is >> uniformValue;
187  fld.resize(len);
188  fld = uniformValue;
189  }
191  return fld;
192 }
193 
194 
195 template<class Type>
197 (
198  const polyPatch& pp,
199  const word& redirectType,
200  const word& entryName,
201  const dictionary& dict,
202  const bool faceValues
203 )
204 :
205  PatchFunction1<Type>(pp, entryName, dict, faceValues),
206  isUniform_(true), // overwritten by getValue()
207  uniformValue_(Zero), // overwritten by getValue()
208  value_
209  (
210  getValue
211  (
212  entryName,
213  dict.findEntry(entryName, keyType::LITERAL),
214  dict,
215  this->size(),
216  isUniform_,
217  uniformValue_
218  )
219  )
220 {}
221 
222 
223 template<class Type>
225 (
226  const polyPatch& pp,
227  const entry* eptr,
228  const word& entryName,
229  const dictionary& dict,
230  const bool faceValues
231 )
232 :
233  PatchFunction1<Type>(pp, entryName, dict, faceValues),
234  isUniform_(true), // overwritten by getValue()
235  uniformValue_(Zero), // overwritten by getValue()
236  value_
237  (
238  getValue
239  (
240  entryName,
241  eptr,
242  dict,
243  this->size(),
244  isUniform_,
245  uniformValue_
246  )
247  )
248 {}
249 
250 
251 template<class Type>
253 (
254  const ConstantField<Type>& rhs
255 )
256 :
257  ConstantField<Type>(rhs, rhs.patch())
258 {}
259 
260 
261 template<class Type>
263 (
264  const ConstantField<Type>& rhs,
265  const polyPatch& pp
266 )
267 :
268  PatchFunction1<Type>(rhs, pp),
269  isUniform_(rhs.isUniform_),
270  uniformValue_(rhs.uniformValue_),
271  value_(rhs.value_)
272 {
273  // If sizes are different...
274  value_.resize(this->size(), Zero);
275 
276  if (isUniform_)
277  {
278  value_ = uniformValue_;
279  }
280 }
281 
282 
283 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
284 
285 template<class Type>
287 (
288  const FieldMapper& mapper
289 )
290 {
291  value_.autoMap(mapper);
292 
293  // If originating from single value override just to make sure
294  if (isUniform_)
295  {
296  value_ = uniformValue_;
297  }
298 }
299 
300 
301 template<class Type>
303 (
304  const PatchFunction1<Type>& pf1,
305  const labelList& addr
306 )
307 {
308  const auto& cst = refCast<const ConstantField<Type>>(pf1);
309  value_.rmap(cst.value_, addr);
310 }
311 
312 
313 template<class Type>
315 (
316  Ostream& os
317 ) const
318 {
320 
321  if (isUniform_)
322  {
323  os.writeKeyword(this->name())
324  << word("constant") << token::SPACE << uniformValue_;
325  os.endEntry();
326  }
327  else
328  {
329  value_.writeEntry(this->name(), os);
330  }
331 }
332 
333 
334 // ************************************************************************* //
A class for handling keywords in dictionaries.
Definition: keyType.H:66
dictionary dict
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
const entry * findEntry(const dictionary &dict, const label val)
Linear search for labelled entry, nullptr if not found.
Templated function that returns a constant value.
Definition: ConstantField.H:62
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:27
label size() const
Number of faces or points on the patch.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
Abstract base class to hold the Field mapping addressing and weights.
Definition: FieldMapper.H:43
virtual void autoMap(const FieldMapper &mapper)
Map (and resize as needed) from self given a mapping object.
Generic templated field type.
Definition: Field.H:62
A class for handling words, derived from Foam::string.
Definition: word.H:63
Space [isspace].
Definition: token.H:131
virtual void rmap(const PatchFunction1< Type > &pf1, const labelList &addr)
Reverse map the given PatchFunction1 onto this PatchFunction1.
virtual void writeData(Ostream &os) const
Write in dictionary format.
bool faceValues() const noexcept
Generate face or point values on patch?
const word & name() const noexcept
The patch name.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
OBJstream os(runTime.globalPath()/outputName)
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))
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
const std::string patch
OpenFOAM patch number as a std::string.
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:69
static bool allowConstructFromLargerSize
Permit read construct from a larger size.
Definition: Field.H:93
virtual void writeData(Ostream &os) const
Write in dictionary format.
uindirectPrimitivePatch pp(UIndirectList< face >(mesh.faces(), faceLabels), mesh.points())
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:63
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127