Replace usage of fp2str with cppformat and deprecate fp2str

This commit is contained in:
Ray Speth 2016-02-04 14:44:58 -05:00
parent b0230fb146
commit f91afda8cb
19 changed files with 93 additions and 92 deletions

View file

@ -19,6 +19,7 @@ namespace Cantera
/*!
* @param x double to be converted
* @param fmt Format to be used (printf style)
* @deprecated Unused. To be removed after Cantera 2.3. Use fmt::format instead
*/
std::string fp2str(const double x, const std::string& fmt="%g");

View file

@ -35,8 +35,7 @@ void checkFinite(const std::string& name, double* values, size_t N)
std::string message = name + " contains non-finite elements:\n\n";
for (size_t j = 0; j < N; j++) {
if (!std::isfinite(values[j])) {
message += name + "[" + int2str(j) + "] = " +
fp2str(values[j]) + "\n";
message += fmt::format("{}[{}] = {}\n", name, j, values[j]);
}
}
throw CanteraError("checkFinite", message);

View file

@ -55,7 +55,7 @@ void addFloatArray(XML_Node& node, const std::string& title, const size_t n,
{
std::string v = "";
for (size_t i = 0; i < n; i++) {
v += fp2str(vals[i],FP_Format);
v += fmt::sprintf(FP_Format, vals[i]);
if (i == n-1) {
v += "\n";
} else if (i > 0 && (i+1) % 3 == 0) {
@ -88,7 +88,7 @@ void addNamedFloatArray(XML_Node& node, const std::string& name, const size_t n,
{
std::string v = "";
for (size_t i = 0; i < n; i++) {
v += fp2str(vals[i],FP_Format);
v += fmt::sprintf(FP_Format, vals[i]);
if (i == n-1) {
v += "\n";
} else if (i > 0 && (i+1) % 3 == 0) {

View file

@ -28,6 +28,8 @@ namespace Cantera
std::string fp2str(const double x, const std::string& fmt)
{
warn_deprecated("fp2str", "Unused. To be removed after Cantera 2.3. "
"Use fmt::format instead.");
char buf[64];
int n = SNPRINTF(buf, 63, fmt.c_str(), x);
if (n > 0) {

View file

@ -446,7 +446,7 @@ void XML_Node::addValue(const std::string& val)
void XML_Node::addValue(const doublereal val, const std::string& fmt)
{
m_value = stripws(fp2str(val, fmt));
m_value = stripws(fmt::sprintf(fmt, val));
}
std::string XML_Node::value() const
@ -482,7 +482,7 @@ void XML_Node::addAttribute(const std::string& attrib, const std::string& value)
void XML_Node::addAttribute(const std::string& attrib,
const doublereal vvalue, const std::string& fmt)
{
m_attribs[attrib] = fp2str(vvalue, fmt);
m_attribs[attrib] = fmt::sprintf(fmt, vvalue);
}
void XML_Node::addAttribute(const std::string& aattrib, const int vvalue)

View file

@ -141,11 +141,11 @@ void Func1::setParent(Func1* p)
string Sin1::write(const string& arg) const
{
string c = "";
if (m_c != 1.0) {
c = fp2str(m_c);
if (m_c == 1.0) {
return fmt::format("\\sin({})", arg);
} else {
return fmt::format("\\sin({}{})", m_c, arg);
}
return "\\sin(" + c + arg + ")";
}
Func1& Sin1::derivative() const
@ -165,11 +165,11 @@ Func1& Cos1::derivative() const
std::string Cos1::write(const std::string& arg) const
{
string c = "";
if (m_c != 1.0) {
c = fp2str(m_c);
if (m_c == 1.0) {
return fmt::format("\\cos({})", arg);
} else {
return fmt::format("\\cos({}{})", m_c, arg);
}
return "\\cos("+c+arg+")";
}
/**************************************************************************/
@ -186,11 +186,11 @@ Func1& Exp1::derivative() const
std::string Exp1::write(const std::string& arg) const
{
string c = "";
if (m_c != 1.0) {
c = fp2str(m_c);
if (m_c == 1.0) {
return fmt::format("\\exp({})", arg);
} else {
return fmt::format("\\exp({}{})", m_c, arg);
}
return "\\exp("+c+arg+")";
}
/******************************************************************************/
@ -211,7 +211,7 @@ Func1& Pow1::derivative() const
string Func1::write(const std::string& arg) const
{
return "<unknown " + int2str(ID()) + ">("+arg+")";
return fmt::format("<unknown {}>({})", ID(), arg);
}
string Pow1::write(const std::string& arg) const
@ -224,8 +224,7 @@ string Pow1::write(const std::string& arg) const
return "\\frac{1}{\\sqrt{" + arg + "}}";
}
if (m_c != 1.0) {
c = fp2str(m_c);
return "\\left("+arg+"\\right)^{"+c+"}";
return fmt::format("\\left({}\\right)^{{{}}}", arg, m_c);
} else {
return arg;
}
@ -233,7 +232,7 @@ string Pow1::write(const std::string& arg) const
string Const1::write(const std::string& arg) const
{
return fp2str(m_c);
return fmt::format("{}", m_c);
}
string Ratio1::write(const std::string& arg) const
@ -299,7 +298,7 @@ string TimesConstant1::write(const std::string& arg) const
if (n >= '0' && n <= '9') {
s = "\\left(" + s + "\\right)";
}
return fp2str(m_c) + s;
return fmt::format("{}{}", m_c, s);
}
string PlusConstant1::write(const std::string& arg) const
@ -307,7 +306,7 @@ string PlusConstant1::write(const std::string& arg) const
if (m_c == 0.0) {
return m_f1->write(arg);
}
return m_f1->write(arg) + " + " + fp2str(m_c);
return fmt::format("{} + {}", m_f1->write(arg), m_c);
}
doublereal Func1::isProportional(TimesConstant1& other)

View file

@ -399,7 +399,7 @@ int RootFind::solve(doublereal xmin, doublereal xmax, int itmax, doublereal& fun
// If the slope can't be trusted using a different strategy for picking
// the next point
if (useNextStrat) {
rfT.reasoning += "Using DeltaXnorm, " + fp2str(DeltaXnorm_) + " and FuncIsGenerallyIncreasing hints. ";
rfT.reasoning += fmt::format("Using DeltaXnorm, {} and FuncIsGenerallyIncreasing hints. ", DeltaXnorm_);
if (f2 < 0.0) {
if (FuncIsGenerallyIncreasing_) {
if (slopePointingToHigher) {
@ -563,8 +563,8 @@ int RootFind::solve(doublereal xmin, doublereal xmax, int itmax, doublereal& fun
sgn = -1.0;
}
deltaXnew = 1.2 * delXMeaningful(xnew) * sgn;
rfT.reasoning += "Enforcing minimum stepsize from " + fp2str(xnew - x2) +
" to " + fp2str(deltaXnew);
rfT.reasoning += fmt::format("Enforcing minimum stepsize from {} to {}",
xnew - x2, deltaXnew);
xnew = x2 + deltaXnew;
}
@ -573,7 +573,7 @@ int RootFind::solve(doublereal xmin, doublereal xmax, int itmax, doublereal& fun
topBump++;
if (topBump < 3) {
xnew = x2 + (xmax - x2) / 2.0;
rfT.reasoning += ("xval reduced to " + fp2str(xnew) + " because predicted xnew was above max value of " + fp2str(xmax));
rfT.reasoning += fmt::format("xval reduced to {} because predicted xnew was above max value of {}", xnew, xmax);
} else {
if (x2 == xmax || x1 == xmax) {
// we are here when we are bumping against the top limit.
@ -581,10 +581,10 @@ int RootFind::solve(doublereal xmin, doublereal xmax, int itmax, doublereal& fun
retn = ROOTFIND_SOLNHIGHERTHANXMAX;
*xbest = xnew;
rfT.slope = slope;
rfT.reasoning += "Giving up because we're at xmax and xnew point higher: " + fp2str(xnew);
rfT.reasoning += fmt::format("Giving up because we're at xmax and xnew point higher: {}", xnew);
goto done;
} else {
rfT.reasoning += "xval reduced from " + fp2str(xnew) + " to the max value, " + fp2str(xmax);
rfT.reasoning += fmt::format("xval reduced from {} to the max value, {}", xnew, xmax);
xnew = xmax;
}
}
@ -595,8 +595,8 @@ int RootFind::solve(doublereal xmin, doublereal xmax, int itmax, doublereal& fun
if (xnew < xmin) {
bottomBump++;
if (bottomBump < 3) {
rfT.reasoning += ("xnew increased from " + fp2str(xnew) +" to " + fp2str(x2 - (x2 - xmin) / 2.0) +
" because above min value of " + fp2str(xmin));
rfT.reasoning += fmt::format("xnew increased from {} to {} because above min value of {}",
xnew, x2 - (x2 - xmin) / 2.0, xmin);
xnew = x2 - (x2 - xmin) / 2.0;
} else {
if (x2 == xmin || x1 == xmin) {
@ -605,10 +605,10 @@ int RootFind::solve(doublereal xmin, doublereal xmax, int itmax, doublereal& fun
retn = ROOTFIND_SOLNLOWERTHANXMIN;
*xbest = xnew;
rfT.slope = slope;
rfT.reasoning = "Giving up because we're already at xmin and xnew points lower: " + fp2str(xnew);
rfT.reasoning = fmt::format("Giving up because we're already at xmin and xnew points lower: {}", xnew);
goto done;
} else {
rfT.reasoning += "xval increased from " + fp2str(xnew) + " to the min value, " + fp2str(xmin);
rfT.reasoning += fmt::format("xval increased from {} to the min value, {}", xnew, xmin);
xnew = xmin;
}
}
@ -951,12 +951,12 @@ done:
if (printLvl >= 1) {
writelogf("RootFind ERROR: Soln probably lies higher than xmax, %g: best guess = %g\n", xmax, *xbest);
}
rfT.reasoning += "Soln probably lies higher than xmax, " + fp2str(xmax) + ": best guess = " + fp2str(*xbest);
rfT.reasoning += fmt::format("Soln probably lies higher than xmax, {}: best guess = {}", xmax, *xbest);
} else if (retn == ROOTFIND_SOLNLOWERTHANXMIN) {
if (printLvl >= 1) {
writelogf("RootFind ERROR: Soln probably lies lower than xmin, %g: best guess = %g\n", xmin, *xbest);
}
rfT.reasoning += "Soln probably lies lower than xmin, " + fp2str(xmin) + ": best guess = " + fp2str(*xbest);
rfT.reasoning += fmt::format("Soln probably lies lower than xmin, {}: best guess = {}", xmin, *xbest);
} else {
retn = ROOTFIND_FAILEDCONVERGENCE;
if (printLvl >= 1) {

View file

@ -366,7 +366,9 @@ int Sim1D::refine(int loglevel)
}
}
} else {
debuglog("refine: discarding point at "+fp2str(d.grid(m))+"\n", loglevel);
if (loglevel > 0) {
writelog("refine: discarding point at {}\n", d.grid(m));
}
}
}
dsize.push_back(znew.size() - nstart);

View file

@ -71,8 +71,7 @@ FixedChemPotSSTP::FixedChemPotSSTP(const std::string& Ename, doublereal val) :
ss.addAttribute("Tmin", "100.");
ss.addChild("t0", "298.15");
ss.addChild("cp0", "0.0");
std::string sval = fp2str(val);
ss.addChild("h", sval);
ss.addChild("h", fmt::format("{}", val));
ss.addChild("s", "0.0");
saveSpeciesData(0, &s);
}

View file

@ -246,8 +246,8 @@ double CarbonDioxide::Psat()
{
double log, sum=0,P;
if ((T < Tmn) || (T > Tc)) {
throw TPX_Error("CarbonDixoide::Psat",
"Temperature out of range. T = " + fp2str(T));
throw CanteraError("CarbonDixoide::Psat",
"Temperature out of range. T = {}", T);
}
for (int i=1; i<=8; i++) {
sum += F[i-1] * pow((T/Tp -1),double(i-1));
@ -262,8 +262,8 @@ double CarbonDioxide::ldens()
{
double xx=1-(T/Tc), sum=0;
if ((T < Tmn) || (T > Tc)) {
throw TPX_Error("CarbonDixoide::ldens",
"Temperature out of range. T = " + fp2str(T));
throw CanteraError("CarbonDixoide::ldens",
"Temperature out of range. T = {}", T);
}
for (int i=1; i<=6; i++) {
sum+=D[i-1]*pow(xx,double(i-1)/3.0);

View file

@ -153,8 +153,8 @@ double HFC134a::Pp()
double HFC134a::Psat()
{
if ((T < Tmn) || (T > Tc)) {
throw TPX_Error("HFC134a::Psat",
"Temperature out of range. T = " + fp2str(T));
throw CanteraError("HFC134a::Psat",
"Temperature out of range. T = {}", T);
}
double x1 = T/Tc;
double x2 = 1.0 - x1;
@ -166,8 +166,8 @@ double HFC134a::Psat()
double HFC134a::ldens()
{
if ((T < Tmn) || (T > Tc)) {
throw TPX_Error("HFC134a::ldens",
"Temperature out of range. T = " + fp2str(T));
throw CanteraError("HFC134a::ldens",
"Temperature out of range. T = {}", T);
}
double x1 = T/Tc;
double x2 = 1.0 - x1;

View file

@ -204,8 +204,8 @@ double Heptane::Psat()
{
double log, sum=0;
if ((T < Tmn) || (T > Tc)) {
throw TPX_Error("Heptane::Psat",
"Temperature out of range. T = " + fp2str(T));
throw CanteraError("Heptane::Psat",
"Temperature out of range. T = {}", T);
}
for (int i=1; i<=8; i++) {
sum += F[i-1] * pow((T/Tp -1),double(i-1));
@ -219,8 +219,8 @@ double Heptane::ldens()
{
double xx=1-(T/Tc), sum=0;
if ((T < Tmn) || (T > Tc)) {
throw TPX_Error("Heptane::ldens",
"Temperature out of range. T = " + fp2str(T));
throw CanteraError("Heptane::ldens",
"Temperature out of range. T = {}", T);
}
for (int i=1; i<=6; i++) {
sum+=D[i-1]*pow(xx,double(i-1)/3.0);

View file

@ -216,8 +216,8 @@ double hydrogen::Pp()
double hydrogen::ldens()
{
if ((T < Tmn) || (T > Tc)) {
throw TPX_Error("hydrogen::ldens",
"Temperature out of range. T = " + fp2str(T));
throw CanteraError("hydrogen::ldens",
"Temperature out of range. T = {}", T);
}
double x=1-T/Tc;
double sum;
@ -233,8 +233,8 @@ double hydrogen::Psat()
double x = (1.0 - Tt/T)/(1.0 - Tt/Tc);
double result;
if ((T < Tmn) || (T > Tc)) {
throw TPX_Error("hydrogen::Psat",
"Temperature out of range. T = " + fp2str(T));
throw CanteraError("hydrogen::Psat",
"Temperature out of range. T = {}", T);
}
result = Fhydro[0]*x + Fhydro[1]*x*x + Fhydro[2]*x*x*x +
Fhydro[3]*x*pow(1-x, alpha);

View file

@ -187,8 +187,8 @@ double methane::Psat()
double x = (1.0 - Tt/T)/(1.0 - Tt/Tc);
double result;
if ((T < Tmn) || (T > Tc)) {
throw TPX_Error("methane::Psat",
"Temperature out of range. T = " + fp2str(T));
throw CanteraError("methane::Psat",
"Temperature out of range. T = {}", T);
}
result = Fmeth[0]*x + Fmeth[1]*x*x + Fmeth[2]*x*x*x +
Fmeth[3]*x*pow(1-x, alpha);
@ -201,8 +201,8 @@ double methane::ldens()
double sum;
double w;
if ((T < Tmn) || (T > Tc)) {
throw TPX_Error("methane::ldens",
"Temperature out of range. T = " + fp2str(T));
throw CanteraError("methane::ldens",
"Temperature out of range. T = {}", T);
}
w = (Tc - T)/(Tc - Tt);
sum = Dmeth[0]*(1.0 - pow(w, 2.0/3.0)) + Dmeth[1]*(1.0 - pow(w, 4.0/3.0))

View file

@ -197,8 +197,8 @@ double nitrogen::Psat()
double lnp;
int i;
if ((T < Tmn) || (T > Tc)) {
throw TPX_Error("nitrogen::Psat",
"Temperature out of range. T = " + fp2str(T));
throw CanteraError("nitrogen::Psat",
"Temperature out of range. T = {}", T);
}
for (i=0, lnp=0; i<=7; i++) {
if (i==3) {
@ -215,8 +215,8 @@ double nitrogen::ldens()
{
double xx=1-T/Tc, sum=0;
if ((T < Tmn) || (T > Tc)) {
throw TPX_Error("nitrogen::ldens",
"Temperature out of range. T = " + fp2str(T));
throw CanteraError("nitrogen::ldens",
"Temperature out of range. T = {}", T);
}
for (int i=0; i<=5; i++) {
sum+=Dnn[i]*pow(xx,double(i)/3.0);

View file

@ -190,8 +190,8 @@ double oxygen::Psat()
double lnp;
int i;
if ((T < Tmn) || (T > Tc)) {
throw TPX_Error("oxygen::Psat",
"Temperature out of range. T = " + fp2str(T));
throw CanteraError("oxygen::Psat",
"Temperature out of range. T = {}", T);
}
for (i=0, lnp=0; i<=7; i++) {
if (i==3) {
@ -208,8 +208,8 @@ double oxygen::ldens()
{
double xx=1-T/Tc, sum=0;
if ((T < Tmn) || (T > Tc)) {
throw TPX_Error("oxygen::ldens",
"Temperature out of range. T = " + fp2str(T));
throw CanteraError("oxygen::ldens",
"Temperature out of range. T = {}", T);
}
for (int i=0; i<=5; i++) {
sum+=Doxy[i]*pow(xx,double(i)/3.0);

View file

@ -211,8 +211,8 @@ void Substance::Set(PropertyPair::type XY, double x0, double y0)
case PropertyPair::PX:
temp = Tsat(x0);
if (y0 > 1.0 || y0 < 0.0) {
throw TPX_Error("Substance::Set",
"Invalid vapor fraction, " + fp2str(y0));
throw CanteraError("Substance::Set",
"Invalid vapor fraction, {}", y0);
} else if (temp >= Tcrit()) {
throw TPX_Error("Substance::Set",
"Can't set vapor fraction above the critical point");
@ -224,8 +224,8 @@ void Substance::Set(PropertyPair::type XY, double x0, double y0)
break;
case PropertyPair::TX:
if (y0 > 1.0 || y0 < 0.0) {
throw TPX_Error("Substance::Set",
"Invalid vapor fraction, " + fp2str(y0));
throw CanteraError("Substance::Set",
"Invalid vapor fraction, {}", y0);
} else if (x0 >= Tcrit()) {
throw TPX_Error("Substance::Set",
"Can't set vapor fraction above the critical point");
@ -247,7 +247,7 @@ void Substance::set_Rho(double r0)
if (r0 > 0.0) {
Rho = r0;
} else {
throw TPX_Error("Substance::set_Rho", "Invalid density: " + fp2str(r0));
throw CanteraError("Substance::set_Rho", "Invalid density: {}", r0);
}
}
@ -256,7 +256,7 @@ void Substance::set_T(double t0)
if ((t0 >= Tmin()) && (t0 <= Tmax())) {
T = t0;
} else {
throw TPX_Error("Substance::set_T", "illegal temperature: " + fp2str(t0));
throw CanteraError("Substance::set_T", "illegal temperature: {}", t0);
}
}
@ -265,16 +265,16 @@ void Substance::set_v(double v0)
if (v0 > 0) {
Rho = 1.0/v0;
} else {
throw TPX_Error("Substance::set_v",
"negative specific volume: "+fp2str(v0));
throw CanteraError("Substance::set_v",
"negative specific volume: {}", v0);
}
}
double Substance::Ps()
{
if (T < Tmin() || T > Tcrit()) {
throw TPX_Error("Substance::Ps",
"illegal temperature value "+fp2str(T));
throw CanteraError("Substance::Ps",
"illegal temperature value {}", T);
}
update_sat();
return Pst;
@ -334,8 +334,8 @@ void Substance::update_sat()
}
}
if (Rhf <= Rhv) {
throw TPX_Error("Substance::update_sat",
"wrong root found for sat. liquid or vapor at P = "+fp2str(pp));
throw CanteraError("Substance::update_sat",
"wrong root found for sat. liquid or vapor at P = {}", pp);
}
if (i >= 20) {
@ -500,13 +500,12 @@ void Substance::set_xy(propertyFlag::type ifx, propertyFlag::type ify,
Set(PropertyPair::TV, t_here, v_here);
LoopCount++;
if (LoopCount > 200) {
std::string msg = "No convergence. " +
propertySymbols[ifx] + " = " + fp2str(X) + ", " +
propertySymbols[ify] + " = " + fp2str(Y);
std::string msg = fmt::format("No convergence. {} = {}, {} = {}",
propertySymbols[ifx], X, propertySymbols[ify], Y);
if (t_here == Tmin()) {
msg += "\nAt temperature limit (Tmin = " + fp2str(Tmin()) + ")";
msg += fmt::format("\nAt temperature limit (Tmin = {})", Tmin());
} else if (t_here == Tmax()) {
msg += "\nAt temperature limit (Tmax = " + fp2str(Tmax()) + ")";
msg += fmt::format("\nAt temperature limit (Tmax = {})", Tmax());
}
throw TPX_Error("Substance::set_xy", msg);
}
@ -639,9 +638,9 @@ void Substance::set_TPp(double Temp, double Pressure)
LoopCount++;
if (LoopCount > 100) {
Set(PropertyPair::TV, Temp, v_save);
throw TPX_Error("Substance::set_TPp",string("no convergence for ")
+"P* = "+fp2str(Pressure/Pcrit())+". V* = "
+fp2str(v_save/Vcrit()));
throw CanteraError("Substance::set_TPp",
"no convergence for P* = {}, V* = {}",
Pressure/Pcrit(), v_save/Vcrit());
}
}
Set(PropertyPair::TV, Temp,v_here);

View file

@ -158,8 +158,8 @@ double water::Psat()
{
double log, sum=0;
if ((T < Tmn) || (T > Tc)) {
throw TPX_Error("water::Psat",
"Temperature out of range. T = " + fp2str(T));
throw CanteraError("water::Psat",
"Temperature out of range. T = {}", T);
}
for (int i=1; i<=8; i++) {
sum += F[i-1]*pow(aww*(T-Tp),double(i-1)); // DGG mod
@ -173,8 +173,8 @@ double water::ldens()
double sum=0;
int i;
if ((T < Tmn) || (T >= Tc)) {
throw TPX_Error("water::ldens",
"Temperature out of range. T = " + fp2str(T));
throw CanteraError("water::ldens",
"Temperature out of range. T = {}", T);
}
for (i=0; i<8; i++) {
sum+=D[i]*pow(1.0 - T/Tc, double(i+1)/3.0);

View file

@ -59,7 +59,7 @@ void writeRxnPathDiagram(double time, ReactionPathBuilder& b,
d.arrow_width = -2.0;
// title
d.title = "time = "+fp2str(time)+" (s)";
d.title = fmt::format("time = {} (s)", time);
// build the diagram following elemental nitrogen
b.build(gas, "N", logfile, d);