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

Improve warning messages in C++ #741

Merged
merged 11 commits into from Nov 11, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions include/cantera/base/global.h
Expand Up @@ -187,6 +187,26 @@ void warn_deprecated(const std::string& method, const std::string& extra="");
//! @copydoc Application::suppress_deprecation_warnings
void suppress_deprecation_warnings();

//! helper function passing user warning to global handler
void _warn_user(const std::string& method, const std::string& extra);

/*!
* Print a user warning raised from *method*.
*
* @param method method name
* @param msg Python-style format string with the following arguments
* @param args arguments for the format string
*/
template <typename... Args>
void warn_user(const std::string& method, const std::string& msg,
const Args&... args) {
if (sizeof...(args) == 0) {
_warn_user(method, msg);
} else {
_warn_user(method, fmt::format(msg, args...));
}
}

//! @copydoc Application::make_deprecation_warnings_fatal
void make_deprecation_warnings_fatal();

Expand Down
2 changes: 1 addition & 1 deletion interfaces/cython/cantera/ck2yaml.py
Expand Up @@ -620,7 +620,7 @@ def __init__(self, A=0.0, T3=0.0, T1=0.0, T2=None):

def reduce(self, output):
troe = FlowMap([('A', self.A), ('T3', self.T3), ('T1', self.T1)])
if self.T2:
if self.T2 is not None:
troe['T2'] = self.T2
output['Troe'] = troe

Expand Down
9 changes: 8 additions & 1 deletion src/base/application.cpp
Expand Up @@ -170,7 +170,14 @@ void Application::warn_deprecated(const std::string& method,
return;
}
warnings.insert(method);
writelog("WARNING: '" + method + "' is deprecated. " + extra);
writelog(fmt::format("DeprecationWarning: {}: {}", method, extra));
writelogendl();
}

void Application::warn_user(const std::string& method,
const std::string& extra)
{
writelog(fmt::format("CanteraWarning: {}: {}", method, extra));
writelogendl();
}

Expand Down
4 changes: 4 additions & 0 deletions src/base/application.h
Expand Up @@ -347,6 +347,10 @@ class Application
m_fatal_deprecation_warnings = true;
}

//! Print a user warning arising during usage of *method*. Additional
//! information can be specified in *extra*.
void warn_user(const std::string& method, const std::string& extra="");

//! Globally disable printing of warnings about problematic thermo data,
//! e.g. NASA polynomials with discontinuities at the midpoint temperature.
void suppress_thermo_warnings(bool suppress=true) {
Expand Down
5 changes: 5 additions & 0 deletions src/base/global.cpp
Expand Up @@ -56,6 +56,11 @@ void warn_deprecated(const std::string& method, const std::string& extra)
app()->warn_deprecated(method, extra);
}

void _warn_user(const std::string& method, const std::string& extra)
{
app()->warn_user(method, extra);
}

void suppress_deprecation_warnings()
{
app()->suppress_deprecation_warnings();
Expand Down
16 changes: 9 additions & 7 deletions src/equil/ChemEquil.cpp
Expand Up @@ -111,9 +111,10 @@ void ChemEquil::initialize(thermo_t& s)
// the element should be an electron... if it isn't
// print a warning.
if (s.atomicWeight(m) > 1.0e-3) {
writelog("WARNING: species {} has {} atoms of element {},"
" but this element is not an electron.\n",
s.speciesName(k), s.nAtoms(k,m), s.elementName(m));
warn_user("ChemEquil::initialize",
"species {} has {} atoms of element {}, "
"but this element is not an electron.",
s.speciesName(k), s.nAtoms(k,m), s.elementName(m));
}
}
}
Expand Down Expand Up @@ -583,9 +584,9 @@ int ChemEquil::equilibrate(thermo_t& s, const char* XYstr,

if (s.temperature() > s.maxTemp() + 1.0 ||
s.temperature() < s.minTemp() - 1.0) {
writelog("Warning: Temperature ({} K) outside valid range of "
"{} K to {} K\n",
s.temperature(), s.minTemp(), s.maxTemp());
warn_user("ChemEquil::equilibrate",
"Temperature ({} K) outside valid range of {} K "
"to {} K", s.temperature(), s.minTemp(), s.maxTemp());
}
return 0;
}
Expand Down Expand Up @@ -657,7 +658,8 @@ int ChemEquil::equilibrate(thermo_t& s, const char* XYstr,
}
}
if (fctr != 1.0 && loglevel > 0) {
writelogf("WARNING Soln Damping because of bounds: %g\n", fctr);
warn_user("ChemEquil::equilibrate",
"Soln Damping because of bounds: %g", fctr);
}

