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 | Copyright (C) 2011-2016 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>
53 (
55 )
56 {
58 }
59 
60 
61 template<class Form, class Cmpt, direction Ncmpts>
62 template<class Form2, class Cmpt2>
64 (
66 )
67 {
69 }
70 
71 
72 template<class Form, class Cmpt, direction Ncmpts>
73 template<class SubVector, direction BStart>
74 inline
76 (
77  const vsType& vs
78 )
79 :
80  vs_(vs)
81 {
82  static_assert
83  (
84  vsType::nComponents >= BStart + nComponents,
85  "Requested block size > VectorSpace size"
86  );
87 }
88 
89 
90 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
91 
92 template<class Form, class Cmpt, direction Ncmpts>
94 {
95  return Ncmpts;
96 }
97 
98 
99 template<class Form, class Cmpt, direction Ncmpts>
101 (
102  const direction d
103 ) const
104 {
105  #ifdef FULLDEBUG
106  if (d >= Ncmpts)
107  {
109  << "index out of range"
110  << abort(FatalError);
111  }
112  #endif
113 
114  return v_[d];
115 }
116 
117 
118 template<class Form, class Cmpt, direction Ncmpts>
120 (
121  const direction d
122 )
123 {
124  #ifdef FULLDEBUG
125  if (d >= Ncmpts)
126  {
128  << "index out of range"
129  << abort(FatalError);
130  }
131  #endif
132 
133  return v_[d];
134 }
135 
136 
137 template<class Form, class Cmpt, direction Ncmpts>
139 (
140  Cmpt& c,
141  const direction d
142 ) const
143 {
144  #ifdef FULLDEBUG
145  if (d >= Ncmpts)
146  {
148  << "index out of range"
149  << abort(FatalError);
150  }
151  #endif
152 
153  c = v_[d];
154 }
155 
156 
157 template<class Form, class Cmpt, direction Ncmpts>
159 (
160  const direction d,
161  const Cmpt& c
162 )
163 {
164  #ifdef FULLDEBUG
165  if (d >= Ncmpts)
166  {
168  << "index out of range"
169  << abort(FatalError);
170  }
171  #endif
172 
173  v_[d] = c;
174 }
175 
176 
177 template<class Form, class Cmpt, direction Ncmpts>
179 {
180  Form v;
182  return v;
183 }
184 
185 
186 template<class Form, class Cmpt, direction Ncmpts>
187 template<class SubVector, direction BStart>
188 inline const typename VectorSpace<Form, Cmpt, Ncmpts>::template
191 {
192  return *this;
193 }
194 
195 
196 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
197 
198 template<class Form, class Cmpt, direction Ncmpts>
199 inline const Cmpt& VectorSpace<Form, Cmpt, Ncmpts>::operator[]
200 (
201  const direction d
202 ) const
203 {
204  #ifdef FULLDEBUG
205  if (d >= Ncmpts)
206  {
208  << "index out of range"
209  << abort(FatalError);
210  }
211  #endif
212 
213  return v_[d];
214 }
215 
216 
217 template<class Form, class Cmpt, direction Ncmpts>
218 inline Cmpt& VectorSpace<Form, Cmpt, Ncmpts>::operator[]
219 (
220  const direction d
221 )
222 {
223  #ifdef FULLDEBUG
224  if (d >= Ncmpts)
225  {
227  << "index out of range"
228  << abort(FatalError);
229  }
230  #endif
231 
232  return v_[d];
233 }
234 
235 
236 template<class Form, class Cmpt, direction Ncmpts>
237 template<class SubVector, direction BStart>
238 inline const Cmpt&
241 (
242  const direction i
243 ) const
244 {
245  #ifdef FULLDEBUG
246  if (i >= Ncmpts)
247  {
249  << "index out of range"
250  << abort(FatalError);
251  }
252  #endif
253 
254  return vs_[BStart + i];
255 }
256 
257 
258 template<class Form, class Cmpt, direction Ncmpts>
259 template<class SubVector, direction BStart>
260 inline const Cmpt&
263 (
264  const direction i,
265  const direction j
266 ) const
267 {
268  #ifdef FULLDEBUG
269  if (i >= Ncmpts)
270  {
272  << "index out of range"
273  << abort(FatalError);
274  }
275 
276  if (j != 0)
277  {
279  << "j != 0"
280  << abort(FatalError);
281  }
282  #endif
283 
284  return vs_[BStart + i];
285 }
286 
287 
288 template<class Form, class Cmpt, direction Ncmpts>
289 inline void VectorSpace<Form, Cmpt, Ncmpts>::operator=
290 (
292 )
293 {
295 }
296 
297 
298 template<class Form, class Cmpt, direction Ncmpts>
299 inline void VectorSpace<Form, Cmpt, Ncmpts>::operator+=
300 (
302 )
303 {
305 }
306 
307 
308 template<class Form, class Cmpt, direction Ncmpts>
309 inline void VectorSpace<Form, Cmpt, Ncmpts>::operator-=
310 (
312 )
313 {
315 }
316 
317 
318 template<class Form, class Cmpt, direction Ncmpts>
320 {
322 }
323 
324 
325 template<class Form, class Cmpt, direction Ncmpts>
326 inline void VectorSpace<Form, Cmpt, Ncmpts>::operator*=
327 (
328  const scalar s
329 )
330 {
332 }
333 
334 
335 template<class Form, class Cmpt, direction Ncmpts>
336 inline void VectorSpace<Form, Cmpt, Ncmpts>::operator/=
337 (
338  const scalar s
339 )
340 {
342 }
343 
344 
345 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
346 
347 template<class Form, class Cmpt, direction Ncmpts>
348 inline Cmpt& setComponent
349 (
351  const direction d
352 )
353 {
354  return vs.component(d);
355 }
356 
357 
358 template<class Form, class Cmpt, direction Ncmpts>
359 inline const Cmpt& component
360 (
362  const direction d
363 )
364 {
365  return vs.component(d);
366 }
367 
368 
369 // Powers of a Form
370 // Equivalent to outer-products between the Form and itself
371 // Form^0 = 1.0
372 template<class Form, class Cmpt, direction Ncmpts>
373 inline typename powProduct<Form, 0>::type pow
374 (
378 )
379 {
380  return 1.0;
381 }
382 
383 // Form^1 = Form
384 template<class Form, class Cmpt, direction Ncmpts>
385 inline typename powProduct<Form, 1>::type pow
386 (
390 )
391 {
392  return static_cast<const Form&>(v);
393 }
394 
395 
396 // Form^2 = sqr(Form)
397 template<class Form, class Cmpt, direction Ncmpts>
398 inline typename powProduct<Form, 2>::type pow
399 (
403 )
404 {
405  return sqr(static_cast<const Form&>(v));
406 }
407 
408 
409 template<class Form, class Cmpt, direction Ncmpts>
410 inline scalar magSqr
411 (
413 )
414 {
415  scalar ms = magSqr(vs.v_[0]);
417  return ms;
418 }
419 
420 
421 template<class Form, class Cmpt, direction Ncmpts>
422 inline scalar mag
423 (
425 )
426 {
427  return ::sqrt(magSqr(static_cast<const Form&>(vs)));
428 }
429 
430 
431 template<class Form, class Cmpt, direction Ncmpts>
433 (
436 )
437 {
438  Form v;
440  return v;
441 }
442 
443 
444 template<class Form, class Cmpt, direction Ncmpts>
446 (
449 )
450 {
451  Form v;
453  return v;
454 }
455 
456 
457 template<class Form, class Cmpt, direction Ncmpts>
459 (
462 )
463 {
464  Form v;
466  return v;
467 }
468 
469 
470 template<class Form, class Cmpt, direction Ncmpts>
472 (
474  const Cmpt& small
475 )
476 {
477  Form v;
479  return v;
480 }
481 
482 
483 template<class Form, class Cmpt, direction Ncmpts>
484 inline Cmpt cmptMax
485 (
487 )
488 {
489  Cmpt cMax = vs.v_[0];
491  return cMax;
492 }
493 
494 
495 template<class Form, class Cmpt, direction Ncmpts>
496 inline Cmpt cmptMin
497 (
499 )
500 {
501  Cmpt cMin = vs.v_[0];
503  return cMin;
504 }
505 
506 
507 template<class Form, class Cmpt, direction Ncmpts>
508 inline Cmpt cmptSum
509 (
511 )
512 {
513  Cmpt sum = vs.v_[0];
515  return sum;
516 }
517 
518 
519 template<class Form, class Cmpt, direction Ncmpts>
520 inline Cmpt cmptAv
521 (
523 )
524 {
525  return cmptSum(vs)/Ncmpts;
526 }
527 
528 template<class Form, class Cmpt, direction Ncmpts>
529 inline Cmpt cmptProduct
530 (
532 )
533 {
534  Cmpt product = vs.v_[0];
536  return product;
537 }
538 
539 
540 template<class Form, class Cmpt, direction Ncmpts>
541 inline Form cmptMag
542 (
544 )
545 {
546  Form v;
548  return v;
549 }
550 
551 
552 template<class Form, class Cmpt, direction Ncmpts>
553 inline Form max
554 (
557 )
558 {
559  Form v;
561  return v;
562 }
563 
564 
565 template<class Form, class Cmpt, direction Ncmpts>
566 inline Form min
567 (
570 )
571 {
572  Form v;
574  return v;
575 }
576 
577 
578 template<class Form, class Cmpt, direction Ncmpts>
579 inline Form minMod
580 (
583 )
584 {
585  Form v;
587  return v;
588 }
589 
590 
591 template<class Type>
592 inline Type dot(const scalar s, const Type& t)
593 {
594  return s * t;
595 }
596 
597 
598 template<class Type>
599 inline Type dot(const Type& t, const scalar s)
600 {
601  return t * s;
602 }
603 
604 
605 template
606 <
607  class Form1, class Cmpt1, direction Ncmpts1,
608  class Form2, class Cmpt2, direction Ncmpts2
609 >
611 (
614 )
615 {
616  return static_cast<const Form1&>(t1) & static_cast<const Form2&>(t2);
617 }
618 
619 
620 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
621 
622 template<class Form, class Cmpt, direction Ncmpts>
623 inline Form operator-
624 (
626 )
627 {
628  Form v;
630  return v;
631 }
632 
633 
634 template<class Form, class Cmpt, direction Ncmpts>
635 inline Form operator+
636 (
639 )
640 {
641  Form v;
643  return v;
644 }
645 
646 template<class Form, class Cmpt, direction Ncmpts>
647 inline Form operator-
648 (
651 )
652 {
653  Form v;
655  return v;
656 }
657 
658 
659 template<class Form, class Cmpt, direction Ncmpts>
660 inline Form operator*
661 (
662  scalar s,
664 )
665 {
666  Form v;
668  return v;
669 }
670 
671 
672 template<class Form, class Cmpt, direction Ncmpts>
673 inline Form operator*
674 (
676  scalar s
677 )
678 {
679  Form v;
681  return v;
682 }
683 
684 
685 template<class Form, class Cmpt, direction Ncmpts>
686 inline Form operator/
687 (
689  scalar s
690 )
691 {
692  Form v;
694  return v;
695 }
696 
697 /*
698 template<class Form, class Cmpt, direction Ncmpts>
699 inline Form operator/
700 (
701  const VectorSpace<Form, Cmpt, Ncmpts>& vs1,
702  const VectorSpace<Form, Cmpt, Ncmpts>& vs2
703 )
704 {
705  Form v;
706  VectorSpaceOps<Ncmpts,0>::op(v, vs1, vs2, divideOp<Cmpt>());
707  return v;
708 }
709 
710 
711 template<class Form, class Cmpt, direction Ncmpts>
712 inline Form operator/
713 (
714  scalar s,
715  const VectorSpace<Form, Cmpt, Ncmpts>& vs
716 )
717 {
718  Form v;
719  VectorSpaceOps<Ncmpts,0>::opSV(v, s, vs, divideOp2<scalar, Cmpt>());
720  return v;
721 }
722 */
723 
724 
725 template<class Form, class Cmpt, direction Ncmpts>
726 inline Cmpt operator&&
727 (
730 )
731 {
732  Cmpt ddProd = vs1.v_[0]*vs2.v_[0];
733  for (direction i=1; i<Ncmpts; ++i)
734  {
735  ddProd += vs1.v_[i]*vs2.v_[i];
736  }
737  return ddProd;
738 }
739 
740 
741 template<class Form, class Cmpt, direction Ncmpts>
742 inline bool operator==
743 (
746 )
747 {
748  bool eq = true;
749  for (direction i=0; i<Ncmpts; ++i)
750  {
751  if (!(eq &= (equal(vs1.v_[i], vs2.v_[i])))) break;
752  }
753  return eq;
754 }
755 
756 
757 template<class Form, class Cmpt, direction Ncmpts>
758 inline bool operator!=
759 (
762 )
763 {
764  return !(vs1 == vs2);
765 }
766 
767 
768 template<class Form, class Cmpt, direction Ncmpts>
769 inline bool operator>
770 (
773 )
774 {
775  bool gt = true;
776  for (direction i=0; i<Ncmpts; ++i)
777  {
778  if (!(gt &= vs1.v_[i] > vs2.v_[i])) break;
779  }
780  return gt;
781 }
782 
783 
784 template<class Form, class Cmpt, direction Ncmpts>
785 inline bool operator<
786 (
789 )
790 {
791  bool lt = true;
792  for (direction i=0; i<Ncmpts; ++i)
793  {
794  if (!(lt &= vs1.v_[i] < vs2.v_[i])) break;
795  }
796  return lt;
797 }
798 
799 
800 template<class Form, class Cmpt, direction Ncmpts>
801 inline bool operator>=
802 (
805 )
806 {
807  return !(vs1 < vs2);
808 }
809 
810 
811 template<class Form, class Cmpt, direction Ncmpts>
812 inline bool operator<=
813 (
816 )
817 {
818  return !(vs1 > vs2);
819 }
820 
821 
822 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
823 
824 } // End namespace Foam
825 
826 // ************************************************************************* //
Const sub-block type.
Definition: VectorSpace.H:126
static void opVS(V &vs, const V1 &vs1, const S &s, Op o)
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:530
const ConstBlock< SubVector, BStart > block() const
error FatalError
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
static void eqOpS(V &vs, const S &s, EqOp eo)
tmp< DimensionedField< typename DimensionedField< Type, GeoMesh >::cmptType, GeoMesh >> cmptAv(const DimensionedField< Type, GeoMesh > &df)
void operator=(const VectorSpace< Form, Cmpt, Ncmpts > &)
Definition: VectorSpaceI.H:290
dimensionedSymmTensor sqr(const dimensionedVector &dv)
Traits classes for inner and outer products of primitives.
VectorSpace()
Construct null.
Definition: VectorSpaceI.H:40
Cmpt cmptSum(const VectorSpace< Form, Cmpt, Ncmpts > &vs)
Definition: VectorSpaceI.H:509
dimensionedScalar sqrt(const dimensionedScalar &ds)
Traits class for primitives.
Definition: pTraits.H:50
Templated vector space.
Definition: VectorSpace.H:53
Combination-Reduction operation for a parallel run.
void replace(const direction, const Cmpt &)
Definition: VectorSpaceI.H:159
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 > &)
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
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.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: ops.H:70
static void SeqOp(S &s, const V &vs, EqOp eo)
static const zero Zero
Definition: zero.H:91
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Scalar cmptPow(const Scalar s1, const Scalar s2)
Definition: Scalar.H:258
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
void cmptMag(FieldField< Field, Type > &cf, const FieldField< Field, Type > &f)
dimensioned< scalar > magSqr(const dimensioned< Type > &)
static direction size()
Return the number of elements in the VectorSpace = Ncmpts.
Definition: VectorSpaceI.H:93
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
friend Ostream & operator(Ostream &, const VectorSpace< Form, Cmpt, Ncmpts > &)
symmTypeOfRank< typename pTraits< arg1 >::cmptType, arg2 *direction(pTraits< arg1 >::rank) >::type type
Definition: products.H:136
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
Scalar minMod(const Scalar s1, const Scalar s2)
Definition: Scalar.H:176
tmp< DimensionedField< scalar, GeoMesh > > stabilise(const DimensionedField< scalar, GeoMesh > &dsf, const dimensioned< scalar > &ds)
const Cmpt & component(const direction) const
Definition: VectorSpaceI.H:101
bool equal(const T &s1, const T &s2)
Definition: doubleFloat.H:62
static void opSV(V &vs, const S &s, const V1 &vs1, Op o)
const dimensionedScalar c
Speed of light in a vacuum.
static Form uniform(const Cmpt &s)
Return a VectorSpace with all elements = s.
Definition: VectorSpaceI.H:178
A class representing the concept of 0 used to avoid unnecessary manipulations for objects that are kn...
Definition: zero.H:49
dimensioned< scalar > mag(const dimensioned< Type > &)
static void eqOp(V1 &vs1, const V2 &vs2, EqOp eo)
void cmptMin(FieldField< Field, typename FieldField< Field, Type >::cmptType > &cf, const FieldField< Field, Type > &f)
static void op(V &vs, const V1 &vs1, const V1 &vs2, Op o)
label & setComponent(label &l, const direction)
Definition: label.H:79
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
typeOfRank< typename pTraits< arg1 >::cmptType, direction(pTraits< arg1 >::rank)+direction(pTraits< arg2 >::rank)-2 >::type type
Definition: products.H:115
Cmpt v_[Ncmpts]
The components of this vector space.
Definition: VectorSpace.H:81
Namespace for OpenFOAM.