Field.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::Field
29 
30 Description
31  Generic templated field type.
32 
33 SourceFiles
34  FieldFunctions.H
35  FieldFunctionsM.H
36  FieldMapper.H
37  FieldI.H
38  FieldM.H
39  Field.C
40  FieldBase.C
41  FieldFunctions.C
42  FieldFunctionsM.C
43 
44 \*---------------------------------------------------------------------------*/
45 
46 #ifndef Foam_Field_H
47 #define Foam_Field_H
48 
49 #include "tmp.H"
50 #include "direction.H"
51 #include "labelList.H"
52 #include "keyType.H"
53 #include "ops.H"
54 #include "scalarList.H"
55 #include "VectorSpace.H"
56 #include "IOobjectOption.H"
57 
58 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
59 
60 namespace Foam
61 {
62 
63 // Forward Declarations
65 class dictionary;
66 class entry;
67 
68 template<class Type> class Field;
69 template<class Type> class SubField;
70 
71 template<class Type> Ostream& operator<<(Ostream&, const Field<Type>&);
72 template<class Type> Ostream& operator<<(Ostream&, const tmp<Field<Type>>&);
73 
74 /*---------------------------------------------------------------------------*\
75  Class FieldBase Declaration
76 \*---------------------------------------------------------------------------*/
77 
78 //- Template invariant parts for Field and SubField
79 class FieldBase
80 :
81  public refCount
82 {
83 public:
84 
85  // Static Data Members
86 
87  //- Typename for Field
88  static const char* const typeName;
89 
90  //- Permit read construct from a larger size.
91  // Mostly required for things like column mesh, for example.
92  static bool allowConstructFromLargerSize;
93 
94  //- GeometricField with extra capacity for flattened boundary fields.
95  //- Uses opt-switch "unifiedGeometricField"
96  static bool unifiedGeometricField;
97 
98  //- Local boundary field consistency checks.
99  //- Uses opt-switch "localBoundaryConsistency"
101 
102  //- Tolerance for local boundary field consistency checks.
103  //- Uses opt-switch "localBoundaryConsistency::tolerance"
104  static scalar localBoundaryTolerance_;
105 
107  // Static Member Functions
108 
109  //- Warn about keyword changes for local boundary consistency checks.
110  // The supplied dictionary corresponds to the optimisationSwitches
113  //- Get flag for local boundary consistency checks.
115  {
117  }
118 
119  //- Set flag for local boundary consistency checks.
120  // \return the previous value
121  static int localBoundaryConsistency(int val) noexcept
122  {
123  int old(localBoundaryConsistency_);
125  return old;
126  }
128 
129  // Constructors
130 
131  //- Default construct
132  constexpr FieldBase() noexcept
133  :
134  refCount()
135  {}
136 };
138 
139 /*---------------------------------------------------------------------------*\
140  Class Field Declaration
141 \*---------------------------------------------------------------------------*/
142 
143 template<class Type>
144 class Field
145 :
146  public FieldBase,
147  public List<Type>
148 {
149 public:
151  //- Component type
152  typedef typename pTraits<Type>::cmptType cmptType;
153 
154  //- Declare type of subField
155  typedef SubField<Type> subField;
156 
157 
158  // Static Member Functions
159 
160  //- Return a null Field (reference to a nullObject).
161  //- Behaves like an empty Field.
162  static const Field<Type>& null() noexcept
163  {
164  return NullObjectRef<Field<Type>>();
165  }
166 
167 
168  // Constructors
169 
170  //- Default construct
171  // For temporary fields that are initialised after construction
172  inline constexpr Field() noexcept;
173 
174  //- Construct given size
175  // For temporary fields that are initialised after construction
176  inline explicit Field(const label len);
178  //- Construct given size and initial value
179  inline Field(const label len, const Type& val);
180 
181  //- Construct given size and initial values of zero
182  inline Field(const label len, Foam::zero);
183 
184  //- Construct with length=1, copying the value as the only content
185  inline Field(Foam::one, const Type& val);
187  //- Construct with length=1, moving the value as the only content
188  inline Field(Foam::one, Type&& val);
189 
190  //- Construct with length=1, initializing content to zero
191  inline Field(Foam::one, Foam::zero);
192 
193  //- Copy construct
194  inline Field(const Field<Type>& fld);
195 
196  //- Copy construct from UList<Type>
197  inline explicit Field(const UList<Type>& list);
198 
199  //- Copy construct from IndirectList
200  template<class Addr>
201  inline explicit Field(const IndirectListBase<Type, Addr>& list);
202 
203  //- Move construct from Field
204  inline Field(Field<Type>&& fld) noexcept;
205 
206  //- Move construct from List
207  inline Field(List<Type>&& list) noexcept;
208 
209  //- Move construct from DynamicList
210  template<int SizeMin>
211  inline Field(DynamicList<Type, SizeMin>&& list);
212 
213  //- Construct by 1 to 1 mapping from the given field
214  Field
215  (
216  const UList<Type>& mapF,
217  const labelUList& mapAddressing
218  );
219 
220  //- Construct by 1 to 1 mapping from the given tmp field
221  Field
222  (
223  const tmp<Field<Type>>& tmapF,
224  const labelUList& mapAddressing
225  );
226 
227  //- Construct by interpolative mapping from the given field
228  Field
229  (
230  const UList<Type>& mapF,
231  const labelListList& mapAddressing,
232  const scalarListList& weights
233  );
234 
235  //- Construct by interpolative mapping from the given tmp field
236  Field
237  (
238  const tmp<Field<Type>>& tmapF,
239  const labelListList& mapAddressing,
240  const scalarListList& weights
241  );
242 
243  //- Construct by mapping from the given field
244  Field
245  (
246  const UList<Type>& mapF,
247  const FieldMapper& map,
248  const bool applyFlip = true
249  );
250 
251  //- Construct by mapping from the given field
252  Field
253  (
254  const UList<Type>& mapF,
255  const FieldMapper& map,
256  const Type& defaultValue,
257  const bool applyFlip = true
258  );
259 
260  //- Construct by mapping from the given field
261  Field
262  (
263  const UList<Type>& mapF,
264  const FieldMapper& map,
265  const UList<Type>& defaultValues,
266  const bool applyFlip = true
267  );
268 
269  //- Construct by mapping from the given tmp field
270  Field
271  (
272  const tmp<Field<Type>>& tmapF,
273  const FieldMapper& map,
274  const bool applyFlip = true
275  );
276 
277  //- Construct by mapping from the given tmp field.
278  //- Uses supplied uniform value for unmapped items
279  Field
280  (
281  const tmp<Field<Type>>& tmapF,
282  const FieldMapper& map,
283  const Type& defaultValue,
284  const bool applyFlip = true
285  );
286 
287  //- Construct by mapping from the given tmp field.
288  //- Uses supplied values for unmapped items
289  Field
290  (
291  const tmp<Field<Type>>& tmapF,
292  const FieldMapper& map,
293  const UList<Type>& defaultValues,
294  const bool applyFlip = true
295  );
296 
297  //- Copy construct or re-use as specified.
298  inline Field(Field<Type>& fld, bool reuse);
299 
300  //- Copy or move construct from tmp
301  inline Field(const tmp<Field<Type>>& tfld);
302 
303  //- Construct from Istream
304  inline Field(Istream& is);
305 
306  //- Construct from a dictionary (primitive) entry.
307  Field(const entry& e, const label len);
308 
309  //- Lookup of a primitive dictionary entry by (literal) name
310  //- and assign its contents to this. The behaviour largely as
311  //- described in assign():
312  // - For MUST_READ and key not found: FatalIOError.
313  // - For LAZY_READ and key not found: initialise field with zero.
314  // - For NO_READ and key not found: simply size the field.
315  // .
316  Field
317  (
318  const word& key,
319  const dictionary& dict,
320  const label len,
321  IOobjectOption::readOption readOpt = IOobjectOption::MUST_READ
322  );
323 
324  //- Clone
325  inline tmp<Field<Type>> clone() const;
326 
327  //- Return a pointer to a new Field created on freestore
328  static autoPtr<Field<Type>> New(Istream& is)
329  {
330  return autoPtr<Field<Type>>(new Field<Type>(is));
331  }
332 
333  //- Return a pointer to a new calculatedFvPatchFieldField created on
334  //- freestore without setting patchField values
335  template<class Type2>
337  {
338  return tmp<Field<Type>>::New(f.size());
339  }
340 
341 
342  // Member Functions
343 
344  //- Assign from a primitive dictionary entry with the following
345  //- behaviour:
346  // - If len == 0 : a no-op
347  // - If len < 0 : no size checking
348  // - If len > 0 : resize \em uniform entries to this size
349  // before assigning. Use as length check for \em nonuniform
350  // entries. A mismatch is possibly Fatal, except when
351  // allowConstructFromLargerSize is true (eg, for column mesh).
352  // .
353  void assign(const entry& e, const label len);
354 
355  //- Lookup a primitive dictionary entry by (literal) name
356  //- and assign its contents to this (behaviour as described above).
357  // - If len == 0 : a no-op and return True.
358  // - For NO_READ : a no-op and return False.
359  // - For LAZY_READ and key not found : a no-op and return False.
360  // - For MUST_READ and key not found : FatalIOError
361  // .
362  // \returns True if an assignment occurred or for zero-length.
363  bool assign
364  (
365  const word& key,
366  const dictionary& dict,
367  const label len,
369  );
370 
371  //- 1 to 1 map from the given field
372  void map
373  (
374  const UList<Type>& mapF,
375  const labelUList& mapAddressing
376  );
377 
378  //- 1 to 1 map from the given tmp field
379  void map
380  (
381  const tmp<Field<Type>>& tmapF,
382  const labelUList& mapAddressing
383  );
384 
385  //- Interpolative map from the given field
386  void map
387  (
388  const UList<Type>& mapF,
389  const labelListList& mapAddressing,
390  const scalarListList& weights
391  );
392 
393  //- Interpolative map from the given tmp field
394  void map
395  (
396  const tmp<Field<Type>>& tmapF,
397  const labelListList& mapAddressing,
398  const scalarListList& weights
399  );
400 
401  //- Map from the given field
402  void map
403  (
404  const UList<Type>& mapF,
405  const FieldMapper& map,
406  const bool applyFlip = true
407  );
408 
409  //- Map from the given tmp field
410  void map
411  (
412  const tmp<Field<Type>>& tmapF,
413  const FieldMapper& map,
414  const bool applyFlip = true
415  );
416 
417  //- Map from self
418  void autoMap
419  (
420  const FieldMapper& map,
421  const bool applyFlip = true
422  );
423 
424  //- 1 to 1 reverse-map from the given field
425  void rmap
426  (
427  const UList<Type>& mapF,
428  const labelUList& mapAddressing
429  );
430 
431  //- 1 to 1 reverse-map from the given tmp field
432  void rmap
433  (
434  const tmp<Field<Type>>& tmapF,
435  const labelUList& mapAddressing
436  );
437 
438  //- Interpolative reverse map from the given field
439  void rmap
440  (
441  const UList<Type>& mapF,
442  const labelUList& mapAddressing,
443  const UList<scalar>& weights
444  );
445 
446  //- Interpolative reverse map from the given tmp field
447  void rmap
448  (
449  const tmp<Field<Type>>& tmapF,
450  const labelUList& mapAddressing,
451  const UList<scalar>& weights
452  );
453 
454  //- Inplace negate this field (negative).
455  // Inverts the state for a bool field.
456  void negate();
457 
458  //- Inplace normalise this field.
459  //- Generally a no-op except for vector fields.
460  // Vector fields are normalised by their magnitude.
461  // Small vectors (mag less than ROOTVSMALL) are set to zero.
462  void normalise();
463 
464  //- Return a component field of the field
466 
467  //- Replace a component field of the field
468  void replace(const direction, const UList<cmptType>&);
469 
470  //- Replace a component field of the field
471  void replace(const direction, const tmp<Field<cmptType>>&);
472 
473  //- Replace a component field of the field
474  void replace(const direction, const cmptType&);
475 
476  //- Impose lower (floor) clamp on the field values (in-place)
477  void clamp_min(const Type& lower);
478 
479  //- Impose lower (floor) clamp on the field values (in-place)
480  void clamp_min(const UList<Type>& lower);
481 
482  //- Impose upper (ceiling) clamp on the field values (in-place)
483  void clamp_max(const Type& upper);
484 
485  //- Impose upper (ceiling) clamp on the field values (in-place)
486  void clamp_max(const UList<Type>& upper);
487 
488  //- Clamp field values (in-place) to the specified range.
489  // Does not check if range is valid or not.
490  void clamp_range(const Type& lower, const Type& upper);
491 
492  //- Clamp field values (in-place) to the specified range.
493  // Does not check if range is valid or not.
494  void clamp_range(const MinMax<Type>& range);
495 
496  template<class VSForm>
497  VSForm block(const label start) const;
498 
499  //- Return the field transpose (only defined for second rank tensors)
500  tmp<Field<Type>> T() const;
501 
502  //- Write the field as a dictionary entry
503  void writeEntry(const word& keyword, Ostream& os) const;
504 
505 
506  // Other Access
507 
508  //- Return SubField slice (non-const access) - no range checking
509  SubField<Type> slice(const label pos, label len = -1);
510 
511  //- Return SubField slice (const access) - no range checking
512  const SubField<Type> slice(const label pos, label len = -1) const;
513 
514  //- Return SubField slice (non-const access) - with range checking
516 
517  //- Return SubField slice (const access) - with range checking
518  const SubField<Type> slice(const labelRange& range) const;
519 
520 
521  // Assignment
522 
523  //- Copy assignment
524  void operator=(const Field<Type>&);
525  void operator=(const tmp<Field<Type>>&);
526 
527  inline void operator=(const UList<Type>& rhs);
528  inline void operator=(const SubField<Type>& rhs);
529 
530  //- Copy assign from IndirectList
531  template<class Addr>
532  inline void operator=(const IndirectListBase<Type, Addr>& rhs);
533 
534  //- Move assignment
535  inline void operator=(Field<Type>&& rhs);
536  inline void operator=(List<Type>&& rhs);
537 
538  template<int SizeMin>
539  inline void operator=(DynamicList<Type, SizeMin>&& rhs);
540 
541  //- Assign entries to the given value
542  inline void operator=(const Type& val);
543 
544  //- Assign entries to zero
545  inline void operator=(Foam::zero);
546 
547  template<class Form, class Cmpt, direction nCmpt>
549 
550 
551  // Member Operators
552 
553  void operator+=(const UList<Type>&);
554  void operator+=(const tmp<Field<Type>>&);
555 
556  void operator-=(const UList<Type>&);
557  void operator-=(const tmp<Field<Type>>&);
558 
559  void operator*=(const UList<scalar>&);
560  void operator*=(const tmp<Field<scalar>>&);
561 
562  void operator/=(const UList<scalar>&);
563  void operator/=(const tmp<Field<scalar>>&);
564 
565  void operator+=(const Type&);
566  void operator-=(const Type&);
567 
568  void operator*=(const scalar&);
569  void operator/=(const scalar&);
570 
571 
572  // IOstream Operators
573 
574  friend Ostream& operator<< <Type>
575  (Ostream&, const Field<Type>&);
576 
577  friend Ostream& operator<< <Type>
578  (Ostream&, const tmp<Field<Type>>&);
579 };
580 
581 
582 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
583 
584 } // End namespace Foam
585 
586 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
587 
588 #include "FieldI.H"
589 #include "FieldFunctions.H"
590 
591 #ifdef NoRepository
592  #include "Field.C"
593 #endif
594 
595 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
596 
597 #endif
598 
599 // ************************************************************************* //
static tmp< Field< Type > > NewCalculatedType(const Field< Type2 > &f)
Return a pointer to a new calculatedFvPatchFieldField created on freestore without setting patchField...
Definition: Field.H:425
static autoPtr< Field< Type > > New(Istream &is)
Return a pointer to a new Field created on freestore.
Definition: Field.H:415
dictionary dict
uint8_t direction
Definition: direction.H:46
void operator-=(const UList< Type > &)
Definition: Field.C:834
Reference counter for various OpenFOAM components.
Definition: refCount.H:44
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
void negate()
Inplace negate this field (negative).
Definition: Field.C:592
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
void clamp_min(const Type &lower)
Impose lower (floor) clamp on the field values (in-place)
Definition: Field.C:654
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:52
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
pTraits< Type >::cmptType cmptType
Component type.
Definition: Field.H:172
A min/max value pair with additional methods. In addition to conveniently storing values...
Definition: HashSet.H:72
static const Field< Type > & null() noexcept
Return a null Field (reference to a nullObject). Behaves like an empty Field.
Definition: Field.H:186
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:61
Templated vector space.
Definition: VectorSpace.H:52
Various functors for unary and binary operations. Can be used for parallel combine-reduce operations ...
void clamp_range(const Type &lower, const Type &upper)
Clamp field values (in-place) to the specified range.
Definition: Field.C:712
void writeEntry(const word &keyword, Ostream &os) const
Write the field as a dictionary entry.
Definition: Field.C:754
static int localBoundaryConsistency_
Local boundary field consistency checks. Uses opt-switch "localBoundaryConsistency".
Definition: Field.H:106
SubField is a Field obtained as a section of another Field, without its own allocation. SubField is derived from a SubList rather than a List.
Definition: Field.H:64
void replace(const direction, const UList< cmptType > &)
Replace a component field of the field.
Definition: Field.C:619
List< labelList > labelListList
List of labelList.
Definition: labelList.H:38
Base for lists with indirect addressing, templated on the list contents type and the addressing type...
scalar range
UList< label > labelUList
A UList of labels.
Definition: UList.H:76
static const char *const typeName
Typename for Field.
Definition: Field.H:87
dimensionedScalar pos(const dimensionedScalar &ds)
static void warnLocalBoundaryConsistencyCompat(const dictionary &)
Warn about keyword changes for local boundary consistency checks.
Definition: FieldBase.C:77
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
Abstract base class to hold the Field mapping addressing and weights.
Definition: FieldMapper.H:43
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
Generic templated field type.
Definition: Field.H:63
List< scalarList > scalarListList
List of scalarList.
Definition: scalarList.H:35
A class for handling words, derived from Foam::string.
Definition: word.H:63
tmp< Field< Type > > clone() const
Clone.
Definition: FieldI.H:140
VSForm block(const label start) const
Definition: Field.C:733
auto key(const Type &t) -> std::enable_if_t< std::is_enum_v< Type >, std::underlying_type_t< Type > >
Definition: foamGltfBase.H:103
SubField< Type > slice(const label pos, label len=-1)
Return SubField slice (non-const access) - no range checking.
Definition: SubField.H:230
constexpr FieldBase() noexcept
Default construct.
Definition: Field.H:150
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
static scalar localBoundaryTolerance_
Tolerance for local boundary field consistency checks. Uses opt-switch "localBoundaryConsistency::tol...
Definition: Field.H:112
Template invariant parts for Field and SubField.
Definition: Field.H:76
void map(const UList< Type > &mapF, const labelUList &mapAddressing)
1 to 1 map from the given field
Definition: Field.C:302
tmp< Field< cmptType > > component(const direction) const
Return a component field of the field.
Definition: Field.C:607
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)
void operator=(const Field< Type > &)
Copy assignment.
Definition: Field.C:781
labelList f(nPoints)
string upper(const std::string &s)
Return string copy transformed with std::toupper on each character.
void assign(const entry &e, const label len)
Assign from a primitive dictionary entry with the following behaviour:
Definition: Field.C:206
void autoMap(const FieldMapper &map, const bool applyFlip=true)
Map from self.
Definition: Field.C:465
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))
void rmap(const UList< Type > &mapF, const labelUList &mapAddressing)
1 to 1 reverse-map from the given field
Definition: Field.C:528
Direction is an 8-bit unsigned integer type used to represent Cartesian directions, components etc.
static bool unifiedGeometricField
GeometricField with extra capacity for flattened boundary fields. Uses opt-switch "unifiedGeometricFi...
Definition: Field.H:100
void clamp_max(const Type &upper)
Impose upper (ceiling) clamp on the field values (in-place)
Definition: Field.C:666
A simple container of IOobject preferences. Can also be used for general handling of read/no-read/rea...
string lower(const std::string &s)
Return string copy transformed with std::tolower on each character.
tmp< Field< Type > > T() const
Return the field transpose (only defined for second rank tensors)
Definition: Field.C:745
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
SubField< Type > subField
Declare type of subField.
Definition: Field.H:177
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
void operator+=(const UList< Type > &)
Definition: Field.C:833
constexpr Field() noexcept
Default construct.
Definition: FieldI.H:24
A class for managing temporary objects.
Definition: HashPtrTable.H:50
void normalise()
Inplace normalise this field. Generally a no-op except for vector fields.
Definition: Field.C:600
static bool allowConstructFromLargerSize
Permit read construct from a larger size.
Definition: Field.H:94
static int localBoundaryConsistency() noexcept
Get flag for local boundary consistency checks.
Definition: Field.H:127
A non-counting (dummy) refCount.
Definition: refCount.H:55
void operator/=(const UList< scalar > &)
Definition: Field.C:836
void operator*=(const UList< scalar > &)
Definition: Field.C:835
Namespace for OpenFOAM.
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:63
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:56
readOption
Enumeration defining read preferences.