OpenFOAM logo
The Open Source CFD Toolbox
  Programmer’s C++ Doxygen 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-2007 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 
00267 // Form^0 = 1.0
00268 template<class Form, class Cmpt, int nCmpt>
00269 inline typename powProduct<Form, 0>::type pow
00270 (
00271     const VectorSpace<Form, Cmpt, nCmpt>&,
00272     typename powProduct<Form, 0>::type
00273   = pTraits<typename powProduct<Form, 0>::type>::zero
00274 )
00275 {
00276     return 1.0;
00277 }
00278 
00279 // Form^1 = Form
00280 template<class Form, class Cmpt, int nCmpt>
00281 inline typename powProduct<Form, 1>::type pow
00282 (
00283     const VectorSpace<Form, Cmpt, nCmpt>& v,
00284     typename powProduct<Form, 1>::type
00285   = pTraits<typename powProduct<Form, 1>::type>::zero
00286 )
00287 {
00288     return static_cast<const Form&>(v);
00289 }
00290 
00291 
00292 // Form^2 = sqr(Form)
00293 template<class Form, class Cmpt, int nCmpt>
00294 inline typename powProduct<Form, 2>::type pow
00295 (
00296     const VectorSpace<Form, Cmpt, nCmpt>& v,
00297     typename powProduct<Form, 2>::type
00298   = pTraits<typename powProduct<Form, 2>::type>::zero
00299 )
00300 {
00301     return sqr(static_cast<const Form&>(v));
00302 }
00303 
00304 
00305 template<class Form, class Cmpt, int nCmpt>
00306 inline scalar magSqr
00307 (
00308     const VectorSpace<Form, Cmpt, nCmpt>& vs
00309 )
00310 {
00311     scalar ms = magSqr(vs.v_[0]);
00312     VectorSpaceOps<nCmpt,1>::SeqOp(ms, vs, plusEqMagSqrOp2<scalar, Cmpt>());
00313     return ms;
00314 }
00315 
00316 
00317 template<class Form, class Cmpt, int nCmpt>
00318 inline scalar mag
00319 (
00320     const VectorSpace<Form, Cmpt, nCmpt>& vs
00321 )
00322 {
00323     return ::sqrt(magSqr(static_cast<const Form&>(vs)));
00324 }
00325 
00326 
00327 template<class Form, class Cmpt, int nCmpt>
00328 inline Cmpt cmptMax
00329 (
00330     const VectorSpace<Form, Cmpt, nCmpt>& vs
00331 )
00332 {
00333     Cmpt cMax = vs.v_[0];
00334     VectorSpaceOps<nCmpt,1>::SeqOp(cMax, vs, maxEqOp<Cmpt>());
00335     return cMax;
00336 }
00337 
00338 
00339 template<class Form, class Cmpt, int nCmpt>
00340 inline Cmpt cmptMin
00341 (
00342     const VectorSpace<Form, Cmpt, nCmpt>& vs
00343 )
00344 {
00345     Cmpt cMin = vs.v_[0];
00346     VectorSpaceOps<nCmpt,1>::SeqOp(cMin, vs, minEqOp<Cmpt>());
00347     return cMin;
00348 }
00349 
00350 
00351 template<class Form, class Cmpt, int nCmpt>
00352 inline Cmpt cmptSum
00353 (
00354     const VectorSpace<Form, Cmpt, nCmpt>& vs
00355 )
00356 {
00357     Cmpt sum = vs.v_[0];
00358     VectorSpaceOps<nCmpt,1>::SeqOp(sum, vs, plusEqOp<Cmpt>());
00359     return sum;
00360 }
00361 
00362 
00363 template<class Form, class Cmpt, int nCmpt>
00364 inline Cmpt cmptAv
00365 (
00366     const VectorSpace<Form, Cmpt, nCmpt>& vs
00367 )
00368 {
00369     return cmptSum(vs)/nCmpt;
00370 }
00371 
00372 
00373 template<class Form, class Cmpt, int nCmpt>
00374 inline Form cmptMag
00375 (
00376     const VectorSpace<Form, Cmpt, nCmpt>& vs
00377 )
00378 {
00379     Form v;
00380     VectorSpaceOps<nCmpt,0>::eqOp(v, vs, eqMagOp<Cmpt>());
00381     return v;
00382 }
00383 
00384 
00385 template<class Form, class Cmpt, int nCmpt>
00386 inline Form max
00387 (
00388     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00389     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00390 )
00391 {
00392     Form v;
00393     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, maxOp<Cmpt>());
00394     return v;
00395 }
00396 
00397 
00398 template<class Form, class Cmpt, int nCmpt>
00399 inline Form min
00400 (
00401     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00402     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00403 )
00404 {
00405     Form v;
00406     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, minOp<Cmpt>());
00407     return v;
00408 }
00409 
00410 
00411 template<class Form, class Cmpt, int nCmpt>
00412 inline Form minMod
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, minModOp<Cmpt>());
00420     return v;
00421 }
00422 
00423 
00424 template<class Form, class Cmpt, int nCmpt>
00425 inline VectorSpace<Form, Cmpt, nCmpt> scale
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, scaleOp<Cmpt>());
00433     return v;
00434 }
00435 
00436 
00437 // * * * * * * * * * * * * * * * Global Operators  * * * * * * * * * * * * * //
00438 
00439 template<class Form, class Cmpt, int nCmpt>
00440 inline Form operator-
00441 (
00442     const VectorSpace<Form, Cmpt, nCmpt>& vs
00443 )
00444 {
00445     Form v;
00446     VectorSpaceOps<nCmpt,0>::eqOp(v, vs, eqMinusOp<Cmpt>());
00447     return v;
00448 }
00449 
00450 
00451 template<class Form, class Cmpt, int nCmpt>
00452 inline Form operator+
00453 (
00454     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00455     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00456 )
00457 {
00458     Form v;
00459     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, plusOp<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, minusOp<Cmpt>());
00473     return v;
00474 }
00475 
00476 
00477 template<class Form, class Cmpt, int nCmpt>
00478 inline Form operator*
00479 (
00480     scalar s,
00481     const VectorSpace<Form, Cmpt, nCmpt>& vs
00482 )
00483 {
00484     Form v;
00485     VectorSpaceOps<nCmpt,0>::opSV(v, s, vs, multiplyOp3<Cmpt, scalar, Cmpt>());
00486     return v;
00487 }
00488 
00489 
00490 template<class Form, class Cmpt, int nCmpt>
00491 inline Form operator*
00492 (
00493     const VectorSpace<Form, Cmpt, nCmpt>& vs,
00494     scalar s
00495 )
00496 {
00497     Form v;
00498     VectorSpaceOps<nCmpt,0>::opVS(v, vs, s, multiplyOp3<Cmpt, Cmpt, scalar>());
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, divideOp3<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>& vs1,
00520     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00521 )
00522 {
00523     Form v;
00524     VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, divideOp<Cmpt>());
00525     return v;
00526 }
00527 
00528 
00529 template<class Form, class Cmpt, int nCmpt>
00530 inline Form operator/
00531 (
00532     scalar s,
00533     const VectorSpace<Form, Cmpt, nCmpt>& vs
00534 )
00535 {
00536     Form v;
00537     VectorSpaceOps<nCmpt,0>::opSV(v, s, vs, divideOp2<scalar, Cmpt>());
00538     return v;
00539 }
00540 */
00541 
00542 template<class Form, class Cmpt, int nCmpt>
00543 inline Cmpt operator&&
00544 (
00545     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00546     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00547 )
00548 {
00549     Cmpt ddProd = vs1.v_[0]*vs2.v_[0];
00550     for (int i=1; i<nCmpt; ++i)
00551     {
00552         ddProd += vs1.v_[i]*vs2.v_[i];
00553     }
00554     return ddProd;
00555 }
00556 
00557 
00558 template<class Form, class Cmpt, int nCmpt>
00559 inline bool operator==
00560 (
00561     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00562     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00563 )
00564 {
00565     bool eq = true;
00566     for (int i=0; i<nCmpt; ++i)
00567     {
00568         if (!(eq &= (equal(vs1.v_[i], vs2.v_[i])))) break;
00569     }
00570     return eq;
00571 }
00572 
00573 
00574 template<class Form, class Cmpt, int nCmpt>
00575 inline bool operator!=
00576 (
00577     const VectorSpace<Form, Cmpt, nCmpt>& vs1,
00578     const VectorSpace<Form, Cmpt, nCmpt>& vs2
00579 )
00580 {
00581     return !(vs1 == vs2);
00582 }
00583 
00584 
00585 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00586 
00587 } // End namespace Foam
00588 
00589 // ************************************************************************* //
Copyright © 2000-2007 OpenCFD Ltd