Skip to content

Commit 508aeda

Browse files
committed
[Transport] Make fitting errors accessible programmatically
1 parent 6b38604 commit 508aeda

File tree

6 files changed

+52
-4
lines changed

6 files changed

+52
-4
lines changed

include/cantera/transport/Transport.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020

2121
#include "cantera/base/ct_defs.h"
2222
#include "cantera/base/ctexceptions.h"
23+
#include "cantera/base/AnyMap.h"
2324

2425
namespace Cantera
2526
{
2627

2728
class ThermoPhase;
28-
class AnyMap;
2929

3030
/**
3131
* @addtogroup tranprops
@@ -389,6 +389,16 @@ class Transport
389389
//! separately.
390390
AnyMap parameters() const;
391391

392+
//! Get error metrics about any functional fits calculated for pure species
393+
//! transport properties.
394+
//!
395+
//! See GasTransport::fitDiffCoeffs and GasTransport::fitProperties.
396+
//!
397+
//! @warning This method is an experimental part of the %Cantera API and may be
398+
//! changed or removed without notice.
399+
//! @since New in %Cantera 3.1.
400+
AnyMap fittingErrors() const { return m_fittingErrors; };
401+
392402
//! @name Transport manager construction
393403
//!
394404
//! These methods are used during construction.
@@ -421,6 +431,9 @@ class Transport
421431

422432
//! Number of species
423433
size_t m_nsp = 0;
434+
435+
//! Maximum errors associated with fitting pure species transport properties.
436+
AnyMap m_fittingErrors;
424437
};
425438

426439
}

interfaces/cython/cantera/transport.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ cdef extern from "cantera/transport/Transport.h" namespace "Cantera":
1818
void getSpeciesViscosities(double*) except +translate_exception
1919
void getCollisionIntegralPolynomial(size_t i, size_t j, double* dataA, double* dataB, double* dataC) except +translate_exception
2020
void setCollisionIntegralPolynomial(size_t i, size_t j, double* dataA, double* dataB, double* dataC, cbool flag) except +translate_exception
21-
21+
CxxAnyMap fittingErrors()
2222

2323
cdef extern from "cantera/transport/DustyGasTransport.h" namespace "Cantera":
2424
cdef cppclass CxxDustyGasTransport "Cantera::DustyGasTransport":

interfaces/cython/cantera/transport.pyx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,23 @@ cdef class Transport(_SolutionBase):
393393
self.transport.setCollisionIntegralPolynomial(i, j, &adata[0], &bdata[0],
394394
&cdata[0], actualT)
395395

396+
property transport_fitting_errors:
397+
"""
398+
Get error metrics about any functional fits calculated for pure species
399+
transport properties. See {ct}`GasTransport::fitDiffCoeffs` and
400+
{ct}`GasTransport::fitProperties`.
401+
402+
.. warning::
403+
404+
This property is an experimental part of the Cantera API and
405+
may be changed or removed without notice.
406+
407+
.. versionadded:: 3.1
408+
"""
409+
def __get__(self):
410+
cdef CxxAnyMap stats = self.transport.fittingErrors()
411+
return anymap_to_py(stats)
412+
396413
cdef class DustyGasTransport(Transport):
397414
"""
398415
Implements the "dusty gas" model for transport in porous media.

src/transport/GasTransport.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,8 @@ void GasTransport::fitProperties(MMCollisionInt& integrals)
616616
mxerr = std::max(mxerr, fabs(err));
617617
mxrelerr = std::max(mxrelerr, fabs(relerr));
618618
}
619+
m_fittingErrors["viscosity-max-abs-error"] = mxerr;
620+
m_fittingErrors["viscosity-max-rel-error"] = mxrelerr;
619621

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

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

691695
// vector of polynomial coefficients
692696
vector<double> c(degree + 1), c2(degree + 1);
693-
double err, relerr,
694-
mxerr = 0.0, mxrelerr = 0.0;
697+
double err, relerr, mxerr = 0.0, mxrelerr = 0.0;
695698

696699
vector<double> diff(np + 1);
697700
m_diffcoeffs.clear();
@@ -744,6 +747,9 @@ void GasTransport::fitDiffCoeffs(MMCollisionInt& integrals)
744747
}
745748
}
746749
}
750+
751+
m_fittingErrors["diff-coeff-max-abs-error"] = mxerr;
752+
m_fittingErrors["diff-coeff-max-rel-error"] = mxrelerr;
747753
if (m_log_level) {
748754
writelogf("Maximum binary diffusion coefficient absolute error:"
749755
" %12.6g\n", mxerr);

src/transport/IonGasTransport.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ void IonGasTransport::fitDiffCoeffs(MMCollisionInt& integrals)
235235
}
236236
}
237237
}
238+
m_fittingErrors["diff-coeff-max-abs-error"] =
239+
std::max(m_fittingErrors.getDouble("diff-coeff-max-abs-error", 0.0),
240+
mxerr);
241+
m_fittingErrors["diff-coeff-max-rel-error"] =
242+
std::max(m_fittingErrors.getDouble("diff-coeff-max-rel-error", 0.0),
243+
mxrelerr);
238244

239245
if (m_log_level) {
240246
writelogf("Maximum binary diffusion coefficient absolute error:"

test/python/test_transport.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,19 @@ def test_mixDiffCoeffsChange(self, phase):
7171

7272
def test_CK_mode(self, phase):
7373
mu_ct = phase.viscosity
74+
err_ct = phase.transport_fitting_errors
7475
phase.transport_model = 'mixture-averaged-CK'
7576
assert phase.transport_model == 'mixture-averaged-CK'
7677
mu_ck = phase.viscosity
78+
err_ck = phase.transport_fitting_errors
7779
# values should be close, but not identical
7880
assert abs(mu_ct - mu_ck) / mu_ct > 1e-8
7981
assert abs(mu_ct - mu_ck) / mu_ct < 1e-2
8082

83+
# Cantera's fits should be an improvement in all cases
84+
for key in err_ct:
85+
assert err_ct[key] < err_ck[key]
86+
8187
def test_ionGas(self, phase):
8288
# IonGasTransport gives the same result for a mixture
8389
# without ionized species

0 commit comments

Comments
 (0)