Skip to content

Commit

Permalink
[Transport] Make fitting errors accessible programmatically
Browse files Browse the repository at this point in the history
  • Loading branch information
speth committed Dec 4, 2024
1 parent 6b38604 commit 508aeda
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
15 changes: 14 additions & 1 deletion include/cantera/transport/Transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

#include "cantera/base/ct_defs.h"
#include "cantera/base/ctexceptions.h"
#include "cantera/base/AnyMap.h"

namespace Cantera
{

class ThermoPhase;
class AnyMap;

/**
* @addtogroup tranprops
Expand Down Expand Up @@ -389,6 +389,16 @@ class Transport
//! separately.
AnyMap parameters() const;

//! Get error metrics about any functional fits calculated for pure species
//! transport properties.
//!
//! See GasTransport::fitDiffCoeffs and GasTransport::fitProperties.
//!
//! @warning This method is an experimental part of the %Cantera API and may be
//! changed or removed without notice.
//! @since New in %Cantera 3.1.
AnyMap fittingErrors() const { return m_fittingErrors; };

//! @name Transport manager construction
//!
//! These methods are used during construction.
Expand Down Expand Up @@ -421,6 +431,9 @@ class Transport

//! Number of species
size_t m_nsp = 0;

//! Maximum errors associated with fitting pure species transport properties.
AnyMap m_fittingErrors;
};

}
Expand Down
2 changes: 1 addition & 1 deletion interfaces/cython/cantera/transport.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ cdef extern from "cantera/transport/Transport.h" namespace "Cantera":
void getSpeciesViscosities(double*) except +translate_exception
void getCollisionIntegralPolynomial(size_t i, size_t j, double* dataA, double* dataB, double* dataC) except +translate_exception
void setCollisionIntegralPolynomial(size_t i, size_t j, double* dataA, double* dataB, double* dataC, cbool flag) except +translate_exception

CxxAnyMap fittingErrors()

cdef extern from "cantera/transport/DustyGasTransport.h" namespace "Cantera":
cdef cppclass CxxDustyGasTransport "Cantera::DustyGasTransport":
Expand Down
17 changes: 17 additions & 0 deletions interfaces/cython/cantera/transport.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,23 @@ cdef class Transport(_SolutionBase):
self.transport.setCollisionIntegralPolynomial(i, j, &adata[0], &bdata[0],
&cdata[0], actualT)

property transport_fitting_errors:
"""
Get error metrics about any functional fits calculated for pure species
transport properties. See {ct}`GasTransport::fitDiffCoeffs` and
{ct}`GasTransport::fitProperties`.
.. warning::
This property is an experimental part of the Cantera API and
may be changed or removed without notice.
.. versionadded:: 3.1
"""
def __get__(self):
cdef CxxAnyMap stats = self.transport.fittingErrors()
return anymap_to_py(stats)

cdef class DustyGasTransport(Transport):
"""
Implements the "dusty gas" model for transport in porous media.
Expand Down
10 changes: 8 additions & 2 deletions src/transport/GasTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,8 @@ void GasTransport::fitProperties(MMCollisionInt& integrals)
mxerr = std::max(mxerr, fabs(err));
mxrelerr = std::max(mxrelerr, fabs(relerr));
}
m_fittingErrors["viscosity-max-abs-error"] = mxerr;
m_fittingErrors["viscosity-max-rel-error"] = mxrelerr;

// evaluate max fit errors for conductivity
for (size_t n = 0; n < np; n++) {
Expand All @@ -635,6 +637,8 @@ void GasTransport::fitProperties(MMCollisionInt& integrals)
}
m_visccoeffs.push_back(c);
m_condcoeffs.push_back(c2);
m_fittingErrors["conductivity-max-abs-error"] = mxerr_cond;
m_fittingErrors["conductivity-max-rel-error"] = mxrelerr_cond;

if (m_log_level >= 2) {
writelog(m_thermo->speciesName(k) + ": [" + vec2str(c) + "]\n");
Expand Down Expand Up @@ -690,8 +694,7 @@ void GasTransport::fitDiffCoeffs(MMCollisionInt& integrals)

// vector of polynomial coefficients
vector<double> c(degree + 1), c2(degree + 1);
double err, relerr,
mxerr = 0.0, mxrelerr = 0.0;
double err, relerr, mxerr = 0.0, mxrelerr = 0.0;

vector<double> diff(np + 1);
m_diffcoeffs.clear();
Expand Down Expand Up @@ -744,6 +747,9 @@ void GasTransport::fitDiffCoeffs(MMCollisionInt& integrals)
}
}
}

m_fittingErrors["diff-coeff-max-abs-error"] = mxerr;
m_fittingErrors["diff-coeff-max-rel-error"] = mxrelerr;
if (m_log_level) {
writelogf("Maximum binary diffusion coefficient absolute error:"
" %12.6g\n", mxerr);
Expand Down
6 changes: 6 additions & 0 deletions src/transport/IonGasTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ void IonGasTransport::fitDiffCoeffs(MMCollisionInt& integrals)
}
}
}
m_fittingErrors["diff-coeff-max-abs-error"] =
std::max(m_fittingErrors.getDouble("diff-coeff-max-abs-error", 0.0),
mxerr);
m_fittingErrors["diff-coeff-max-rel-error"] =
std::max(m_fittingErrors.getDouble("diff-coeff-max-rel-error", 0.0),
mxrelerr);

if (m_log_level) {
writelogf("Maximum binary diffusion coefficient absolute error:"
Expand Down
6 changes: 6 additions & 0 deletions test/python/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,19 @@ def test_mixDiffCoeffsChange(self, phase):

def test_CK_mode(self, phase):
mu_ct = phase.viscosity
err_ct = phase.transport_fitting_errors
phase.transport_model = 'mixture-averaged-CK'
assert phase.transport_model == 'mixture-averaged-CK'
mu_ck = phase.viscosity
err_ck = phase.transport_fitting_errors
# values should be close, but not identical
assert abs(mu_ct - mu_ck) / mu_ct > 1e-8
assert abs(mu_ct - mu_ck) / mu_ct < 1e-2

# Cantera's fits should be an improvement in all cases
for key in err_ct:
assert err_ct[key] < err_ck[key]

def test_ionGas(self, phase):
# IonGasTransport gives the same result for a mixture
# without ionized species
Expand Down

0 comments on commit 508aeda

Please sign in to comment.