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

FitData.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 "FitData.H"
00028 #include "surfaceFields.H"
00029 #include "volFields.H"
00030 #include "SVD.H"
00031 
00032 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
00033 
00034 template<class Form, class ExtendedStencil, class Polynomial>
00035 Foam::FitData<Form, ExtendedStencil, Polynomial>::FitData
00036 (
00037     const fvMesh& mesh,
00038     const ExtendedStencil& stencil,
00039     const bool linearCorrection,
00040     const scalar linearLimitFactor,
00041     const scalar centralWeight
00042 )
00043 :
00044     MeshObject<fvMesh, Form>(mesh),
00045     stencil_(stencil),
00046     linearCorrection_(linearCorrection),
00047     linearLimitFactor_(linearLimitFactor),
00048     centralWeight_(centralWeight),
00049 #   ifdef SPHERICAL_GEOMETRY
00050     dim_(2),
00051 #   else
00052     dim_(mesh.nGeometricD()),
00053 #   endif
00054     minSize_(Polynomial::nTerms(dim_))
00055 {
00056     // Check input
00057     if (linearLimitFactor <= SMALL || linearLimitFactor > 3)
00058     {
00059         FatalErrorIn("FitData<Polynomial>::FitData(..)")
00060             << "linearLimitFactor requested = " << linearLimitFactor
00061             << " should be between zero and 3"
00062             << exit(FatalError);
00063     }
00064 }
00065 
00066 
00067 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00068 
00069 template<class FitDataType, class ExtendedStencil, class Polynomial>
00070 void Foam::FitData<FitDataType, ExtendedStencil, Polynomial>::findFaceDirs
00071 (
00072     vector& idir,        // value changed in return
00073     vector& jdir,        // value changed in return
00074     vector& kdir,        // value changed in return
00075     const label facei
00076 )
00077 {
00078     const fvMesh& mesh = this->mesh();
00079 
00080     idir = mesh.faceAreas()[facei];
00081     idir /= mag(idir);
00082 
00083 #   ifndef SPHERICAL_GEOMETRY
00084     if (mesh.nGeometricD() <= 2) // find the normal direction
00085     {
00086         if (mesh.geometricD()[0] == -1)
00087         {
00088             kdir = vector(1, 0, 0);
00089         }
00090         else if (mesh.geometricD()[1] == -1)
00091         {
00092             kdir = vector(0, 1, 0);
00093         }
00094         else
00095         {
00096             kdir = vector(0, 0, 1);
00097         }
00098     }
00099     else // 3D so find a direction in the plane of the face
00100     {
00101         const face& f = mesh.faces()[facei];
00102         kdir = mesh.points()[f[0]] - mesh.faceCentres()[facei];
00103     }
00104 #   else
00105     // Spherical geometry so kdir is the radial direction
00106     kdir = mesh.faceCentres()[facei];
00107 #   endif
00108 
00109     if (mesh.nGeometricD() == 3)
00110     {
00111         // Remove the idir component from kdir and normalise
00112         kdir -= (idir & kdir)*idir;
00113 
00114         scalar magk = mag(kdir);
00115 
00116         if (magk < SMALL)
00117         {
00118             FatalErrorIn("findFaceDirs(..)") << " calculated kdir = zero"
00119                 << exit(FatalError);
00120         }
00121         else
00122         {
00123             kdir /= magk;
00124         }
00125     }
00126 
00127     jdir = kdir ^ idir;
00128 }
00129 
00130 
00131 template<class FitDataType, class ExtendedStencil, class Polynomial>
00132 void Foam::FitData<FitDataType, ExtendedStencil, Polynomial>::calcFit
00133 (
00134     scalarList& coeffsi,
00135     const List<point>& C,
00136     const scalar wLin,
00137     const label facei
00138 )
00139 {
00140     vector idir(1,0,0);
00141     vector jdir(0,1,0);
00142     vector kdir(0,0,1);
00143     findFaceDirs(idir, jdir, kdir, facei);
00144 
00145     // Setup the point weights
00146     scalarList wts(C.size(), scalar(1));
00147     wts[0] = centralWeight_;
00148     if (linearCorrection_)
00149     {
00150         wts[1] = centralWeight_;
00151     }
00152 
00153     // Reference point
00154     point p0 = this->mesh().faceCentres()[facei];
00155 
00156     // Info << "Face " << facei << " at " << p0 << " stencil points at:\n"
00157     //     << C - p0 << endl;
00158 
00159     // p0 -> p vector in the face-local coordinate system
00160     vector d;
00161 
00162     // Local coordinate scaling
00163     scalar scale = 1;
00164 
00165     // Matrix of the polynomial components
00166     scalarRectangularMatrix B(C.size(), minSize_, scalar(0));
00167 
00168     for(label ip = 0; ip < C.size(); ip++)
00169     {
00170         const point& p = C[ip];
00171 
00172         d.x() = (p - p0)&idir;
00173         d.y() = (p - p0)&jdir;
00174 #       ifndef SPHERICAL_GEOMETRY
00175         d.z() = (p - p0)&kdir;
00176 #       else
00177         d.z() = mag(p) - mag(p0);
00178 #       endif
00179 
00180         if (ip == 0)
00181         {
00182             scale = cmptMax(cmptMag((d)));
00183         }
00184 
00185         // Scale the radius vector
00186         d /= scale;
00187 
00188         Polynomial::addCoeffs
00189         (
00190             B[ip],
00191             d,
00192             wts[ip],
00193             dim_
00194         );
00195     }
00196 
00197     // Additional weighting for constant and linear terms
00198     for(label i = 0; i < B.n(); i++)
00199     {
00200         B[i][0] *= wts[0];
00201         B[i][1] *= wts[0];
00202     }
00203 
00204     // Set the fit
00205     label stencilSize = C.size();
00206     coeffsi.setSize(stencilSize);
00207 
00208     bool goodFit = false;
00209     for(int iIt = 0; iIt < 8 && !goodFit; iIt++)
00210     {
00211         SVD svd(B, SMALL);
00212 
00213         scalar maxCoeff = 0;
00214         label maxCoeffi = 0;
00215 
00216         for(label i=0; i<stencilSize; i++)
00217         {
00218             coeffsi[i] = wts[0]*wts[i]*svd.VSinvUt()[0][i];
00219             if (mag(coeffsi[i]) > maxCoeff)
00220             {
00221                 maxCoeff = mag(coeffsi[i]);
00222                 maxCoeffi = i;
00223             }
00224         }
00225 
00226         if (linearCorrection_)
00227         {
00228             goodFit =
00229                 (mag(coeffsi[0] - wLin) < linearLimitFactor_*wLin)
00230              && (mag(coeffsi[1] - (1 - wLin)) < linearLimitFactor_*(1 - wLin))
00231              && maxCoeffi <= 1;
00232         }
00233         else
00234         {
00235             // Upwind: weight on face is 1.
00236             goodFit =
00237                 (mag(coeffsi[0] - 1.0) < linearLimitFactor_*1.0)
00238              && maxCoeffi <= 1;
00239         }
00240 
00241         // if (goodFit && iIt > 0)
00242         // {
00243             // Info << "FitData<Polynomial>::calcFit"
00244             //     << "(const List<point>& C, const label facei" << nl
00245             //     << "Can now fit face " << facei << " iteration " << iIt
00246             //     << " with sum of weights " << sum(coeffsi) << nl
00247             //     << "    Weights " << coeffsi << nl
00248             //     << "    Linear weights " << wLin << " " << 1 - wLin << nl
00249             //     << "    sing vals " << svd.S() << endl;
00250         // }
00251 
00252         if (!goodFit) // (not good fit so increase weight in the centre and weight
00253                       //  for constant and linear terms)
00254         {
00255             // if (iIt == 7)
00256             // {
00257             //     WarningIn
00258             //     (
00259             //         "FitData<Polynomial>::calcFit"
00260             //         "(const List<point>& C, const label facei"
00261             //     )   << "Cannot fit face " << facei << " iteration " << iIt
00262             //         << " with sum of weights " << sum(coeffsi) << nl
00263             //         << "    Weights " << coeffsi << nl
00264             //         << "    Linear weights " << wLin << " " << 1 - wLin << nl
00265             //         << "    sing vals " << svd.S() << endl;
00266             // }
00267 
00268             wts[0] *= 10;
00269             if (linearCorrection_)
00270             {
00271                 wts[1] *= 10;
00272             }
00273 
00274             for(label j = 0; j < B.m(); j++)
00275             {
00276                 B[0][j] *= 10;
00277                 B[1][j] *= 10;
00278             }
00279 
00280             for(label i = 0; i < B.n(); i++)
00281             {
00282                 B[i][0] *= 10;
00283                 B[i][1] *= 10;
00284             }
00285         }
00286     }
00287 
00288     if (goodFit)
00289     {
00290         if (linearCorrection_)
00291         {
00292             // Remove the uncorrected linear coefficients
00293             coeffsi[0] -= wLin;
00294             coeffsi[1] -= 1 - wLin;
00295         }
00296         else
00297         {
00298             // Remove the uncorrected upwind coefficients
00299             coeffsi[0] -= 1.0;
00300         }
00301     }
00302     else
00303     {
00304         // if (debug)
00305         // {
00306             WarningIn
00307             (
00308                 "FitData<Polynomial>::calcFit(..)"
00309             )   << "Could not fit face " << facei
00310                 << "    Weights = " << coeffsi
00311                 << ", reverting to linear." << nl
00312                 << "    Linear weights " << wLin << " " << 1 - wLin << endl;
00313         // }
00314 
00315         coeffsi = 0;
00316     }
00317 }
00318 
00319 
00320 template<class FitDataType, class ExtendedStencil, class Polynomial>
00321 bool Foam::FitData<FitDataType, ExtendedStencil, Polynomial>::movePoints()
00322 {
00323     calcFit();
00324     return true;
00325 }
00326 
00327 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd