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

ODEChemistryModel.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) 2009-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 "ODEChemistryModel.H"
00028 #include "chemistrySolver.H"
00029 #include "reactingMixture.H"
00030 
00031 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00032 
00033 template<class CompType, class ThermoType>
00034 Foam::ODEChemistryModel<CompType, ThermoType>::ODEChemistryModel
00035 (
00036     const fvMesh& mesh,
00037     const word& compTypeName,
00038     const word& thermoTypeName
00039 )
00040 :
00041     CompType(mesh, thermoTypeName),
00042 
00043     ODE(),
00044 
00045     Y_(this->thermo().composition().Y()),
00046 
00047     reactions_
00048     (
00049         dynamic_cast<const reactingMixture<ThermoType>&>(this->thermo())
00050     ),
00051     specieThermo_
00052     (
00053         dynamic_cast<const reactingMixture<ThermoType>&>
00054             (this->thermo()).speciesData()
00055     ),
00056 
00057     nSpecie_(Y_.size()),
00058     nReaction_(reactions_.size()),
00059 
00060     solver_
00061     (
00062         chemistrySolver<CompType, ThermoType>::New
00063         (
00064             *this,
00065             compTypeName,
00066             thermoTypeName
00067         )
00068     ),
00069 
00070     RR_(nSpecie_)
00071 {
00072     // create the fields for the chemistry sources
00073     forAll(RR_, fieldI)
00074     {
00075         RR_.set
00076         (
00077             fieldI,
00078             new scalarField(mesh.nCells(), 0.0)
00079         );
00080     }
00081 
00082     Info<< "ODEChemistryModel: Number of species = " << nSpecie_
00083         << " and reactions = " << nReaction_ << endl;
00084 }
00085 
00086 
00087 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00088 
00089 template<class CompType, class ThermoType>
00090 Foam::ODEChemistryModel<CompType, ThermoType>::~ODEChemistryModel()
00091 {}
00092 
00093 
00094 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00095 
00096 template<class CompType, class ThermoType>
00097 Foam::scalarField Foam::ODEChemistryModel<CompType, ThermoType>::omega
00098 (
00099     const scalarField& c,
00100     const scalar T,
00101     const scalar p
00102 ) const
00103 {
00104     scalar pf, cf, pr, cr;
00105     label lRef, rRef;
00106 
00107     scalarField om(nEqns(), 0.0);
00108 
00109     forAll(reactions_, i)
00110     {
00111         const Reaction<ThermoType>& R = reactions_[i];
00112 
00113         scalar omegai = omega
00114         (
00115             R, c, T, p, pf, cf, lRef, pr, cr, rRef
00116         );
00117 
00118         forAll(R.lhs(), s)
00119         {
00120             label si = R.lhs()[s].index;
00121             scalar sl = R.lhs()[s].stoichCoeff;
00122             om[si] -= sl*omegai;
00123         }
00124 
00125         forAll(R.rhs(), s)
00126         {
00127             label si = R.rhs()[s].index;
00128             scalar sr = R.rhs()[s].stoichCoeff;
00129             om[si] += sr*omegai;
00130         }
00131     }
00132 
00133     return om;
00134 }
00135 
00136 
00137 template<class CompType, class ThermoType>
00138 Foam::scalar Foam::ODEChemistryModel<CompType, ThermoType>::omega
00139 (
00140     const Reaction<ThermoType>& R,
00141     const scalarField& c,
00142     const scalar T,
00143     const scalar p,
00144     scalar& pf,
00145     scalar& cf,
00146     label& lRef,
00147     scalar& pr,
00148     scalar& cr,
00149     label& rRef
00150 ) const
00151 {
00152     scalarField c2(nSpecie_, 0.0);
00153     for (label i=0; i<nSpecie_; i++)
00154     {
00155         c2[i] = max(0.0, c[i]);
00156     }
00157 
00158     scalar kf = R.kf(T, p, c2);
00159     scalar kr = R.kr(kf, T, p, c2);
00160 
00161     pf = 1.0;
00162     pr = 1.0;
00163 
00164     label Nl = R.lhs().size();
00165     label Nr = R.rhs().size();
00166 
00167     label slRef = 0;
00168     lRef = R.lhs()[slRef].index;
00169 
00170     pf = kf;
00171     for (label s=1; s<Nl; s++)
00172     {
00173         label si = R.lhs()[s].index;
00174 
00175         if (c[si] < c[lRef])
00176         {
00177             scalar exp = R.lhs()[slRef].exponent;
00178             pf *= pow(max(0.0, c[lRef]), exp);
00179             lRef = si;
00180             slRef = s;
00181         }
00182         else
00183         {
00184             scalar exp = R.lhs()[s].exponent;
00185             pf *= pow(max(0.0, c[si]), exp);
00186         }
00187     }
00188     cf = max(0.0, c[lRef]);
00189 
00190     {
00191         scalar exp = R.lhs()[slRef].exponent;
00192         if (exp<1.0)
00193         {
00194             if (cf > SMALL)
00195             {
00196                 pf *= pow(cf, exp - 1.0);
00197             }
00198             else
00199             {
00200                 pf = 0.0;
00201             }
00202         }
00203         else
00204         {
00205             pf *= pow(cf, exp - 1.0);
00206         }
00207     }
00208 
00209     label srRef = 0;
00210     rRef = R.rhs()[srRef].index;
00211 
00212     // find the matrix element and element position for the rhs
00213     pr = kr;
00214     for (label s=1; s<Nr; s++)
00215     {
00216         label si = R.rhs()[s].index;
00217         if (c[si] < c[rRef])
00218         {
00219             scalar exp = R.rhs()[srRef].exponent;
00220             pr *= pow(max(0.0, c[rRef]), exp);
00221             rRef = si;
00222             srRef = s;
00223         }
00224         else
00225         {
00226             scalar exp = R.rhs()[s].exponent;
00227             pr *= pow(max(0.0, c[si]), exp);
00228         }
00229     }
00230     cr = max(0.0, c[rRef]);
00231 
00232     {
00233         scalar exp = R.rhs()[srRef].exponent;
00234         if (exp<1.0)
00235         {
00236             if (cr>SMALL)
00237             {
00238                 pr *= pow(cr, exp - 1.0);
00239             }
00240             else
00241             {
00242                 pr = 0.0;
00243             }
00244         }
00245         else
00246         {
00247             pr *= pow(cr, exp - 1.0);
00248         }
00249     }
00250 
00251     return pf*cf - pr*cr;
00252 }
00253 
00254 
00255 template<class CompType, class ThermoType>
00256 void Foam::ODEChemistryModel<CompType, ThermoType>::derivatives
00257 (
00258     const scalar time,
00259     const scalarField &c,
00260     scalarField& dcdt
00261 ) const
00262 {
00263     scalar T = c[nSpecie_];
00264     scalar p = c[nSpecie_ + 1];
00265 
00266     dcdt = omega(c, T, p);
00267 
00268     // constant pressure
00269     // dT/dt = ...
00270     scalar rho = 0.0;
00271     scalar cSum = 0.0;
00272     for (label i=0; i<nSpecie_; i++)
00273     {
00274         scalar W = specieThermo_[i].W();
00275         cSum += c[i];
00276         rho += W*c[i];
00277     }
00278     scalar mw = rho/cSum;
00279     scalar cp = 0.0;
00280     for (label i=0; i<nSpecie_; i++)
00281     {
00282         scalar cpi = specieThermo_[i].cp(T);
00283         scalar Xi = c[i]/rho;
00284         cp += Xi*cpi;
00285     }
00286     cp /= mw;
00287 
00288     scalar dT = 0.0;
00289     for (label i=0; i<nSpecie_; i++)
00290     {
00291         scalar hi = specieThermo_[i].h(T);
00292         dT += hi*dcdt[i];
00293     }
00294     dT /= rho*cp;
00295 
00296     // limit the time-derivative, this is more stable for the ODE
00297     // solver when calculating the allowed time step
00298     scalar dtMag = min(500.0, mag(dT));
00299     dcdt[nSpecie_] = -dT*dtMag/(mag(dT) + 1.0e-10);
00300 
00301     // dp/dt = ...
00302     dcdt[nSpecie_+1] = 0.0;
00303 }
00304 
00305 
00306 template<class CompType, class ThermoType>
00307 void Foam::ODEChemistryModel<CompType, ThermoType>::jacobian
00308 (
00309     const scalar t,
00310     const scalarField& c,
00311     scalarField& dcdt,
00312     scalarSquareMatrix& dfdc
00313 ) const
00314 {
00315     scalar T = c[nSpecie_];
00316     scalar p = c[nSpecie_ + 1];
00317 
00318     scalarField c2(nSpecie_, 0.0);
00319     for (label i=0; i<nSpecie_; i++)
00320     {
00321         c2[i] = max(c[i], 0.0);
00322     }
00323 
00324     for (label i=0; i<nEqns(); i++)
00325     {
00326         for (label j=0; j<nEqns(); j++)
00327         {
00328             dfdc[i][j] = 0.0;
00329         }
00330     }
00331 
00332     // length of the first argument must be nSpecie()
00333     dcdt = omega(c2, T, p);
00334 
00335     for (label ri=0; ri<reactions_.size(); ri++)
00336     {
00337         const Reaction<ThermoType>& R = reactions_[ri];
00338 
00339         scalar kf0 = R.kf(T, p, c2);
00340         scalar kr0 = R.kr(T, p, c2);
00341 
00342         forAll(R.lhs(), j)
00343         {
00344             label sj = R.lhs()[j].index;
00345             scalar kf = kf0;
00346             forAll(R.lhs(), i)
00347             {
00348                 label si = R.lhs()[i].index;
00349                 scalar el = R.lhs()[i].exponent;
00350                 if (i == j)
00351                 {
00352                     if (el < 1.0)
00353                     {
00354                         if (c2[si]>SMALL)
00355                         {
00356                             kf *= el*pow(c2[si] + VSMALL, el - 1.0);
00357                         }
00358                         else
00359                         {
00360                             kf = 0.0;
00361                         }
00362                     }
00363                     else
00364                     {
00365                         kf *= el*pow(c2[si], el - 1.0);
00366                     }
00367                 }
00368                 else
00369                 {
00370                     kf *= pow(c2[si], el);
00371                 }
00372             }
00373 
00374             forAll(R.lhs(), i)
00375             {
00376                 label si = R.lhs()[i].index;
00377                 scalar sl = R.lhs()[i].stoichCoeff;
00378                 dfdc[si][sj] -= sl*kf;
00379             }
00380             forAll(R.rhs(), i)
00381             {
00382                 label si = R.rhs()[i].index;
00383                 scalar sr = R.rhs()[i].stoichCoeff;
00384                 dfdc[si][sj] += sr*kf;
00385             }
00386         }
00387 
00388         forAll(R.rhs(), j)
00389         {
00390             label sj = R.rhs()[j].index;
00391             scalar kr = kr0;
00392             forAll(R.rhs(), i)
00393             {
00394                 label si = R.rhs()[i].index;
00395                 scalar er = R.rhs()[i].exponent;
00396                 if (i==j)
00397                 {
00398                     if (er<1.0)
00399                     {
00400                         if (c2[si]>SMALL)
00401                         {
00402                             kr *= er*pow(c2[si] + VSMALL, er - 1.0);
00403                         }
00404                         else
00405                         {
00406                             kr = 0.0;
00407                         }
00408                     }
00409                     else
00410                     {
00411                         kr *= er*pow(c2[si], er - 1.0);
00412                     }
00413                 }
00414                 else
00415                 {
00416                     kr *= pow(c2[si], er);
00417                 }
00418             }
00419 
00420             forAll(R.lhs(), i)
00421             {
00422                 label si = R.lhs()[i].index;
00423                 scalar sl = R.lhs()[i].stoichCoeff;
00424                 dfdc[si][sj] += sl*kr;
00425             }
00426             forAll(R.rhs(), i)
00427             {
00428                 label si = R.rhs()[i].index;
00429                 scalar sr = R.rhs()[i].stoichCoeff;
00430                 dfdc[si][sj] -= sr*kr;
00431             }
00432         }
00433     }
00434 
00435     // calculate the dcdT elements numerically
00436     scalar delta = 1.0e-8;
00437     scalarField dcdT0 = omega(c2, T - delta, p);
00438     scalarField dcdT1 = omega(c2, T + delta, p);
00439 
00440     for (label i=0; i<nEqns(); i++)
00441     {
00442         dfdc[i][nSpecie()] = 0.5*(dcdT1[i] - dcdT0[i])/delta;
00443     }
00444 
00445 }
00446 
00447 
00448 template<class CompType, class ThermoType>
00449 Foam::tmp<Foam::volScalarField>
00450 Foam::ODEChemistryModel<CompType, ThermoType>::tc() const
00451 {
00452     scalar pf,cf,pr,cr;
00453     label lRef, rRef;
00454 
00455     const volScalarField rho
00456     (
00457         IOobject
00458         (
00459             "rho",
00460             this->time().timeName(),
00461             this->mesh(),
00462             IOobject::NO_READ,
00463             IOobject::NO_WRITE,
00464             false
00465         ),
00466         this->thermo().rho()
00467     );
00468 
00469     tmp<volScalarField> tsource
00470     (
00471         new volScalarField
00472         (
00473             IOobject
00474             (
00475                 "tc",
00476                 this->time().timeName(),
00477                 this->mesh(),
00478                 IOobject::NO_READ,
00479                 IOobject::NO_WRITE
00480             ),
00481             this->mesh(),
00482             dimensionedScalar("zero", dimTime, SMALL),
00483             zeroGradientFvPatchScalarField::typeName
00484         )
00485     );
00486 
00487     scalarField& t = tsource();
00488 
00489     label nReaction = reactions_.size();
00490 
00491     if (this->chemistry_)
00492     {
00493         forAll(rho, celli)
00494         {
00495             scalar rhoi = rho[celli];
00496             scalar Ti = this->thermo().T()[celli];
00497             scalar pi = this->thermo().p()[celli];
00498             scalarField c(nSpecie_);
00499             scalar cSum = 0.0;
00500 
00501             for (label i=0; i<nSpecie_; i++)
00502             {
00503                 scalar Yi = Y_[i][celli];
00504                 c[i] = rhoi*Yi/specieThermo_[i].W();
00505                 cSum += c[i];
00506             }
00507 
00508             forAll(reactions_, i)
00509             {
00510                 const Reaction<ThermoType>& R = reactions_[i];
00511 
00512                 omega
00513                 (
00514                     R, c, Ti, pi, pf, cf, lRef, pr, cr, rRef
00515                 );
00516 
00517                 forAll(R.rhs(), s)
00518                 {
00519                     scalar sr = R.rhs()[s].stoichCoeff;
00520                     t[celli] += sr*pf*cf;
00521                 }
00522             }
00523             t[celli] = nReaction*cSum/t[celli];
00524         }
00525     }
00526 
00527 
00528     tsource().correctBoundaryConditions();
00529 
00530     return tsource;
00531 }
00532 
00533 
00534 template<class CompType, class ThermoType>
00535 Foam::tmp<Foam::volScalarField>
00536 Foam::ODEChemistryModel<CompType, ThermoType>::dQ() const
00537 {
00538     tmp<volScalarField> tdQ
00539     (
00540         new volScalarField
00541         (
00542             IOobject
00543             (
00544                 "dQ",
00545                 this->mesh_.time().timeName(),
00546                 this->mesh_,
00547                 IOobject::NO_READ,
00548                 IOobject::NO_WRITE
00549             ),
00550             this->mesh_,
00551             dimensionedScalar
00552             (
00553                 "zero",
00554                 dimensionSet(1, -3, -1 , 0, 0, 0, 0),
00555                 0.0
00556             )
00557         )
00558     );
00559 
00560     if (this->chemistry_)
00561     {
00562         scalarField& dQ = tdQ();
00563 
00564         scalarField cp(dQ.size(), 0.0);
00565 
00566         forAll(Y_, i)
00567         {
00568             forAll(dQ, cellI)
00569             {
00570                 scalar Ti = this->thermo().T()[cellI];
00571                 cp[cellI] += Y_[i][cellI]*specieThermo_[i].Cp(Ti);
00572                 scalar hi = specieThermo_[i].h(Ti);
00573                 dQ[cellI] -= hi*RR_[i][cellI];
00574             }
00575         }
00576 
00577         dQ /= cp;
00578     }
00579 
00580     return tdQ;
00581 }
00582 
00583 
00584 template<class CompType, class ThermoType>
00585 Foam::label Foam::ODEChemistryModel<CompType, ThermoType>::nEqns() const
00586 {
00587     // nEqns = number of species + temperature + pressure
00588     return nSpecie_ + 2;
00589 }
00590 
00591 
00592 template<class CompType, class ThermoType>
00593 void Foam::ODEChemistryModel<CompType, ThermoType>::calculate()
00594 {
00595     const volScalarField rho
00596     (
00597         IOobject
00598         (
00599             "rho",
00600             this->time().timeName(),
00601             this->mesh(),
00602             IOobject::NO_READ,
00603             IOobject::NO_WRITE,
00604             false
00605         ),
00606         this->thermo().rho()
00607     );
00608 
00609     for (label i=0; i<nSpecie_; i++)
00610     {
00611         RR_[i].setSize(rho.size());
00612     }
00613 
00614     if (this->chemistry_)
00615     {
00616         forAll(rho, celli)
00617         {
00618             for (label i=0; i<nSpecie_; i++)
00619             {
00620                 RR_[i][celli] = 0.0;
00621             }
00622 
00623             scalar rhoi = rho[celli];
00624             scalar Ti = this->thermo().T()[celli];
00625             scalar pi = this->thermo().p()[celli];
00626 
00627             scalarField c(nSpecie_);
00628             scalarField dcdt(nEqns(), 0.0);
00629 
00630             for (label i=0; i<nSpecie_; i++)
00631             {
00632                 scalar Yi = Y_[i][celli];
00633                 c[i] = rhoi*Yi/specieThermo_[i].W();
00634             }
00635 
00636             dcdt = omega(c, Ti, pi);
00637 
00638             for (label i=0; i<nSpecie_; i++)
00639             {
00640                 RR_[i][celli] = dcdt[i]*specieThermo_[i].W();
00641             }
00642         }
00643     }
00644 }
00645 
00646 
00647 template<class CompType, class ThermoType>
00648 Foam::scalar Foam::ODEChemistryModel<CompType, ThermoType>::solve
00649 (
00650     const scalar t0,
00651     const scalar deltaT
00652 )
00653 {
00654     const volScalarField rho
00655     (
00656         IOobject
00657         (
00658             "rho",
00659             this->time().timeName(),
00660             this->mesh(),
00661             IOobject::NO_READ,
00662             IOobject::NO_WRITE,
00663             false
00664         ),
00665         this->thermo().rho()
00666     );
00667 
00668     for (label i=0; i<nSpecie_; i++)
00669     {
00670         RR_[i].setSize(rho.size());
00671     }
00672 
00673     if (!this->chemistry_)
00674     {
00675         return GREAT;
00676     }
00677 
00678     scalar deltaTMin = GREAT;
00679 
00680     forAll(rho, celli)
00681     {
00682         for (label i=0; i<nSpecie_; i++)
00683         {
00684             RR_[i][celli] = 0.0;
00685         }
00686 
00687         scalar rhoi = rho[celli];
00688         scalar Ti = this->thermo().T()[celli];
00689         scalar hi = this->thermo().h()[celli];
00690         scalar pi = this->thermo().p()[celli];
00691 
00692         scalarField c(nSpecie_);
00693         scalarField c0(nSpecie_);
00694         scalarField dc(nSpecie_, 0.0);
00695 
00696         for (label i=0; i<nSpecie_; i++)
00697         {
00698             c[i] = rhoi*Y_[i][celli]/specieThermo_[i].W();
00699         }
00700         c0 = c;
00701 
00702         scalar t = t0;
00703         scalar tauC = this->deltaTChem_[celli];
00704         scalar dt = min(deltaT, tauC);
00705         scalar timeLeft = deltaT;
00706 
00707         // calculate the chemical source terms
00708         scalar cTot = 0.0;
00709 
00710         while(timeLeft > SMALL)
00711         {
00712             tauC = solver().solve(c, Ti, pi, t, dt);
00713             t += dt;
00714 
00715             // update the temperature
00716             cTot = sum(c);
00717             ThermoType mixture(0.0*specieThermo_[0]);
00718             for (label i=0; i<nSpecie_; i++)
00719             {
00720                 mixture += (c[i]/cTot)*specieThermo_[i];
00721             }
00722             Ti = mixture.TH(hi, Ti);
00723 
00724             timeLeft -= dt;
00725             this->deltaTChem_[celli] = tauC;
00726             dt = min(timeLeft, tauC);
00727             dt = max(dt, SMALL);
00728         }
00729         deltaTMin = min(tauC, deltaTMin);
00730 
00731         dc = c - c0;
00732         scalar WTot = 0.0;
00733         for (label i=0; i<nSpecie_; i++)
00734         {
00735             WTot += c[i]*specieThermo_[i].W();
00736         }
00737         WTot /= cTot;
00738 
00739         for (label i=0; i<nSpecie_; i++)
00740         {
00741             RR_[i][celli] = dc[i]*specieThermo_[i].W()/deltaT;
00742         }
00743     }
00744 
00745     // Don't allow the time-step to change more than a factor of 2
00746     deltaTMin = min(deltaTMin, 2*deltaT);
00747 
00748     return deltaTMin;
00749 }
00750 
00751 
00752 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd