Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / src / OpenFOAM / primitives / VectorSpace / VectorSpaceI.H
blob849362342d3d080e5a72cadeea24575b395b039a
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2004-2010 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
13     the Free Software Foundation, either version 3 of the License, or
14     (at your 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
19     for more details.
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/>.
24 \*---------------------------------------------------------------------------*/
26 #include "error.H"
27 #include "products.H"
28 #include "VectorSpaceM.H"
29 #include "ops.H"
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 namespace Foam
36 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
38 template<class Form, class Cmpt, int nCmpt>
39 inline VectorSpace<Form, Cmpt, nCmpt>::VectorSpace()
43 template<class Form, class Cmpt, int nCmpt>
44 inline VectorSpace<Form, Cmpt, nCmpt>::VectorSpace
46     const VectorSpace<Form, Cmpt, nCmpt>& vs
49     VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, eqOp<Cmpt>());
53 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
55 template<class Form, class Cmpt, int nCmpt>
56 inline label VectorSpace<Form, Cmpt, nCmpt>::size() const
58     return nCmpt;
62 template<class Form, class Cmpt, int nCmpt>
63 inline const Cmpt& VectorSpace<Form, Cmpt, nCmpt>::component
65     const direction d
66 ) const
68 #   ifdef FULLDEBUG
69     if (d >= nCmpt)
70     {
71         FatalErrorIn
72         (
73             "VectorSpace<Form, Cmpt, nCmpt>::component(direction) const"
74         )   << "index out of range"
75             << abort(FatalError);
76     }
77 #   endif
79     return v_[d];
83 template<class Form, class Cmpt, int nCmpt>
84 inline Cmpt& VectorSpace<Form, Cmpt, nCmpt>::component
86     const direction d
89 #   ifdef FULLDEBUG
90     if (d >= nCmpt)
91     {
92         FatalErrorIn("VectorSpace<Form, Cmpt, nCmpt>::component(direction)")
93             << "index out of range"
94             << abort(FatalError);
95     }
96 #   endif
98     return v_[d];
102 template<class Form, class Cmpt, int nCmpt>
103 inline void VectorSpace<Form, Cmpt, nCmpt>::component
105     Cmpt& c,
106     const direction d
107 ) const
109 #   ifdef FULLDEBUG
110     if (d >= nCmpt)
111     {
112         FatalErrorIn
113         (
114             "VectorSpace<Form, Cmpt, nCmpt>::component(Cmpt&, direction) const"
115         )   << "index out of range"
116             << abort(FatalError);
117     }
118 #   endif
120     c = v_[d];
124 template<class Form, class Cmpt, int nCmpt>
125 inline void VectorSpace<Form, Cmpt, nCmpt>::replace
127     const direction d,
128     const Cmpt& c
131 #   ifdef FULLDEBUG
132     if (d >= nCmpt)
133     {
134         FatalErrorIn
135         (
136             "VectorSpace<Form, Cmpt, nCmpt>::"
137             "replace(direction, const Cmpt&) const"
138         )   << "index out of range"
139             << abort(FatalError);
140     }
141 #   endif
143     v_[d] = c;
147 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
149 template<class Form, class Cmpt, int nCmpt>
150 inline const Cmpt& VectorSpace<Form, Cmpt, nCmpt>::operator[]
152     const direction d
153 ) const
155 #   ifdef FULLDEBUG
156     if (d >= nCmpt)
157     {
158         FatalErrorIn
159         (
160             "VectorSpace<Form, Cmpt, nCmpt>::operator[](direction d) const"
161         )   << "index out of range"
162             << abort(FatalError);
163     }
164 #   endif
166     return v_[d];
170 template<class Form, class Cmpt, int nCmpt>
171 inline Cmpt& VectorSpace<Form, Cmpt, nCmpt>::operator[]
173     const direction d
176 #   ifdef FULLDEBUG
177     if (d >= nCmpt)
178     {
179         FatalErrorIn("VectorSpace<Form, Cmpt, nCmpt>::operator[](direction d)")
180             << "index out of range"
181             << abort(FatalError);
182     }
183 #   endif
185     return v_[d];
189 template<class Form, class Cmpt, int nCmpt>
190 inline void VectorSpace<Form, Cmpt, nCmpt>::operator=
192     const VectorSpace<Form, Cmpt, nCmpt>& vs
195     VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, eqOp<Cmpt>());
199 template<class Form, class Cmpt, int nCmpt>
200 inline void VectorSpace<Form, Cmpt, nCmpt>::operator+=
202     const VectorSpace<Form, Cmpt, nCmpt>& vs
205     VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, plusEqOp<Cmpt>());
209 template<class Form, class Cmpt, int nCmpt>
210 inline void VectorSpace<Form, Cmpt, nCmpt>::operator-=
212     const VectorSpace<Form, Cmpt, nCmpt>& vs
215     VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, minusEqOp<Cmpt>());
219 template<class Form, class Cmpt, int nCmpt>
220 inline void VectorSpace<Form, Cmpt, nCmpt>::operator*=
222     const scalar s
225     VectorSpaceOps<nCmpt,0>::eqOpS(*this, s, multiplyEqOp2<Cmpt, scalar>());
229 template<class Form, class Cmpt, int nCmpt>
230 inline void VectorSpace<Form, Cmpt, nCmpt>::operator/=
232     const scalar s
235     VectorSpaceOps<nCmpt,0>::eqOpS(*this, s, divideEqOp2<Cmpt, scalar>());
239 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
241 template<class Form, class Cmpt, int nCmpt>
242 inline Cmpt& setComponent
244     VectorSpace<Form, Cmpt, nCmpt>& vs,
245     const direction d
248     return vs.component(d);
252 template<class Form, class Cmpt, int nCmpt>
253 inline const Cmpt& component
255     const VectorSpace<Form, Cmpt, nCmpt>& vs,
256     const direction d
259     return vs.component(d);
263 // Powers of a Form
264 // Equivalent to outer-products between the Form and itself
265 // Form^0 = 1.0
266 template<class Form, class Cmpt, int nCmpt>
267 inline typename powProduct<Form, 0>::type pow
269     const VectorSpace<Form, Cmpt, nCmpt>&,
270     typename powProduct<Form, 0>::type
271     = pTraits<typename powProduct<Form, 0>::type>::zero
274     return 1.0;
277 // Form^1 = Form
278 template<class Form, class Cmpt, int nCmpt>
279 inline typename powProduct<Form, 1>::type pow
281     const VectorSpace<Form, Cmpt, nCmpt>& v,
282     typename powProduct<Form, 1>::type
283   = pTraits<typename powProduct<Form, 1>::type>::zero
286     return static_cast<const Form&>(v);
290 // Form^2 = sqr(Form)
291 template<class Form, class Cmpt, int nCmpt>
292 inline typename powProduct<Form, 2>::type pow
294     const VectorSpace<Form, Cmpt, nCmpt>& v,
295     typename powProduct<Form, 2>::type
296   = pTraits<typename powProduct<Form, 2>::type>::zero
299     return sqr(static_cast<const Form&>(v));
303 template<class Form, class Cmpt, int nCmpt>
304 inline scalar magSqr
306     const VectorSpace<Form, Cmpt, nCmpt>& vs
309     scalar ms = magSqr(vs.v_[0]);
310     VectorSpaceOps<nCmpt,1>::SeqOp(ms, vs, plusEqMagSqrOp2<scalar, Cmpt>());
311     return ms;
315 template<class Form, class Cmpt, int nCmpt>
316 inline scalar mag
318     const VectorSpace<Form, Cmpt, nCmpt>& vs
321     return ::sqrt(magSqr(static_cast<const Form&>(vs)));
325 template<class Form, class Cmpt, int nCmpt>
326 inline VectorSpace<Form, Cmpt, nCmpt> cmptMultiply
328     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
329     const VectorSpace<Form, Cmpt, nCmpt>& vs2
332     Form v;
333     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, cmptMultiplyOp<Cmpt>());
334     return v;
338 template<class Form, class Cmpt, int nCmpt>
339 inline VectorSpace<Form, Cmpt, nCmpt> cmptDivide
341     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
342     const VectorSpace<Form, Cmpt, nCmpt>& vs2
345     Form v;
346     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, cmptDivideOp<Cmpt>());
347     return v;
351 template<class Form, class Cmpt, int nCmpt>
352 inline VectorSpace<Form, Cmpt, nCmpt> stabilise
354     const VectorSpace<Form, Cmpt, nCmpt>& vs,
355     const Cmpt& small
358     Form v;
359     VectorSpaceOps<nCmpt,0>::opVS(v, vs, small, stabiliseOp<Cmpt>());
360     return v;
364 template<class Form, class Cmpt, int nCmpt>
365 inline Cmpt cmptMax
367     const VectorSpace<Form, Cmpt, nCmpt>& vs
370     Cmpt cMax = vs.v_[0];
371     VectorSpaceOps<nCmpt,1>::SeqOp(cMax, vs, maxEqOp<Cmpt>());
372     return cMax;
376 template<class Form, class Cmpt, int nCmpt>
377 inline Cmpt cmptMin
379     const VectorSpace<Form, Cmpt, nCmpt>& vs
382     Cmpt cMin = vs.v_[0];
383     VectorSpaceOps<nCmpt,1>::SeqOp(cMin, vs, minEqOp<Cmpt>());
384     return cMin;
388 template<class Form, class Cmpt, int nCmpt>
389 inline Cmpt cmptSum
391     const VectorSpace<Form, Cmpt, nCmpt>& vs
394     Cmpt sum = vs.v_[0];
395     VectorSpaceOps<nCmpt,1>::SeqOp(sum, vs, plusEqOp<Cmpt>());
396     return sum;
400 template<class Form, class Cmpt, int nCmpt>
401 inline Cmpt cmptAv
403     const VectorSpace<Form, Cmpt, nCmpt>& vs
406     return cmptSum(vs)/nCmpt;
409 template<class Form, class Cmpt, int nCmpt>
410 inline Cmpt cmptProduct
412     const VectorSpace<Form, Cmpt, nCmpt>& vs
415     Cmpt product = vs.v_[0];
416     VectorSpaceOps<nCmpt,1>::SeqOp(product, vs, multiplyEqOp<Cmpt>());
417     return product;
421 template<class Form, class Cmpt, int nCmpt>
422 inline Form cmptMag
424     const VectorSpace<Form, Cmpt, nCmpt>& vs
427     Form v;
428     VectorSpaceOps<nCmpt,0>::eqOp(v, vs, eqMagOp<Cmpt>());
429     return v;
433 template<class Form, class Cmpt, int nCmpt>
434 inline Form max
436     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
437     const VectorSpace<Form, Cmpt, nCmpt>& vs2
440     Form v;
441     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, maxOp<Cmpt>());
442     return v;
446 template<class Form, class Cmpt, int nCmpt>
447 inline Form min
449     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
450     const VectorSpace<Form, Cmpt, nCmpt>& vs2
453     Form v;
454     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, minOp<Cmpt>());
455     return v;
459 template<class Form, class Cmpt, int nCmpt>
460 inline Form minMod
462     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
463     const VectorSpace<Form, Cmpt, nCmpt>& vs2
466     Form v;
467     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, minModOp<Cmpt>());
468     return v;
472 template<class Type>
473 inline Type dot(const scalar s, const Type& t)
475     return s * t;
479 template<class Type>
480 inline Type dot(const Type& t, const scalar s)
482     return t * s;
486 template
488     class Form1, class Cmpt1, int nCmpt1,
489     class Form2, class Cmpt2, int nCmpt2
491 inline typename innerProduct<Form1, Form2>::type dot
493     const VectorSpace<Form1, Cmpt1, nCmpt1>& t1,
494     const VectorSpace<Form2, Cmpt2, nCmpt2>& t2
497     return static_cast<const Form1&>(t1) & static_cast<const Form2&>(t2);
501 // * * * * * * * * * * * * * * * Global Operators  * * * * * * * * * * * * * //
503 template<class Form, class Cmpt, int nCmpt>
504 inline Form operator-
506     const VectorSpace<Form, Cmpt, nCmpt>& vs
509     Form v;
510     VectorSpaceOps<nCmpt,0>::eqOp(v, vs, eqMinusOp<Cmpt>());
511     return v;
515 template<class Form, class Cmpt, int nCmpt>
516 inline Form operator+
518     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
519     const VectorSpace<Form, Cmpt, nCmpt>& vs2
522     Form v;
523     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, plusOp<Cmpt>());
524     return v;
527 template<class Form, class Cmpt, int nCmpt>
528 inline Form operator-
530     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
531     const VectorSpace<Form, Cmpt, nCmpt>& vs2
534     Form v;
535     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, minusOp<Cmpt>());
536     return v;
540 template<class Form, class Cmpt, int nCmpt>
541 inline Form operator*
543     scalar s,
544     const VectorSpace<Form, Cmpt, nCmpt>& vs
547     Form v;
548     VectorSpaceOps<nCmpt,0>::opSV(v, s, vs, multiplyOp3<Cmpt, scalar, Cmpt>());
549     return v;
553 template<class Form, class Cmpt, int nCmpt>
554 inline Form operator*
556     const VectorSpace<Form, Cmpt, nCmpt>& vs,
557     scalar s
560     Form v;
561     VectorSpaceOps<nCmpt,0>::opVS(v, vs, s, multiplyOp3<Cmpt, Cmpt, scalar>());
562     return v;
566 template<class Form, class Cmpt, int nCmpt>
567 inline Form operator/
569     const VectorSpace<Form, Cmpt, nCmpt>& vs,
570     scalar s
573     Form v;
574     VectorSpaceOps<nCmpt,0>::opVS(v, vs, s, divideOp3<Cmpt, Cmpt, scalar>());
575     return v;
579 template<class Form, class Cmpt, int nCmpt>
580 inline Form operator/
582     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
583     const VectorSpace<Form, Cmpt, nCmpt>& vs2
586     Form v;
587     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, divideOp<Cmpt>());
588     return v;
592 template<class Form, class Cmpt, int nCmpt>
593 inline Form operator/
595     scalar s,
596     const VectorSpace<Form, Cmpt, nCmpt>& vs
599     Form v;
600     VectorSpaceOps<nCmpt,0>::opSV(v, s, vs, divideOp2<scalar, Cmpt>());
601     return v;
606 template<class Form, class Cmpt, int nCmpt>
607 inline Cmpt operator&&
609     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
610     const VectorSpace<Form, Cmpt, nCmpt>& vs2
613     Cmpt ddProd = vs1.v_[0]*vs2.v_[0];
614     for (int i=1; i<nCmpt; ++i)
615     {
616         ddProd += vs1.v_[i]*vs2.v_[i];
617     }
618     return ddProd;
622 template<class Form, class Cmpt, int nCmpt>
623 inline bool operator==
625     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
626     const VectorSpace<Form, Cmpt, nCmpt>& vs2
629     bool eq = true;
630     for (int i=0; i<nCmpt; ++i)
631     {
632         if (!(eq &= (equal(vs1.v_[i], vs2.v_[i])))) break;
633     }
634     return eq;
638 template<class Form, class Cmpt, int nCmpt>
639 inline bool operator!=
641     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
642     const VectorSpace<Form, Cmpt, nCmpt>& vs2
645     return !(vs1 == vs2);
649 template<class Form, class Cmpt, int nCmpt>
650 inline bool operator>
652     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
653     const VectorSpace<Form, Cmpt, nCmpt>& vs2
656     bool gt = true;
657     for (int i=0; i<nCmpt; ++i)
658     {
659         if (!(gt &= vs1.v_[i] > vs2.v_[i])) break;
660     }
661     return gt;
665 template<class Form, class Cmpt, int nCmpt>
666 inline bool operator<
668     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
669     const VectorSpace<Form, Cmpt, nCmpt>& vs2
672     bool lt = true;
673     for (int i=0; i<nCmpt; ++i)
674     {
675         if (!(lt &= vs1.v_[i] < vs2.v_[i])) break;
676     }
677     return lt;
681 template<class Form, class Cmpt, int nCmpt>
682 inline bool operator>=
684     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
685     const VectorSpace<Form, Cmpt, nCmpt>& vs2
688     return !(vs1 < vs2);
692 template<class Form, class Cmpt, int nCmpt>
693 inline bool operator<=
695     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
696     const VectorSpace<Form, Cmpt, nCmpt>& vs2
699     return !(vs1 > vs2);
703 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
705 } // End namespace Foam
707 // ************************************************************************* //