From 3fae5d93f75fe037c07ae8159dd6e68a64e94cd6 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Wed, 8 Aug 2012 22:17:33 +0000 Subject: [PATCH] Refactored ThermoPhase::reportCSV The reportCSV method is responsible for formatting and writing the data provided by the getCsvReportData method. Derived classes need only override the latter method to customize their reports. Also, the data which was not in a CSV format has been removed from the output so the name of the method is no longer misleading. --- include/cantera/thermo/MolalityVPSSTP.h | 10 +- include/cantera/thermo/PureFluidPhase.h | 8 - include/cantera/thermo/ThermoPhase.h | 9 +- src/thermo/MolalityVPSSTP.cpp | 161 ++++--------------- src/thermo/PureFluidPhase.cpp | 146 ----------------- src/thermo/ThermoPhase.cpp | 201 +++++++----------------- 6 files changed, 100 insertions(+), 435 deletions(-) diff --git a/include/cantera/thermo/MolalityVPSSTP.h b/include/cantera/thermo/MolalityVPSSTP.h index 494a72083..58f7e7b4e 100644 --- a/include/cantera/thermo/MolalityVPSSTP.h +++ b/include/cantera/thermo/MolalityVPSSTP.h @@ -821,16 +821,12 @@ public: */ virtual std::string report(bool show_thermo = true) const; - //! returns a summary of the state of the phase to specified - //! comma separated files - /*! - * @param csvFile ofstream file to print comma separated data for - * the phase - */ - virtual void reportCSV(std::ofstream& csvFile) const; protected: + virtual void getCsvReportData(std::vector& names, + std::vector& data) const; + //! Get the array of unscaled non-dimensional molality based //! activity coefficients at the current solution temperature, //! pressure, and solution concentration. diff --git a/include/cantera/thermo/PureFluidPhase.h b/include/cantera/thermo/PureFluidPhase.h index 10655044e..1cfe75204 100644 --- a/include/cantera/thermo/PureFluidPhase.h +++ b/include/cantera/thermo/PureFluidPhase.h @@ -540,14 +540,6 @@ public: */ virtual std::string report(bool show_thermo = true) const; - //! returns a summary of the state of the phase to specified - //! comma separated files - /*! - * @param csvFile ofstream file to print comma separated data for - * the phase - */ - virtual void reportCSV(std::ofstream& csvFile) const; - protected: //! Main call to the tpx level to set the state of the system diff --git a/include/cantera/thermo/ThermoPhase.h b/include/cantera/thermo/ThermoPhase.h index b2f26bc1c..2dce1fb11 100644 --- a/include/cantera/thermo/ThermoPhase.h +++ b/include/cantera/thermo/ThermoPhase.h @@ -1595,7 +1595,9 @@ public: */ virtual std::string report(bool show_thermo = true) const; - //! returns a summary of the state of the phase to a comma separated file + //! returns a summary of the state of the phase to a comma separated file. + //! To customize the data included in the report, derived classes should + //! override the getCsvReportData method. /*! * @param csvFile ofstream file to print comma separated data for * the phase @@ -1606,6 +1608,11 @@ public: protected: + //! Fills `names` and `data` with the column names and species thermo + //! properties to be included in the output of the reportCSV method. + virtual void getCsvReportData(std::vector& names, + std::vector& data) const; + //! Pointer to the calculation manager for species //! reference-state thermodynamic properties /*! diff --git a/src/thermo/MolalityVPSSTP.cpp b/src/thermo/MolalityVPSSTP.cpp index 56c543dea..54adfb043 100644 --- a/src/thermo/MolalityVPSSTP.cpp +++ b/src/thermo/MolalityVPSSTP.cpp @@ -955,145 +955,44 @@ std::string MolalityVPSSTP::report(bool show_thermo) const /* * Format a summary of the mixture state for output. */ -void MolalityVPSSTP::reportCSV(std::ofstream& csvFile) const +void MolalityVPSSTP::getCsvReportData(std::vector& names, + std::vector& data) const { + names.clear(); + data.assign(10, vector_fp(nSpecies())); + names.push_back("X"); + getMoleFractions(&data[0][0]); - csvFile.precision(3); - int tabS = 15; - int tabM = 30; - int tabL = 40; - try { - if (name() != "") { - csvFile << "\n"+name()+"\n\n"; - } - csvFile << setw(tabL) << "temperature (K) =" << setw(tabS) << temperature() << endl; - csvFile << setw(tabL) << "pressure (Pa) =" << setw(tabS) << pressure() << endl; - csvFile << setw(tabL) << "density (kg/m^3) =" << setw(tabS) << density() << endl; - csvFile << setw(tabL) << "mean mol. weight (amu) =" << setw(tabS) << meanMolecularWeight() << endl; - csvFile << setw(tabL) << "potential (V) =" << setw(tabS) << electricPotential() << endl; - csvFile << endl; + names.push_back("Molal"); + getMolalities(&data[1][0]); - csvFile << setw(tabL) << "enthalpy (J/kg) = " << setw(tabS) << enthalpy_mass() << setw(tabL) << "enthalpy (J/kmol) = " << setw(tabS) << enthalpy_mole() << endl; - csvFile << setw(tabL) << "internal E (J/kg) = " << setw(tabS) << intEnergy_mass() << setw(tabL) << "internal E (J/kmol) = " << setw(tabS) << intEnergy_mole() << endl; - csvFile << setw(tabL) << "entropy (J/kg) = " << setw(tabS) << entropy_mass() << setw(tabL) << "entropy (J/kmol) = " << setw(tabS) << entropy_mole() << endl; - csvFile << setw(tabL) << "Gibbs (J/kg) = " << setw(tabS) << gibbs_mass() << setw(tabL) << "Gibbs (J/kmol) = " << setw(tabS) << gibbs_mole() << endl; - csvFile << setw(tabL) << "heat capacity c_p (J/K/kg) = " << setw(tabS) << cp_mass() << setw(tabL) << "heat capacity c_p (J/K/kmol) = " << setw(tabS) << cp_mole() << endl; - csvFile << setw(tabL) << "heat capacity c_v (J/K/kg) = " << setw(tabS) << cv_mass() << setw(tabL) << "heat capacity c_v (J/K/kmol) = " << setw(tabS) << cv_mole() << endl; + names.push_back("Chem. Pot. (J/kmol)"); + getChemPotentials(&data[2][0]); - csvFile.precision(8); + names.push_back("Chem. Pot. SS (J/kmol)"); + getStandardChemPotentials(&data[3][0]); - vector pNames; - vector data; - vector_fp temp(nSpecies()); + names.push_back("Molal Act. Coeff."); + getMolalityActivityCoefficients(&data[4][0]); - getMoleFractions(&temp[0]); - pNames.push_back("X"); - data.push_back(temp); - try { - getMolalities(&temp[0]); - pNames.push_back("Molal"); - data.push_back(temp); - } catch (CanteraError& err) { - err.save(); - } - try { - getChemPotentials(&temp[0]); - pNames.push_back("Chem. Pot. (J/kmol)"); - data.push_back(temp); - } catch (CanteraError& err) { - err.save(); - } - try { - getStandardChemPotentials(&temp[0]); - pNames.push_back("Chem. Pot. SS (J/kmol)"); - data.push_back(temp); - } catch (CanteraError& err) { - err.save(); - } - try { - getMolalityActivityCoefficients(&temp[0]); - pNames.push_back("Molal Act. Coeff."); - data.push_back(temp); - } catch (CanteraError& err) { - err.save(); - } - try { - getActivities(&temp[0]); - pNames.push_back("Molal Activity"); - data.push_back(temp); - size_t iHp = speciesIndex("H+"); - if (iHp != npos) { - double pH = -log(temp[iHp]) / log(10.0); - csvFile << setw(tabL) << "pH = " << setw(tabS) << pH << endl; - } - } catch (CanteraError& err) { - err.save(); - } - try { - getPartialMolarEnthalpies(&temp[0]); - pNames.push_back("Part. Mol Enthalpy (J/kmol)"); - data.push_back(temp); - } catch (CanteraError& err) { - err.save(); - } - try { - getPartialMolarEntropies(&temp[0]); - pNames.push_back("Part. Mol. Entropy (J/K/kmol)"); - data.push_back(temp); - } catch (CanteraError& err) { - err.save(); - } - try { - getPartialMolarIntEnergies(&temp[0]); - pNames.push_back("Part. Mol. Energy (J/kmol)"); - data.push_back(temp); - } catch (CanteraError& err) { - err.save(); - } - try { - getPartialMolarCp(&temp[0]); - pNames.push_back("Part. Mol. Cp (J/K/kmol"); - data.push_back(temp); - } catch (CanteraError& err) { - err.save(); - } - try { - getPartialMolarVolumes(&temp[0]); - pNames.push_back("Part. Mol. Cv (J/K/kmol)"); - data.push_back(temp); - } catch (CanteraError& err) { - err.save(); - } + names.push_back("Molal Activity"); + getActivities(&data[5][0]); - csvFile << endl << setw(tabS) << "Species,"; - for (size_t i = 0; i < pNames.size(); i++) { - csvFile << setw(tabM) << pNames[i] << ","; - } - csvFile << endl; - /* - csvFile.fill('-'); - csvFile << setw(tabS+(tabM+1)*pNames.size()) << "-\n"; - csvFile.fill(' '); - */ - for (size_t k = 0; k < nSpecies(); k++) { - csvFile << setw(tabS) << speciesName(k) + ","; - if (data[0][k] > SmallNumber) { - for (size_t i = 0; i < pNames.size(); i++) { - csvFile << setw(tabM) << data[i][k] << ","; - } - csvFile << endl; - } else { - for (size_t i = 0; i < pNames.size(); i++) { - csvFile << setw(tabM) << 0 << ","; - } - csvFile << endl; - } - } - } catch (CanteraError& err) { - err.save(); - } + names.push_back("Part. Mol Enthalpy (J/kmol)"); + getPartialMolarEnthalpies(&data[5][0]); + + names.push_back("Part. Mol. Entropy (J/K/kmol)"); + getPartialMolarEntropies(&data[6][0]); + + names.push_back("Part. Mol. Energy (J/kmol)"); + getPartialMolarIntEnergies(&data[7][0]); + + names.push_back("Part. Mol. Cp (J/K/kmol"); + getPartialMolarCp(&data[8][0]); + + names.push_back("Part. Mol. Cv (J/K/kmol)"); + getPartialMolarVolumes(&data[9][0]); } } - diff --git a/src/thermo/PureFluidPhase.cpp b/src/thermo/PureFluidPhase.cpp index de755b975..6c0ee0461 100644 --- a/src/thermo/PureFluidPhase.cpp +++ b/src/thermo/PureFluidPhase.cpp @@ -555,8 +555,6 @@ void PureFluidPhase::setState_Psat(doublereal p, doublereal x) */ std::string PureFluidPhase::report(bool show_thermo) const { - - char p[800]; string s = ""; try { @@ -665,149 +663,5 @@ std::string PureFluidPhase::report(bool show_thermo) const } return s; } -//==================================================================================================================== -/* - * Format a summary of the mixture state for output. - */ -void PureFluidPhase::reportCSV(std::ofstream& csvFile) const -{ - - csvFile.precision(3); - int tabS = 15; - int tabM = 30; - int tabL = 40; - try { - if (name() != "") { - csvFile << "\n"+name()+"\n\n"; - } - csvFile << setw(tabL) << "temperature (K) =" << setw(tabS) << temperature() << endl; - csvFile << setw(tabL) << "pressure (Pa) =" << setw(tabS) << pressure() << endl; - csvFile << setw(tabL) << "density (kg/m^3) =" << setw(tabS) << density() << endl; - csvFile << setw(tabL) << "mean mol. weight (amu) =" << setw(tabS) << meanMolecularWeight() << endl; - csvFile << setw(tabL) << "potential (V) =" << setw(tabS) << electricPotential() << endl; - if (eosType() == cPureFluid) { - double xx = ((PureFluidPhase*)(this))->vaporFraction(); - csvFile << setw(tabL) << "vapor fraction = " << setw(tabS) << xx << endl; - } - csvFile << endl; - - csvFile << setw(tabL) << "enthalpy (J/kg) = " << setw(tabS) << enthalpy_mass() << setw(tabL) << "enthalpy (J/kmol) = " << setw(tabS) << enthalpy_mole() << endl; - csvFile << setw(tabL) << "internal E (J/kg) = " << setw(tabS) << intEnergy_mass() << setw(tabL) << "internal E (J/kmol) = " << setw(tabS) << intEnergy_mole() << endl; - csvFile << setw(tabL) << "entropy (J/kg) = " << setw(tabS) << entropy_mass() << setw(tabL) << "entropy (J/kmol) = " << setw(tabS) << entropy_mole() << endl; - csvFile << setw(tabL) << "Gibbs (J/kg) = " << setw(tabS) << gibbs_mass() << setw(tabL) << "Gibbs (J/kmol) = " << setw(tabS) << gibbs_mole() << endl; - csvFile << setw(tabL) << "heat capacity c_p (J/K/kg) = " << setw(tabS) << cp_mass() << setw(tabL) << "heat capacity c_p (J/K/kmol) = " << setw(tabS) << cp_mole() << endl; - csvFile << setw(tabL) << "heat capacity c_v (J/K/kg) = " << setw(tabS) << cv_mass() << setw(tabL) << "heat capacity c_v (J/K/kmol) = " << setw(tabS) << cv_mole() << endl; - - csvFile.precision(8); - - size_t kk = nSpecies(); - std::vector x(kk, 0.0); - std::vector y(kk, 0.0); - std::vector mu(kk, 0.0); - std::vector a(kk, 0.0); - std::vector ac(kk, 0.0); - std::vector hbar(kk, 0.0); - std::vector sbar(kk, 0.0); - std::vector ubar(kk, 0.0); - std::vector cpbar(kk, 0.0); - std::vector vbar(kk, 0.0); - std::vector pNames; - std::vector > data; - - getMoleFractions(&x[0]); - pNames.push_back("X"); - data.push_back(x); - try { - getMassFractions(&y[0]); - pNames.push_back("Y"); - data.push_back(y); - } catch (CanteraError& err) { - err.save(); - } - try { - getChemPotentials(&mu[0]); - pNames.push_back("Chem. Pot (J/kmol)"); - data.push_back(mu); - } catch (CanteraError& err) { - err.save(); - } - try { - getActivities(&a[0]); - pNames.push_back("Activity"); - data.push_back(a); - } catch (CanteraError& err) { - err.save(); - } - try { - getActivityCoefficients(&ac[0]); - pNames.push_back("Act. Coeff."); - data.push_back(ac); - } catch (CanteraError& err) { - err.save(); - } - try { - getPartialMolarEnthalpies(&hbar[0]); - pNames.push_back("Part. Mol Enthalpy (J/kmol)"); - data.push_back(hbar); - } catch (CanteraError& err) { - err.save(); - } - try { - getPartialMolarEntropies(&sbar[0]); - pNames.push_back("Part. Mol. Entropy (J/K/kmol)"); - data.push_back(sbar); - } catch (CanteraError& err) { - err.save(); - } - try { - getPartialMolarIntEnergies(&ubar[0]); - pNames.push_back("Part. Mol. Energy (J/kmol)"); - data.push_back(ubar); - } catch (CanteraError& err) { - err.save(); - } - try { - getPartialMolarCp(&cpbar[0]); - pNames.push_back("Part. Mol. Cp (J/K/kmol"); - data.push_back(cpbar); - } catch (CanteraError& err) { - err.save(); - } - try { - getPartialMolarVolumes(&vbar[0]); - pNames.push_back("Part. Mol. Cv (J/K/kmol)"); - data.push_back(vbar); - } catch (CanteraError& err) { - err.save(); - } - - csvFile << endl << setw(tabS) << "Species,"; - for (int i = 0; i < (int)pNames.size(); i++) { - csvFile << setw(tabM) << pNames[i] << ","; - } - csvFile << endl; - /* - csvFile.fill('-'); - csvFile << setw(tabS+(tabM+1)*pNames.size()) << "-\n"; - csvFile.fill(' '); - */ - for (size_t k = 0; k < kk; k++) { - csvFile << setw(tabS) << speciesName(k) + ","; - if (x[k] > SmallNumber) { - for (int i = 0; i < (int)pNames.size(); i++) { - csvFile << setw(tabM) << data[i][k] << ","; - } - csvFile << endl; - } else { - for (int i = 0; i < (int)pNames.size(); i++) { - csvFile << setw(tabM) << 0 << ","; - } - csvFile << endl; - } - } - } catch (CanteraError& err) { - err.save(); - } -} } diff --git a/src/thermo/ThermoPhase.cpp b/src/thermo/ThermoPhase.cpp index 498bc00f7..c5defa7a3 100644 --- a/src/thermo/ThermoPhase.cpp +++ b/src/thermo/ThermoPhase.cpp @@ -1354,156 +1354,73 @@ std::string ThermoPhase::report(bool show_thermo) const */ void ThermoPhase::reportCSV(std::ofstream& csvFile) const { - - csvFile.precision(3); int tabS = 15; int tabM = 30; - int tabL = 40; - try { - if (name() != "") { - csvFile << "\n"+name()+"\n\n"; - } - csvFile << setw(tabL) << "temperature (K) =" << setw(tabS) << temperature() << endl; - csvFile << setw(tabL) << "pressure (Pa) =" << setw(tabS) << pressure() << endl; - csvFile << setw(tabL) << "density (kg/m^3) =" << setw(tabS) << density() << endl; - csvFile << setw(tabL) << "mean mol. weight (amu) =" << setw(tabS) << meanMolecularWeight() << endl; - csvFile << setw(tabL) << "potential (V) =" << setw(tabS) << electricPotential() << endl; - csvFile << endl; + csvFile.precision(8); - csvFile << setw(tabL) << "enthalpy (J/kg) = " << setw(tabS) << enthalpy_mass() << setw(tabL) - << "enthalpy (J/kmol) = " << setw(tabS) << enthalpy_mole() << endl; - csvFile << setw(tabL) << "internal E (J/kg) = " << setw(tabS) << intEnergy_mass() << setw(tabL) - << "internal E (J/kmol) = " << setw(tabS) << intEnergy_mole() << endl; - csvFile << setw(tabL) << "entropy (J/kg) = " << setw(tabS) << entropy_mass() << setw(tabL) - << "entropy (J/kmol) = " << setw(tabS) << entropy_mole() << endl; - csvFile << setw(tabL) << "Gibbs (J/kg) = " << setw(tabS) << gibbs_mass() << setw(tabL) - << "Gibbs (J/kmol) = " << setw(tabS) << gibbs_mole() << endl; - csvFile << setw(tabL) << "heat capacity c_p (J/K/kg) = " << setw(tabS) << cp_mass() - << setw(tabL) << "heat capacity c_p (J/K/kmol) = " << setw(tabS) << cp_mole() << endl; - csvFile << setw(tabL) << "heat capacity c_v (J/K/kg) = " << setw(tabS) << cv_mass() - << setw(tabL) << "heat capacity c_v (J/K/kmol) = " << setw(tabS) << cv_mole() << endl; + vector_fp X(nSpecies()); + getMoleFractions(&X[0]); - csvFile.precision(8); + std::vector pNames; + std::vector data; + getCsvReportData(pNames, data); - size_t kk = nSpecies(); - doublereal* x = new doublereal[kk]; - doublereal* y = new doublereal[kk]; - doublereal* mu = new doublereal[kk]; - doublereal* a = new doublereal[kk]; - doublereal* ac = new doublereal[kk]; - doublereal* hbar = new doublereal[kk]; - doublereal* sbar = new doublereal[kk]; - doublereal* ubar = new doublereal[kk]; - doublereal* cpbar= new doublereal[kk]; - doublereal* vbar = new doublereal[kk]; - std::vector pNames; - std::vector data; - - getMoleFractions(x); - pNames.push_back("X"); - data.push_back(x); - try { - getMassFractions(y); - pNames.push_back("Y"); - data.push_back(y); - } catch (CanteraError& err) { - err.save(); - } - try { - getChemPotentials(mu); - pNames.push_back("Chem. Pot (J/kmol)"); - data.push_back(mu); - } catch (CanteraError& err) { - err.save(); - } - try { - getActivities(a); - pNames.push_back("Activity"); - data.push_back(a); - } catch (CanteraError& err) { - err.save(); - } - try { - getActivityCoefficients(ac); - pNames.push_back("Act. Coeff."); - data.push_back(ac); - } catch (CanteraError& err) { - err.save(); - } - try { - getPartialMolarEnthalpies(hbar); - pNames.push_back("Part. Mol Enthalpy (J/kmol)"); - data.push_back(hbar); - } catch (CanteraError& err) { - err.save(); - } - try { - getPartialMolarEntropies(sbar); - pNames.push_back("Part. Mol. Entropy (J/K/kmol)"); - data.push_back(sbar); - } catch (CanteraError& err) { - err.save(); - } - try { - getPartialMolarIntEnergies(ubar); - pNames.push_back("Part. Mol. Energy (J/kmol)"); - data.push_back(ubar); - } catch (CanteraError& err) { - err.save(); - } - try { - getPartialMolarCp(cpbar); - pNames.push_back("Part. Mol. Cp (J/K/kmol"); - data.push_back(cpbar); - } catch (CanteraError& err) { - err.save(); - } - try { - getPartialMolarVolumes(vbar); - pNames.push_back("Part. Mol. Cv (J/K/kmol)"); - data.push_back(vbar); - } catch (CanteraError& err) { - err.save(); - } - - csvFile << endl << setw(tabS) << "Species,"; - for (size_t i = 0; i < pNames.size(); i++) { - csvFile << setw(tabM) << pNames[i] << ","; - } - csvFile << endl; - /* - csvFile.fill('-'); - csvFile << setw(tabS+(tabM+1)*pNames.size()) << "-\n"; - csvFile.fill(' '); - */ - for (size_t k = 0; k < kk; k++) { - csvFile << setw(tabS) << speciesName(k) + ","; - if (x[k] > SmallNumber) { - for (size_t i = 0; i < pNames.size(); i++) { - csvFile << setw(tabM) << data[i][k] << ","; - } - csvFile << endl; - } else { - for (size_t i = 0; i < pNames.size(); i++) { - csvFile << setw(tabM) << 0 << ","; - } - csvFile << endl; + csvFile << setw(tabS) << "Species,"; + for (size_t i = 0; i < pNames.size(); i++) { + csvFile << setw(tabM) << pNames[i] << ","; + } + csvFile << endl; + for (size_t k = 0; k < nSpecies(); k++) { + csvFile << setw(tabS) << speciesName(k) + ","; + if (X[k] > SmallNumber) { + for (size_t i = 0; i < pNames.size(); i++) { + csvFile << setw(tabM) << data[i][k] << ","; } + csvFile << endl; + } else { + for (size_t i = 0; i < pNames.size(); i++) { + csvFile << setw(tabM) << 0 << ","; + } + csvFile << endl; } - delete [] x; - delete [] y; - delete [] mu; - delete [] a; - delete [] ac; - delete [] hbar; - delete [] sbar; - delete [] ubar; - delete [] cpbar; - delete [] vbar; - - } catch (CanteraError& err) { - err.save(); } } +void ThermoPhase::getCsvReportData(std::vector& names, + std::vector& data) const +{ + names.clear(); + data.assign(10, vector_fp(nSpecies())); + + names.push_back("X"); + getMoleFractions(&data[0][0]); + + names.push_back("Y"); + getMassFractions(&data[1][0]); + + names.push_back("Chem. Pot (J/kmol)"); + getChemPotentials(&data[2][0]); + + names.push_back("Activity"); + getActivities(&data[3][0]); + + names.push_back("Act. Coeff."); + getActivityCoefficients(&data[4][0]); + + names.push_back("Part. Mol Enthalpy (J/kmol)"); + getPartialMolarEnthalpies(&data[5][0]); + + names.push_back("Part. Mol. Entropy (J/K/kmol)"); + getPartialMolarEntropies(&data[6][0]); + + names.push_back("Part. Mol. Energy (J/kmol)"); + getPartialMolarIntEnergies(&data[7][0]); + + names.push_back("Part. Mol. Cp (J/K/kmol"); + getPartialMolarCp(&data[8][0]); + + names.push_back("Part. Mol. Cv (J/K/kmol)"); + getPartialMolarVolumes(&data[9][0]); +} + }