DimensionedField.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "DimensionedField.H"
27 #include "dimensionedType.H"
28 #include "Time.H"
29 
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34 
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 
37 // check mesh for two fields
38 #define checkField(df1, df2, op) \
39 if (&(df1).mesh() != &(df2).mesh()) \
40 { \
41  FatalErrorInFunction \
42  << "different mesh for fields " \
43  << (df1).name() << " and " << (df2).name() \
44  << " during operatrion " << op \
45  << abort(FatalError); \
46 }
47 
48 
49 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
50 
51 template<class Type, class GeoMesh>
53 (
54  const IOobject& io,
55  const Mesh& mesh,
56  const dimensionSet& dims,
57  const Field<Type>& field
58 )
59 :
60  regIOobject(io),
62  mesh_(mesh),
63  dimensions_(dims)
64 {
65  if (field.size() && field.size() != GeoMesh::size(mesh))
66  {
68  << "size of field = " << field.size()
69  << " is not the same as the size of mesh = "
70  << GeoMesh::size(mesh)
71  << abort(FatalError);
72  }
73 }
74 
75 
76 template<class Type, class GeoMesh>
78 (
79  const IOobject& io,
80  const Mesh& mesh,
81  const dimensionSet& dims,
82  const bool checkIOFlags
83 )
84 :
85  regIOobject(io),
86  Field<Type>(GeoMesh::size(mesh)),
87  mesh_(mesh),
88  dimensions_(dims)
89 {
90  if (checkIOFlags)
91  {
92  readIfPresent();
93  }
94 }
95 
96 
97 template<class Type, class GeoMesh>
99 (
100  const IOobject& io,
101  const Mesh& mesh,
102  const dimensioned<Type>& dt,
103  const bool checkIOFlags
104 )
105 :
106  regIOobject(io),
107  Field<Type>(GeoMesh::size(mesh), dt.value()),
108  mesh_(mesh),
109  dimensions_(dt.dimensions())
110 {
111  if (checkIOFlags)
112  {
113  readIfPresent();
114  }
115 }
116 
117 
118 template<class Type, class GeoMesh>
120 (
122 )
123 :
124  regIOobject(df),
125  Field<Type>(df),
126  mesh_(df.mesh_),
127  dimensions_(df.dimensions_)
128 {}
129 
130 
131 template<class Type, class GeoMesh>
133 (
135  bool reuse
136 )
137 :
138  regIOobject(df, reuse),
139  Field<Type>(df, reuse),
140  mesh_(df.mesh_),
141  dimensions_(df.dimensions_)
142 {}
143 
144 
145 template<class Type, class GeoMesh>
147 (
149 )
150 :
151  regIOobject(move(df), true),
152  Field<Type>(move(df)),
153  mesh_(df.mesh_),
154  dimensions_(move(df.dimensions_))
155 {}
156 
157 
158 template<class Type, class GeoMesh>
160 (
162 )
163 :
164  regIOobject(tdf(), tdf.isTmp()),
166  (
167  const_cast<DimensionedField<Type, GeoMesh>&>(tdf()),
168  tdf.isTmp()
169  ),
170  mesh_(tdf().mesh_),
171  dimensions_(tdf().dimensions_)
172 {
173  tdf.clear();
174 }
175 
176 
177 template<class Type, class GeoMesh>
179 (
180  const IOobject& io,
182 )
183 :
184  regIOobject(io),
185  Field<Type>(df),
186  mesh_(df.mesh_),
187  dimensions_(df.dimensions_)
188 {}
189 
190 
191 template<class Type, class GeoMesh>
193 (
194  const IOobject& io,
196  bool reuse
197 )
198 :
199  regIOobject(io, df),
200  Field<Type>(df, reuse),
201  mesh_(df.mesh_),
202  dimensions_(df.dimensions_)
203 {}
204 
205 
206 template<class Type, class GeoMesh>
208 (
209  const word& newName,
211 )
212 :
213  regIOobject(newName, df, newName != df.name()),
214  Field<Type>(df),
215  mesh_(df.mesh_),
216  dimensions_(df.dimensions_)
217 {}
218 
219 
220 template<class Type, class GeoMesh>
222 (
223  const word& newName,
225  bool reuse
226 )
227 :
228  regIOobject(newName, df, true),
229  Field<Type>(df, reuse),
230  mesh_(df.mesh_),
231  dimensions_(df.dimensions_)
232 {}
233 
234 
235 template<class Type, class GeoMesh>
237 (
238  const word& newName,
240 )
241 :
242  regIOobject(newName, tdf(), true),
244  (
245  const_cast<DimensionedField<Type, GeoMesh>&>(tdf()),
246  tdf.isTmp()
247  ),
248  mesh_(tdf().mesh_),
249  dimensions_(tdf().dimensions_)
250 {
251  tdf.clear();
252 }
253 
254 
255 template<class Type, class GeoMesh>
258 {
260  (
262  );
263 }
264 
265 
266 template<class Type, class GeoMesh>
269 (
270  const word& name,
271  const Mesh& mesh,
272  const dimensionSet& ds
273 )
274 {
275  const bool cacheTmp = mesh.thisDb().cacheTemporaryObject(name);
276 
278  (
280  (
281  IOobject
282  (
283  name,
284  mesh.thisDb().time().timeName(),
285  mesh.thisDb(),
288  cacheTmp
289  ),
290  mesh,
291  ds,
292  false
293  ),
294  cacheTmp
295  );
296 }
297 
298 
299 template<class Type, class GeoMesh>
302 (
303  const word& name,
304  const Mesh& mesh,
305  const dimensioned<Type>& dt
306 )
307 {
308  const bool cacheTmp = mesh.thisDb().cacheTemporaryObject(name);
309 
311  (
313  (
314  IOobject
315  (
316  name,
317  mesh.thisDb().time().timeName(),
318  mesh.thisDb(),
321  cacheTmp
322  ),
323  mesh,
324  dt,
325  false
326  ),
327  cacheTmp
328  );
329 }
330 
331 
332 template<class Type, class GeoMesh>
335 (
336  const word& newName,
338 )
339 {
340  const bool cacheTmp = df.db().cacheTemporaryObject(newName);
341 
343  (
345  (
346  IOobject
347  (
348  newName,
349  df.instance(),
350  df.local(),
351  df.db(),
354  cacheTmp
355  ),
356  df
357  ),
358  cacheTmp
359  );
360 }
361 
362 
363 template<class Type, class GeoMesh>
366 (
367  const word& newName,
369 )
370 {
371  const bool cacheTmp = tdf().db().cacheTemporaryObject(newName);
372 
374  (
376  (
377  IOobject
378  (
379  newName,
380  tdf().instance(),
381  tdf().local(),
382  tdf().db(),
385  cacheTmp
386  ),
387  tdf
388  ),
389  cacheTmp
390  );
391 }
392 
393 
394 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
395 
396 template<class Type, class GeoMesh>
398 {
399  db().cacheTemporaryObject(*this);
400 }
401 
402 
403 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
404 
405 template<class Type, class GeoMesh>
406 tmp
407 <
409  <typename DimensionedField<Type, GeoMesh>::cmptType, GeoMesh>
410 >
412 (
413  const direction d
414 ) const
415 {
417  (
419  (
420  name() + ".component(" + ::Foam::name(d) + ')',
421  mesh_,
422  dimensions_
423  )
424  );
425 
426  Foam::component(result.ref(), *this, d);
427 
428  return result;
429 }
430 
431 
432 template<class Type, class GeoMesh>
434 (
435  const direction d,
436  const DimensionedField
437  <typename DimensionedField<Type, GeoMesh>::cmptType, GeoMesh>& df
438 )
439 {
440  Field<Type>::replace(d, df);
441 }
442 
443 
444 template<class Type, class GeoMesh>
446 (
447  const direction d,
448  const tmp
449  <
451  <typename DimensionedField<Type, GeoMesh>::cmptType, GeoMesh>
452  >& tdf
453 )
454 {
455  replace(d, tdf());
456  tdf.clear();
457 }
458 
459 
460 template<class Type, class GeoMesh>
463 {
465  (
467  (
468  name() + ".T()",
469  mesh_,
470  dimensions_
471  )
472  );
473 
474  Foam::T(result.ref(), *this);
475 
476  return result;
477 }
478 
479 
480 template<class Type, class GeoMesh>
482 {
483  dimensioned<Type> Average
484  (
485  this->name() + ".average()",
486  this->dimensions(),
487  gAverage(field())
488  );
489 
490  return Average;
491 }
492 
493 
494 template<class Type, class GeoMesh>
496 (
497  const DimensionedField<scalar, GeoMesh>& weightField
498 ) const
499 {
500  return
501  (
503  (
504  this->name() + ".weightedAverage(weights)",
505  this->dimensions(),
506  gSum(weightField*field())/gSum(weightField)
507  )
508  );
509 }
510 
511 
512 template<class Type, class GeoMesh>
514 (
515  const tmp<DimensionedField<scalar, GeoMesh>>& tweightField
516 ) const
517 {
518  dimensioned<Type> wa = weightedAverage(tweightField());
519  tweightField.clear();
520  return wa;
521 }
522 
523 
524 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
525 
526 template<class Type, class GeoMesh>
527 void DimensionedField<Type, GeoMesh>::operator=
528 (
530 )
531 {
532  // Check for assignment to self
533  if (this == &df)
534  {
536  << "attempted assignment to self"
537  << abort(FatalError);
538  }
539 
540  checkField(*this, df, "=");
541 
542  dimensions_ = df.dimensions();
544 }
545 
546 
547 template<class Type, class GeoMesh>
548 void DimensionedField<Type, GeoMesh>::operator=
549 (
551 )
552 {
553  // Check for assignment to self
554  if (this == &df)
555  {
557  << "attempted assignment to self"
558  << abort(FatalError);
559  }
560 
561  checkField(*this, df, "=");
562 
563  dimensions_ = move(df.dimensions());
564  Field<Type>::operator=(move(df));
565 }
566 
567 
568 template<class Type, class GeoMesh>
569 void DimensionedField<Type, GeoMesh>::operator=
570 (
572 )
573 {
574  const DimensionedField<Type, GeoMesh>& df = tdf();
575 
576  // Check for assignment to self
577  if (this == &df)
578  {
580  << "attempted assignment to self"
581  << abort(FatalError);
582  }
583 
584  checkField(*this, df, "=");
585 
586  dimensions_ = df.dimensions();
587  this->transfer(const_cast<DimensionedField<Type, GeoMesh>&>(df));
588  tdf.clear();
589 }
590 
591 
592 template<class Type, class GeoMesh>
593 void DimensionedField<Type, GeoMesh>::operator=
594 (
595  const dimensioned<Type>& dt
596 )
597 {
598  dimensions_ = dt.dimensions();
600 }
601 
602 
603 template<class Type, class GeoMesh>
605 {
607 }
608 
609 
610 #define COMPUTED_ASSIGNMENT(TYPE, op) \
611  \
612 template<class Type, class GeoMesh> \
613 void DimensionedField<Type, GeoMesh>::operator op \
614 ( \
615  const DimensionedField<TYPE, GeoMesh>& df \
616 ) \
617 { \
618  checkField(*this, df, #op); \
619  \
620  dimensions_ op df.dimensions(); \
621  Field<Type>::operator op(df); \
622 } \
623  \
624 template<class Type, class GeoMesh> \
625 void DimensionedField<Type, GeoMesh>::operator op \
626 ( \
627  const tmp<DimensionedField<TYPE, GeoMesh>>& tdf \
628 ) \
629 { \
630  operator op(tdf()); \
631  tdf.clear(); \
632 } \
633  \
634 template<class Type, class GeoMesh> \
635 void DimensionedField<Type, GeoMesh>::operator op \
636 ( \
637  const dimensioned<TYPE>& dt \
638 ) \
639 { \
640  dimensions_ op dt.dimensions(); \
641  Field<Type>::operator op(dt.value()); \
642 }
643 
648 
649 #undef COMPUTED_ASSIGNMENT
650 
651 
652 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
653 
654 #undef checkField
655 
656 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
657 
658 } // End namespace Foam
659 
660 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
661 
662 #include "DimensionedFieldIO.C"
664 
665 // ************************************************************************* //
bool cacheTemporaryObject(const word &name) const
Return true if given name is in the cacheTemporaryObjects set.
const word & name() const
Return name.
Definition: IOobject.H:303
#define checkField(df1, df2, op)
static tmp< DimensionedField< Type, GeoMesh > > New(const word &name, const Mesh &mesh, const dimensionSet &)
Return a temporary field constructed from name, mesh.
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:181
tmp< DimensionedField< Type, GeoMesh > > clone() const
Clone.
uint8_t direction
Definition: direction.H:45
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
tmp< DimensionedField< Type, GeoMesh > > T() const
Return the field transpose (only defined for second rank tensors)
dimensioned< Type > weightedAverage(const DimensionedField< scalar, GeoMesh > &) const
Calculate and return weighted average.
Generic dimensioned Type class.
void replace(const direction, const UList< cmptType > &)
Replace a component field of the field.
Definition: Field.C:453
const dimensionSet & dimensions() const
Return dimensions.
Dimension set for the base types.
Definition: dimensionSet.H:120
void operator=(const DimensionedField< Type, GeoMesh > &)
Type gSum(const FieldField< Field, Type > &f)
Pre-declare SubField and related Field type.
Definition: Field.H:56
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:125
A class for handling words, derived from string.
Definition: word.H:59
const fileName & local() const
Definition: IOobject.H:400
const Type & value() const
Return const reference to value.
static const zero Zero
Definition: zero.H:97
virtual ~DimensionedField()
Destructor.
Foam::pointMesh ::Mesh Mesh
Type of mesh on which this DimensionedField is instantiated.
errorManip< error > abort(error &err)
Definition: errorManip.H:131
#define COMPUTED_ASSIGNMENT(TYPE, op)
void operator=(const Field< Type > &)
Definition: Field.C:512
DimensionedField(const IOobject &, const Mesh &mesh, const dimensionSet &, const Field< Type > &)
Construct from components.
void replace(const direction, const DimensionedField< cmptType, GeoMesh > &)
Replace a component field of the field.
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
dimensioned< Type > average() const
Calculate and return arithmetic average.
const fileName & instance() const
Definition: IOobject.H:390
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...
const dimensionSet & dimensions() const
Return const reference to dimensions.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:52
A class representing the concept of 0 used to avoid unnecessary manipulations for objects that are kn...
Definition: zero.H:49
tmp< DimensionedField< cmptType, GeoMesh > > component(const direction) const
Return a component field of the field.
Generic mesh wrapper used by volMesh, surfaceMesh, pointMesh etc.
Definition: GeoMesh.H:46
rDeltaTY field()
A class for managing temporary objects.
Definition: PtrList.H:53
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:312
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Namespace for OpenFOAM.