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-------------------------------------------------------------------------------
10License
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
32template<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
49template<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
74 }
75}
76
77
78template<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
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);
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
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
167template<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
194template<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
221template<class Type>
223(
224 const ConstantField<Type>& rhs
225)
226:
227 ConstantField<Type>(rhs, rhs.patch())
228{}
229
230
231template<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
255template<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
271template<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
283template<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// ************************************************************************* //
Info<< nl<< "Wrote faMesh in vtk format: "<< writer.output().name()<< nl;}{ vtk::lineWriter writer(aMesh.points(), aMesh.edges(), fileName(aMesh.mesh().time().globalPath()/"finiteArea-edges"));writer.writeGeometry();writer.beginCellData(4);writer.writeProcIDs();{ Field< scalar > fld(faMeshTools::flattenEdgeField(aMesh.magLe(), true))
static bool allowConstructFromLargerSize
Permit read construct from a larger size.
Definition: FieldBase.H:63
Abstract base class to hold the Field mapping addressing and weights.
Definition: FieldMapper.H:50
Generic templated field type.
Definition: Field.H:82
An input stream of tokens.
Definition: ITstream.H:56
const token & peek() const
Failsafe peek at what the next read would return,.
Definition: ITstream.C:352
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
Templated function that returns a constant value.
Definition: ConstantField.H:61
virtual void rmap(const PatchFunction1< Type > &pf1, const labelList &addr)
Reverse map the given PatchFunction1 onto this PatchFunction1.
virtual void autoMap(const FieldMapper &mapper)
Map (and resize as needed) from self given a mapping object.
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
const polyPatch const word const word const dictionary & dict
const polyPatch const word const word const dictionary const bool faceValues
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:70
virtual ITstream & stream() const =0
Return token stream, if entry is a primitive entry.
virtual bool isStream() const noexcept
Return true if this entry is a stream.
Definition: entry.H:223
A class for handling keywords in dictionaries.
Definition: keyType.H:71
label size() const
Number of faces or points on the patch.
const word & name() const noexcept
The patch name.
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:75
@ SPACE
Space [isspace].
Definition: token.H:125
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE)
Definition: tokenI.H:609
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
OBJstream os(runTime.globalPath()/outputName)
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
IOerror FatalIOError
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict