From cf8d2efa090a5c8f76579db730b34bb9e0dc745d Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Fri, 20 Sep 2019 13:07:24 -0500 Subject: [PATCH] [SpeciesThermo] assign nCoeffs in C++ layer --- include/cantera/thermo/ConstCpPoly.h | 3 ++ include/cantera/thermo/Mu0Poly.h | 3 ++ .../cantera/thermo/Nasa9PolyMultiTempRegion.h | 2 ++ include/cantera/thermo/NasaPoly2.h | 2 ++ include/cantera/thermo/ShomatePoly.h | 2 ++ .../cantera/thermo/SpeciesThermoInterpType.h | 6 ++++ interfaces/cython/cantera/_cantera.pxd | 1 + interfaces/cython/cantera/speciesthermo.pyx | 34 ++++++++++++++----- src/thermo/Mu0Poly.cpp | 5 +++ src/thermo/Nasa9PolyMultiTempRegion.cpp | 5 +++ src/thermo/SpeciesThermoInterpType.cpp | 6 ++++ 11 files changed, 61 insertions(+), 8 deletions(-) diff --git a/include/cantera/thermo/ConstCpPoly.h b/include/cantera/thermo/ConstCpPoly.h index b753b9c22..f7f55cb19 100644 --- a/include/cantera/thermo/ConstCpPoly.h +++ b/include/cantera/thermo/ConstCpPoly.h @@ -86,6 +86,9 @@ public: void updatePropertiesTemp(const doublereal temp, doublereal* cp_R, doublereal* h_RT, doublereal* s_R) const; + + size_t nCoeffs() const { return 4; } + void reportParameters(size_t& n, int& type, doublereal& tlow, doublereal& thigh, doublereal& pref, diff --git a/include/cantera/thermo/Mu0Poly.h b/include/cantera/thermo/Mu0Poly.h index caa20e0ac..525442f05 100644 --- a/include/cantera/thermo/Mu0Poly.h +++ b/include/cantera/thermo/Mu0Poly.h @@ -127,6 +127,9 @@ public: doublereal* cp_R, doublereal* h_RT, doublereal* s_R) const; + + virtual size_t nCoeffs() const; + virtual void reportParameters(size_t& n, int& type, doublereal& tlow, doublereal& thigh, doublereal& pref, diff --git a/include/cantera/thermo/Nasa9PolyMultiTempRegion.h b/include/cantera/thermo/Nasa9PolyMultiTempRegion.h index 0b3baa927..55bc4a40e 100644 --- a/include/cantera/thermo/Nasa9PolyMultiTempRegion.h +++ b/include/cantera/thermo/Nasa9PolyMultiTempRegion.h @@ -75,6 +75,8 @@ public: doublereal* cp_R, doublereal* h_RT, doublereal* s_R) const; + virtual size_t nCoeffs() const; + //! This utility function reports back the type of parameterization and all //! of the parameters for the species, index. /*! diff --git a/include/cantera/thermo/NasaPoly2.h b/include/cantera/thermo/NasaPoly2.h index fa518ae7f..d8dbf5bf8 100644 --- a/include/cantera/thermo/NasaPoly2.h +++ b/include/cantera/thermo/NasaPoly2.h @@ -123,6 +123,8 @@ public: } } + size_t nCoeffs() const { return 15; } + void reportParameters(size_t& n, int& type, doublereal& tlow, doublereal& thigh, doublereal& pref, diff --git a/include/cantera/thermo/ShomatePoly.h b/include/cantera/thermo/ShomatePoly.h index 7489df91b..c904118fc 100644 --- a/include/cantera/thermo/ShomatePoly.h +++ b/include/cantera/thermo/ShomatePoly.h @@ -306,6 +306,8 @@ public: } } + virtual size_t nCoeffs() const { return 15; } + virtual void reportParameters(size_t& n, int& type, doublereal& tlow, doublereal& thigh, doublereal& pref, diff --git a/include/cantera/thermo/SpeciesThermoInterpType.h b/include/cantera/thermo/SpeciesThermoInterpType.h index 763b85cdc..0c56f3a3e 100644 --- a/include/cantera/thermo/SpeciesThermoInterpType.h +++ b/include/cantera/thermo/SpeciesThermoInterpType.h @@ -206,6 +206,12 @@ public: doublereal* h_RT, doublereal* s_R) const; + + //! This utility function reports back the number of coefficients + //! for a given type of species parameterization + virtual size_t nCoeffs() const; + + //! This utility function reports back the type of parameterization and all //! of the parameters for the species. /*! diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index 02a1fb121..9fbd48b6c 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -96,6 +96,7 @@ cdef extern from "cantera/thermo/SpeciesThermoInterpType.h": double maxTemp() double refPressure() void reportParameters(size_t&, int&, double&, double&, double&, double* const) except +translate_exception + int nCoeffs() except +translate_exception cdef extern from "cantera/thermo/SpeciesThermoFactory.h": cdef CxxSpeciesThermo* CxxNewSpeciesThermo "Cantera::newSpeciesThermoInterpType"\ diff --git a/interfaces/cython/cantera/speciesthermo.pyx b/interfaces/cython/cantera/speciesthermo.pyx index 0de508904..0fa06df1e 100644 --- a/interfaces/cython/cantera/speciesthermo.pyx +++ b/interfaces/cython/cantera/speciesthermo.pyx @@ -32,7 +32,7 @@ cdef class SpeciesThermo: if not init: return - if len(coeffs) != self.n_coeffs: + if not self.check_n_coeffs(len(coeffs)): raise ValueError("Coefficient array has incorrect length") cdef np.ndarray[np.double_t, ndim=1] data = np.ascontiguousarray( coeffs, dtype=np.double) @@ -59,6 +59,11 @@ cdef class SpeciesThermo: def __get__(self): return self.spthermo.refPressure() + property n_coeffs: + """ Number of parameters for the parameterization.""" + def __get__(self): + return self.spthermo.nCoeffs() + property coeffs: """ Array of coefficients for the parameterization. The length of this @@ -74,6 +79,9 @@ cdef class SpeciesThermo: T_high, P_ref, &data[0]) return data + def check_n_coeffs(self, n): + raise NotImplementedError('Needs to be overloaded') + def cp(self, T): """ Molar heat capacity at constant pressure [J/kmol/K] at temperature *T*. @@ -109,13 +117,15 @@ cdef class ConstantCp(SpeciesThermo): - `coeffs[3]` = :math:`c_p^o(T_0, p_{ref})` [J/kmol-K] """ derived_type = SPECIES_THERMO_CONSTANT_CP - n_coeffs = 4 + + def check_n_coeffs(self, n): + return n == 4 cdef class Mu0Poly(SpeciesThermo): """ Thermodynamic properties for a species which is parameterized using an - interpolation of the Gibbs free energy based on a piecewise constant heat + interpolation of the Gibbs free energy based on a piecewise constant heat capacity approximation. This is a wrapper for the C++ class :ct:`Mu0Poly`. :param coeffs: @@ -130,7 +140,9 @@ cdef class Mu0Poly(SpeciesThermo): - ... """ derived_type = SPECIES_THERMO_MU0_INTERP - n_coeffs = 2 + + def check_n_coeffs(self, n): + return n > 3 and n % 2 == 0 cdef class NasaPoly2(SpeciesThermo): @@ -153,13 +165,15 @@ cdef class NasaPoly2(SpeciesThermo): input files. """ derived_type = SPECIES_THERMO_NASA2 - n_coeffs = 15 + + def check_n_coeffs(self, n): + return n == 15 cdef class Nasa9PolyMultiTempRegion(SpeciesThermo): """ Thermodynamic properties for a species which is parameterized using the - 9-coefficient NASA polynomial form encompassing multiple temperature ranges. + 9-coefficient NASA polynomial form encompassing multiple temperature ranges. This is a wrapper for the C++ class :ct:`Nasa9PolyMultiTempRegion`. :param coeffs: @@ -176,7 +190,9 @@ cdef class Nasa9PolyMultiTempRegion(SpeciesThermo): as in the NIST Chemistry WebBook). """ derived_type = SPECIES_THERMO_NASA9MULTITEMP - n_coeffs = 11 + + def check_n_coeffs(self, n): + return n > 11 and ((n - 1) % 11) == 0 cdef class ShomatePoly2(SpeciesThermo): @@ -200,7 +216,9 @@ cdef class ShomatePoly2(SpeciesThermo): as in the NIST Chemistry WebBook). """ derived_type = SPECIES_THERMO_SHOMATE2 - n_coeffs = 15 + + def check_n_coeffs(self, n): + return n == 15 cdef wrapSpeciesThermo(shared_ptr[CxxSpeciesThermo] spthermo): diff --git a/src/thermo/Mu0Poly.cpp b/src/thermo/Mu0Poly.cpp index eef61c9e1..2620ec5dc 100644 --- a/src/thermo/Mu0Poly.cpp +++ b/src/thermo/Mu0Poly.cpp @@ -129,6 +129,11 @@ void Mu0Poly::updatePropertiesTemp(const doublereal T, updateProperties(&T, cp_R, h_RT, s_R); } +size_t Mu0Poly::nCoeffs() const +{ + return 2*m_numIntervals + 2; +} + void Mu0Poly::reportParameters(size_t& n, int& type, doublereal& tlow, doublereal& thigh, doublereal& pref, diff --git a/src/thermo/Nasa9PolyMultiTempRegion.cpp b/src/thermo/Nasa9PolyMultiTempRegion.cpp index 3c96c5a8d..7ea0519a6 100644 --- a/src/thermo/Nasa9PolyMultiTempRegion.cpp +++ b/src/thermo/Nasa9PolyMultiTempRegion.cpp @@ -127,6 +127,11 @@ void Nasa9PolyMultiTempRegion::updatePropertiesTemp(const doublereal temp, m_regionPts[m_currRegion]->updatePropertiesTemp(temp, cp_R, h_RT, s_R); } +size_t Nasa9PolyMultiTempRegion::nCoeffs() const +{ + return 11*m_regionPts.size() + 1; +} + void Nasa9PolyMultiTempRegion::reportParameters(size_t& n, int& type, doublereal& tlow, doublereal& thigh, doublereal& pref, diff --git a/src/thermo/SpeciesThermoInterpType.cpp b/src/thermo/SpeciesThermoInterpType.cpp index b24bcb65f..3edaf9e53 100644 --- a/src/thermo/SpeciesThermoInterpType.cpp +++ b/src/thermo/SpeciesThermoInterpType.cpp @@ -41,6 +41,12 @@ void SpeciesThermoInterpType::updatePropertiesTemp(const double temp, "Not implemented"); } +size_t SpeciesThermoInterpType::nCoeffs() const +{ + throw CanteraError("SpeciesThermoInterpType::nCoeffs", + "Not implemented"); +} + void SpeciesThermoInterpType::reportParameters(size_t& index, int& type, double& minTemp, double& maxTemp, double& refPressure, double* const coeffs) const