Phase.cpp

Go to the documentation of this file.
00001 /**
00002  *  @file Phase.cpp
00003  *   Definition file for class, Phase, which contains functions for setting the
00004  *   state of a phase, and for referencing species by name
00005  *   (see \ref phases and class \link Cantera::Phase Phase\endlink).
00006  */
00007 
00008 // Copyright 2001  California Institute of Technology
00009 
00010 #ifdef WIN32
00011 #pragma warning(disable:4786)
00012 #pragma warning(disable:4503)
00013 #endif
00014 
00015 #include "ct_defs.h"
00016 #include "Phase.h"
00017 #include "vec_functions.h"
00018 #include "ctexceptions.h"
00019 
00020 using namespace std;
00021 
00022 namespace Cantera {    
00023 
00024   Phase::Phase() : 
00025     Constituents(),
00026     State(),
00027     m_kk(-1), 
00028     m_ndim(3),
00029     m_index(-1), 
00030     m_xml(new XML_Node("phase")), 
00031     m_id("<phase>"),
00032     m_name("") 
00033   {
00034   }
00035 
00036   /*
00037    * Copy Constructor
00038    *
00039    * This function just does the default initialization, and
00040    * then calls the assignment operator.
00041    */
00042   Phase::Phase(const Phase &right) :
00043     Constituents(),
00044     State(),
00045     m_kk(-1),
00046     m_ndim(3),
00047     m_index(-1), 
00048     m_xml(new XML_Node("phase")), 
00049     m_id("<phase>"),
00050     m_name("") 
00051   {
00052     /*
00053      * Call the assignment operator.
00054      */
00055     *this = operator=(right);
00056   }
00057     
00058   /*
00059    * Assignment operator
00060    *
00061    * This operation is sort of complicated. We have to
00062    * call the assignment operator for the Constituents and
00063    * State operators that Phase inherits from. Then,
00064    * we have to copy our own data, making sure to do a 
00065    * deep copy on the XML_Node data owned by this object.
00066    */
00067   Phase &Phase::operator=(const Phase &right) {
00068     /*
00069      * Check for self assignment.
00070      */
00071     if (this == &right) return *this;
00072     /*
00073      * Now call the inherited-classes assignment operators.
00074      */
00075     (void) Constituents::operator=(right);
00076     (void) State::operator=(right);
00077     /*
00078      * Handle its own data
00079      */
00080     m_kk    = right.m_kk;
00081     m_ndim  = right.m_ndim;
00082     m_index = right.m_index;
00083     m_data  = right.m_data;
00084     /*
00085      * This is a little complicated. -> Because we delete m_xml
00086      * in the destructor, we own m_xml completely, and we need
00087      * to have our own individual copies of the XML data tree
00088      * in each object
00089      */
00090     if (m_xml) {
00091       delete m_xml;
00092       m_xml = 0;
00093     }
00094     if (right.m_xml) {
00095       m_xml   = new XML_Node();
00096       (right.m_xml)->copy(m_xml);
00097     }
00098     m_id    = right.m_id;
00099     m_name  = right.m_name;
00100 
00101     return *this;
00102   }
00103 
00104   // Destructor.
00105   Phase::~Phase() { 
00106     if (m_xml) {
00107       delete m_xml;
00108       m_xml = 0;
00109     }
00110   }
00111 
00112   XML_Node& Phase::xml() { 
00113     return *m_xml;
00114   }
00115 
00116   std::string Phase::id() const { 
00117     return m_id; 
00118   }
00119 
00120   void Phase::setID(std::string id) {
00121     m_id = id;
00122   } 
00123 
00124   std::string Phase::name() const {
00125     return m_name; 
00126   }
00127 
00128   void Phase::setName(std::string nm) { 
00129     m_name = nm; 
00130   }
00131 
00132   int Phase::index() const { 
00133     return m_index;
00134   }
00135 
00136   void Phase::setIndex(int m) { 
00137     m_index = m;
00138   }
00139 
00140   void Phase::saveState(vector_fp& state) const {
00141     state.resize(nSpecies() + 2);
00142     saveState(state.size(),&(state[0]));
00143   }
00144   void Phase::saveState(int lenstate, doublereal* state) const {
00145     state[0] = temperature();
00146     state[1] = density();
00147     getMassFractions(state + 2);
00148   }
00149 
00150   void Phase::restoreState(const vector_fp& state) {
00151     restoreState(state.size(),&state[0]);
00152   }
00153 
00154   void Phase::restoreState(int lenstate, const doublereal* state) {
00155     if (int(lenstate) >= nSpecies() + 2) {
00156       setMassFractions_NoNorm(state + 2);
00157       setTemperature(state[0]);
00158       setDensity(state[1]);
00159     }
00160     else {
00161       throw ArraySizeError("Phase::restoreState",
00162                            lenstate,nSpecies()+2);
00163     }
00164   }
00165 
00166   void Phase::setMoleFractionsByName(compositionMap& xMap) {
00167     int kk = nSpecies();
00168     doublereal x;
00169     vector_fp mf(kk, 0.0);
00170     for (int k = 0; k < kk; k++) {
00171       x = xMap[speciesName(k)];
00172       if (x > 0.0) mf[k] = x;
00173     }
00174     setMoleFractions(&mf[0]);
00175   }
00176 
00177   void Phase::setMoleFractionsByName(const std::string& x) {
00178     compositionMap xx;
00179     int kk = nSpecies();
00180     for (int k = 0; k < kk; k++) { 
00181       xx[speciesName(k)] = -1.0;
00182     }
00183     parseCompString(x, xx);
00184     setMoleFractionsByName(xx);
00185     //int kk = nSpecies();
00186     //vector_fp mf(kk);
00187     //for (int k = 0; k < kk; k++) { 
00188     //    mf[k] = xx[speciesName(k)];
00189     //}
00190     //setMoleFractions(mf.begin());
00191   }
00192 
00193   void Phase::setMassFractionsByName(compositionMap& yMap) {
00194     int kk = nSpecies();
00195     doublereal y;
00196     vector_fp mf(kk, 0.0);
00197     for (int k = 0; k < kk; k++) { 
00198       y = yMap[speciesName(k)];
00199       if (y > 0.0) mf[k] = y;
00200     }
00201     setMassFractions(&mf[0]);
00202   }
00203 
00204   void Phase::setMassFractionsByName(const std::string& y) {
00205     compositionMap yy;
00206     int kk = nSpecies();
00207     for (int k = 0; k < kk; k++) { 
00208       yy[speciesName(k)] = -1.0;
00209     }
00210     parseCompString(y, yy);
00211     setMassFractionsByName(yy);
00212   }
00213 
00214   /** Set the temperature (K), density (kg/m^3), and mole fractions. */
00215   void Phase::setState_TRX(doublereal t, doublereal dens, 
00216                            const doublereal* x) {
00217     setMoleFractions(x); setTemperature(t); setDensity(dens);
00218   }
00219 
00220   void Phase::setState_TNX(doublereal t, doublereal n, 
00221                            const doublereal* x) {
00222     setMoleFractions(x); setTemperature(t); setMolarDensity(n);
00223   }
00224 
00225   /** Set the temperature (K), density (kg/m^3), and mole fractions. */
00226   void Phase::setState_TRX(doublereal t, doublereal dens, 
00227                            compositionMap& x) {
00228     setMoleFractionsByName(x); setTemperature(t); setDensity(dens);
00229   }
00230 
00231   /** Set the temperature (K), density (kg/m^3), and mass fractions. */
00232   void Phase::setState_TRY(doublereal t, doublereal dens, 
00233                            const doublereal* y) {
00234     setMassFractions(y); setTemperature(t); setDensity(dens);
00235   }        
00236 
00237   /** Set the temperature (K), density (kg/m^3), and mass fractions. */
00238   void Phase::setState_TRY(doublereal t, doublereal dens, 
00239                            compositionMap& y) {
00240     setMassFractionsByName(y); setTemperature(t); setDensity(dens);
00241   }
00242     
00243   /** Set the temperature (K) and density (kg/m^3) */
00244   void Phase::setState_TR(doublereal t, doublereal rho) {
00245     setTemperature(t); setDensity(rho);
00246   }
00247     
00248   /** Set the temperature (K) and mole fractions.  */
00249   void Phase::setState_TX(doublereal t, doublereal* x) {
00250     setTemperature(t); setMoleFractions(x);
00251   }
00252 
00253   /** Set the temperature (K) and mass fractions.  */
00254   void Phase::setState_TY(doublereal t, doublereal* y) {
00255     setTemperature(t); setMassFractions(y);
00256   }
00257 
00258   /** Set the density (kg/m^3) and mole fractions.  */
00259   void Phase::setState_RX(doublereal rho, doublereal* x) {
00260     setMoleFractions(x); setDensity(rho);
00261   }
00262 
00263   /** Set the density (kg/m^3) and mass fractions.  */
00264   void Phase::setState_RY(doublereal rho, doublereal* y) {
00265     setMassFractions(y); setDensity(rho);
00266   }
00267 
00268   /*
00269    * Copy the vector of molecular weights into vector weights.
00270    */
00271   void Phase::getMolecularWeights(vector_fp& weights) const {
00272     const array_fp& mw = Constituents::molecularWeights();
00273     if (weights.size() < mw.size()) weights.resize(mw.size());
00274     copy(mw.begin(), mw.end(), weights.begin());
00275   }
00276 
00277   /*
00278    * Copy the vector of molecular weights into array weights.
00279    * @deprecated
00280    */
00281   void Phase::getMolecularWeights(int iwt, doublereal* weights) const {
00282     const array_fp& mw = Constituents::molecularWeights();
00283     copy(mw.begin(), mw.end(), weights);
00284   }
00285 
00286   /*
00287    * Copy the vector of molecular weights into array weights.
00288    */
00289   void Phase::getMolecularWeights(doublereal* weights) const {
00290     const array_fp& mw = Constituents::molecularWeights();
00291     copy(mw.begin(), mw.end(), weights);
00292   }
00293 
00294   /**
00295    * Return a const reference to the internal vector of
00296    * molecular weights.
00297    */
00298   const array_fp& Phase::molecularWeights() const {
00299     return Constituents::molecularWeights(); 
00300   }
00301 
00302 
00303   /**
00304    * Get the mole fractions by name. 
00305    */
00306   void Phase::getMoleFractionsByName(compositionMap& x) const {
00307     x.clear();
00308     int kk = nSpecies();
00309     for (int k = 0; k < kk; k++) {
00310       x[speciesName(k)] = State::moleFraction(k);
00311     }
00312   }
00313 
00314   doublereal Phase::moleFraction(int k) const {
00315     return State::moleFraction(k);
00316   }
00317 
00318   doublereal Phase::moleFraction(std::string name) const {
00319     int iloc = speciesIndex(name);
00320     if (iloc >= 0) return State::moleFraction(iloc);
00321     else return 0.0;
00322   }
00323 
00324   doublereal Phase::massFraction(int k) const {
00325     return State::massFraction(k);
00326   }
00327 
00328   doublereal Phase::massFraction(std::string name) const {
00329     int iloc = speciesIndex(name);
00330     if (iloc >= 0) return massFractions()[iloc];
00331     else return 0.0;
00332   }
00333 
00334   doublereal Phase::chargeDensity() const {
00335     int k;
00336     int nsp = nSpecies();
00337     doublereal cdens = 0.0;
00338     for (k = 0; k < nsp; k++) 
00339       cdens += charge(k)*State::moleFraction(k);
00340     cdens *= Faraday;
00341     return cdens;
00342   }
00343 
00344   /** 
00345    *  Finished adding species, prepare to use them for calculation
00346    *  of mixture properties.
00347    */
00348   void Phase::freezeSpecies() {
00349     Constituents::freezeSpecies();
00350     init(Constituents::molecularWeights());
00351     int kk = nSpecies();
00352     int nv = kk + 2;
00353     m_data.resize(nv,0.0);
00354     m_data[0] = 300.0;
00355     m_data[1] = 0.001;
00356     m_data[2] = 1.0;
00357 
00358     //setState_TRY(300.0, density(), &m_data[2]);
00359 
00360     m_kk = nSpecies();
00361   } 
00362 
00363   bool Phase::ready() const {
00364     return (m_kk > 0 && Constituents::ready() && State::ready());
00365   }
00366 
00367   //         int Phase::installUpdater_T(Updater* u) {
00368   //             return m_T_updater.install(u);
00369   //         }
00370 
00371   //         int Phase::installUpdater_C(Updater* u) {
00372   //             return m_C_updater.install(u);
00373   //         }
00374 }
Generated by  doxygen 1.6.3