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-2022 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),
61  Field<Type>(field),
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 && df.registered()),
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)),
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()),
165  Field<Type>
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),
243  Field<Type>
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  const Field<Type>& field
274 )
275 {
276  const bool cacheTmp = mesh.thisDb().cacheTemporaryObject(name);
277 
279  (
281  (
282  IOobject
283  (
284  name,
285  mesh.thisDb().time().name(),
286  mesh.thisDb(),
287  IOobject::NO_READ,
288  IOobject::NO_WRITE,
289  cacheTmp
290  ),
291  mesh,
292  ds,
293  field
294  ),
295  cacheTmp
296  );
297 }
298 
299 
300 template<class Type, class GeoMesh>
303 (
304  const word& name,
305  const Mesh& mesh,
306  const dimensionSet& ds
307 )
308 {
309  const bool cacheTmp = mesh.thisDb().cacheTemporaryObject(name);
310 
312  (
314  (
315  IOobject
316  (
317  name,
318  mesh.thisDb().time().name(),
319  mesh.thisDb(),
320  IOobject::NO_READ,
321  IOobject::NO_WRITE,
322  cacheTmp
323  ),
324  mesh,
325  ds,
326  false
327  ),
328  cacheTmp
329  );
330 }
331 
332 
333 template<class Type, class GeoMesh>
336 (
337  const word& name,
338  const Mesh& mesh,
339  const dimensioned<Type>& dt
340 )
341 {
342  const bool cacheTmp = mesh.thisDb().cacheTemporaryObject(name);
343 
345  (
347  (
348  IOobject
349  (
350  name,
351  mesh.thisDb().time().name(),
352  mesh.thisDb(),
353  IOobject::NO_READ,
354  IOobject::NO_WRITE,
355  cacheTmp
356  ),
357  mesh,
358  dt,
359  false
360  ),
361  cacheTmp
362  );
363 }
364 
365 
366 template<class Type, class GeoMesh>
369 (
370  const word& newName,
372 )
373 {
374  const bool cacheTmp = df.db().cacheTemporaryObject(newName);
375 
377  (
379  (
380  IOobject
381  (
382  newName,
383  df.instance(),
384  df.local(),
385  df.db(),
386  IOobject::NO_READ,
387  IOobject::NO_WRITE,
388  cacheTmp
389  ),
390  df
391  ),
392  cacheTmp
393  );
394 }
395 
396 
397 template<class Type, class GeoMesh>
400 (
401  const word& newName,
403 )
404 {
405  const bool cacheTmp = tdf().db().cacheTemporaryObject(newName);
406 
408  (
410  (
411  IOobject
412  (
413  newName,
414  tdf().instance(),
415  tdf().local(),
416  tdf().db(),
417  IOobject::NO_READ,
418  IOobject::NO_WRITE,
419  cacheTmp
420  ),
421  tdf
422  ),
423  cacheTmp
424  );
425 }
426 
427 
428 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
429 
430 template<class Type, class GeoMesh>
432 {
433  db().cacheTemporaryObject(*this);
434 }
435 
436 
437 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
438 
439 template<class Type, class GeoMesh>
440 tmp
441 <
444 >
446 (
447  const direction d
448 ) const
449 {
451  (
453  (
454  name() + ".component(" + ::Foam::name(d) + ')',
455  mesh_,
456  dimensions_
457  )
458  );
459 
460  Foam::component(result.ref(), *this, d);
461 
462  return result;
463 }
464 
465 
466 template<class Type, class GeoMesh>
467 void DimensionedField<Type, GeoMesh>::replace
468 (
469  const direction d,
470  const DimensionedField
471  <typename DimensionedField<Type, GeoMesh>::cmptType, GeoMesh>& df
472 )
473 {
474  Field<Type>::replace(d, df);
475 }
476 
477 
478 template<class Type, class GeoMesh>
479 void DimensionedField<Type, GeoMesh>::replace
480 (
481  const direction d,
482  const tmp
483  <
484  DimensionedField
485  <typename DimensionedField<Type, GeoMesh>::cmptType, GeoMesh>
486  >& tdf
487 )
488 {
489  replace(d, tdf());
490  tdf.clear();
491 }
492 
493 
494 template<class Type, class GeoMesh>
495 tmp<DimensionedField<Type, GeoMesh>>
497 {
499  (
501  (
502  name() + ".T()",
503  mesh_,
504  dimensions_
505  )
506  );
507 
508  Foam::T(result.ref(), *this);
509 
510  return result;
511 }
512 
513 
514 template<class Type, class GeoMesh>
516 {
517  dimensioned<Type> Average
518  (
519  this->name() + ".average()",
520  this->dimensions(),
521  gAverage(field())
522  );
523 
524  return Average;
525 }
526 
527 
528 template<class Type, class GeoMesh>
530 (
531  const DimensionedField<scalar, GeoMesh>& weightField
532 ) const
533 {
534  return
535  (
537  (
538  this->name() + ".weightedAverage(weights)",
539  this->dimensions(),
540  gSum(weightField*field())/gSum(weightField)
541  )
542  );
543 }
544 
545 
546 template<class Type, class GeoMesh>
548 (
549  const tmp<DimensionedField<scalar, GeoMesh>>& tweightField
550 ) const
551 {
552  dimensioned<Type> wa = weightedAverage(tweightField());
553  tweightField.clear();
554  return wa;
555 }
556 
557 
558 template<class Type, class GeoMesh>
560 (
562 )
563 {
564  // Check for assignment to self
565  if (this == &df)
566  {
568  << "attempted assignment to self"
569  << abort(FatalError);
570  }
571 
572  dimensions_ = df.dimensions();
574 }
575 
576 
577 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
578 
579 template<class Type, class GeoMesh>
581 (
583 )
584 {
585  // Check for assignment to self
586  if (this == &df)
587  {
589  << "attempted assignment to self"
590  << abort(FatalError);
591  }
592 
593  checkField(*this, df, "=");
594 
595  dimensions_ = df.dimensions();
597 }
598 
599 
600 template<class Type, class GeoMesh>
602 (
604 )
605 {
606  // Check for assignment to self
607  if (this == &df)
608  {
610  << "attempted assignment to self"
611  << abort(FatalError);
612  }
613 
614  checkField(*this, df, "=");
615 
616  dimensions_ = move(df.dimensions());
617  Field<Type>::operator=(move(df));
618 }
619 
620 
621 template<class Type, class GeoMesh>
623 (
625 )
626 {
627  const DimensionedField<Type, GeoMesh>& df = tdf();
628 
629  // Check for assignment to self
630  if (this == &df)
631  {
633  << "attempted assignment to self"
634  << abort(FatalError);
635  }
636 
637  checkField(*this, df, "=");
638 
639  dimensions_ = df.dimensions();
640  this->transfer(const_cast<DimensionedField<Type, GeoMesh>&>(df));
641  tdf.clear();
642 }
643 
644 
645 template<class Type, class GeoMesh>
647 (
648  const dimensioned<Type>& dt
649 )
650 {
651  dimensions_ = dt.dimensions();
652  Field<Type>::operator=(dt.value());
653 }
654 
655 
656 template<class Type, class GeoMesh>
658 {
660 }
661 
662 
663 #define COMPUTED_ASSIGNMENT(TYPE, op) \
664  \
665 template<class Type, class GeoMesh> \
666 void DimensionedField<Type, GeoMesh>::operator op \
667 ( \
668  const DimensionedField<TYPE, GeoMesh>& df \
669 ) \
670 { \
671  checkField(*this, df, #op); \
672  \
673  dimensions_ op df.dimensions(); \
674  Field<Type>::operator op(df); \
675 } \
676  \
677 template<class Type, class GeoMesh> \
678 void DimensionedField<Type, GeoMesh>::operator op \
679 ( \
680  const tmp<DimensionedField<TYPE, GeoMesh>>& tdf \
681 ) \
682 { \
683  operator op(tdf()); \
684  tdf.clear(); \
685 } \
686  \
687 template<class Type, class GeoMesh> \
688 void DimensionedField<Type, GeoMesh>::operator op \
689 ( \
690  const dimensioned<TYPE>& dt \
691 ) \
692 { \
693  dimensions_ op dt.dimensions(); \
694  Field<Type>::operator op(dt.value()); \
695 }
696 
701 
702 #undef COMPUTED_ASSIGNMENT
703 
704 
705 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
706 
707 #undef checkField
708 
709 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
710 
711 } // End namespace Foam
712 
713 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
714 
715 #include "DimensionedFieldIO.C"
717 
718 // ************************************************************************* //
#define checkField(df1, df2, op)
#define COMPUTED_ASSIGNMENT(TYPE, op)
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
GeoMesh::Mesh Mesh
Type of mesh on which this DimensionedField is instantiated.
const dimensionSet & dimensions() const
Return dimensions.
const Mesh & mesh() const
Return mesh.
Field< Type >::cmptType cmptType
Component type of the elements of the field.
const Field< Type > & field() const
DimensionedField(const IOobject &, const Mesh &mesh, const dimensionSet &, const Field< Type > &)
Construct from components.
Pre-declare SubField and related Field type.
Definition: Field.H:82
Generic mesh wrapper used by volMesh, surfaceMesh, pointMesh etc.
Definition: GeoMesh.H:47
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
const fileName & local() const
Definition: IOobject.H:400
fileName & instance() const
Return the instance directory, constant, system, <time> etc.
Definition: IOobject.C:355
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:312
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:125
Dimension set for the base types.
Definition: dimensionSet.H:122
Generic dimensioned Type class.
bool cacheTemporaryObject(const word &name) const
Return true if given name is in the cacheTemporaryObjects set.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:55
A class for managing temporary objects.
Definition: tmp.H:55
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:181
A class for handling words, derived from string.
Definition: word.H:62
A class representing the concept of 0 used to avoid unnecessary manipulations for objects that are kn...
Definition: zero.H:50
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
autoPtr< CompressibleMomentumTransportModel > New(const volScalarField &rho, const volVectorField &U, const surfaceScalarField &phi, const viscosity &viscosity)
tmp< VolField< Type > > average(const SurfaceField< Type > &ssf)
Area-weighted average a surfaceField creating a volField.
Definition: fvcAverage.C:46
Namespace for OpenFOAM.
static const zero Zero
Definition: zero.H:97
Type gSum(const FieldField< Field, Type > &f)
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
errorManip< error > abort(error &err)
Definition: errorManip.H:131
T clone(const T &t)
Definition: List.H:55
Type gAverage(const FieldField< Field, Type > &f)
error FatalError
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
uint8_t direction
Definition: direction.H:45