SubField.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) 2018-2021 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::SubField
29 
30 Description
31  SubField is a Field obtained as a section of another Field,
32  without its own allocation.
33  SubField is derived from a SubList rather than a List.
34 
35 SourceFiles
36  SubFieldI.H
37 
38 \*---------------------------------------------------------------------------*/
39 
40 #ifndef Foam_SubField_H
41 #define Foam_SubField_H
42 
43 #include "Field.H"
44 #include "SubList.H"
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 
51 // Forward Declarations
52 template<class Type> class SubField;
53 
54 /*---------------------------------------------------------------------------*\
55  Class SubField Declaration
56 \*---------------------------------------------------------------------------*/
57 
58 template<class Type>
59 class SubField
60 :
61  public FieldBase,
62  public SubList<Type>
63 {
64 public:
65 
66  //- Component type
67  typedef typename pTraits<Type>::cmptType cmptType;
68 
69 
70  // Static Member Functions
71 
72  //- Return a null SubField (reference to a nullObject).
73  //- Behaves like an empty SubField.
74  static const SubField<Type>& null() noexcept
75  {
76  return NullObjectRef<SubField<Type>>();
77  }
78 
79 
80  // Constructors
81 
82  //- Default construct, zero-sized and nullptr
83  SubField() noexcept = default;
84 
85  //- Copy construct (shallow copy)
86  inline SubField(const SubField<Type>& fld);
87 
88  //- Copy construct from SubList
89  inline SubField(const SubList<Type>& list);
90 
91  //- Construct from UList, the entire size
92  inline explicit SubField(const UList<Type>& list) noexcept;
93 
94  //- Construct from UList with a given sub-list size, start at 0
95  inline SubField
96  (
97  const UList<Type>& list,
98  const label len
99  );
100 
101  //- Construct from UList, sub-list size and start index
102  inline SubField
103  (
104  const UList<Type>& list,
105  const label len,
106  const label start
107  );
108 
109  //- Construct from UList and a (start,size) range.
110  // The range is subsetted with the list size itself to ensure that the
111  // result always addresses a valid section of the list.
112  inline SubField
113  (
114  const UList<Type>& list,
115  const labelRange& range
116  );
117 
118  //- Construct from UList and a (start,size) range,
119  //- but bypassing run-time range checking.
120  inline SubField
121  (
122  const labelRange& range,
123  const UList<Type>& list
124  );
125 
126 
127  // Member Functions
128 
129  //- Return a component field of the field
130  inline tmp<Field<cmptType>> component(const direction) const;
131 
132  //- Return the field transpose (only defined for second rank tensors)
133  tmp<Field<Type>> T() const;
134 
135 
136  // Member Operators
137 
138  //- Allow cast to a const Field<Type>&
139  inline operator const Foam::Field<Type>&() const;
140 
141  //- Copy assign via UList operator. Takes linear time.
142  inline void operator=(const SubField<Type>&);
143 
144  //- Copy assign via UList operator. Takes linear time.
145  inline void operator=(const Field<Type>&);
146 
147  //- Assign all entries to the given value
148  inline void operator=(const Type& val);
149 
150  //- Assign all entries to zero
151  inline void operator=(const Foam::zero);
152 
153  //- Copy assign via UList operator. Takes linear time.
154  template<class Form, direction Ncmpts>
155  inline void operator=(const VectorSpace<Form, Type, Ncmpts>& rhs);
156 
157  //- Add value to each entry
158  inline void operator+=(const Type& val);
159 
160  //- Subtract value from each entry
161  inline void operator-=(const Type& val);
162 
163  //- Multiply each entry by value
164  inline void operator*=(const scalar& s);
165 
166  //- Divide each entry by value
167  inline void operator/=(const scalar& s);
168 };
169 
170 
171 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
172 
173 } // End namespace Foam
174 
175 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
176 
177 #include "SubFieldI.H"
178 
179 // * * * * * * * * * * * * * * * Implementations * * * * * * * * * * * * * * //
180 
181 template<class Type>
183 Foam::Field<Type>::slice(const label pos, label len)
184 {
185  if (len < 0)
186  {
187  len = (this->size() - pos);
188  }
189  return SubField<Type>(*this, len, pos);
190 }
191 
192 
193 template<class Type>
195 Foam::Field<Type>::slice(const label pos, label len) const
196 {
197  if (len < 0)
198  {
199  len = (this->size() - pos);
200  }
201  return SubField<Type>(*this, len, pos);
202 }
203 
204 
205 template<class Type>
207 Foam::Field<Type>::slice(const labelRange& range)
208 {
209  return SubField<Type>(*this, range); // with range checking
210 }
211 
212 
213 template<class Type>
215 Foam::Field<Type>::slice(const labelRange& range) const
216 {
217  return SubField<Type>(*this, range); // with range checking
218 }
219 
220 
221 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
222 
223 #endif
224 
225 // ************************************************************************* //
tmp< Field< cmptType > > component(const direction) const
Return a component field of the field.
Definition: SubFieldI.H:104
uint8_t direction
Definition: direction.H:46
tmp< Field< Type > > T() const
Return the field transpose (only defined for second rank tensors)
Definition: SubFieldI.H:113
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:52
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:75
Templated vector space.
Definition: VectorSpace.H:52
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
static const SubField< Type > & null() noexcept
Return a null SubField (reference to a nullObject). Behaves like an empty SubField.
Definition: SubField.H:73
scalar range
SubField() noexcept=default
Default construct, zero-sized and nullptr.
dimensionedScalar pos(const dimensionedScalar &ds)
A List obtained as a section of another List.
Definition: SubList.H:50
Generic templated field type.
Definition: Field.H:62
pTraits< Type >::cmptType cmptType
Component type.
Definition: SubField.H:64
SubField< Type > slice(const label pos, label len=-1)
Return SubField slice (non-const access) - no range checking.
Definition: SubField.H:223
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
const direction noexcept
Definition: Scalar.H:258
label size() const noexcept
The number of elements in the container.
Definition: UList.H:680
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))
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
A class for managing temporary objects.
Definition: HashPtrTable.H:50
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;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Namespace for OpenFOAM.