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-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 \*---------------------------------------------------------------------------*/
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 {
40  VectorSpaceOps<Ncmpts>::fill_n(this->begin(), Cmpt(Foam::zero{}));
41 }
42 
43 
44 template<class Form, class Cmpt, Foam::direction Ncmpts>
46 (
48 )
49 {
50  VectorSpaceOps<Ncmpts>::copy_n(vs.cbegin(), this->begin());
51 }
52 
53 
54 template<class Form, class Cmpt, Foam::direction Ncmpts>
55 template<class Form2, class Cmpt2>
57 (
59 )
60 {
61  VectorSpaceOps<Ncmpts>::copy_n(vs.cbegin(), this->begin());
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>
165 {
167 }
168 
169 
170 template<class Form, class Cmpt, Foam::direction Ncmpts>
171 inline Form Foam::VectorSpace<Form, Cmpt, Ncmpts>::uniform(const Cmpt& s)
172 {
173  Form v;
174  v.fill(s);
175  return v;
176 }
177 
178 
179 template<class Form, class Cmpt, Foam::direction Ncmpts>
180 template<class SubVector, Foam::direction BStart>
182  ConstBlock<SubVector, BStart>
184 {
185  return *this;
186 }
187 
188 
189 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
190 
191 template<class Form, class Cmpt, Foam::direction Ncmpts>
193 (
194  const direction d
195 ) const
196 {
197  #ifdef FULLDEBUG
198  if (d >= Ncmpts)
199  {
201  << "index out of range"
202  << abort(FatalError);
203  }
204  #endif
206  return v_[d];
207 }
208 
209 
210 template<class Form, class Cmpt, Foam::direction Ncmpts>
212 (
213  const direction d
214 )
215 {
216  #ifdef FULLDEBUG
217  if (d >= Ncmpts)
218  {
220  << "index out of range"
221  << abort(FatalError);
222  }
223  #endif
224 
225  return v_[d];
226 }
228 
229 template<class Form, class Cmpt, Foam::direction Ncmpts>
230 template<class SubVector, Foam::direction BStart>
231 inline const Cmpt&
234 (
235  const direction d
236 ) const
237 {
238  #ifdef FULLDEBUG
239  if (d >= Ncmpts)
240  {
242  << "index out of range"
243  << abort(FatalError);
244  }
245  #endif
246 
247  return vs_[BStart + d];
248 }
250 
251 template<class Form, class Cmpt, Foam::direction Ncmpts>
252 template<class SubVector, Foam::direction BStart>
253 inline const Cmpt&
256 (
257  const direction i,
258  const direction j
259 ) const
260 {
261  #ifdef FULLDEBUG
262  if (i >= Ncmpts)
263  {
265  << "index " << i << " out of range"
266  << abort(FatalError);
267  }
268 
269  if (j)
270  {
272  << "index " << j << " != 0"
273  << abort(FatalError);
274  }
275  #endif
277  return vs_[BStart + i];
278 }
279 
280 
281 template<class Form, class Cmpt, Foam::direction Ncmpts>
283 (
285 )
286 {
288 }
289 
290 
291 template<class Form, class Cmpt, Foam::direction Ncmpts>
292 inline void Foam::VectorSpace<Form, Cmpt, Ncmpts>::operator+=
293 (
295 )
296 {
298 }
299 
300 
301 template<class Form, class Cmpt, Foam::direction Ncmpts>
303 (
305 )
306 {
308 }
309 
310 
311 template<class Form, class Cmpt, Foam::direction Ncmpts>
313 {
315 }
316 
317 
318 template<class Form, class Cmpt, Foam::direction Ncmpts>
320 (
321  const scalar s
322 )
323 {
325 }
326 
327 
328 template<class Form, class Cmpt, Foam::direction Ncmpts>
330 (
331  const scalar s
332 )
333 {
335 }
336 
337 
338 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
339 
340 namespace Foam
341 {
342 
343 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
344 
345 template<class Form, class Cmpt, direction Ncmpts>
346 inline Cmpt& setComponent
347 (
349  const direction d
350 )
351 {
352  return vs.component(d);
353 }
354 
355 
356 template<class Form, class Cmpt, direction Ncmpts>
357 inline const Cmpt& component
358 (
359  const VectorSpace<Form, Cmpt, Ncmpts>& vs,
360  const direction d
361 )
362 {
363  return vs.component(d);
364 }
366 
367 // Powers of a Form
368 // Equivalent to outer-products between the Form and itself
369 // Form^0 = 1.0
370 template<class Form, class Cmpt, direction Ncmpts>
371 inline typename powProduct<Form, 0>::type pow
372 (
376 )
377 {
378  return 1.0;
379 }
380 
381 
382 // Form^1 = Form
383 template<class Form, class Cmpt, direction Ncmpts>
384 inline typename powProduct<Form, 1>::type pow
385 (
386  const VectorSpace<Form, Cmpt, Ncmpts>& v,
388  = pTraits<typename powProduct<Form, 1>::type>::zero
389 )
390 {
391  return static_cast<const Form&>(v);
392 }
393 
394 
395 // Form^2 = sqr(Form)
396 template<class Form, class Cmpt, direction Ncmpts>
397 inline typename powProduct<Form, 2>::type pow
398 (
399  const VectorSpace<Form, Cmpt, Ncmpts>& v,
401  = pTraits<typename powProduct<Form, 2>::type>::zero
402 )
403 {
404  return sqr(static_cast<const Form&>(v));
405 }
406 
407 
408 template<class Form, class Cmpt, direction Ncmpts>
409 inline scalar magSqr
410 (
411  const VectorSpace<Form, Cmpt, Ncmpts>& vs
412 )
413 {
414  scalar ms = magSqr(vs.v_[0]);
416  return ms;
417 }
418 
419 
420 template<class Form, class Cmpt, direction Ncmpts>
421 inline scalar mag
422 (
423  const VectorSpace<Form, Cmpt, Ncmpts>& vs
424 )
425 {
426  return ::sqrt(magSqr(static_cast<const Form&>(vs)));
427 }
428 
429 
430 //- Return the vector type normalised by its magnitude
431 // For small magnitudes (less than ROOTVSMALL) return zero.
432 template<class Form, class Cmpt, direction Ncmpts>
434 (
436 )
437 {
438  #ifdef __clang__
439  volatile // Use volatile to avoid aggressive branch optimization
440  #endif
441  const scalar s(mag(vs));
442  return s < ROOTVSMALL ? Zero : vs/s;
443 }
445 
446 template<class Form, class Cmpt, direction Ncmpts>
448 (
451 )
452 {
453  Form v;
455  return v;
456 }
458 
459 template<class Form, class Cmpt, direction Ncmpts>
461 (
464 )
465 {
466  Form v;
468  return v;
469 }
471 
472 template<class Form, class Cmpt, direction Ncmpts>
474 (
477 )
478 {
479  Form v;
481  return v;
482 }
484 
485 template<class Form, class Cmpt, direction Ncmpts>
487 (
489  const Cmpt& small
490 )
491 {
492  Form v;
494  return v;
495 }
497 
498 template<class Form, class Cmpt, direction Ncmpts>
499 inline Cmpt cmptMax
500 (
502 )
503 {
504  Cmpt cMax = vs.v_[0];
506  return cMax;
507 }
509 
510 template<class Form, class Cmpt, direction Ncmpts>
511 inline Cmpt cmptMin
512 (
514 )
515 {
516  Cmpt cMin = vs.v_[0];
518  return cMin;
519 }
521 
522 template<class Form, class Cmpt, direction Ncmpts>
523 inline Cmpt cmptSum
524 (
526 )
527 {
528  Cmpt sum = vs.v_[0];
530  return sum;
531 }
533 
534 template<class Form, class Cmpt, direction Ncmpts>
535 inline Cmpt cmptAv
536 (
538 )
539 {
540  return cmptSum(vs)/Ncmpts;
541 }
543 
544 template<class Form, class Cmpt, direction Ncmpts>
545 inline Cmpt cmptProduct
546 (
548 )
549 {
550  Cmpt product = vs.v_[0];
552  return product;
553 }
555 
556 template<class Form, class Cmpt, direction Ncmpts>
557 inline Form cmptSqr
558 (
560 )
561 {
562  Form v;
564  return v;
565 }
567 
568 template<class Form, class Cmpt, direction Ncmpts>
569 inline Form cmptMag
570 (
572 )
573 {
574  Form v;
576  return v;
577 }
579 
580 template<class Form, class Cmpt, direction Ncmpts>
581 inline Form cmptMagSqr
582 (
584 )
585 {
586  Form v;
588  return v;
589 }
591 
592 template<class Form, class Cmpt, direction Ncmpts>
593 inline Form max
594 (
597 )
598 {
599  Form v;
601  return v;
602 }
604 
605 template<class Form, class Cmpt, direction Ncmpts>
606 inline Form min
607 (
610 )
611 {
612  Form v;
614  return v;
615 }
617 
618 template<class Form, class Cmpt, direction Ncmpts>
619 inline Form minMod
620 (
623 )
624 {
625  Form v;
627  return v;
628 }
629 
630 
631 template<class Type>
632 inline Type dot(const scalar s, const Type& t)
633 {
634  return s * t;
635 }
636 
637 
638 template<class Type>
639 inline Type dot(const Type& t, const scalar s)
640 {
641  return t * s;
642 }
643 
644 
645 template
646 <
647  class Form1, class Cmpt1, direction Ncmpts1,
648  class Form2, class Cmpt2, direction Ncmpts2
649 >
651 (
654 )
655 {
656  return static_cast<const Form1&>(t1) & static_cast<const Form2&>(t2);
657 }
658 
659 
660 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
661 
662 template<class Form, class Cmpt, direction Ncmpts>
663 inline Form operator-
664 (
666 )
667 {
668  Form v;
670  return v;
671 }
673 
674 template<class Form, class Cmpt, direction Ncmpts>
675 inline Form operator+
676 (
679 )
680 {
681  Form v;
683  return v;
684 }
685 
686 template<class Form, class Cmpt, direction Ncmpts>
687 inline Form operator-
688 (
689  const VectorSpace<Form, Cmpt, Ncmpts>& vs1,
690  const VectorSpace<Form, Cmpt, Ncmpts>& vs2
691 )
692 {
693  Form v;
694  VectorSpaceOps<Ncmpts,0>::op(v, vs1, vs2, minusOp<Cmpt>());
695  return v;
696 }
698 
699 template<class Form, class Cmpt, direction Ncmpts>
700 inline Form operator*
701 (
702  scalar s,
704 )
705 {
706  Form v;
708  return v;
709 }
711 
712 template<class Form, class Cmpt, direction Ncmpts>
713 inline Form operator*
714 (
716  scalar s
717 )
718 {
719  Form v;
721  return v;
722 }
724 
725 template<class Form, class Cmpt, direction Ncmpts>
726 inline Form operator/
727 (
729  scalar s
730 )
731 {
732  Form v;
734  return v;
735 }
736 
737 /*
738 template<class Form, class Cmpt, direction Ncmpts>
739 inline Form operator/
740 (
741  const VectorSpace<Form, Cmpt, Ncmpts>& vs1,
742  const VectorSpace<Form, Cmpt, Ncmpts>& vs2
743 )
744 {
745  Form v;
746  VectorSpaceOps<Ncmpts,0>::op(v, vs1, vs2, divideOp<Cmpt>());
747  return v;
748 }
749 
750 
751 template<class Form, class Cmpt, direction Ncmpts>
752 inline Form operator/
753 (
754  scalar s,
755  const VectorSpace<Form, Cmpt, Ncmpts>& vs
756 )
757 {
758  Form v;
759  VectorSpaceOps<Ncmpts,0>::opSV(v, s, vs, divideOp2<scalar, Cmpt>());
760  return v;
761 }
762 */
764 
765 template<class Form, class Cmpt, direction Ncmpts>
766 inline Cmpt operator&&
767 (
770 )
771 {
772  Cmpt ddProd = vs1.v_[0]*vs2.v_[0];
773  for (direction i=1; i<Ncmpts; ++i)
774  {
775  ddProd += vs1.v_[i]*vs2.v_[i];
776  }
777  return ddProd;
778 }
780 
781 template<class Form, class Cmpt, direction Ncmpts>
782 inline bool operator==
783 (
786 )
787 {
788  for (direction i=0; i<Ncmpts; ++i)
789  {
790  if (!equal(vs1.v_[i], vs2.v_[i])) return false;
791  }
792  return true;
793 }
795 
796 template<class Form, class Cmpt, direction Ncmpts>
797 inline bool operator!=
798 (
801 )
802 {
803  return !(vs1 == vs2);
804 }
806 
807 template<class Form, class Cmpt, direction Ncmpts>
808 inline bool operator<
809 (
812 )
813 {
814  // Compare all components, stop at first non less-than component
815  for (direction i=0; i<Ncmpts; ++i)
816  {
817  if (!(vs1.v_[i] < vs2.v_[i])) return false;
818  }
819  return true;
820 }
822 
823 template<class Form, class Cmpt, direction Ncmpts>
824 inline bool operator<=
825 (
828 )
829 {
830  return !(vs2 < vs1);
831 }
833 
834 template<class Form, class Cmpt, direction Ncmpts>
835 inline bool operator>
836 (
839 )
840 {
841  return (vs2 < vs1);
842 }
844 
845 template<class Form, class Cmpt, direction Ncmpts>
846 inline bool operator>=
847 (
850 )
851 {
852  return !(vs1 < vs2);
853 }
854 
855 
856 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
857 
858 } // End namespace Foam
859 
860 // ************************************************************************* //
Const sub-block type.
Definition: VectorSpace.H:142
void cmptMax(FieldField< Field, typename FieldField< Field, Type >::cmptType > &cf, const FieldField< Field, Type > &f)
uint8_t direction
Definition: direction.H:46
Cmpt cmptProduct(const VectorSpace< Form, Cmpt, Ncmpts > &vs)
Definition: VectorSpaceI.H:542
tmp< DimensionedField< typename DimensionedField< Type, GeoMesh >::cmptType, GeoMesh >> cmptAv(const DimensionedField< Type, GeoMesh > &f1)
static const Form max
Definition: VectorSpace.H:130
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:600
void operator=(const VectorSpace< Form, Cmpt, Ncmpts > &)
Definition: VectorSpaceI.H:276
Recursive execution. Terminating at <N>, starting at index <I>
dimensionedSymmTensor sqr(const dimensionedVector &dv)
static void eqOpS(V &vs, const S &s, EqOp eo)
Apply the binary assignment operation to each vector-space component.
Traits classes for inner and outer products of primitives.
iterator begin() noexcept
Return an iterator (pointer) to begin of VectorSpace.
Definition: VectorSpace.H:275
dimensionedScalar sqrt(const dimensionedScalar &ds)
static void op(V &vs, const V1 &vs1, const V1 &vs2, BinaryOp bop)
Apply the binary operation between two vector-space data and assign the result.
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:61
Templated vector space.
Definition: VectorSpace.H:52
Various functors for unary and binary operations. Can be used for parallel combine-reduce operations ...
dimensionedScalar stabilise(const dimensionedScalar &x, const dimensionedScalar &y)
static void copy_n(Input input, Output result)
Somewhat equivalent to std::copy_n() but with templated loops.
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)
Apply the inplace binary reduction operation.
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &f1, const label comm)
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:131
void cmptMagSqr(Field< Type > &result, const UList< Type > &f1)
const_iterator cbegin() const noexcept
Return const_iterator (const pointer) to begin of VectorSpace.
Definition: VectorSpace.H:285
VectorSpace()=default
Default construct.
errorManip< error > abort(error &err)
Definition: errorManip.H:139
Scalar cmptPow(const Scalar s1, const Scalar s2)
Definition: scalarImpl.H:446
const ConstBlock< SubVector, BStart > block() const
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
static constexpr direction nComponents
Number of components in this vector space.
Definition: VectorSpace.H:114
void cmptMag(FieldField< Field, Type > &cf, const FieldField< Field, Type > &f)
Scalar cmptSqr(const Scalar s)
Definition: scalarImpl.H:476
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: scalarImpl.H:368
void fill(const Cmpt &s)
Assign all components to given value.
Definition: VectorSpaceI.H:157
static void eqOp(V1 &vs1, const V2 &vs2, EqOp eo)
Apply the inplace binary assignment operation to the components.
static void opSV(V &vs, const S &s, const V1 &vs1, BinaryOp bop)
Apply the binary operation between scalar and vector-space data and assign the result.
const dimensionedScalar c
Speed of light in a vacuum.
const Cmpt & component(const direction) const
Definition: VectorSpaceI.H:80
static void fill_n(Output result, const T &val)
Somewhat equivalent to std::fill_n() but with templated loops.
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 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:128
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:151
static void opVS(V &vs, const V1 &vs1, const S &s, BinaryOp bop)
Apply the binary operation between vector-space and scalar data and assign the result.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127