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

MULESTemplates.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 "MULES.H"
00028 #include "upwind.H"
00029 #include "uncorrectedSnGrad.H"
00030 #include "gaussConvectionScheme.H"
00031 #include "gaussLaplacianScheme.H"
00032 #include "uncorrectedSnGrad.H"
00033 #include "surfaceInterpolate.H"
00034 #include "fvcSurfaceIntegrate.H"
00035 #include "slicedSurfaceFields.H"
00036 #include "syncTools.H"
00037 
00038 #include "fvCFD.H"
00039 
00040 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00041 
00042 template<class RhoType, class SpType, class SuType>
00043 void Foam::MULES::explicitSolve
00044 (
00045     const RhoType& rho,
00046     volScalarField& psi,
00047     const surfaceScalarField& phi,
00048     surfaceScalarField& phiPsi,
00049     const SpType& Sp,
00050     const SuType& Su,
00051     const scalar psiMax,
00052     const scalar psiMin
00053 )
00054 {
00055     Info<< "MULES: Solving for " << psi.name() << endl;
00056 
00057     const fvMesh& mesh = psi.mesh();
00058     psi.correctBoundaryConditions();
00059 
00060     surfaceScalarField phiBD = upwind<scalar>(psi.mesh(), phi).flux(psi);
00061 
00062     surfaceScalarField& phiCorr = phiPsi;
00063     phiCorr -= phiBD;
00064 
00065     scalarField allLambda(mesh.nFaces(), 1.0);
00066 
00067     slicedSurfaceScalarField lambda
00068     (
00069         IOobject
00070         (
00071             "lambda",
00072             mesh.time().timeName(),
00073             mesh,
00074             IOobject::NO_READ,
00075             IOobject::NO_WRITE,
00076             false
00077         ),
00078         mesh,
00079         dimless,
00080         allLambda,
00081         false   // Use slices for the couples
00082     );
00083 
00084     limiter
00085     (
00086         allLambda,
00087         rho,
00088         psi,
00089         phiBD,
00090         phiCorr,
00091         Sp.field(),
00092         Su.field(),
00093         psiMax,
00094         psiMin,
00095         3
00096     );
00097 
00098     phiPsi = phiBD + lambda*phiCorr;
00099 
00100     scalarField& psiIf = psi;
00101     const scalarField& psi0 = psi.oldTime();
00102     const scalar deltaT = mesh.time().deltaT().value();
00103 
00104     psiIf = 0.0;
00105     fvc::surfaceIntegrate(psiIf, phiPsi);
00106 
00107     if (mesh.moving())
00108     {
00109         psiIf =
00110         (
00111             mesh.V0()*rho.oldTime()*psi0/(deltaT*mesh.V())
00112           + Su.field()
00113           - psiIf
00114         )/(rho/deltaT - Sp.field());
00115     }
00116     else
00117     {
00118         psiIf =
00119         (
00120             rho.oldTime()*psi0/deltaT
00121           + Su.field()
00122           - psiIf
00123         )/(rho/deltaT - Sp.field());
00124     }
00125 
00126     psi.correctBoundaryConditions();
00127 }
00128 
00129 
00130 template<class RhoType, class SpType, class SuType>
00131 void Foam::MULES::implicitSolve
00132 (
00133     const RhoType& rho,
00134     volScalarField& psi,
00135     const surfaceScalarField& phi,
00136     surfaceScalarField& phiPsi,
00137     const SpType& Sp,
00138     const SuType& Su,
00139     const scalar psiMax,
00140     const scalar psiMin
00141 )
00142 {
00143     const fvMesh& mesh = psi.mesh();
00144 
00145     const dictionary& MULEScontrols = 
00146        mesh.solverDict(psi.name()).subDict("MULESImplicit");
00147 
00148     label maxIter
00149     (
00150         readLabel(MULEScontrols.lookup("maxIter"))
00151     );
00152 
00153     label nLimiterIter
00154     (
00155         readLabel(MULEScontrols.lookup("nLimiterIter"))
00156     );
00157 
00158     scalar maxUnboundedness
00159     (
00160         readScalar(MULEScontrols.lookup("maxUnboundedness"))
00161     );
00162 
00163     scalar CoCoeff
00164     (
00165         readScalar(MULEScontrols.lookup("CoCoeff"))
00166     );
00167 
00168     scalarField allCoLambda(mesh.nFaces());
00169 
00170     {
00171         surfaceScalarField Cof =
00172             mesh.time().deltaT()*mesh.surfaceInterpolation::deltaCoeffs()
00173            *mag(phi)/mesh.magSf();
00174 
00175         slicedSurfaceScalarField CoLambda
00176         (
00177             IOobject
00178             (
00179                 "CoLambda",
00180                 mesh.time().timeName(),
00181                 mesh,
00182                 IOobject::NO_READ,
00183                 IOobject::NO_WRITE,
00184                 false
00185             ),
00186             mesh,
00187             dimless,
00188             allCoLambda,
00189             false   // Use slices for the couples
00190         );
00191 
00192         CoLambda == 1.0/max(CoCoeff*Cof, scalar(1));
00193     }
00194 
00195     scalarField allLambda(allCoLambda);
00196     //scalarField allLambda(mesh.nFaces(), 1.0);
00197 
00198     slicedSurfaceScalarField lambda
00199     (
00200         IOobject
00201         (
00202             "lambda",
00203             mesh.time().timeName(),
00204             mesh,
00205             IOobject::NO_READ,
00206             IOobject::NO_WRITE,
00207             false
00208         ),
00209         mesh,
00210         dimless,
00211         allLambda,
00212         false   // Use slices for the couples
00213     );
00214 
00215     linear<scalar> CDs(mesh);
00216     upwind<scalar> UDs(mesh, phi);
00217     //fv::uncorrectedSnGrad<scalar> snGrads(mesh);
00218 
00219     fvScalarMatrix psiConvectionDiffusion
00220     (
00221         fvm::ddt(rho, psi)
00222       + fv::gaussConvectionScheme<scalar>(mesh, phi, UDs).fvmDiv(phi, psi)
00223         //- fv::gaussLaplacianScheme<scalar, scalar>(mesh, CDs, snGrads)
00224         //.fvmLaplacian(Dpsif, psi)
00225       - fvm::Sp(Sp, psi)
00226       - Su
00227     );
00228 
00229     surfaceScalarField phiBD = psiConvectionDiffusion.flux();
00230 
00231     surfaceScalarField& phiCorr = phiPsi;
00232     phiCorr -= phiBD;
00233 
00234     for (label i=0; i<maxIter; i++)
00235     {
00236         if (i != 0 && i < 4)
00237         {
00238             allLambda = allCoLambda;
00239         }
00240 
00241         limiter
00242         (
00243             allLambda,
00244             rho,
00245             psi,
00246             phiBD,
00247             phiCorr,
00248             Sp.field(),
00249             Su.field(),
00250             psiMax,
00251             psiMin,
00252             nLimiterIter
00253         );
00254 
00255         solve
00256         (
00257             psiConvectionDiffusion + fvc::div(lambda*phiCorr),
00258             MULEScontrols.lookup("solver")
00259         );
00260 
00261         scalar maxPsiM1 = gMax(psi.internalField()) - 1.0;
00262         scalar minPsi = gMin(psi.internalField());
00263 
00264         scalar unboundedness = max(max(maxPsiM1, 0.0), -min(minPsi, 0.0));
00265 
00266         if (unboundedness < maxUnboundedness)
00267         {
00268             break;
00269         }
00270         else
00271         {
00272             Info<< "MULES: max(" << psi.name() << " - 1) = " << maxPsiM1
00273                 << " min(" << psi.name() << ") = " << minPsi << endl;
00274 
00275             phiBD = psiConvectionDiffusion.flux();
00276 
00277             /*
00278             word gammaScheme("div(phi,gamma)");
00279             word gammarScheme("div(phirb,gamma)");
00280 
00281             const surfaceScalarField& phir =
00282                 mesh.lookupObject<surfaceScalarField>("phir");
00283 
00284             phiCorr =
00285                 fvc::flux
00286                 (
00287                     phi,
00288                     psi,
00289                     gammaScheme
00290                 )
00291               + fvc::flux
00292                 (
00293                     -fvc::flux(-phir, scalar(1) - psi, gammarScheme),
00294                     psi,
00295                     gammarScheme
00296                 )
00297                 - phiBD;
00298             */
00299         }
00300     }
00301 
00302     phiPsi = psiConvectionDiffusion.flux() + lambda*phiCorr;
00303 }
00304 
00305 
00306 template<class RhoType, class SpType, class SuType>
00307 void Foam::MULES::limiter
00308 (
00309     scalarField& allLambda,
00310     const RhoType& rho,
00311     const volScalarField& psi,
00312     const surfaceScalarField& phiBD,
00313     const surfaceScalarField& phiCorr,
00314     const SpType& Sp,
00315     const SuType& Su,
00316     const scalar psiMax,
00317     const scalar psiMin,
00318     const label nLimiterIter
00319 )
00320 {
00321     const scalarField& psiIf = psi;
00322     const volScalarField::GeometricBoundaryField& psiBf = psi.boundaryField();
00323 
00324     const scalarField& psi0 = psi.oldTime();
00325 
00326     const fvMesh& mesh = psi.mesh();
00327 
00328     const unallocLabelList& owner = mesh.owner();
00329     const unallocLabelList& neighb = mesh.neighbour();
00330     const scalarField& V = mesh.V();
00331     const scalar deltaT = mesh.time().deltaT().value();
00332 
00333     const scalarField& phiBDIf = phiBD;
00334     const surfaceScalarField::GeometricBoundaryField& phiBDBf =
00335         phiBD.boundaryField();
00336 
00337     const scalarField& phiCorrIf = phiCorr;
00338     const surfaceScalarField::GeometricBoundaryField& phiCorrBf =
00339         phiCorr.boundaryField();
00340 
00341     slicedSurfaceScalarField lambda
00342     (
00343         IOobject
00344         (
00345             "lambda",
00346             mesh.time().timeName(),
00347             mesh,
00348             IOobject::NO_READ,
00349             IOobject::NO_WRITE,
00350             false
00351         ),
00352         mesh,
00353         dimless,
00354         allLambda,
00355         false   // Use slices for the couples
00356     );
00357 
00358     scalarField& lambdaIf = lambda;
00359     surfaceScalarField::GeometricBoundaryField& lambdaBf =
00360         lambda.boundaryField();
00361 
00362     scalarField psiMaxn(psiIf.size(), psiMin);
00363     scalarField psiMinn(psiIf.size(), psiMax);
00364 
00365     scalarField sumPhiBD(psiIf.size(), 0.0);
00366 
00367     scalarField sumPhip(psiIf.size(), VSMALL);
00368     scalarField mSumPhim(psiIf.size(), VSMALL);
00369 
00370     forAll(phiCorrIf, facei)
00371     {
00372         label own = owner[facei];
00373         label nei = neighb[facei];
00374 
00375         psiMaxn[own] = max(psiMaxn[own], psiIf[nei]);
00376         psiMinn[own] = min(psiMinn[own], psiIf[nei]);
00377 
00378         psiMaxn[nei] = max(psiMaxn[nei], psiIf[own]);
00379         psiMinn[nei] = min(psiMinn[nei], psiIf[own]);
00380 
00381         sumPhiBD[own] += phiBDIf[facei];
00382         sumPhiBD[nei] -= phiBDIf[facei];
00383 
00384         scalar phiCorrf = phiCorrIf[facei];
00385 
00386         if (phiCorrf > 0.0)
00387         {
00388             sumPhip[own] += phiCorrf;
00389             mSumPhim[nei] += phiCorrf;
00390         }
00391         else
00392         {
00393             mSumPhim[own] -= phiCorrf;
00394             sumPhip[nei] -= phiCorrf;
00395         }
00396     }
00397 
00398     forAll(phiCorrBf, patchi)
00399     {
00400         const fvPatchScalarField& psiPf = psiBf[patchi];
00401         const scalarField& phiBDPf = phiBDBf[patchi];
00402         const scalarField& phiCorrPf = phiCorrBf[patchi];
00403 
00404         const labelList& pFaceCells = mesh.boundary()[patchi].faceCells();
00405 
00406         if (psiPf.coupled())
00407         {
00408             scalarField psiPNf = psiPf.patchNeighbourField();
00409 
00410             forAll(phiCorrPf, pFacei)
00411             {
00412                 label pfCelli = pFaceCells[pFacei];
00413 
00414                 psiMaxn[pfCelli] = max(psiMaxn[pfCelli], psiPNf[pFacei]);
00415                 psiMinn[pfCelli] = min(psiMinn[pfCelli], psiPNf[pFacei]);
00416             }
00417         }
00418         else
00419         {
00420             forAll(phiCorrPf, pFacei)
00421             {
00422                 label pfCelli = pFaceCells[pFacei];
00423 
00424                 psiMaxn[pfCelli] = max(psiMaxn[pfCelli], psiPf[pFacei]);
00425                 psiMinn[pfCelli] = min(psiMinn[pfCelli], psiPf[pFacei]);
00426             }
00427         }
00428 
00429         forAll(phiCorrPf, pFacei)
00430         {
00431             label pfCelli = pFaceCells[pFacei];
00432 
00433             sumPhiBD[pfCelli] += phiBDPf[pFacei];
00434 
00435             scalar phiCorrf = phiCorrPf[pFacei];
00436 
00437             if (phiCorrf > 0.0)
00438             {
00439                 sumPhip[pfCelli] += phiCorrf;
00440             }
00441             else
00442             {
00443                 mSumPhim[pfCelli] -= phiCorrf;
00444             }
00445         }
00446     }
00447 
00448     psiMaxn = min(psiMaxn, psiMax);
00449     psiMinn = max(psiMinn, psiMin);
00450 
00451     //scalar smooth = 0.5;
00452     //psiMaxn = min((1.0 - smooth)*psiIf + smooth*psiMaxn, psiMax);
00453     //psiMinn = max((1.0 - smooth)*psiIf + smooth*psiMinn, psiMin);
00454 
00455     if (mesh.moving())
00456     {
00457         psiMaxn =
00458             V*((rho/deltaT - Sp)*psiMaxn - Su)
00459           - (mesh.V0()/deltaT)*rho.oldTime()*psi0
00460           + sumPhiBD;
00461 
00462         psiMinn =
00463             V*(Su - (rho/deltaT - Sp)*psiMinn)
00464           + (mesh.V0()/deltaT)*rho.oldTime()*psi0
00465           - sumPhiBD;
00466     }
00467     else
00468     {
00469         psiMaxn =
00470             V*((rho/deltaT - Sp)*psiMaxn - (rho.oldTime()/deltaT)*psi0 - Su)
00471           + sumPhiBD;
00472 
00473         psiMinn =
00474             V*((rho/deltaT)*psi0 - (rho.oldTime()/deltaT - Sp)*psiMinn + Su)
00475           - sumPhiBD;
00476     }
00477 
00478     scalarField sumlPhip(psiIf.size());
00479     scalarField mSumlPhim(psiIf.size());
00480 
00481     for(int j=0; j<nLimiterIter; j++)
00482     {
00483         sumlPhip = 0.0;
00484         mSumlPhim = 0.0;
00485 
00486         forAll(lambdaIf, facei)
00487         {
00488             label own = owner[facei];
00489             label nei = neighb[facei];
00490 
00491             scalar lambdaPhiCorrf = lambdaIf[facei]*phiCorrIf[facei];
00492 
00493             if (lambdaPhiCorrf > 0.0)
00494             {
00495                 sumlPhip[own] += lambdaPhiCorrf;
00496                 mSumlPhim[nei] += lambdaPhiCorrf;
00497             }
00498             else
00499             {
00500                 mSumlPhim[own] -= lambdaPhiCorrf;
00501                 sumlPhip[nei] -= lambdaPhiCorrf;
00502             }
00503         }
00504 
00505         forAll(lambdaBf, patchi)
00506         {
00507             scalarField& lambdaPf = lambdaBf[patchi];
00508             const scalarField& phiCorrfPf = phiCorrBf[patchi];
00509 
00510             const labelList& pFaceCells = mesh.boundary()[patchi].faceCells();
00511 
00512             forAll(lambdaPf, pFacei)
00513             {
00514                 label pfCelli = pFaceCells[pFacei];
00515 
00516                 scalar lambdaPhiCorrf = lambdaPf[pFacei]*phiCorrfPf[pFacei];
00517 
00518                 if (lambdaPhiCorrf > 0.0)
00519                 {
00520                     sumlPhip[pfCelli] += lambdaPhiCorrf;
00521                 }
00522                 else
00523                 {
00524                     mSumlPhim[pfCelli] -= lambdaPhiCorrf;
00525                 }
00526             }
00527         }
00528 
00529         forAll (sumlPhip, celli)
00530         {
00531             sumlPhip[celli] =
00532                 max(min
00533                 (
00534                     (sumlPhip[celli] + psiMaxn[celli])/mSumPhim[celli],
00535                     1.0), 0.0
00536                 );
00537 
00538             mSumlPhim[celli] =
00539                 max(min
00540                 (
00541                     (mSumlPhim[celli] + psiMinn[celli])/sumPhip[celli],
00542                     1.0), 0.0
00543                 );
00544         }
00545 
00546         const scalarField& lambdam = sumlPhip;
00547         const scalarField& lambdap = mSumlPhim;
00548 
00549         forAll(lambdaIf, facei)
00550         {
00551             if (phiCorrIf[facei] > 0.0)
00552             {
00553                 lambdaIf[facei] = min
00554                 (
00555                     lambdaIf[facei],
00556                     min(lambdap[owner[facei]], lambdam[neighb[facei]])
00557                 );
00558             }
00559             else
00560             {
00561                 lambdaIf[facei] = min
00562                 (
00563                     lambdaIf[facei],
00564                     min(lambdam[owner[facei]], lambdap[neighb[facei]])
00565                 );
00566             }
00567         }
00568 
00569 
00570         forAll(lambdaBf, patchi)
00571         {
00572             fvsPatchScalarField& lambdaPf = lambdaBf[patchi];
00573             const scalarField& phiCorrfPf = phiCorrBf[patchi];
00574 
00575             const labelList& pFaceCells = mesh.boundary()[patchi].faceCells();
00576 
00577             forAll(lambdaPf, pFacei)
00578             {
00579                 label pfCelli = pFaceCells[pFacei];
00580 
00581                 if (phiCorrfPf[pFacei] > 0.0)
00582                 {
00583                     lambdaPf[pFacei] = min(lambdaPf[pFacei], lambdap[pfCelli]);
00584                 }
00585                 else
00586                 {
00587                     lambdaPf[pFacei] = min(lambdaPf[pFacei], lambdam[pfCelli]);
00588                 }
00589             }
00590         }
00591 
00592         syncTools::syncFaceList(mesh, allLambda, minEqOp<scalar>(), false);
00593     }
00594 }
00595 
00596 
00597 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd