7 #ifndef CT_STOICH_MGR_H
8 #define CT_STOICH_MGR_H
144 static doublereal ppow(doublereal x, doublereal order)
147 return std::pow(x, order);
153 inline static std::string fmt(std::string r,
size_t n)
155 return r +
"[" +
int2str(n) +
"]";
169 C1(
size_t rxn = 0,
size_t ic0 = 0) :
174 C1(
const C1& right) :
179 C1& operator=(
const C1& right) {
180 if (
this != &right) {
187 size_t data(std::vector<size_t>& ic) {
193 void incrementSpecies(
const doublereal* R, doublereal* S)
const {
197 void decrementSpecies(
const doublereal* R, doublereal* S)
const {
201 void multiply(
const doublereal* S, doublereal* R)
const {
205 void incrementReaction(
const doublereal* S, doublereal* R)
const {
209 void decrementReaction(
const doublereal* S, doublereal* R)
const {
213 size_t rxnNumber()
const {
216 size_t speciesIndex(
size_t n)
const {
223 void writeMultiply(std::string r, std::map<size_t, std::string>& out) {
227 void writeIncrementReaction(std::string r, std::map<size_t, std::string>& out) {
230 void writeDecrementReaction(std::string r, std::map<size_t, std::string>& out) {
234 void writeIncrementSpecies(std::string r, std::map<size_t, std::string>& out) {
237 void writeDecrementSpecies(std::string r, std::map<size_t, std::string>& out) {
257 C2(
size_t rxn = 0,
size_t ic0 = 0,
size_t ic1 = 0)
260 C2(
const C2& right) :
266 C2& operator=(
const C2& right) {
267 if (
this != &right) {
275 size_t data(std::vector<size_t>& ic) {
282 void incrementSpecies(
const doublereal* R, doublereal* S)
const {
284 S[m_ic1] += R[
m_rxn];
287 void decrementSpecies(
const doublereal* R, doublereal* S)
const {
289 S[m_ic1] -= R[
m_rxn];
292 void multiply(
const doublereal* S, doublereal* R)
const {
296 void incrementReaction(
const doublereal* S, doublereal* R)
const {
300 void decrementReaction(
const doublereal* S, doublereal* R)
const {
304 size_t rxnNumber()
const {
307 size_t speciesIndex(
size_t n)
const {
308 return (n == 0 ?
m_ic0 : m_ic1);
314 void writeMultiply(std::string r, std::map<size_t, std::string>& out) {
315 out[
m_rxn] = fmt(r,
m_ic0) +
" * " + fmt(r, m_ic1);
317 void writeIncrementReaction(std::string r, std::map<size_t, std::string>& out) {
318 out[
m_rxn] +=
" + "+fmt(r,
m_ic0)+
" + "+fmt(r, m_ic1);
320 void writeDecrementReaction(std::string r, std::map<size_t, std::string>& out) {
321 out[
m_rxn] +=
" - "+fmt(r,
m_ic0)+
" - "+fmt(r, m_ic1);
324 void writeIncrementSpecies(std::string r, std::map<size_t, std::string>& out) {
325 std::string s =
" + "+fmt(r,
m_rxn);
329 void writeDecrementSpecies(std::string r, std::map<size_t, std::string>& out) {
330 std::string s =
" - "+fmt(r,
m_rxn);
357 C3(
size_t rxn = 0,
size_t ic0 = 0,
size_t ic1 = 0,
size_t ic2 = 0)
358 : m_rxn(rxn), m_ic0(ic0), m_ic1(ic1), m_ic2(ic2) {}
360 C3(
const C3& right) :
367 C3& operator=(
const C3& right) {
368 if (
this != &right) {
377 size_t data(std::vector<size_t>& ic) {
385 void incrementSpecies(
const doublereal* R, doublereal* S)
const {
386 S[m_ic0] += R[m_rxn];
387 S[m_ic1] += R[m_rxn];
388 S[m_ic2] += R[m_rxn];
391 void decrementSpecies(
const doublereal* R, doublereal* S)
const {
392 S[m_ic0] -= R[m_rxn];
393 S[m_ic1] -= R[m_rxn];
394 S[m_ic2] -= R[m_rxn];
397 void multiply(
const doublereal* S, doublereal* R)
const {
398 R[m_rxn] *= S[m_ic0] * S[m_ic1] * S[m_ic2];
401 void incrementReaction(
const doublereal* S, doublereal* R)
const {
402 R[m_rxn] += S[m_ic0] + S[m_ic1] + S[m_ic2];
405 void decrementReaction(
const doublereal* S, doublereal* R)
const {
406 R[m_rxn] -= (S[m_ic0] + S[m_ic1] + S[m_ic2]);
409 size_t rxnNumber()
const {
412 size_t speciesIndex(
size_t n)
const {
413 return (n == 0 ? m_ic0 : (n == 1 ? m_ic1 : m_ic2));
419 void writeMultiply(std::string r, std::map<size_t, std::string>& out) {
420 out[m_rxn] = fmt(r, m_ic0) +
" * " + fmt(r, m_ic1) +
" * " + fmt(r, m_ic2);
422 void writeIncrementReaction(std::string r, std::map<size_t, std::string>& out) {
423 out[m_rxn] +=
" + "+fmt(r, m_ic0)+
" + "+fmt(r, m_ic1)+
" + "+fmt(r, m_ic2);
425 void writeDecrementReaction(std::string r, std::map<size_t, std::string>& out) {
426 out[m_rxn] +=
" - "+fmt(r, m_ic0)+
" - "+fmt(r, m_ic1)+
" - "+fmt(r, m_ic2);
428 void writeIncrementSpecies(std::string r, std::map<size_t, std::string>& out) {
429 std::string s =
" + "+fmt(r, m_rxn);
434 void writeDecrementSpecies(std::string r, std::map<size_t, std::string>& out) {
435 std::string s =
" - "+fmt(r, m_rxn);
468 m_stoich.resize(
m_n);
469 for (
size_t n = 0; n <
m_n; n++) {
471 m_order[n] = order[n];
472 m_stoich[n] = stoich[n];
480 m_order(right.m_order),
481 m_stoich(right.m_stoich) {
485 if (
this != &right) {
489 m_order = right.m_order;
490 m_stoich = right.m_stoich;
495 size_t data(std::vector<size_t>& ic) {
497 for (
size_t n = 0; n <
m_n; n++) {
503 doublereal order(
size_t n)
const {
506 doublereal stoich(
size_t n)
const {
509 size_t speciesIndex(
size_t n)
const {
513 void multiply(
const doublereal* input, doublereal* output)
const {
515 for (
size_t n = 0; n <
m_n; n++) {
518 output[
m_rxn] *= ppow(input[
m_ic[n]], oo);
523 void incrementSpecies(
const doublereal* input,
524 doublereal* output)
const {
525 doublereal x = input[
m_rxn];
526 for (
size_t n = 0; n <
m_n; n++) {
527 output[
m_ic[n]] += m_stoich[n]*x;
531 void decrementSpecies(
const doublereal* input,
532 doublereal* output)
const {
533 doublereal x = input[
m_rxn];
534 for (
size_t n = 0; n <
m_n; n++) {
535 output[
m_ic[n]] -= m_stoich[n]*x;
539 void incrementReaction(
const doublereal* input,
540 doublereal* output)
const {
541 for (
size_t n = 0; n <
m_n; n++) output[
m_rxn]
542 += m_stoich[n]*input[
m_ic[n]];
545 void decrementReaction(
const doublereal* input,
546 doublereal* output)
const {
547 for (
size_t n = 0; n <
m_n; n++) output[
m_rxn]
548 -= m_stoich[n]*input[
m_ic[n]];
551 void writeMultiply(std::string r, std::map<size_t, std::string>& out) {
553 for (
size_t n = 0; n <
m_n; n++) {
554 if (m_order[n] == 1.0) {
564 void writeIncrementReaction(std::string r, std::map<size_t, std::string>& out) {
565 for (
size_t n = 0; n <
m_n; n++) {
569 void writeDecrementReaction(std::string r, std::map<size_t, std::string>& out) {
570 for (
size_t n = 0; n <
m_n; n++) {
574 void writeIncrementSpecies(std::string r, std::map<size_t, std::string>& out) {
575 std::string s = fmt(r,
m_rxn);
576 for (
size_t n = 0; n <
m_n; n++) {
577 out[
m_ic[n]] +=
" + "+
fp2str(m_stoich[n]) +
"*" + s;
581 void writeDecrementSpecies(std::string r, std::map<size_t, std::string>& out) {
582 std::string s = fmt(r,
m_rxn);
583 for (
size_t n = 0; n <
m_n; n++) {
584 out[
m_ic[n]] +=
" - "+
fp2str(m_stoich[n]) +
"*" + s;
615 template<
class InputIter,
class Vec1,
class Vec2>
616 inline static void _multiply(InputIter begin, InputIter end,
617 const Vec1& input, Vec2& output)
619 for (; begin != end; ++begin) {
620 begin->multiply(input, output);
624 template<
class InputIter,
class Vec1,
class Vec2>
625 inline static void _incrementSpecies(InputIter begin,
626 InputIter end,
const Vec1& input, Vec2& output)
628 for (; begin != end; ++begin) {
629 begin->incrementSpecies(input, output);
633 template<
class InputIter,
class Vec1,
class Vec2>
634 inline static void _decrementSpecies(InputIter begin,
635 InputIter end,
const Vec1& input, Vec2& output)
637 for (; begin != end; ++begin) {
638 begin->decrementSpecies(input, output);
642 template<
class InputIter,
class Vec1,
class Vec2>
643 inline static void _incrementReactions(InputIter begin,
644 InputIter end,
const Vec1& input, Vec2& output)
646 for (; begin != end; ++begin) {
647 begin->incrementReaction(input, output);
651 template<
class InputIter,
class Vec1,
class Vec2>
652 inline static void _decrementReactions(InputIter begin,
653 InputIter end,
const Vec1& input, Vec2& output)
655 for (; begin != end; ++begin) {
656 begin->decrementReaction(input, output);
661 template<
class InputIter>
662 inline static void _writeIncrementSpecies(InputIter begin, InputIter end, std::string r,
663 std::map<size_t, std::string>& out)
665 for (; begin != end; ++begin) {
666 begin->writeIncrementSpecies(r, out);
670 template<
class InputIter>
671 inline static void _writeDecrementSpecies(InputIter begin, InputIter end, std::string r,
672 std::map<size_t, std::string>& out)
674 for (; begin != end; ++begin) {
675 begin->writeDecrementSpecies(r, out);
679 template<
class InputIter>
680 inline static void _writeIncrementReaction(InputIter begin, InputIter end, std::string r,
681 std::map<size_t, std::string>& out)
683 for (; begin != end; ++begin) {
684 begin->writeIncrementReaction(r, out);
688 template<
class InputIter>
689 inline static void _writeDecrementReaction(InputIter begin, InputIter end, std::string r,
690 std::map<size_t, std::string>& out)
692 for (; begin != end; ++begin) {
693 begin->writeDecrementReaction(r, out);
697 template<
class InputIter>
698 inline static void _writeMultiply(InputIter begin, InputIter end, std::string r,
699 std::map<size_t, std::string>& out)
701 for (; begin != end; ++begin) {
702 begin->writeMultiply(r, out);
765 StoichManagerN(
const StoichManagerN& right) :
766 m_c1_list(right.m_c1_list),
767 m_c2_list(right.m_c2_list),
768 m_c3_list(right.m_c3_list),
769 m_cn_list(right.m_cn_list),
776 StoichManagerN& operator=(
const StoichManagerN& right) {
777 if (
this != &right) {
778 m_c1_list = right.m_c1_list;
779 m_c2_list = right.m_c2_list;
780 m_c3_list = right.m_c3_list;
781 m_cn_list = right.m_cn_list;
796 void add(
size_t rxn,
const std::vector<size_t>& k) {
799 add(rxn, k, order, stoich);
802 void add(
size_t rxn,
const std::vector<size_t>& k,
const vector_fp& order) {
804 add(rxn, k, order, stoich);
825 void add(
size_t rxn,
const std::vector<size_t>& k,
const vector_fp& order,
829 for (
size_t n = 0; n < stoich.size(); n++) {
830 if (stoich[n] != 1.0 || order[n] != 1.0) {
836 m_loc[rxn] = m_cn_list.size();
837 m_cn_list.push_back(C_AnyN(rxn, k, order, stoich));
841 m_loc[rxn] = m_c1_list.size();
842 m_c1_list.push_back(C1(rxn, k[0]));
845 m_loc[rxn] = m_c2_list.size();
846 m_c2_list.push_back(C2(rxn, k[0], k[1]));
849 m_loc[rxn] = m_c3_list.size();
850 m_c3_list.push_back(C3(rxn, k[0], k[1], k[2]));
853 m_loc[rxn] = m_cn_list.size();
854 m_cn_list.push_back(C_AnyN(rxn, k, order, stoich));
859 void multiply(
const doublereal* input, doublereal* output)
const {
860 _multiply(m_c1_list.begin(), m_c1_list.end(), input, output);
861 _multiply(m_c2_list.begin(), m_c2_list.end(), input, output);
862 _multiply(m_c3_list.begin(), m_c3_list.end(), input, output);
863 _multiply(m_cn_list.begin(), m_cn_list.end(), input, output);
866 void incrementSpecies(
const doublereal* input, doublereal* output)
const {
867 _incrementSpecies(m_c1_list.begin(), m_c1_list.end(), input, output);
868 _incrementSpecies(m_c2_list.begin(), m_c2_list.end(), input, output);
869 _incrementSpecies(m_c3_list.begin(), m_c3_list.end(), input, output);
870 _incrementSpecies(m_cn_list.begin(), m_cn_list.end(), input, output);
873 void decrementSpecies(
const doublereal* input, doublereal* output)
const {
874 _decrementSpecies(m_c1_list.begin(), m_c1_list.end(), input, output);
875 _decrementSpecies(m_c2_list.begin(), m_c2_list.end(), input, output);
876 _decrementSpecies(m_c3_list.begin(), m_c3_list.end(), input, output);
877 _decrementSpecies(m_cn_list.begin(), m_cn_list.end(), input, output);
880 void incrementReactions(
const doublereal* input, doublereal* output)
const {
881 _incrementReactions(m_c1_list.begin(), m_c1_list.end(), input, output);
882 _incrementReactions(m_c2_list.begin(), m_c2_list.end(), input, output);
883 _incrementReactions(m_c3_list.begin(), m_c3_list.end(), input, output);
884 _incrementReactions(m_cn_list.begin(), m_cn_list.end(), input, output);
887 void decrementReactions(
const doublereal* input, doublereal* output)
const {
888 _decrementReactions(m_c1_list.begin(), m_c1_list.end(), input, output);
889 _decrementReactions(m_c2_list.begin(), m_c2_list.end(), input, output);
890 _decrementReactions(m_c3_list.begin(), m_c3_list.end(), input, output);
891 _decrementReactions(m_cn_list.begin(), m_cn_list.end(), input, output);
894 void writeIncrementSpecies(std::string r, std::map<size_t, std::string>& out) {
895 _writeIncrementSpecies(m_c1_list.begin(), m_c1_list.end(), r, out);
896 _writeIncrementSpecies(m_c2_list.begin(), m_c2_list.end(), r, out);
897 _writeIncrementSpecies(m_c3_list.begin(), m_c3_list.end(), r, out);
898 _writeIncrementSpecies(m_cn_list.begin(), m_cn_list.end(), r, out);
901 void writeDecrementSpecies(std::string r, std::map<size_t, std::string>& out) {
902 _writeDecrementSpecies(m_c1_list.begin(), m_c1_list.end(), r, out);
903 _writeDecrementSpecies(m_c2_list.begin(), m_c2_list.end(), r, out);
904 _writeDecrementSpecies(m_c3_list.begin(), m_c3_list.end(), r, out);
905 _writeDecrementSpecies(m_cn_list.begin(), m_cn_list.end(), r, out);
908 void writeIncrementReaction(std::string r, std::map<size_t, std::string>& out) {
909 _writeIncrementReaction(m_c1_list.begin(), m_c1_list.end(), r, out);
910 _writeIncrementReaction(m_c2_list.begin(), m_c2_list.end(), r, out);
911 _writeIncrementReaction(m_c3_list.begin(), m_c3_list.end(), r, out);
912 _writeIncrementReaction(m_cn_list.begin(), m_cn_list.end(), r, out);
915 void writeDecrementReaction(std::string r, std::map<size_t, std::string>& out) {
916 _writeDecrementReaction(m_c1_list.begin(), m_c1_list.end(), r, out);
917 _writeDecrementReaction(m_c2_list.begin(), m_c2_list.end(), r, out);
918 _writeDecrementReaction(m_c3_list.begin(), m_c3_list.end(), r, out);
919 _writeDecrementReaction(m_cn_list.begin(), m_cn_list.end(), r, out);
922 void writeMultiply(std::string r, std::map<size_t, std::string>& out) {
923 _writeMultiply(m_c1_list.begin(), m_c1_list.end(), r, out);
924 _writeMultiply(m_c2_list.begin(), m_c2_list.end(), r, out);
925 _writeMultiply(m_c3_list.begin(), m_c3_list.end(), r, out);
926 _writeMultiply(m_cn_list.begin(), m_cn_list.end(), r, out);
930 std::vector<C1> m_c1_list;
931 std::vector<C2> m_c2_list;
932 std::vector<C3> m_c3_list;
933 std::vector<C_AnyN> m_cn_list;
938 std::map<size_t, size_t> m_n;
943 std::map<size_t, size_t> m_loc;