VectorI.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-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 \*---------------------------------------------------------------------------*/
28 
29 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 
31 template<class Cmpt>
33 :
35 {}
36 
37 
38 template<class Cmpt>
39 template<class Cmpt2>
41 (
42  const VectorSpace<Vector<Cmpt2>, Cmpt2, 3>& vs
43 )
44 :
45  Vector::vsType(vs)
46 {}
47 
48 
49 template<class Cmpt>
51 (
52  const Cmpt& vx,
53  const Cmpt& vy,
54  const Cmpt& vz
55 )
56 {
57  this->v_[X] = vx;
58  this->v_[Y] = vy;
59  this->v_[Z] = vz;
60 }
61 
62 
63 template<class Cmpt>
65 :
66  Vector::vsType(is)
67 {}
68 
69 
70 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
71 
72 template<class Cmpt>
74 (
76 ) const noexcept
77 {
78  return *this;
79 }
80 
81 
82 template<class Cmpt>
83 inline Foam::scalar Foam::Vector<Cmpt>::magSqr() const
84 {
85  return
86  (
87  Foam::magSqr(this->x())
88  + Foam::magSqr(this->y())
89  + Foam::magSqr(this->z())
90  );
91 }
92 
93 
94 template<class Cmpt>
95 inline Foam::scalar Foam::Vector<Cmpt>::mag() const
96 {
97  return ::sqrt(this->magSqr());
98 }
99 
100 
101 template<class Cmpt>
102 inline Foam::scalar Foam::Vector<Cmpt>::distSqr(const Vector<Cmpt>& v2) const
103 {
104  return
105  (
106  Foam::magSqr(v2.x() - this->x())
107  + Foam::magSqr(v2.y() - this->y())
108  + Foam::magSqr(v2.z() - this->z())
109  );
110 }
111 
112 
113 template<class Cmpt>
114 inline Foam::scalar Foam::Vector<Cmpt>::dist(const Vector<Cmpt>& v2) const
115 {
116  return ::sqrt(this->distSqr(v2));
117 }
118 
119 
120 template<class Cmpt>
121 inline Foam::Vector<Cmpt>& Foam::Vector<Cmpt>::normalise(const scalar tol)
122 {
123  const scalar s = this->mag();
124 
125  if (s < tol)
126  {
127  *this = Zero;
128  }
129  else
130  {
131  *this /= s;
132  }
134  return *this;
135 }
136 
137 
138 template<class Cmpt>
139 inline Foam::Vector<Cmpt>&
141 {
142  *this -= (*this & unitVec) * unitVec;
143  return *this;
144 }
145 
146 
147 // * * * * * * * * * * * * * * * Member Operations * * * * * * * * * * * * * //
148 
149 template<class Cmpt>
150 inline Cmpt Foam::Vector<Cmpt>::inner(const Vector<Cmpt>& v2) const
151 {
152  const Vector<Cmpt>& v1 = *this;
154  return (v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z());
155 }
156 
157 
158 template<class Cmpt>
159 inline Foam::Vector<Cmpt>
161 {
162  const Vector<Cmpt>& v1 = *this;
163 
164  return Vector<Cmpt>
165  (
166  (v1.y()*v2.z() - v1.z()*v2.y()),
167  (v1.z()*v2.x() - v1.x()*v2.z()),
168  (v1.x()*v2.y() - v1.y()*v2.x())
169  );
170 }
171 
172 
173 // * * * * * * * * * * * * * Comparison Operations * * * * * * * * * * * * * //
174 
175 template<class Cmpt>
176 inline bool
178 {
179  return
180  (
181  (a.x() < b.x()) // Component is less
182  ||
183  (
184  !(b.x() < a.x()) // Equal? Check next component
185  &&
186  (
187  (a.y() < b.y()) // Component is less
188  ||
189  (
190  !(b.y() < a.y()) // Equal? Check next component
191  && (a.z() < b.z())
192  )
193  )
194  )
195  );
196 }
197 
198 
199 template<class Cmpt>
200 inline bool
202 {
203  return
204  (
205  (a.y() < b.y()) // Component is less
206  ||
207  (
208  !(b.y() < a.y()) // Equal? Check next component
209  &&
210  (
211  (a.z() < b.z()) // Component is less
212  ||
213  (
214  !(b.z() < a.z()) // Equal? Check next component
215  && (a.x() < b.x())
216  )
217  )
218  )
219  );
220 }
221 
222 
223 template<class Cmpt>
224 inline bool
226 {
227  return
228  (
229  (a.z() < b.z()) // Component is less
230  ||
231  (
232  !(b.z() < a.z()) // Equal? Check next component
233  &&
234  (
235  (a.x() < b.x()) // Component is less
236  ||
237  (
238  !(b.x() < a.x()) // Equal? Check next component
239  && (a.y() < b.y())
240  )
241  )
242  )
243  );
244 }
245 
246 
247 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
248 
249 namespace Foam
250 {
251 
252 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
253 
254 //- Linear interpolation of vectors a and b by factor t
255 template<class Cmpt>
256 inline Vector<Cmpt> lerp
257 (
258  const Vector<Cmpt>& a,
259  const Vector<Cmpt>& b,
260  const scalar t
261 )
262 {
263  const scalar onet = (1-t);
264 
265  return Vector<Cmpt>
266  (
267  onet*a.x() + t*b.x(),
268  onet*a.y() + t*b.y(),
269  onet*a.z() + t*b.z()
270  );
271 }
272 
273 
274 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
275 
276 //- Dummy innerProduct for scalar
277 // Allows the construction of vtables for virtual member functions
278 // involving the inner-products of fields
279 // for which a "NotImplemented" specialization for scalar is provided.
280 template<class Cmpt>
281 class innerProduct<Vector<Cmpt>, scalar>
282 {
283 public:
284 
285  typedef scalar type;
286 };
287 
289 template<class Cmpt>
290 inline Cmpt operator&(const Vector<Cmpt>& v1, const Vector<Cmpt>& v2)
291 {
292  return v1.inner(v2);
293 }
294 
296 template<class Cmpt>
297 inline Vector<Cmpt> operator^(const Vector<Cmpt>& v1, const Vector<Cmpt>& v2)
298 {
299  return v1.cross(v2);
300 }
301 
303 template<class Cmpt>
304 inline Vector<Cmpt> operator*(const Cmpt& s, const Vector<Cmpt>& v)
305 {
306  return Vector<Cmpt>(s*v.x(), s*v.y(), s*v.z());
307 }
308 
310 template<class Cmpt>
311 inline Vector<Cmpt> operator*(const Vector<Cmpt>& v, const Cmpt& s)
312 {
313  return s*v;
314 }
315 
316 
317 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
318 
319 } // End namespace Foam
320 
321 // ************************************************************************* //
static bool less_yzx(const Vector< Cmpt > &a, const Vector< Cmpt > &b)
Lexicographically compare a and b with order (y:z:x)
Definition: VectorI.H:194
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
scalar distSqr(const Vector< Cmpt > &v2) const
The L2-norm distance squared from another vector. The magSqr() of the difference. ...
Definition: VectorI.H:95
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Vector()=default
Default construct.
dimensionedScalar sqrt(const dimensionedScalar &ds)
Templated vector space.
Definition: VectorSpace.H:52
Cmpt inner(const Vector< Cmpt > &v2) const
Scalar-product of this with another Vector.
Definition: VectorI.H:143
const Cmpt & y() const noexcept
Access to the vector y component.
Definition: Vector.H:140
Vector< Cmpt > & normalise(const scalar tol=ROOTVSMALL)
Inplace normalise the vector by its magnitude.
Definition: VectorI.H:114
scalar y
tmp< faMatrix< Type > > operator*(const areaScalarField::Internal &, const faMatrix< Type > &)
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
static bool less_zxy(const Vector< Cmpt > &a, const Vector< Cmpt > &b)
Lexicographically compare a and b with order (z:x:y)
Definition: VectorI.H:218
scalar magSqr() const
The length (L2-norm) squared of the vector.
Definition: VectorI.H:76
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:99
Templated 3D Vector derived from VectorSpace adding construction from 3 components, element access using x(), y() and z() member functions and the inner-product (dot-product) and cross-product operators.
Definition: Vector.H:58
const direction noexcept
Definition: Scalar.H:258
const Cmpt & x() const noexcept
Access to the vector x component.
Definition: Vector.H:135
dimensioned< Type > lerp(const dimensioned< Type > &a, const dimensioned< Type > &b, const scalar t)
const Vector< Cmpt > & centre(const Foam::UList< Vector< Cmpt >> &) const noexcept
Return this (for point which is a typedef to Vector<scalar>)
Definition: VectorI.H:67
scalar mag() const
The length (L2-norm) of the vector.
Definition: VectorI.H:88
static bool less_xyz(const Vector< Cmpt > &a, const Vector< Cmpt > &b)
Lexicographically compare a and b with order (x:y:z)
Definition: VectorI.H:170
bitSet operator^(const bitSet &a, const bitSet &b)
Bitwise-XOR of two bitsets to form a unique bit-set.
Definition: bitSetI.H:771
const Cmpt & z() const noexcept
Access to the vector z component.
Definition: Vector.H:145
tmp< GeometricField< Type, faPatchField, areaMesh > > operator &(const faMatrix< Type > &, const DimensionedField< Type, areaMesh > &)
PtrList< volScalarField > & Y
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
Vector< Cmpt > cross(const Vector< Cmpt > &v2) const
Cross-product of this with another Vector.
Definition: VectorI.H:153
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))
Vector< Cmpt > & removeCollinear(const Vector< Cmpt > &unitVec)
Inplace removal of components that are collinear to the given unit vector.
Definition: VectorI.H:133
typeOfRank< typename pTraits< arg1 >::cmptType, direction(pTraits< arg1 >::rank)+direction(pTraits< arg2 >::rank) - 2 >::type type
Definition: products.H:155
scalar dist(const Vector< Cmpt > &v2) const
The L2-norm distance from another vector. The mag() of the difference.
Definition: VectorI.H:107
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
Namespace for OpenFOAM.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:133