FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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