initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / primitives / VectorSpace / VectorSpaceI.H
blobeeea5f6bffe8fb4488e72f626beae526725adf02
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 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 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
19     for more details.
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 \*---------------------------------------------------------------------------*/
27 #include "error.H"
28 #include "products.H"
29 #include "VectorSpaceM.H"
30 #include "ops.H"
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 namespace Foam
37 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
39 template<class Form, class Cmpt, int nCmpt>
40 inline VectorSpace<Form, Cmpt, nCmpt>::VectorSpace()
44 template<class Form, class Cmpt, int nCmpt>
45 inline VectorSpace<Form, Cmpt, nCmpt>::VectorSpace
47     const VectorSpace<Form, Cmpt, nCmpt>& vs
50     VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, eqOp<Cmpt>());
54 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
56 template<class Form, class Cmpt, int nCmpt>
57 inline label VectorSpace<Form, Cmpt, nCmpt>::size() const
59     return nCmpt;
63 template<class Form, class Cmpt, int nCmpt>
64 inline const Cmpt& VectorSpace<Form, Cmpt, nCmpt>::component
66     const direction d
67 ) const
69 #   ifdef FULLDEBUG
70     if (d >= nCmpt)
71     {
72         FatalErrorIn
73         (
74             "VectorSpace<Form, Cmpt, nCmpt>::component(direction) const"
75         )   << "index out of range"
76             << abort(FatalError);
77     }
78 #   endif
80     return v_[d];
84 template<class Form, class Cmpt, int nCmpt>
85 inline Cmpt& VectorSpace<Form, Cmpt, nCmpt>::component
87     const direction d
90 #   ifdef FULLDEBUG
91     if (d >= nCmpt)
92     {
93         FatalErrorIn("VectorSpace<Form, Cmpt, nCmpt>::component(direction)")
94             << "index out of range"
95             << abort(FatalError);
96     }
97 #   endif
99     return v_[d];
103 template<class Form, class Cmpt, int nCmpt>
104 inline void VectorSpace<Form, Cmpt, nCmpt>::component
106     Cmpt& c,
107     const direction d
108 ) const
110 #   ifdef FULLDEBUG
111     if (d >= nCmpt)
112     {
113         FatalErrorIn
114         (
115             "VectorSpace<Form, Cmpt, nCmpt>::component(Cmpt&, direction) const"
116         )   << "index out of range"
117             << abort(FatalError);
118     }
119 #   endif
121     c = v_[d];
125 template<class Form, class Cmpt, int nCmpt>
126 inline void VectorSpace<Form, Cmpt, nCmpt>::replace
128     const direction d,
129     const Cmpt& c
132 #   ifdef FULLDEBUG
133     if (d >= nCmpt)
134     {
135         FatalErrorIn
136         (
137             "VectorSpace<Form, Cmpt, nCmpt>::"
138             "replace(direction, const Cmpt&) const"
139         )   << "index out of range"
140             << abort(FatalError);
141     }
142 #   endif
144     v_[d] = c;
148 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
150 template<class Form, class Cmpt, int nCmpt>
151 inline const Cmpt& VectorSpace<Form, Cmpt, nCmpt>::operator[]
153     const direction d
154 ) const
156 #   ifdef FULLDEBUG
157     if (d >= nCmpt)
158     {
159         FatalErrorIn
160         (
161             "VectorSpace<Form, Cmpt, nCmpt>::operator[](direction d) const"
162         )   << "index out of range"
163             << abort(FatalError);
164     }
165 #   endif
167     return v_[d];
171 template<class Form, class Cmpt, int nCmpt>
172 inline Cmpt& VectorSpace<Form, Cmpt, nCmpt>::operator[]
174     const direction d
177 #   ifdef FULLDEBUG
178     if (d >= nCmpt)
179     {
180         FatalErrorIn("VectorSpace<Form, Cmpt, nCmpt>::operator[](direction d)")
181             << "index out of range"
182             << abort(FatalError);
183     }
184 #   endif
186     return v_[d];
190 template<class Form, class Cmpt, int nCmpt>
191 inline void VectorSpace<Form, Cmpt, nCmpt>::operator=
193     const VectorSpace<Form, Cmpt, nCmpt>& vs
196     VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, eqOp<Cmpt>());
200 template<class Form, class Cmpt, int nCmpt>
201 inline void VectorSpace<Form, Cmpt, nCmpt>::operator+=
203     const VectorSpace<Form, Cmpt, nCmpt>& vs
206     VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, plusEqOp<Cmpt>());
210 template<class Form, class Cmpt, int nCmpt>
211 inline void VectorSpace<Form, Cmpt, nCmpt>::operator-=
213     const VectorSpace<Form, Cmpt, nCmpt>& vs
216     VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, minusEqOp<Cmpt>());
220 template<class Form, class Cmpt, int nCmpt>
221 inline void VectorSpace<Form, Cmpt, nCmpt>::operator*=
223     const scalar s
226     VectorSpaceOps<nCmpt,0>::eqOpS(*this, s, multiplyEqOp2<Cmpt, scalar>());
230 template<class Form, class Cmpt, int nCmpt>
231 inline void VectorSpace<Form, Cmpt, nCmpt>::operator/=
233     const scalar s
236     VectorSpaceOps<nCmpt,0>::eqOpS(*this, s, divideEqOp2<Cmpt, scalar>());
240 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
242 template<class Form, class Cmpt, int nCmpt>
243 inline Cmpt& setComponent
245     VectorSpace<Form, Cmpt, nCmpt>& vs,
246     const direction d
249     return vs.component(d);
253 template<class Form, class Cmpt, int nCmpt>
254 inline const Cmpt& component
256     const VectorSpace<Form, Cmpt, nCmpt>& vs,
257     const direction d
260     return vs.component(d);
264 // Powers of a Form
265 // Equivalent to outer-products between the Form and itself
266 // Form^0 = 1.0
267 template<class Form, class Cmpt, int nCmpt>
268 inline typename powProduct<Form, 0>::type pow
270     const VectorSpace<Form, Cmpt, nCmpt>&,
271     typename powProduct<Form, 0>::type
272     = pTraits<typename powProduct<Form, 0>::type>::zero
275     return 1.0;
278 // Form^1 = Form
279 template<class Form, class Cmpt, int nCmpt>
280 inline typename powProduct<Form, 1>::type pow
282     const VectorSpace<Form, Cmpt, nCmpt>& v,
283     typename powProduct<Form, 1>::type
284   = pTraits<typename powProduct<Form, 1>::type>::zero
287     return static_cast<const Form&>(v);
291 // Form^2 = sqr(Form)
292 template<class Form, class Cmpt, int nCmpt>
293 inline typename powProduct<Form, 2>::type pow
295     const VectorSpace<Form, Cmpt, nCmpt>& v,
296     typename powProduct<Form, 2>::type
297   = pTraits<typename powProduct<Form, 2>::type>::zero
300     return sqr(static_cast<const Form&>(v));
304 template<class Form, class Cmpt, int nCmpt>
305 inline scalar magSqr
307     const VectorSpace<Form, Cmpt, nCmpt>& vs
310     scalar ms = magSqr(vs.v_[0]);
311     VectorSpaceOps<nCmpt,1>::SeqOp(ms, vs, plusEqMagSqrOp2<scalar, Cmpt>());
312     return ms;
316 template<class Form, class Cmpt, int nCmpt>
317 inline scalar mag
319     const VectorSpace<Form, Cmpt, nCmpt>& vs
322     return ::sqrt(magSqr(static_cast<const Form&>(vs)));
326 template<class Form, class Cmpt, int nCmpt>
327 inline VectorSpace<Form, Cmpt, nCmpt> cmptMultiply
329     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
330     const VectorSpace<Form, Cmpt, nCmpt>& vs2
333     Form v;
334     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, cmptMultiplyOp<Cmpt>());
335     return v;
339 template<class Form, class Cmpt, int nCmpt>
340 inline VectorSpace<Form, Cmpt, nCmpt> cmptDivide
342     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
343     const VectorSpace<Form, Cmpt, nCmpt>& vs2
346     Form v;
347     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, cmptDivideOp<Cmpt>());
348     return v;
352 template<class Form, class Cmpt, int nCmpt>
353 inline VectorSpace<Form, Cmpt, nCmpt> stabilise
355     const VectorSpace<Form, Cmpt, nCmpt>& vs,
356     const Cmpt& small
359     Form v;
360     VectorSpaceOps<nCmpt,0>::opVS(v, vs, small, stabiliseOp<Cmpt>());
361     return v;
365 template<class Form, class Cmpt, int nCmpt>
366 inline Cmpt cmptMax
368     const VectorSpace<Form, Cmpt, nCmpt>& vs
371     Cmpt cMax = vs.v_[0];
372     VectorSpaceOps<nCmpt,1>::SeqOp(cMax, vs, maxEqOp<Cmpt>());
373     return cMax;
377 template<class Form, class Cmpt, int nCmpt>
378 inline Cmpt cmptMin
380     const VectorSpace<Form, Cmpt, nCmpt>& vs
383     Cmpt cMin = vs.v_[0];
384     VectorSpaceOps<nCmpt,1>::SeqOp(cMin, vs, minEqOp<Cmpt>());
385     return cMin;
389 template<class Form, class Cmpt, int nCmpt>
390 inline Cmpt cmptSum
392     const VectorSpace<Form, Cmpt, nCmpt>& vs
395     Cmpt sum = vs.v_[0];
396     VectorSpaceOps<nCmpt,1>::SeqOp(sum, vs, plusEqOp<Cmpt>());
397     return sum;
401 template<class Form, class Cmpt, int nCmpt>
402 inline Cmpt cmptAv
404     const VectorSpace<Form, Cmpt, nCmpt>& vs
407     return cmptSum(vs)/nCmpt;
411 template<class Form, class Cmpt, int nCmpt>
412 inline Form cmptMag
414     const VectorSpace<Form, Cmpt, nCmpt>& vs
417     Form v;
418     VectorSpaceOps<nCmpt,0>::eqOp(v, vs, eqMagOp<Cmpt>());
419     return v;
423 template<class Form, class Cmpt, int nCmpt>
424 inline Form max
426     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
427     const VectorSpace<Form, Cmpt, nCmpt>& vs2
430     Form v;
431     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, maxOp<Cmpt>());
432     return v;
436 template<class Form, class Cmpt, int nCmpt>
437 inline Form min
439     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
440     const VectorSpace<Form, Cmpt, nCmpt>& vs2
443     Form v;
444     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, minOp<Cmpt>());
445     return v;
449 template<class Form, class Cmpt, int nCmpt>
450 inline Form minMod
452     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
453     const VectorSpace<Form, Cmpt, nCmpt>& vs2
456     Form v;
457     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, minModOp<Cmpt>());
458     return v;
462 template<class Type>
463 inline Type dot(const scalar s, const Type& t)
465     return s * t;
469 template<class Type>
470 inline Type dot(const Type& t, const scalar s)
472     return t * s;
476 template
478     class Form1, class Cmpt1, int nCmpt1,
479     class Form2, class Cmpt2, int nCmpt2
481 inline typename innerProduct<Form1, Form2>::type dot
483     const VectorSpace<Form1, Cmpt1, nCmpt1>& t1,
484     const VectorSpace<Form2, Cmpt2, nCmpt2>& t2
487     return static_cast<const Form1&>(t1) & static_cast<const Form2&>(t2);
491 // * * * * * * * * * * * * * * * Global Operators  * * * * * * * * * * * * * //
493 template<class Form, class Cmpt, int nCmpt>
494 inline Form operator-
496     const VectorSpace<Form, Cmpt, nCmpt>& vs
499     Form v;
500     VectorSpaceOps<nCmpt,0>::eqOp(v, vs, eqMinusOp<Cmpt>());
501     return v;
505 template<class Form, class Cmpt, int nCmpt>
506 inline Form operator+
508     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
509     const VectorSpace<Form, Cmpt, nCmpt>& vs2
512     Form v;
513     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, plusOp<Cmpt>());
514     return v;
517 template<class Form, class Cmpt, int nCmpt>
518 inline Form operator-
520     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
521     const VectorSpace<Form, Cmpt, nCmpt>& vs2
524     Form v;
525     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, minusOp<Cmpt>());
526     return v;
530 template<class Form, class Cmpt, int nCmpt>
531 inline Form operator*
533     scalar s,
534     const VectorSpace<Form, Cmpt, nCmpt>& vs
537     Form v;
538     VectorSpaceOps<nCmpt,0>::opSV(v, s, vs, multiplyOp3<Cmpt, scalar, Cmpt>());
539     return v;
543 template<class Form, class Cmpt, int nCmpt>
544 inline Form operator*
546     const VectorSpace<Form, Cmpt, nCmpt>& vs,
547     scalar s
550     Form v;
551     VectorSpaceOps<nCmpt,0>::opVS(v, vs, s, multiplyOp3<Cmpt, Cmpt, scalar>());
552     return v;
556 template<class Form, class Cmpt, int nCmpt>
557 inline Form operator/
559     const VectorSpace<Form, Cmpt, nCmpt>& vs,
560     scalar s
563     Form v;
564     VectorSpaceOps<nCmpt,0>::opVS(v, vs, s, divideOp3<Cmpt, Cmpt, scalar>());
565     return v;
569 template<class Form, class Cmpt, int nCmpt>
570 inline Form operator/
572     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
573     const VectorSpace<Form, Cmpt, nCmpt>& vs2
576     Form v;
577     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, divideOp<Cmpt>());
578     return v;
582 template<class Form, class Cmpt, int nCmpt>
583 inline Form operator/
585     scalar s,
586     const VectorSpace<Form, Cmpt, nCmpt>& vs
589     Form v;
590     VectorSpaceOps<nCmpt,0>::opSV(v, s, vs, divideOp2<scalar, Cmpt>());
591     return v;
596 template<class Form, class Cmpt, int nCmpt>
597 inline Cmpt operator&&
599     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
600     const VectorSpace<Form, Cmpt, nCmpt>& vs2
603     Cmpt ddProd = vs1.v_[0]*vs2.v_[0];
604     for (int i=1; i<nCmpt; ++i)
605     {
606         ddProd += vs1.v_[i]*vs2.v_[i];
607     }
608     return ddProd;
612 template<class Form, class Cmpt, int nCmpt>
613 inline bool operator==
615     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
616     const VectorSpace<Form, Cmpt, nCmpt>& vs2
619     bool eq = true;
620     for (int i=0; i<nCmpt; ++i)
621     {
622         if (!(eq &= (equal(vs1.v_[i], vs2.v_[i])))) break;
623     }
624     return eq;
628 template<class Form, class Cmpt, int nCmpt>
629 inline bool operator!=
631     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
632     const VectorSpace<Form, Cmpt, nCmpt>& vs2
635     return !(vs1 == vs2);
639 template<class Form, class Cmpt, int nCmpt>
640 inline bool operator>
642     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
643     const VectorSpace<Form, Cmpt, nCmpt>& vs2
646     bool gt = true;
647     for (int i=0; i<nCmpt; ++i)
648     {
649         if (!(gt &= vs1.v_[i] > vs2.v_[i])) break;
650     }
651     return gt;
655 template<class Form, class Cmpt, int nCmpt>
656 inline bool operator<
658     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
659     const VectorSpace<Form, Cmpt, nCmpt>& vs2
662     bool lt = true;
663     for (int i=0; i<nCmpt; ++i)
664     {
665         if (!(lt &= vs1.v_[i] < vs2.v_[i])) break;
666     }
667     return lt;
671 template<class Form, class Cmpt, int nCmpt>
672 inline bool operator>=
674     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
675     const VectorSpace<Form, Cmpt, nCmpt>& vs2
678     return !(vs1 < vs2);
682 template<class Form, class Cmpt, int nCmpt>
683 inline bool operator<=
685     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
686     const VectorSpace<Form, Cmpt, nCmpt>& vs2
689     return !(vs1 > vs2);
693 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
695 } // End namespace Foam
697 // ************************************************************************* //