00001
00002
00003
00004
00005
00006
00007
00008
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
00038
00039
00040
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
00054
00055 *this = operator=(right);
00056 }
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 Phase &Phase::operator=(const Phase &right) {
00068
00069
00070
00071 if (this == &right) return *this;
00072
00073
00074
00075 (void) Constituents::operator=(right);
00076 (void) State::operator=(right);
00077
00078
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
00086
00087
00088
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
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
00186
00187
00188
00189
00190
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
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
00226 void Phase::setState_TRX(doublereal t, doublereal dens,
00227 compositionMap& x) {
00228 setMoleFractionsByName(x); setTemperature(t); setDensity(dens);
00229 }
00230
00231
00232 void Phase::setState_TRY(doublereal t, doublereal dens,
00233 const doublereal* y) {
00234 setMassFractions(y); setTemperature(t); setDensity(dens);
00235 }
00236
00237
00238 void Phase::setState_TRY(doublereal t, doublereal dens,
00239 compositionMap& y) {
00240 setMassFractionsByName(y); setTemperature(t); setDensity(dens);
00241 }
00242
00243
00244 void Phase::setState_TR(doublereal t, doublereal rho) {
00245 setTemperature(t); setDensity(rho);
00246 }
00247
00248
00249 void Phase::setState_TX(doublereal t, doublereal* x) {
00250 setTemperature(t); setMoleFractions(x);
00251 }
00252
00253
00254 void Phase::setState_TY(doublereal t, doublereal* y) {
00255 setTemperature(t); setMassFractions(y);
00256 }
00257
00258
00259 void Phase::setState_RX(doublereal rho, doublereal* x) {
00260 setMoleFractions(x); setDensity(rho);
00261 }
00262
00263
00264 void Phase::setState_RY(doublereal rho, doublereal* y) {
00265 setMassFractions(y); setDensity(rho);
00266 }
00267
00268
00269
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
00279
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
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
00296
00297
00298 const array_fp& Phase::molecularWeights() const {
00299 return Constituents::molecularWeights();
00300 }
00301
00302
00303
00304
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
00346
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
00359
00360 m_kk = nSpecies();
00361 }
00362
00363 bool Phase::ready() const {
00364 return (m_kk > 0 && Constituents::ready() && State::ready());
00365 }
00366
00367
00368
00369
00370
00371
00372
00373
00374 }