VectorSpaceI.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-2022 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 #include "error.H"
30 #include "products.H"
31 #include "VectorSpaceOps.H"
32 #include "ops.H"
33 #include <type_traits>
34 
35 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
36 
37 template<class Form, class Cmpt, Foam::direction Ncmpts>
39 {
41 }
42 
43 
44 template<class Form, class Cmpt, Foam::direction Ncmpts>
46 (
48 )
49 {
51 }
52 
53 
54 template<class Form, class Cmpt, Foam::direction Ncmpts>
55 template<class Form2, class Cmpt2>
57 (
59 )
60 {
62 }
63 
64 
65 template<class Form, class Cmpt, Foam::direction Ncmpts>
66 template<class SubVector, Foam::direction BStart>
69 (
70  const vsType& vs
71 )
72 :
73  vs_(vs)
74 {
75  static_assert
76  (
77  vsType::nComponents >= BStart + nComponents,
78  "Requested block size > VectorSpace size"
79  );
80 }
81 
82 
83 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
84 
85 template<class Form, class Cmpt, Foam::direction Ncmpts>
87 (
88  const direction d
89 ) const
90 {
91  #ifdef FULLDEBUG
92  if (d >= Ncmpts)
93  {
95  << "index out of range"
96  << abort(FatalError);
97  }
98  #endif
99 
100  return v_[d];
101 }
102 
103 
104 template<class Form, class Cmpt, Foam::direction Ncmpts>
106 (
107  const direction d
108 )
109 {
110  #ifdef FULLDEBUG
111  if (d >= Ncmpts)
112  {
114  << "index out of range"
115  << abort(FatalError);
116  }
117  #endif
119  return v_[d];
120 }
121 
122 
123 template<class Form, class Cmpt, Foam::direction Ncmpts>
125 (
126  Cmpt& c,
127  const direction d
128 ) const
129 {
130  #ifdef FULLDEBUG
131  if (d >= Ncmpts)
132  {
134  << "index out of range"
135  << abort(FatalError);
136  }
137  #endif
139  c = v_[d];
140 }
141 
142 
143 template<class Form, class Cmpt, Foam::direction Ncmpts>
145 (
146  const direction d,
147  const Cmpt& c
148 )
149 {
150  #ifdef FULLDEBUG
151  if (d >= Ncmpts)
152  {
154  << "index out of range"
155  << abort(FatalError);
156  }
157  #endif
158 
159  v_[d] = c;
160 }
161 
162 
163 template<class Form, class Cmpt, Foam::direction Ncmpts>
164 inline Form Foam::VectorSpace<Form, Cmpt, Ncmpts>::uniform(const Cmpt& s)
165 {
166  Form v;
168  return v;
169 }
170 
171 
172 template<class Form, class Cmpt, Foam::direction Ncmpts>
173 template<class SubVector, Foam::direction BStart>
175  ConstBlock<SubVector, BStart>
177 {
178  return *this;
179 }
180 
181 
182 // * * * * * * * * * * * * * * * * Iterator * * * * * * * * * * * * * * * * //
183 
184 template<class Form, class Cmpt, Foam::direction Ncmpts>
186 {
187  return v_;
188 }
189 
190 
191 template<class Form, class Cmpt, Foam::direction Ncmpts>
193 {
194  return v_;
195 }
196 
197 
198 template<class Form, class Cmpt, Foam::direction Ncmpts>
200 {
201  return v_;
202 }
203 
204 
205 template<class Form, class Cmpt, Foam::direction Ncmpts>
207 {
208  return (v_ + Ncmpts);
209 }
210 
211 
212 template<class Form, class Cmpt, Foam::direction Ncmpts>
214 const noexcept
215 {
216  return v_;
217 }
218 
219 
220 template<class Form, class Cmpt, Foam::direction Ncmpts>
222 const noexcept
223 {
224  return (v_ + Ncmpts);
225 }
226 
227 
228 template<class Form, class Cmpt, Foam::direction Ncmpts>
230 const noexcept
231 {
232  return v_;
233 }
234 
235 
236 template<class Form, class Cmpt, Foam::direction Ncmpts>
238 const noexcept
239 {
240  return (v_ + Ncmpts);
241 }
242 
243 
244 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
245 
246 template<class Form, class Cmpt, Foam::direction Ncmpts>
248 (
249  const direction d
250 ) const
251 {
252  #ifdef FULLDEBUG
253  if (d >= Ncmpts)
254  {
256  << "index out of range"
257  << abort(FatalError);
258  }
259  #endif
261  return v_[d];
262 }
263 
264 
265 template<class Form, class Cmpt, Foam::direction Ncmpts>
267 (
268  const direction d
269 )
270 {
271  #ifdef FULLDEBUG
272  if (d >= Ncmpts)
273  {
275  << "index out of range"
276  << abort(FatalError);
277  }
278  #endif
279 
280  return v_[d];
281 }
283 
284 template<class Form, class Cmpt, Foam::direction Ncmpts>
285 template<class SubVector, Foam::direction BStart>
286 inline const Cmpt&
289 (
290  const direction d
291 ) const
292 {
293  #ifdef FULLDEBUG
294  if (d >= Ncmpts)
295  {
297  << "index out of range"
298  << abort(FatalError);
299  }
300  #endif
301 
302  return vs_[BStart + d];
303 }
305 
306 template<class Form, class Cmpt, Foam::direction Ncmpts>
307 template<class SubVector, Foam::direction BStart>
308 inline const Cmpt&
311 (
312  const direction i,
313  const direction j
314 ) const
315 {
316  #ifdef FULLDEBUG
317  if (i >= Ncmpts)
318  {
320  << "index " << i << " out of range"
321  << abort(FatalError);
322  }
323 
324  if (j)
325  {
327  << "index " << j << " != 0"
328  << abort(FatalError);
329  }
330  #endif
332  return vs_[BStart + i];
333 }
334 
335 
336 template<class Form, class Cmpt, Foam::direction Ncmpts>
338 (
340 )
341 {
343 }
344 
345 
346 template<class Form, class Cmpt, Foam::direction Ncmpts>
348 (
350 )
351 {
353 }
354 
355 
356 template<class Form, class Cmpt, Foam::direction Ncmpts>
358 (
360 )
361 {
363 }
364 
365 
366 template<class Form, class Cmpt, Foam::direction Ncmpts>
368 {
370 }
371 
372 
373 template<class Form, class Cmpt, Foam::direction Ncmpts>
375 (
376  const scalar s
377 )
378 {
380 }
381 
382 
383 template<class Form, class Cmpt, Foam::direction Ncmpts>
385 (
386  const scalar s
387 )
388 {
390 }
391 
392 
393 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
394 
395 namespace Foam
396 {
397 
398 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
399 
400 template<class Form, class Cmpt, direction Ncmpts>
401 inline Cmpt& setComponent
402 (
404  const direction d
405 )
406 {
407  return vs.component(d);
408 }
409 
410 
411 template<class Form, class Cmpt, direction Ncmpts>
412 inline const Cmpt& component
413 (
414  const VectorSpace<Form, Cmpt, Ncmpts>& vs,
415  const direction d
416 )
417 {
418  return vs.component(d);
419 }
421 
422 // Powers of a Form
423 // Equivalent to outer-products between the Form and itself
424 // Form^0 = 1.0
425 template<class Form, class Cmpt, direction Ncmpts>
426 inline typename powProduct<Form, 0>::type pow
427 (
431 )
432 {
433  return 1.0;
434 }
435 
436 
437 // Form^1 = Form
438 template<class Form, class Cmpt, direction Ncmpts>
439 inline typename powProduct<Form, 1>::type pow
440 (
441  const VectorSpace<Form, Cmpt, Ncmpts>& v,
443  = pTraits<typename powProduct<Form, 1>::type>::zero
444 )
445 {
446  return static_cast<const Form&>(v);
447 }
448 
449 
450 // Form^2 = sqr(Form)
451 template<class Form, class Cmpt, direction Ncmpts>
452 inline typename powProduct<Form, 2>::type pow
453 (
454  const VectorSpace<Form, Cmpt, Ncmpts>& v,
456  = pTraits<typename powProduct<Form, 2>::type>::zero
457 )
458 {
459  return sqr(static_cast<const Form&>(v));
460 }
461 
462 
463 template<class Form, class Cmpt, direction Ncmpts>
464 inline scalar magSqr
465 (
466  const VectorSpace<Form, Cmpt, Ncmpts>& vs
467 )
468 {
469  scalar ms = magSqr(vs.v_[0]);
471  return ms;
472 }
473 
474 
475 template<class Form, class Cmpt, direction Ncmpts>
476 inline scalar mag
477 (
478  const VectorSpace<Form, Cmpt, Ncmpts>& vs
479 )
480 {
481  return ::sqrt(magSqr(static_cast<const Form&>(vs)));
482 }
483 
484 
485 //- Return the vector type normalised by its magnitude
486 // For small magnitudes (less than ROOTVSMALL) return zero.
487 template<class Form, class Cmpt, direction Ncmpts>
489 (
491 )
492 {
493  #ifdef __clang__
494  volatile // Use volatile to avoid aggressive branch optimization
495  #endif
496  const scalar s(mag(vs));
497  return s < ROOTVSMALL ? Zero : vs/s;
498 }
500 
501 template<class Form, class Cmpt, direction Ncmpts>
503 (
506 )
507 {
508  Form v;
510  return v;
511 }
513 
514 template<class Form, class Cmpt, direction Ncmpts>
516 (
519 )
520 {
521  Form v;
523  return v;
524 }
526 
527 template<class Form, class Cmpt, direction Ncmpts>
529 (
532 )
533 {
534  Form v;
536  return v;
537 }
539 
540 template<class Form, class Cmpt, direction Ncmpts>
542 (
544  const Cmpt& small
545 )
546 {
547  Form v;
549  return v;
550 }
552 
553 template<class Form, class Cmpt, direction Ncmpts>
554 inline Cmpt cmptMax
555 (
557 )
558 {
559  Cmpt cMax = vs.v_[0];
561  return cMax;
562 }
564 
565 template<class Form, class Cmpt, direction Ncmpts>
566 inline Cmpt cmptMin
567 (
569 )
570 {
571  Cmpt cMin = vs.v_[0];
573  return cMin;
574 }
576 
577 template<class Form, class Cmpt, direction Ncmpts>
578 inline Cmpt cmptSum
579 (
581 )
582 {
583  Cmpt sum = vs.v_[0];
585  return sum;
586 }
588 
589 template<class Form, class Cmpt, direction Ncmpts>
590 inline Cmpt cmptAv
591 (
593 )
594 {
595  return cmptSum(vs)/Ncmpts;
596 }
598 
599 template<class Form, class Cmpt, direction Ncmpts>
600 inline Cmpt cmptProduct
601 (
603 )
604 {
605  Cmpt product = vs.v_[0];
607  return product;
608 }
610 
611 template<class Form, class Cmpt, direction Ncmpts>
612 inline Form cmptSqr
613 (
615 )
616 {
617  Form v;
619  return v;
620 }
622 
623 template<class Form, class Cmpt, direction Ncmpts>
624 inline Form cmptMag
625 (
627 )
628 {
629  Form v;
631  return v;
632 }
634 
635 template<class Form, class Cmpt, direction Ncmpts>
636 inline Form cmptMagSqr
637 (
639 )
640 {
641  Form v;
643  return v;
644 }
646 
647 template<class Form, class Cmpt, direction Ncmpts>
648 inline Form max
649 (
652 )
653 {
654  Form v;
656  return v;
657 }
659 
660 template<class Form, class Cmpt, direction Ncmpts>
661 inline Form min
662 (
665 )
666 {
667  Form v;
669  return v;
670 }
672 
673 template<class Form, class Cmpt, direction Ncmpts>
674 inline Form minMod
675 (
678 )
679 {
680  Form v;
682  return v;
683 }
684 
685 
686 template<class Type>
687 inline Type dot(const scalar s, const Type& t)
688 {
689  return s * t;
690 }
691 
692 
693 template<class Type>
694 inline Type dot(const Type& t, const scalar s)
695 {
696  return t * s;
697 }
698 
699 
700 template
701 <
702  class Form1, class Cmpt1, direction Ncmpts1,
703  class Form2, class Cmpt2, direction Ncmpts2
704 >
706 (
709 )
710 {
711  return static_cast<const Form1&>(t1) & static_cast<const Form2&>(t2);
712 }
713 
714 
715 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
716 
717 template<class Form, class Cmpt, direction Ncmpts>
718 inline Form operator-
719 (
721 )
722 {
723  Form v;
725  return v;
726 }
728 
729 template<class Form, class Cmpt, direction Ncmpts>
730 inline Form operator+
731 (
734 )
735 {
736  Form v;
738  return v;
739 }
740 
741 template<class Form, class Cmpt, direction Ncmpts>
742 inline Form operator-
743 (
744  const VectorSpace<Form, Cmpt, Ncmpts>& vs1,
745  const VectorSpace<Form, Cmpt, Ncmpts>& vs2
746 )
747 {
748  Form v;
749  VectorSpaceOps<Ncmpts,0>::op(v, vs1, vs2, minusOp<Cmpt>());
750  return v;
751 }
753 
754 template<class Form, class Cmpt, direction Ncmpts>
755 inline Form operator*
756 (
757  scalar s,
759 )
760 {
761  Form v;
763  return v;
764 }
766 
767 template<class Form, class Cmpt, direction Ncmpts>
768 inline Form operator*
769 (
771  scalar s
772 )
773 {
774  Form v;
776  return v;
777 }
779 
780 template<class Form, class Cmpt, direction Ncmpts>
781 inline Form operator/
782 (
784  scalar s
785 )
786 {
787  Form v;
789  return v;
790 }
791 
792 /*
793 template<class Form, class Cmpt, direction Ncmpts>
794 inline Form operator/
795 (
796  const VectorSpace<Form, Cmpt, Ncmpts>& vs1,
797  const VectorSpace<Form, Cmpt, Ncmpts>& vs2
798 )
799 {
800  Form v;
801  VectorSpaceOps<Ncmpts,0>::op(v, vs1, vs2, divideOp<Cmpt>());
802  return v;
803 }
804 
805 
806 template<class Form, class Cmpt, direction Ncmpts>
807 inline Form operator/
808 (
809  scalar s,
810  const VectorSpace<Form, Cmpt, Ncmpts>& vs
811 )
812 {
813  Form v;
814  VectorSpaceOps<Ncmpts,0>::opSV(v, s, vs, divideOp2<scalar, Cmpt>());
815  return v;
816 }
817 */
819 
820 template<class Form, class Cmpt, direction Ncmpts>
821 inline Cmpt operator&&
822 (
825 )
826 {
827  Cmpt ddProd = vs1.v_[0]*vs2.v_[0];
828  for (direction i=1; i<Ncmpts; ++i)
829  {
830  ddProd += vs1.v_[i]*vs2.v_[i];
831  }
832  return ddProd;
833 }
835 
836 template<class Form, class Cmpt, direction Ncmpts>
837 inline bool operator==
838 (
841 )
842 {
843  for (direction i=0; i<Ncmpts; ++i)
844  {
845  if (!equal(vs1.v_[i], vs2.v_[i])) return false;
846  }
847  return true;
848 }
850 
851 template<class Form, class Cmpt, direction Ncmpts>
852 inline bool operator!=
853 (
856 )
857 {
858  return !(vs1 == vs2);
859 }
861 
862 template<class Form, class Cmpt, direction Ncmpts>
863 inline bool operator<
864 (
867 )
868 {
869  // Compare all components, stop at first non less-than component
870  for (direction i=0; i<Ncmpts; ++i)
871  {
872  if (!(vs1.v_[i] < vs2.v_[i])) return false;
873  }
874  return true;
875 }
877 
878 template<class Form, class Cmpt, direction Ncmpts>
879 inline bool operator<=
880 (
883 )
884 {
885  return !(vs2 < vs1);
886 }
888 
889 template<class Form, class Cmpt, direction Ncmpts>
890 inline bool operator>
891 (
894 )
895 {
896  return (vs2 < vs1);
897 }
899 
900 template<class Form, class Cmpt, direction Ncmpts>
901 inline bool operator>=
902 (
905 )
906 {
907  return !(vs1 < vs2);
908 }
909 
910 
911 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
912 
913 } // End namespace Foam
914 
915 // ************************************************************************* //
Const sub-block type.
Definition: VectorSpace.H:137
const Cmpt * cdata() const noexcept
Return const pointer to the first data element.
Definition: VectorSpaceI.H:185
void cmptMax(FieldField< Field, typename FieldField< Field, Type >::cmptType > &cf, const FieldField< Field, Type > &f)
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &f1)
Definition: ops.H:67
uint8_t direction
Definition: direction.H:46
Cmpt cmptProduct(const VectorSpace< Form, Cmpt, Ncmpts > &vs)
Definition: VectorSpaceI.H:597
tmp< DimensionedField< typename DimensionedField< Type, GeoMesh >::cmptType, GeoMesh >> cmptAv(const DimensionedField< Type, GeoMesh > &f1)
static const Form max
Definition: VectorSpace.H:125
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
Cmpt cmptSum(const SphericalTensor< Cmpt > &st)
Return the sum of components of a SphericalTensor.
bool equal(const T &a, const T &b)
Compare two values for equality.
Definition: label.H:164
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
void operator=(const VectorSpace< Form, Cmpt, Ncmpts > &)
Definition: VectorSpaceI.H:331
General looping form. Executing at index <I> with termination at <N>
Cmpt * data() noexcept
Return pointer to the first data element.
Definition: VectorSpaceI.H:178
dimensionedSymmTensor sqr(const dimensionedVector &dv)
static void eqOpS(V &vs, const S &s, EqOp eo)
Traits classes for inner and outer products of primitives.
dimensionedScalar sqrt(const dimensionedScalar &ds)
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:75
Templated vector space.
Definition: VectorSpace.H:52
static void opVS(V &vs, const V1 &vs1, const S &s, Op o)
Various functors for unary and binary operations. Can be used for parallel combine-reduce operations ...
iterator end() noexcept
Return an iterator to end of VectorSpace.
Definition: VectorSpaceI.H:199
dimensionedScalar stabilise(const dimensionedScalar &x, const dimensionedScalar &y)
quaternion normalised(const quaternion &q)
Return the normalised (unit) quaternion of the given quaternion.
Definition: quaternionI.H:674
void replace(const direction, const Cmpt &)
Definition: VectorSpaceI.H:138
static void SeqOp(S &s, const V &vs, EqOp eo)
void dot(FieldField< Field1, typename innerProduct< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
static const Form min
Definition: VectorSpace.H:126
const_iterator cend() const noexcept
Return const_iterator to end of VectorSpace.
Definition: VectorSpaceI.H:214
void cmptMagSqr(Field< Type > &result, const UList< Type > &f1)
VectorSpace()=default
Default construct.
errorManip< error > abort(error &err)
Definition: errorManip.H:139
Scalar cmptPow(const Scalar s1, const Scalar s2)
Definition: Scalar.H:449
const ConstBlock< SubVector, BStart > block() const
const_iterator cbegin() const noexcept
Return const_iterator to begin of VectorSpace.
Definition: VectorSpaceI.H:206
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
static constexpr direction nComponents
Number of components in this vector space.
Definition: VectorSpace.H:109
void cmptMag(FieldField< Field, Type > &cf, const FieldField< Field, Type > &f)
const direction noexcept
Definition: Scalar.H:258
static void op(V &vs, const V1 &vs1, const V1 &vs2, Op o)
Scalar cmptSqr(const Scalar s)
Definition: Scalar.H:479
static void opSV(V &vs, const S &s, const V1 &vs1, Op o)
friend Ostream & operator(Ostream &, const VectorSpace< Form, Cmpt, Ncmpts > &)
symmTypeOfRank< typename pTraits< arg1 >::cmptType, arg2 *direction(pTraits< arg1 >::rank) >::type type
Definition: products.H:176
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
Scalar minMod(const Scalar s1, const Scalar s2)
Definition: Scalar.H:371
static void eqOp(V1 &vs1, const V2 &vs2, EqOp eo)
const dimensionedScalar c
Speed of light in a vacuum.
iterator begin() noexcept
Return an iterator to begin of VectorSpace.
Definition: VectorSpaceI.H:192
const Cmpt & component(const direction) const
Definition: VectorSpaceI.H:80
static Form uniform(const Cmpt &s)
Return a VectorSpace with all elements = s.
Definition: VectorSpaceI.H:157
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
void cmptMin(FieldField< Field, typename FieldField< Field, Type >::cmptType > &cf, const FieldField< Field, Type > &f)
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))
label & setComponent(label &val, const direction) noexcept
Non-const access to integer-type (has no components)
Definition: label.H:144
typeOfRank< typename pTraits< arg1 >::cmptType, direction(pTraits< arg1 >::rank)+direction(pTraits< arg2 >::rank) - 2 >::type type
Definition: products.H:155
static const Form zero
Definition: VectorSpace.H:123
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
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:146
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127