Skip to content

Commit

Permalink
WIP: Modify some cases to avoid reliance on a working NaN implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
speth committed Dec 21, 2021
1 parent 80c1e56 commit 5abdaa8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 26 deletions.
4 changes: 2 additions & 2 deletions include/cantera/base/ValueCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ namespace Cantera
template <class T>
struct CachedValue {
CachedValue() :
state1(std::numeric_limits<double>::quiet_NaN()),
state2(std::numeric_limits<double>::quiet_NaN()),
state1(1.2345e300),
state2(1.2345e300),
stateNum(std::numeric_limits<int>::min()),
value(T())
{
Expand Down
14 changes: 7 additions & 7 deletions include/cantera/kinetics/ReactionData.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct ReactionData
//! assumed to be constant, like the reaction rate parameters or the number of
//! reactions.
virtual void invalidateCache() {
temperature = NAN;
temperature = 1.2345e300;
}

double temperature; //!< temperature
Expand Down Expand Up @@ -129,13 +129,13 @@ struct FalloffData : public ReactionData
virtual void update(double T, double M) override;

virtual void resize(size_t n_species, size_t n_reactions) override {
conc_3b.resize(n_reactions, NAN);
conc_3b.resize(n_reactions, 1.2345e300);
ready = true;
}

virtual void invalidateCache() override {
ReactionData::invalidateCache();
molar_density = NAN;
molar_density = 1.2345e300;
}

bool ready; //!< boolean indicating whether vectors are accessible
Expand All @@ -154,7 +154,7 @@ struct FalloffData : public ReactionData
*/
struct PlogData : public ReactionData
{
PlogData() : pressure(NAN), logP(0.) {}
PlogData() : pressure(1.2345e300), logP(0.) {}

virtual void update(double T) override;

Expand All @@ -168,7 +168,7 @@ struct PlogData : public ReactionData

virtual void invalidateCache() override {
ReactionData::invalidateCache();
pressure = NAN;
pressure = 1.2345e300;
}

double pressure; //!< pressure
Expand All @@ -183,7 +183,7 @@ struct PlogData : public ReactionData
*/
struct ChebyshevData : public ReactionData
{
ChebyshevData() : pressure(NAN), log10P(0.) {}
ChebyshevData() : pressure(1.2345e300), log10P(0.) {}

virtual void update(double T) override;

Expand All @@ -197,7 +197,7 @@ struct ChebyshevData : public ReactionData

virtual void invalidateCache() override {
ReactionData::invalidateCache();
pressure = NAN;
pressure = 1.2345e300;
}

double pressure; //!< pressure
Expand Down
8 changes: 4 additions & 4 deletions src/kinetics/ReactionData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ bool ArrheniusData::update(const ThermoPhase& bulk, const Kinetics& kin)

BlowersMaselData::BlowersMaselData()
: ready(false)
, density(NAN)
, density(1.2345e300)
, m_state_mf_number(-1)
{
dH.resize(1, NAN);
dH.resize(1, 1.2345e300);
}

void BlowersMaselData::update(double T)
Expand Down Expand Up @@ -69,10 +69,10 @@ bool BlowersMaselData::update(const ThermoPhase& bulk, const Kinetics& kin)

FalloffData::FalloffData()
: ready(false)
, molar_density(NAN)
, molar_density(1.2345e300)
, m_state_mf_number(-1)
{
conc_3b.resize(1, NAN);
conc_3b.resize(1, 1.2345e300);
}

void FalloffData::update(double T)
Expand Down
2 changes: 2 additions & 0 deletions src/thermo/IdealGasPhase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ void IdealGasPhase::_updateThermo() const

// If the temperature has changed since the last time these
// properties were computed, recompute them.
// writelog("IdealGasPhase::_update: checking {} versus {}: do update = {}\n",
// cached.state1, tnow, cached.state1 != tnow);
if (cached.state1 != tnow) {
m_spthermo.update(tnow, &m_cp0_R[0], &m_h0_RT[0], &m_s0_R[0]);
cached.state1 = tnow;
Expand Down
28 changes: 15 additions & 13 deletions src/thermo/RedlichKwongMFTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using namespace std;
namespace bmt = boost::math::tools;

static const double fast_nan = 1.2345e300;

namespace Cantera
{

Expand Down Expand Up @@ -71,10 +73,10 @@ void RedlichKwongMFTP::setSpeciesCoeffs(const std::string& species,
}

// a_coeff_vec(0) is initialized to NaN to mark uninitialized species
if (isnan(a_coeff_vec(0, j + m_kk * j))) {
if (a_coeff_vec(0, j + m_kk * j) == fast_nan) {
// The diagonal element of the jth species has not yet been defined.
continue;
} else if (isnan(a_coeff_vec(0, j + m_kk * k))) {
} else if (a_coeff_vec(0, j + m_kk * k) == fast_nan) {
// Only use the mixing rules if the off-diagonal element has not already been defined by a
// user-specified crossFluidParameters entry:
double a0kj = sqrt(a_coeff_vec(0, j + m_kk * j) * a0);
Expand Down Expand Up @@ -388,8 +390,8 @@ bool RedlichKwongMFTP::addSpecies(shared_ptr<Species> spec)

// Initialize a_vec and b_vec to NaN, to screen for species with
// pureFluidParameters which are undefined in the input file:
b_vec_Curr_.push_back(NAN);
a_coeff_vec.resize(2, m_kk * m_kk, NAN);
b_vec_Curr_.push_back(fast_nan);
a_coeff_vec.resize(2, m_kk * m_kk, fast_nan);

m_pp.push_back(0.0);
m_coeffs_from_db.push_back(false);
Expand All @@ -412,7 +414,7 @@ void RedlichKwongMFTP::initThermoXML(XML_Node& phaseNode, const std::string& id)
// Reset any coefficients which may have been set using values from
// 'critical-properties.yaml' as part of non-XML initialization, so that
// off-diagonal elements can be correctly initialized
a_coeff_vec.data().assign(a_coeff_vec.data().size(), NAN);
a_coeff_vec.data().assign(a_coeff_vec.data().size(), fast_nan);

// Go get all of the coefficients and factors in the
// activityCoefficients XML block
Expand Down Expand Up @@ -449,7 +451,7 @@ void RedlichKwongMFTP::initThermoXML(XML_Node& phaseNode, const std::string& id)
size_t counter = iSpecies + m_kk * iSpecies;

// If not, then search the database:
if (isnan(a_coeff_vec(0, counter))) {
if (a_coeff_vec(0, counter) == fast_nan) {

vector<double> coeffArray;

Expand All @@ -460,7 +462,7 @@ void RedlichKwongMFTP::initThermoXML(XML_Node& phaseNode, const std::string& id)

// Check if species was found in the database of critical properties,
// and assign the results
if (!isnan(coeffArray[0])) {
if (coeffArray[0] != fast_nan) {
//Assuming no temperature dependence (i,e a1 = 0)
setSpeciesCoeffs(iName, coeffArray[0], 0.0, coeffArray[1]);
m_coeffs_from_db[i] = true;
Expand All @@ -481,7 +483,7 @@ void RedlichKwongMFTP::initThermo()
for (auto& item : m_species) {
auto& data = item.second->input;
size_t k = speciesIndex(item.first);
if (!isnan(a_coeff_vec(0, k + m_kk * k))) {
if (a_coeff_vec(0, k + m_kk * k) != fast_nan) {
continue;
}
bool foundCoeffs = false;
Expand Down Expand Up @@ -531,7 +533,7 @@ void RedlichKwongMFTP::initThermo()
}

// Coefficients have not been populated from model-specific input
double Tc = NAN, Pc = NAN;
double Tc = fast_nan, Pc = fast_nan;
if (data.hasKey("critical-parameters")) {
// Use critical state information stored in the species entry to
// calculate a and b
Expand All @@ -557,7 +559,7 @@ void RedlichKwongMFTP::initThermo()
}

// Check if critical properties were found in either location
if (!isnan(Tc)) {
if (Tc != fast_nan) {
// Assuming no temperature dependence (i.e. a1 = 0)
double a = omega_a * pow(GasConstant, 2) * pow(Tc, 2.5) / Pc;
double b = omega_b * GasConstant * Tc / Pc;
Expand Down Expand Up @@ -615,7 +617,7 @@ vector<double> RedlichKwongMFTP::getCoeff(const std::string& iName)
warn_deprecated("RedlichKwongMFTP::getCoeff", "To be removed after Cantera 2.6. "
"Use of critical-properties.yaml is integrated into initThermo() "
"for YAML input files.");
vector_fp spCoeff{NAN, NAN};
vector_fp spCoeff{fast_nan, fast_nan};
AnyMap data = AnyMap::fromYamlFile("critical-properties.yaml");
const auto& species = data["species"].asMap("name");

Expand Down Expand Up @@ -921,11 +923,11 @@ void RedlichKwongMFTP::updateMixingExpressions()
m_a_current += a_vec_Curr_[i * m_kk + j] * moleFractions_[i] * moleFractions_[j];
}
}
if (isnan(m_b_current)) {
if (m_b_current == fast_nan) { // does not work
// One or more species do not have specified coefficients.
fmt::memory_buffer b;
for (size_t k = 0; k < m_kk; k++) {
if (isnan(b_vec_Curr_[k])) {
if (b_vec_Curr_[k] == fast_nan) {
if (b.size() > 0) {
fmt_append(b, ", {}", speciesName(k));
} else {
Expand Down

0 comments on commit 5abdaa8

Please sign in to comment.