Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
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:
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 {
68 FatalIOErrorInFunction(dict)
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
78template<class Type>
80Foam::PatchFunction1Types::ConstantField<Type>::getValue
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;
92
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);
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;
190
191 return fld;
192}
193
194
195template<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
223template<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
251template<class Type>
253(
255 const polyPatch& pp
256)
257:
258 PatchFunction1<Type>(rhs, pp),
259 isUniform_(rhs.isUniform_),
260 uniformValue_(rhs.uniformValue_),
261 value_(rhs.value_)
262{
263 // If sizes are different...
264 value_.resize(this->size(), Zero);
265
266 if (isUniform_)
268 value_ = uniformValue_;
269 }
270}
271
272
273template<class Type>
275(
276 const ConstantField<Type>& rhs
277)
278:
280{}
281
282
283// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
284
285template<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_)
296 value_ = uniformValue_;
297 }
298}
299
300
301template<class Type>
303(
304 const PatchFunction1<Type>& pf1,
305 const labelList& addr
306)
308 const auto& cst = refCast<const ConstantField<Type>>(pf1);
309 value_.rmap(cst.value_, addr);
310}
311
312
313template<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// ************************************************************************* //
Info<< nl;Info<< "Write faMesh in vtk format:"<< nl;{ vtk::uindirectPatchWriter writer(aMesh.patch(), fileName(aMesh.time().globalPath()/vtkBaseFileName));writer.writeGeometry();globalIndex procAddr(aMesh.nFaces());labelList cellIDs;if(UPstream::master()) { cellIDs.resize(procAddr.totalSize());for(const labelRange &range :procAddr.ranges()) { auto slice=cellIDs.slice(range);slice=identity(range);} } writer.beginCellData(4);writer.writeProcIDs();writer.write("cellID", cellIDs);writer.write("area", aMesh.S().field());writer.write("normal", aMesh.faceAreaNormals());writer.beginPointData(1);writer.write("normal", aMesh.pointAreaNormals());Info<< " "<< writer.output().name()<< nl;}{ vtk::lineWriter writer(aMesh.points(), aMesh.edges(), fileName(aMesh.time().globalPath()/(vtkBaseFileName+"-edges")));writer.writeGeometry();writer.beginCellData(4);writer.writeProcIDs();{ Field< scalar > fld(faMeshTools::flattenEdgeField(aMesh.magLe(), true))
uindirectPrimitivePatch pp(UIndirectList< face >(mesh.faces(), faceLabels), mesh.points())
static bool allowConstructFromLargerSize
Permit read construct from a larger size.
Definition Field.H:100
Abstract base class to hold the Field mapping addressing and weights.
Definition FieldMapper.H:44
Generic templated field type that is much like a Foam::List except that it is expected to hold numeri...
Definition Field.H:172
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
Templated function that returns a constant value.
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.
virtual void writeData(Ostream &os) const
Write in dictionary format.
ConstantField(const polyPatch &pp, const word &entryName, const Type &uniformValue, const dictionary &dict=dictionary::null, const bool faceValues=true)
Construct from a uniform value.
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 & entryName
virtual void writeData(Ostream &os) const
Write in dictionary format.
const polyPatch const word const word const dictionary & dict
const polyPatch & pp
const polyPatch const word const word const dictionary const bool faceValues
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A keyword and a list of tokens is an 'entry'.
Definition entry.H:66
virtual ITstream & stream() const =0
Return token stream, if entry is a primitive entry.
virtual bool isStream() const noexcept
True if this entry is a stream.
Definition entry.H:267
A class for handling keywords in dictionaries.
Definition keyType.H:69
const polyPatch & patch() const noexcept
Reference to the patch.
label size() const
Number of faces or points on the patch.
A patch is a list of labels that address the faces in the global face list.
Definition polyPatch.H:73
@ SPACE
Space [isspace].
Definition token.H:144
A class for handling words, derived from Foam::string.
Definition word.H:66
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
OBJstream os(runTime.globalPath()/outputName)
auto & name
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
Type & refCast(U &obj)
A dynamic_cast (for references) to Type reference.
Definition typeInfo.H:172
List< label > labelList
A List of labels.
Definition List.H:62
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict