Cantera 2.6.0

wrappers.h
1// This file is part of Cantera. See License.txt in the top-level directory or
2// at https://cantera.org/license.txt for license and copyright information.
3
4#include "cantera/base/logger.h"
5#include "cantera/numerics/eigen_sparse.h"
6#include "cantera/thermo/ThermoPhase.h"
7#include "cantera/transport/TransportBase.h"
8#include "cantera/kinetics/Kinetics.h"
9
10#include "Python.h"
11
12// Warning types supported by the Python C-API.
13// See https://docs.python.org/3/c-api/exceptions.html#issuing-warnings
14std::map<std::string, PyObject*> mapped_PyWarnings = {
15 {"", PyExc_Warning},
16 {"Bytes", PyExc_BytesWarning},
17 {"Cantera", PyExc_UserWarning}, // pre-existing warning
18 {"Deprecation", PyExc_DeprecationWarning},
19 {"Future", PyExc_FutureWarning},
20 {"Import", PyExc_ImportWarning},
21 {"PendingDeprecation", PyExc_PendingDeprecationWarning},
22 {"Resource", PyExc_ResourceWarning},
23 {"Runtime", PyExc_RuntimeWarning},
24 {"Syntax", PyExc_SyntaxWarning},
25 {"Unicode", PyExc_UnicodeWarning},
26 {"User", PyExc_UserWarning}
27};
28
29// Wrappers for preprocessor defines
30std::string get_cantera_version()
31{
32 return std::string(CANTERA_VERSION);
33}
34
35 int get_sundials_version()
36{
37 return CT_SUNDIALS_VERSION;
38}
39
40 class PythonLogger : public Cantera::Logger
41{
42 public:
43 virtual void write(const std::string& s) {
44 // 1000 bytes is the maximum size permitted by PySys_WriteStdout
45 static const size_t N = 999;
46 for (size_t i = 0; i < s.size(); i+=N) {
47 PySys_WriteStdout("%s", s.substr(i, N).c_str());
48 }
49 std::cout.flush();
50 }
51
52 virtual void writeendl() {
53 PySys_WriteStdout("%s", "\n");
54 std::cout.flush();
55 }
56
57 virtual void warn(const std::string& warning, const std::string& msg) {
58 if (mapped_PyWarnings.find(warning) != mapped_PyWarnings.end()) {
59 PyErr_WarnEx(mapped_PyWarnings[warning], msg.c_str(), 1);
60 } else {
61 // issue generic warning
62 PyErr_WarnEx(PyExc_Warning, msg.c_str(), 1);
63 }
64 }
65
66 virtual void error(const std::string& msg) {
67 PyErr_SetString(PyExc_RuntimeError, msg.c_str());
68 }
69};
70
71// Function for assigning elements of Array2D, since Cython has trouble
72// with assigning to the reference returned by operator()
73 void CxxArray2D_set(Cantera::Array2D& array, size_t i, size_t j, double value)
74{
75 array(i,j) = value;
76}
77
78// Service function to pass index/value triplets describing sparse matrix
79 size_t sparseTriplets(const Eigen::SparseMatrix<double>& mat,
80 int* rows, int* cols, double* data, size_t length)
81{
82 size_t count = 0;
83 for (int i = 0; i < mat.outerSize(); i++) {
84 for (Eigen::SparseMatrix<double>::InnerIterator it(mat, i); it; ++it) {
85 if (count < length) {
86 rows[count] = it.row();
87 cols[count] = it.col();
88 data[count] = it.value();
89 }
90 count++;
91 }
92 }
93 if (count > length) {
94 throw Cantera::CanteraError("sparseComponents",
95 "Output arrays have insufficient length. Required size is {}, "
96 "while provided length is {}.", count, length);
97 }
98 return count;
99}
100
101// Service function to pass CSC data describing sparse matrix
102 void sparseCscData(const Eigen::SparseMatrix<double>& mat,
103 double* value, int* inner, int* outer)
104{
105 if (!mat.isCompressed()) {
106 throw Cantera::CanteraError("sparseCscData",
107 "Invalid input: Eigen matrix is not compressed.");
108 }
109
110 const double* valuePtr = mat.valuePtr();
111 const int* innerPtr = mat.innerIndexPtr();
112 for (size_t i = 0; i < mat.nonZeros(); ++i) {
113 value[i] = valuePtr[i];
114 inner[i] = innerPtr[i];
115 }
116
117 const int* outerPtr = mat.outerIndexPtr();
118 for (size_t i = 0; i < mat.outerSize() + 1; ++i) {
119 outer[i] = outerPtr[i];
120 }
121}
122
123// Function which passes sparse matrix
124#define SPARSE_MATRIX(PREFIX, CLASS_NAME, FUNC_NAME) \
125 Eigen::SparseMatrix<double> PREFIX ## _ ## FUNC_NAME(Cantera::CLASS_NAME* object) \
126 { return object->FUNC_NAME(); }
127
128// Function which populates a 1D array
129#define ARRAY_FUNC(PREFIX, CLASS_NAME, FUNC_NAME) \
130 void PREFIX ## _ ## FUNC_NAME(Cantera::CLASS_NAME* object, double* data) \
131 { object->FUNC_NAME(data); }
132
133// function which takes a stride as the first argument and populates a 2D array
134#define ARRAY_FUNC2(PREFIX, CLASS_NAME, FUNC_NAME) \
135 void PREFIX ## _ ## FUNC_NAME(Cantera::CLASS_NAME* object, size_t dim, double* data) \
136 { object->FUNC_NAME(dim, data); }
137
138// Function which populates a 1D array, extra arguments
139#define ARRAY_POLY(PREFIX, CLASS_NAME, FUNC_NAME) \
140 void PREFIX ## _ ## FUNC_NAME(Cantera::CLASS_NAME* object, size_t i, double* data) \
141 { object->FUNC_NAME(i, data); }
142
143#define ARRAY_POLY_BINARY(PREFIX, CLASS_NAME, FUNC_NAME) \
144 void PREFIX ## _ ## FUNC_NAME(Cantera::CLASS_NAME* object, size_t i, size_t j, double* data) \
145 { object->FUNC_NAME(i, j, data); }
146
147#define THERMO_1D(FUNC_NAME) ARRAY_FUNC(thermo, ThermoPhase, FUNC_NAME)
148#define KIN_1D(FUNC_NAME) ARRAY_FUNC(kin, Kinetics, FUNC_NAME)
149#define KIN_SPARSE_MATRIX(FUNC_NAME) SPARSE_MATRIX(kin, Kinetics, FUNC_NAME)
150#define TRANSPORT_1D(FUNC_NAME) ARRAY_FUNC(tran, Transport, FUNC_NAME)
151#define TRANSPORT_2D(FUNC_NAME) ARRAY_FUNC2(tran, Transport, FUNC_NAME)
152#define TRANSPORT_POLY(FUNC_NAME) ARRAY_POLY(tran, Transport, FUNC_NAME)
153#define TRANSPORT_POLY_BINARY(FUNC_NAME) ARRAY_POLY_BINARY(tran, Transport, FUNC_NAME)
154
155THERMO_1D(getMassFractions)
156THERMO_1D(setMassFractions)
157THERMO_1D(getMoleFractions)
158THERMO_1D(setMoleFractions)
159THERMO_1D(getConcentrations)
160THERMO_1D(setConcentrations)
161
162THERMO_1D(getMolecularWeights)
163THERMO_1D(getCharges)
164THERMO_1D(getChemPotentials)
165THERMO_1D(getElectrochemPotentials)
166THERMO_1D(getPartialMolarEnthalpies)
167THERMO_1D(getPartialMolarEntropies)
168THERMO_1D(getPartialMolarIntEnergies)
169THERMO_1D(getPartialMolarCp)
170THERMO_1D(getPartialMolarVolumes)
171THERMO_1D(getEnthalpy_RT)
172THERMO_1D(getEntropy_R)
173THERMO_1D(getIntEnergy_RT)
174THERMO_1D(getGibbs_RT)
175THERMO_1D(getCp_R)
176THERMO_1D(getActivities)
177THERMO_1D(getActivityCoefficients)
178
179KIN_SPARSE_MATRIX(reactantStoichCoeffs)
180KIN_SPARSE_MATRIX(productStoichCoeffs)
181KIN_SPARSE_MATRIX(revProductStoichCoeffs)
182
183KIN_1D(getFwdRatesOfProgress)
184KIN_1D(getRevRatesOfProgress)
185KIN_1D(getNetRatesOfProgress)
186
187KIN_SPARSE_MATRIX(fwdRatesOfProgress_ddX)
188KIN_SPARSE_MATRIX(revRatesOfProgress_ddX)
189KIN_SPARSE_MATRIX(netRatesOfProgress_ddX)
190
191KIN_1D(getFwdRateConstants_ddT)
192KIN_1D(getFwdRateConstants_ddP)
193KIN_1D(getFwdRateConstants_ddC)
194
195KIN_1D(getFwdRatesOfProgress_ddT)
196KIN_1D(getRevRatesOfProgress_ddT)
197KIN_1D(getNetRatesOfProgress_ddT)
198
199KIN_1D(getFwdRatesOfProgress_ddP)
200KIN_1D(getRevRatesOfProgress_ddP)
201KIN_1D(getNetRatesOfProgress_ddP)
202
203KIN_1D(getFwdRatesOfProgress_ddC)
204KIN_1D(getRevRatesOfProgress_ddC)
205KIN_1D(getNetRatesOfProgress_ddC)
206
207KIN_1D(getEquilibriumConstants)
208KIN_1D(getFwdRateConstants)
209KIN_1D(getRevRateConstants)
210
211KIN_1D(getDeltaEnthalpy)
212KIN_1D(getDeltaGibbs)
213KIN_1D(getDeltaEntropy)
214KIN_1D(getDeltaSSEnthalpy)
215KIN_1D(getDeltaSSGibbs)
216KIN_1D(getDeltaSSEntropy)
217
218KIN_1D(getThirdBodyConcentrations)
219
220KIN_1D(getCreationRates)
221KIN_1D(getDestructionRates)
222KIN_1D(getNetProductionRates)
223
224KIN_SPARSE_MATRIX(creationRates_ddX)
225KIN_SPARSE_MATRIX(destructionRates_ddX)
226KIN_SPARSE_MATRIX(netProductionRates_ddX)
227
228KIN_1D(getCreationRates_ddT)
229KIN_1D(getDestructionRates_ddT)
230KIN_1D(getNetProductionRates_ddT)
231
232KIN_1D(getCreationRates_ddP)
233KIN_1D(getDestructionRates_ddP)
234KIN_1D(getNetProductionRates_ddP)
235
236KIN_1D(getCreationRates_ddC)
237KIN_1D(getDestructionRates_ddC)
238KIN_1D(getNetProductionRates_ddC)
239
240TRANSPORT_1D(getMixDiffCoeffs)
241TRANSPORT_1D(getMixDiffCoeffsMass)
242TRANSPORT_1D(getMixDiffCoeffsMole)
243TRANSPORT_1D(getThermalDiffCoeffs)
244TRANSPORT_1D(getSpeciesViscosities)
245TRANSPORT_1D(getMobilities)
246
247TRANSPORT_2D(getMultiDiffCoeffs)
248TRANSPORT_2D(getBinaryDiffCoeffs)
249
250TRANSPORT_POLY(getViscosityPolynomial)
251TRANSPORT_POLY(setViscosityPolynomial)
252TRANSPORT_POLY(getConductivityPolynomial)
253TRANSPORT_POLY(setConductivityPolynomial)
254TRANSPORT_POLY_BINARY(getBinDiffusivityPolynomial)
255TRANSPORT_POLY_BINARY(setBinDiffusivityPolynomial)
Kinetics.h
Base class for kinetics managers and also contains the kineticsmgr module documentation (see Kinetics...
ThermoPhase.h
Header file for class ThermoPhase, the base class for phases with thermodynamic properties,...
TransportBase.h
Headers for the Transport object, which is the virtual base class for all transport property evaluato...
Cantera::Array2D
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Definition: Array.h:30
Cantera::CanteraError
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:61
Cantera::Logger
Base class for 'loggers' that write text messages to log files.
Definition: logger.h:41
Cantera::Logger::writeendl
virtual void writeendl()
Write an end of line character and flush output.
Definition: logger.h:66
Cantera::Logger::write
virtual void write(const std::string &msg)
Write a log message.
Definition: logger.h:57
Cantera::Logger::error
virtual void error(const std::string &msg)
Write an error message and quit.
Definition: logger.h:91
Cantera::Logger::warn
virtual void warn(const std::string &warning, const std::string &msg)
Write a warning message.
Definition: logger.h:76
logger.h
Header for Base class for 'loggers' that write text messages to log files (see Writing messages to th...