DimensionedField.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2015-2025 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
30 #include "dimensionedType.H"
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 // Check that both fields use the same mesh
35 #undef checkField
36 #define checkField(fld1, fld2, op) \
37 if (&(fld1).mesh() != &(fld2).mesh()) \
38 { \
39  FatalErrorInFunction \
40  << "Different mesh for fields " \
41  << (fld1).name() << " and " << (fld2).name() \
42  << " during operation " << op \
43  << abort(FatalError); \
44 }
45 
46 
47 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
48 
49 template<class Type, class GeoMesh>
51 {
52  const label fieldSize = this->size();
53  if (fieldSize)
54  {
55  const label meshSize = GeoMesh::size(this->mesh_);
56  if (fieldSize != meshSize)
57  {
59  << "size of field = " << fieldSize
60  << " is not the same as the size of mesh = "
61  << meshSize
62  << abort(FatalError);
63  }
64  }
65 }
66 
67 
68 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
69 
70 template<class Type, class GeoMesh>
72 (
73  const IOobject& io,
74  const Mesh& mesh,
75  const dimensionSet& dims,
76  const Field<Type>& field
77 )
78 :
79  regIOobject(io),
80  Field<Type>(field),
81  mesh_(mesh),
82  dimensions_(dims)
83 {
84  checkFieldSize();
85 }
86 
87 
88 template<class Type, class GeoMesh>
90 (
91  const IOobject& io,
92  const Mesh& mesh,
93  const dimensionSet& dims,
95 )
96 :
97  regIOobject(io),
98  Field<Type>(std::move(field)),
99  mesh_(mesh),
100  dimensions_(dims)
101 {
102  checkFieldSize();
103 }
104 
105 
106 template<class Type, class GeoMesh>
108 (
109  const IOobject& io,
110  const Mesh& mesh,
111  const dimensionSet& dims,
112  List<Type>&& field
113 )
114 :
115  regIOobject(io),
116  Field<Type>(std::move(field)),
117  mesh_(mesh),
118  dimensions_(dims)
119 {
120  checkFieldSize();
121 }
122 
123 
124 template<class Type, class GeoMesh>
126 (
127  const IOobject& io,
128  const Mesh& mesh,
129  const dimensionSet& dims,
130  const tmp<Field<Type>>& tfield
131 )
132 :
133  regIOobject(io),
134  Field<Type>(tfield.constCast(), tfield.movable()),
135  mesh_(mesh),
136  dimensions_(dims)
137 {
138  tfield.clear();
139  checkFieldSize();
140 }
141 
142 
143 template<class Type, class GeoMesh>
145 (
146  const IOobject& io,
147  const Mesh& mesh,
148  const dimensionSet& dims,
149  const bool checkIOFlags
150 )
151 :
152  regIOobject(io),
153  Field<Type>(GeoMesh::size(mesh)),
154  mesh_(mesh),
155  dimensions_(dims)
156 {
157  if (checkIOFlags)
158  {
160  }
161 }
162 
163 
164 template<class Type, class GeoMesh>
166 (
167  const IOobject& io,
168  const Mesh& mesh,
169  const Type& value,
170  const dimensionSet& dims,
171  const bool checkIOFlags
172 )
173 :
174  regIOobject(io),
175  Field<Type>(GeoMesh::size(mesh)),
176  mesh_(mesh),
177  dimensions_(dims)
178 {
179  if (!checkIOFlags || !readIfPresent())
180  {
181  // Set default value (if not read)
182  this->field() = value;
183  }
184 }
185 
186 
187 template<class Type, class GeoMesh>
189 (
190  const IOobject& io,
191  const Mesh& mesh,
192  const dimensioned<Type>& dt,
193  const bool checkIOFlags
194 )
195 :
196  DimensionedField<Type, GeoMesh>
197  (
198  io,
199  mesh,
200  dt.value(),
201  dt.dimensions(),
202  checkIOFlags
203  )
204 {}
205 
206 
207 template<class Type, class GeoMesh>
209 (
211 )
212 :
213  regIOobject(df),
214  Field<Type>(df),
215  mesh_(df.mesh_),
216  dimensions_(df.dimensions_),
217  oriented_(df.oriented_)
218 {}
219 
220 
221 template<class Type, class GeoMesh>
223 (
225 )
226 :
227  DimensionedField<Type, GeoMesh>(df, true)
228 {}
229 
230 
231 template<class Type, class GeoMesh>
233 (
235  bool reuse
236 )
237 :
238  regIOobject(df, reuse),
239  Field<Type>(df, reuse),
240  mesh_(df.mesh_),
241  dimensions_(df.dimensions_),
242  oriented_(df.oriented_)
243 {}
244 
245 
246 template<class Type, class GeoMesh>
248 (
250 )
251 :
252  DimensionedField<Type, GeoMesh>(tdf.constCast(), tdf.movable())
253 {
254  tdf.clear();
255 }
256 
257 
258 template<class Type, class GeoMesh>
260 (
261  const IOobject& io,
263 )
264 :
265  regIOobject(io),
266  Field<Type>(df),
267  mesh_(df.mesh_),
268  dimensions_(df.dimensions_),
269  oriented_(df.oriented_)
270 {}
271 
272 
273 template<class Type, class GeoMesh>
275 (
276  const IOobject& io,
278 )
279 :
280  DimensionedField<Type, GeoMesh>(io, df, true)
281 {}
282 
283 
284 template<class Type, class GeoMesh>
286 (
287  const IOobject& io,
289  bool reuse
290 )
291 :
292  regIOobject(io, df),
293  Field<Type>(df, reuse),
294  mesh_(df.mesh_),
295  dimensions_(df.dimensions_),
296  oriented_(df.oriented_)
297 {}
298 
299 
300 template<class Type, class GeoMesh>
302 (
303  const IOobject& io,
305 )
306 :
307  DimensionedField<Type, GeoMesh>(io, tdf.constCast(), tdf.movable())
308 {
309  tdf.clear();
310 }
311 
312 
313 template<class Type, class GeoMesh>
315 (
316  const word& newName,
318 )
319 :
320  regIOobject(newName, df, newName != df.name()),
321  Field<Type>(df),
322  mesh_(df.mesh_),
323  dimensions_(df.dimensions_),
324  oriented_(df.oriented_)
325 {}
326 
327 
328 template<class Type, class GeoMesh>
330 (
331  const word& newName,
333 )
334 :
335  DimensionedField<Type, GeoMesh>(newName, df, true)
336 {}
337 
338 
339 template<class Type, class GeoMesh>
341 (
342  const word& newName,
344  bool reuse
345 )
346 :
347  regIOobject(newName, df, true),
348  Field<Type>(df, reuse),
349  mesh_(df.mesh_),
350  dimensions_(df.dimensions_),
351  oriented_(df.oriented_)
352 {}
353 
354 
355 template<class Type, class GeoMesh>
357 (
358  const word& newName,
360 )
361 :
362  DimensionedField<Type, GeoMesh>(newName, tdf.constCast(), tdf.movable())
363 {
364  tdf.clear();
365 }
366 
367 
368 template<class Type, class GeoMesh>
371 {
373 }
374 
375 
376 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * * //
377 
378 template<class Type, class GeoMesh>
380 {
381  // FUTURE: register cache field info
382  // // this->db().cacheTemporaryObject(*this);
383 }
384 
385 
386 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
387 
388 template<class Type, class GeoMesh>
389 Foam::tmp
390 <
392  <
394  >
395 >
397 (
398  const direction d
399 ) const
400 {
402  (
403  name() + ".component(" + ::Foam::name(d) + ')',
404  mesh_,
405  dimensions_
406  );
407 
408  Foam::component(tresult.ref(), *this, d);
409 
410  return tresult;
411 }
412 
413 
414 template<class Type, class GeoMesh>
416 (
417  const direction d,
418  const DimensionedField
419  <
420  typename DimensionedField<Type, GeoMesh>::cmptType, GeoMesh
421  >& df
422 )
423 {
424  Field<Type>::replace(d, df);
425 }
426 
427 
428 template<class Type, class GeoMesh>
430 (
431  const direction d,
432  const tmp
433  <
434  DimensionedField
435  <
436  typename DimensionedField<Type, GeoMesh>::cmptType, GeoMesh
437  >
438  >& tdf
439 )
440 {
441  replace(d, tdf());
442  tdf.clear();
443 }
444 
445 
446 template<class Type, class GeoMesh>
449 {
451  (
452  name() + ".T()",
453  mesh_,
454  dimensions_
455  );
456 
457  Foam::T(tresult.ref(), *this);
459  return tresult;
460 }
461 
462 
463 template<class Type, class GeoMesh>
465 (
466  const label comm
467 ) const
468 {
469  return
471  (
472  this->name() + ".average()",
473  this->dimensions(),
474  gAverage(this->field(), comm)
475  );
476 }
477 
478 
479 template<class Type, class GeoMesh>
481 (
482  const DimensionedField<scalar, GeoMesh>& weights,
483  const label comm
484 ) const
485 {
486  return
488  (
489  this->name() + ".weightedAverage(weights)",
490  this->dimensions(),
491  gWeightedAverage(weights.field(), this->field(), comm)
492  );
493 }
494 
495 
496 template<class Type, class GeoMesh>
498 (
499  const tmp<DimensionedField<scalar, GeoMesh>>& tweights,
500  const label comm
501 ) const
502 {
503  dimensioned<Type> result = this->weightedAverage(tweights(), comm);
504  tweights.clear();
505  return result;
506 }
507 
508 
509 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
510 
511 template<class Type, class GeoMesh>
513 (
515 )
516 {
517  if (this == &df)
518  {
519  return; // Self-assignment is a no-op
520  }
521 
522  checkField(*this, df, "=");
523 
524  dimensions_ = df.dimensions();
525  oriented_ = df.oriented();
527 }
528 
529 
530 template<class Type, class GeoMesh>
532 (
534 )
535 {
536  auto& df = tdf.constCast();
537 
538  if (this == &df)
539  {
540  return; // Self-assignment is a no-op
541  }
542 
543  checkField(*this, df, "=");
544 
545  dimensions_ = df.dimensions();
546  oriented_ = df.oriented();
547  this->transfer(df);
548  tdf.clear();
549 }
550 
551 
552 template<class Type, class GeoMesh>
554 (
555  const dimensioned<Type>& dt
556 )
557 {
558  dimensions_ = dt.dimensions();
559  Field<Type>::operator=(dt.value());
560 }
561 
562 
563 #define COMPUTED_ASSIGNMENT(TYPE, op) \
564  \
565 template<class Type, class GeoMesh> \
566 void Foam::DimensionedField<Type, GeoMesh>::operator op \
567 ( \
568  const DimensionedField<TYPE, GeoMesh>& df \
569 ) \
570 { \
571  checkField(*this, df, #op); \
572  \
573  dimensions_ op df.dimensions(); \
574  oriented_ op df.oriented(); \
575  Field<Type>::operator op(df); \
576 } \
577  \
578 template<class Type, class GeoMesh> \
579 void Foam::DimensionedField<Type, GeoMesh>::operator op \
580 ( \
581  const tmp<DimensionedField<TYPE, GeoMesh>>& tdf \
582 ) \
583 { \
584  operator op(tdf()); \
585  tdf.clear(); \
586 } \
587  \
588 template<class Type, class GeoMesh> \
589 void Foam::DimensionedField<Type, GeoMesh>::operator op \
590 ( \
591  const dimensioned<TYPE>& dt \
592 ) \
593 { \
594  dimensions_ op dt.dimensions(); \
595  Field<Type>::operator op(dt.value()); \
596 }
597 
598 COMPUTED_ASSIGNMENT(Type, +=)
599 COMPUTED_ASSIGNMENT(Type, -=)
600 COMPUTED_ASSIGNMENT(scalar, *=)
601 COMPUTED_ASSIGNMENT(scalar, /=)
602 
603 #undef COMPUTED_ASSIGNMENT
604 
605 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
606 
607 #undef checkField
608 
609 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
610 
611 #include "DimensionedFieldIO.C"
612 #include "DimensionedFieldNew.C"
614 
615 // ************************************************************************* //
tmp< DimensionedField< Type, GeoMesh > > clone() const
Clone.
const Type & value() const noexcept
Return const reference to value.
Type gWeightedAverage(const UList< scalar > &weights, const UList< Type > &fld, const label comm)
The global weighted average of a field, using the mag() of the weights.
rDeltaTY field()
uint8_t direction
Definition: direction.H:46
dimensioned< Type > average(const label comm=UPstream::worldComm) const
Return the global arithmetic average.
void replace(const direction d, const DimensionedField< cmptType, GeoMesh > &df)
Replace a component field of the field.
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
dimensioned< Type > weightedAverage(const DimensionedField< scalar, GeoMesh > &weights, const label comm=UPstream::worldComm) const
Return the global weighted average.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:600
DimensionedField(const IOobject &io, const Mesh &mesh, const dimensionSet &dims, const Field< Type > &field)
Construct from components, copy initial field content.
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
Generic dimensioned Type class.
Field< Type >::cmptType cmptType
Component type of the field elements.
tmp< DimensionedField< cmptType, GeoMesh > > component(const direction d) const
Return a component field of the field.
Dimension set for the base types, which can be used to implement rigorous dimension checking for alge...
Definition: dimensionSet.H:105
dynamicFvMesh & mesh
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
Generic templated field type.
Definition: Field.H:63
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:130
A class for handling words, derived from Foam::string.
Definition: word.H:63
Type weightedAverage(const UList< scalar > &weights, const UList< Type > &fld)
The local weighted average of a field, using the mag() of the weights.
tmp< DimensionedField< Type, GeoMesh > > T() const
Return the field transpose (only defined for second rank tensors)
const dimensionSet & dimensions() const noexcept
Return const reference to dimensions.
virtual ~DimensionedField()
Destructor.
errorManip< error > abort(error &err)
Definition: errorManip.H:139
#define COMPUTED_ASSIGNMENT(TYPE, op)
Type gAverage(const FieldField< Field, Type > &f, const label comm)
The global arithmetic average of a FieldField.
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
#define checkField(fld1, fld2, op)
const Field< Type > & field() const noexcept
Return const-reference to the primitive field values.
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
meshDefDict readIfPresent("polyMeshPatches", polyPatchNames)
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:68
Generic mesh wrapper used by volMesh, surfaceMesh, pointMesh etc.
Definition: GeoMesh.H:45
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:180
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)