VectorSpaceI.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 \*---------------------------------------------------------------------------*/
25 
26 #include "error.H"
27 #include "products.H"
28 #include "VectorSpaceOps.H"
29 #include "ops.H"
30 #include <type_traits>
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
38 
39 template<class Form, class Cmpt, direction Ncmpts>
41 {}
42 
43 
44 template<class Form, class Cmpt, direction Ncmpts>
46 {
48 }
49 
50 
51 template<class Form, class Cmpt, direction Ncmpts>
52 template<class Form2, class Cmpt2>
54 (
56 )
57 {
59 }
60 
61 
62 template<class Form, class Cmpt, direction Ncmpts>
63 template<class SubVector, direction BStart>
64 inline
66 (
67  const vsType& vs
68 )
69 :
70  vs_(vs)
71 {
72  static_assert
73  (
74  vsType::nComponents >= BStart + nComponents,
75  "Requested block size > VectorSpace size"
76  );
77 }
78 
79 
80 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
81 
82 template<class Form, class Cmpt, direction Ncmpts>
84 {
85  return Ncmpts;
86 }
87 
88 
89 template<class Form, class Cmpt, direction Ncmpts>
91 (
92  const direction d
93 ) const
94 {
95  #ifdef FULLDEBUG
96  if (d >= Ncmpts)
97  {
99  << "index out of range"
100  << abort(FatalError);
101  }
102  #endif
103 
104  return v_[d];
105 }
106 
107 
108 template<class Form, class Cmpt, direction Ncmpts>
110 (
111  const direction d
112 )
113 {
114  #ifdef FULLDEBUG
115  if (d >= Ncmpts)
116  {
118  << "index out of range"
119  << abort(FatalError);
120  }
121  #endif
122 
123  return v_[d];
124 }
125 
126 
127 template<class Form, class Cmpt, direction Ncmpts>
129 (
130  Cmpt& c,
131  const direction d
132 ) const
133 {
134  #ifdef FULLDEBUG
135  if (d >= Ncmpts)
136  {
138  << "index out of range"
139  << abort(FatalError);
140  }
141  #endif
142 
143  c = v_[d];
144 }
145 
146 
147 template<class Form, class Cmpt, direction Ncmpts>
149 (
150  const direction d,
151  const Cmpt& c
152 )
153 {
154  #ifdef FULLDEBUG
155  if (d >= Ncmpts)
156  {
158  << "index out of range"
159  << abort(FatalError);
160  }
161  #endif
162 
163  v_[d] = c;
164 }
165 
166 
167 template<class Form, class Cmpt, direction Ncmpts>
169 {
170  Form v;
172  return v;
173 }
174 
175 
176 template<class Form, class Cmpt, direction Ncmpts>
177 template<class SubVector, direction BStart>
179  ConstBlock<SubVector, BStart>
181 {
182  return *this;
183 }
184 
185 
186 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
187 
188 template<class Form, class Cmpt, direction Ncmpts>
190 (
191  const direction d
192 ) const
193 {
194  #ifdef FULLDEBUG
195  if (d >= Ncmpts)
196  {
198  << "index out of range"
200  }
201  #endif
202 
203  return v_[d];
204 }
205 
206 
207 template<class Form, class Cmpt, direction Ncmpts>
209 (
210  const direction d
211 )
212 {
213  #ifdef FULLDEBUG
214  if (d >= Ncmpts)
215  {
217  << "index out of range"
218  << abort(FatalError);
219  }
220  #endif
221 
222  return v_[d];
223 }
224 
225 
226 template<class Form, class Cmpt, direction Ncmpts>
227 template<class SubVector, direction BStart>
228 inline const Cmpt&
230 ConstBlock<SubVector, BStart>::operator[]
231 (
232  const direction i
233 ) const
234 {
235  #ifdef FULLDEBUG
236  if (i >= Ncmpts)
237  {
239  << "index out of range"
240  << abort(FatalError);
241  }
242  #endif
243 
244  return vs_[BStart + i];
245 }
246 
247 
248 template<class Form, class Cmpt, direction Ncmpts>
249 template<class SubVector, direction BStart>
250 inline const Cmpt&
253 (
254  const direction i,
255  const direction j
256 ) const
257 {
258  #ifdef FULLDEBUG
259  if (i >= Ncmpts)
260  {
262  << "index out of range"
263  << abort(FatalError);
264  }
265 
266  if (j != 0)
267  {
269  << "j != 0"
270  << abort(FatalError);
271  }
272  #endif
273 
274  return vs_[BStart + i];
275 }
276 
277 
278 template<class Form, class Cmpt, direction Ncmpts>
280 (
282 )
283 {
285 }
286 
287 
288 template<class Form, class Cmpt, direction Ncmpts>
290 (
292 )
293 {
295 }
296 
297 
298 template<class Form, class Cmpt, direction Ncmpts>
300 {
302 }
303 
304 
305 template<class Form, class Cmpt, direction Ncmpts>
306 template<class Type, class Enable>
308 (
309  const Type s
310 )
311 {
313 }
314 
315 
316 template<class Form, class Cmpt, direction Ncmpts>
317 template<class Type, class Enable>
319 (
320  const Type s
321 )
322 {
324 }
325 
326 
327 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
328 
329 template<class Form, class Cmpt, direction Ncmpts>
330 inline Cmpt& setComponent
331 (
333  const direction d
334 )
335 {
336  return vs.component(d);
337 }
338 
339 
340 template<class Form, class Cmpt, direction Ncmpts>
341 inline const Cmpt& component
342 (
344  const direction d
345 )
346 {
347  return vs.component(d);
348 }
349 
350 
351 // Powers of a Form
352 // Equivalent to outer-products between the Form and itself
353 // Form^0 = 1.0
354 template<class Form, class Cmpt, direction Ncmpts>
356 (
358  typename powProduct<Form, 0>::type =
360 )
361 {
362  return 1.0;
363 }
364 
365 // Form^1 = Form
366 template<class Form, class Cmpt, direction Ncmpts>
368 (
370  typename powProduct<Form, 1>::type =
372 )
373 {
374  return static_cast<const Form&>(v);
375 }
376 
377 
378 // Form^2 = sqr(Form)
379 template<class Form, class Cmpt, direction Ncmpts>
381 (
383  typename powProduct<Form, 2>::type =
385 )
386 {
387  return sqr(static_cast<const Form&>(v));
388 }
389 
390 
391 template<class Form, class Cmpt, direction Ncmpts>
392 inline auto magSqr
393 (
395 )
396 {
397  auto ms = magSqr(vs.v_[0]);
399  (
400  ms,
401  vs,
402  plusEqMagSqrOp2<decltype(ms), Cmpt>()
403  );
404  return ms;
405 }
406 
407 
408 template<class Form, class Cmpt, direction Ncmpts>
409 inline auto mag
410 (
412 )
413 {
414  return sqrt(magSqr(static_cast<const Form&>(vs)));
415 }
416 
417 
418 template<class Form, class Cmpt, direction Ncmpts>
419 inline Form normalised
420 (
422 )
423 {
424  const auto magVs = mag(vs);
425  return magVs > 0 ? vs/magVs : Zero;
426 }
427 
428 
429 template<class Form, class Cmpt, direction Ncmpts>
431 (
434 )
435 {
436  Form v;
438  return v;
439 }
440 
441 
442 template<class Form, class Cmpt, direction Ncmpts>
444 (
447 )
448 {
449  Form v;
451  return v;
452 }
453 
454 
455 template<class Form, class Cmpt, direction Ncmpts>
457 (
460 )
461 {
462  Form v;
464  return v;
465 }
466 
467 
468 template<class Form, class Cmpt, direction Ncmpts>
470 (
472  const Cmpt& small
473 )
474 {
475  Form v;
477  return v;
478 }
479 
480 
481 template<class Form, class Cmpt, direction Ncmpts>
482 inline Cmpt cmptMax
483 (
485 )
486 {
487  Cmpt cMax = vs.v_[0];
489  return cMax;
490 }
491 
492 
493 template<class Form, class Cmpt, direction Ncmpts>
494 inline Cmpt cmptMin
495 (
497 )
498 {
499  Cmpt cMin = vs.v_[0];
501  return cMin;
502 }
503 
504 
505 template<class Form, class Cmpt, direction Ncmpts>
506 inline Cmpt cmptSum
507 (
509 )
510 {
511  Cmpt sum = vs.v_[0];
513  return sum;
514 }
515 
516 
517 template<class Form, class Cmpt, direction Ncmpts>
518 inline Cmpt cmptAv
519 (
521 )
522 {
523  return cmptSum(vs)/Ncmpts;
524 }
525 
526 template<class Form, class Cmpt, direction Ncmpts>
527 inline Cmpt cmptProduct
528 (
530 )
531 {
532  Cmpt product = vs.v_[0];
534  return product;
535 }
536 
537 
538 template<class Form, class Cmpt, direction Ncmpts>
539 inline Form cmptSqr
540 (
542 )
543 {
544  Form v;
546  return v;
547 }
548 
549 
550 template<class Form, class Cmpt, direction Ncmpts>
551 inline Form cmptMag
552 (
554 )
555 {
556  Form v;
558  return v;
559 }
560 
561 
562 template<class Form, class Cmpt, direction Ncmpts>
563 inline Form max
564 (
567 )
568 {
569  Form v;
571  return v;
572 }
573 
574 
575 template<class Form, class Cmpt, direction Ncmpts>
576 inline Form min
577 (
580 )
581 {
582  Form v;
584  return v;
585 }
586 
587 
588 template<class Form, class Cmpt, direction Ncmpts>
589 inline Form minMod
590 (
593 )
594 {
595  Form v;
597  return v;
598 }
599 
600 
601 template
602 <
603  class Form1, class Cmpt1, direction Ncmpts1,
604  class Form2, class Cmpt2, direction Ncmpts2
605 >
607 (
610 )
611 {
612  return static_cast<const Form1&>(t1) & static_cast<const Form2&>(t2);
613 }
614 
615 
616 template<class Form, class Cmpt, direction Ncmpts>
618 {
619  direction index = 0;
620  for (direction i=1; i<Ncmpts; ++i)
621  {
622  index = vs[index] > vs[i] ? index : i;
623  }
624  return index;
625 }
626 
627 
628 template<class Form, class Cmpt, direction Ncmpts>
630 {
631  direction index = 0;
632  for (direction i=1; i<Ncmpts; ++i)
633  {
634  index = vs[index] < vs[i] ? index : i;
635  }
636  return index;
637 }
638 
639 
640 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
641 
642 template<class Form, class Cmpt, direction Ncmpts>
643 inline Form operator-
644 (
646 )
647 {
648  Form v;
650  return v;
651 }
652 
653 
654 template<class Form, class Cmpt, direction Ncmpts>
655 inline Form operator+
656 (
659 )
660 {
661  Form v;
663  return v;
664 }
665 
666 template<class Form, class Cmpt, direction Ncmpts>
667 inline Form operator-
668 (
671 )
672 {
673  Form v;
675  return v;
676 }
677 
678 
679 template
680 <
681  class Form,
682  class Cmpt,
683  direction Ncmpts,
684  class Type,
685  class Enable = EnableRank0<Type>
686 >
687 inline Form operator*
688 (
689  Type s,
691 )
692 {
693  Form v;
695  return v;
696 }
697 
698 
699 template
700 <
701  class Form,
702  class Cmpt,
703  direction Ncmpts,
704  class Type,
705  class Enable = EnableRank0<Type>
706 >
707 inline Form operator*
708 (
710  Type s
711 )
712 {
713  Form v;
715  return v;
716 }
717 
718 
719 template
720 <
721  class Form,
722  class Cmpt,
723  direction Ncmpts,
724  class Type,
725  class Enable = EnableRank0<Type>
726 >
727 inline Form operator/
728 (
730  Type s
731 )
732 {
733  Form v;
735  return v;
736 }
737 
738 
739 template<class Form, class Cmpt, direction Ncmpts>
740 inline Cmpt operator&&
741 (
744 )
745 {
746  Cmpt ddProd = vs1.v_[0]*vs2.v_[0];
747  for (direction i=1; i<Ncmpts; ++i)
748  {
749  ddProd += vs1.v_[i]*vs2.v_[i];
750  }
751  return ddProd;
752 }
753 
754 
755 template<class Form, class Cmpt, direction Ncmpts>
756 inline bool operator==
757 (
760 )
761 {
762  bool eq = true;
763  for (direction i=0; i<Ncmpts; ++i)
764  {
765  if (!(eq &= (equal(vs1.v_[i], vs2.v_[i])))) break;
766  }
767  return eq;
768 }
769 
770 
771 template<class Form, class Cmpt, direction Ncmpts>
772 inline bool operator!=
773 (
776 )
777 {
778  return !(vs1 == vs2);
779 }
780 
781 
782 template<class Form, class Cmpt, direction Ncmpts>
783 inline bool operator>
784 (
787 )
788 {
789  bool gt = true;
790  for (direction i=0; i<Ncmpts; ++i)
791  {
792  if (!(gt &= vs1.v_[i] > vs2.v_[i])) break;
793  }
794  return gt;
795 }
796 
797 
798 template<class Form, class Cmpt, direction Ncmpts>
799 inline bool operator<
800 (
803 )
804 {
805  bool lt = true;
806  for (direction i=0; i<Ncmpts; ++i)
807  {
808  if (!(lt &= vs1.v_[i] < vs2.v_[i])) break;
809  }
810  return lt;
811 }
812 
813 
814 template<class Form, class Cmpt, direction Ncmpts>
815 inline bool operator>=
816 (
819 )
820 {
821  return !(vs1 < vs2);
822 }
823 
824 
825 template<class Form, class Cmpt, direction Ncmpts>
826 inline bool operator<=
827 (
830 )
831 {
832  return !(vs1 > vs2);
833 }
834 
835 
836 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
837 
838 } // End namespace Foam
839 
840 // ************************************************************************* //
static void opVS(V &vs, const V1 &vs1, const S &s, Op o)
static void opSV(V &vs, const S &s, const V1 &vs1, Op o)
static void SeqOp(S &s, const V &vs, EqOp eo)
static void eqOpS(V &vs, const S &s, EqOp eo)
static void eqOp(V1 &vs1, const V2 &vs2, EqOp eo)
static void op(V &vs, const V1 &vs1, const V1 &vs2, Op o)
static const direction nComponents
Number of components in this vector space.
Definition: VectorSpace.H:142
Templated vector space.
Definition: VectorSpace.H:85
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 max
Definition: VectorSpace.H:120
const ConstBlock< SubVector, BStart > block() const
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
static const Form min
Definition: VectorSpace.H:121
static direction size()
Return the number of elements in the VectorSpace = Ncmpts.
Definition: VectorSpaceI.H:83
void operator=(const Foam::zero)
Definition: VectorSpaceI.H:299
Definition: ops.H:70
typeOfRank< typename pTraits< arg1 >::cmptType, direction(pTraits< arg1 >::rank)+direction(pTraits< arg2 >::rank) - 2 >::type type
Definition: products.H:115
Traits class for primitives.
Definition: pTraits.H:53
symmTypeOfRank< typename pTraits< arg1 >::cmptType, arg2 *direction(pTraits< arg1 >::rank) >::type type
Definition: products.H:136
A class representing the concept of 0 used to avoid unnecessary manipulations for objects that are kn...
Definition: zero.H:50
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
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))
const dimensionedScalar c
Speed of light in a vacuum.
Namespace for OpenFOAM.
static const zero Zero
Definition: zero.H:97
bool equal(const T &s1, const T &s2)
Definition: doubleFloat.H:62
tmp< DimensionedField< scalar, GeoMesh > > stabilise(const DimensionedField< scalar, GeoMesh > &dsf, const dimensioned< scalar > &ds)
dimensionedSymmTensor sqr(const dimensionedVector &dv)
label findMax(const ListType &, const label start=0)
Find index of max element (and larger than given element).
Cmpt cmptProduct(const VectorSpace< Form, Cmpt, Ncmpts > &vs)
Definition: VectorSpaceI.H:528
label & setComponent(label &l, const direction)
Definition: label.H:86
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
errorManip< error > abort(error &err)
Definition: errorManip.H:131
void cmptMin(FieldField< Field, typename FieldField< Field, Type >::cmptType > &cf, const FieldField< Field, Type > &f)
void dot(FieldField< Field1, typename innerProduct< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
Scalar minMod(const Scalar s1, const Scalar s2)
Definition: Scalar.H:228
tmp< DimensionedField< typename DimensionedField< Type, GeoMesh >::cmptType, GeoMesh >> cmptAv(const DimensionedField< Type, GeoMesh > &df)
dimensionedScalar sqrt(const dimensionedScalar &ds)
dimensionSet normalised(const dimensionSet &)
Definition: dimensionSet.C:510
dimensioned< scalar > mag(const dimensioned< Type > &)
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
dimensionSet cmptMag(const dimensionSet &)
Definition: dimensionSet.C:296
void cmptMax(FieldField< Field, typename FieldField< Field, Type >::cmptType > &cf, const FieldField< Field, Type > &f)
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
label findMin(const ListType &, const label start=0)
Find index of min element (and less than given element).
Scalar cmptPow(const Scalar s1, const Scalar s2)
Definition: Scalar.H:308
error FatalError
Cmpt cmptSum(const VectorSpace< Form, Cmpt, Ncmpts > &vs)
Definition: VectorSpaceI.H:507
dimensioned< scalar > magSqr(const dimensioned< Type > &)
uint8_t direction
Definition: direction.H:45
Scalar cmptSqr(const Scalar s)
Definition: Scalar.H:338
Combination-Reduction operation for a parallel run.
Traits classes for inner and outer products of primitives.