OpenFOAM logo
Open Source CFD Toolkit

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-2005 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 const Cmpt& VectorSpace<Form, Cmpt, nCmpt>::component
00058 (
00059     const direction d
00060 ) const
00061 {
00062 #   ifdef FULLDEBUG
00063     if (d >= nCmpt)
00064     {
00065         FatalErrorIn
00066         (
00067             "VectorSpace<Form, Cmpt, nCmpt>::component(direction) const"
00068         )   << "index out of range"
00069             << abort(FatalError);
00070     }
00071 #   endif
00072 
00073     return v_[d];
00074 }
00075 
00076 
00077 template<class Form, class Cmpt, int nCmpt>
00078 inline Cmpt& VectorSpace<Form, Cmpt, nCmpt>::component
00079 (
00080     const direction d
00081 )
00082 {
00083 #   ifdef FULLDEBUG
00084     if (d >= nCmpt)
00085     {
00086         FatalErrorIn("VectorSpace<Form, Cmpt, nCmpt>::component(direction)")
00087             << "index out of range"
00088             << abort(FatalError);
00089     }
00090 #   endif
00091 
00092     return v_[d];
00093 }
00094 
00095 
00096 template<class Form, class Cmpt, int nCmpt>
00097 inline void VectorSpace<Form, Cmpt, nCmpt>::component
00098 (
00099     Cmpt& c,
00100     const direction d
00101 ) const
00102 {
00103 #   ifdef FULLDEBUG
00104     if (d >= nCmpt)
00105     {
00106         FatalErrorIn
00107         (
00108             "VectorSpace<Form, Cmpt, nCmpt>::component(Cmpt&, direction) const"
00109         )   << "index out of range"
00110             << abort(FatalError);
00111     }
00112 #   endif
00113 
00114     c = v_[d];
00115 }
00116 
00117 
00118 template<class Form, class Cmpt, int nCmpt>
00119 inline void VectorSpace<Form, Cmpt, nCmpt>::replace
00120 (
00121     const direction d,
00122     const Cmpt& c
00123 )
00124 {
00125 #   ifdef FULLDEBUG
00126     if (d >= nCmpt)
00127     {
00128         FatalErrorIn
00129         (
00130             "VectorSpace<Form, Cmpt, nCmpt>::"
00131             "replace(direction, const Cmpt&) const"
00132         )   << "index out of range"
00133             << abort(FatalError);
00134     }
00135 #   endif
00136 
00137     v_[d] = c;
00138 }
00139 
00140 
00141 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00142 
00143 template<class Form, class Cmpt, int nCmpt>
00144 inline const Cmpt& VectorSpace<Form, Cmpt, nCmpt>::operator[]
00145 (
00146     const direction d
00147 ) const
00148 {
00149 #   ifdef FULLDEBUG
00150     if (d >= nCmpt)
00151     {
00152         FatalErrorIn
00153         (
00154             "VectorSpace<Form, Cmpt, nCmpt>::operator[](direction d) const"
00155         )   << "index out of range"
00156             << abort(FatalError);
00157     }
00158 #   endif
00159 
00160     return v_[d];
00161 }
00162 
00163 
00164 template<class Form, class Cmpt, int nCmpt>
00165 inline Cmpt& VectorSpace<Form, Cmpt, nCmpt>::operator[]
00166 (
00167     const direction d
00168 )
00169 {
00170 #   ifdef FULLDEBUG
00171     if (d >= nCmpt)
00172     {
00173         FatalErrorIn("VectorSpace<Form, Cmpt, nCmpt>::operator[](direction d)")
00174             << "index out of range"
00175             << abort(FatalError);
00176     }
00177 #   endif
00178 
00179     return v_[d];
00180 }
00181 
00182 
00183 template<class Form, class Cmpt, int nCmpt>
00184 inline void VectorSpace<Form, Cmpt, nCmpt>::operator=
00185 (
00186     const VectorSpace<Form, Cmpt, nCmpt>& vs
00187 )
00188 {
00189     VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, eqOp<Cmpt>());
00190 }
00191 
00192 
00193 template<class Form, class Cmpt, int nCmpt>
00194 inline void VectorSpace<Form, Cmpt, nCmpt>::operator+=
00195 (
00196     const VectorSpace<Form, Cmpt, nCmpt>& vs
00197 )
00198 {
00199     VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, plusEqOp<Cmpt>());
00200 }
00201 
00202 
00203 template<class Form, class Cmpt, int nCmpt>
00204 inline void VectorSpace<Form, Cmpt, nCmpt>::operator-=
00205 (
00206     const VectorSpace<Form, Cmpt, nCmpt>& vs
00207 )
00208 {
00209     VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, minusEqOp<Cmpt>());
00210 }
00211 
00212 
00213 template<class Form, class Cmpt, int nCmpt>
00214 inline void VectorSpace<Form, Cmpt, nCmpt>::operator*=
00215 (
00216     const scalar s
00217 )
00218 {
00219     VectorSpaceOps<nCmpt,0>::eqOpS(*this, s, multiplyEqOp2<Cmpt, scalar>());
00220 }
00221 
00222 
00223 template<class Form, class Cmpt, int nCmpt>
00224 inline void VectorSpace<Form, Cmpt, nCmpt>::operator/=
00225 (
00226     const scalar s
00227 )
00228 {
00229     VectorSpaceOps<nCmpt,0>::eqOpS(*this, s, divideEqOp2<Cmpt, scalar>());
00230 }
00231 
00232 
00233 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
00234 
00235 template<class Form, class Cmpt, int nCmpt>
00236 inline Cmpt& setComponent
00237 (
00238     VectorSpace<Form, Cmpt, nCmpt>& vs,
00239     const direction d
00240 )
00241 {
00242     return vs.component(d);
00243 }
00244 
00245 
00246 template<class Form, class Cmpt, int nCmpt>
00247 inline const Cmpt& component
00248 (
00249     const VectorSpace<Form, Cmpt, nCmpt>& vs,
00250     const direction d
00251 )
00252 {
00253     return vs.component(d);
00254 }
00255 
00256 
00257 // Powers of a Form
00258 // Equivalent to outer-products between the Form and itself
00259 
00260 // Form^0 = 1.0
00261 template<class Form, class Cmpt, int nCmpt>
00262 inline typename powProduct<Form, 0>::type pow
00263 (
00264     const VectorSpace<Form, Cmpt, nCmpt>&,
00265     typename powProduct<Form, 0>::type
00266   = pTraits<typename powProduct<Form, 0>::type>::zero
00267 )
00268 {
00269     return 1.0;
00270 }
00271 
00272 // Form^1 = Form
00273 template<class Form, class Cmpt, int nCmpt>
00274 inline typename powProduct<Form, 1>::type pow
00275 (
00276     const VectorSpace<Form, Cmpt, nCmpt>& v,
00277     typename powProduct<Form, 1>::type
00278   = pTraits<typename powProduct<Form, 1>::type>::zero
00279 )
00280 {
00281     return static_cast<const Form&>(v);
00282 }
00283 
00284 // sqr(Form) = Form*Form
00285 template<class Form, class Cmpt, int nCmpt>
00286 inline typename powProduct<Form, 2>::type sqr
00287 (
00288     const VectorSpace<Form, Cmpt, nCmpt>& v
00289 )
00290 {
00291     return static_cast<const Form&>(v)*static_cast<const Form&>(v);
00292 }
00293 
00294 // Form^2 = sqr(Form)
00295 template<class Form, class Cmpt, int nCmpt>
00296 inline typename powProduct<Form, 2>::type pow
00297 (
00298     const VectorSpace<Form, Cmpt, nCmpt>& v,
00299     typename powProduct<Form, 2>::type
00300   = pTraits<typename powProduct<Form, 2>::type>::zero
00301 )
00302 {
00303     return sqr(v);
00304 }
00305 
00306 /* Form^3 = Form*sqr(Form)
00307 template<class Form, class Cmpt, int nCmpt>
00308 inline typename powProduct<Form, 3>::type pow
00309 (
00310     const VectorSpace<Form, Cmpt, nCmpt>& v,
00311     typename powProduct<Form, 3>::type
00312   = pTraits<typename powProduct<Form, 3>::type>::zero
00313 )
00314 {
00315     return ((const Form&)v)*sqr(v);
00316 }*/
00317 
00318 /* Form^4 = sqr(sqr(Form))
00319 template<class Form, class Cmpt, int nCmpt>
00320 inline typename powProduct<Form, 4>::type pow
00321 (
00322     const VectorSpace<Form, Cmpt, nCmpt>& v,
00323     typename powProduct<Form, 4>::type
00324   = pTraits<typename powProduct<Form, 4>::type>::zero
00325 )
00326 {
00327     return sqr(sqr(v));
00328 }*/
00329 
00330 
00331 template<class Form, class Cmpt, int nCmpt>
00332 inline scalar magSqr
00333 (
00334     const VectorSpace<Form, Cmpt, nCmpt>& vs
00335 )
00336 {
00337     scalar ms = magSqr(vs.v_[0]);
00338     VectorSpaceOps<nCmpt,1>::SeqOp(ms, vs, plusEqMagSqrOp2<scalar, Cmpt>());
00339     return ms;
00340 }
00341 
00342 
00343 template<class Form, class Cmpt, int nCmpt>
00344 inline scalar mag
00345 (
00346     const VectorSpace<Form, Cmpt, nCmpt>& vs
00347 )
00348 {
00349     return ::sqrt(magSqr(vs));
00350 }
00351 
00352 
00353 template<class Form, class Cmpt, int nCmpt>
00354 inline Cmpt cmptMax
00355 (
00356     const VectorSpace<Form, Cmpt, nCmpt>& vs
00357 )
00358 {
00359     Cmpt cMax = vs.v_[0];
00360     VectorSpaceOps<nCmpt,1>::SeqOp(cMax, vs, maxEqOp<Cmpt>());
00361     return cMax;
00362 }
00363 
00364 
00365 template<class Form, class Cmpt, int nCmpt>
00366 inline Cmpt cmptMin
00367 (
00368     const VectorSpace<Form, Cmpt, nCmpt>& vs
00369 )
00370 {
00371     Cmpt cMin = vs.v_[0];
00372     VectorSpaceOps<nCmpt,1>::SeqOp(cMin, vs, minEqOp<Cmpt>());
00373     return cMin;
00374 }
00375 
00376 
00377 template<class Form, class Cmpt, int nCmpt>
00378 inline Cmpt cmptSum
00379 (
00380     const VectorSpace<Form, Cmpt, nCmpt>& vs
00381 )
00382 {
00383     Cmpt sum = vs.v_[0];
00384     VectorSpaceOps<nCmpt,1>::SeqOp(sum, vs, plusEqOp<Cmpt>());
00385     return sum;
00386 }
00387 
00388 
00389 template<class Form, class Cmpt, int nCmpt>
00390 inline Cmpt cmptAv
00391 (
00392     const VectorSpace<Form, Cmpt, nCmpt>& vs
00393 )
00394 {
00395     return cmptSum(vs)/nCmpt;
00396 }
00397 
00398 
00399 template<class Form, class Cmpt, int nCmpt>
00400 inline Form cmptMag
00401 (
00402     const VectorSpace<Form, Cmpt, nCmpt>& vs
00403 )
00404 {
00405     Form v;
00406     VectorSpaceOps<nCmpt,0>::eqOp(v, vs, eqMagOp<Cmpt>());
00407     return v;
00408 }
00409 
00410 
00411 template<class Form, class Cmpt, int nCmpt>
00412 inline Form max
00413 (
00414     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00415     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00416 )
00417 {
00418     Form v;
00419     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, maxOp<Cmpt>());
00420     return v;
00421 }
00422 
00423 
00424 template<class Form, class Cmpt, int nCmpt>
00425 inline Form min
00426 (
00427     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00428     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00429 )
00430 {
00431     Form v;
00432     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, minOp<Cmpt>());
00433     return v;
00434 }
00435 
00436 
00437 template<class Form, class Cmpt, int nCmpt>
00438 inline VectorSpace<Form, Cmpt, nCmpt> scale
00439 (
00440     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00441     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00442 )
00443 {
00444     Form v;
00445     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, scaleOp<Cmpt>());
00446     return v;
00447 }
00448 
00449 
00450 // * * * * * * * * * * * * * * * Global Operators  * * * * * * * * * * * * * //
00451 
00452 template<class Form, class Cmpt, int nCmpt>
00453 inline Form operator-
00454 (
00455     const VectorSpace<Form, Cmpt, nCmpt>& vs
00456 )
00457 {
00458     Form v;
00459     VectorSpaceOps<nCmpt,0>::eqOp(v, vs, eqMinusOp<Cmpt>());
00460     return v;
00461 }
00462 
00463 
00464 template<class Form, class Cmpt, int nCmpt>
00465 inline Form operator+
00466 (
00467     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00468     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00469 )
00470 {
00471     Form v;
00472     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, plusOp<Cmpt>());
00473     return v;
00474 }
00475 
00476 
00477 template<class Form, class Cmpt, int nCmpt>
00478 inline Form operator-
00479 (
00480     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00481     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00482 )
00483 {
00484     Form v;
00485     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, minusOp<Cmpt>());
00486     return v;
00487 }
00488 
00489 
00490 template<class Form, class Cmpt, int nCmpt>
00491 inline Form operator*
00492 (
00493     scalar s,
00494     const VectorSpace<Form, Cmpt, nCmpt>& vs
00495 )
00496 {
00497     Form v;
00498     VectorSpaceOps<nCmpt,0>::opSV(v, s, vs, multiplyOp3<Cmpt, scalar, Cmpt>());
00499     return v;
00500 }
00501 
00502 
00503 template<class Form, class Cmpt, int nCmpt>
00504 inline Form operator*
00505 (
00506     const VectorSpace<Form, Cmpt, nCmpt>& vs,
00507     scalar s
00508 )
00509 {
00510     Form v;
00511     VectorSpaceOps<nCmpt,0>::opVS(v, vs, s, multiplyOp3<Cmpt, Cmpt, scalar>());
00512     return v;
00513 }
00514 
00515 
00516 template<class Form, class Cmpt, int nCmpt>
00517 inline Form operator/
00518 (
00519     const VectorSpace<Form, Cmpt, nCmpt>& vs,
00520     scalar s
00521 )
00522 {
00523     Form v;
00524     VectorSpaceOps<nCmpt,0>::opVS(v, vs, s, divideOp3<Cmpt, Cmpt, scalar>());
00525     return v;
00526 }
00527 
00528 /*
00529 template<class Form, class Cmpt, int nCmpt>
00530 inline Form operator/
00531 (
00532     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00533     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00534 )
00535 {
00536     Form v;
00537     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, divideOp<Cmpt>());
00538     return v;
00539 }
00540 
00541 
00542 template<class Form, class Cmpt, int nCmpt>
00543 inline Form operator/
00544 (
00545     scalar s,
00546     const VectorSpace<Form, Cmpt, nCmpt>& vs
00547 )
00548 {
00549     Form v;
00550     VectorSpaceOps<nCmpt,0>::opSV(v, s, vs, divideOp2<scalar, Cmpt>());
00551     return v;
00552 }
00553 */
00554 
00555 template<class Form, class Cmpt, int nCmpt>
00556 inline Cmpt operator&&
00557 (
00558     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00559     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00560 )
00561 {
00562     Cmpt ddProd = vs1.v_[0]*vs2.v_[0];
00563     for (int i=1; i<nCmpt; ++i)
00564     {
00565         ddProd += vs1.v_[i]*vs2.v_[i];
00566     }
00567     return ddProd;
00568 }
00569 
00570 
00571 template<class Form, class Cmpt, int nCmpt>
00572 inline bool operator==
00573 (
00574     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00575     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00576 )
00577 {
00578     bool eq = true;
00579     for (int i=0; i<nCmpt; ++i)
00580     {
00581         if (!(eq &= (equal(vs1.v_[i], vs2.v_[i])))) break;
00582     }
00583     return eq;
00584 }
00585 
00586 
00587 template<class Form, class Cmpt, int nCmpt>
00588 inline bool operator!=
00589 (
00590     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00591     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00592 )
00593 {
00594     return !(vs1 == vs2);
00595 }
00596 
00597 
00598 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00599 
00600 } // End namespace Foam
00601 
00602 // ************************************************************************* //
For further information go to www.openfoam.org