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-2023 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 "scalarList.H"
54 #include "VectorSpace.H"
55 #include "IOobjectOption.H"
56 
57 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
58 
59 namespace Foam
60 {
61 
62 // Forward Declarations
64 class dictionary;
65 class entry;
66 
67 template<class Type> class Field;
68 template<class Type> class SubField;
69 
70 template<class Type> Ostream& operator<<(Ostream&, const Field<Type>&);
71 template<class Type> Ostream& operator<<(Ostream&, const tmp<Field<Type>>&);
72 
73 /*---------------------------------------------------------------------------*\
74  Class FieldBase Declaration
75 \*---------------------------------------------------------------------------*/
76 
77 //- Template invariant parts for Field and SubField
78 class FieldBase
79 :
80  public refCount
81 {
82 public:
83 
84  // Static Data Members
85 
86  //- Typename for Field
87  static const char* const typeName;
88 
89  //- Permit read construct from a larger size.
90  // Mostly required for things like column mesh, for example.
91  static bool allowConstructFromLargerSize;
92 
93 
94  // Constructors
95 
96  //- Default construct
97  constexpr FieldBase() noexcept
98  :
99  refCount()
100  {}
101 };
102 
103 
104 /*---------------------------------------------------------------------------*\
105  Class Field Declaration
106 \*---------------------------------------------------------------------------*/
107 
108 template<class Type>
109 class Field
110 :
111  public FieldBase,
112  public List<Type>
113 {
114 public:
115 
116  //- Component type
117  typedef typename pTraits<Type>::cmptType cmptType;
118 
119  //- Declare type of subField
120  typedef SubField<Type> subField;
121 
122 
123  // Static Member Functions
124 
125  //- Return nullObject reference Field
126  inline static const Field<Type>& null();
127 
129  // Constructors
130 
131  //- Default construct
132  // For temporary fields that are initialised after construction
133  inline constexpr Field() noexcept;
134 
135  //- Construct given size
136  // For temporary fields that are initialised after construction
137  inline explicit Field(const label len);
138 
139  //- Construct given size and initial value
140  inline Field(const label len, const Type& val);
141 
142  //- Construct given size and initial values of zero
143  inline Field(const label len, const Foam::zero);
144 
145  //- Construct with length=1, copying the value as the only content
146  inline Field(const Foam::one, const Type& val);
147 
148  //- Construct with length=1, moving the value as the only content
149  inline Field(const Foam::one, Type&& val);
150 
151  //- Construct with length=1, initializing content to zero
152  inline Field(const Foam::one, const Foam::zero);
153 
154  //- Copy construct
155  inline Field(const Field<Type>& fld);
156 
157  //- Copy construct from UList<Type>
158  inline explicit Field(const UList<Type>& list);
159 
160  //- Copy construct from IndirectList
161  template<class Addr>
162  inline explicit Field(const IndirectListBase<Type, Addr>& list);
163 
164  //- Move construct from Field
165  inline Field(Field<Type>&& fld) noexcept;
166 
167  //- Move construct from List
168  inline Field(List<Type>&& list) noexcept;
169 
170  //- Move construct from DynamicList
171  template<int SizeMin>
172  inline Field(DynamicList<Type, SizeMin>&& list);
173 
174  //- Construct by 1 to 1 mapping from the given field
175  Field
176  (
177  const UList<Type>& mapF,
178  const labelUList& mapAddressing
179  );
180 
181  //- Construct by 1 to 1 mapping from the given tmp field
182  Field
183  (
184  const tmp<Field<Type>>& tmapF,
185  const labelUList& mapAddressing
186  );
187 
188  //- Construct by interpolative mapping from the given field
189  Field
190  (
191  const UList<Type>& mapF,
192  const labelListList& mapAddressing,
193  const scalarListList& weights
194  );
195 
196  //- Construct by interpolative mapping from the given tmp field
197  Field
198  (
199  const tmp<Field<Type>>& tmapF,
200  const labelListList& mapAddressing,
201  const scalarListList& weights
202  );
203 
204  //- Construct by mapping from the given field
205  Field
206  (
207  const UList<Type>& mapF,
208  const FieldMapper& map,
209  const bool applyFlip = true
210  );
211 
212  //- Construct by mapping from the given field
213  Field
214  (
215  const UList<Type>& mapF,
216  const FieldMapper& map,
217  const Type& defaultValue,
218  const bool applyFlip = true
219  );
220 
221  //- Construct by mapping from the given field
222  Field
223  (
224  const UList<Type>& mapF,
225  const FieldMapper& map,
226  const UList<Type>& defaultValues,
227  const bool applyFlip = true
228  );
229 
230  //- Construct by mapping from the given tmp field
231  Field
232  (
233  const tmp<Field<Type>>& tmapF,
234  const FieldMapper& map,
235  const bool applyFlip = true
236  );
237 
238  //- Construct by mapping from the given tmp field.
239  //- Uses supplied uniform value for unmapped items
240  Field
241  (
242  const tmp<Field<Type>>& tmapF,
243  const FieldMapper& map,
244  const Type& defaultValue,
245  const bool applyFlip = true
246  );
247 
248  //- Construct by mapping from the given tmp field.
249  //- Uses supplied values for unmapped items
250  Field
251  (
252  const tmp<Field<Type>>& tmapF,
253  const FieldMapper& map,
254  const UList<Type>& defaultValues,
255  const bool applyFlip = true
256  );
257 
258  //- Copy construct or re-use as specified.
259  inline Field(Field<Type>& fld, bool reuse);
260 
261  //- Copy or move construct from tmp
262  inline Field(const tmp<Field<Type>>& tfld);
263 
264  //- Construct from Istream
265  inline Field(Istream& is);
266 
267  //- Construct from a dictionary (primitive) entry.
268  Field(const entry& e, const label len);
269 
270  //- Lookup of a primitive dictionary entry by (literal) name
271  //- and assign its contents to this. The behaviour largely as
272  //- described in assign():
273  // - For MUST_READ and key not found: FatalIOError.
274  // - For LAZY_READ and key not found: initialise field with Zero.
275  // - For NO_READ and key not found: simply size the field.
276  // .
277  Field
278  (
279  const word& key,
280  const dictionary& dict,
281  const label len,
282  IOobjectOption::readOption readOpt = IOobjectOption::MUST_READ
283  );
284 
285  //- Clone
286  inline tmp<Field<Type>> clone() const;
287 
288  //- Return a pointer to a new Field created on freestore
289  static autoPtr<Field<Type>> New(Istream& is)
290  {
291  return autoPtr<Field<Type>>(new Field<Type>(is));
292  }
293 
294  //- Return a pointer to a new calculatedFvPatchFieldField created on
295  //- freestore without setting patchField values
296  template<class Type2>
298  {
299  return tmp<Field<Type>>::New(f.size());
300  }
301 
302 
303  // Member Functions
304 
305  //- Assign from a primitive dictionary entry with the following
306  //- behaviour:
307  // - If len == 0 : a no-op
308  // - If len < 0 : no size checking
309  // - If len > 0 : resize \em uniform entries to this size
310  // before assigning. Use as length check for \em nonuniform
311  // entries. A mismatch is possibly Fatal, except when
312  // allowConstructFromLargerSize is true (eg, for column mesh).
313  // .
314  void assign(const entry& e, const label len);
315 
316  //- Lookup a primitive dictionary entry by (literal) name
317  //- and assign its contents to this (behaviour as described above).
318  // - If len == 0 : a no-op and return True.
319  // - For NO_READ : a no-op and return False.
320  // - For LAZY_READ and key not found : a no-op and return False.
321  // - For MUST_READ and key not found : FatalIOError
322  // .
323  // \returns True if an assignment occurred or for zero-length.
324  bool assign
325  (
326  const word& key,
327  const dictionary& dict,
328  const label len,
330  );
331 
332  //- 1 to 1 map from the given field
333  void map
334  (
335  const UList<Type>& mapF,
336  const labelUList& mapAddressing
337  );
338 
339  //- 1 to 1 map from the given tmp field
340  void map
341  (
342  const tmp<Field<Type>>& tmapF,
343  const labelUList& mapAddressing
344  );
345 
346  //- Interpolative map from the given field
347  void map
348  (
349  const UList<Type>& mapF,
350  const labelListList& mapAddressing,
351  const scalarListList& weights
352  );
353 
354  //- Interpolative map from the given tmp field
355  void map
356  (
357  const tmp<Field<Type>>& tmapF,
358  const labelListList& mapAddressing,
359  const scalarListList& weights
360  );
361 
362  //- Map from the given field
363  void map
364  (
365  const UList<Type>& mapF,
366  const FieldMapper& map,
367  const bool applyFlip = true
368  );
369 
370  //- Map from the given tmp field
371  void map
372  (
373  const tmp<Field<Type>>& tmapF,
374  const FieldMapper& map,
375  const bool applyFlip = true
376  );
377 
378  //- Map from self
379  void autoMap
380  (
381  const FieldMapper& map,
382  const bool applyFlip = true
383  );
384 
385  //- 1 to 1 reverse-map from the given field
386  void rmap
387  (
388  const UList<Type>& mapF,
389  const labelUList& mapAddressing
390  );
391 
392  //- 1 to 1 reverse-map from the given tmp field
393  void rmap
394  (
395  const tmp<Field<Type>>& tmapF,
396  const labelUList& mapAddressing
397  );
398 
399  //- Interpolative reverse map from the given field
400  void rmap
401  (
402  const UList<Type>& mapF,
403  const labelUList& mapAddressing,
404  const UList<scalar>& weights
405  );
406 
407  //- Interpolative reverse map from the given tmp field
408  void rmap
409  (
410  const tmp<Field<Type>>& tmapF,
411  const labelUList& mapAddressing,
412  const UList<scalar>& weights
413  );
414 
415  //- Inplace negate this field (negative).
416  // Inverts the state for a bool field.
417  void negate();
418 
419  //- Inplace normalise this field.
420  //- Generally a no-op except for vector fields.
421  // Vector fields are normalised by their magnitude.
422  // Small vectors (mag less than ROOTVSMALL) are set to zero.
423  void normalise();
424 
425  //- Return a component field of the field
427 
428  //- Replace a component field of the field
429  void replace(const direction, const UList<cmptType>&);
430 
431  //- Replace a component field of the field
432  void replace(const direction, const tmp<Field<cmptType>>&);
433 
434  //- Replace a component field of the field
435  void replace(const direction, const cmptType&);
436 
437  //- Impose lower (floor) clamp on the field values (in-place)
438  void clamp_min(const Type& lower);
439 
440  //- Impose upper (ceiling) clamp on the field values (in-place)
441  void clamp_max(const Type& upper);
442 
443  //- Clamp field values (in-place) to the specified range.
444  // Does not check if range is valid or not.
445  void clamp_range(const Type& lower, const Type& upper);
446 
447  //- Clamp field values (in-place) to the specified range.
448  // Does not check if range is valid or not.
449  void clamp_range(const MinMax<Type>& range);
450 
451  template<class VSForm>
452  VSForm block(const label start) const;
453 
454  //- Return the field transpose (only defined for second rank tensors)
455  tmp<Field<Type>> T() const;
456 
457  //- Write the field as a dictionary entry
458  void writeEntry(const word& keyword, Ostream& os) const;
459 
460 
461  // Other Access
462 
463  //- Return SubField slice (non-const access) - no range checking
464  SubField<Type> slice(const label pos, label len = -1);
465 
466  //- Return SubField slice (const access) - no range checking
467  const SubField<Type> slice(const label pos, label len = -1) const;
468 
469  //- Return SubField slice (non-const access) - with range checking
471 
472  //- Return SubField slice (const access) - with range checking
473  const SubField<Type> slice(const labelRange& range) const;
474 
475 
476  // Assignment
477 
478  //- Copy assignment
479  void operator=(const Field<Type>&);
480  void operator=(const tmp<Field<Type>>&);
481 
482  inline void operator=(const UList<Type>& rhs);
483  inline void operator=(const SubField<Type>& rhs);
484 
485  //- Copy assign from IndirectList
486  template<class Addr>
487  inline void operator=(const IndirectListBase<Type, Addr>& rhs);
488 
489  //- Move assignment
490  inline void operator=(Field<Type>&& rhs);
491  inline void operator=(List<Type>&& rhs);
492 
493  template<int SizeMin>
494  inline void operator=(DynamicList<Type, SizeMin>&& rhs);
495 
496  //- Assign entries to the given value
497  inline void operator=(const Type& val);
498 
499  //- Assign entries to zero
500  inline void operator=(const Foam::zero);
501 
502  template<class Form, class Cmpt, direction nCmpt>
504 
505 
506  // Member Operators
507 
508  void operator+=(const UList<Type>&);
509  void operator+=(const tmp<Field<Type>>&);
510 
511  void operator-=(const UList<Type>&);
512  void operator-=(const tmp<Field<Type>>&);
513 
514  void operator*=(const UList<scalar>&);
515  void operator*=(const tmp<Field<scalar>>&);
516 
517  void operator/=(const UList<scalar>&);
518  void operator/=(const tmp<Field<scalar>>&);
519 
520  void operator+=(const Type&);
521  void operator-=(const Type&);
522 
523  void operator*=(const scalar&);
524  void operator/=(const scalar&);
525 
526 
527  // IOstream Operators
528 
529  friend Ostream& operator<< <Type>
530  (Ostream&, const Field<Type>&);
531 
532  friend Ostream& operator<< <Type>
533  (Ostream&, const tmp<Field<Type>>&);
534 };
535 
536 
537 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
538 
539 } // End namespace Foam
540 
541 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
542 
543 #include "FieldI.H"
544 #include "FieldFunctions.H"
545 
546 #ifdef NoRepository
547  #include "Field.C"
548 #endif
549 
550 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
551 
552 #endif
553 
554 // ************************************************************************* //
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:372
static autoPtr< Field< Type > > New(Istream &is)
Return a pointer to a new Field created on freestore.
Definition: Field.H:362
dictionary dict
uint8_t direction
Definition: direction.H:46
void operator-=(const UList< Type > &)
Definition: Field.C:800
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:593
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:655
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:123
A min/max value pair with additional methods. In addition to conveniently storing values...
Definition: HashSet.H:72
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:75
Templated vector space.
Definition: VectorSpace.H:52
void clamp_range(const Type &lower, const Type &upper)
Clamp field values (in-place) to the specified range.
Definition: Field.C:678
string upper(const std::string &s)
Return string copy transformed with std::toupper on each character.
Definition: stringOps.C:1187
void writeEntry(const word &keyword, Ostream &os) const
Write the field as a dictionary entry.
Definition: Field.C:720
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:63
void replace(const direction, const UList< cmptType > &)
Replace a component field of the field.
Definition: Field.C:620
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:78
static const char *const typeName
Typename for Field.
Definition: Field.H:86
dimensionedScalar pos(const dimensionedScalar &ds)
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:62
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:151
VSForm block(const label start) const
Definition: Field.C:699
SubField< Type > slice(const label pos, label len=-1)
Return SubField slice (non-const access) - no range checking.
Definition: SubField.H:219
constexpr FieldBase() noexcept
Default construct.
Definition: Field.H:101
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
Template invariant parts for Field and SubField.
Definition: Field.H:75
void map(const UList< Type > &mapF, const labelUList &mapAddressing)
1 to 1 map from the given field
Definition: Field.C:303
tmp< Field< cmptType > > component(const direction) const
Return a component field of the field.
Definition: Field.C:608
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
void operator=(const Field< Type > &)
Copy assignment.
Definition: Field.C:747
labelList f(nPoints)
void assign(const entry &e, const label len)
Assign from a primitive dictionary entry with the following behaviour:
Definition: Field.C:207
void autoMap(const FieldMapper &map, const bool applyFlip=true)
Map from self.
Definition: Field.C:466
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:529
Direction is an 8-bit unsigned integer type used to represent Cartesian directions, components etc.
string lower(const std::string &s)
Return string copy transformed with std::tolower on each character.
Definition: stringOps.C:1171
void clamp_max(const Type &upper)
Impose upper (ceiling) clamp on the field values (in-place)
Definition: Field.C:666
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
static const Field< Type > & null()
Return nullObject reference Field.
Definition: FieldI.H:24
A simple container of IOobject preferences. Can also be used for general handling of read/no-read/rea...
tmp< Field< Type > > T() const
Return the field transpose (only defined for second rank tensors)
Definition: Field.C:711
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:128
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:799
constexpr Field() noexcept
Default construct.
Definition: FieldI.H:33
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:601
static bool allowConstructFromLargerSize
Permit read construct from a larger size.
Definition: Field.H:93
A non-counting (dummy) refCount.
Definition: refCount.H:55
void operator/=(const UList< scalar > &)
Definition: Field.C:802
void operator*=(const UList< scalar > &)
Definition: Field.C:801
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.