7 #ifndef CT_STOICH_MGR_H
8 #define CT_STOICH_MGR_H
129 static doublereal ppow(doublereal x, doublereal order)
132 return std::pow(x, order);
139 inline static std::string
fmt(
const std::string& r,
size_t n)
141 return r +
"[" +
int2str(n) +
"]";
153 C1(
size_t rxn = 0,
size_t ic0 = 0) :
158 size_t data(std::vector<size_t>& ic) {
164 void incrementSpecies(
const doublereal* R, doublereal* S)
const {
168 void decrementSpecies(
const doublereal* R, doublereal* S)
const {
172 void multiply(
const doublereal* S, doublereal* R)
const {
176 void incrementReaction(
const doublereal* S, doublereal* R)
const {
180 void decrementReaction(
const doublereal* S, doublereal* R)
const {
184 size_t rxnNumber()
const {
187 size_t speciesIndex(
size_t n)
const {
195 void writeMultiply(
const std::string& r, std::map<size_t, std::string>& out) {
234 C2(
size_t rxn = 0,
size_t ic0 = 0,
size_t ic1 = 0)
237 size_t data(std::vector<size_t>& ic) {
244 void incrementSpecies(
const doublereal* R, doublereal* S)
const {
246 S[m_ic1] += R[
m_rxn];
249 void decrementSpecies(
const doublereal* R, doublereal* S)
const {
251 S[m_ic1] -= R[
m_rxn];
254 void multiply(
const doublereal* S, doublereal* R)
const {
258 void incrementReaction(
const doublereal* S, doublereal* R)
const {
262 void decrementReaction(
const doublereal* S, doublereal* R)
const {
266 size_t rxnNumber()
const {
269 size_t speciesIndex(
size_t n)
const {
270 return (n == 0 ?
m_ic0 : m_ic1);
277 void writeMultiply(
const std::string& r, std::map<size_t, std::string>& out) {
293 std::string s =
" + "+
fmt(r,
m_rxn);
300 std::string s =
" - "+
fmt(r,
m_rxn);
321 C3(
size_t rxn = 0,
size_t ic0 = 0,
size_t ic1 = 0,
size_t ic2 = 0)
322 : m_rxn(rxn), m_ic0(ic0), m_ic1(ic1), m_ic2(ic2) {}
324 size_t data(std::vector<size_t>& ic) {
332 void incrementSpecies(
const doublereal* R, doublereal* S)
const {
333 S[m_ic0] += R[m_rxn];
334 S[m_ic1] += R[m_rxn];
335 S[m_ic2] += R[m_rxn];
338 void decrementSpecies(
const doublereal* R, doublereal* S)
const {
339 S[m_ic0] -= R[m_rxn];
340 S[m_ic1] -= R[m_rxn];
341 S[m_ic2] -= R[m_rxn];
344 void multiply(
const doublereal* S, doublereal* R)
const {
345 R[m_rxn] *= S[m_ic0] * S[m_ic1] * S[m_ic2];
348 void incrementReaction(
const doublereal* S, doublereal* R)
const {
349 R[m_rxn] += S[m_ic0] + S[m_ic1] + S[m_ic2];
352 void decrementReaction(
const doublereal* S, doublereal* R)
const {
353 R[m_rxn] -= (S[m_ic0] + S[m_ic1] + S[m_ic2]);
356 size_t rxnNumber()
const {
359 size_t speciesIndex(
size_t n)
const {
360 return (n == 0 ? m_ic0 : (n == 1 ? m_ic1 : m_ic2));
367 void writeMultiply(
const std::string& r, std::map<size_t, std::string>& out) {
368 out[m_rxn] =
fmt(r, m_ic0) +
" * " +
fmt(r, m_ic1) +
" * " +
fmt(r, m_ic2);
373 out[m_rxn] +=
" + "+
fmt(r, m_ic0)+
" + "+
fmt(r, m_ic1)+
" + "+
fmt(r, m_ic2);
378 out[m_rxn] +=
" - "+
fmt(r, m_ic0)+
" - "+
fmt(r, m_ic1)+
" - "+
fmt(r, m_ic2);
383 std::string s =
" + "+
fmt(r, m_rxn);
391 std::string s =
" - "+
fmt(r, m_rxn);
424 for (
size_t n = 0; n <
m_n; n++) {
431 size_t data(std::vector<size_t>& ic) {
433 for (
size_t n = 0; n <
m_n; n++) {
439 doublereal order(
size_t n)
const {
442 doublereal stoich(
size_t n)
const {
445 size_t speciesIndex(
size_t n)
const {
449 void multiply(
const doublereal* input, doublereal* output)
const {
451 for (
size_t n = 0; n <
m_n; n++) {
454 output[
m_rxn] *= ppow(input[
m_ic[n]], oo);
459 void incrementSpecies(
const doublereal* input,
460 doublereal* output)
const {
461 doublereal x = input[
m_rxn];
462 for (
size_t n = 0; n <
m_n; n++) {
467 void decrementSpecies(
const doublereal* input,
468 doublereal* output)
const {
469 doublereal x = input[
m_rxn];
470 for (
size_t n = 0; n <
m_n; n++) {
475 void incrementReaction(
const doublereal* input,
476 doublereal* output)
const {
477 for (
size_t n = 0; n <
m_n; n++) output[
m_rxn]
481 void decrementReaction(
const doublereal* input,
482 doublereal* output)
const {
483 for (
size_t n = 0; n <
m_n; n++) output[
m_rxn]
488 void writeMultiply(
const std::string& r, std::map<size_t, std::string>& out) {
490 for (
size_t n = 0; n <
m_n; n++) {
504 for (
size_t n = 0; n <
m_n; n++) {
511 for (
size_t n = 0; n <
m_n; n++) {
519 for (
size_t n = 0; n <
m_n; n++) {
527 for (
size_t n = 0; n <
m_n; n++) {
574 template<
class InputIter,
class Vec1,
class Vec2>
575 inline static void _multiply(InputIter begin, InputIter end,
576 const Vec1& input, Vec2& output)
578 for (; begin != end; ++begin) {
579 begin->multiply(input, output);
583 template<
class InputIter,
class Vec1,
class Vec2>
584 inline static void _incrementSpecies(InputIter begin,
585 InputIter end,
const Vec1& input, Vec2& output)
587 for (; begin != end; ++begin) {
588 begin->incrementSpecies(input, output);
592 template<
class InputIter,
class Vec1,
class Vec2>
593 inline static void _decrementSpecies(InputIter begin,
594 InputIter end,
const Vec1& input, Vec2& output)
596 for (; begin != end; ++begin) {
597 begin->decrementSpecies(input, output);
601 template<
class InputIter,
class Vec1,
class Vec2>
602 inline static void _incrementReactions(InputIter begin,
603 InputIter end,
const Vec1& input, Vec2& output)
605 for (; begin != end; ++begin) {
606 begin->incrementReaction(input, output);
610 template<
class InputIter,
class Vec1,
class Vec2>
611 inline static void _decrementReactions(InputIter begin,
612 InputIter end,
const Vec1& input, Vec2& output)
614 for (; begin != end; ++begin) {
615 begin->decrementReaction(input, output);
620 template<
class InputIter>
622 const std::string& r, std::map<size_t, std::string>& out)
624 for (; begin != end; ++begin) {
625 begin->writeIncrementSpecies(r, out);
630 template<
class InputIter>
632 const std::string& r, std::map<size_t, std::string>& out)
634 for (; begin != end; ++begin) {
635 begin->writeDecrementSpecies(r, out);
640 template<
class InputIter>
642 const std::string& r, std::map<size_t, std::string>& out)
644 for (; begin != end; ++begin) {
645 begin->writeIncrementReaction(r, out);
650 template<
class InputIter>
652 const std::string& r, std::map<size_t, std::string>& out)
654 for (; begin != end; ++begin) {
655 begin->writeDecrementReaction(r, out);
660 template<
class InputIter>
662 const std::string& r, std::map<size_t, std::string>& out)
664 for (; begin != end; ++begin) {
665 begin->writeMultiply(r, out);
736 void add(
size_t rxn,
const std::vector<size_t>& k) {
739 add(rxn, k, order, stoich);
742 void add(
size_t rxn,
const std::vector<size_t>& k,
const vector_fp& order) {
744 add(rxn, k, order, stoich);
764 void add(
size_t rxn,
const std::vector<size_t>& k,
const vector_fp& order,
766 if (order.size() != k.size()) {
767 throw CanteraError(
"StoichManagerN::add()",
"size of order and species arrays differ");
769 if (stoich.size() != k.size()) {
770 throw CanteraError(
"StoichManagerN::add()",
"size of stoich and species arrays differ");
773 for (
size_t n = 0; n < stoich.size(); n++) {
774 if (fmod(stoich[n], 1.0) || stoich[n] != order[n]) {
779 if (frac || k.size() > 3) {
780 m_cn_list.push_back(C_AnyN(rxn, k, order, stoich));
786 std::vector<size_t> kRep;
787 for (
size_t n = 0; n < k.size(); n++) {
788 for (
size_t i = 0; i < stoich[n]; i++)
789 kRep.push_back(k[n]);
792 switch (kRep.size()) {
794 m_c1_list.push_back(C1(rxn, kRep[0]));
797 m_c2_list.push_back(C2(rxn, kRep[0], kRep[1]));
800 m_c3_list.push_back(C3(rxn, kRep[0], kRep[1], kRep[2]));
803 m_cn_list.push_back(C_AnyN(rxn, k, order, stoich));
808 void multiply(
const doublereal* input, doublereal* output)
const {
809 _multiply(m_c1_list.begin(), m_c1_list.end(), input, output);
810 _multiply(m_c2_list.begin(), m_c2_list.end(), input, output);
811 _multiply(m_c3_list.begin(), m_c3_list.end(), input, output);
812 _multiply(m_cn_list.begin(), m_cn_list.end(), input, output);
815 void incrementSpecies(
const doublereal* input, doublereal* output)
const {
816 _incrementSpecies(m_c1_list.begin(), m_c1_list.end(), input, output);
817 _incrementSpecies(m_c2_list.begin(), m_c2_list.end(), input, output);
818 _incrementSpecies(m_c3_list.begin(), m_c3_list.end(), input, output);
819 _incrementSpecies(m_cn_list.begin(), m_cn_list.end(), input, output);
822 void decrementSpecies(
const doublereal* input, doublereal* output)
const {
823 _decrementSpecies(m_c1_list.begin(), m_c1_list.end(), input, output);
824 _decrementSpecies(m_c2_list.begin(), m_c2_list.end(), input, output);
825 _decrementSpecies(m_c3_list.begin(), m_c3_list.end(), input, output);
826 _decrementSpecies(m_cn_list.begin(), m_cn_list.end(), input, output);
829 void incrementReactions(
const doublereal* input, doublereal* output)
const {
830 _incrementReactions(m_c1_list.begin(), m_c1_list.end(), input, output);
831 _incrementReactions(m_c2_list.begin(), m_c2_list.end(), input, output);
832 _incrementReactions(m_c3_list.begin(), m_c3_list.end(), input, output);
833 _incrementReactions(m_cn_list.begin(), m_cn_list.end(), input, output);
836 void decrementReactions(
const doublereal* input, doublereal* output)
const {
837 _decrementReactions(m_c1_list.begin(), m_c1_list.end(), input, output);
838 _decrementReactions(m_c2_list.begin(), m_c2_list.end(), input, output);
839 _decrementReactions(m_c3_list.begin(), m_c3_list.end(), input, output);
840 _decrementReactions(m_cn_list.begin(), m_cn_list.end(), input, output);
844 void writeIncrementSpecies(
const std::string& r, std::map<size_t, std::string>& out) {
852 void writeDecrementSpecies(
const std::string& r, std::map<size_t, std::string>& out) {
860 void writeIncrementReaction(
const std::string& r, std::map<size_t, std::string>& out) {
868 void writeDecrementReaction(
const std::string& r, std::map<size_t, std::string>& out) {
876 void writeMultiply(
const std::string& r, std::map<size_t, std::string>& out) {
884 std::vector<C1> m_c1_list;
885 std::vector<C2> m_c2_list;
886 std::vector<C3> m_c3_list;
887 std::vector<C_AnyN> m_cn_list;
size_t m_n
Length of the m_ic vector.
std::string int2str(const int n, const std::string &fmt)
Convert an int to a string using a format converter.
void writeIncrementReaction(const std::string &r, std::map< size_t, std::string > &out)
void writeMultiply(const std::string &r, std::map< size_t, std::string > &out)
void writeDecrementReaction(const std::string &r, std::map< size_t, std::string > &out)
static void _writeIncrementSpecies(InputIter begin, InputIter end, const std::string &r, std::map< size_t, std::string > &out)
size_t m_rxn
Reaction index -> index into the ROP vector.
const size_t npos
index returned by functions to indicate "no position"
size_t m_ic0
Species number.
void writeIncrementReaction(const std::string &r, std::map< size_t, std::string > &out)
static std::string fmt(const std::string &r, size_t n)
void writeMultiply(const std::string &r, std::map< size_t, std::string > &out)
void writeDecrementSpecies(const std::string &r, std::map< size_t, std::string > &out)
void writeIncrementReaction(const std::string &r, std::map< size_t, std::string > &out)
void writeIncrementSpecies(const std::string &r, std::map< size_t, std::string > &out)
static void _writeIncrementReaction(InputIter begin, InputIter end, const std::string &r, std::map< size_t, std::string > &out)
void writeDecrementReaction(const std::string &r, std::map< size_t, std::string > &out)
size_t m_rxn
Reaction number.
void writeMultiply(const std::string &r, std::map< size_t, std::string > &out)
size_t m_ic0
Species index -> index into the species vector for the two species.
static void _writeDecrementReaction(InputIter begin, InputIter end, const std::string &r, std::map< size_t, std::string > &out)
Handles two species in a single reaction.
std::vector< size_t > m_ic
Vector of species which are involved with this stoichiometric manager calculations.
Handles any number of species in a reaction, including fractional stoichiometric coefficients, and arbitrary reaction orders.
std::string fp2str(const double x, const std::string &fmt)
Convert a double into a c++ string.
void multiply(const DenseMatrix &A, const double *const b, double *const prod)
Multiply A*b and return the result in prod. Uses BLAS routine DGEMV.
void writeDecrementSpecies(const std::string &r, std::map< size_t, std::string > &out)
void writeDecrementReaction(const std::string &r, std::map< size_t, std::string > &out)
void writeIncrementReaction(const std::string &r, std::map< size_t, std::string > &out)
void writeIncrementSpecies(const std::string &r, std::map< size_t, std::string > &out)
void writeMultiply(const std::string &r, std::map< size_t, std::string > &out)
Handles one species in a reaction.
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
void writeDecrementSpecies(const std::string &r, std::map< size_t, std::string > &out)
Contains declarations for string manipulation functions within Cantera.
Handles three species in a reaction.
void writeDecrementReaction(const std::string &r, std::map< size_t, std::string > &out)
static void _writeMultiply(InputIter begin, InputIter end, const std::string &r, std::map< size_t, std::string > &out)
void writeIncrementSpecies(const std::string &r, std::map< size_t, std::string > &out)
size_t m_rxn
ID of the reaction corresponding to this stoichiometric manager.
vector_fp m_stoich
Stoichiometric coefficients for the reaction, reactant or product side.
static void _writeDecrementSpecies(InputIter begin, InputIter end, const std::string &r, std::map< size_t, std::string > &out)
void writeIncrementSpecies(const std::string &r, std::map< size_t, std::string > &out)
vector_fp m_order
Reaction orders for the reaction.
void writeDecrementSpecies(const std::string &r, std::map< size_t, std::string > &out)
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...