1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
29 #include "VectorSpaceM.H"
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
40 template<class Form, class Cmpt, int nCmpt>
41 inline VectorSpace<Form, Cmpt, nCmpt>::VectorSpace()
45 template<class Form, class Cmpt, int nCmpt>
46 inline VectorSpace<Form, Cmpt, nCmpt>::VectorSpace
48 const VectorSpace<Form, Cmpt, nCmpt>& vs
51 VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, eqOp<Cmpt>());
55 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
57 template<class Form, class Cmpt, int nCmpt>
58 inline label VectorSpace<Form, Cmpt, nCmpt>::size() const
64 template<class Form, class Cmpt, int nCmpt>
65 inline const Cmpt& VectorSpace<Form, Cmpt, nCmpt>::component
75 "VectorSpace<Form, Cmpt, nCmpt>::component(direction) const"
76 ) << "index out of range"
85 template<class Form, class Cmpt, int nCmpt>
86 inline Cmpt& VectorSpace<Form, Cmpt, nCmpt>::component
94 FatalErrorIn("VectorSpace<Form, Cmpt, nCmpt>::component(direction)")
95 << "index out of range"
104 template<class Form, class Cmpt, int nCmpt>
105 inline void VectorSpace<Form, Cmpt, nCmpt>::component
116 "VectorSpace<Form, Cmpt, nCmpt>::component(Cmpt&, direction) const"
117 ) << "index out of range"
118 << abort(FatalError);
126 template<class Form, class Cmpt, int nCmpt>
127 inline void VectorSpace<Form, Cmpt, nCmpt>::replace
138 "VectorSpace<Form, Cmpt, nCmpt>::"
139 "replace(direction, const Cmpt&) const"
140 ) << "index out of range"
141 << abort(FatalError);
149 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
151 template<class Form, class Cmpt, int nCmpt>
152 inline const Cmpt& VectorSpace<Form, Cmpt, nCmpt>::operator[]
162 "VectorSpace<Form, Cmpt, nCmpt>::operator[](direction d) const"
163 ) << "index out of range"
164 << abort(FatalError);
172 template<class Form, class Cmpt, int nCmpt>
173 inline Cmpt& VectorSpace<Form, Cmpt, nCmpt>::operator[]
181 FatalErrorIn("VectorSpace<Form, Cmpt, nCmpt>::operator[](direction d)")
182 << "index out of range"
183 << abort(FatalError);
191 template<class Form, class Cmpt, int nCmpt>
192 inline void VectorSpace<Form, Cmpt, nCmpt>::operator=
194 const VectorSpace<Form, Cmpt, nCmpt>& vs
197 VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, eqOp<Cmpt>());
201 template<class Form, class Cmpt, int nCmpt>
202 inline void VectorSpace<Form, Cmpt, nCmpt>::operator+=
204 const VectorSpace<Form, Cmpt, nCmpt>& vs
207 VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, plusEqOp<Cmpt>());
211 template<class Form, class Cmpt, int nCmpt>
212 inline void VectorSpace<Form, Cmpt, nCmpt>::operator-=
214 const VectorSpace<Form, Cmpt, nCmpt>& vs
217 VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, minusEqOp<Cmpt>());
221 template<class Form, class Cmpt, int nCmpt>
222 inline void VectorSpace<Form, Cmpt, nCmpt>::operator*=
227 VectorSpaceOps<nCmpt,0>::eqOpS(*this, s, multiplyEqOp2<Cmpt, scalar>());
231 template<class Form, class Cmpt, int nCmpt>
232 inline void VectorSpace<Form, Cmpt, nCmpt>::operator/=
237 VectorSpaceOps<nCmpt,0>::eqOpS(*this, s, divideEqOp2<Cmpt, scalar>());
241 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
243 template<class Form, class Cmpt, int nCmpt>
244 inline Cmpt& setComponent
246 VectorSpace<Form, Cmpt, nCmpt>& vs,
250 return vs.component(d);
254 template<class Form, class Cmpt, int nCmpt>
255 inline const Cmpt& component
257 const VectorSpace<Form, Cmpt, nCmpt>& vs,
261 return vs.component(d);
266 // Equivalent to outer-products between the Form and itself
268 template<class Form, class Cmpt, int nCmpt>
269 inline typename powProduct<Form, 0>::type pow
271 const VectorSpace<Form, Cmpt, nCmpt>&,
272 typename powProduct<Form, 0>::type
273 = pTraits<typename powProduct<Form, 0>::type>::zero
280 template<class Form, class Cmpt, int nCmpt>
281 inline typename powProduct<Form, 1>::type pow
283 const VectorSpace<Form, Cmpt, nCmpt>& v,
284 typename powProduct<Form, 1>::type
285 = pTraits<typename powProduct<Form, 1>::type>::zero
288 return static_cast<const Form&>(v);
292 // Form^2 = sqr(Form)
293 template<class Form, class Cmpt, int nCmpt>
294 inline typename powProduct<Form, 2>::type pow
296 const VectorSpace<Form, Cmpt, nCmpt>& v,
297 typename powProduct<Form, 2>::type
298 = pTraits<typename powProduct<Form, 2>::type>::zero
301 return sqr(static_cast<const Form&>(v));
305 template<class Form, class Cmpt, int nCmpt>
308 const VectorSpace<Form, Cmpt, nCmpt>& vs
311 scalar ms = magSqr(vs.v_[0]);
312 VectorSpaceOps<nCmpt,1>::SeqOp(ms, vs, plusEqMagSqrOp2<scalar, Cmpt>());
317 template<class Form, class Cmpt, int nCmpt>
320 const VectorSpace<Form, Cmpt, nCmpt>& vs
323 return ::sqrt(magSqr(static_cast<const Form&>(vs)));
327 template<class Form, class Cmpt, int nCmpt>
328 inline VectorSpace<Form, Cmpt, nCmpt> cmptMultiply
330 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
331 const VectorSpace<Form, Cmpt, nCmpt>& vs2
335 VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, cmptMultiplyOp<Cmpt>());
340 template<class Form, class Cmpt, int nCmpt>
341 inline VectorSpace<Form, Cmpt, nCmpt> cmptDivide
343 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
344 const VectorSpace<Form, Cmpt, nCmpt>& vs2
348 VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, cmptDivideOp<Cmpt>());
353 template<class Form, class Cmpt, int nCmpt>
354 inline Cmpt cmptSumMultiply
356 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
357 const VectorSpace<Form, Cmpt, nCmpt>& vs2
360 return cmptSum(cmptMultiply(vs1, vs2));
364 template<class Form, class Cmpt, int nCmpt>
365 inline VectorSpace<Form, Cmpt, nCmpt> stabilise
367 const VectorSpace<Form, Cmpt, nCmpt>& vs,
372 VectorSpaceOps<nCmpt,0>::opVS(v, vs, small, stabiliseOp<Cmpt>());
377 template<class Form, class Cmpt, int nCmpt>
380 const VectorSpace<Form, Cmpt, nCmpt>& vs
383 Cmpt cMax = vs.v_[0];
384 VectorSpaceOps<nCmpt,1>::SeqOp(cMax, vs, maxEqOp<Cmpt>());
389 template<class Form, class Cmpt, int nCmpt>
392 const VectorSpace<Form, Cmpt, nCmpt>& vs
395 Cmpt cMin = vs.v_[0];
396 VectorSpaceOps<nCmpt,1>::SeqOp(cMin, vs, minEqOp<Cmpt>());
401 template<class Form, class Cmpt, int nCmpt>
404 const VectorSpace<Form, Cmpt, nCmpt>& vs
408 VectorSpaceOps<nCmpt,1>::SeqOp(sum, vs, plusEqOp<Cmpt>());
413 template<class Form, class Cmpt, int nCmpt>
416 const VectorSpace<Form, Cmpt, nCmpt>& vs
419 return cmptSum(vs)/nCmpt;
423 template<class Form, class Cmpt, int nCmpt>
426 const VectorSpace<Form, Cmpt, nCmpt>& vs
430 VectorSpaceOps<nCmpt,0>::eqOp(v, vs, eqMagOp<Cmpt>());
435 template<class Form, class Cmpt, int nCmpt>
436 inline Form cmptStabilise
438 const VectorSpace<Form, Cmpt, nCmpt>& vs,
444 for (int i=0; i<nCmpt; i++)
446 if (mag(vs.v_[i]) < small)
448 v.v_[i] = sign(vs.v_[i])*value;
455 template<class Form, class Cmpt, int nCmpt>
458 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
459 const VectorSpace<Form, Cmpt, nCmpt>& vs2
463 VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, maxOp<Cmpt>());
468 template<class Form, class Cmpt, int nCmpt>
471 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
472 const VectorSpace<Form, Cmpt, nCmpt>& vs2
476 VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, minOp<Cmpt>());
481 template<class Form, class Cmpt, int nCmpt>
484 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
485 const VectorSpace<Form, Cmpt, nCmpt>& vs2
489 VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, minModOp<Cmpt>());
495 inline Type dot(const scalar s, const Type& t)
502 inline Type dot(const Type& t, const scalar s)
510 class Form1, class Cmpt1, int nCmpt1,
511 class Form2, class Cmpt2, int nCmpt2
513 inline typename innerProduct<Form1, Form2>::type dot
515 const VectorSpace<Form1, Cmpt1, nCmpt1>& t1,
516 const VectorSpace<Form2, Cmpt2, nCmpt2>& t2
519 return static_cast<const Form1&>(t1) & static_cast<const Form2&>(t2);
523 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
525 template<class Form, class Cmpt, int nCmpt>
526 inline Form operator-
528 const VectorSpace<Form, Cmpt, nCmpt>& vs
532 VectorSpaceOps<nCmpt,0>::eqOp(v, vs, eqMinusOp<Cmpt>());
537 template<class Form, class Cmpt, int nCmpt>
538 inline Form operator+
540 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
541 const VectorSpace<Form, Cmpt, nCmpt>& vs2
545 VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, plusOp<Cmpt>());
550 template<class Form, class Cmpt, int nCmpt>
551 inline Form operator-
553 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
554 const VectorSpace<Form, Cmpt, nCmpt>& vs2
558 VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, minusOp<Cmpt>());
563 template<class Form, class Cmpt, int nCmpt>
564 inline Form operator*
567 const VectorSpace<Form, Cmpt, nCmpt>& vs
571 VectorSpaceOps<nCmpt,0>::opSV(v, s, vs, multiplyOp3<Cmpt, scalar, Cmpt>());
576 template<class Form, class Cmpt, int nCmpt>
577 inline Form operator*
579 const VectorSpace<Form, Cmpt, nCmpt>& vs,
584 VectorSpaceOps<nCmpt,0>::opVS(v, vs, s, multiplyOp3<Cmpt, Cmpt, scalar>());
589 template<class Form, class Cmpt, int nCmpt>
590 inline Form operator/
592 const VectorSpace<Form, Cmpt, nCmpt>& vs,
597 VectorSpaceOps<nCmpt,0>::opVS(v, vs, s, divideOp3<Cmpt, Cmpt, scalar>());
602 template<class Form, class Cmpt, int nCmpt>
603 inline Form operator/
605 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
606 const VectorSpace<Form, Cmpt, nCmpt>& vs2
610 VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, divideOp<Cmpt>());
615 template<class Form, class Cmpt, int nCmpt>
616 inline Form operator/
619 const VectorSpace<Form, Cmpt, nCmpt>& vs
623 VectorSpaceOps<nCmpt,0>::opSV(v, s, vs, divideOp2<scalar, Cmpt>());
629 template<class Form, class Cmpt, int nCmpt>
630 inline Cmpt operator&&
632 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
633 const VectorSpace<Form, Cmpt, nCmpt>& vs2
636 Cmpt ddProd = vs1.v_[0]*vs2.v_[0];
637 for (int i=1; i<nCmpt; ++i)
639 ddProd += vs1.v_[i]*vs2.v_[i];
645 template<class Form, class Cmpt, int nCmpt>
646 inline bool operator==
648 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
649 const VectorSpace<Form, Cmpt, nCmpt>& vs2
653 for (int i=0; i<nCmpt; ++i)
655 if (!(eq &= (equal(vs1.v_[i], vs2.v_[i])))) break;
661 template<class Form, class Cmpt, int nCmpt>
662 inline bool operator!=
664 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
665 const VectorSpace<Form, Cmpt, nCmpt>& vs2
668 return !(vs1 == vs2);
672 template<class Form, class Cmpt, int nCmpt>
673 inline bool operator>
675 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
676 const VectorSpace<Form, Cmpt, nCmpt>& vs2
680 for (int i=0; i<nCmpt; ++i)
682 if (!(gt &= vs1.v_[i] > vs2.v_[i])) break;
688 template<class Form, class Cmpt, int nCmpt>
689 inline bool operator<
691 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
692 const VectorSpace<Form, Cmpt, nCmpt>& vs2
696 for (int i=0; i<nCmpt; ++i)
698 if (!(lt &= vs1.v_[i] < vs2.v_[i])) break;
704 template<class Form, class Cmpt, int nCmpt>
705 inline bool operator>=
707 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
708 const VectorSpace<Form, Cmpt, nCmpt>& vs2
715 template<class Form, class Cmpt, int nCmpt>
716 inline bool operator<=
718 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
719 const VectorSpace<Form, Cmpt, nCmpt>& vs2
726 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
728 } // End namespace Foam
730 // ************************************************************************* //