// multiply the step by the scaling factor
Expand Down
3 changes: 2 additions & 1 deletion src/equil/vcs_VolPhase.cpp
Expand Up @@ -587,7 +587,8 @@ void vcs_VolPhase::setPtrThermoPhase(ThermoPhase* tp_ptr)
size_t nelem = TP_ptr->nElements();
if (nsp != m_numSpecies) {
if (m_numSpecies != 0) {
plogf("Warning Nsp != NVolSpeces: %d %d \n", nsp, m_numSpecies);
warn_user("vcs_VolPhase::setPtrThermoPhase",
"Nsp != NVolSpeces: {} {}", nsp, m_numSpecies);
}
resize(VP_ID_, nsp, nelem, PhaseName.c_str());
}
Expand Down
10 changes: 10 additions & 0 deletions src/kinetics/Falloff.cpp
Expand Up @@ -8,6 +8,7 @@

#include "cantera/base/stringUtils.h"
#include "cantera/base/ctexceptions.h"
#include "cantera/base/global.h"
#include "cantera/kinetics/Falloff.h"

namespace Cantera
Expand Down Expand Up @@ -43,6 +44,15 @@ void Troe::init(const vector_fp& c)
}

if (c.size() == 4) {
if (std::abs(c[3]) < SmallNumber) {
warn_user("Troe::init",
"Unexpected parameter value T2=0. Omitting exp(T2/T) term from "
"falloff expression. To suppress this warning, remove value "
"for T2 from the input file. In the unlikely case that the "
"exp(T2/T) term should be included with T2 effectively equal "
"to 0, set T2 to a sufficiently small value "
"(i.e. T2 < 1e-16).");
}
ischoegl marked this conversation as resolved.
Show resolved Hide resolved
m_t2 = c[3];
}
}
Expand Down
16 changes: 11 additions & 5 deletions src/kinetics/Reaction.cpp
Expand Up @@ -390,19 +390,25 @@ void readFalloff(FalloffReaction& R, const AnyMap& node)
vector_fp params{
f["A"].asDouble(),
f["T3"].asDouble(),
f["T1"].asDouble(),
f.getDouble("T2", 0.0)
f["T1"].asDouble()
};
if (f.hasKey("T2")) {
params.push_back(f["T2"].asDouble());
}
R.falloff = newFalloff("Troe", params);
} else if (node.hasKey("SRI")) {
auto& f = node["SRI"].as<AnyMap>();
vector_fp params{
f["A"].asDouble(),
f["B"].asDouble(),
f["C"].asDouble(),
f.getDouble("D", 1.0),
f.getDouble("E", 0.0)
f["C"].asDouble()
};
if (f.hasKey("D")) {
params.push_back(f["D"].asDouble());
}
if (f.hasKey("E")) {
params.push_back(f["E"].asDouble());
}
R.falloff = newFalloff("SRI", params);
} else {
R.falloff = newFalloff("Lindemann", {});
Expand Down
6 changes: 3 additions & 3 deletions src/oneD/Domain1D.cpp
Expand Up @@ -129,9 +129,9 @@ 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 {}"
" when one of length {} was expected. "
"Tolerances for individual species may not be preserved.\n",
warn_user("Domain1D::restore", "Received an array of length "
"{} when one of length {} was expected. Tolerances for "
"individual species may not be preserved.",
values.size(), nComponents());
}
// The number of components will differ when restoring from a
Expand Down
4 changes: 2 additions & 2 deletions src/oneD/Sim1D.cpp
Expand Up @@ -124,8 +124,8 @@ void Sim1D::restore(const std::string& fname, const std::string& id,
for (size_t m = 0; m < nDomains(); m++) {
Domain1D& dom = domain(m);
if (loglevel > 0 && xd[m]->attrib("id") != dom.id()) {
writelog("Warning: domain names do not match: '" +
(*xd[m])["id"] + + "' and '" + dom.id() + "'\n");
warn_user("Sim1D::restore", "Domain names do not match: "
"'{} and '{}'", (*xd[m])["id"], dom.id());
}
dom.resize(domain(m).nComponents(), intValue((*xd[m])["points"]));
}
Expand Down
2 changes: 1 addition & 1 deletion src/oneD/StFlow.cpp
Expand Up @@ -760,7 +760,7 @@ void StFlow::restore(const XML_Node& dom, doublereal* soln, int loglevel)
// This may occur when restoring from a mechanism with a different
// number of species.
if (loglevel > 0) {
writelog("\nWarning: StFlow::restore: species_enabled is "
warn_user("StFlow::restore", "species_enabled is "
"length {} but should be length {}. Enabling all species "
"equations by default.", x.size(), m_nsp);
}
Expand Down
6 changes: 4 additions & 2 deletions src/thermo/MixtureFugacityTP.cpp
Expand Up @@ -708,11 +708,13 @@ doublereal MixtureFugacityTP::calculatePsat(doublereal TKelvin, doublereal& mola
}
}
if (!foundGas || !foundLiquid) {
writelog("error couldn't find a starting pressure\n");
warn_user("MixtureFugacityTP::calculatePsat",
"could not find a starting pressure; exiting.");
return 0.0;
}
if (presGas != presLiquid) {
writelog("error couldn't find a starting pressure\n");
warn_user("MixtureFugacityTP::calculatePsat",
"could not find a starting pressure; exiting");
return 0.0;
}

Expand Down
27 changes: 15 additions & 12 deletions src/thermo/NasaPoly2.cpp
Expand Up @@ -34,28 +34,31 @@ void NasaPoly2::validate(const std::string& name)

double delta = cp_low - cp_high;
if (fabs(delta/(fabs(cp_low)+1.0E-4)) > 0.01) {
writelog("\n\n**** WARNING ****\nFor species {}, discontinuity"
" in cp/R detected at Tmid = {}\n", name, m_midT);
writelog("\tValue computed using low-temperature polynomial: {}\n", cp_low);
writelog("\tValue computed using high-temperature polynomial: {}\n", cp_high);
warn_user("NasaPoly2::validate",
"\nFor species {}, discontinuity in cp/R detected at Tmid = {}\n"
"\tValue computed using low-temperature polynomial: {}\n"
"\tValue computed using high-temperature polynomial: {}\n",
name, m_midT, cp_low, cp_high);
}

// enthalpy
delta = h_low - h_high;
if (fabs(delta/cp_low) > 0.001) {
writelog("\n\n**** WARNING ****\nFor species {}, discontinuity"
" in h/RT detected at Tmid = {}\n", name, m_midT);
writelog("\tValue computed using low-temperature polynomial: {}\n", h_low);
writelog("\tValue computed using high-temperature polynomial: {}\n", h_high);
warn_user("NasaPoly2::validate",
"\nFor species {}, discontinuity in h/RT detected at Tmid = {}\n"
"\tValue computed using low-temperature polynomial: {}\n"
"\tValue computed using high-temperature polynomial: {}\n",
name, m_midT, h_low, h_high);
}

// entropy
delta = s_low - s_high;
if (fabs(delta/(fabs(s_low)+cp_low)) > 0.001) {
writelog("\n\n**** WARNING ****\nFor species {}, discontinuity"
" in s/R detected at Tmid = {}\n", name, m_midT);
writelog("\tValue computed using low-temperature polynomial: {}\n", s_low);
writelog("\tValue computed using high-temperature polynomial: {}\n", s_high);
warn_user("NasaPoly2::validate",
"\nFor species {}, discontinuity in s/R detected at Tmid = {}\n"
"\tValue computed using low-temperature polynomial: {}\n"
"\tValue computed using high-temperature polynomial: {}\n",
name, m_midT, s_low, s_high);
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/thermo/PDSS_HKFT.cpp
Expand Up @@ -314,10 +314,11 @@ void PDSS_HKFT::initThermo()
" not consistent with G and S: {} vs {} cal gmol-1",
sname, Hcalc/toSI("cal/gmol"), m_deltaH_formation_tr_pr);
} else {
writelog("PDSS_HKFT::initThermo() WARNING: DHjmol for {} is not"
" consistent with G and S: calculated {} vs input {} cal gmol-1",
sname, Hcalc/toSI("cal/gmol"), m_deltaH_formation_tr_pr);
writelog(" : continuing with consistent DHjmol = {}", Hcalc/toSI("cal/gmol"));
warn_user("PDSS_HKFT::initThermo",
"DHjmol for {} is not consistent with G and S: calculated {} "
"vs input {} cal gmol-1; continuing with consistent DHjmol = {}",
sname, Hcalc/toSI("cal/gmol"), m_deltaH_formation_tr_pr,
Hcalc/toSI("cal/gmol"));
m_deltaH_formation_tr_pr = Hcalc / toSI("cal/gmol");
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/thermo/RedlichKwongMFTP.cpp
Expand Up @@ -1289,9 +1289,9 @@ int RedlichKwongMFTP::NicholsSolve(double TKelvin, double pres, doublereal a, do
if (fabs(tmp) > 1.0E-4) {
for (int j = 0; j < 3; j++) {
if (j != i && fabs(Vroot[i] - Vroot[j]) < 1.0E-4 * (fabs(Vroot[i]) + fabs(Vroot[j]))) {
writelog("RedlichKwongMFTP::NicholsSolve(T = {}, p = {}):"
" WARNING roots have merged: {}, {}\n",
TKelvin, pres, Vroot[i], Vroot[j]);
warn_user("RedlichKwongMFTP::NicholsSolve",
"roots have merged: {}, {} (T = {}, p = {})",
Vroot[i], Vroot[j], TKelvin, pres);
}
}
}
Expand Down Expand Up @@ -1349,9 +1349,9 @@ int RedlichKwongMFTP::NicholsSolve(double TKelvin, double pres, doublereal a, do
}
}
if ((fabs(res) > 1.0E-14) && (fabs(res) > 1.0E-14 * fabs(dresdV) * fabs(Vroot[i]))) {
writelog("RedlichKwongMFTP::NicholsSolve(T = {}, p = {}): "
"WARNING root didn't converge V = {}", TKelvin, pres, Vroot[i]);
writelogendl();
warn_user("RedlichKwongMFTP::NicholsSolve",
"root did not converge: V = {} (T = {}, p = {})",
Vroot[i], TKelvin, pres);
}
}

