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

quadraticFitSnGradData.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 "quadraticFitSnGradData.H"
00028 #include "surfaceFields.H"
00029 #include "volFields.H"
00030 #include "SVD.H"
00031 #include "syncTools.H"
00032 
00033 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00034 
00035 namespace Foam
00036 {
00037     defineTypeNameAndDebug(quadraticFitSnGradData, 0);
00038 }
00039 
00040 
00041 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
00042 
00043 Foam::quadraticFitSnGradData::quadraticFitSnGradData
00044 (
00045     const fvMesh& mesh,
00046     const scalar cWeight
00047 )
00048 :
00049     MeshObject<fvMesh, quadraticFitSnGradData>(mesh),
00050     centralWeight_(cWeight),
00051     #ifdef SPHERICAL_GEOMETRY
00052         dim_(2),
00053     #else
00054         dim_(mesh.nGeometricD()),
00055     #endif
00056     minSize_
00057     (
00058         dim_ == 1 ? 3 :
00059         dim_ == 2 ? 6 :
00060         dim_ == 3 ? 9 : 0
00061     ),
00062     stencil_(mesh),
00063     fit_(mesh.nInternalFaces())
00064 {
00065     if (debug)
00066     {
00067         Info << "Contructing quadraticFitSnGradData" << endl;
00068     }
00069 
00070     // check input
00071     if (centralWeight_ < 1 - SMALL)
00072     {
00073         FatalErrorIn("quadraticFitSnGradData::quadraticFitSnGradData")
00074             << "centralWeight requested = " << centralWeight_
00075             << " should not be less than one"
00076             << exit(FatalError);
00077     }
00078 
00079     if (minSize_ == 0)
00080     {
00081         FatalErrorIn("quadraticFitSnGradData")
00082             << " dimension must be 1,2 or 3, not" << dim_ << exit(FatalError);
00083     }
00084 
00085     // store the polynomial size for each face to write out
00086     surfaceScalarField snGradPolySize
00087     (
00088         IOobject
00089         (
00090             "quadraticFitSnGradPolySize",
00091             "constant",
00092             mesh,
00093             IOobject::NO_READ,
00094             IOobject::NO_WRITE
00095         ),
00096         mesh,
00097         dimensionedScalar("quadraticFitSnGradPolySize", dimless, scalar(0))
00098     );
00099 
00100     // Get the cell/face centres in stencil order.
00101     // Centred face stencils no good for triangles of tets. Need bigger stencils
00102     List<List<point> > stencilPoints(stencil_.stencil().size());
00103     stencil_.collectData
00104     (
00105         mesh.C(),
00106         stencilPoints
00107     );
00108 
00109     // find the fit coefficients for every face in the mesh
00110 
00111     for(label faci = 0; faci < mesh.nInternalFaces(); faci++)
00112     {
00113         snGradPolySize[faci] = calcFit(stencilPoints[faci], faci);
00114     }
00115 
00116     if (debug)
00117     {
00118         snGradPolySize.write();
00119         Info<< "quadraticFitSnGradData::quadraticFitSnGradData() :"
00120             << "Finished constructing polynomialFit data"
00121             << endl;
00122     }
00123 }
00124 
00125 
00126 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00127 
00128 void Foam::quadraticFitSnGradData::findFaceDirs
00129 (
00130     vector& idir,        // value changed in return
00131     vector& jdir,        // value changed in return
00132     vector& kdir,        // value changed in return
00133     const fvMesh& mesh,
00134     const label faci
00135 )
00136 {
00137     idir = mesh.Sf()[faci];
00138     idir /= mag(idir);
00139 
00140     #ifndef SPHERICAL_GEOMETRY
00141         if (mesh.nGeometricD() <= 2) // find the normal direcion
00142         {
00143             if (mesh.geometricD()[0] == -1)
00144             {
00145                 kdir = vector(1, 0, 0);
00146             }
00147             else if (mesh.geometricD()[1] == -1)
00148             {
00149                 kdir = vector(0, 1, 0);
00150             }
00151             else
00152             {
00153                 kdir = vector(0, 0, 1);
00154             }
00155         }
00156         else // 3D so find a direction in the plane of the face
00157         {
00158             const face& f = mesh.faces()[faci];
00159             kdir = mesh.points()[f[0]] - mesh.points()[f[1]];
00160         }
00161     #else
00162         // Spherical geometry so kdir is the radial direction
00163         kdir = mesh.Cf()[faci];
00164     #endif
00165 
00166     if (mesh.nGeometricD() == 3)
00167     {
00168         // Remove the idir component from kdir and normalise
00169         kdir -= (idir & kdir)*idir;
00170 
00171         scalar magk = mag(kdir);
00172 
00173         if (magk < SMALL)
00174         {
00175             FatalErrorIn("findFaceDirs") << " calculated kdir = zero"
00176                 << exit(FatalError);
00177         }
00178         else
00179         {
00180             kdir /= magk;
00181         }
00182     }
00183 
00184     jdir = kdir ^ idir;
00185 }
00186 
00187 
00188 Foam::label Foam::quadraticFitSnGradData::calcFit
00189 (
00190     const List<point>& C,
00191     const label faci
00192 )
00193 {
00194     vector idir(1,0,0);
00195     vector jdir(0,1,0);
00196     vector kdir(0,0,1);
00197     findFaceDirs(idir, jdir, kdir, mesh(), faci);
00198 
00199     scalarList wts(C.size(), scalar(1));
00200     wts[0] = centralWeight_;
00201     wts[1] = centralWeight_;
00202 
00203     point p0 = mesh().faceCentres()[faci];
00204     scalar scale = 0;
00205 
00206     // calculate the matrix of the polynomial components
00207     scalarRectangularMatrix B(C.size(), minSize_, scalar(0));
00208 
00209     for(label ip = 0; ip < C.size(); ip++)
00210     {
00211         const point& p = C[ip];
00212 
00213         scalar px = (p - p0)&idir;
00214         scalar py = (p - p0)&jdir;
00215         #ifdef SPHERICAL_GEOMETRY
00216             scalar pz = mag(p) - mag(p0);
00217         #else
00218             scalar pz = (p - p0)&kdir;
00219         #endif
00220 
00221         if (ip == 0) scale = max(max(mag(px), mag(py)), mag(pz));
00222 
00223         px /= scale;
00224         py /= scale;
00225         pz /= scale;
00226 
00227         label is = 0;
00228 
00229         B[ip][is++] = wts[0]*wts[ip];
00230         B[ip][is++] = wts[0]*wts[ip]*px;
00231         B[ip][is++] = wts[ip]*sqr(px);
00232 
00233         if (dim_ >= 2)
00234         {
00235             B[ip][is++] = wts[ip]*py;
00236             B[ip][is++] = wts[ip]*px*py;
00237             B[ip][is++] = wts[ip]*sqr(py);
00238         }
00239         if (dim_ == 3)
00240         {
00241             B[ip][is++] = wts[ip]*pz;
00242             B[ip][is++] = wts[ip]*px*pz;
00243             //B[ip][is++] = wts[ip]*py*pz;
00244             B[ip][is++] = wts[ip]*sqr(pz);
00245         }
00246     }
00247 
00248     // Set the fit
00249     label stencilSize = C.size();
00250     fit_[faci].setSize(stencilSize);
00251     scalarList singVals(minSize_);
00252     label nSVDzeros = 0;
00253 
00254     const scalar& deltaCoeff = mesh().deltaCoeffs()[faci];
00255 
00256     bool goodFit = false;
00257     for(int iIt = 0; iIt < 10 && !goodFit; iIt++)
00258     {
00259         SVD svd(B, SMALL);
00260 
00261         scalar fit0 = wts[0]*wts[0]*svd.VSinvUt()[1][0]/scale;
00262         scalar fit1 = wts[0]*wts[1]*svd.VSinvUt()[1][1]/scale;
00263 
00264         goodFit =
00265             fit0 < 0 && fit1 > 0
00266          && mag(fit0 + deltaCoeff) < 0.5*deltaCoeff
00267          && mag(fit1 - deltaCoeff) < 0.5*deltaCoeff;
00268 
00269         if (goodFit)
00270         {
00271             fit_[faci][0] = fit0;
00272             fit_[faci][1] = fit1;
00273             for(label i = 2; i < stencilSize; i++)
00274             {
00275                 fit_[faci][i] = wts[0]*wts[i]*svd.VSinvUt()[1][i]/scale;
00276             }
00277             singVals = svd.S();
00278             nSVDzeros = svd.nZeros();
00279         }
00280         else // (not good fit so increase weight in the centre and for linear)
00281         {
00282             wts[0] *= 10;
00283             wts[1] *= 10;
00284 
00285             for(label i = 0; i < B.n(); i++)
00286             {
00287                 B[i][0] *= 10;
00288                 B[i][1] *= 10;
00289             }
00290 
00291             for(label j = 0; j < B.m(); j++)
00292             {
00293                 B[0][j] *= 10;
00294                 B[1][j] *= 10;
00295             }
00296         }
00297     }
00298 
00299     if (goodFit)
00300     {
00301         // remove the uncorrected snGradScheme coefficients
00302         fit_[faci][0] += deltaCoeff;
00303         fit_[faci][1] -= deltaCoeff;
00304     }
00305     else
00306     {
00307         Pout<< "quadratifFitSnGradData could not fit face " << faci
00308             << " fit_[faci][0] =  " << fit_[faci][0]
00309             << " fit_[faci][1] =  " << fit_[faci][1]
00310             << " deltaCoeff =  " << deltaCoeff << endl;
00311         fit_[faci] = 0;
00312     }
00313 
00314     return minSize_ - nSVDzeros;
00315 }
00316 
00317 bool Foam::quadraticFitSnGradData::movePoints()
00318 {
00319     notImplemented("quadraticFitSnGradData::movePoints()");
00320 
00321     return true;
00322 }
00323 
00324 
00325 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd