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

tensor.C

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 "tensor.H"
00028 #include "mathematicalConstants.H"
00029 
00030 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00031 
00032 namespace Foam
00033 {
00034 
00035 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00036 
00037 template<>
00038 const char* const tensor::typeName = "tensor";
00039 
00040 template<>
00041 const char* tensor::componentNames[] =
00042 {
00043     "xx", "xy", "xz",
00044     "yx", "yy", "yz",
00045     "zx", "zy", "zz"
00046 };
00047 
00048 template<>
00049 const tensor tensor::zero
00050 (
00051     0, 0, 0,
00052     0, 0, 0,
00053     0, 0, 0
00054 );
00055 
00056 template<>
00057 const tensor tensor::one
00058 (
00059     1, 1, 1,
00060     1, 1, 1,
00061     1, 1, 1
00062 );
00063 
00064 template<>
00065 const tensor tensor::max
00066 (
00067     VGREAT, VGREAT, VGREAT,
00068     VGREAT, VGREAT, VGREAT,
00069     VGREAT, VGREAT, VGREAT
00070 );
00071 
00072 template<>
00073 const tensor tensor::min
00074 (
00075     -VGREAT, -VGREAT, -VGREAT,
00076     -VGREAT, -VGREAT, -VGREAT,
00077     -VGREAT, -VGREAT, -VGREAT
00078 );
00079 
00080 
00081 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00082 
00083 // Return eigenvalues in ascending order of absolute values
00084 vector eigenValues(const tensor& t)
00085 {
00086     scalar i = 0;
00087     scalar ii = 0;
00088     scalar iii = 0;
00089 
00090     if
00091     (
00092         (
00093             mag(t.xy()) + mag(t.xz()) + mag(t.yx())
00094           + mag(t.yz()) + mag(t.zx()) + mag(t.zy())
00095         )
00096       < SMALL
00097     )
00098     {
00099         // diagonal matrix
00100         i = t.xx();
00101         ii = t.yy();
00102         iii = t.zz();
00103     }
00104     else
00105     {
00106         scalar a = -t.xx() - t.yy() - t.zz();
00107 
00108         scalar b = t.xx()*t.yy() + t.xx()*t.zz() + t.yy()*t.zz()
00109             - t.xy()*t.yx() - t.xz()*t.zx() - t.yz()*t.zy();
00110 
00111         scalar c = - t.xx()*t.yy()*t.zz() - t.xy()*t.yz()*t.zx()
00112             - t.xz()*t.yx()*t.zy() + t.xz()*t.yy()*t.zx()
00113             + t.xy()*t.yx()*t.zz() + t.xx()*t.yz()*t.zy();
00114 
00115         // If there is a zero root
00116         if (mag(c) < 1.0e-100)
00117         {
00118             scalar disc = sqr(a) - 4*b;
00119 
00120             if (disc >= -SMALL)
00121             {
00122                 scalar q = -0.5*sqrt(max(0.0, disc));
00123 
00124                 i = 0;
00125                 ii = -0.5*a + q;
00126                 iii = -0.5*a - q;
00127             }
00128             else
00129             {
00130                 FatalErrorIn("eigenValues(const tensor&)")
00131                     << "zero and complex eigenvalues in tensor: " << t
00132                     << abort(FatalError);
00133             }
00134         }
00135         else
00136         {
00137             scalar Q = (a*a - 3*b)/9;
00138             scalar R = (2*a*a*a - 9*a*b + 27*c)/54;
00139 
00140             scalar R2 = sqr(R);
00141             scalar Q3 = pow3(Q);
00142 
00143             // Three different real roots
00144             if (R2 < Q3)
00145             {
00146                 scalar sqrtQ = sqrt(Q);
00147                 scalar theta = acos(R/(Q*sqrtQ));
00148 
00149                 scalar m2SqrtQ = -2*sqrtQ;
00150                 scalar aBy3 = a/3;
00151 
00152                 i = m2SqrtQ*cos(theta/3) - aBy3;
00153                 ii = m2SqrtQ*cos((theta + mathematicalConstant::twoPi)/3)
00154                     - aBy3;
00155                 iii = m2SqrtQ*cos((theta - mathematicalConstant::twoPi)/3)
00156                     - aBy3;
00157             }
00158             else
00159             {
00160                 scalar A = cbrt(R + sqrt(R2 - Q3));
00161 
00162                 // Three equal real roots
00163                 if (A < SMALL)
00164                 {
00165                     scalar root = -a/3;
00166                     return vector(root, root, root);
00167                 }
00168                 else
00169                 {
00170                     // Complex roots
00171                     WarningIn("eigenValues(const tensor&)")
00172                         << "complex eigenvalues detected for tensor: " << t
00173                         << endl;
00174 
00175                     return vector::zero;
00176                 }
00177             }
00178         }
00179     }
00180 
00181 
00182     // Sort the eigenvalues into ascending order
00183     if (mag(i) > mag(ii))
00184     {
00185         Swap(i, ii);
00186     }
00187 
00188     if (mag(ii) > mag(iii))
00189     {
00190         Swap(ii, iii);
00191     }
00192 
00193     if (mag(i) > mag(ii))
00194     {
00195         Swap(i, ii);
00196     }
00197 
00198     return vector(i, ii, iii);
00199 }
00200 
00201 
00202 vector eigenVector(const tensor& t, const scalar lambda)
00203 {
00204     if (mag(lambda) < SMALL)
00205     {
00206         return vector::zero;
00207     }
00208 
00209     // Construct the matrix for the eigenvector problem
00210     tensor A(t - lambda*I);
00211 
00212     // Calculate the sub-determinants of the 3 components
00213     scalar sd0 = A.yy()*A.zz() - A.yz()*A.zy();
00214     scalar sd1 = A.xx()*A.zz() - A.xz()*A.zx();
00215     scalar sd2 = A.xx()*A.yy() - A.xy()*A.yx();
00216 
00217     scalar magSd0 = mag(sd0);
00218     scalar magSd1 = mag(sd1);
00219     scalar magSd2 = mag(sd2);
00220 
00221     // Evaluate the eigenvector using the largest sub-determinant
00222     if (magSd0 > magSd1 && magSd0 > magSd2 && magSd0 > SMALL)
00223     {
00224         vector ev
00225         (
00226             1,
00227             (A.yz()*A.zx() - A.zz()*A.yx())/sd0,
00228             (A.zy()*A.yx() - A.yy()*A.zx())/sd0
00229         );
00230         ev /= mag(ev);
00231 
00232         return ev;
00233     }
00234     else if (magSd1 > magSd2 && magSd1 > SMALL)
00235     {
00236         vector ev
00237         (
00238             (A.xz()*A.zy() - A.zz()*A.xy())/sd1,
00239             1,
00240             (A.zx()*A.xy() - A.xx()*A.zy())/sd1
00241         );
00242         ev /= mag(ev);
00243 
00244         return ev;
00245     }
00246     else if (magSd2 > SMALL)
00247     {
00248         vector ev
00249         (
00250             (A.xy()*A.yz() - A.yy()*A.xz())/sd2,
00251             (A.yx()*A.xz() - A.xx()*A.yz())/sd2,
00252             1
00253         );
00254         ev /= mag(ev);
00255 
00256         return ev;
00257     }
00258     else
00259     {
00260         return vector::zero;
00261     }
00262 }
00263 
00264 
00265 tensor eigenVectors(const tensor& t)
00266 {
00267     vector evals(eigenValues(t));
00268 
00269     tensor evs;
00270     evs.x() = eigenVector(t, evals.x());
00271     evs.y() = eigenVector(t, evals.y());
00272     evs.z() = eigenVector(t, evals.z());
00273 
00274     return evs;
00275 }
00276 
00277 
00278 // Return eigenvalues in ascending order of absolute values
00279 vector eigenValues(const symmTensor& t)
00280 {
00281     scalar i = 0;
00282     scalar ii = 0;
00283     scalar iii = 0;
00284 
00285     if
00286     (
00287         (
00288             mag(t.xy()) + mag(t.xz()) + mag(t.xy())
00289           + mag(t.yz()) + mag(t.xz()) + mag(t.yz())
00290         )
00291       < SMALL
00292     )
00293     {
00294         // diagonal matrix
00295         i = t.xx();
00296         ii = t.yy();
00297         iii = t.zz();
00298     }
00299     else
00300     {
00301         scalar a = -t.xx() - t.yy() - t.zz();
00302 
00303         scalar b = t.xx()*t.yy() + t.xx()*t.zz() + t.yy()*t.zz()
00304             - t.xy()*t.xy() - t.xz()*t.xz() - t.yz()*t.yz();
00305 
00306         scalar c = - t.xx()*t.yy()*t.zz() - t.xy()*t.yz()*t.xz()
00307             - t.xz()*t.xy()*t.yz() + t.xz()*t.yy()*t.xz()
00308             + t.xy()*t.xy()*t.zz() + t.xx()*t.yz()*t.yz();
00309 
00310         // If there is a zero root
00311         if (mag(c) < 1.0e-100)
00312         {
00313             scalar disc = sqr(a) - 4*b;
00314 
00315             if (disc >= -SMALL)
00316             {
00317                 scalar q = -0.5*sqrt(max(0.0, disc));
00318 
00319                 i = 0;
00320                 ii = -0.5*a + q;
00321                 iii = -0.5*a - q;
00322             }
00323             else
00324             {
00325                 FatalErrorIn("eigenValues(const tensor&)")
00326                     << "zero and complex eigenvalues in tensor: " << t
00327                     << abort(FatalError);
00328             }
00329         }
00330         else
00331         {
00332             scalar Q = (a*a - 3*b)/9;
00333             scalar R = (2*a*a*a - 9*a*b + 27*c)/54;
00334 
00335             scalar R2 = sqr(R);
00336             scalar Q3 = pow3(Q);
00337 
00338             // Three different real roots
00339             if (R2 < Q3)
00340             {
00341                 scalar sqrtQ = sqrt(Q);
00342                 scalar theta = acos(R/(Q*sqrtQ));
00343 
00344                 scalar m2SqrtQ = -2*sqrtQ;
00345                 scalar aBy3 = a/3;
00346 
00347                 i = m2SqrtQ*cos(theta/3) - aBy3;
00348                 ii = m2SqrtQ*cos((theta + mathematicalConstant::twoPi)/3)
00349                     - aBy3;
00350                 iii = m2SqrtQ*cos((theta - mathematicalConstant::twoPi)/3)
00351                     - aBy3;
00352             }
00353             else
00354             {
00355                 scalar A = cbrt(R + sqrt(R2 - Q3));
00356 
00357                 // Three equal real roots
00358                 if (A < SMALL)
00359                 {
00360                     scalar root = -a/3;
00361                     return vector(root, root, root);
00362                 }
00363                 else
00364                 {
00365                     // Complex roots
00366                     WarningIn("eigenValues(const symmTensor&)")
00367                         << "complex eigenvalues detected for symmTensor: " << t
00368                         << endl;
00369 
00370                     return vector::zero;
00371                 }
00372             }
00373         }
00374     }
00375 
00376 
00377     // Sort the eigenvalues into ascending order
00378     if (mag(i) > mag(ii))
00379     {
00380         Swap(i, ii);
00381     }
00382 
00383     if (mag(ii) > mag(iii))
00384     {
00385         Swap(ii, iii);
00386     }
00387 
00388     if (mag(i) > mag(ii))
00389     {
00390         Swap(i, ii);
00391     }
00392 
00393     return vector(i, ii, iii);
00394 }
00395 
00396 
00397 vector eigenVector(const symmTensor& t, const scalar lambda)
00398 {
00399     if (mag(lambda) < SMALL)
00400     {
00401         return vector::zero;
00402     }
00403 
00404     // Construct the matrix for the eigenvector problem
00405     symmTensor A(t - lambda*I);
00406 
00407     // Calculate the sub-determinants of the 3 components
00408     scalar sd0 = A.yy()*A.zz() - A.yz()*A.yz();
00409     scalar sd1 = A.xx()*A.zz() - A.xz()*A.xz();
00410     scalar sd2 = A.xx()*A.yy() - A.xy()*A.xy();
00411 
00412     scalar magSd0 = mag(sd0);
00413     scalar magSd1 = mag(sd1);
00414     scalar magSd2 = mag(sd2);
00415 
00416     // Evaluate the eigenvector using the largest sub-determinant
00417     if (magSd0 > magSd1 && magSd0 > magSd2 && magSd0 > SMALL)
00418     {
00419         vector ev
00420         (
00421             1,
00422             (A.yz()*A.xz() - A.zz()*A.xy())/sd0,
00423             (A.yz()*A.xy() - A.yy()*A.xz())/sd0
00424         );
00425         ev /= mag(ev);
00426 
00427         return ev;
00428     }
00429     else if (magSd1 > magSd2 && magSd1 > SMALL)
00430     {
00431         vector ev
00432         (
00433             (A.xz()*A.yz() - A.zz()*A.xy())/sd1,
00434             1,
00435             (A.xz()*A.xy() - A.xx()*A.yz())/sd1
00436         );
00437         ev /= mag(ev);
00438 
00439         return ev;
00440     }
00441     else if (magSd2 > SMALL)
00442     {
00443         vector ev
00444         (
00445             (A.xy()*A.yz() - A.yy()*A.xz())/sd2,
00446             (A.xy()*A.xz() - A.xx()*A.yz())/sd2,
00447             1
00448         );
00449         ev /= mag(ev);
00450 
00451         return ev;
00452     }
00453     else
00454     {
00455         return vector::zero;
00456     }
00457 }
00458 
00459 
00460 tensor eigenVectors(const symmTensor& t)
00461 {
00462     vector evals(eigenValues(t));
00463 
00464     tensor evs;
00465     evs.x() = eigenVector(t, evals.x());
00466     evs.y() = eigenVector(t, evals.y());
00467     evs.z() = eigenVector(t, evals.z());
00468 
00469     return evs;
00470 }
00471 
00472 
00473 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00474 
00475 } // End namespace Foam
00476 
00477 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd