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

tensor2D.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 "tensor2D.H"
00028 #include "mathematicalConstants.H"
00029 
00030 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00031 
00032 namespace Foam
00033 {
00034 
00035 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00036 
00037 template<>
00038 const char* const tensor2D::typeName = "tensor2D";
00039 
00040 template<>
00041 const char* tensor2D::componentNames[] =
00042 {
00043     "xx", "xy",
00044     "yx", "yy"
00045 };
00046 
00047 template<>
00048 const tensor2D tensor2D::zero
00049 (
00050     0, 0,
00051     0, 0
00052 );
00053 
00054 template<>
00055 const tensor2D tensor2D::one
00056 (
00057     1, 1,
00058     1, 1
00059 );
00060 
00061 template<>
00062 const tensor2D tensor2D::max
00063 (
00064     VGREAT, VGREAT,
00065     VGREAT, VGREAT
00066 );
00067 
00068 template<>
00069 const tensor2D tensor2D::min
00070 (
00071     -VGREAT, -VGREAT,
00072     -VGREAT, -VGREAT
00073 );
00074 
00075 
00076 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00077 
00078 // Return eigenvalues in ascending order of absolute values
00079 vector2D eigenValues(const tensor2D& t)
00080 {
00081     scalar i = 0;
00082     scalar ii = 0;
00083 
00084     if (mag(t.xy()) < SMALL && mag(t.yx()) < SMALL)
00085     {
00086         i = t.xx();
00087         ii = t.yy();
00088     }
00089     else
00090     {
00091         scalar mb = t.xx() + t.yy();
00092         scalar c = t.xx()*t.yy() - t.xy()*t.yx();
00093 
00094         // If there is a zero root
00095         if (mag(c) < SMALL)
00096         {
00097             i = 0;
00098             ii = mb;
00099         }
00100         else
00101         {
00102             scalar disc = sqr(mb) - 4*c;
00103 
00104             if (disc > 0)
00105             {
00106                 scalar q = sqrt(disc);
00107 
00108                 i = 0.5*(mb - q);
00109                 ii = 0.5*(mb + q);
00110             }
00111             else
00112             {
00113                 FatalErrorIn("eigenValues(const tensor2D&)")
00114                     << "zero and complex eigenvalues in tensor2D: " << t
00115                     << abort(FatalError);
00116             }
00117         }
00118     }
00119 
00120     // Sort the eigenvalues into ascending order
00121     if (mag(i) > mag(ii))
00122     {
00123         Swap(i, ii);
00124     }
00125 
00126     return vector2D(i, ii);
00127 }
00128 
00129 
00130 vector2D eigenVector(const tensor2D& t, const scalar lambda)
00131 {
00132     if (lambda < SMALL)
00133     {
00134         return vector2D::zero;
00135     }
00136 
00137     if (mag(t.xy()) < SMALL && mag(t.yx()) < SMALL)
00138     {
00139         if (lambda > min(t.xx(), t.yy()))
00140         {
00141             return vector2D(1, 0);
00142         }
00143         else
00144         {
00145             return vector2D(0, 1);
00146         }
00147     }
00148     else if (mag(t.xy()) < SMALL)
00149     {
00150         return vector2D(lambda - t.yy(), t.yx());
00151     }
00152     else
00153     {
00154         return vector2D(t.xy(), lambda - t.yy());
00155     }
00156 }
00157 
00158 
00159 tensor2D eigenVectors(const tensor2D& t)
00160 {
00161     vector2D evals(eigenValues(t));
00162 
00163     tensor2D evs;
00164     evs.x() = eigenVector(t, evals.x());
00165     evs.y() = eigenVector(t, evals.y());
00166 
00167     return evs;
00168 }
00169 
00170 
00171 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00172 
00173 } // End namespace Foam
00174 
00175 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd