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

Gas transport polynomial fits in Python #1077

Merged
merged 8 commits into from
Oct 1, 2021
30 changes: 30 additions & 0 deletions include/cantera/cython/wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,32 @@ void CxxArray2D_set(Cantera::Array2D& array, size_t i, size_t j, double value)
void PREFIX ## _ ## FUNC_NAME(Cantera::CLASS_NAME* object, size_t dim, double* data) \
{ object->FUNC_NAME(dim, data); }

// Function which populates a 1D array, extra arguments
#define ARRAY_FUNC1_1SPEC(PREFIX, CLASS_NAME, FUNC_NAME) \
void PREFIX ## _ ## FUNC_NAME(Cantera::CLASS_NAME* object, size_t i, double* data) \
{ object->FUNC_NAME(i, data); }

#define ARRAY_FUNC1_2SPEC(PREFIX, CLASS_NAME, FUNC_NAME) \
void PREFIX ## _ ## FUNC_NAME(Cantera::CLASS_NAME* object, size_t i, size_t j, double* data) \
{ object->FUNC_NAME(i, j, data); }

#define ARRAY_FUNC3_2SPEC(PREFIX, CLASS_NAME, FUNC_NAME) \
void PREFIX ## _ ## FUNC_NAME(Cantera::CLASS_NAME* object, size_t i, size_t j, double* dataA, double* dataB, double* dataC) \
{ object->FUNC_NAME(i, j, dataA, dataB, dataC); }

#define ARRAY_FUNC3_2SPEC_BOOL(PREFIX, CLASS_NAME, FUNC_NAME) \
void PREFIX ## _ ## FUNC_NAME(Cantera::CLASS_NAME* object, size_t i, size_t j, double* dataA, double* dataB, double* dataC, bool flag) \
{ object->FUNC_NAME(i, j, dataA, dataB, dataC, flag); }
lavdwall marked this conversation as resolved.
Show resolved Hide resolved


#define THERMO_1D(FUNC_NAME) ARRAY_FUNC(thermo, ThermoPhase, FUNC_NAME)
#define KIN_1D(FUNC_NAME) ARRAY_FUNC(kin, Kinetics, FUNC_NAME)
#define TRANSPORT_1D(FUNC_NAME) ARRAY_FUNC(tran, Transport, FUNC_NAME)
#define TRANSPORT_2D(FUNC_NAME) ARRAY_FUNC2(tran, Transport, FUNC_NAME)
#define TRANSPORT_POLY_1SPECIE(FUNC_NAME) ARRAY_FUNC1_1SPEC(tran, Transport, FUNC_NAME)
#define TRANSPORT_POLY_2SPECIE(FUNC_NAME) ARRAY_FUNC1_2SPEC(tran, Transport, FUNC_NAME)
#define TRANSPORT_3POLY_2SPECIE(FUNC_NAME) ARRAY_FUNC3_2SPEC(tran, Transport, FUNC_NAME)
#define TRANSPORT_3POLY_2SPECIE_BOOL(FUNC_NAME) ARRAY_FUNC3_2SPEC_BOOL(tran, Transport, FUNC_NAME)
lavdwall marked this conversation as resolved.
Show resolved Hide resolved

THERMO_1D(getMassFractions)
THERMO_1D(setMassFractions)
Expand Down Expand Up @@ -117,3 +138,12 @@ TRANSPORT_1D(getMobilities)

TRANSPORT_2D(getMultiDiffCoeffs)
TRANSPORT_2D(getBinaryDiffCoeffs)

TRANSPORT_POLY_1SPECIE(getViscosityPolynomials)
TRANSPORT_POLY_1SPECIE(setViscosityPolynomials)
TRANSPORT_POLY_1SPECIE(getConductivityPolynomials)
TRANSPORT_POLY_1SPECIE(setConductivityPolynomials)
TRANSPORT_POLY_2SPECIE(getBinDiffusivityPolynomials)
TRANSPORT_POLY_2SPECIE(setBinDiffusivityPolynomials)
lavdwall marked this conversation as resolved.
Show resolved Hide resolved
TRANSPORT_3POLY_2SPECIE(getCollisionIntegralPolynomials)
TRANSPORT_3POLY_2SPECIE_BOOL(setCollisionIntegralPolynomials)
lavdwall marked this conversation as resolved.
Show resolved Hide resolved
30 changes: 30 additions & 0 deletions include/cantera/transport/GasTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,33 @@ class GasTransport : public Transport
//! @see fitDiffCoeffs()
virtual void getBinDiffusivityPolynomials(size_t i, size_t j, double* coeffs) const;

//! Return the polynomial fits to the collision integral of species pair (i, j)
//! @see fitCollisionIntegrals()
virtual void getCollisionIntegralPolynomials(size_t i, size_t j, double* astar_coeffs,
double* bstar_coeffs, double* cstar_coeffs) const;

//! Modify the polynomial fits to the viscosity of species i
//! @see fitProperties()
virtual void setViscosityPolynomials(size_t i, double* coeffs);

//! Modify the temperature fits of the heat conductivity of species i
//! @see fitProperties()
virtual void setConductivityPolynomials(size_t i, double* coeffs);

//! Modify the polynomial fits to the binary diffusivity of species pair (i, j)
//! @see fitDiffCoeffs()
virtual void setBinDiffusivityPolynomials(size_t i, size_t j, double* coeffs);

//! Modify the polynomial fits to the collision integral of species pair (i, j)
//! @see fitCollisionIntegrals()
virtual void setCollisionIntegralPolynomials(size_t i, size_t j, double* astar_coeffs,
double* bstar_coeffs, double* cstar_coeffs, bool actualT);

virtual void init(ThermoPhase* thermo, int mode=0, int log_level=0);

bool CKMode() const {
ischoegl marked this conversation as resolved.
Show resolved Hide resolved
return m_mode == CK_Mode;
}

protected:
GasTransport(ThermoPhase* thermo=0);
Expand Down Expand Up @@ -386,6 +412,10 @@ class GasTransport : public Transport
* (i,j).
*/
std::vector<vector_fp> m_omega22_poly;

//! Flag to indicate for which (i,j) interaction pairs the
//! actual temperature is used instead of the reduced temperature
std::vector<vector_int> m_star_poly_actualT;
ischoegl marked this conversation as resolved.
Show resolved Hide resolved

//! Fit for astar collision integral
/*!
Expand Down
39 changes: 39 additions & 0 deletions include/cantera/transport/TransportBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,41 @@ class Transport
throw NotImplementedError("Transport::getMixDiffCoeffsMass");
}

//! Get/set the polynomial fits to the transport properties
ischoegl marked this conversation as resolved.
Show resolved Hide resolved
virtual void getViscosityPolynomials(size_t i, double* coeffs) const{
throw NotImplementedError("Transport::getViscosityPolynomials");
}

virtual void getConductivityPolynomials(size_t i, double* coeffs) const{
throw NotImplementedError("Transport::getConductivityPolynomials");
}

virtual void getBinDiffusivityPolynomials(size_t i, size_t j, double* coeffs) const{
throw NotImplementedError("Transport::getBinDiffusivityPolynomials");
}

virtual void getCollisionIntegralPolynomials(size_t i, size_t j, double* astar_coeffs,
double* bstar_coeffs, double* cstar_coeffs) const{
throw NotImplementedError("Transport::getCollisionIntegralPolynomials");
}

virtual void setViscosityPolynomials(size_t i, double* coeffs){
throw NotImplementedError("Transport::setViscosityPolynomials");
}

virtual void setConductivityPolynomials(size_t i, double* coeffs){
throw NotImplementedError("Transport::setConductivityPolynomials");
}

virtual void setBinDiffusivityPolynomials(size_t i, size_t j, double* coeffs){
throw NotImplementedError("Transport::setBinDiffusivityPolynomials");
}

virtual void setCollisionIntegralPolynomials(size_t i, size_t j, double* astar_coeffs,
double* bstar_coeffs, double* cstar_coeffs, bool flag){
throw NotImplementedError("Transport::setCollisionIntegralPolynomials");
}

//! Set model parameters for derived classes
/*!
* This method may be derived in subclasses to set model-specific
Expand Down Expand Up @@ -667,6 +702,10 @@ class Transport

//! Set root Solution holding all phase information
virtual void setRoot(std::shared_ptr<Solution> root);

virtual bool CKMode() const {
ischoegl marked this conversation as resolved.
Show resolved Hide resolved
throw NotImplementedError("Transport::CK_Mode");
}

protected:
//! Enable the transport object for use.
Expand Down
11 changes: 11 additions & 0 deletions interfaces/cython/cantera/_cantera.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ cdef extern from "cantera/transport/TransportBase.h" namespace "Cantera":
cdef cppclass CxxTransport "Cantera::Transport":
CxxTransport(CxxThermoPhase*)
string transportType()
cbool CKMode()
double viscosity() except +translate_exception
double thermalConductivity() except +translate_exception
double electricalConductivity() except +translate_exception
Expand Down Expand Up @@ -1096,6 +1097,16 @@ cdef extern from "cantera/cython/wrappers.h":
cdef void tran_getMultiDiffCoeffs(CxxTransport*, size_t, double*) except +translate_exception
cdef void tran_getBinaryDiffCoeffs(CxxTransport*, size_t, double*) except +translate_exception

cdef void tran_getViscosityPolynomials(CxxTransport*, size_t, double*) except +translate_exception
cdef void tran_getConductivityPolynomials(CxxTransport*, size_t, double*) except +translate_exception
cdef void tran_getBinDiffusivityPolynomials(CxxTransport*, size_t, size_t, double*) except +translate_exception
cdef void tran_getCollisionIntegralPolynomials(CxxTransport*, size_t, size_t, double*, double*, double*) except +translate_exception
lavdwall marked this conversation as resolved.
Show resolved Hide resolved

cdef void tran_setViscosityPolynomials(CxxTransport*, size_t, double*) except +translate_exception
cdef void tran_setConductivityPolynomials(CxxTransport*, size_t, double*) except +translate_exception
cdef void tran_setBinDiffusivityPolynomials(CxxTransport*, size_t, size_t, double*) except +translate_exception
cdef void tran_setCollisionIntegralPolynomials(CxxTransport*, size_t, size_t, double*, double*, double*, bool) except +translate_exception
lavdwall marked this conversation as resolved.
Show resolved Hide resolved

# typedefs
ctypedef void (*thermoMethod1d)(CxxThermoPhase*, double*) except +translate_exception
ctypedef void (*transportMethod1d)(CxxTransport*, double*) except +translate_exception
Expand Down
48 changes: 47 additions & 1 deletion interfaces/cython/cantera/test/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,53 @@ def test_species_visosities(self):
self.phase.TP = 800, 5*ct.one_atm
self.assertNear(self.phase[species_name].species_viscosities[0],
visc)


def test_transport_polynomial_fits_viscosity(self):
visc1_h2o = self.phase['H2O'].species_viscosities[0]
mu_poly_h2o = self.phase.viscosity_polynomial(self.phase.species_index('H2O'))
visc1_h2 = self.phase['H2'].species_viscosities[0]
mu_poly_h2 = self.phase.viscosity_polynomial(self.phase.species_index('H2'))
self.phase.modify_viscosity_polynomial(self.phase.species_index('H2'), mu_poly_h2o)
visc2_h2 = self.phase['H2'].species_viscosities[0]
self.phase.modify_viscosity_polynomial(self.phase.species_index('H2'), mu_poly_h2)
visc3_h2 = self.phase['H2'].species_viscosities[0]
self.assertTrue(visc1_h2o != visc1_h2)
self.assertEqual(visc1_h2o, visc2_h2)
self.assertEqual(visc1_h2, visc3_h2)

def test_transport_polynomial_fits_conductivity(self):
self.phase.X = {'O2': 1}
cond1_o2 = self.phase.thermal_conductivity
lambda_poly_o2 = self.phase.thermal_conductivity_polynomial(self.phase.species_index('O2'))
self.phase.X = {'H2': 1}
cond1_h2 = self.phase.thermal_conductivity
lambda_poly_h2 = self.phase.thermal_conductivity_polynomial(self.phase.species_index('H2'))
self.phase.modify_thermal_conductivity_polynomial(self.phase.species_index('H2'), lambda_poly_o2)
cond2_h2 = self.phase.thermal_conductivity
self.phase.modify_thermal_conductivity_polynomial(self.phase.species_index('H2'), lambda_poly_h2)
cond3_h2 = self.phase.thermal_conductivity
self.assertTrue(cond1_o2 != cond1_h2)
self.assertEqual(cond1_o2, cond2_h2)
self.assertEqual(cond1_h2, cond3_h2)

def test_transport_polynomial_fits_diffusion(self):
D12 = self.phase.binary_diff_coeffs[1, 2]
D23 = self.phase.binary_diff_coeffs[2, 3]
bd_poly_12 = self.phase.binary_diff_coeffs_polynomial(1, 2)
bd_poly_23 = self.phase.binary_diff_coeffs_polynomial(2, 3)
self.phase.modify_binary_diff_coeffs_polynomial(1, 2, bd_poly_23)
self.phase.modify_binary_diff_coeffs_polynomial(2, 3, bd_poly_12)
D12mod = self.phase.binary_diff_coeffs[1, 2]
D23mod = self.phase.binary_diff_coeffs[2, 3]
self.phase.modify_binary_diff_coeffs_polynomial(1, 2, bd_poly_12)
self.phase.modify_binary_diff_coeffs_polynomial(2, 3, bd_poly_23)
D12new = self.phase.binary_diff_coeffs[1, 2]
D23new = self.phase.binary_diff_coeffs[2, 3]
self.assertTrue(D12 != D23)
self.assertEqual(D12, D23mod)
self.assertEqual(D23, D12mod)
self.assertEqual(D12, D12new)
self.assertEqual(D23, D23new)

class TestIonTransport(utilities.CanteraTest):
def setUp(self):
Expand Down
79 changes: 78 additions & 1 deletion interfaces/cython/cantera/transport.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ cdef np.ndarray get_transport_2d(Transport tran, transportMethod2d method):
method(tran.transport, kk, &data[0,0])
return data


cdef class GasTransportData:
"""
Transport data for a single gas-phase species which can be used in
Expand Down Expand Up @@ -182,6 +181,11 @@ cdef class Transport(_SolutionBase):
self.transport = newTransportMgr(stringify(model), self.thermo)
self._transport.reset(self.transport)

property CK_mode:
"""Boolean to indicate if the chemkin interpretation is used."""
def __get__(self):
return self.transport.CKMode()

property viscosity:
"""Viscosity [Pa-s]."""
def __get__(self):
Expand Down Expand Up @@ -256,6 +260,79 @@ cdef class Transport(_SolutionBase):
def __get__(self):
return get_transport_1d(self, tran_getMobilities)

def viscosity_polynomial(self, i):
"""Get the polynomial fit to the logarithm of temperature for
the viscosity of species i."""
cdef np.ndarray[np.double_t, ndim=1] data = np.empty(4 if self.transport.CKMode() else 5)
tran_getViscosityPolynomials(self.transport, i, &data[0])
return data
lavdwall marked this conversation as resolved.
Show resolved Hide resolved

def thermal_conductivity_polynomial(self, i):
"""Get the polynomial fit to the logarithm of temperature for
the thermal conductivity of species i."""
cdef np.ndarray[np.double_t, ndim=1] data = np.empty(4 if self.transport.CKMode() else 5)
tran_getConductivityPolynomials(self.transport, i, &data[0])
return data

def binary_diff_coeffs_polynomial(self, i, j):
"""Get the polynomial fit to the logarithm of temperature for
the binary diffusion coefficient of species i and j."""
cdef np.ndarray[np.double_t, ndim=1] data = np.empty(4 if self.transport.CKMode() else 5)
tran_getBinDiffusivityPolynomials(self.transport, i, j, &data[0])
return data

def collision_integral_polynomials(self, i, j):
"""Get the polynomial fit to the logarithm of temperature for
the collision integral of species i and j."""
cdef np.ndarray[np.double_t, ndim=1] adata = np.empty(7 if self.transport.CKMode() else 9)
cdef np.ndarray[np.double_t, ndim=1] bdata = np.empty(7 if self.transport.CKMode() else 9)
cdef np.ndarray[np.double_t, ndim=1] cdata = np.empty(7 if self.transport.CKMode() else 9)
tran_getCollisionIntegralPolynomials(self.transport, i, j, &adata[0], &bdata[0], &cdata[0])
return adata, bdata, cdata

def modify_viscosity_polynomial(self, i, values):
lavdwall marked this conversation as resolved.
Show resolved Hide resolved
"""Set the polynomial fit to the logarithm of temperature for
the viscosity of species i."""
if len(values) != (4 if self.transport.CKMode() else 5):
msg = "Got {}. Expected {}".format(len(values), (4 if self.transport.CKMode() else 5))
raise ValueError('Array has incorrect length. ' + msg + '.')
ischoegl marked this conversation as resolved.
Show resolved Hide resolved
cdef np.ndarray[np.double_t, ndim=1] data = np.ascontiguousarray(values, dtype=np.double)
tran_setViscosityPolynomials(self.transport, i, &data[0])

def modify_thermal_conductivity_polynomial(self, i, values):
"""Set the polynomial fit to the logarithm of temperature for
the thermal conductivity of species i."""
if len(values) != (4 if self.transport.CKMode() else 5):
msg = "Got {}. Expected {}".format(len(values), (4 if self.transport.CKMode() else 5))
raise ValueError('Array has incorrect length. ' + msg + '.')
cdef np.ndarray[np.double_t, ndim=1] data = np.ascontiguousarray(values, dtype=np.double)
tran_setConductivityPolynomials(self.transport, i, &data[0])

def modify_binary_diff_coeffs_polynomial(self, i, j, values):
"""Set the polynomial fit to the logarithm of temperature for
the binary diffusion coefficient of species i and j."""
if len(values) != (4 if self.transport.CKMode() else 5):
msg = "Got {}. Expected {}".format(len(values), (4 if self.transport.CKMode() else 5))
raise ValueError('Array has incorrect length. ' + msg + '.')
cdef np.ndarray[np.double_t, ndim=1] data = np.ascontiguousarray(values, dtype=np.double)
tran_setBinDiffusivityPolynomials(self.transport, i, j, &data[0])

def modify_collision_integral_polynomials(self, i, j, avalues, bvalues, cvalues, actualT=True):
"""Get the polynomial fit to the logarithm of temperature for
the collision integral of species i and j."""
if len(avalues) != (7 if self.transport.CKMode() else 9):
msg = "Got {}. Expected {}".format(len(avalues), (7 if self.transport.CKMode() else 9))
raise ValueError('Array has incorrect length. ' + msg + '.')
if len(bvalues) != (7 if self.transport.CKMode() else 9):
msg = "Got {}. Expected {}".format(len(bvalues), (7 if self.transport.CKMode() else 9))
raise ValueError('Array has incorrect length. ' + msg + '.')
if len(cvalues) != (7 if self.transport.CKMode() else 9):
msg = "Got {}. Expected {}".format(len(cvalues), (7 if self.transport.CKMode() else 9))
raise ValueError('Array has incorrect length. ' + msg + '.')
cdef np.ndarray[np.double_t, ndim=1] adata = np.ascontiguousarray(avalues, dtype=np.double)
cdef np.ndarray[np.double_t, ndim=1] bdata = np.ascontiguousarray(bvalues, dtype=np.double)
cdef np.ndarray[np.double_t, ndim=1] cdata = np.ascontiguousarray(cvalues, dtype=np.double)
tran_setCollisionIntegralPolynomials(self.transport, i, j, &adata[0], &bdata[0], &cdata[0], actualT)

cdef class DustyGasTransport(Transport):
"""
Expand Down
Loading