Expand Down
14 changes: 9 additions & 5 deletions src/transport/MMCollisionInt.cpp
Expand Up @@ -428,8 +428,9 @@ void MMCollisionInt::fit_omega22(int degree, doublereal deltastar,
w[0]= -1.0;
double rmserr = polyfit(n, degree, logT, values.data(), w.data(), o22);
if (m_loglevel > 0 && rmserr > 0.01) {
writelogf("Warning: RMS error = %12.6g in omega_22 fit"
"with delta* = %12.6g\n", rmserr, deltastar);
warn_user("MMCollisionInt::fit_omega22",
"RMS error = {:12.6g} in omega_22 fit "
"with delta* = {:12.6g}", rmserr, deltastar);
}
}

Expand Down Expand Up @@ -474,17 +475,20 @@ void MMCollisionInt::fit(int degree, doublereal deltastar,

writelog("astar = [" + vec2str(vector_fp(a, a+degree+1))+ "]\n");
if (rmserr > 0.01) {
writelogf("Warning: RMS error = %12.6g for A* fit\n", rmserr);
warn_user("MMCollisionInt::fit",
"RMS error = {:12.6g} for A* fit", rmserr);
}

writelog("bstar = [" + vec2str(vector_fp(b, b+degree+1))+ "]\n");
if (rmserr > 0.01) {
writelogf("Warning: RMS error = %12.6g for B* fit\n", rmserr);
warn_user("MMCollisionInt::fit",
"RMS error = {:12.6g} for B* fit", rmserr);
}

writelog("cstar = [" + vec2str(vector_fp(c, c+degree+1))+ "]\n");
if (rmserr > 0.01) {
writelogf("Warning: RMS error = %12.6g for C* fit\n", rmserr);
warn_user("MMCollisionInt::fit",
"RMS error = {:12.6g} for C* fit", rmserr);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/data/surface1-gas.inp
Expand Up @@ -55,7 +55,7 @@ HO2 + HO2 <=> O2 + H2O2 1.320E+11 0.000 -6.820
DUPLICATE
OH + OH (+M) <=> H2O2 (+M) 7.230E+19 -0.370 0.000
LOW / 5.530E+13 -0.760 0.000 /
TROE / 0.5 0. 0. 0. /
TROE / 0.5 0. 0. /
H2 / 1. / H2O / 6.5 / O2 / 0.4 /
OH + H2O2 <=> HO2 + H2O 5.400E+12 0.000 4.200
END