VectorSpace.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) 2019-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::VectorSpace
29 
30 Description
31  Templated vector space.
32 
33  Template arguments are the Form the vector space will be used to create,
34  the type of the elements and the number of elements.
35 
36 SourceFiles
37  VectorSpaceI.H
38  VectorSpace.C
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef Foam_VectorSpace_H
43 #define Foam_VectorSpace_H
44 
45 #include "direction.H"
46 #include "scalar.H"
47 #include "word.H"
48 #include "zero.H"
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 
55 // Forward Declarations
56 
57 template<class Form, class Cmpt, direction Ncmpts> class VectorSpace;
58 
59 template<class Form, class Cmpt, direction Ncmpts>
60 Istream& operator>>
61 (
62  Istream& is,
64 );
65 
66 template<class Form, class Cmpt, direction Ncmpts>
67 Ostream& operator<<
68 (
69  Ostream& os,
71 );
72 
73 
74 /*---------------------------------------------------------------------------*\
75  Class VectorSpace Declaration
76 \*---------------------------------------------------------------------------*/
77 
78 template<class Form, class Cmpt, direction Ncmpts>
79 class VectorSpace
80 {
81 public:
82 
83  //- The components of this vector space
84  Cmpt v_[Ncmpts];
85 
86  //- VectorSpace type
88 
89  //- Component type
90  typedef Cmpt cmptType;
91 
92  //- Magnitude type
93  typedef Cmpt magType;
94 
95  //- The type to represent the size of a VectorSpace
97 
98 
99  // Static Constants
100 
101  //- Dimensionality of space
102  static constexpr direction dim = 3;
103 
104  //- Number of components in this vector space
105  static constexpr direction nComponents = Ncmpts;
106 
107 
108  // VectorSpace currently defaults to a column-vector
109  // This will be removed when column-vector is introduced
110  // as a specialization
111  static constexpr direction mRows = Ncmpts;
112  static constexpr direction nCols = 1;
113 
115  // Static Data Members
116 
117  static const char* const typeName;
118  static const char* const componentNames[];
119  static const Form zero;
120  static const Form one;
121  static const Form max;
122  static const Form min;
123  static const Form rootMax;
124  static const Form rootMin;
125 
127  // Sub-Block Classes
129  //- Const sub-block type
130  template<class SubVector, direction BStart>
131  class ConstBlock
132  {
133  const vsType& vs_;
134 
135  public:
136 
137  //- Number of components in this vector space
139 
140  //- Construct for a given vector
141  inline ConstBlock(const vsType& vs);
143  //- [i] const element access operator
144  inline const Cmpt& operator[](const direction i) const;
145 
146  //- (i, 0) const element access operator
147  inline const Cmpt& operator()
148  (
149  const direction i,
150  const direction
151  ) const;
152  };
153 
154 
155  // Generated Methods
156 
157  //- Default construct
158  VectorSpace() = default;
159 
160 
161  // Constructors
162 
163  //- Construct initialized to zero
164  inline VectorSpace(Foam::zero);
165 
166  //- Copy construct
168 
169  //- Copy construct of a VectorSpace with the same size
170  template<class Form2, class Cmpt2>
171  inline explicit VectorSpace(const VectorSpace<Form2, Cmpt2, Ncmpts>&);
172 
173  //- Construct from Istream
174  explicit VectorSpace(Istream& is);
175 
176 
177  // Member Functions
178 
179  //- The number of elements in the VectorSpace = Ncmpts.
180  static constexpr direction size() noexcept { return Ncmpts; }
181 
182  inline const Cmpt& component(const direction) const;
183  inline Cmpt& component(const direction);
184 
185  inline void component(Cmpt&, const direction) const;
186  inline void replace(const direction, const Cmpt&);
187 
188  //- Return const pointer to the first data element
189  const Cmpt* cdata() const noexcept { return v_; }
190 
191  //- Return pointer to the first data element
192  Cmpt* data() noexcept { return v_; }
193 
194  //- Assign all components to given value
195  inline void fill(const Cmpt& s);
196 
197  //- Return a VectorSpace with all elements = s
198  inline static Form uniform(const Cmpt& s);
199 
200  template<class SubVector, direction BStart>
201  inline const ConstBlock<SubVector, BStart> block() const;
202 
203 
204  // Member Operators
205 
206  inline const Cmpt& operator[](const direction) const;
207  inline Cmpt& operator[](const direction);
208 
209  inline void operator=(const VectorSpace<Form, Cmpt, Ncmpts>&);
210  inline void operator+=(const VectorSpace<Form, Cmpt, Ncmpts>&);
212 
213  inline void operator=(Foam::zero);
214  inline void operator*=(const scalar);
215  inline void operator/=(const scalar);
216 
217 
218  // Iterators
219 
220  //- Random access iterator for traversing VectorSpace
221  typedef Cmpt* iterator;
223  //- Random access iterator for traversing VectorSpace
224  typedef const Cmpt* const_iterator;
225 
226 
227  // Random access iterators (const and non-const)
228 
229  //- Return an iterator (pointer) to begin of VectorSpace
230  iterator begin() noexcept { return v_; }
231 
232  //- Return const_iterator (const pointer) to begin of VectorSpace
233  const_iterator begin() const noexcept { return v_; }
234 
235  //- Return const_iterator (const pointer) to begin of VectorSpace
236  const_iterator cbegin() const noexcept { return v_; }
237 
238  //- Return an iterator (pointer) to end of VectorSpace
239  iterator end() noexcept { return (v_ + Ncmpts); }
240 
241  //- Return const_iterator (const pointer) to end of VectorSpace
242  const_iterator end() const noexcept { return (v_ + Ncmpts); }
243 
244  //- Return const_iterator (const pointer) to end of VectorSpace
245  const_iterator cend() const noexcept { return (v_ + Ncmpts); }
246 
247 
248  // IOstream Operators
249 
250  friend Istream& operator>> <Form, Cmpt, Ncmpts>
251  (
252  Istream&,
253  VectorSpace<Form, Cmpt, Ncmpts>&
254  );
255 
256  friend Ostream& operator<< <Form, Cmpt, Ncmpts>
257  (
258  Ostream&,
259  const VectorSpace<Form, Cmpt, Ncmpts>&
260  );
261 };
263 
264 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
265 
266 //- A word representation of a VectorSpace
267 template<class Form, class Cmpt, direction Ncmpts>
269 
270 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
271 
272 } // End namespace Foam
273 
274 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
276 #include "VectorSpaceI.H"
277 
278 #ifdef NoRepository
279  #include "VectorSpace.C"
280 #endif
281 
282 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
283 
284 #endif
286 // ************************************************************************* //
static const char *const typeName
Definition: VectorSpace.H:126
static constexpr direction nCols
Definition: VectorSpace.H:121
static constexpr direction mRows
Definition: VectorSpace.H:120
uint8_t direction
Definition: direction.H:46
static const Form max
Definition: VectorSpace.H:130
void operator=(const VectorSpace< Form, Cmpt, Ncmpts > &)
Definition: VectorSpaceI.H:276
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
iterator begin() noexcept
Return an iterator (pointer) to begin of VectorSpace.
Definition: VectorSpace.H:275
static const char *const componentNames[]
Definition: VectorSpace.H:127
static const Form rootMin
Definition: VectorSpace.H:133
Cmpt magType
Magnitude type.
Definition: VectorSpace.H:96
Templated vector space.
Definition: VectorSpace.H:52
const_iterator cend() const noexcept
Return const_iterator (const pointer) to end of VectorSpace.
Definition: VectorSpace.H:300
ConstBlock(const vsType &vs)
Construct for a given vector.
Definition: VectorSpaceI.H:62
::Foam::direction nComponents(const expressions::valueTypeCode) noexcept
The number of components associated with given valueTypeCode.
Definition: exprTraits.C:40
void replace(const direction, const Cmpt &)
Definition: VectorSpaceI.H:138
static const Form min
Definition: VectorSpace.H:131
void operator-=(const VectorSpace< Form, Cmpt, Ncmpts > &)
Definition: VectorSpaceI.H:296
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
const Cmpt * const_iterator
Random access iterator for traversing VectorSpace.
Definition: VectorSpace.H:267
const_iterator cbegin() const noexcept
Return const_iterator (const pointer) to begin of VectorSpace.
Definition: VectorSpace.H:285
A class for handling words, derived from Foam::string.
Definition: word.H:63
void operator+=(const VectorSpace< Form, Cmpt, Ncmpts > &)
Definition: VectorSpaceI.H:286
VectorSpace()=default
Default construct.
Cmpt * data() noexcept
Return pointer to the first data element.
Definition: VectorSpace.H:227
direction size_type
The type to represent the size of a VectorSpace.
Definition: VectorSpace.H:101
static constexpr direction dim
Dimensionality of space.
Definition: VectorSpace.H:109
const ConstBlock< SubVector, BStart > block() const
static constexpr direction nComponents
Number of components in this vector space.
Definition: VectorSpace.H:114
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
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
OBJstream os(runTime.globalPath()/outputName)
friend Ostream & operator(Ostream &, const VectorSpace< Form, Cmpt, Ncmpts > &)
static const Form rootMax
Definition: VectorSpace.H:132
Direction is an 8-bit unsigned integer type used to represent Cartesian directions, components etc.
Cmpt cmptType
Component type.
Definition: VectorSpace.H:91
void fill(const Cmpt &s)
Assign all components to given value.
Definition: VectorSpaceI.H:157
static constexpr direction size() noexcept
The number of elements in the VectorSpace = Ncmpts.
Definition: VectorSpace.H:211
void operator*=(const scalar)
Definition: VectorSpaceI.H:313
iterator end() noexcept
Return an iterator (pointer) to end of VectorSpace.
Definition: VectorSpace.H:290
const Cmpt * cdata() const noexcept
Return const pointer to the first data element.
Definition: VectorSpace.H:222
const Cmpt & component(const direction) const
Definition: VectorSpaceI.H:80
static const Form one
Definition: VectorSpace.H:129
VectorSpace< Form, Cmpt, Ncmpts > vsType
VectorSpace type.
Definition: VectorSpace.H:86
static Form uniform(const Cmpt &s)
Return a VectorSpace with all elements = s.
Definition: VectorSpaceI.H:164
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
void operator/=(const scalar)
Definition: VectorSpaceI.H:323
Cmpt * iterator
Random access iterator for traversing VectorSpace.
Definition: VectorSpace.H:262
const Cmpt & operator[](const direction) const
Definition: VectorSpaceI.H:186
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))
static const Form zero
Definition: VectorSpace.H:128
Cmpt v_[Ncmpts]
The components of this vector space.
Definition: VectorSpace.H:81
Namespace for OpenFOAM.
static const direction nComponents
Number of components in this vector space.
Definition: VectorSpace.H:151
const Cmpt & operator[](const direction i) const
[i] const element access operator
Definition: VectorSpaceI.H:227