Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address fmt::format_to memory_buffer deprecation #1122

Merged
merged 3 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions include/cantera/base/fmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,18 @@ void format_to(fmt::memory_buffer& b, Args... args) {
inline std::string to_string(fmt::memory_buffer& b) {
return b.str();
}
#endif

#if !defined(FMT_VERSION) || FMT_VERSION < 80000
template <typename... Args>
void fmt_append(fmt::memory_buffer& b, Args... args) {
format_to(b, args...);
}
#else
template <typename... Args>
void fmt_append(fmt::memory_buffer& b, Args... args) {
format_to(fmt::appender(b), args...);
}
#endif

#endif
29 changes: 14 additions & 15 deletions src/base/AnyMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1393,11 +1393,11 @@ std::string AnyMap::keys_str() const
fmt::memory_buffer b;
auto iter = this->begin();
if (iter != this->end()) {
format_to(b, "{}", iter->first);
fmt_append(b, "{}", iter->first);
++iter;
}
while (iter != this->end()) {
format_to(b, ", {}", iter->first);
fmt_append(b, ", {}", iter->first);
++iter;
}
return to_string(b);
Expand Down Expand Up @@ -1756,7 +1756,7 @@ void formatInputFile(fmt::memory_buffer& b, const shared_ptr<AnyMap>& metadata,
column2 = column;
}

format_to(b, "| Line |\n");
fmt_append(b, "| Line |\n");
if (!metadata->hasKey("file-contents")) {
std::ifstream infile(findInputFile(filename));
std::stringstream buffer;
Expand All @@ -1769,15 +1769,15 @@ void formatInputFile(fmt::memory_buffer& b, const shared_ptr<AnyMap>& metadata,
std::stringstream contents((*metadata)["file-contents"].asString());
while (std::getline(contents, line)) {
if (i == lineno || i == lineno2) {
format_to(b, "> {: 5d} > {}\n", i+1, line);
format_to(b, "{:>{}}\n", "^", column + 11);
fmt_append(b, "> {: 5d} > {}\n", i+1, line);
fmt_append(b, "{:>{}}\n", "^", column + 11);
lastShown = i;
} else if ((lineno + 4 > i && lineno < i + 6) ||
(lineno2 + 4 > i && lineno2 < i + 6)) {
if (lastShown >= 0 && i - lastShown > 1) {
format_to(b, "...\n");
fmt_append(b, "...\n");
}
format_to(b, "| {: 5d} | {}\n", i+1, line);
fmt_append(b, "| {: 5d} | {}\n", i+1, line);
lastShown = i;
}
i++;
Expand All @@ -1795,7 +1795,7 @@ std::string InputFileError::formatError(const std::string& message,
std::string filename = metadata->getString("filename", "input string");

fmt::memory_buffer b;
format_to(b, "Error on line {} of {}:\n{}\n", lineno+1, filename, message);
fmt_append(b, "Error on line {} of {}:\n{}\n", lineno+1, filename, message);
formatInputFile(b, metadata, filename, lineno, column);
return to_string(b);
}
Expand All @@ -1814,16 +1814,15 @@ std::string InputFileError::formatError2(const std::string& message,

fmt::memory_buffer b;
if (filename1 == filename2) {
format_to(b, "Error on lines {} and {} of {}:\n",
std::min(line1, line2) + 1, std::max(line1, line2) + 1,
filename1);
format_to(b, "{}\n", message);
fmt_append(b, "Error on lines {} and {} of {}:\n",
std::min(line1, line2) + 1, std::max(line1, line2) + 1, filename1);
fmt_append(b, "{}\n", message);
formatInputFile(b, metadata1, filename1, line1, column1, line2, column2);
} else {
format_to(b, "Error on line {} of {} and line {} of {}:\n{}\n",
line1+1, filename1, line2+1, filename2, message);
fmt_append(b, "Error on line {} of {} and line {} of {}:\n{}\n",
line1+1, filename1, line2+1, filename2, message);
formatInputFile(b, metadata1, filename1, line1, column1);
format_to(b, "\n");
fmt_append(b, "\n");
formatInputFile(b, metadata2, filename2, line2, column2);
}

Expand Down
4 changes: 2 additions & 2 deletions src/kinetics/KineticsFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void addReactions(Kinetics& kin, const AnyMap& phaseNode, const AnyMap& rootNode
try {
kin.addReaction(newReaction(R, kin), false);
} catch (CanteraError& err) {
format_to(add_rxn_err, "{}", err.what());
fmt_append(add_rxn_err, "{}", err.what());
}
}
} else {
Expand All @@ -187,7 +187,7 @@ void addReactions(Kinetics& kin, const AnyMap& phaseNode, const AnyMap& rootNode
try {
kin.addReaction(newReaction(R, kin), false);
} catch (CanteraError& err) {
format_to(add_rxn_err, "{}", err.what());
fmt_append(add_rxn_err, "{}", err.what());
}
#else
kin.addReaction(newReaction(R, kin), false);
Expand Down
8 changes: 4 additions & 4 deletions src/kinetics/RxnRates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,10 @@ void Plog::validate(const std::string& equation)
k += rates_.at(p).updateRC(log(T[i]), 1.0/T[i]);
}
if (k < 0) {
format_to(err_reactions,
"\nInvalid rate coefficient for reaction '{}'\n"
"at P = {:.5g}, T = {:.1f}\n",
equation, std::exp(iter->first), T[i]);
fmt_append(err_reactions,
"\nInvalid rate coefficient for reaction '{}'\n"
"at P = {:.5g}, T = {:.1f}\n",
equation, std::exp(iter->first), T[i]);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/numerics/CVodesIntegrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,8 @@ string CVodesIntegrator::getErrorInfo(int N)
sort(weightedErrors.begin(), weightedErrors.end());
fmt::memory_buffer s;
for (int i=0; i<N; i++) {
format_to(s, "{}: {}\n",
get<2>(weightedErrors[i]), get<1>(weightedErrors[i]));
fmt_append(s, "{}: {}\n",
get<2>(weightedErrors[i]), get<1>(weightedErrors[i]));
}
return to_string(s);
}
Expand Down
85 changes: 44 additions & 41 deletions src/thermo/MolalityVPSSTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,16 +381,16 @@ std::string MolalityVPSSTP::report(bool show_thermo, doublereal threshold) const
fmt::memory_buffer b;
try {
if (name() != "") {
format_to(b, "\n {}:\n", name());
fmt_append(b, "\n {}:\n", name());
}
format_to(b, "\n");
format_to(b, " temperature {:12.6g} K\n", temperature());
format_to(b, " pressure {:12.6g} Pa\n", pressure());
format_to(b, " density {:12.6g} kg/m^3\n", density());
format_to(b, " mean mol. weight {:12.6g} amu\n", meanMolecularWeight());
fmt_append(b, "\n");
fmt_append(b, " temperature {:12.6g} K\n", temperature());
fmt_append(b, " pressure {:12.6g} Pa\n", pressure());
fmt_append(b, " density {:12.6g} kg/m^3\n", density());
fmt_append(b, " mean mol. weight {:12.6g} amu\n", meanMolecularWeight());

doublereal phi = electricPotential();
format_to(b, " potential {:12.6g} V\n", phi);
fmt_append(b, " potential {:12.6g} V\n", phi);

vector_fp x(m_kk);
vector_fp molal(m_kk);
Expand All @@ -408,63 +408,66 @@ 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);
format_to(b, " pH {:12.4g}\n", pH);
fmt_append(b,
" pH {:12.4g}\n", pH);
}

if (show_thermo) {
format_to(b, "\n");
format_to(b, " 1 kg 1 kmol\n");
format_to(b, " ----------- ------------\n");
format_to(b, " enthalpy {:12.6g} {:12.4g} J\n",
enthalpy_mass(), enthalpy_mole());
format_to(b, " internal energy {:12.6g} {:12.4g} J\n",
intEnergy_mass(), intEnergy_mole());
format_to(b, " entropy {:12.6g} {:12.4g} J/K\n",
entropy_mass(), entropy_mole());
format_to(b, " Gibbs function {:12.6g} {:12.4g} J\n",
gibbs_mass(), gibbs_mole());
format_to(b, " heat capacity c_p {:12.6g} {:12.4g} J/K\n",
cp_mass(), cp_mole());
fmt_append(b, "\n");
fmt_append(b, " 1 kg 1 kmol\n");
fmt_append(b, " ----------- ------------\n");
fmt_append(b, " enthalpy {:12.6g} {:12.4g} J\n",
enthalpy_mass(), enthalpy_mole());
fmt_append(b, " internal energy {:12.6g} {:12.4g} J\n",
intEnergy_mass(), intEnergy_mole());
fmt_append(b, " entropy {:12.6g} {:12.4g} J/K\n",
entropy_mass(), entropy_mole());
fmt_append(b, " Gibbs function {:12.6g} {:12.4g} J\n",
gibbs_mass(), gibbs_mole());
fmt_append(b, " heat capacity c_p {:12.6g} {:12.4g} J/K\n",
cp_mass(), cp_mole());
try {
format_to(b, " heat capacity c_v {:12.6g} {:12.4g} J/K\n",
cv_mass(), cv_mole());
fmt_append(b, " heat capacity c_v {:12.6g} {:12.4g} J/K\n",
cv_mass(), cv_mole());
} catch (NotImplementedError&) {
format_to(b, " heat capacity c_v <not implemented>\n");
fmt_append(b, " heat capacity c_v <not implemented>\n");
}
}

format_to(b, "\n");
fmt_append(b, "\n");
int nMinor = 0;
doublereal xMinor = 0.0;
if (show_thermo) {
format_to(b, " X "
" Molalities Chem.Pot. ChemPotSS ActCoeffMolal\n");
format_to(b, " "
" (J/kmol) (J/kmol)\n");
format_to(b, " ------------- "
" ------------ ------------ ------------ ------------\n");
fmt_append(b, " X "
" Molalities Chem.Pot. ChemPotSS ActCoeffMolal\n");
fmt_append(b, " "
" (J/kmol) (J/kmol)\n");
fmt_append(b, " ------------- "
" ------------ ------------ ------------ ------------\n");
for (size_t k = 0; k < m_kk; k++) {
if (x[k] > threshold) {
if (x[k] > SmallNumber) {
format_to(b, "{:>18s} {:12.6g} {:12.6g} {:12.6g} {:12.6g} {:12.6g}\n",
speciesName(k), x[k], molal[k], mu[k], muss[k], acMolal[k]);
fmt_append(b,
"{:>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 {
format_to(b, "{:>18s} {:12.6g} {:12.6g} N/A {:12.6g} {:12.6g}\n",
speciesName(k), x[k], molal[k], muss[k], acMolal[k]);
fmt_append(b,
"{:>18s} {:12.6g} {:12.6g} N/A "
"{:12.6g} {:12.6g}\n", speciesName(k),
x[k], molal[k], muss[k], acMolal[k]);
}
} else {
nMinor++;
xMinor += x[k];
}
}
} else {
format_to(b, " X"
"Molalities\n");
format_to(b, " -------------"
" ------------\n");
fmt_append(b, " X Molalities\n");
fmt_append(b, " ------------- ------------\n");
for (size_t k = 0; k < m_kk; k++) {
if (x[k] > threshold) {
format_to(b, "{:>18s} {:12.6g} {:12.6g}\n",
fmt_append(b, "{:>18s} {:12.6g} {:12.6g}\n",
speciesName(k), x[k], molal[k]);
} else {
nMinor++;
Expand All @@ -473,7 +476,7 @@ std::string MolalityVPSSTP::report(bool show_thermo, doublereal threshold) const
}
}
if (nMinor) {
format_to(b, " [{:+5d} minor] {:12.6g}\n", nMinor, xMinor);
fmt_append(b, " [{:+5d} minor] {:12.6g}\n", nMinor, xMinor);
}
} catch (CanteraError& err) {
return to_string(b) + err.what();
Expand Down
59 changes: 29 additions & 30 deletions src/thermo/PureFluidPhase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,50 +415,49 @@ std::string PureFluidPhase::report(bool show_thermo, doublereal threshold) const

string blank_leader = fmt::format("{:{}}", "", name_width);

string one_property = "{:>{}} {:<.5g} {}\n";
string one_property = fmt::format("{{:>{}}} {{:<.5g}} {{}}\n", name_width);

string two_prop_header = "{} {:^15} {:^15}\n";
string kg_kmol_header = fmt::format(
two_prop_header, blank_leader, "1 kg", "1 kmol"
);
string Y_X_header = fmt::format(
two_prop_header, blank_leader, "mass frac. Y", "mole frac. X"
);
string two_prop_sep = fmt::format(
"{} {:-^15} {:-^15}\n", blank_leader, "", ""
);
string two_property = "{:>{}} {:15.5g} {:15.5g} {}\n";

string three_prop_header = fmt::format(
"{} {:^15} {:^15} {:^15}\n", blank_leader, "mass frac. Y",
"mole frac. X", "chem. pot. / RT"
);
string three_prop_sep = fmt::format(
"{} {:-^15} {:-^15} {:-^15}\n", blank_leader, "", "", ""
string two_property = fmt::format(
"{{:>{}}} {{:15.5g}} {{:15.5g}} {{}}\n", name_width
);
string three_property = "{:>{}} {:15.5g} {:15.5g} {:15.5g}\n";

if (name() != "") {
format_to(b, "\n {}:\n", name());
fmt_append(b, "\n {}:\n", name());
}
format_to(b, "\n");
format_to(b, one_property, "temperature", name_width, temperature(), "K");
format_to(b, one_property, "pressure", name_width, pressure(), "Pa");
format_to(b, one_property, "density", name_width, density(), "kg/m^3");
format_to(b, one_property, "mean mol. weight", name_width, meanMolecularWeight(), "kg/kmol");
format_to(b, "{:>{}} {:<.5g}\n", "vapor fraction", name_width, vaporFraction());
format_to(b, "{:>{}} {}\n", "phase of matter", name_width, phaseOfMatter());
fmt_append(b, "\n");
fmt_append(b, one_property, "temperature", temperature(), "K");
fmt_append(b, one_property, "pressure", pressure(), "Pa");
fmt_append(b, one_property, "density", density(), "kg/m^3");
fmt_append(b, one_property,
"mean mol. weight", meanMolecularWeight(), "kg/kmol");
fmt_append(b, "{:>{}} {:<.5g}\n",
"vapor fraction", name_width, vaporFraction());
fmt_append(b, "{:>{}} {}\n",
"phase of matter", name_width, phaseOfMatter());

if (show_thermo) {
format_to(b, "\n");
format_to(b, kg_kmol_header);
format_to(b, two_prop_sep);
format_to(b, two_property, "enthalpy", name_width, enthalpy_mass(), enthalpy_mole(), "J");
format_to(b, two_property, "internal energy", name_width, intEnergy_mass(), intEnergy_mole(), "J");
format_to(b, two_property, "entropy", name_width, entropy_mass(), entropy_mole(), "J/K");
format_to(b, two_property, "Gibbs function", name_width, gibbs_mass(), gibbs_mole(), "J");
format_to(b, two_property, "heat capacity c_p", name_width, cp_mass(), cp_mole(), "J/K");
format_to(b, two_property, "heat capacity c_v", name_width, cv_mass(), cv_mole(), "J/K");
fmt_append(b, "\n");
fmt_append(b, kg_kmol_header);
fmt_append(b, two_prop_sep);
fmt_append(b, two_property,
"enthalpy", enthalpy_mass(), enthalpy_mole(), "J");
fmt_append(b, two_property,
"internal energy", intEnergy_mass(), intEnergy_mole(), "J");
fmt_append(b, two_property,
"entropy", entropy_mass(), entropy_mole(), "J/K");
fmt_append(b, two_property,
"Gibbs function", gibbs_mass(), gibbs_mole(), "J");
fmt_append(b, two_property,
"heat capacity c_p", cp_mass(), cp_mole(), "J/K");
fmt_append(b, two_property,
"heat capacity c_v", cv_mass(), cv_mole(), "J/K");
}

return to_string(b);
Expand Down
4 changes: 2 additions & 2 deletions src/thermo/RedlichKwongMFTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -933,9 +933,9 @@ void RedlichKwongMFTP::updateMixingExpressions()
for (size_t k = 0; k < m_kk; k++) {
if (isnan(b_vec_Curr_[k])) {
if (b.size() > 0) {
format_to(b, ", {}", speciesName(k));
fmt_append(b, ", {}", speciesName(k));
} else {
format_to(b, "{}", speciesName(k));
fmt_append(b, "{}", speciesName(k));
}
}
}
Expand Down
Loading