From f44b5fed5741f75eea060e698dccc3a5a2354563 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Thu, 4 Feb 2016 19:11:18 -0500 Subject: [PATCH] Replace usage of int2str with cppformat and deprecate int2str --- include/cantera/base/stringUtils.h | 2 ++ include/cantera/oneD/Domain1D.h | 4 ++-- samples/cxx/bvp/BoundaryValueProblem.h | 2 +- src/base/stringUtils.cpp | 4 ++++ src/base/xml.cpp | 8 ++++---- src/numerics/CVodesIntegrator.cpp | 24 ++++++++++++++++-------- src/numerics/DenseMatrix.cpp | 16 ++++++++-------- src/numerics/IDA_Solver.cpp | 12 ++++++++---- src/numerics/SquareMatrix.cpp | 14 +++++++------- src/oneD/Domain1D.cpp | 8 ++++---- src/oneD/MultiNewton.cpp | 4 +++- src/oneD/Sim1D.cpp | 4 +++- src/oneD/StFlow.cpp | 4 +++- src/oneD/refine.cpp | 4 ++-- src/thermo/LatticeSolidPhase.cpp | 2 +- src/thermo/VPSSMgrFactory.cpp | 3 ++- src/tpx/Sub.cpp | 8 ++++---- src/transport/DustyGasTransport.cpp | 2 +- 18 files changed, 75 insertions(+), 50 deletions(-) diff --git a/include/cantera/base/stringUtils.h b/include/cantera/base/stringUtils.h index b7e6e2749..e086f8247 100644 --- a/include/cantera/base/stringUtils.h +++ b/include/cantera/base/stringUtils.h @@ -27,12 +27,14 @@ std::string fp2str(const double x, const std::string& fmt="%g"); /*! * @param n int to be converted * @param fmt format converter for an int int the printf command + * @deprecated Unused. To be removed after Cantera 2.3. Use fmt::format instead */ std::string int2str(const int n, const std::string& fmt="%d"); //! Convert an unsigned integer to a string /*! * @param n int to be converted + * @deprecated Unused. To be removed after Cantera 2.3. Use fmt::format instead */ std::string int2str(const size_t n); diff --git a/include/cantera/oneD/Domain1D.h b/include/cantera/oneD/Domain1D.h index e1738bc30..5dd93e06f 100644 --- a/include/cantera/oneD/Domain1D.h +++ b/include/cantera/oneD/Domain1D.h @@ -203,7 +203,7 @@ public: if (m_name[n] != "") { return m_name[n]; } else { - return "component " + int2str(n); + return fmt::format("component {}", n); } } @@ -480,7 +480,7 @@ public: if (m_id != "") { return m_id; } else { - return std::string("domain ") + int2str(m_index); + return fmt::format("domain {}", m_index); } } diff --git a/samples/cxx/bvp/BoundaryValueProblem.h b/samples/cxx/bvp/BoundaryValueProblem.h index 26e49f05f..5b4a74ebc 100644 --- a/samples/cxx/bvp/BoundaryValueProblem.h +++ b/samples/cxx/bvp/BoundaryValueProblem.h @@ -130,7 +130,7 @@ public: m_refiner->setActive(n, c.refine); // set a default name if one has not been entered if (c.name == "") { - c.name = "Component "+Cantera::int2str(n); + c.name = fmt::format("Component {}", n); } setComponentName(n, c.name); } diff --git a/src/base/stringUtils.cpp b/src/base/stringUtils.cpp index 7b2a2314f..3939a7011 100644 --- a/src/base/stringUtils.cpp +++ b/src/base/stringUtils.cpp @@ -41,6 +41,8 @@ std::string fp2str(const double x, const std::string& fmt) std::string int2str(const int n, const std::string& fmt) { + warn_deprecated("int2str", "Unused. To be removed after Cantera 2.3. " + "Use fmt::format instead."); char buf[30]; int m = SNPRINTF(buf, 30, fmt.c_str(), n); if (m > 0) { @@ -52,6 +54,8 @@ std::string int2str(const int n, const std::string& fmt) std::string int2str(const size_t n) { + warn_deprecated("int2str", "Unused. To be removed after Cantera 2.3. " + "Use fmt::format instead."); std::stringstream ss; ss << n; return ss.str(); diff --git a/src/base/xml.cpp b/src/base/xml.cpp index af0021059..c0a2025f4 100644 --- a/src/base/xml.cpp +++ b/src/base/xml.cpp @@ -34,7 +34,7 @@ protected: m_line(line), m_msg("Error in XML file") { if (line > 0) { - m_msg += " at line " + int2str(line+1); + m_msg += fmt::format(" at line {}", line+1); } m_msg += ".\n"; } @@ -487,12 +487,12 @@ void XML_Node::addAttribute(const std::string& attrib, void XML_Node::addAttribute(const std::string& aattrib, const int vvalue) { - m_attribs[aattrib] = int2str(vvalue); + m_attribs[aattrib] = fmt::format("{}", vvalue); } void XML_Node::addAttribute(const std::string& aattrib, const size_t vvalue) { - m_attribs[aattrib] = int2str(vvalue); + m_attribs[aattrib] = fmt::format("{}", vvalue); } std::string XML_Node::operator[](const std::string& attr) const @@ -622,7 +622,7 @@ XML_Node* XML_Node::findNameIDIndex(const std::string& nameTarget, XML_Node* scResult = 0; std::string idattrib = id(); std::string ii = attrib("index"); - std::string index_s = int2str(index_i); + std::string index_s = fmt::format("{}", index_i); int iMax = -1000000; if (name() == nameTarget && (idTarget == "" || idTarget == idattrib) && index_s == ii) { return const_cast(this); diff --git a/src/numerics/CVodesIntegrator.cpp b/src/numerics/CVodesIntegrator.cpp index 192dc5835..53c8d58a1 100644 --- a/src/numerics/CVodesIntegrator.cpp +++ b/src/numerics/CVodesIntegrator.cpp @@ -341,7 +341,8 @@ void CVodesIntegrator::reinitialize(double t0, FuncEval& func) int result; result = CVodeReInit(m_cvode_mem, m_t0, m_y); if (result != CV_SUCCESS) { - throw CVodesErr("CVodeReInit failed. result = "+int2str(result)); + throw CanteraError("CVodesIntegrator::reinitialize", + "CVodeReInit failed. result = {}", result); } applyOptions(); } @@ -393,8 +394,10 @@ void CVodesIntegrator::integrate(double tout) { int flag = CVode(m_cvode_mem, tout, m_y, &m_time, CV_NORMAL); if (flag != CV_SUCCESS) { - throw CVodesErr("CVodes error encountered. Error code: " + int2str(flag) + "\n" + m_error_message + - "\nComponents with largest weighted error estimates:\n" + getErrorInfo(10)); + throw CanteraError("CVodesIntegrator::integrate", + "CVodes error encountered. Error code: {}\n{}\n" + "Components with largest weighted error estimates:\n{}", + flag, m_error_message, getErrorInfo(10)); } m_sens_ok = false; } @@ -403,8 +406,10 @@ double CVodesIntegrator::step(double tout) { int flag = CVode(m_cvode_mem, tout, m_y, &m_time, CV_ONE_STEP); if (flag != CV_SUCCESS) { - throw CVodesErr("CVodes error encountered. Error code: " + int2str(flag) + "\n" + m_error_message + - "\nComponents with largest weighted error estimates:\n" + getErrorInfo(10)); + throw CanteraError("CVodesIntegrator::step", + "CVodes error encountered. Error code: {}\n{}\n" + "Components with largest weighted error estimates:\n{}", + flag, m_error_message, getErrorInfo(10)); } m_sens_ok = false; @@ -427,16 +432,19 @@ double CVodesIntegrator::sensitivity(size_t k, size_t p) if (!m_sens_ok && m_np) { int flag = CVodeGetSens(m_cvode_mem, &m_time, m_yS); if (flag != CV_SUCCESS) { - throw CVodesErr("CVodeGetSens failed. Error code: " + int2str(flag)); + throw CanteraError("CVodesIntegrator::sensitivity", + "CVodeGetSens failed. Error code: {}", flag); } m_sens_ok = true; } if (k >= m_neq) { - throw CVodesErr("sensitivity: k out of range ("+int2str(p)+")"); + throw CanteraError("CVodesIntegrator::sensitivity", + "sensitivity: k out of range ({})", k); } if (p >= m_np) { - throw CVodesErr("sensitivity: p out of range ("+int2str(p)+")"); + throw CanteraError("CVodesIntegrator::sensitivity", + "sensitivity: p out of range ({})", p); } return NV_Ith_S(m_yS[p],k); } diff --git a/src/numerics/DenseMatrix.cpp b/src/numerics/DenseMatrix.cpp index 430aa72b7..7d4bbd668 100644 --- a/src/numerics/DenseMatrix.cpp +++ b/src/numerics/DenseMatrix.cpp @@ -143,10 +143,10 @@ int solve(DenseMatrix& A, double* b, size_t nrhs, size_t ldb) "it is used to solve a system of equations.\n", info); } if (!A.m_useReturnErrorCode) { - throw CELapackError("solve(DenseMatrix& A, double* b)", - "DGETRF returned INFO = "+int2str(info) + ". U(i,i) is exactly zero. The factorization has" + throw CanteraError("solve(DenseMatrix& A, double* b)", + "DGETRF returned INFO = {}. U(i,i) is exactly zero. The factorization has" " been completed, but the factor U is exactly singular, and division by zero will occur if " - "it is used to solve a system of equations."); + "it is used to solve a system of equations.", info); } return info; } else if (info < 0) { @@ -154,8 +154,8 @@ int solve(DenseMatrix& A, double* b, size_t nrhs, size_t ldb) writelogf("solve(DenseMatrix& A, double* b): DGETRF returned INFO = %d. The argument i has an illegal value\n", info); } - throw CELapackError("solve(DenseMatrix& A, double* b)", - "DGETRF returned INFO = "+int2str(info) + ". The argument i has an illegal value"); + throw CanteraError("solve(DenseMatrix& A, double* b)", + "DGETRF returned INFO = {}. The argument i has an illegal value", info); } if (ldb == 0) { @@ -168,7 +168,7 @@ int solve(DenseMatrix& A, double* b, size_t nrhs, size_t ldb) writelogf("solve(DenseMatrix& A, double* b): DGETRS returned INFO = %d\n", info); } if (info < 0 || !A.m_useReturnErrorCode) { - throw CELapackError("solve(DenseMatrix& A, double* b)", "DGETRS returned INFO = "+int2str(info)); + throw CanteraError("solve(DenseMatrix& A, double* b)", "DGETRS returned INFO = {}", info); } } return info; @@ -204,7 +204,7 @@ int invert(DenseMatrix& A, size_t nn) writelogf("invert(DenseMatrix& A, int nn): DGETRS returned INFO = %d\n", info); } if (! A.m_useReturnErrorCode) { - throw CELapackError("invert(DenseMatrix& A, int nn)", "DGETRS returned INFO = "+int2str(info)); + throw CanteraError("invert(DenseMatrix& A, int nn)", "DGETRS returned INFO = {}", info); } return info; } @@ -218,7 +218,7 @@ int invert(DenseMatrix& A, size_t nn) writelogf("invert(DenseMatrix& A, int nn): DGETRS returned INFO = %d\n", info); } if (! A.m_useReturnErrorCode) { - throw CELapackError("invert(DenseMatrix& A, int nn)", "DGETRI returned INFO="+int2str(info)); + throw CanteraError("invert(DenseMatrix& A, int nn)", "DGETRI returned INFO={}", info); } } return info; diff --git a/src/numerics/IDA_Solver.cpp b/src/numerics/IDA_Solver.cpp index cf3a760bd..19217d86b 100644 --- a/src/numerics/IDA_Solver.cpp +++ b/src/numerics/IDA_Solver.cpp @@ -465,12 +465,14 @@ void IDA_Solver::correctInitial_Y_given_Yp(doublereal* y, doublereal* yp, double int flag = IDACalcIC(m_ida_mem, icopt, tout1); if (flag != IDA_SUCCESS) { - throw IDA_Err("IDACalcIC failed: error = " + int2str(flag)); + throw CanteraError("IDA_Solver::correctInitial_Y_given_Yp", + "IDACalcIC failed: error = {}", flag); } flag = IDAGetConsistentIC(m_ida_mem, m_y, m_ydot); if (flag != IDA_SUCCESS) { - throw IDA_Err("IDAGetSolution failed: error = " + int2str(flag)); + throw CanteraError("IDA_Solver::correctInitial_Y_given_Yp", + "IDAGetSolution failed: error = {}", flag); } doublereal* yy = NV_DATA_S(m_y); doublereal* yyp = NV_DATA_S(m_ydot); @@ -495,12 +497,14 @@ void IDA_Solver::correctInitial_YaYp_given_Yd(doublereal* y, doublereal* yp, dou int flag = IDACalcIC(m_ida_mem, icopt, tout1); if (flag != IDA_SUCCESS) { - throw IDA_Err("IDACalcIC failed: error = " + int2str(flag)); + throw CanteraError("IDA_Solver::correctInitial_YaYp_given_Yd", + "IDACalcIC failed: error = {}", flag); } flag = IDAGetConsistentIC(m_ida_mem, m_y, m_ydot); if (flag != IDA_SUCCESS) { - throw IDA_Err("IDAGetSolution failed: error = " + int2str(flag)); + throw CanteraError("IDA_Solver::correctInitial_YaYp_given_Yd", + "IDAGetSolution failed: error = {}", flag); } doublereal* yy = NV_DATA_S(m_y); doublereal* yyp = NV_DATA_S(m_ydot); diff --git a/src/numerics/SquareMatrix.cpp b/src/numerics/SquareMatrix.cpp index 58c00e537..8b94d2e69 100644 --- a/src/numerics/SquareMatrix.cpp +++ b/src/numerics/SquareMatrix.cpp @@ -78,7 +78,7 @@ int SquareMatrix::solve(doublereal* b, size_t nrhs, size_t ldb) writelogf("SquareMatrix::solve(): DGETRS returned INFO = %d\n", info); } if (! m_useReturnErrorCode) { - throw CELapackError("SquareMatrix::solve()", "DGETRS returned INFO = " + int2str(info)); + throw CanteraError("SquareMatrix::solve()", "DGETRS returned INFO = {}", info); } } return info; @@ -124,7 +124,7 @@ int SquareMatrix::factor() writelogf("SquareMatrix::factor(): DGETRS returned INFO = %d\n", info); } if (! m_useReturnErrorCode) { - throw CELapackError("SquareMatrix::factor()", "DGETRS returned INFO = "+int2str(info)); + throw CanteraError("SquareMatrix::factor()", "DGETRS returned INFO = {}", info); } } return info; @@ -151,7 +151,7 @@ int SquareMatrix::factorQR() writelogf("SquareMatrix::factorQR(): DGEQRF returned INFO = %d\n", info); } if (! m_useReturnErrorCode) { - throw CELapackError("SquareMatrix::factorQR()", "DGEQRF returned INFO = " + int2str(info)); + throw CanteraError("SquareMatrix::factorQR()", "DGEQRF returned INFO = {}", info); } } size_t lworkOpt = static_cast(work[0]); @@ -187,7 +187,7 @@ int SquareMatrix::solveQR(doublereal* b) writelogf("SquareMatrix::solveQR(): DORMQR returned INFO = %d\n", info); } if (! m_useReturnErrorCode) { - throw CELapackError("SquareMatrix::solveQR()", "DORMQR returned INFO = " + int2str(info)); + throw CanteraError("SquareMatrix::solveQR()", "DORMQR returned INFO = {}", info); } } size_t lworkOpt = static_cast(work[0]); @@ -203,7 +203,7 @@ int SquareMatrix::solveQR(doublereal* b) writelogf("SquareMatrix::solveQR(): DTRTRS returned INFO = %d\n", info); } if (! m_useReturnErrorCode) { - throw CELapackError("SquareMatrix::solveQR()", "DTRTRS returned INFO = " + int2str(info)); + throw CanteraError("SquareMatrix::solveQR()", "DTRTRS returned INFO = {}", info); } } return info; @@ -230,7 +230,7 @@ doublereal SquareMatrix::rcond(doublereal anorm) writelogf("SquareMatrix::rcond(): DGECON returned INFO = %d\n", rinfo); } if (! m_useReturnErrorCode) { - throw CELapackError("SquareMatrix::rcond()", "DGECON returned INFO = " + int2str(rinfo)); + throw CanteraError("SquareMatrix::rcond()", "DGECON returned INFO = {}", rinfo); } } return rcond; @@ -262,7 +262,7 @@ doublereal SquareMatrix::rcondQR() writelogf("SquareMatrix::rcondQR(): DTRCON returned INFO = %d\n", rinfo); } if (! m_useReturnErrorCode) { - throw CELapackError("SquareMatrix::rcondQR()", "DTRCON returned INFO = " + int2str(rinfo)); + throw CanteraError("SquareMatrix::rcondQR()", "DTRCON returned INFO = {}", rinfo); } } return rcond; diff --git a/src/oneD/Domain1D.cpp b/src/oneD/Domain1D.cpp index 6eac33c4a..98313aef7 100644 --- a/src/oneD/Domain1D.cpp +++ b/src/oneD/Domain1D.cpp @@ -111,10 +111,10 @@ void Domain1D::restore(const XML_Node& dom, doublereal* soln, int loglevel) getFloatArray(*nodes[i], values, false); if (values.size() != nComponents()) { if (loglevel > 0) { - writelog("Warning: Domain1D::restore: Got an array of length " + - int2str(values.size()) + " when one of length " + - int2str(nComponents()) + " was expected. " + - "Tolerances for individual species may not be preserved.\n"); + writelog("Warning: Domain1D::restore: Got an array of length {}" + " when one of length {} was expected. " + "Tolerances for individual species may not be preserved.\n", + values.size(), nComponents()); } // The number of components will differ when restoring from a // mechanism with a different number of species. Assuming that diff --git a/src/oneD/MultiNewton.cpp b/src/oneD/MultiNewton.cpp index 3d4197baa..4f2101c53 100644 --- a/src/oneD/MultiNewton.cpp +++ b/src/oneD/MultiNewton.cpp @@ -310,7 +310,9 @@ int MultiNewton::solve(doublereal* x0, doublereal* x1, while (true) { // Check whether the Jacobian should be re-evaluated. if (jac.age() > m_maxAge) { - debuglog("\nMaximum Jacobian age reached ("+int2str(m_maxAge)+")\n", loglevel); + if (loglevel > 0) { + writelog("\nMaximum Jacobian age reached ({})\n", m_maxAge); + } forceNewJac = true; } diff --git a/src/oneD/Sim1D.cpp b/src/oneD/Sim1D.cpp index 3e8c7c635..83cbe5a3a 100644 --- a/src/oneD/Sim1D.cpp +++ b/src/oneD/Sim1D.cpp @@ -256,7 +256,9 @@ void Sim1D::solve(int loglevel, bool refine_grid) saveResidual("debug_sim1d.xml", "residual", "After unsuccessful Newton solve"); } - debuglog("Take "+int2str(nsteps)+" timesteps ", loglevel); + if (loglevel > 0) { + writelog("Take {} timesteps ", nsteps); + } dt = timeStep(nsteps, dt, m_x.data(), m_xnew.data(), loglevel-1); if (loglevel > 6) { diff --git a/src/oneD/StFlow.cpp b/src/oneD/StFlow.cpp index 2ec35d0df..6f10d8b84 100644 --- a/src/oneD/StFlow.cpp +++ b/src/oneD/StFlow.cpp @@ -643,7 +643,9 @@ void StFlow::restore(const XML_Node& dom, doublereal* soln, int loglevel) if (nm == "z") { getFloatArray(fa,x,false); np = x.size(); - debuglog("Grid contains "+int2str(np)+" points.\n", loglevel >= 2); + if (loglevel >= 2) { + writelog("Grid contains {} points.\n", np); + } readgrid = true; setupGrid(np, x.data()); } diff --git a/src/oneD/refine.cpp b/src/oneD/refine.cpp index fd040f3ad..df8832c02 100644 --- a/src/oneD/refine.cpp +++ b/src/oneD/refine.cpp @@ -152,7 +152,7 @@ int Refiner::analyze(size_t n, const doublereal* z, // Add a new point if the ratio with left interval is too large if (dz[j] > m_ratio*dz[j-1]) { m_loc[j] = 1; - m_c["point "+int2str(j)] = 1; + m_c[fmt::format("point {}", j)] = 1; m_keep[j-1] = 1; m_keep[j] = 1; m_keep[j+1] = 1; @@ -162,7 +162,7 @@ int Refiner::analyze(size_t n, const doublereal* z, // Add a point if the ratio with right interval is too large if (dz[j] < dz[j-1]/m_ratio) { m_loc[j-1] = 1; - m_c["point "+int2str(j-1)] = 1; + m_c[fmt::format("point {}", j-1)] = 1; m_keep[j-2] = 1; m_keep[j-1] = 1; m_keep[j] = 1; diff --git a/src/thermo/LatticeSolidPhase.cpp b/src/thermo/LatticeSolidPhase.cpp index 847d755c1..3b8080350 100644 --- a/src/thermo/LatticeSolidPhase.cpp +++ b/src/thermo/LatticeSolidPhase.cpp @@ -343,7 +343,7 @@ void LatticeSolidPhase::installSlavePhases(XML_Node* phaseNode) } // Add in the lattice stoichiometry constraint if (n > 0) { - string econ = "LC_" + int2str(n) + "_" + id(); + string econ = fmt::format("LC_{}_{}", n, id()); size_t m = addElement(econ, 0.0, 0, 0.0, CT_ELEM_TYPE_LATTICERATIO); size_t mm = nElements(); size_t nsp0 = m_lattice[0]->nSpecies(); diff --git a/src/thermo/VPSSMgrFactory.cpp b/src/thermo/VPSSMgrFactory.cpp index 536a388c3..9505e86b5 100644 --- a/src/thermo/VPSSMgrFactory.cpp +++ b/src/thermo/VPSSMgrFactory.cpp @@ -305,7 +305,8 @@ VPSSMgr* VPSSMgrFactory::newVPSSMgr(VPSSMgr_enumType type, return new VPSSMgr_General(vp_ptr, &spthermoRef); case cVPSSMGR_UNDEF: default: - throw UnknownVPSSMgrModel("VPSSMgrFactory::newVPSSMgr", int2str(type)); + throw CanteraError("VPSSMgrFactory::newVPSSMgr", + "Specified VPSSMgr model {} does not match any known type.", type); return 0; } } diff --git a/src/tpx/Sub.cpp b/src/tpx/Sub.cpp index 623a11910..82386f609 100644 --- a/src/tpx/Sub.cpp +++ b/src/tpx/Sub.cpp @@ -214,8 +214,8 @@ void Substance::Set(PropertyPair::type XY, double x0, double 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"); + throw CanteraError("Substance::Set", + "Can't set vapor fraction above the critical point"); } else { set_T(temp); update_sat(); @@ -227,8 +227,8 @@ void Substance::Set(PropertyPair::type XY, double x0, double 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"); + throw CanteraError("Substance::Set", + "Can't set vapor fraction above the critical point"); } else { set_T(x0); update_sat(); diff --git a/src/transport/DustyGasTransport.cpp b/src/transport/DustyGasTransport.cpp index 999552ac6..8d29c9497 100644 --- a/src/transport/DustyGasTransport.cpp +++ b/src/transport/DustyGasTransport.cpp @@ -237,7 +237,7 @@ void DustyGasTransport::updateMultiDiffCoeffs() int ierr = invert(m_multidiff); if (ierr != 0) { throw CanteraError("DustyGasTransport::updateMultiDiffCoeffs", - "invert returned ierr = "+int2str(ierr)); + "invert returned ierr = {}", ierr); } }