FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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