7 #ifndef CT_STOICH_MGR_H
8 #define CT_STOICH_MGR_H
128 static doublereal ppow(doublereal x, doublereal order)
131 return std::pow(x, order);
137 inline static std::string fmt(
const std::string& r,
size_t n)
139 return r +
"[" +
int2str(n) +
"]";
151 C1(
size_t rxn = 0,
size_t ic0 = 0) :
156 C1(
const C1& right) :
161 C1& operator=(
const C1& right) {
162 if (
this != &right) {
169 size_t data(std::vector<size_t>& ic) {
175 void incrementSpecies(
const doublereal* R, doublereal* S)
const {
179 void decrementSpecies(
const doublereal* R, doublereal* S)
const {
183 void multiply(
const doublereal* S, doublereal* R)
const {
187 void incrementReaction(
const doublereal* S, doublereal* R)
const {
191 void decrementReaction(
const doublereal* S, doublereal* R)
const {
195 size_t rxnNumber()
const {
198 size_t speciesIndex(
size_t n)
const {
205 void writeMultiply(
const std::string& r, std::map<size_t, std::string>& out) {
209 void writeIncrementReaction(
const std::string& r, std::map<size_t, std::string>& out) {
212 void writeDecrementReaction(
const std::string& r, std::map<size_t, std::string>& out) {
216 void writeIncrementSpecies(
const std::string& r, std::map<size_t, std::string>& out) {
219 void writeDecrementSpecies(
const std::string& r, std::map<size_t, std::string>& out) {
238 C2(
size_t rxn = 0,
size_t ic0 = 0,
size_t ic1 = 0)
241 C2(
const C2& right) :
247 C2& operator=(
const C2& right) {
248 if (
this != &right) {
256 size_t data(std::vector<size_t>& ic) {
263 void incrementSpecies(
const doublereal* R, doublereal* S)
const {
265 S[m_ic1] += R[
m_rxn];
268 void decrementSpecies(
const doublereal* R, doublereal* S)
const {
270 S[m_ic1] -= R[
m_rxn];
273 void multiply(
const doublereal* S, doublereal* R)
const {
277 void incrementReaction(
const doublereal* S, doublereal* R)
const {
281 void decrementReaction(
const doublereal* S, doublereal* R)
const {
285 size_t rxnNumber()
const {
288 size_t speciesIndex(
size_t n)
const {
289 return (n == 0 ?
m_ic0 : m_ic1);
295 void writeMultiply(
const std::string& r, std::map<size_t, std::string>& out) {
296 out[
m_rxn] = fmt(r,
m_ic0) +
" * " + fmt(r, m_ic1);
298 void writeIncrementReaction(
const std::string& r, std::map<size_t, std::string>& out) {
299 out[
m_rxn] +=
" + "+fmt(r,
m_ic0)+
" + "+fmt(r, m_ic1);
301 void writeDecrementReaction(
const std::string& r, std::map<size_t, std::string>& out) {
302 out[
m_rxn] +=
" - "+fmt(r,
m_ic0)+
" - "+fmt(r, m_ic1);
305 void writeIncrementSpecies(
const std::string& r, std::map<size_t, std::string>& out) {
306 std::string s =
" + "+fmt(r,
m_rxn);
310 void writeDecrementSpecies(
const std::string& r, std::map<size_t, std::string>& out) {
311 std::string s =
" - "+fmt(r,
m_rxn);
332 C3(
size_t rxn = 0,
size_t ic0 = 0,
size_t ic1 = 0,
size_t ic2 = 0)
333 : m_rxn(rxn), m_ic0(ic0), m_ic1(ic1), m_ic2(ic2) {}
335 C3(
const C3& right) :
342 C3& operator=(
const C3& right) {
343 if (
this != &right) {
352 size_t data(std::vector<size_t>& ic) {
360 void incrementSpecies(
const doublereal* R, doublereal* S)
const {
361 S[m_ic0] += R[m_rxn];
362 S[m_ic1] += R[m_rxn];
363 S[m_ic2] += R[m_rxn];
366 void decrementSpecies(
const doublereal* R, doublereal* S)
const {
367 S[m_ic0] -= R[m_rxn];
368 S[m_ic1] -= R[m_rxn];
369 S[m_ic2] -= R[m_rxn];
372 void multiply(
const doublereal* S, doublereal* R)
const {
373 R[m_rxn] *= S[m_ic0] * S[m_ic1] * S[m_ic2];
376 void incrementReaction(
const doublereal* S, doublereal* R)
const {
377 R[m_rxn] += S[m_ic0] + S[m_ic1] + S[m_ic2];
380 void decrementReaction(
const doublereal* S, doublereal* R)
const {
381 R[m_rxn] -= (S[m_ic0] + S[m_ic1] + S[m_ic2]);
384 size_t rxnNumber()
const {
387 size_t speciesIndex(
size_t n)
const {
388 return (n == 0 ? m_ic0 : (n == 1 ? m_ic1 : m_ic2));
394 void writeMultiply(
const std::string& r, std::map<size_t, std::string>& out) {
395 out[m_rxn] = fmt(r, m_ic0) +
" * " + fmt(r, m_ic1) +
" * " + fmt(r, m_ic2);
397 void writeIncrementReaction(
const std::string& r, std::map<size_t, std::string>& out) {
398 out[m_rxn] +=
" + "+fmt(r, m_ic0)+
" + "+fmt(r, m_ic1)+
" + "+fmt(r, m_ic2);
400 void writeDecrementReaction(
const std::string& r, std::map<size_t, std::string>& out) {
401 out[m_rxn] +=
" - "+fmt(r, m_ic0)+
" - "+fmt(r, m_ic1)+
" - "+fmt(r, m_ic2);
403 void writeIncrementSpecies(
const std::string& r, std::map<size_t, std::string>& out) {
404 std::string s =
" + "+fmt(r, m_rxn);
409 void writeDecrementSpecies(
const std::string& r, std::map<size_t, std::string>& out) {
410 std::string s =
" - "+fmt(r, m_rxn);
442 m_stoich.resize(
m_n);
443 for (
size_t n = 0; n <
m_n; n++) {
445 m_order[n] = order_[n];
446 m_stoich[n] = stoich_[n];
454 m_order(right.m_order),
455 m_stoich(right.m_stoich) {
459 if (
this != &right) {
463 m_order = right.m_order;
464 m_stoich = right.m_stoich;
469 size_t data(std::vector<size_t>& ic) {
471 for (
size_t n = 0; n <
m_n; n++) {
477 doublereal order(
size_t n)
const {
480 doublereal stoich(
size_t n)
const {
483 size_t speciesIndex(
size_t n)
const {
487 void multiply(
const doublereal* input, doublereal* output)
const {
489 for (
size_t n = 0; n <
m_n; n++) {
492 output[
m_rxn] *= ppow(input[
m_ic[n]], oo);
497 void incrementSpecies(
const doublereal* input,
498 doublereal* output)
const {
499 doublereal x = input[
m_rxn];
500 for (
size_t n = 0; n <
m_n; n++) {
501 output[
m_ic[n]] += m_stoich[n]*x;
505 void decrementSpecies(
const doublereal* input,
506 doublereal* output)
const {
507 doublereal x = input[
m_rxn];
508 for (
size_t n = 0; n <
m_n; n++) {
509 output[
m_ic[n]] -= m_stoich[n]*x;
513 void incrementReaction(
const doublereal* input,
514 doublereal* output)
const {
515 for (
size_t n = 0; n <
m_n; n++) output[
m_rxn]
516 += m_stoich[n]*input[
m_ic[n]];
519 void decrementReaction(
const doublereal* input,
520 doublereal* output)
const {
521 for (
size_t n = 0; n <
m_n; n++) output[
m_rxn]
522 -= m_stoich[n]*input[
m_ic[n]];
525 void writeMultiply(
const std::string& r, std::map<size_t, std::string>& out) {
527 for (
size_t n = 0; n <
m_n; n++) {
528 if (m_order[n] == 1.0) {
538 void writeIncrementReaction(
const std::string& r, std::map<size_t, std::string>& out) {
539 for (
size_t n = 0; n <
m_n; n++) {
543 void writeDecrementReaction(
const std::string& r, std::map<size_t, std::string>& out) {
544 for (
size_t n = 0; n <
m_n; n++) {
548 void writeIncrementSpecies(
const std::string& r, std::map<size_t, std::string>& out) {
549 std::string s = fmt(r,
m_rxn);
550 for (
size_t n = 0; n <
m_n; n++) {
551 out[
m_ic[n]] +=
" + "+
fp2str(m_stoich[n]) +
"*" + s;
555 void writeDecrementSpecies(
const std::string& r, std::map<size_t, std::string>& out) {
556 std::string s = fmt(r,
m_rxn);
557 for (
size_t n = 0; n <
m_n; n++) {
558 out[
m_ic[n]] +=
" - "+
fp2str(m_stoich[n]) +
"*" + s;
587 template<
class InputIter,
class Vec1,
class Vec2>
588 inline static void _multiply(InputIter begin, InputIter end,
589 const Vec1& input, Vec2& output)
591 for (; begin != end; ++begin) {
592 begin->multiply(input, output);
596 template<
class InputIter,
class Vec1,
class Vec2>
597 inline static void _incrementSpecies(InputIter begin,
598 InputIter end,
const Vec1& input, Vec2& output)
600 for (; begin != end; ++begin) {
601 begin->incrementSpecies(input, output);
605 template<
class InputIter,
class Vec1,
class Vec2>
606 inline static void _decrementSpecies(InputIter begin,
607 InputIter end,
const Vec1& input, Vec2& output)
609 for (; begin != end; ++begin) {
610 begin->decrementSpecies(input, output);
614 template<
class InputIter,
class Vec1,
class Vec2>
615 inline static void _incrementReactions(InputIter begin,
616 InputIter end,
const Vec1& input, Vec2& output)
618 for (; begin != end; ++begin) {
619 begin->incrementReaction(input, output);
623 template<
class InputIter,
class Vec1,
class Vec2>
624 inline static void _decrementReactions(InputIter begin,
625 InputIter end,
const Vec1& input, Vec2& output)
627 for (; begin != end; ++begin) {
628 begin->decrementReaction(input, output);
633 template<
class InputIter>
634 inline static void _writeIncrementSpecies(InputIter begin, InputIter end,
635 const std::string& r, std::map<size_t, std::string>& out)
637 for (; begin != end; ++begin) {
638 begin->writeIncrementSpecies(r, out);
642 template<
class InputIter>
643 inline static void _writeDecrementSpecies(InputIter begin, InputIter end,
644 const std::string& r, std::map<size_t, std::string>& out)
646 for (; begin != end; ++begin) {
647 begin->writeDecrementSpecies(r, out);
651 template<
class InputIter>
652 inline static void _writeIncrementReaction(InputIter begin, InputIter end,
653 const std::string& r, std::map<size_t, std::string>& out)
655 for (; begin != end; ++begin) {
656 begin->writeIncrementReaction(r, out);
660 template<
class InputIter>
661 inline static void _writeDecrementReaction(InputIter begin, InputIter end,
662 const std::string& r, std::map<size_t, std::string>& out)
664 for (; begin != end; ++begin) {
665 begin->writeDecrementReaction(r, out);
669 template<
class InputIter>
670 inline static void _writeMultiply(InputIter begin, InputIter end,
671 const std::string& r, std::map<size_t, std::string>& out)
673 for (; begin != end; ++begin) {
674 begin->writeMultiply(r, out);
737 StoichManagerN(
const StoichManagerN& right) :
738 m_c1_list(right.m_c1_list),
739 m_c2_list(right.m_c2_list),
740 m_c3_list(right.m_c3_list),
741 m_cn_list(right.m_cn_list),
746 StoichManagerN& operator=(
const StoichManagerN& right) {
747 if (
this != &right) {
748 m_c1_list = right.m_c1_list;
749 m_c2_list = right.m_c2_list;
750 m_c3_list = right.m_c3_list;
751 m_cn_list = right.m_cn_list;
766 void add(
size_t rxn,
const std::vector<size_t>& k) {
769 add(rxn, k, order, stoich);
772 void add(
size_t rxn,
const std::vector<size_t>& k,
const vector_fp& order) {
774 add(rxn, k, order, stoich);
794 void add(
size_t rxn,
const std::vector<size_t>& k,
const vector_fp& order,
798 for (
size_t n = 0; n < stoich.size(); n++) {
799 if (stoich[n] != 1.0 || order[n] != 1.0) {
805 m_loc[rxn] = m_cn_list.size();
806 m_cn_list.push_back(C_AnyN(rxn, k, order, stoich));
810 m_loc[rxn] = m_c1_list.size();
811 m_c1_list.push_back(C1(rxn, k[0]));
814 m_loc[rxn] = m_c2_list.size();
815 m_c2_list.push_back(C2(rxn, k[0], k[1]));
818 m_loc[rxn] = m_c3_list.size();
819 m_c3_list.push_back(C3(rxn, k[0], k[1], k[2]));
822 m_loc[rxn] = m_cn_list.size();
823 m_cn_list.push_back(C_AnyN(rxn, k, order, stoich));
828 void multiply(
const doublereal* input, doublereal* output)
const {
829 _multiply(m_c1_list.begin(), m_c1_list.end(), input, output);
830 _multiply(m_c2_list.begin(), m_c2_list.end(), input, output);
831 _multiply(m_c3_list.begin(), m_c3_list.end(), input, output);
832 _multiply(m_cn_list.begin(), m_cn_list.end(), input, output);
835 void incrementSpecies(
const doublereal* input, doublereal* output)
const {
836 _incrementSpecies(m_c1_list.begin(), m_c1_list.end(), input, output);
837 _incrementSpecies(m_c2_list.begin(), m_c2_list.end(), input, output);
838 _incrementSpecies(m_c3_list.begin(), m_c3_list.end(), input, output);
839 _incrementSpecies(m_cn_list.begin(), m_cn_list.end(), input, output);
842 void decrementSpecies(
const doublereal* input, doublereal* output)
const {
843 _decrementSpecies(m_c1_list.begin(), m_c1_list.end(), input, output);
844 _decrementSpecies(m_c2_list.begin(), m_c2_list.end(), input, output);
845 _decrementSpecies(m_c3_list.begin(), m_c3_list.end(), input, output);
846 _decrementSpecies(m_cn_list.begin(), m_cn_list.end(), input, output);
849 void incrementReactions(
const doublereal* input, doublereal* output)
const {
850 _incrementReactions(m_c1_list.begin(), m_c1_list.end(), input, output);
851 _incrementReactions(m_c2_list.begin(), m_c2_list.end(), input, output);
852 _incrementReactions(m_c3_list.begin(), m_c3_list.end(), input, output);
853 _incrementReactions(m_cn_list.begin(), m_cn_list.end(), input, output);
856 void decrementReactions(
const doublereal* input, doublereal* output)
const {
857 _decrementReactions(m_c1_list.begin(), m_c1_list.end(), input, output);
858 _decrementReactions(m_c2_list.begin(), m_c2_list.end(), input, output);
859 _decrementReactions(m_c3_list.begin(), m_c3_list.end(), input, output);
860 _decrementReactions(m_cn_list.begin(), m_cn_list.end(), input, output);
863 void writeIncrementSpecies(
const std::string& r, std::map<size_t, std::string>& out) {
864 _writeIncrementSpecies(m_c1_list.begin(), m_c1_list.end(), r, out);
865 _writeIncrementSpecies(m_c2_list.begin(), m_c2_list.end(), r, out);
866 _writeIncrementSpecies(m_c3_list.begin(), m_c3_list.end(), r, out);
867 _writeIncrementSpecies(m_cn_list.begin(), m_cn_list.end(), r, out);
870 void writeDecrementSpecies(
const std::string& r, std::map<size_t, std::string>& out) {
871 _writeDecrementSpecies(m_c1_list.begin(), m_c1_list.end(), r, out);
872 _writeDecrementSpecies(m_c2_list.begin(), m_c2_list.end(), r, out);
873 _writeDecrementSpecies(m_c3_list.begin(), m_c3_list.end(), r, out);
874 _writeDecrementSpecies(m_cn_list.begin(), m_cn_list.end(), r, out);
877 void writeIncrementReaction(
const std::string& r, std::map<size_t, std::string>& out) {
878 _writeIncrementReaction(m_c1_list.begin(), m_c1_list.end(), r, out);
879 _writeIncrementReaction(m_c2_list.begin(), m_c2_list.end(), r, out);
880 _writeIncrementReaction(m_c3_list.begin(), m_c3_list.end(), r, out);
881 _writeIncrementReaction(m_cn_list.begin(), m_cn_list.end(), r, out);
884 void writeDecrementReaction(
const std::string& r, std::map<size_t, std::string>& out) {
885 _writeDecrementReaction(m_c1_list.begin(), m_c1_list.end(), r, out);
886 _writeDecrementReaction(m_c2_list.begin(), m_c2_list.end(), r, out);
887 _writeDecrementReaction(m_c3_list.begin(), m_c3_list.end(), r, out);
888 _writeDecrementReaction(m_cn_list.begin(), m_cn_list.end(), r, out);
891 void writeMultiply(
const std::string& r, std::map<size_t, std::string>& out) {
892 _writeMultiply(m_c1_list.begin(), m_c1_list.end(), r, out);
893 _writeMultiply(m_c2_list.begin(), m_c2_list.end(), r, out);
894 _writeMultiply(m_c3_list.begin(), m_c3_list.end(), r, out);
895 _writeMultiply(m_cn_list.begin(), m_cn_list.end(), r, out);
899 std::vector<C1> m_c1_list;
900 std::vector<C2> m_c2_list;
901 std::vector<C3> m_c3_list;
902 std::vector<C_AnyN> m_cn_list;
907 std::map<size_t, size_t> m_n;
912 std::map<size_t, size_t> m_loc;
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.
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.
size_t m_rxn
Reaction number.
size_t m_ic0
Species index -> index into the species vector for the two species.
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.
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.
Contains declarations for string manipulation functions within Cantera.
Handles three species in a reaction.
size_t m_rxn
ID of the reaction corresponding to this stoichiometric manager.