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

Reaction.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 Description
00026     Simple extension of ReactionThermo to handle Reaction kinetics in addition
00027     to the equilibrium thermodynamics already handled.
00028 
00029 \*---------------------------------------------------------------------------*/
00030 
00031 #include "Reaction.H"
00032 #include "DynamicList.H"
00033 
00034 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00035 
00036 namespace Foam
00037 {
00038 
00039 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00040 
00041 template<class ReactionThermo>
00042 void Reaction<ReactionThermo>::setThermo
00043 (
00044     const HashPtrTable<ReactionThermo>& thermoDatabase
00045 )
00046 {
00047     ReactionThermo::operator=
00048     (
00049         rhs_[0].stoichCoeff*(*thermoDatabase[species_[rhs_[0].index]])
00050     );
00051 
00052     for (label i=1; i<rhs_.size(); i++)
00053     {
00054         operator+=
00055         (
00056             rhs_[i].stoichCoeff*(*thermoDatabase[species_[rhs_[i].index]])
00057         );
00058     }
00059 
00060     for (label i=0; i<lhs_.size(); i++)
00061     {
00062         operator-=
00063         (
00064             lhs_[i].stoichCoeff*(*thermoDatabase[species_[lhs_[i].index]])
00065         );
00066     }
00067 }
00068 
00069 
00070 // Construct from components
00071 template<class ReactionThermo>
00072 Reaction<ReactionThermo>::Reaction
00073 (
00074     const speciesTable& species,
00075     const List<specieCoeffs>& lhs,
00076     const List<specieCoeffs>& rhs,
00077     const HashPtrTable<ReactionThermo>& thermoDatabase
00078 )
00079 :
00080     ReactionThermo(*thermoDatabase[species[0]]),
00081     species_(species),
00082     lhs_(lhs),
00083     rhs_(rhs)
00084 {
00085     setThermo(thermoDatabase);
00086 }
00087 
00088 
00089 // Construct as copy given new speciesTable
00090 template<class ReactionThermo>
00091 Reaction<ReactionThermo>::Reaction
00092 (
00093     const Reaction<ReactionThermo>& r,
00094     const speciesTable& species
00095 )
00096 :
00097     ReactionThermo(r),
00098     species_(species),
00099     lhs_(r.lhs_),
00100     rhs_(r.rhs_)
00101 {}
00102 
00103 
00104 template<class ReactionThermo>
00105 Reaction<ReactionThermo>::specieCoeffs::specieCoeffs
00106 (
00107     const speciesTable& species,
00108     Istream& is
00109 )
00110 {
00111     token t(is);
00112 
00113     if (t.isNumber())
00114     {
00115         stoichCoeff = t.number();
00116         is >> t;
00117     }
00118     else
00119     {
00120         stoichCoeff = 1.0;
00121     }
00122 
00123     exponent = stoichCoeff;
00124 
00125     if (t.isWord())
00126     {
00127         word specieName = t.wordToken();
00128 
00129         size_t i = specieName.find('^');
00130 
00131         if (i != word::npos)
00132         {
00133             string exponentStr = specieName
00134             (
00135                 i + 1,
00136                 specieName.size() - i - 1
00137             );
00138             exponent = atof(exponentStr.c_str());
00139             specieName = specieName(0, i);
00140         }
00141 
00142         index = species[specieName];
00143     }
00144     else
00145     {
00146         FatalIOErrorIn("Reaction<ReactionThermo>::lrhs(Istream& is)", is)
00147             << "Expected a word but found " << t.info()
00148             << exit(FatalIOError);
00149     }
00150 }
00151 
00152 
00153 template<class ReactionThermo>
00154 void Reaction<ReactionThermo>::setLRhs(Istream& is)
00155 {
00156     DynamicList<specieCoeffs> dlrhs;
00157 
00158     while (is)
00159     {
00160         dlrhs.append(specieCoeffs(species_, is));
00161 
00162         token t(is);
00163 
00164         if (t.isPunctuation())
00165         {
00166             if (t == token::ADD)
00167             {
00168             }
00169             else if (t == token::ASSIGN)
00170             {
00171                 lhs_ = dlrhs.shrink();
00172                 dlrhs.clear();
00173             }
00174             else
00175             {
00176                 rhs_ = dlrhs.shrink();
00177                 is.putBack(t);
00178                 return;
00179             }
00180         }
00181         else
00182         {
00183             rhs_ = dlrhs.shrink();
00184             is.putBack(t);
00185             return;
00186         }
00187     }
00188 
00189     FatalIOErrorIn("Reaction<ReactionThermo>::lrhs(Istream& is)", is)
00190         << "Cannot continue reading reaction data from stream"
00191         << exit(FatalIOError);
00192 }
00193 
00194 
00195 //- Construct from Istream
00196 template<class ReactionThermo>
00197 Reaction<ReactionThermo>::Reaction
00198 (
00199     const speciesTable& species,
00200     const HashPtrTable<ReactionThermo>& thermoDatabase,
00201     Istream& is
00202 )
00203 :
00204     ReactionThermo(*thermoDatabase[species[0]]),
00205     species_(species)
00206 {
00207     setLRhs(is);
00208     setThermo(thermoDatabase);
00209 }
00210     
00211 
00212 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
00213 
00214 template<class ReactionThermo>
00215 autoPtr<Reaction<ReactionThermo> > Reaction<ReactionThermo>::New
00216 (
00217     const speciesTable& species,
00218     const HashPtrTable<ReactionThermo>& thermoDatabase,
00219     Istream& is
00220 )
00221 {
00222     if (is.eof())
00223     {
00224         FatalIOErrorIn
00225         (
00226             "Reaction<ReactionThermo>::New(const speciesTable& species,"
00227             " const HashPtrTable<ReactionThermo>& thermoDatabase, Istream&)",
00228             is
00229         )   << "Reaction type not specified" << endl << endl
00230             << "Valid Reaction types are :" << endl
00231             << IstreamConstructorTablePtr_->toc()
00232             << exit(FatalIOError);
00233     }
00234 
00235     word reactionTypeName(is);
00236 
00237     typename IstreamConstructorTable::iterator cstrIter
00238         = IstreamConstructorTablePtr_->find(reactionTypeName);
00239 
00240     if (cstrIter == IstreamConstructorTablePtr_->end())
00241     {
00242         FatalIOErrorIn
00243         (
00244             "Reaction<ReactionThermo>::New(const speciesTable& species,"
00245             " const HashPtrTable<ReactionThermo>& thermoDatabase, Istream&)",
00246             is
00247         )   << "Unknown reaction type " << reactionTypeName << endl << endl
00248             << "Valid reaction types are :" << endl
00249             << IstreamConstructorTablePtr_->toc()
00250             << exit(FatalIOError);
00251     }
00252 
00253     return autoPtr<Reaction<ReactionThermo> >
00254     (
00255         cstrIter()(species, thermoDatabase, is)
00256     );
00257 }
00258 
00259 
00260 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00261 
00262 template<class ReactionThermo>
00263 void Reaction<ReactionThermo>::write(Ostream& os) const
00264 {
00265     os << type() << nl << "    ";
00266 
00267     forAll(lhs_, i)
00268     {
00269         const typename Reaction<ReactionThermo>::specieCoeffs& sc = lhs_[i];
00270 
00271         if (sc.stoichCoeff != 1)
00272         {
00273             os << sc.stoichCoeff;
00274         }
00275 
00276         os << species_[sc.index];
00277 
00278         if (sc.exponent != sc.stoichCoeff)
00279         {
00280             os << '^' << sc.exponent;
00281         }
00282 
00283         if (i < lhs_.size() - 1)
00284         {
00285             os << " + ";
00286         }
00287     }
00288 
00289     os << " = ";
00290 
00291     forAll(rhs_, i)
00292     {
00293         const typename Reaction<ReactionThermo>::specieCoeffs& sc = rhs_[i];
00294 
00295         if (sc.stoichCoeff != 1)
00296         {
00297             os << sc.stoichCoeff;
00298         }
00299 
00300         os << species_[sc.index];
00301 
00302         if (sc.exponent != sc.stoichCoeff)
00303         {
00304             os << '^' << sc.exponent;
00305         }
00306 
00307         if (i < rhs_.size() - 1)
00308         {
00309             os << " + ";
00310         }
00311     }
00312 
00313     os  << endl << "   ";
00314 }
00315 
00316 
00317 template<class ReactionThermo>
00318 scalar Reaction<ReactionThermo>::kf
00319 (
00320     const scalar T,
00321     const scalar p,
00322     const scalarField& c
00323 ) const
00324 {
00325     return 0.0;
00326 }
00327 
00328 
00329 template<class ReactionThermo>
00330 scalar Reaction<ReactionThermo>::kr
00331 (
00332     const scalar kfwd,
00333     const scalar T,
00334     const scalar p,
00335     const scalarField& c
00336 ) const
00337 {
00338     return 0.0;
00339 }
00340 
00341 template<class ReactionThermo>
00342 scalar Reaction<ReactionThermo>::kr
00343 (
00344     const scalar T,
00345     const scalar p,
00346     const scalarField& c
00347 ) const
00348 {
00349     return 0.0;
00350 }
00351 
00352 
00353 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00354 
00355 } // End namespace Foam
00356 
00357 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd