VectorSpace.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::VectorSpace
26 
27 Description
28  Templated vector space.
29 
30  Template arguments are the Form the vector space will be used to create,
31  the type of the elements and the number of elements.
32 
33 SourceFiles
34  VectorSpaceI.H
35  VectorSpace.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef VectorSpace_H
40 #define VectorSpace_H
41 
42 #include "direction.H"
43 #include "scalar.H"
44 #include "word.H"
45 #include "zero.H"
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 // Forward declaration of friend functions and operators
53 
54 template<class Form, class Cmpt, direction Ncmpts> class VectorSpace;
55 
56 template<class Form, class Cmpt, direction Ncmpts>
57 void writeEntry(Ostream& os, const VectorSpace<Form, Cmpt, Ncmpts>& value);
58 
59 template<class Form, class Cmpt, direction Ncmpts>
60 Istream& operator>>
61 (
62  Istream&,
64 );
65 
66 template<class Form, class Cmpt, direction Ncmpts>
67 Ostream& operator<<
68 (
69  Ostream&,
71 );
72 
73 
74 // Helper to restrict templated methods to rank-0 primitives
75 
76 template<class Type>
77 using EnableRank0 = typename std::enable_if<pTraits<Type>::rank == 0>::type;
78 
79 
80 /*---------------------------------------------------------------------------*\
81  Class VectorSpace Declaration
82 \*---------------------------------------------------------------------------*/
83 
84 template<class Form, class Cmpt, direction Ncmpts>
85 class VectorSpace
86 {
87 
88 public:
89 
90  //- The components of this vector space
91  Cmpt v_[Ncmpts];
92 
93  //- VectorSpace type
95 
96  //- Component type
97  typedef Cmpt cmptType;
98 
99 
100  // Static constants
101 
102  //- Dimensionality of space
103  static const direction dim = 3;
104 
105  //- Number of components in this vector space
106  static const direction nComponents = Ncmpts;
107 
108  // VectorSpace currently defaults to a column-vector
109  // This will be removed when column-vector is introduced
110  // as a specialisation
111  static const direction mRows = Ncmpts;
112  static const direction nCols = 1;
113 
114 
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  static const Form nan;
126 
127 
128  // Sub-Block Classes
129 
130  //- Const sub-block type
131  template
132  <
133  class SubVector,
134  direction BStart
135  >
136  class ConstBlock
137  {
138  const vsType& vs_;
139 
140  public:
141 
142  //- Number of components in this vector space
143  static const direction nComponents = SubVector::nComponents;
144 
145  //- Construct for a given vector
146  inline ConstBlock(const vsType& vs);
147 
148  //- [i] const element access operator
149  inline const Cmpt& operator[](const direction i) const;
150 
151  //- (i, 0) const element access operator
152  inline const Cmpt& operator()
153  (
154  const direction i,
155  const direction
156  ) const;
157  };
158 
159 
160  // Constructors
161 
162  //- Construct null
163  inline VectorSpace();
164 
165  //- Construct initialised to zero
166  inline VectorSpace(const Foam::zero);
167 
168  //- Construct from Istream
170 
171  //- Construct as copy of a VectorSpace with the same size
172  template<class Form2, class Cmpt2>
173  inline explicit VectorSpace(const VectorSpace<Form2, Cmpt2, Ncmpts>&);
174 
175 
176  // Member Functions
177 
178  //- Return the number of elements in the VectorSpace = Ncmpts.
179  inline static direction size();
180 
181  inline const Cmpt& component(const direction) const;
182  inline Cmpt& component(const direction);
183 
184  inline void component(Cmpt&, const direction) const;
185  inline void replace(const direction, const Cmpt&);
186 
187  //- Return a VectorSpace with all elements = s
188  inline static Form uniform(const Cmpt& s);
189 
190  template<class SubVector, direction BStart>
191  inline const ConstBlock<SubVector, BStart> block() const;
192 
193 
194  // Member Operators
195 
196  inline const Cmpt& operator[](const direction) const;
197  inline Cmpt& operator[](const direction);
198 
199  inline void operator+=(const VectorSpace<Form, Cmpt, Ncmpts>&);
200  inline void operator-=(const VectorSpace<Form, Cmpt, Ncmpts>&);
201 
202  inline void operator=(const Foam::zero);
203 
204  template<class Type, class Enable = EnableRank0<Type>>
205  inline void operator*=(const Type);
206  template<class Type, class Enable = EnableRank0<Type>>
207  inline void operator/=(const Type);
208 
209 
210  // IOstream Operators
211 
212  friend Istream& operator>> <Form, Cmpt, Ncmpts>
213  (
214  Istream&,
216  );
217 
218  friend Ostream& operator<< <Form, Cmpt, Ncmpts>
219  (
220  Ostream&,
222  );
223 };
224 
225 
226 // * * * * * * * * * * * * * * Global functions * * * * * * * * * * * * * * //
227 
228 //- Return a string representation of a VectorSpace
229 template<class Form, class Cmpt, direction Ncmpts>
231 
232 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
233 
234 } // End namespace Foam
235 
236 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
237 
238 #include "VectorSpaceI.H"
239 
240 #ifdef NoRepository
241  #include "VectorSpace.C"
242 #endif
243 
244 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
245 
246 #endif
247 
248 // ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Const sub-block type.
Definition: VectorSpace.H:136
ConstBlock(const vsType &vs)
Construct for a given vector.
Definition: VectorSpaceI.H:66
static const direction nComponents
Number of components in this vector space.
Definition: VectorSpace.H:142
const Cmpt & operator[](const direction i) const
[i] const element access operator
Definition: VectorSpaceI.H:231
Templated vector space.
Definition: VectorSpace.H:85
static const Form zero
Definition: VectorSpace.H:118
static const char *const componentNames[]
Definition: VectorSpace.H:117
void operator/=(const Type)
Definition: VectorSpaceI.H:319
friend Ostream & operator(Ostream &, const VectorSpace< Form, Cmpt, Ncmpts > &)
const Cmpt & component(const direction) const
Definition: VectorSpaceI.H:91
static const direction nComponents
Number of components in this vector space.
Definition: VectorSpace.H:105
void replace(const direction, const Cmpt &)
Definition: VectorSpaceI.H:149
VectorSpace()
Construct null.
Definition: VectorSpaceI.H:40
static const Form one
Definition: VectorSpace.H:119
static const direction dim
Dimensionality of space.
Definition: VectorSpace.H:102
void operator*=(const Type)
Definition: VectorSpaceI.H:308
static const Form nan
Definition: VectorSpace.H:124
static const Form rootMax
Definition: VectorSpace.H:122
VectorSpace< Form, Cmpt, Ncmpts > vsType
VectorSpace type.
Definition: VectorSpace.H:93
static const Form max
Definition: VectorSpace.H:120
void operator+=(const VectorSpace< Form, Cmpt, Ncmpts > &)
Definition: VectorSpaceI.H:280
const ConstBlock< SubVector, BStart > block() const
static const Form rootMin
Definition: VectorSpace.H:123
const Cmpt & operator[](const direction) const
Definition: VectorSpaceI.H:190
void operator-=(const VectorSpace< Form, Cmpt, Ncmpts > &)
Definition: VectorSpaceI.H:290
Cmpt v_[Ncmpts]
The components of this vector space.
Definition: VectorSpace.H:90
static Form uniform(const Cmpt &s)
Return a VectorSpace with all elements = s.
Definition: VectorSpaceI.H:168
Cmpt cmptType
Component type.
Definition: VectorSpace.H:96
static const Form min
Definition: VectorSpace.H:121
static direction size()
Return the number of elements in the VectorSpace = Ncmpts.
Definition: VectorSpaceI.H:83
static const direction nCols
Definition: VectorSpace.H:111
static const direction mRows
Definition: VectorSpace.H:110
static const char *const typeName
Definition: VectorSpace.H:116
void operator=(const Foam::zero)
Definition: VectorSpaceI.H:299
A class for handling words, derived from string.
Definition: word.H:62
A class representing the concept of 0 used to avoid unnecessary manipulations for objects that are kn...
Definition: zero.H:50
Direction is an 8-bit unsigned integer type used to represent the Cartesian directions etc.
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Namespace for OpenFOAM.
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
typename std::enable_if< pTraits< Type >::rank==0 >::type EnableRank0
Definition: VectorSpace.H:76
uint8_t direction
Definition: direction.H:45
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488