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
17 changes: 17 additions & 0 deletions include/cantera/cython/wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,21 @@ 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_POLY(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_POLY_BINARY(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 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(FUNC_NAME) ARRAY_POLY(tran, Transport, FUNC_NAME)
#define TRANSPORT_POLY_BINARY(FUNC_NAME) ARRAY_POLY_BINARY(tran, Transport, FUNC_NAME)

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

TRANSPORT_2D(getMultiDiffCoeffs)
TRANSPORT_2D(getBinaryDiffCoeffs)

TRANSPORT_POLY(getViscosityPolynomial)
TRANSPORT_POLY(setViscosityPolynomial)
TRANSPORT_POLY(getConductivityPolynomial)
TRANSPORT_POLY(setConductivityPolynomial)
TRANSPORT_POLY_BINARY(getBinDiffusivityPolynomial)
TRANSPORT_POLY_BINARY(setBinDiffusivityPolynomial)
42 changes: 39 additions & 3 deletions include/cantera/transport/GasTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,49 @@ class GasTransport : public Transport

//! Return the polynomial fits to the viscosity of species i
//! @see fitProperties()
virtual void getViscosityPolynomials(size_t i, double* coeffs) const;
virtual void getViscosityPolynomial(size_t i, double* coeffs) const;

//! Return the temperature fits of the heat conductivity of species i
//! @see fitProperties()
virtual void getConductivityPolynomials(size_t i, double* coeffs) const;
virtual void getConductivityPolynomial(size_t i, double* coeffs) const;

//! Return the polynomial fits to the binary diffusivity of species pair (i, j)
//! @see fitDiffCoeffs()
virtual void getBinDiffusivityPolynomials(size_t i, size_t j, double* coeffs) const;
virtual void getBinDiffusivityPolynomial(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 getCollisionIntegralPolynomial(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 setViscosityPolynomial(size_t i, double* coeffs);

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

//! Modify the polynomial fits to the binary diffusivity of species pair (i, j)
//! @see fitDiffCoeffs()
virtual void setBinDiffusivityPolynomial(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 setCollisionIntegralPolynomial(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);

//! Boolean indicating the form of the transport properties polynomial fits.
//! Returns true if the Chemkin form is used.
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 +418,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_uses_actualT;

//! Fit for astar collision integral
/*!
Expand Down
3 changes: 0 additions & 3 deletions include/cantera/transport/MultiTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,6 @@ class MultiTransport : public GasTransport
//! Dense matrix for cstar
DenseMatrix m_cstar;

//! Dense matrix for omega22
DenseMatrix m_om22;

vector_fp m_cinternal;

vector_fp m_sqrt_eps_k;
Expand Down
52 changes: 52 additions & 0 deletions include/cantera/transport/TransportBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,52 @@ class Transport
throw NotImplementedError("Transport::getMixDiffCoeffsMass");
}

//! Return the polynomial fits to the viscosity of species i
virtual void getViscosityPolynomial(size_t i, double* coeffs) const{
throw NotImplementedError("Transport::getViscosityPolynomial");
}

//! Return the temperature fits of the heat conductivity of species i
virtual void getConductivityPolynomial(size_t i, double* coeffs) const{
throw NotImplementedError("Transport::getConductivityPolynomial");
}

//! Return the polynomial fits to the binary diffusivity of species pair (i, j)
virtual void getBinDiffusivityPolynomial(size_t i, size_t j, double* coeffs) const{
throw NotImplementedError("Transport::getBinDiffusivityPolynomial");
}

//! Return the polynomial fits to the collision integral of species pair (i, j)
virtual void getCollisionIntegralPolynomial(size_t i, size_t j,
double* astar_coeffs,
double* bstar_coeffs,
double* cstar_coeffs) const{
throw NotImplementedError("Transport::getCollisionIntegralPolynomial");
}

//! Modify the polynomial fits to the viscosity of species i
virtual void setViscosityPolynomial(size_t i, double* coeffs){
throw NotImplementedError("Transport::setViscosityPolynomial");
}

//! Modify the temperature fits of the heat conductivity of species i
virtual void setConductivityPolynomial(size_t i, double* coeffs){
throw NotImplementedError("Transport::setConductivityPolynomial");
}

//! Modify the polynomial fits to the binary diffusivity of species pair (i, j)
virtual void setBinDiffusivityPolynomial(size_t i, size_t j, double* coeffs){
throw NotImplementedError("Transport::setBinDiffusivityPolynomial");
}

//! Modify the polynomial fits to the collision integral of species pair (i, j)
virtual void setCollisionIntegralPolynomial(size_t i, size_t j,
double* astar_coeffs,
double* bstar_coeffs,
double* cstar_coeffs, bool flag){
throw NotImplementedError("Transport::setCollisionIntegralPolynomial");
}

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

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

//! Boolean indicating the form of the transport properties polynomial fits.
//! Returns true if the Chemkin form is used.
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
15 changes: 15 additions & 0 deletions interfaces/cython/cantera/_cantera.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -581,10 +581,13 @@ 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
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


cdef extern from "cantera/transport/DustyGasTransport.h" namespace "Cantera":
Expand Down Expand Up @@ -1096,10 +1099,20 @@ 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_getViscosityPolynomial(CxxTransport*, size_t, double*) except +translate_exception
cdef void tran_getConductivityPolynomial(CxxTransport*, size_t, double*) except +translate_exception
cdef void tran_getBinDiffusivityPolynomial(CxxTransport*, size_t, size_t, double*) except +translate_exception

cdef void tran_setViscosityPolynomial(CxxTransport*, size_t, double*) except +translate_exception
cdef void tran_setConductivityPolynomial(CxxTransport*, size_t, double*) except +translate_exception
cdef void tran_setBinDiffusivityPolynomial(CxxTransport*, size_t, size_t, double*) except +translate_exception

# typedefs
ctypedef void (*thermoMethod1d)(CxxThermoPhase*, double*) except +translate_exception
ctypedef void (*transportMethod1d)(CxxTransport*, double*) except +translate_exception
ctypedef void (*transportMethod2d)(CxxTransport*, size_t, double*) except +translate_exception
ctypedef void (*transportPolyMethod1i)(CxxTransport*, size_t, double*) except +translate_exception
ctypedef void (*transportPolyMethod2i)(CxxTransport*, size_t, size_t, double*) except +translate_exception
ctypedef void (*kineticsMethod1d)(CxxKinetics*, double*) except +translate_exception

# classes
Expand Down Expand Up @@ -1363,6 +1376,8 @@ cdef np.ndarray get_species_array(Kinetics kin, kineticsMethod1d method)
cdef np.ndarray get_reaction_array(Kinetics kin, kineticsMethod1d method)
cdef np.ndarray get_transport_1d(Transport tran, transportMethod1d method)
cdef np.ndarray get_transport_2d(Transport tran, transportMethod2d method)
cdef np.ndarray get_transport_polynomial(Transport tran, transportPolyMethod1i method, int index, int n_coeffs)
cdef np.ndarray get_binary_transport_polynomial(Transport tran, transportPolyMethod2i method, int indexi, int indexj, int n_coeffs)
cdef CxxIdealGasPhase* getIdealGasPhase(ThermoPhase phase) except *
cdef wrapSpeciesThermo(shared_ptr[CxxSpeciesThermo] spthermo)

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.get_viscosity_polynomial(self.phase.species_index('H2O'))
visc1_h2 = self.phase['H2'].species_viscosities[0]
mu_poly_h2 = self.phase.get_viscosity_polynomial(self.phase.species_index('H2'))
self.phase.set_viscosity_polynomial(self.phase.species_index('H2'), mu_poly_h2o)
visc2_h2 = self.phase['H2'].species_viscosities[0]
self.phase.set_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.get_thermal_conductivity_polynomial(self.phase.species_index('O2'))
self.phase.X = {'H2': 1}
cond1_h2 = self.phase.thermal_conductivity
lambda_poly_h2 = self.phase.get_thermal_conductivity_polynomial(self.phase.species_index('H2'))
self.phase.set_thermal_conductivity_polynomial(self.phase.species_index('H2'), lambda_poly_o2)
cond2_h2 = self.phase.thermal_conductivity
self.phase.set_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.get_binary_diff_coeffs_polynomial(1, 2)
bd_poly_23 = self.phase.get_binary_diff_coeffs_polynomial(2, 3)
self.phase.set_binary_diff_coeffs_polynomial(1, 2, bd_poly_23)
self.phase.set_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.set_binary_diff_coeffs_polynomial(1, 2, bd_poly_12)
self.phase.set_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
Loading