OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

VectorSpaceI.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "error.H"
00028 #include "products.H"
00029 #include "VectorSpaceM.H"
00030 #include "ops.H"
00031 
00032 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00033 
00034 namespace Foam
00035 {
00036 
00037 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00038 
00039 template<class Form, class Cmpt, int nCmpt>
00040 inline VectorSpace<Form, Cmpt, nCmpt>::VectorSpace()
00041 {}
00042 
00043 
00044 template<class Form, class Cmpt, int nCmpt>
00045 inline VectorSpace<Form, Cmpt, nCmpt>::VectorSpace
00046 (
00047     const VectorSpace<Form, Cmpt, nCmpt>& vs
00048 )
00049 {
00050     VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, eqOp<Cmpt>());
00051 }
00052 
00053 
00054 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00055 
00056 template<class Form, class Cmpt, int nCmpt>
00057 inline label VectorSpace<Form, Cmpt, nCmpt>::size() const
00058 {
00059     return nCmpt;
00060 }
00061 
00062 
00063 template<class Form, class Cmpt, int nCmpt>
00064 inline const Cmpt& VectorSpace<Form, Cmpt, nCmpt>::component
00065 (
00066     const direction d
00067 ) const
00068 {
00069 #   ifdef FULLDEBUG
00070     if (d >= nCmpt)
00071     {
00072         FatalErrorIn
00073         (
00074             "VectorSpace<Form, Cmpt, nCmpt>::component(direction) const"
00075         )   << "index out of range"
00076             << abort(FatalError);
00077     }
00078 #   endif
00079 
00080     return v_[d];
00081 }
00082 
00083 
00084 template<class Form, class Cmpt, int nCmpt>
00085 inline Cmpt& VectorSpace<Form, Cmpt, nCmpt>::component
00086 (
00087     const direction d
00088 )
00089 {
00090 #   ifdef FULLDEBUG
00091     if (d >= nCmpt)
00092     {
00093         FatalErrorIn("VectorSpace<Form, Cmpt, nCmpt>::component(direction)")
00094             << "index out of range"
00095             << abort(FatalError);
00096     }
00097 #   endif
00098 
00099     return v_[d];
00100 }
00101 
00102 
00103 template<class Form, class Cmpt, int nCmpt>
00104 inline void VectorSpace<Form, Cmpt, nCmpt>::component
00105 (
00106     Cmpt& c,
00107     const direction d
00108 ) const
00109 {
00110 #   ifdef FULLDEBUG
00111     if (d >= nCmpt)
00112     {
00113         FatalErrorIn
00114         (
00115             "VectorSpace<Form, Cmpt, nCmpt>::component(Cmpt&, direction) const"
00116         )   << "index out of range"
00117             << abort(FatalError);
00118     }
00119 #   endif
00120 
00121     c = v_[d];
00122 }
00123 
00124 
00125 template<class Form, class Cmpt, int nCmpt>
00126 inline void VectorSpace<Form, Cmpt, nCmpt>::replace
00127 (
00128     const direction d,
00129     const Cmpt& c
00130 )
00131 {
00132 #   ifdef FULLDEBUG
00133     if (d >= nCmpt)
00134     {
00135         FatalErrorIn
00136         (
00137             "VectorSpace<Form, Cmpt, nCmpt>::"
00138             "replace(direction, const Cmpt&) const"
00139         )   << "index out of range"
00140             << abort(FatalError);
00141     }
00142 #   endif
00143 
00144     v_[d] = c;
00145 }
00146 
00147 
00148 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00149 
00150 template<class Form, class Cmpt, int nCmpt>
00151 inline const Cmpt& VectorSpace<Form, Cmpt, nCmpt>::operator[]
00152 (
00153     const direction d
00154 ) const
00155 {
00156 #   ifdef FULLDEBUG
00157     if (d >= nCmpt)
00158     {
00159         FatalErrorIn
00160         (
00161             "VectorSpace<Form, Cmpt, nCmpt>::operator[](direction d) const"
00162         )   << "index out of range"
00163             << abort(FatalError);
00164     }
00165 #   endif
00166 
00167     return v_[d];
00168 }
00169 
00170 
00171 template<class Form, class Cmpt, int nCmpt>
00172 inline Cmpt& VectorSpace<Form, Cmpt, nCmpt>::operator[]
00173 (
00174     const direction d
00175 )
00176 {
00177 #   ifdef FULLDEBUG
00178     if (d >= nCmpt)
00179     {
00180         FatalErrorIn("VectorSpace<Form, Cmpt, nCmpt>::operator[](direction d)")
00181             << "index out of range"
00182             << abort(FatalError);
00183     }
00184 #   endif
00185 
00186     return v_[d];
00187 }
00188 
00189 
00190 template<class Form, class Cmpt, int nCmpt>
00191 inline void VectorSpace<Form, Cmpt, nCmpt>::operator=
00192 (
00193     const VectorSpace<Form, Cmpt, nCmpt>& vs
00194 )
00195 {
00196     VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, eqOp<Cmpt>());
00197 }
00198 
00199 
00200 template<class Form, class Cmpt, int nCmpt>
00201 inline void VectorSpace<Form, Cmpt, nCmpt>::operator+=
00202 (
00203     const VectorSpace<Form, Cmpt, nCmpt>& vs
00204 )
00205 {
00206     VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, plusEqOp<Cmpt>());
00207 }
00208 
00209 
00210 template<class Form, class Cmpt, int nCmpt>
00211 inline void VectorSpace<Form, Cmpt, nCmpt>::operator-=
00212 (
00213     const VectorSpace<Form, Cmpt, nCmpt>& vs
00214 )
00215 {
00216     VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, minusEqOp<Cmpt>());
00217 }
00218 
00219 
00220 template<class Form, class Cmpt, int nCmpt>
00221 inline void VectorSpace<Form, Cmpt, nCmpt>::operator*=
00222 (
00223     const scalar s
00224 )
00225 {
00226     VectorSpaceOps<nCmpt,0>::eqOpS(*this, s, multiplyEqOp2<Cmpt, scalar>());
00227 }
00228 
00229 
00230 template<class Form, class Cmpt, int nCmpt>
00231 inline void VectorSpace<Form, Cmpt, nCmpt>::operator/=
00232 (
00233     const scalar s
00234 )
00235 {
00236     VectorSpaceOps<nCmpt,0>::eqOpS(*this, s, divideEqOp2<Cmpt, scalar>());
00237 }
00238 
00239 
00240 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
00241 
00242 template<class Form, class Cmpt, int nCmpt>
00243 inline Cmpt& setComponent
00244 (
00245     VectorSpace<Form, Cmpt, nCmpt>& vs,
00246     const direction d
00247 )
00248 {
00249     return vs.component(d);
00250 }
00251 
00252 
00253 template<class Form, class Cmpt, int nCmpt>
00254 inline const Cmpt& component
00255 (
00256     const VectorSpace<Form, Cmpt, nCmpt>& vs,
00257     const direction d
00258 )
00259 {
00260     return vs.component(d);
00261 }
00262 
00263 
00264 // Powers of a Form
00265 // Equivalent to outer-products between the Form and itself
00266 // Form^0 = 1.0
00267 template<class Form, class Cmpt, int nCmpt>
00268 inline typename powProduct<Form, 0>::type pow
00269 (
00270     const VectorSpace<Form, Cmpt, nCmpt>&,
00271     typename powProduct<Form, 0>::type
00272     = pTraits<typename powProduct<Form, 0>::type>::zero
00273 )
00274 {
00275     return 1.0;
00276 }
00277 
00278 // Form^1 = Form
00279 template<class Form, class Cmpt, int nCmpt>
00280 inline typename powProduct<Form, 1>::type pow
00281 (
00282     const VectorSpace<Form, Cmpt, nCmpt>& v,
00283     typename powProduct<Form, 1>::type
00284   = pTraits<typename powProduct<Form, 1>::type>::zero
00285 )
00286 {
00287     return static_cast<const Form&>(v);
00288 }
00289 
00290 
00291 // Form^2 = sqr(Form)
00292 template<class Form, class Cmpt, int nCmpt>
00293 inline typename powProduct<Form, 2>::type pow
00294 (
00295     const VectorSpace<Form, Cmpt, nCmpt>& v,
00296     typename powProduct<Form, 2>::type
00297   = pTraits<typename powProduct<Form, 2>::type>::zero
00298 )
00299 {
00300     return sqr(static_cast<const Form&>(v));
00301 }
00302 
00303 
00304 template<class Form, class Cmpt, int nCmpt>
00305 inline scalar magSqr
00306 (
00307     const VectorSpace<Form, Cmpt, nCmpt>& vs
00308 )
00309 {
00310     scalar ms = magSqr(vs.v_[0]);
00311     VectorSpaceOps<nCmpt,1>::SeqOp(ms, vs, plusEqMagSqrOp2<scalar, Cmpt>());
00312     return ms;
00313 }
00314 
00315 
00316 template<class Form, class Cmpt, int nCmpt>
00317 inline scalar mag
00318 (
00319     const VectorSpace<Form, Cmpt, nCmpt>& vs
00320 )
00321 {
00322     return ::sqrt(magSqr(static_cast<const Form&>(vs)));
00323 }
00324 
00325 
00326 template<class Form, class Cmpt, int nCmpt>
00327 inline VectorSpace<Form, Cmpt, nCmpt> cmptMultiply
00328 (
00329     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00330     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00331 )
00332 {
00333     Form v;
00334     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, cmptMultiplyOp<Cmpt>());
00335     return v;
00336 }
00337 
00338 
00339 template<class Form, class Cmpt, int nCmpt>
00340 inline VectorSpace<Form, Cmpt, nCmpt> cmptDivide
00341 (
00342     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00343     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00344 )
00345 {
00346     Form v;
00347     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, cmptDivideOp<Cmpt>());
00348     return v;
00349 }
00350 
00351 
00352 template<class Form, class Cmpt, int nCmpt>
00353 inline VectorSpace<Form, Cmpt, nCmpt> stabilise
00354 (
00355     const VectorSpace<Form, Cmpt, nCmpt>& vs,
00356     const Cmpt& small
00357 )
00358 {
00359     Form v;
00360     VectorSpaceOps<nCmpt,0>::opVS(v, vs, small, stabiliseOp<Cmpt>());
00361     return v;
00362 }
00363 
00364 
00365 template<class Form, class Cmpt, int nCmpt>
00366 inline Cmpt cmptMax
00367 (
00368     const VectorSpace<Form, Cmpt, nCmpt>& vs
00369 )
00370 {
00371     Cmpt cMax = vs.v_[0];
00372     VectorSpaceOps<nCmpt,1>::SeqOp(cMax, vs, maxEqOp<Cmpt>());
00373     return cMax;
00374 }
00375 
00376 
00377 template<class Form, class Cmpt, int nCmpt>
00378 inline Cmpt cmptMin
00379 (
00380     const VectorSpace<Form, Cmpt, nCmpt>& vs
00381 )
00382 {
00383     Cmpt cMin = vs.v_[0];
00384     VectorSpaceOps<nCmpt,1>::SeqOp(cMin, vs, minEqOp<Cmpt>());
00385     return cMin;
00386 }
00387 
00388 
00389 template<class Form, class Cmpt, int nCmpt>
00390 inline Cmpt cmptSum
00391 (
00392     const VectorSpace<Form, Cmpt, nCmpt>& vs
00393 )
00394 {
00395     Cmpt sum = vs.v_[0];
00396     VectorSpaceOps<nCmpt,1>::SeqOp(sum, vs, plusEqOp<Cmpt>());
00397     return sum;
00398 }
00399 
00400 
00401 template<class Form, class Cmpt, int nCmpt>
00402 inline Cmpt cmptAv
00403 (
00404     const VectorSpace<Form, Cmpt, nCmpt>& vs
00405 )
00406 {
00407     return cmptSum(vs)/nCmpt;
00408 }
00409 
00410 
00411 template<class Form, class Cmpt, int nCmpt>
00412 inline Form cmptMag
00413 (
00414     const VectorSpace<Form, Cmpt, nCmpt>& vs
00415 )
00416 {
00417     Form v;
00418     VectorSpaceOps<nCmpt,0>::eqOp(v, vs, eqMagOp<Cmpt>());
00419     return v;
00420 }
00421 
00422 
00423 template<class Form, class Cmpt, int nCmpt>
00424 inline Form max
00425 (
00426     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00427     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00428 )
00429 {
00430     Form v;
00431     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, maxOp<Cmpt>());
00432     return v;
00433 }
00434 
00435 
00436 template<class Form, class Cmpt, int nCmpt>
00437 inline Form min
00438 (
00439     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00440     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00441 )
00442 {
00443     Form v;
00444     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, minOp<Cmpt>());
00445     return v;
00446 }
00447 
00448 
00449 template<class Form, class Cmpt, int nCmpt>
00450 inline Form minMod
00451 (
00452     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00453     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00454 )
00455 {
00456     Form v;
00457     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, minModOp<Cmpt>());
00458     return v;
00459 }
00460 
00461 
00462 template<class Type>
00463 inline Type dot(const scalar s, const Type& t)
00464 {
00465     return s * t;
00466 }
00467 
00468 
00469 template<class Type>
00470 inline Type dot(const Type& t, const scalar s)
00471 {
00472     return t * s;
00473 }
00474 
00475 
00476 template
00477 <
00478     class Form1, class Cmpt1, int nCmpt1,
00479     class Form2, class Cmpt2, int nCmpt2
00480 >
00481 inline typename innerProduct<Form1, Form2>::type dot
00482 (
00483     const VectorSpace<Form1, Cmpt1, nCmpt1>& t1,
00484     const VectorSpace<Form2, Cmpt2, nCmpt2>& t2
00485 )
00486 {
00487     return static_cast<const Form1&>(t1) & static_cast<const Form2&>(t2);
00488 }
00489 
00490 
00491 // * * * * * * * * * * * * * * * Global Operators  * * * * * * * * * * * * * //
00492 
00493 template<class Form, class Cmpt, int nCmpt>
00494 inline Form operator-
00495 (
00496     const VectorSpace<Form, Cmpt, nCmpt>& vs
00497 )
00498 {
00499     Form v;
00500     VectorSpaceOps<nCmpt,0>::eqOp(v, vs, eqMinusOp<Cmpt>());
00501     return v;
00502 }
00503 
00504 
00505 template<class Form, class Cmpt, int nCmpt>
00506 inline Form operator+
00507 (
00508     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00509     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00510 )
00511 {
00512     Form v;
00513     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, plusOp<Cmpt>());
00514     return v;
00515 }
00516 
00517 template<class Form, class Cmpt, int nCmpt>
00518 inline Form operator-
00519 (
00520     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00521     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00522 )
00523 {
00524     Form v;
00525     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, minusOp<Cmpt>());
00526     return v;
00527 }
00528 
00529 
00530 template<class Form, class Cmpt, int nCmpt>
00531 inline Form operator*
00532 (
00533     scalar s,
00534     const VectorSpace<Form, Cmpt, nCmpt>& vs
00535 )
00536 {
00537     Form v;
00538     VectorSpaceOps<nCmpt,0>::opSV(v, s, vs, multiplyOp3<Cmpt, scalar, Cmpt>());
00539     return v;
00540 }
00541 
00542 
00543 template<class Form, class Cmpt, int nCmpt>
00544 inline Form operator*
00545 (
00546     const VectorSpace<Form, Cmpt, nCmpt>& vs,
00547     scalar s
00548 )
00549 {
00550     Form v;
00551     VectorSpaceOps<nCmpt,0>::opVS(v, vs, s, multiplyOp3<Cmpt, Cmpt, scalar>());
00552     return v;
00553 }
00554 
00555 
00556 template<class Form, class Cmpt, int nCmpt>
00557 inline Form operator/
00558 (
00559     const VectorSpace<Form, Cmpt, nCmpt>& vs,
00560     scalar s
00561 )
00562 {
00563     Form v;
00564     VectorSpaceOps<nCmpt,0>::opVS(v, vs, s, divideOp3<Cmpt, Cmpt, scalar>());
00565     return v;
00566 }
00567 
00568 /*
00569 template<class Form, class Cmpt, int nCmpt>
00570 inline Form operator/
00571 (
00572     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00573     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00574 )
00575 {
00576     Form v;
00577     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, divideOp<Cmpt>());
00578     return v;
00579 }
00580 
00581 
00582 template<class Form, class Cmpt, int nCmpt>
00583 inline Form operator/
00584 (
00585     scalar s,
00586     const VectorSpace<Form, Cmpt, nCmpt>& vs
00587 )
00588 {
00589     Form v;
00590     VectorSpaceOps<nCmpt,0>::opSV(v, s, vs, divideOp2<scalar, Cmpt>());
00591     return v;
00592 }
00593 */
00594 
00595 
00596 template<class Form, class Cmpt, int nCmpt>
00597 inline Cmpt operator&&
00598 (
00599     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00600     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00601 )
00602 {
00603     Cmpt ddProd = vs1.v_[0]*vs2.v_[0];
00604     for (int i=1; i<nCmpt; ++i)
00605     {
00606         ddProd += vs1.v_[i]*vs2.v_[i];
00607     }
00608     return ddProd;
00609 }
00610 
00611 
00612 template<class Form, class Cmpt, int nCmpt>
00613 inline bool operator==
00614 (
00615     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00616     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00617 )
00618 {
00619     bool eq = true;
00620     for (int i=0; i<nCmpt; ++i)
00621     {
00622         if (!(eq &= (equal(vs1.v_[i], vs2.v_[i])))) break;
00623     }
00624     return eq;
00625 }
00626 
00627 
00628 template<class Form, class Cmpt, int nCmpt>
00629 inline bool operator!=
00630 (
00631     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00632     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00633 )
00634 {
00635     return !(vs1 == vs2);
00636 }
00637 
00638 
00639 template<class Form, class Cmpt, int nCmpt>
00640 inline bool operator>
00641 (
00642     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00643     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00644 )
00645 {
00646     bool gt = true;
00647     for (int i=0; i<nCmpt; ++i)
00648     {
00649         if (!(gt &= vs1.v_[i] > vs2.v_[i])) break;
00650     }
00651     return gt;
00652 }
00653 
00654 
00655 template<class Form, class Cmpt, int nCmpt>
00656 inline bool operator<
00657 (
00658     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00659     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00660 )
00661 {
00662     bool lt = true;
00663     for (int i=0; i<nCmpt; ++i)
00664     {
00665         if (!(lt &= vs1.v_[i] < vs2.v_[i])) break;
00666     }
00667     return lt;
00668 }
00669 
00670 
00671 template<class Form, class Cmpt, int nCmpt>
00672 inline bool operator>=
00673 (
00674     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00675     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00676 )
00677 {
00678     return !(vs1 < vs2);
00679 }
00680 
00681 
00682 template<class Form, class Cmpt, int nCmpt>
00683 inline bool operator<=
00684 (
00685     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00686     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00687 )
00688 {
00689     return !(vs1 > vs2);
00690 }
00691 
00692 
00693 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00694 
00695 } // End namespace Foam
00696 
00697 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd