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-2020 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 
29 #include "DimensionedField.H"
30 #include "dimensionedType.H"
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 // Check mesh for two fields
35 #define checkField(df1, df2, op) \
36 if (&(df1).mesh() != &(df2).mesh()) \
37 { \
38  FatalErrorInFunction \
39  << "different mesh for fields " \
40  << (df1).name() << " and " << (df2).name() \
41  << " during operation " << op \
42  << abort(FatalError); \
43 }
44 
45 
46 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
47 
48 template<class Type, class GeoMesh>
50 {
51  const label fieldSize = this->size();
52  if (fieldSize)
53  {
54  const label meshSize = GeoMesh::size(this->mesh_);
55  if (fieldSize != meshSize)
56  {
58  << "size of field = " << fieldSize
59  << " is not the same as the size of mesh = "
60  << meshSize
61  << abort(FatalError);
62  }
63  }
64 }
65 
66 
67 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
68 
69 template<class Type, class GeoMesh>
71 (
72  const IOobject& io,
73  const Mesh& mesh,
74  const dimensionSet& dims,
75  const Field<Type>& field
76 )
77 :
78  regIOobject(io),
79  Field<Type>(field),
80  mesh_(mesh),
81  dimensions_(dims),
82  oriented_()
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  oriented_()
138 {
139  tfield.clear();
140  checkFieldSize();
141 }
142 
143 
144 template<class Type, class GeoMesh>
146 (
147  const IOobject& io,
148  const Mesh& mesh,
149  const dimensionSet& dims,
150  const bool checkIOFlags
151 )
152 :
153  regIOobject(io),
154  Field<Type>(GeoMesh::size(mesh)),
155  mesh_(mesh),
156  dimensions_(dims),
157  oriented_()
158 {
159  if (checkIOFlags)
160  {
162  }
163 }
164 
165 
166 template<class Type, class GeoMesh>
168 (
169  const IOobject& io,
170  const Mesh& mesh,
171  const dimensioned<Type>& dt,
172  const bool checkIOFlags
173 )
174 :
175  regIOobject(io),
176  Field<Type>(GeoMesh::size(mesh), dt.value()),
177  mesh_(mesh),
178  dimensions_(dt.dimensions()),
179  oriented_()
180 {
181  if (checkIOFlags)
182  {
184  }
185 }
186 
187 
188 template<class Type, class GeoMesh>
190 (
191  const DimensionedField<Type, GeoMesh>& df
192 )
193 :
194  regIOobject(df),
195  Field<Type>(df),
196  mesh_(df.mesh_),
197  dimensions_(df.dimensions_),
198  oriented_(df.oriented_)
199 {}
200 
201 
202 template<class Type, class GeoMesh>
204 (
206 )
207 :
208  DimensionedField<Type, GeoMesh>(df, true)
209 {}
210 
211 
212 template<class Type, class GeoMesh>
214 (
216  bool reuse
217 )
218 :
219  regIOobject(df, reuse),
220  Field<Type>(df, reuse),
221  mesh_(df.mesh_),
222  dimensions_(df.dimensions_),
223  oriented_(df.oriented_)
224 {}
225 
226 
227 template<class Type, class GeoMesh>
229 (
231 )
232 :
233  DimensionedField<Type, GeoMesh>(tdf.constCast(), tdf.movable())
234 {
235  tdf.clear();
236 }
237 
238 
239 template<class Type, class GeoMesh>
241 (
242  const IOobject& io,
244 )
245 :
246  regIOobject(io),
247  Field<Type>(df),
248  mesh_(df.mesh_),
249  dimensions_(df.dimensions_),
250  oriented_(df.oriented_)
251 {}
252 
253 
254 template<class Type, class GeoMesh>
256 (
257  const IOobject& io,
259 )
260 :
261  DimensionedField<Type, GeoMesh>(io, df, true)
262 {}
263 
264 
265 template<class Type, class GeoMesh>
267 (
268  const IOobject& io,
270  bool reuse
271 )
272 :
273  regIOobject(io, df),
274  Field<Type>(df, reuse),
275  mesh_(df.mesh_),
276  dimensions_(df.dimensions_),
277  oriented_(df.oriented_)
278 {}
279 
280 
281 template<class Type, class GeoMesh>
283 (
284  const IOobject& io,
286 )
287 :
288  DimensionedField<Type, GeoMesh>(io, tdf.constCast(), tdf.movable())
289 {
290  tdf.clear();
291 }
292 
293 
294 template<class Type, class GeoMesh>
296 (
297  const word& newName,
299 )
300 :
301  regIOobject(newName, df, newName != df.name()),
302  Field<Type>(df),
303  mesh_(df.mesh_),
304  dimensions_(df.dimensions_),
305  oriented_(df.oriented_)
306 {}
307 
308 
309 template<class Type, class GeoMesh>
311 (
312  const word& newName,
314 )
315 :
316  DimensionedField<Type, GeoMesh>(newName, df, true)
317 {}
318 
319 
320 template<class Type, class GeoMesh>
322 (
323  const word& newName,
325  bool reuse
326 )
327 :
328  regIOobject(newName, df, true),
329  Field<Type>(df, reuse),
330  mesh_(df.mesh_),
331  dimensions_(df.dimensions_),
332  oriented_(df.oriented_)
333 {}
334 
335 
336 template<class Type, class GeoMesh>
338 (
339  const word& newName,
341 )
342 :
343  DimensionedField<Type, GeoMesh>(newName, tdf.constCast(), tdf.movable())
344 {
345  tdf.clear();
346 }
347 
348 
349 template<class Type, class GeoMesh>
352 {
354 }
355 
356 
357 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
358 
359 template<class Type, class GeoMesh>
360 Foam::tmp
361 <
363  <
365  >
366 >
368 (
369  const direction d
370 ) const
371 {
372  auto tresult = tmp<DimensionedField<cmptType, GeoMesh>>::New
373  (
374  IOobject
375  (
376  name() + ".component(" + ::Foam::name(d) + ')',
377  instance(),
378  db()
379  ),
380  mesh_,
381  dimensions_
382  );
383 
384  Foam::component(tresult.ref(), *this, d);
385 
386  return tresult;
387 }
388 
389 
390 template<class Type, class GeoMesh>
392 (
393  const direction d,
394  const DimensionedField
395  <
396  typename DimensionedField<Type, GeoMesh>::cmptType, GeoMesh
397  >& df
398 )
399 {
400  Field<Type>::replace(d, df);
401 }
402 
403 
404 template<class Type, class GeoMesh>
406 (
407  const direction d,
408  const tmp
409  <
410  DimensionedField
411  <
412  typename DimensionedField<Type, GeoMesh>::cmptType, GeoMesh
413  >
414  >& tdf
415 )
416 {
417  replace(d, tdf());
418  tdf.clear();
419 }
420 
421 
422 template<class Type, class GeoMesh>
425 {
427  (
428  IOobject
429  (
430  name() + ".T()",
431  instance(),
432  db()
433  ),
434  mesh_,
435  dimensions_
436  );
437 
438  Foam::T(tresult.ref(), *this);
439 
440  return tresult;
441 }
442 
443 
444 template<class Type, class GeoMesh>
446 {
447  return
449  (
450  this->name() + ".average()",
451  this->dimensions(),
453  );
454 }
455 
456 
457 template<class Type, class GeoMesh>
459 (
460  const DimensionedField<scalar, GeoMesh>& weightField
461 ) const
462 {
463  return
465  (
466  this->name() + ".weightedAverage(weights)",
467  this->dimensions(),
468  gSum(weightField*field())/gSum(weightField)
469  );
470 }
471 
472 
473 template<class Type, class GeoMesh>
475 (
476  const tmp<DimensionedField<scalar, GeoMesh>>& tweightField
477 ) const
478 {
479  dimensioned<Type> result = weightedAverage(tweightField());
480  tweightField.clear();
481  return result;
482 }
483 
484 
485 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
486 
487 template<class Type, class GeoMesh>
489 (
491 )
492 {
493  if (this == &df)
494  {
495  return; // Self-assignment is a no-op
496  }
497 
498  checkField(*this, df, "=");
499 
500  dimensions_ = df.dimensions();
501  oriented_ = df.oriented();
503 }
504 
505 
506 template<class Type, class GeoMesh>
508 (
510 )
511 {
512  auto& df = tdf.constCast();
513 
514  if (this == &df)
515  {
516  return; // Self-assignment is a no-op
517  }
518 
519  checkField(*this, df, "=");
520 
521  dimensions_ = df.dimensions();
522  oriented_ = df.oriented();
523  this->transfer(df);
524  tdf.clear();
525 }
526 
527 
528 template<class Type, class GeoMesh>
530 (
531  const dimensioned<Type>& dt
532 )
533 {
534  dimensions_ = dt.dimensions();
535  Field<Type>::operator=(dt.value());
536 }
537 
538 
539 #define COMPUTED_ASSIGNMENT(TYPE, op) \
540  \
541 template<class Type, class GeoMesh> \
542 void Foam::DimensionedField<Type, GeoMesh>::operator op \
543 ( \
544  const DimensionedField<TYPE, GeoMesh>& df \
545 ) \
546 { \
547  checkField(*this, df, #op); \
548  \
549  dimensions_ op df.dimensions(); \
550  oriented_ op df.oriented(); \
551  Field<Type>::operator op(df); \
552 } \
553  \
554 template<class Type, class GeoMesh> \
555 void Foam::DimensionedField<Type, GeoMesh>::operator op \
556 ( \
557  const tmp<DimensionedField<TYPE, GeoMesh>>& tdf \
558 ) \
559 { \
560  operator op(tdf()); \
561  tdf.clear(); \
562 } \
563  \
564 template<class Type, class GeoMesh> \
565 void Foam::DimensionedField<Type, GeoMesh>::operator op \
566 ( \
567  const dimensioned<TYPE>& dt \
568 ) \
569 { \
570  dimensions_ op dt.dimensions(); \
571  Field<Type>::operator op(dt.value()); \
572 }
573 
574 COMPUTED_ASSIGNMENT(Type, +=)
575 COMPUTED_ASSIGNMENT(Type, -=)
576 COMPUTED_ASSIGNMENT(scalar, *=)
577 COMPUTED_ASSIGNMENT(scalar, /=)
578 
579 #undef COMPUTED_ASSIGNMENT
580 
581 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
582 
583 #undef checkField
584 
585 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
586 
587 #include "DimensionedFieldIO.C"
588 #include "DimensionedFieldNew.C"
590 
591 // ************************************************************************* //
tmp< DimensionedField< Type, GeoMesh > > clone() const
Clone.
dimensioned< Type > average() const
Calculate and return arithmetic average.
rDeltaTY field()
uint8_t direction
Definition: direction.H:46
#define checkField(df1, df2, op)
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...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:578
DimensionedField(const IOobject &io, const Mesh &mesh, const dimensionSet &dims, const Field< Type > &field)
Construct from components, copy initial field content.
Generic dimensioned Type class.
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
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
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, false)
dynamicFvMesh & mesh
Type gSum(const FieldField< Field, Type > &f)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
Generic templated field type.
Definition: Field.H:61
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:109
A class for handling words, derived from Foam::string.
Definition: word.H:63
tmp< DimensionedField< Type, GeoMesh > > T() const
Return the field transpose (only defined for second rank tensors)
propsDict readIfPresent("fields", acceptFields)
errorManip< error > abort(error &err)
Definition: errorManip.H:139
#define COMPUTED_ASSIGNMENT(TYPE, op)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Type gAverage(const FieldField< Field, Type > &f)
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
Definition: areaFieldsFwd.H:42
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:69
dimensioned< Type > weightedAverage(const DimensionedField< scalar, GeoMesh > &weightField) const
Calculate and return weighted average.
Generic mesh wrapper used by volMesh, surfaceMesh, pointMesh etc.
Definition: GeoMesh.H:42
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:166
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
const dimensionSet & dimensions() const noexcept
Return dimensions.