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-2019 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 bool isUniform,
38  const Type& uniformValue,
39  const Field<Type>& nonUniformValue,
40  const dictionary& dict,
41  const bool faceValues
42 )
43 :
44  PatchFunction1<Type>(pp, entryName, dict, faceValues),
45  isUniform_(isUniform),
46  uniformValue_(uniformValue),
47  value_(nonUniformValue)
48 {
49  if (faceValues && nonUniformValue.size() != pp.size())
50  {
52  << "Supplied field size " << nonUniformValue.size()
53  << " is not equal to the number of faces " << pp.size()
54  << " of patch " << pp.name() << exit(FatalIOError);
55  }
56  else if (!faceValues && nonUniformValue.size() != pp.nPoints())
57  {
59  << "Supplied field size " << nonUniformValue.size()
60  << " is not equal to the number of points " << pp.nPoints()
61  << " of patch " << pp.name() << exit(FatalIOError);
62  }
63 }
64 
65 
66 template<class Type>
68 (
69  const word& keyword,
70  const dictionary& dict,
71  const label len,
72  bool& isUniform,
73  Type& uniformValue
74 )
75 {
76  isUniform = true;
78 
80 
81  if (len)
82  {
83  ITstream& is = dict.lookup(keyword);
84 
85  // Read first token
86  token firstToken(is);
87 
88  if (firstToken.isWord())
89  {
90  if
91  (
92  firstToken.wordToken() == "uniform"
93  || firstToken.wordToken() == "constant"
94  )
95  {
96  is >> uniformValue;
97  fld.setSize(len);
98  fld = uniformValue;
99  }
100  else if (firstToken.wordToken() == "nonuniform")
101  {
102  List<Type>& list = fld;
103  is >> list;
104  isUniform = false;
105 
106  label currentSize = fld.size();
107  if (currentSize != len)
108  {
109  if
110  (
111  len < currentSize
112  && FieldBase::allowConstructFromLargerSize
113  )
114  {
115  #ifdef FULLDEBUG
117  << "Sizes do not match. "
118  << "Re-sizing " << currentSize
119  << " entries to " << len
120  << endl;
121  #endif
122 
123  // Resize the data
124  fld.setSize(len);
125  }
126  else
127  {
129  << "size " << fld.size()
130  << " is not equal to the given value of " << len
131  << exit(FatalIOError);
132  }
133  }
134  }
135  else
136  {
137  isUniform = false;
139  << "Expected keyword 'uniform', 'nonuniform' or 'constant'"
140  << ", found " << firstToken.wordToken()
141  << exit(FatalIOError);
142  }
143  }
144  else
145  {
146  is.putBack(firstToken);
147  is >> uniformValue;
148  fld.setSize(len);
149  fld = uniformValue;
150  }
151  }
152 
153  return fld;
154 }
155 
156 
157 template<class Type>
159 (
160  const polyPatch& pp,
161  const word& type,
162  const word& entryName,
163  const dictionary& dict,
164  const bool faceValues
165 )
166 :
167  PatchFunction1<Type>(pp, entryName, dict, faceValues),
168  value_
169  (
170  getValue
171  (
172  entryName,
173  dict,
174  (faceValues ? pp.size() : pp.nPoints()),
175  isUniform_,
176  uniformValue_
177  )
178  )
179 {}
180 
181 
182 template<class Type>
184 (
185  const ConstantField<Type>& cnst
186 )
187 :
188  PatchFunction1<Type>(cnst),
189  isUniform_(cnst.isUniform_),
190  uniformValue_(cnst.uniformValue_),
191  value_(cnst.value_)
192 {}
193 
194 
195 template<class Type>
197 (
198  const ConstantField<Type>& cnst,
199  const polyPatch& pp
200 )
201 :
202  PatchFunction1<Type>(cnst, pp),
203  isUniform_(cnst.isUniform_),
204  uniformValue_(cnst.uniformValue_),
205  value_(cnst.value_)
206 {
207  // If different sizes do what?
208  value_.setSize
209  (
210  this->faceValues_
211  ? this->patch_.size()
212  : this->patch_.nPoints()
213  );
214  if (isUniform_)
215  {
216  value_ = uniformValue_;
217  }
218 }
219 
220 
221 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
222 
223 template<class Type>
225 (
226  const FieldMapper& mapper
227 )
228 {
229  value_.autoMap(mapper);
230 
231  // If originating from single value override just to make sure
232  if (isUniform_)
233  {
234  value_ = uniformValue_;
235  }
236 }
237 
238 
239 template<class Type>
241 (
242  const PatchFunction1<Type>& pf1,
243  const labelList& addr
244 )
245 {
246  const auto& cst = refCast<const ConstantField<Type>>(pf1);
247  value_.rmap(cst.value_, addr);
248 }
249 
250 
251 template<class Type>
253 (
254  Ostream& os
255 ) const
256 {
258 
259  if (isUniform_)
260  {
261  os.writeKeyword(this->name_)
262  << "constant " << uniformValue_
263  << token::END_STATEMENT << nl;
264  }
265  else
266  {
267  value_.writeEntry(this->name_, os);
268  }
269 }
270 
271 
272 // ************************************************************************* //
ConstantField.H
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::PatchFunction1Types::ConstantField::writeData
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition: ConstantField.C:253
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
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:337
Foam::token
A token holds an item read from Istream.
Definition: token.H:69
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::Field
Generic templated field type.
Definition: Field.H:63
Foam::ITstream
An input stream of tokens.
Definition: ITstream.H:55
Foam::polyPatch
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:66
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::PrimitivePatch::nPoints
label nPoints() const
Return number of points supporting patch faces.
Definition: PrimitivePatch.H:311
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:63
Foam::Ostream::writeKeyword
virtual Ostream & writeKeyword(const keyType &kw)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:57
Foam::PatchFunction1Types::ConstantField::ConstantField
ConstantField(const polyPatch &pp, const word &entryName, const bool isUniform, const Type &uniformValue, const Field< Type > &nonUniformValue, const dictionary &dict=dictionary::null, const bool faceValues=true)
Construct from components.
Definition: ConstantField.C:34
Foam::PatchFunction1Types::ConstantField::autoMap
virtual void autoMap(const FieldMapper &mapper)
Map (and resize as needed) from self given a mapping object.
Definition: ConstantField.C:225
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::List< Type >
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:241
Foam::roots::type
type
Types of root.
Definition: Roots.H:54
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:375
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:306
Foam::uniformValue
Definition: uniformValue.H:50
Foam::patchIdentifier::name
const word & name() const
Return the patch name.
Definition: patchIdentifier.H:109