Use cppformat in ThermoPhase::report and overrides

This commit is contained in:
Ray Speth 2015-07-16 18:07:55 -04:00
parent 952ed7d39a
commit caf9c6f062
5 changed files with 107 additions and 188 deletions

View file

@ -8,6 +8,7 @@
#define CT_STRINGUTILS_H
#include "ct_defs.h"
#include "cantera/ext/format.h"
#include <string>

View file

@ -461,25 +461,19 @@ void MolalityVPSSTP::initThermoXML(XML_Node& phaseNode, const std::string& id_)
std::string MolalityVPSSTP::report(bool show_thermo, doublereal threshold) const
{
char p[800];
string s = "";
fmt::MemoryWriter b;
try {
if (name() != "") {
sprintf(p, " \n %s:\n", name().c_str());
s += p;
b.write("\n {}:\n", name());
}
sprintf(p, " \n temperature %12.6g K\n", temperature());
s += p;
sprintf(p, " pressure %12.6g Pa\n", pressure());
s += p;
sprintf(p, " density %12.6g kg/m^3\n", density());
s += p;
sprintf(p, " mean mol. weight %12.6g amu\n", meanMolecularWeight());
s += p;
b.write("\n");
b.write(" temperature {:12.6g} K\n", temperature());
b.write(" pressure {:12.6g} Pa\n", pressure());
b.write(" density {:12.6g} kg/m^3\n", density());
b.write(" mean mol. weight {:12.6g} amu\n", meanMolecularWeight());
doublereal phi = electricPotential();
sprintf(p, " potential %12.6g V\n", phi);
s += p;
b.write(" potential {:12.6g} V\n", phi);
vector_fp x(m_kk);
vector_fp molal(m_kk);
@ -497,84 +491,65 @@ std::string MolalityVPSSTP::report(bool show_thermo, doublereal threshold) const
size_t iHp = speciesIndex("H+");
if (iHp != npos) {
double pH = -log(actMolal[iHp]) / log(10.0);
sprintf(p, " pH %12.4g \n", pH);
s += p;
b.write(" pH {:12.4g}\n", pH);
}
if (show_thermo) {
sprintf(p, " \n");
s += p;
sprintf(p, " 1 kg 1 kmol\n");
s += p;
sprintf(p, " ----------- ------------\n");
s += p;
sprintf(p, " enthalpy %12.6g %12.4g J\n",
b.write("\n");
b.write(" 1 kg 1 kmol\n");
b.write(" ----------- ------------\n");
b.write(" enthalpy {:12.6g} {:12.4g} J\n",
enthalpy_mass(), enthalpy_mole());
s += p;
sprintf(p, " internal energy %12.6g %12.4g J\n",
b.write(" internal energy {:12.6g} {:12.4g} J\n",
intEnergy_mass(), intEnergy_mole());
s += p;
sprintf(p, " entropy %12.6g %12.4g J/K\n",
b.write(" entropy {:12.6g} {:12.4g} J/K\n",
entropy_mass(), entropy_mole());
s += p;
sprintf(p, " Gibbs function %12.6g %12.4g J\n",
b.write(" Gibbs function {:12.6g} {:12.4g} J\n",
gibbs_mass(), gibbs_mole());
s += p;
sprintf(p, " heat capacity c_p %12.6g %12.4g J/K\n",
b.write(" heat capacity c_p {:12.6g} {:12.4g} J/K\n",
cp_mass(), cp_mole());
s += p;
try {
sprintf(p, " heat capacity c_v %12.6g %12.4g J/K\n",
b.write(" heat capacity c_v {:12.6g} {:12.4g} J/K\n",
cv_mass(), cv_mole());
s += p;
} catch (CanteraError& e) {
e.save();
sprintf(p, " heat capacity c_v <not implemented> \n");
s += p;
b.write(" heat capacity c_v <not implemented>\n");
}
}
sprintf(p, " \n");
s += p;
b.write("\n");
int nMinor = 0;
doublereal xMinor = 0.0;
if (show_thermo) {
sprintf(p, " X "
b.write(" X "
" Molalities Chem.Pot. ChemPotSS ActCoeffMolal\n");
s += p;
sprintf(p, " "
" (J/kmol) (J/kmol) \n");
s += p;
sprintf(p, " ------------- "
b.write(" "
" (J/kmol) (J/kmol)\n");
b.write(" ------------- "
" ------------ ------------ ------------ ------------\n");
s += p;
for (size_t k = 0; k < m_kk; k++) {
if (x[k] > threshold) {
if (x[k] > SmallNumber) {
sprintf(p, "%18s %12.6g %12.6g %12.6g %12.6g %12.6g\n",
speciesName(k).c_str(), x[k], molal[k], mu[k], muss[k], acMolal[k]);
b.write("{:>18s} {:12.6g} {:12.6g} {:12.6g} {:12.6g} {:12.6g}\n",
speciesName(k), x[k], molal[k], mu[k], muss[k], acMolal[k]);
} else {
sprintf(p, "%18s %12.6g %12.6g N/A %12.6g %12.6g \n",
speciesName(k).c_str(), x[k], molal[k], muss[k], acMolal[k]);
b.write("{:>18s} {:12.6g} {:12.6g} N/A {:12.6g} {:12.6g}\n",
speciesName(k), x[k], molal[k], muss[k], acMolal[k]);
}
s += p;
} else {
nMinor++;
xMinor += x[k];
}
}
} else {
sprintf(p, " X"
b.write(" X"
"Molalities\n");
s += p;
sprintf(p, " -------------"
b.write(" -------------"
" ------------\n");
s += p;
for (size_t k = 0; k < m_kk; k++) {
if (x[k] > threshold) {
sprintf(p, "%18s %12.6g %12.6g\n",
speciesName(k).c_str(), x[k], molal[k]);
s += p;
b.write("{:>18s} {:12.6g} {:12.6g}\n",
speciesName(k), x[k], molal[k]);
} else {
nMinor++;
xMinor += x[k];
@ -582,13 +557,12 @@ std::string MolalityVPSSTP::report(bool show_thermo, doublereal threshold) const
}
}
if (nMinor) {
sprintf(p, " [%+5i minor] %12.6g\n", nMinor, xMinor);
s += p;
b.write(" [{:+5d} minor] {:12.6g}\n", nMinor, xMinor);
}
} catch (CanteraError& err) {
err.save();
}
return s;
return b.str();
}
void MolalityVPSSTP::getCsvReportData(std::vector<std::string>& names,

View file

@ -393,25 +393,19 @@ void MolarityIonicVPSSTP::readXMLBinarySpecies(XML_Node& xmLBinarySpecies)
std::string MolarityIonicVPSSTP::report(bool show_thermo, doublereal threshold) const
{
char p[800];
string s = "";
fmt::MemoryWriter b;
try {
if (name() != "") {
sprintf(p, " \n %s:\n", name().c_str());
s += p;
b.write("\n {}:\n", name().c_str());
}
sprintf(p, " \n temperature %12.6g K\n", temperature());
s += p;
sprintf(p, " pressure %12.6g Pa\n", pressure());
s += p;
sprintf(p, " density %12.6g kg/m^3\n", density());
s += p;
sprintf(p, " mean mol. weight %12.6g amu\n", meanMolecularWeight());
s += p;
b.write("\n");
b.write(" temperature {:12.6g} K\n", temperature());
b.write(" pressure {:12.6g} Pa\n", pressure());
b.write(" density {:12.6g} kg/m^3\n", density());
b.write(" mean mol. weight {:12.6g} amu\n", meanMolecularWeight());
doublereal phi = electricPotential();
sprintf(p, " potential %12.6g V\n", phi);
s += p;
b.write(" potential {:12.6g} V\n", phi);
vector_fp x(m_kk);
vector_fp molal(m_kk);
@ -426,41 +420,31 @@ std::string MolarityIonicVPSSTP::report(bool show_thermo, doublereal threshold)
getActivities(&actMolal[0]);
if (show_thermo) {
sprintf(p, " \n");
s += p;
sprintf(p, " 1 kg 1 kmol\n");
s += p;
sprintf(p, " ----------- ------------\n");
s += p;
sprintf(p, " enthalpy %12.6g %12.4g J\n",
b.write("\n");
b.write(" 1 kg 1 kmol\n");
b.write(" ----------- ------------\n");
b.write(" enthalpy {:12.6g} {:12.4g} J\n",
enthalpy_mass(), enthalpy_mole());
s += p;
sprintf(p, " internal energy %12.6g %12.4g J\n",
b.write(" internal energy {:12.6g} {:12.4g} J\n",
intEnergy_mass(), intEnergy_mole());
s += p;
sprintf(p, " entropy %12.6g %12.4g J/K\n",
b.write(" entropy {:12.6g} {:12.4g} J/K\n",
entropy_mass(), entropy_mole());
s += p;
sprintf(p, " Gibbs function %12.6g %12.4g J\n",
b.write(" Gibbs function {:12.6g} {:12.4g} J\n",
gibbs_mass(), gibbs_mole());
s += p;
sprintf(p, " heat capacity c_p %12.6g %12.4g J/K\n",
b.write(" heat capacity c_p {:12.6g} {:12.4g} J/K\n",
cp_mass(), cp_mole());
s += p;
try {
sprintf(p, " heat capacity c_v %12.6g %12.4g J/K\n",
b.write(" heat capacity c_v {:12.6g} {:12.4g} J/K\n",
cv_mass(), cv_mole());
s += p;
} catch (CanteraError& e) {
e.save();
sprintf(p, " heat capacity c_v <not implemented> \n");
s += p;
b.write(" heat capacity c_v <not implemented>\n");
}
}
} catch (CanteraError& e) {
e.save();
}
return s;
return b.str();
}
}

View file

@ -10,6 +10,7 @@
#include "cantera/tpx/Sub.h"
#include "cantera/tpx/utils.h"
#include "cantera/base/stringUtils.h"
#include <cstdio>
@ -351,61 +352,44 @@ void PureFluidPhase::setState_Psat(doublereal p, doublereal x)
std::string PureFluidPhase::report(bool show_thermo, doublereal threshold) const
{
char p[800];
string s = "";
fmt::MemoryWriter b;
if (name() != "") {
sprintf(p, " \n %s:\n", name().c_str());
s += p;
b.write("\n {}:\n", name().c_str());
}
sprintf(p, " \n temperature %12.6g K\n", temperature());
s += p;
sprintf(p, " pressure %12.6g Pa\n", pressure());
s += p;
sprintf(p, " density %12.6g kg/m^3\n", density());
s += p;
sprintf(p, " mean mol. weight %12.6g amu\n", meanMolecularWeight());
s += p;
sprintf(p, " vapor fraction %12.6g \n", vaporFraction());
s += p;
b.write("\n");
b.write(" temperature {:12.6g} K\n", temperature());
b.write(" pressure {:12.6g} Pa\n", pressure());
b.write(" density {:12.6g} kg/m^3\n", density());
b.write(" mean mol. weight {:12.6g} amu\n", meanMolecularWeight());
b.write(" vapor fraction {:12.6g}\n", vaporFraction());
doublereal phi = electricPotential();
if (phi != 0.0) {
sprintf(p, " potential %12.6g V\n", phi);
s += p;
b.write(" potential {:12.6g} V\n", phi);
}
if (show_thermo) {
sprintf(p, " \n");
s += p;
sprintf(p, " 1 kg 1 kmol\n");
s += p;
sprintf(p, " ----------- ------------\n");
s += p;
sprintf(p, " enthalpy %12.6g %12.4g J\n",
b.write("\n");
b.write(" 1 kg 1 kmol\n");
b.write(" ----------- ------------\n");
b.write(" enthalpy {:12.6g} {:12.4g} J\n",
enthalpy_mass(), enthalpy_mole());
s += p;
sprintf(p, " internal energy %12.6g %12.4g J\n",
b.write(" internal energy {:12.6g} {:12.4g} J\n",
intEnergy_mass(), intEnergy_mole());
s += p;
sprintf(p, " entropy %12.6g %12.4g J/K\n",
b.write(" entropy {:12.6g} {:12.4g} J/K\n",
entropy_mass(), entropy_mole());
s += p;
sprintf(p, " Gibbs function %12.6g %12.4g J\n",
b.write(" Gibbs function {:12.6g} {:12.4g} J\n",
gibbs_mass(), gibbs_mole());
s += p;
sprintf(p, " heat capacity c_p %12.6g %12.4g J/K\n",
b.write(" heat capacity c_p {:12.6g} {:12.4g} J/K\n",
cp_mass(), cp_mole());
s += p;
try {
sprintf(p, " heat capacity c_v %12.6g %12.4g J/K\n",
b.write(" heat capacity c_v {:12.6g} {:12.4g} J/K\n",
cv_mass(), cv_mole());
s += p;
} catch (CanteraError& e) {
e.save();
sprintf(p, " heat capacity c_v <not implemented> \n");
s += p;
b.write(" heat capacity c_v <not implemented>\n");
}
}
return s;
return b.str();
}
}

View file

@ -908,57 +908,41 @@ void ThermoPhase::getdlnActCoeffdlnN_numderiv(const size_t ld, doublereal* const
std::string ThermoPhase::report(bool show_thermo, doublereal threshold) const
{
char p[800];
string s = "";
fmt::MemoryWriter b;
try {
if (name() != "") {
sprintf(p, " \n %s:\n", name().c_str());
s += p;
b.write("\n {}:\n", name());
}
sprintf(p, " \n temperature %12.6g K\n", temperature());
s += p;
sprintf(p, " pressure %12.6g Pa\n", pressure());
s += p;
sprintf(p, " density %12.6g kg/m^3\n", density());
s += p;
sprintf(p, " mean mol. weight %12.6g amu\n", meanMolecularWeight());
s += p;
b.write("\n");
b.write(" temperature {:12.6g} K\n", temperature());
b.write(" pressure {:12.6g} Pa\n", pressure());
b.write(" density {:12.6g} kg/m^3\n", density());
b.write(" mean mol. weight {:12.6g} amu\n", meanMolecularWeight());
doublereal phi = electricPotential();
if (phi != 0.0) {
sprintf(p, " potential %12.6g V\n", phi);
s += p;
b.write(" potential {:12.6g} V\n", phi);
}
if (show_thermo) {
sprintf(p, " \n");
s += p;
sprintf(p, " 1 kg 1 kmol\n");
s += p;
sprintf(p, " ----------- ------------\n");
s += p;
sprintf(p, " enthalpy %12.5g %12.4g J\n",
b.write("\n");
b.write(" 1 kg 1 kmol\n");
b.write(" ----------- ------------\n");
b.write(" enthalpy {:12.5g} {:12.4g} J\n",
enthalpy_mass(), enthalpy_mole());
s += p;
sprintf(p, " internal energy %12.5g %12.4g J\n",
b.write(" internal energy {:12.5g} {:12.4g} J\n",
intEnergy_mass(), intEnergy_mole());
s += p;
sprintf(p, " entropy %12.5g %12.4g J/K\n",
b.write(" entropy {:12.5g} {:12.4g} J/K\n",
entropy_mass(), entropy_mole());
s += p;
sprintf(p, " Gibbs function %12.5g %12.4g J\n",
b.write(" Gibbs function {:12.5g} {:12.4g} J\n",
gibbs_mass(), gibbs_mole());
s += p;
sprintf(p, " heat capacity c_p %12.5g %12.4g J/K\n",
b.write(" heat capacity c_p {:12.5g} {:12.4g} J/K\n",
cp_mass(), cp_mole());
s += p;
try {
sprintf(p, " heat capacity c_v %12.5g %12.4g J/K\n",
b.write(" heat capacity c_v {:12.5g} {:12.4g} J/K\n",
cv_mass(), cv_mole());
s += p;
} catch (CanteraError& err) {
err.save();
sprintf(p, " heat capacity c_v <not implemented> \n");
s += p;
b.write(" heat capacity c_v <not implemented> \n");
}
}
@ -971,23 +955,21 @@ std::string ThermoPhase::report(bool show_thermo, doublereal threshold) const
int nMinor = 0;
doublereal xMinor = 0.0;
doublereal yMinor = 0.0;
b.write("\n");
if (show_thermo) {
sprintf(p, " \n X "
" Y Chem. Pot. / RT \n");
s += p;
sprintf(p, " ------------- "
b.write(" X "
" Y Chem. Pot. / RT\n");
b.write(" ------------- "
"------------ ------------\n");
s += p;
for (size_t k = 0; k < m_kk; k++) {
if (abs(x[k]) >= threshold) {
if (abs(x[k]) > SmallNumber) {
sprintf(p, "%18s %12.6g %12.6g %12.6g\n",
speciesName(k).c_str(), x[k], y[k], mu[k]/RT());
b.write("{:>18s} {:12.6g} {:12.6g} {:12.6g}\n",
speciesName(k), x[k], y[k], mu[k]/RT());
} else {
sprintf(p, "%18s %12.6g %12.6g \n",
speciesName(k).c_str(), x[k], y[k]);
b.write("{:>18s} {:12.6g} {:12.6g}\n",
speciesName(k), x[k], y[k]);
}
s += p;
} else {
nMinor++;
xMinor += x[k];
@ -995,17 +977,12 @@ std::string ThermoPhase::report(bool show_thermo, doublereal threshold) const
}
}
} else {
sprintf(p, " \n X "
" Y\n");
s += p;
sprintf(p, " -------------"
" ------------\n");
s += p;
b.write(" X Y\n");
b.write(" ------------- ------------\n");
for (size_t k = 0; k < m_kk; k++) {
if (abs(x[k]) >= threshold) {
sprintf(p, "%18s %12.6g %12.6g\n",
speciesName(k).c_str(), x[k], y[k]);
s += p;
b.write("{:>18s} {:12.6g} {:12.6g}\n",
speciesName(k), x[k], y[k]);
} else {
nMinor++;
xMinor += x[k];
@ -1014,14 +991,13 @@ std::string ThermoPhase::report(bool show_thermo, doublereal threshold) const
}
}
if (nMinor) {
sprintf(p, " [%+5i minor] %12.6g %12.6g\n",
b.write(" [{:+5d} minor] {:12.6g} {:12.6g}\n",
nMinor, xMinor, yMinor);
s += p;
}
} catch (CanteraError& err) {
err.save();
}
return s;
return b.str();
}
void ThermoPhase::reportCSV(std::ofstream& csvFile) const