DimensionedField.H
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 Class
28  Foam::DimensionedField
29 
30 Description
31  Field with dimensions and associated with geometry type GeoMesh which is
32  used to size the field and a reference to it is maintained.
33 
34 SourceFiles
35  DimensionedFieldI.H
36  DimensionedField.C
37  DimensionedFieldIO.C
38 
39 \*---------------------------------------------------------------------------*/
40 
41 #ifndef Foam_DimensionedField_H
42 #define Foam_DimensionedField_H
43 
44 #include "regIOobject.H"
45 #include "Field.H"
46 #include "dimensionedType.H"
47 #include "orientedType.H"
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 // Forward Declarations
55 template<class Type, class GeoMesh> class DimensionedField;
56 
57 template<class Type, class GeoMesh>
58 Ostream& operator<<
59 (
60  Ostream& os,
61  const DimensionedField<Type, GeoMesh>& df
62 );
63 
64 template<class Type, class GeoMesh>
65 Ostream& operator<<
66 (
67  Ostream& os,
68  const tmp<DimensionedField<Type, GeoMesh>>& tdf
69 );
70 
71 
72 /*---------------------------------------------------------------------------*\
73  Class DimensionedField Declaration
74 \*---------------------------------------------------------------------------*/
75 
76 template<class Type, class GeoMesh>
77 class DimensionedField
78 :
79  public regIOobject,
80  public Field<Type>
81 {
82 public:
83 
84  // Public Typedefs
85 
86  //- The template instance used for this DimensionedField type
88 
89  //- The mesh type for the DimensionedField
90  typedef typename GeoMesh::Mesh Mesh;
91 
92  //- The DimensionedField is usually synonymous with an internal field
94 
95  //- Type of the field from which this DimensionedField is derived
96  typedef Field<Type> FieldType;
97 
98  //- Component type of the field elements
99  typedef typename Field<Type>::cmptType cmptType;
100 
101 
102 private:
103 
104  // Private Data
105 
106  //- Reference to mesh
107  const Mesh& mesh_;
108 
109  //- Dimension set for this field
110  dimensionSet dimensions_;
111 
112  //- Oriented flag
113  orientedType oriented_;
114 
115 
116  // Private Member Functions
117 
118  //- Assert that non-zero field size == mesh size
119  void checkFieldSize() const;
120 
121  //- Read from file if it is present. Checks the readOption flag
122  bool readIfPresent(const word& fieldDictEntry = "value");
123 
124  //- Read the field - create the field dictionary on-the-fly
125  void readField(const word& fieldDictEntry = "value");
126 
127  //- Implementation for 'New' with specified registerObject preference.
128  // For LEGACY_REGISTER, registration is determined by
129  // objectRegistry::is_cacheTemporaryObject().
130  template<class... Args>
131  static tmp<DimensionedField<Type, GeoMesh>> New_impl
132  (
134  const word& name,
135  const Mesh& mesh,
136  Args&&... args
137  );
138 
139 public:
140 
141  //- Runtime type information
142  TypeName("DimensionedField");
143 
144 
145  // Static Member Functions
146 
147  //- Return a null DimensionedField (reference to a nullObject).
148  static const this_type& null() noexcept
149  {
150  return NullObjectRef<this_type>();
151  }
152 
153 
154  // Constructors
155 
156  //- Construct from components, copy initial field content
158  (
159  const IOobject& io,
160  const Mesh& mesh,
161  const dimensionSet& dims,
162  const Field<Type>& field
163  );
164 
165  //- Construct from components, transferring initial field content
167  (
168  const IOobject& io,
169  const Mesh& mesh,
170  const dimensionSet& dims,
172  );
173 
174  //- Construct from components, transferring initial field content
176  (
177  const IOobject& io,
178  const Mesh& mesh,
179  const dimensionSet& dims,
180  List<Type>&& field
181  );
182 
183  //- Construct from components, copy or transfer tmp content
185  (
186  const IOobject& io,
187  const Mesh& mesh,
188  const dimensionSet& dims,
189  const tmp<Field<Type>>& tfield
190  );
191 
192  //- Construct from components, setting initial size and dimensions,
193  //- but not initialising any field values.
194  // Used for temporary fields which are initialised after construction
196  (
197  const IOobject& io,
198  const Mesh& mesh,
199  const dimensionSet& dims,
200  const bool checkIOFlags = true
201  );
202 
203  //- Construct from components, setting dimensions and initial
204  //- uniform field value.
206  (
207  const IOobject& io,
208  const Mesh& mesh,
209  const Type& value,
210  const dimensionSet& dims,
211  const bool checkIOFlags = true
212  );
213 
214  //- Construct from components, setting dimensions and initial
215  //- uniform field value.
216  // The name of the dimensioned<Type> has no influence.
218  (
219  const IOobject& io,
220  const Mesh& mesh,
221  const dimensioned<Type>& dt,
222  const bool checkIOFlags = true
223  );
224 
225  //- Construct from Istream.
227  (
228  const IOobject& io,
229  const Mesh& mesh,
230  const word& fieldDictEntry = "value"
231  );
232 
233  //- Construct from dictionary
235  (
236  const IOobject& io,
237  const Mesh& mesh,
238  const dictionary& fieldDict,
239  const word& fieldDictEntry = "value"
240  );
241 
242  //- Copy construct
244 
245  //- Move construct
247 
248  //- Copy construct or reuse (move) as specified.
250 
251  //- Construct from tmp<DimensionedField> deleting argument
253  (
255  );
256 
257  //- Copy construct, resetting IO parameters
259  (
260  const IOobject& io,
262  );
263 
264  //- Move construct, resetting IO parameters
266  (
267  const IOobject& io,
269  );
270 
271  //- Copy or move construct, resetting IO parameters.
273  (
274  const IOobject& io,
276  bool reuse
277  );
278 
279  //- Construct from tmp<DimensionedField> deleting argument,
280  //- resetting IO parameters.
282  (
283  const IOobject& io,
285  );
286 
287  //- Copy construct with a new name
289  (
290  const word& newName,
292  );
293 
294  //- Move construct with a new name
296  (
297  const word& newName,
299  );
300 
301  //- Copy or move construct, resetting name.
303  (
304  const word& newName,
306  bool reuse
307  );
308 
309  //- Construct with a new name from tmp<DimensionedField>
311  (
312  const word& newName,
314  );
315 
316  //- Clone
318 
319 
320  // Factory Methods
321 
322  //- Return tmp field (NO_READ, NO_WRITE)
323  //- from name, mesh, dimensions, copy of internal field.
324  //- [Takes current timeName from the mesh registry].
325  // For LEGACY_REGISTER, registration is determined by
326  // objectRegistry::is_cacheTemporaryObject().
328  (
329  const word& name,
331  const Mesh& mesh,
332  const dimensionSet& dims,
333  const Field<Type>& iField
334  );
335 
336  //- Return tmp field (NO_READ, NO_WRITE)
337  //- from name, mesh, dimensions, copy of internal field.
338  //- [Takes current timeName from the mesh registry].
339  // Registration/persistence determined by
340  // objectRegistry::is_cacheTemporaryObject().
342  (
343  const word& name,
344  const Mesh& mesh,
345  const dimensionSet& dims,
346  const Field<Type>& iField
347  );
348 
349  //- Return tmp field (NO_READ, NO_WRITE)
350  //- from name, mesh, dimensions, move internal field contents.
351  //- [Takes current timeName from the mesh registry].
352  // For LEGACY_REGISTER, registration is determined by
353  // objectRegistry::is_cacheTemporaryObject().
355  (
356  const word& name,
358  const Mesh& mesh,
359  const dimensionSet& dims,
360  Field<Type>&& iField
361  );
362 
363  //- Return tmp field (NO_READ, NO_WRITE)
364  //- from name, mesh, dimensions, move internal field contents.
365  //- [Takes current timeName from the mesh registry].
366  // Registration/persistence determined by
367  // objectRegistry::is_cacheTemporaryObject().
369  (
370  const word& name,
371  const Mesh& mesh,
372  const dimensionSet& dims,
373  Field<Type>&& iField
374  );
375 
376  //- Return tmp field (NO_READ, NO_WRITE)
377  //- from name, mesh, dimensions, primitive field (tmp).
378  //- [Takes current timeName from the mesh registry].
379  // For LEGACY_REGISTER, registration is determined by
380  // objectRegistry::is_cacheTemporaryObject().
382  (
383  const word& name,
385  const Mesh& mesh,
386  const dimensionSet& dims,
387  const tmp<Field<Type>>& tfield
388  );
389 
390  //- Return tmp field (NO_READ, NO_WRITE)
391  //- from name, mesh, dimensions, primitive field (tmp).
392  //- [Takes current timeName from the mesh registry].
393  // Registration/persistence determined by
394  // objectRegistry::is_cacheTemporaryObject().
396  (
397  const word& name,
398  const Mesh& mesh,
399  const dimensionSet& dims,
400  const tmp<Field<Type>>& tfield
401  );
402 
403  //- Return tmp field (NO_READ, NO_WRITE)
404  //- from name, mesh, dimensions.
405  //- [Takes current timeName from the mesh registry].
406  // For LEGACY_REGISTER, registration is determined by
407  // objectRegistry::is_cacheTemporaryObject().
409  (
410  const word& name,
412  const Mesh& mesh,
413  const dimensionSet& dims
414  );
415 
416  //- Return tmp field (NO_READ, NO_WRITE)
417  //- from name, mesh, dimensions.
418  //- [Takes current timeName from the mesh registry].
419  // Registration/persistence determined by
420  // objectRegistry::is_cacheTemporaryObject().
422  (
423  const word& name,
424  const Mesh& mesh,
425  const dimensionSet& dims
426  );
427 
428  //- Return tmp field (NO_READ, NO_WRITE)
429  //- from name, mesh, value, dimensions.
430  //- [Takes current timeName from the mesh registry].
431  // For LEGACY_REGISTER, registration is determined by
432  // objectRegistry::is_cacheTemporaryObject().
434  (
435  const word& name,
437  const Mesh& mesh,
438  const Type& value,
439  const dimensionSet& dims
440  );
441 
442  //- Return tmp field (NO_READ, NO_WRITE)
443  //- from name, mesh, value, dimensions.
444  //- [Takes current timeName from the mesh registry].
445  // Registration/persistence determined by
446  // objectRegistry::is_cacheTemporaryObject().
448  (
449  const word& name,
450  const Mesh& mesh,
451  const Type& value,
452  const dimensionSet& dims
453  );
454 
455  //- Return tmp field (NO_READ, NO_WRITE)
456  //- from name, mesh, dimensioned-type.
457  //- [Takes current timeName from the mesh registry].
458  // For LEGACY_REGISTER, registration is determined by
459  // objectRegistry::is_cacheTemporaryObject().
461  (
462  const word& name,
464  const Mesh& mesh,
465  const dimensioned<Type>& dt
466  );
467 
468  //- Return tmp field (NO_READ, NO_WRITE)
469  //- from name, mesh, dimensioned-type.
470  //- [Takes current timeName from the mesh registry].
471  // Registration/persistence determined by
472  // objectRegistry::is_cacheTemporaryObject().
474  (
475  const word& name,
476  const Mesh& mesh,
477  const dimensioned<Type>& dt
478  );
479 
480  //- Return renamed tmp field (NO_READ, NO_WRITE)
481  //- retaining its instance/local.
482  // For LEGACY_REGISTER, registration is determined by
483  // objectRegistry::is_cacheTemporaryObject().
485  (
486  const word& newName,
489  );
490 
491  //- Return renamed tmp field (NO_READ, NO_WRITE)
492  //- retaining its instance/local.
493  // Registration/persistence determined by
494  // objectRegistry::is_cacheTemporaryObject().
496  (
497  const word& newName,
499  );
500 
501  //- Construct tmp field (NO_READ, NO_WRITE)
502  //- based on mesh/registry information from an existing field.
503  //- [Takes instance from the field].
504  // Registration/persistence determined by
505  // objectRegistry::is_cacheTemporaryObject().
506  template<class AnyType>
508  (
510  const word& name,
511  const dimensionSet& dims
512  );
513 
514  //- Construct tmp field (NO_READ, NO_WRITE)
515  //- based on mesh/registry information from an existing field
516  //- and initialise with value.
517  // Registration/persistence determined by
518  // objectRegistry::is_cacheTemporaryObject().
519  template<class AnyType>
521  (
523  const word& name,
524  const dimensioned<Type>& dt
525  );
526 
527 
528  //- Destructor
529  virtual ~DimensionedField();
530 
531 
532  // Member Functions
533 
534  //- Resize to the mesh size and read the field from the dictionary.
535  //- Internal implementation checks field vs mesh size.
536  void readField
537  (
538  const dictionary& fieldDict,
539  const word& fieldDictEntry = "value"
540  );
541 
542  //- Return non-const reference to this field
543  this_type& constCast() const noexcept
544  {
545  return const_cast<this_type&>(*this);
546  }
547 
548  //- Return const reference to mesh
549  const Mesh& mesh() const noexcept { return mesh_; }
550 
551  //- Return dimensions
552  const dimensionSet& dimensions() const noexcept { return dimensions_; }
553 
554  //- Return non-const access to dimensions
555  dimensionSet& dimensions() noexcept { return dimensions_; }
556 
557  //- Return oriented type
558  orientedType oriented() const noexcept { return oriented_; }
559 
560  //- Return non-const access to the oriented type
561  orientedType& oriented() noexcept { return oriented_; }
562 
563  //- True if field is ORIENTED
564  inline bool is_oriented() const noexcept;
565 
566  //- Set the oriented flag: on/off
567  inline void setOriented(bool on = true) noexcept;
568 
569  //- Return const-reference to the primitive field values
570  const Field<Type>& field() const noexcept { return *this; }
571 
572  //- Return reference to the primitive field values
573  Field<Type>& field() noexcept { return *this; }
574 
575  //- Return a component field of the field
576  tmp<DimensionedField<cmptType, GeoMesh>> component
577  (
578  const direction d
579  ) const;
580 
581  //- Replace a component field of the field
582  void replace
583  (
584  const direction d,
585  const DimensionedField<cmptType, GeoMesh>& df
586  );
587 
588  //- Replace a component field of the field
589  void replace
590  (
591  const direction d,
592  const tmp<DimensionedField<cmptType, GeoMesh>>& tdf
593  );
594 
595  // Inherits clamp, clamp_min, clamp_max (without dimensions) from Field
596 
597  //- Return the field transpose (only defined for second rank tensors)
598  tmp<DimensionedField<Type, GeoMesh>> T() const;
599 
600  //- Return the global arithmetic average
601  dimensioned<Type> average(const label comm = UPstream::worldComm) const;
602 
603  //- Return the global weighted average
604  dimensioned<Type> weightedAverage
605  (
606  const DimensionedField<scalar, GeoMesh>& weights,
607  const label comm = UPstream::worldComm
608  ) const;
609 
610  //- Return the global weighted average
611  dimensioned<Type> weightedAverage
612  (
613  const tmp<DimensionedField<scalar, GeoMesh>>& tweights,
614  const label comm = UPstream::worldComm
615  ) const;
616 
617 
618  // Write
619 
620  //- Write dimensions, oriented flag (if valid) and the
621  //- field data as a dictionary entry with the specified name.
622  bool writeData(Ostream& os, const word& fieldDictEntry) const;
623 
624  //- The writeData function (required by regIOobject),
625  //- calls writeData with dictionary entry name = "value"
626  bool writeData(Ostream& os) const { return writeData(os, "value"); }
627 
628 
629  // Member Operators
630 
631  void operator=(const DimensionedField<Type, GeoMesh>& df);
632  void operator=(const tmp<DimensionedField<Type, GeoMesh>>& tdf);
633 
634  //- Assign dimensions and value.
635  void operator=(const dimensioned<Type>& dt);
636 
637  void operator+=(const DimensionedField<Type, GeoMesh>& df);
638  void operator+=(const tmp<DimensionedField<Type, GeoMesh>>& tdf);
639 
640  void operator-=(const DimensionedField<Type, GeoMesh>& df);
641  void operator-=(const tmp<DimensionedField<Type, GeoMesh>>& tdf);
642 
643  void operator*=(const DimensionedField<scalar, GeoMesh>& df);
644  void operator*=(const tmp<DimensionedField<scalar, GeoMesh>>& tdf);
645 
646  void operator/=(const DimensionedField<scalar, GeoMesh>& df);
647  void operator/=(const tmp<DimensionedField<scalar, GeoMesh>>& tdf);
648 
649  void operator+=(const dimensioned<Type>& dt);
650  void operator-=(const dimensioned<Type>& dt);
651 
652  void operator*=(const dimensioned<scalar>& dt);
653  void operator/=(const dimensioned<scalar>& dt);
654 
655 
656  // Ostream Operators
657 
658  //- Calls DimensionedField::writeData()
659  friend Ostream& operator<< <Type, GeoMesh>
660  (
661  Ostream& os,
662  const DimensionedField<Type, GeoMesh>& df
663  );
664 
665  //- Calls DimensionedField::writeData()
666  friend Ostream& operator<< <Type, GeoMesh>
667  (
668  Ostream& os,
670  );
671 };
672 
673 
674 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
676 } // End namespace Foam
677 
678 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
679 
682 
683 #ifdef NoRepository
684  #include "DimensionedField.C"
685 #endif
686 
687 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
688 
689 #endif
691 // ************************************************************************* //
tmp< DimensionedField< Type, GeoMesh > > clone() const
Clone.
static const this_type & null() noexcept
Return a null DimensionedField (reference to a nullObject).
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.
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
dimensioned< Type > weightedAverage(const DimensionedField< scalar, GeoMesh > &weights, const label comm=UPstream::worldComm) const
Return the global weighted average.
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:195
DimensionedField(const IOobject &io, const Mesh &mesh, const dimensionSet &dims, const Field< Type > &field)
Construct from components, copy initial field content.
pTraits< Type >::cmptType cmptType
Component type.
Definition: Field.H:172
bool is_oriented() const noexcept
True if field is ORIENTED.
void operator-=(const DimensionedField< Type, GeoMesh > &df)
Class to determine the &#39;oriented&#39; status of surface fields.
Definition: orientedType.H:52
Generic dimensioned Type class.
Field< Type >::cmptType cmptType
Component type of the field elements.
static label worldComm
Communicator for all ranks. May differ from commGlobal() if local worlds are in use.
Definition: UPstream.H:987
DimensionedField< Type, GeoMesh > this_type
The template instance used for this DimensionedField type.
Field< Type > FieldType
Type of the field from which this DimensionedField is derived.
tmp< DimensionedField< cmptType, GeoMesh > > component(const direction d) const
Return a component field of the field.
orientedType oriented() const noexcept
Return oriented type.
void operator/=(const DimensionedField< scalar, GeoMesh > &df)
Dimension set for the base types, which can be used to implement rigorous dimension checking for alge...
Definition: dimensionSet.H:105
Generic templated field type.
Definition: Field.H:63
bool writeData(Ostream &os, const word &fieldDictEntry) const
Write dimensions, oriented flag (if valid) and the field data as a dictionary entry with the specifie...
A class for handling words, derived from Foam::string.
Definition: word.H:63
void operator*=(const DimensionedField< scalar, GeoMesh > &df)
void operator=(const DimensionedField< Type, GeoMesh > &df)
void setOriented(bool on=true) noexcept
Set the oriented flag: on/off.
void operator+=(const DimensionedField< Type, GeoMesh > &df)
tmp< DimensionedField< Type, GeoMesh > > T() const
Return the field transpose (only defined for second rank tensors)
TypeName("DimensionedField")
Runtime type information.
virtual ~DimensionedField()
Destructor.
GeoMesh::Mesh Mesh
The mesh type for the DimensionedField.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: scalarImpl.H:255
OBJstream os(runTime.globalPath()/outputName)
MESH Mesh
Definition: GeoMesh.H:61
const dimensionSet & dimensions() const noexcept
Return dimensions.
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))
registerOption
Enumeration for use with registerObject(). Values map to bool (false/true)
const Field< Type > & field() const noexcept
Return const-reference to the primitive field values.
static tmp< DimensionedField< Type, GeoMesh > > New(const word &name, IOobjectOption::registerOption regOpt, const Mesh &mesh, const dimensionSet &dims, const Field< Type > &iField)
Return tmp field (NO_READ, NO_WRITE) from name, mesh, dimensions, copy of internal field...
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
const Mesh & mesh() const noexcept
Return const reference to mesh.
DimensionedField< Type, GeoMesh > Internal
The DimensionedField is usually synonymous with an internal field.
constexpr Field() noexcept
Default construct.
Definition: FieldI.H:24
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
Foam::argList args(argc, argv)
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:180
Namespace for OpenFOAM.
this_type & constCast() const noexcept
Return non-const reference to this field.