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 a null Field (reference to a nullObject).
126  //- Behaves like an empty Field.
127  static const Field<Type>& null() noexcept
128  {
129  return NullObjectRef<Field<Type>>();
130  }
131 
132 
133  // Constructors
134 
135  //- Default construct
136  // For temporary fields that are initialised after construction
137  inline constexpr Field() noexcept;
138 
139  //- Construct given size
140  // For temporary fields that are initialised after construction
141  inline explicit Field(const label len);
142 
143  //- Construct given size and initial value
144  inline Field(const label len, const Type& val);
145 
146  //- Construct given size and initial values of zero
147  inline Field(const label len, const Foam::zero);
148 
149  //- Construct with length=1, copying the value as the only content
150  inline Field(const Foam::one, const Type& val);
151 
152  //- Construct with length=1, moving the value as the only content
153  inline Field(const Foam::one, Type&& val);
154 
155  //- Construct with length=1, initializing content to zero
156  inline Field(const Foam::one, const Foam::zero);
157 
158  //- Copy construct
159  inline Field(const Field<Type>& fld);
160 
161  //- Copy construct from UList<Type>
162  inline explicit Field(const UList<Type>& list);
163 
164  //- Copy construct from IndirectList
165  template<class Addr>
166  inline explicit Field(const IndirectListBase<Type, Addr>& list);
167 
168  //- Move construct from Field
169  inline Field(Field<Type>&& fld) noexcept;
170 
171  //- Move construct from List
172  inline Field(List<Type>&& list) noexcept;
173 
174  //- Move construct from DynamicList
175  template<int SizeMin>
176  inline Field(DynamicList<Type, SizeMin>&& list);
177 
178  //- Construct by 1 to 1 mapping from the given field
179  Field
180  (
181  const UList<Type>& mapF,
182  const labelUList& mapAddressing
183  );
184 
185  //- Construct by 1 to 1 mapping from the given tmp field
186  Field
187  (
188  const tmp<Field<Type>>& tmapF,
189  const labelUList& mapAddressing
190  );
191 
192  //- Construct by interpolative mapping from the given field
193  Field
194  (
195  const UList<Type>& mapF,
196  const labelListList& mapAddressing,
197  const scalarListList& weights
198  );
199 
200  //- Construct by interpolative mapping from the given tmp field
201  Field
202  (
203  const tmp<Field<Type>>& tmapF,
204  const labelListList& mapAddressing,
205  const scalarListList& weights
206  );
207 
208  //- Construct by mapping from the given field
209  Field
210  (
211  const UList<Type>& mapF,
212  const FieldMapper& map,
213  const bool applyFlip = true
214  );
215 
216  //- Construct by mapping from the given field
217  Field
218  (
219  const UList<Type>& mapF,
220  const FieldMapper& map,
221  const Type& defaultValue,
222  const bool applyFlip = true
223  );
224 
225  //- Construct by mapping from the given field
226  Field
227  (
228  const UList<Type>& mapF,
229  const FieldMapper& map,
230  const UList<Type>& defaultValues,
231  const bool applyFlip = true
232  );
233 
234  //- Construct by mapping from the given tmp field
235  Field
236  (
237  const tmp<Field<Type>>& tmapF,
238  const FieldMapper& map,
239  const bool applyFlip = true
240  );
241 
242  //- Construct by mapping from the given tmp field.
243  //- Uses supplied uniform value for unmapped items
244  Field
245  (
246  const tmp<Field<Type>>& tmapF,
247  const FieldMapper& map,
248  const Type& defaultValue,
249  const bool applyFlip = true
250  );
251 
252  //- Construct by mapping from the given tmp field.
253  //- Uses supplied values for unmapped items
254  Field
255  (
256  const tmp<Field<Type>>& tmapF,
257  const FieldMapper& map,
258  const UList<Type>& defaultValues,
259  const bool applyFlip = true
260  );
261 
262  //- Copy construct or re-use as specified.
263  inline Field(Field<Type>& fld, bool reuse);
264 
265  //- Copy or move construct from tmp
266  inline Field(const tmp<Field<Type>>& tfld);
267 
268  //- Construct from Istream
269  inline Field(Istream& is);
270 
271  //- Construct from a dictionary (primitive) entry.
272  Field(const entry& e, const label len);
273 
274  //- Lookup of a primitive dictionary entry by (literal) name
275  //- and assign its contents to this. The behaviour largely as
276  //- described in assign():
277  // - For MUST_READ and key not found: FatalIOError.
278  // - For LAZY_READ and key not found: initialise field with Zero.
279  // - For NO_READ and key not found: simply size the field.
280  // .
281  Field
282  (
283  const word& key,
284  const dictionary& dict,
285  const label len,
286  IOobjectOption::readOption readOpt = IOobjectOption::MUST_READ
287  );
288 
289  //- Clone
290  inline tmp<Field<Type>> clone() const;
291 
292  //- Return a pointer to a new Field created on freestore
293  static autoPtr<Field<Type>> New(Istream& is)
294  {
295  return autoPtr<Field<Type>>(new Field<Type>(is));
296  }
297 
298  //- Return a pointer to a new calculatedFvPatchFieldField created on
299  //- freestore without setting patchField values
300  template<class Type2>
302  {
303  return tmp<Field<Type>>::New(f.size());
304  }
305 
306 
307  // Member Functions
308 
309  //- Assign from a primitive dictionary entry with the following
310  //- behaviour:
311  // - If len == 0 : a no-op
312  // - If len < 0 : no size checking
313  // - If len > 0 : resize \em uniform entries to this size
314  // before assigning. Use as length check for \em nonuniform
315  // entries. A mismatch is possibly Fatal, except when
316  // allowConstructFromLargerSize is true (eg, for column mesh).
317  // .
318  void assign(const entry& e, const label len);
319 
320  //- Lookup a primitive dictionary entry by (literal) name
321  //- and assign its contents to this (behaviour as described above).
322  // - If len == 0 : a no-op and return True.
323  // - For NO_READ : a no-op and return False.
324  // - For LAZY_READ and key not found : a no-op and return False.
325  // - For MUST_READ and key not found : FatalIOError
326  // .
327  // \returns True if an assignment occurred or for zero-length.
328  bool assign
329  (
330  const word& key,
331  const dictionary& dict,
332  const label len,
334  );
335 
336  //- 1 to 1 map from the given field
337  void map
338  (
339  const UList<Type>& mapF,
340  const labelUList& mapAddressing
341  );
342 
343  //- 1 to 1 map from the given tmp field
344  void map
345  (
346  const tmp<Field<Type>>& tmapF,
347  const labelUList& mapAddressing
348  );
349 
350  //- Interpolative map from the given field
351  void map
352  (
353  const UList<Type>& mapF,
354  const labelListList& mapAddressing,
355  const scalarListList& weights
356  );
357 
358  //- Interpolative map from the given tmp field
359  void map
360  (
361  const tmp<Field<Type>>& tmapF,
362  const labelListList& mapAddressing,
363  const scalarListList& weights
364  );
365 
366  //- Map from the given field
367  void map
368  (
369  const UList<Type>& mapF,
370  const FieldMapper& map,
371  const bool applyFlip = true
372  );
373 
374  //- Map from the given tmp field
375  void map
376  (
377  const tmp<Field<Type>>& tmapF,
378  const FieldMapper& map,
379  const bool applyFlip = true
380  );
381 
382  //- Map from self
383  void autoMap
384  (
385  const FieldMapper& map,
386  const bool applyFlip = true
387  );
388 
389  //- 1 to 1 reverse-map from the given field
390  void rmap
391  (
392  const UList<Type>& mapF,
393  const labelUList& mapAddressing
394  );
395 
396  //- 1 to 1 reverse-map from the given tmp field
397  void rmap
398  (
399  const tmp<Field<Type>>& tmapF,
400  const labelUList& mapAddressing
401  );
402 
403  //- Interpolative reverse map from the given field
404  void rmap
405  (
406  const UList<Type>& mapF,
407  const labelUList& mapAddressing,
408  const UList<scalar>& weights
409  );
410 
411  //- Interpolative reverse map from the given tmp field
412  void rmap
413  (
414  const tmp<Field<Type>>& tmapF,
415  const labelUList& mapAddressing,
416  const UList<scalar>& weights
417  );
418 
419  //- Inplace negate this field (negative).
420  // Inverts the state for a bool field.
421  void negate();
422 
423  //- Inplace normalise this field.
424  //- Generally a no-op except for vector fields.
425  // Vector fields are normalised by their magnitude.
426  // Small vectors (mag less than ROOTVSMALL) are set to zero.
427  void normalise();
428 
429  //- Return a component field of the field
431 
432  //- Replace a component field of the field
433  void replace(const direction, const UList<cmptType>&);
434 
435  //- Replace a component field of the field
436  void replace(const direction, const tmp<Field<cmptType>>&);
437 
438  //- Replace a component field of the field
439  void replace(const direction, const cmptType&);
440 
441  //- Impose lower (floor) clamp on the field values (in-place)
442  void clamp_min(const Type& lower);
443 
444  //- Impose upper (ceiling) clamp on the field values (in-place)
445  void clamp_max(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 Type& lower, const Type& upper);
450 
451  //- Clamp field values (in-place) to the specified range.
452  // Does not check if range is valid or not.
453  void clamp_range(const MinMax<Type>& range);
454 
455  template<class VSForm>
456  VSForm block(const label start) const;
457 
458  //- Return the field transpose (only defined for second rank tensors)
459  tmp<Field<Type>> T() const;
460 
461  //- Write the field as a dictionary entry
462  void writeEntry(const word& keyword, Ostream& os) const;
463 
464 
465  // Other Access
466 
467  //- Return SubField slice (non-const access) - no range checking
468  SubField<Type> slice(const label pos, label len = -1);
469 
470  //- Return SubField slice (const access) - no range checking
471  const SubField<Type> slice(const label pos, label len = -1) const;
472 
473  //- Return SubField slice (non-const access) - with range checking
475 
476  //- Return SubField slice (const access) - with range checking
477  const SubField<Type> slice(const labelRange& range) const;
478 
479 
480  // Assignment
481 
482  //- Copy assignment
483  void operator=(const Field<Type>&);
484  void operator=(const tmp<Field<Type>>&);
485 
486  inline void operator=(const UList<Type>& rhs);
487  inline void operator=(const SubField<Type>& rhs);
488 
489  //- Copy assign from IndirectList
490  template<class Addr>
491  inline void operator=(const IndirectListBase<Type, Addr>& rhs);
492 
493  //- Move assignment
494  inline void operator=(Field<Type>&& rhs);
495  inline void operator=(List<Type>&& rhs);
496 
497  template<int SizeMin>
498  inline void operator=(DynamicList<Type, SizeMin>&& rhs);
499 
500  //- Assign entries to the given value
501  inline void operator=(const Type& val);
502 
503  //- Assign entries to zero
504  inline void operator=(const Foam::zero);
505 
506  template<class Form, class Cmpt, direction nCmpt>
508 
509 
510  // Member Operators
511 
512  void operator+=(const UList<Type>&);
513  void operator+=(const tmp<Field<Type>>&);
514 
515  void operator-=(const UList<Type>&);
516  void operator-=(const tmp<Field<Type>>&);
517 
518  void operator*=(const UList<scalar>&);
519  void operator*=(const tmp<Field<scalar>>&);
520 
521  void operator/=(const UList<scalar>&);
522  void operator/=(const tmp<Field<scalar>>&);
523 
524  void operator+=(const Type&);
525  void operator-=(const Type&);
526 
527  void operator*=(const scalar&);
528  void operator/=(const scalar&);
529 
530 
531  // IOstream Operators
532 
533  friend Ostream& operator<< <Type>
534  (Ostream&, const Field<Type>&);
535 
536  friend Ostream& operator<< <Type>
537  (Ostream&, const tmp<Field<Type>>&);
538 };
539 
540 
541 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
542 
543 } // End namespace Foam
544 
545 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
546 
547 #include "FieldI.H"
548 #include "FieldFunctions.H"
549 
550 #ifdef NoRepository
551  #include "Field.C"
552 #endif
553 
554 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
555 
556 #endif
557 
558 // ************************************************************************* //
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:376
static autoPtr< Field< Type > > New(Istream &is)
Return a pointer to a new Field created on freestore.
Definition: Field.H:366
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
static const Field< Type > & null() noexcept
Return a null Field (reference to a nullObject). Behaves like an empty Field.
Definition: Field.H:137
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:142
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:223
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
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: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: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.