17 void SpeciesNode::addPath(Path* path)
19 m_paths.push_back(path);
20 if (path->begin() ==
this) {
21 m_out += path->flow();
22 }
else if (path->end() ==
this) {
25 throw CanteraError(
"SpeciesNode::addPath",
"path added to wrong node");
29 void SpeciesNode::printPaths()
31 for (
size_t i = 0; i < m_paths.size(); i++) {
32 cout << m_paths[i]->begin()->name <<
" --> "
33 << m_paths[i]->end()->name <<
": "
34 << m_paths[i]->flow() << endl;
38 Path::Path(SpeciesNode* begin, SpeciesNode* end)
39 : m_a(begin), m_b(end), m_total(0.0)
45 void Path::addReaction(
size_t rxnNumber, doublereal value,
48 m_rxn[rxnNumber] += value;
51 m_label[label] += value;
55 void Path::writeLabel(ostream& s, doublereal threshold)
57 if (m_label.size() == 0) {
61 for (
const auto& label : m_label) {
62 v = label.second/m_total;
63 if (m_label.size() == 1) {
64 s << label.first <<
"\\l";
65 }
else if (v > threshold) {
67 int percent = int(100*v + 0.5);
69 s <<
" (" << percent <<
"%)\\l";
77 ReactionPathDiagram::ReactionPathDiagram()
79 name =
"reaction_paths";
82 normal_color =
"steelblue";
83 dashed_color =
"gray";
84 dot_options =
"center=1;";
104 for (
const auto& node : m_nodes) {
109 size_t nn = nPaths();
110 for (
size_t n = 0; n < nn; n++) {
111 delete m_pathlist[n];
117 doublereal flmax = 0.0;
118 for (
size_t i = 0; i < nPaths(); i++) {
120 flmax = std::max(p->flow(), flmax);
123 for (
size_t i = 0; i < nPaths(); i++) {
124 for (
const auto& rxn : path(i)->reactionMap()) {
125 double flxratio = rxn.second/flmax;
126 if (flxratio > threshold) {
127 m_rxns[rxn.first] = 1;
132 for (
const auto& rxn : m_rxns) {
133 r.push_back(
int(rxn.first));
138 void ReactionPathDiagram::add(ReactionPathDiagram& d)
140 for (
size_t n = 0; n < nPaths(); n++) {
142 size_t k1 = p->begin()->number;
143 size_t k2 = p->end()->number;
144 p->setFlow(p->flow() + d.flow(k1,k2));
148 void ReactionPathDiagram::findMajorPaths(doublereal athreshold,
size_t lda,
152 for (
size_t n = 0; n < nNodes(); n++) {
153 for (
size_t m = n+1; m < nNodes(); m++) {
154 size_t k1 = m_speciesNumber[n];
155 size_t k2 = m_speciesNumber[m];
156 double fl = fabs(
netFlow(k1,k2));
157 netmax = std::max(fl, netmax);
160 for (
size_t n = 0; n < nNodes(); n++) {
161 for (
size_t m = n+1; m < nNodes(); m++) {
162 size_t k1 = m_speciesNumber[n];
163 size_t k2 = m_speciesNumber[m];
164 double fl = fabs(
netFlow(k1,k2));
165 if (fl > athreshold*netmax) {
172 void ReactionPathDiagram::writeData(ostream& s)
175 for (
size_t i1 = 0; i1 < nNodes(); i1++) {
176 size_t k1 = m_speciesNumber[i1];
177 s << m_nodes[k1]->name <<
" ";
180 for (
size_t i1 = 0; i1 < nNodes(); i1++) {
181 size_t k1 = m_speciesNumber[i1];
182 for (
size_t i2 = i1+1; i2 < nNodes(); i2++) {
183 size_t k2 = m_speciesNumber[i2];
184 double f1 =
flow(k1, k2);
185 double f2 =
flow(k2, k1);
186 s << m_nodes[k1]->name <<
" " << m_nodes[k2]->name
187 <<
" " << f1 <<
" " << -f2 << endl;
194 doublereal flmax = 0.0;
198 s <<
"digraph " << name <<
" {" << endl;
211 if (dot_options !=
"") {
212 s << dot_options << endl;
216 if (flow_type == NetFlow) {
220 for (
size_t i1 = 0; i1 < nNodes(); i1++) {
221 size_t k1 = m_speciesNumber[i1];
223 for (
size_t i2 = i1+1; i2 < nNodes(); i2++) {
224 size_t k2 = m_speciesNumber[i2];
229 flmax = std::max(flx, flmax);
235 flmax = std::max(flmax, 1e-10);
238 for (
size_t i1 = 0; i1 < nNodes(); i1++) {
239 size_t k1 = m_speciesNumber[i1];
240 for (
size_t i2 = i1+1; i2 < nNodes(); i2++) {
241 size_t k2 = m_speciesNumber[i2];
243 if (m_local !=
npos && k1 != m_local && k2 != m_local) {
254 flxratio = flx/flmax;
258 flxratio = -flx/flmax;
263 if (flxratio >= threshold) {
268 s <<
"s" << kbegin <<
" -> s" << kend;
269 s <<
"[fontname=\""+m_font+
"\", penwidth=";
271 if (arrow_width < 0) {
272 double lwidth = 1.0 - 4.0
273 * log10(flxratio/threshold)/log10(threshold) + 1.0;
276 << std::min(6.0, 0.5*lwidth);
279 s <<
", arrowsize=" << flxratio + 1;
282 doublereal hue = 0.7;
283 doublereal bright = 0.9;
284 s <<
", color=" <<
"\"" << hue <<
", "
286 <<
", " << bright <<
"\"" << endl;
288 if (flxratio > label_min) {
289 s <<
", label=\" " << flxratio;
291 if (
flow(kbegin, kend) > 0.0) {
293 <<
flow(kbegin, kend)/flmax <<
"\\l";
294 path(kbegin, kend)->writeLabel(s);
296 if (
flow(kend, kbegin) > 0.0) {
298 <<
flow(kend,kbegin)/flmax <<
"\\l";
299 path(kend, kbegin)->writeLabel(s);
311 for (
size_t i = 0; i < nPaths(); i++) {
312 flmax = std::max(path(i)->
flow(), flmax);
318 for (
size_t i = 0; i < nPaths(); i++) {
320 double flxratio = p->flow()/flmax;
321 if (m_local !=
npos) {
322 if (p->begin()->number != m_local
323 && p->end()->number != m_local) {
327 if (flxratio > threshold) {
328 p->begin()->visible =
true;
329 p->end()->visible =
true;
330 s <<
"s" << p->begin()->number
331 <<
" -> s" << p->end()->number;
333 if (arrow_width < 0) {
334 double lwidth = 1.0 - 4.0 * log10(flxratio/threshold)/log10(threshold)
336 s <<
"[fontname=\""+m_font+
"\", penwidth="
339 << std::min(6.0, 0.5*lwidth);
343 s <<
", arrowsize=" << flxratio + 1;
345 doublereal hue = 0.7;
346 doublereal bright = 0.9;
347 s <<
", color=" <<
"\"" << hue <<
", " << flxratio + 0.5
348 <<
", " << bright <<
"\"" << endl;
350 if (flxratio > label_min) {
351 s <<
", label = \" " << flxratio;
363 for (
const auto& node : m_nodes) {
365 s <<
"s" << node.first <<
" [ fontname=\""+m_font+
"\", label=\"" << node.second->
name
369 s <<
" label = " <<
"\"" <<
"Scale = "
370 << flmax <<
"\\l " << title <<
"\";" << endl;
371 s <<
" fontname = \""+m_font+
"\";" << endl <<
"}" << endl;
375 void ReactionPathDiagram::addNode(
size_t k,
const string& nm, doublereal x)
379 m_nodes[k]->number = k;
380 m_nodes[k]->name = nm;
381 m_nodes[k]->value = x;
382 m_speciesNumber.push_back(k);
386 void ReactionPathDiagram::linkNodes(
size_t k1,
size_t k2,
size_t rxn,
387 doublereal value,
string legend)
389 Path* ff = m_paths[k1][k2];
391 ff=
new Path(m_nodes[k1], m_nodes[k2]);
392 m_paths[k1][k2] = ff;
393 m_pathlist.push_back(ff);
395 ff->addReaction(rxn, value, legend);
397 m_flxmax = std::max(ff->flow(), m_flxmax);
400 std::vector<size_t> ReactionPathDiagram::species()
402 return m_speciesNumber;
405 int ReactionPathBuilder::findGroups(ostream& logfile, Kinetics& s)
407 m_groups.resize(m_nr);
408 for (
size_t i = 0; i < m_nr; i++) {
409 logfile << endl <<
"Reaction " << i+1 <<
": "
410 << s.reactionString(i);
412 if (m_determinate[i]) {
413 logfile <<
" ... OK." << endl;
414 }
else if (m_reac[i].size() == 2 && m_prod[i].size() == 2) {
416 size_t kr0 = m_reac[i][0];
417 size_t kr1 = m_reac[i][1];
420 size_t kp0 = m_prod[i][0];
421 size_t kp1 = m_prod[i][1];
424 const Group& r0 = m_sgroup[kr0];
425 const Group& r1 = m_sgroup[kr1];
426 const Group& p0 = m_sgroup[kp0];
427 const Group& p1 = m_sgroup[kp1];
429 const Group* group_a0=0, *group_b0=0, *group_c0=0,
430 *group_a1=0, *group_b1=0, *group_c1=0;
433 if (b0.valid() && b1.valid()) {
434 logfile <<
" ... ambiguous." << endl;
435 }
else if (!b0.valid() && !b1.valid()) {
436 logfile <<
" ... cannot express as A + BC = AB + C" << endl;
446 m_transfer[i][0][0] = r0;
447 m_transfer[i][1][0] = b0;
448 m_transfer[i][1][1] = p1;
454 m_transfer[i][1][1] = r1;
455 m_transfer[i][0][1] = b0;
456 m_transfer[i][0][0] = p0;
459 group_a0->fmt(logfile, m_elementSymbols);
461 group_b0->fmt(logfile,m_elementSymbols);
462 group_c0->fmt(logfile, m_elementSymbols);
464 group_a0->fmt(logfile, m_elementSymbols);
465 group_b0->fmt(logfile, m_elementSymbols);
467 group_c0->fmt(logfile, m_elementSymbols);
469 logfile <<
" [<= default] " << endl;
481 m_transfer[i][0][1] = r0;
482 m_transfer[i][1][1] = b0;
483 m_transfer[i][1][0] = p0;
491 m_transfer[i][1][0] = r1;
492 m_transfer[i][0][0] = b0;
493 m_transfer[i][0][1] = p1;
497 group_a1->fmt(logfile, m_elementSymbols);
499 group_b1->fmt(logfile, m_elementSymbols);
500 group_c1->fmt(logfile, m_elementSymbols);
502 group_a1->fmt(logfile, m_elementSymbols);
503 group_b1->fmt(logfile, m_elementSymbols);
505 group_c1->fmt(logfile, m_elementSymbols);
509 logfile <<
"... cannot parse. [ignored]" << endl;
515 void ReactionPathBuilder::findElements(Kinetics& kin)
519 for (
size_t i = 0; i < kin.nPhases(); i++) {
520 ThermoPhase* p = &kin.thermo(i);
522 for (
size_t m = 0; m < p->nElements(); m++) {
523 string ename = p->elementName(m);
529 if (m_enamemap.find(ename) == m_enamemap.end()) {
530 m_enamemap[ename] = m_nel + 1;
531 m_elementSymbols.push_back(ename);
536 m_atoms.resize(kin.nTotalSpecies(), m_nel, 0.0);
538 for (
size_t m = 0; m < m_nel; m++) {
541 for (
size_t ip = 0; ip < kin.nPhases(); ip++) {
542 ThermoPhase* p = &kin.thermo(ip);
543 size_t mlocal = p->elementIndex(m_elementSymbols[m]);
544 for (
size_t kp = 0; kp < p->nSpecies(); kp++) {
545 if (mlocal !=
npos) {
546 m_atoms(k, m) = p->nAtoms(kp, mlocal);
554 int ReactionPathBuilder::init(ostream& logfile, Kinetics& kin)
557 m_elementSymbols.clear();
559 m_ns = kin.nTotalSpecies();
560 m_nr = kin.nReactions();
564 vector<vector<size_t> > allProducts(m_nr);
565 vector<vector<size_t> > allReactants(m_nr);
566 for (
size_t i = 0; i < m_nr; i++) {
567 for (
size_t k = 0; k < m_ns; k++) {
568 for (
int n = 0; n < kin.reactantStoichCoeff(k, i); n++) {
569 allReactants[i].push_back(k);
571 for (
int n = 0; n < kin.productStoichCoeff(k, i); n++) {
572 allProducts[i].push_back(k);
583 m_determinate.resize(m_nr);
585 m_elatoms.resize(m_nel, m_nr);
587 for (
size_t i = 0; i < m_nr; i++) {
592 map<size_t, int> net;
593 size_t nr = allReactants[i].size();
594 size_t np = allProducts[i].size();
595 for (
size_t ir = 0; ir < nr; ir++) {
596 net[allReactants[i][ir]]--;
598 for (
size_t ip = 0; ip < np; ip++) {
599 net[allProducts[i][ip]]++;
602 for (
size_t k = 0; k < m_ns; k++) {
604 size_t nmol = -net[k];
605 for (
size_t jr = 0; jr < nmol; jr++) {
606 m_reac[i].push_back(k);
608 }
else if (net[k] > 0) {
609 size_t nmol = net[k];
610 for (
size_t jp = 0; jp < nmol; jp++) {
611 m_prod[i].push_back(k);
616 size_t nrnet = m_reac[i].size();
621 for (
size_t n = 0; n < nrnet; n++) {
622 size_t k = m_reac[i][n];
623 for (
size_t m = 0; m < m_nel; m++) {
624 m_elatoms(m,i) += m_atoms(k,m);
631 m_sgroup.resize(m_ns);
632 for (
size_t j = 0; j < m_ns; j++) {
633 for (
size_t m = 0; m < m_nel; m++) {
634 comp[m] = int(m_atoms(j,m));
636 m_sgroup[j] = Group(comp);
645 for (
size_t i = 0; i < m_nr; i++) {
646 size_t nr = m_reac[i].size();
647 size_t np = m_prod[i].size();
648 m_determinate[i] =
true;
649 for (
size_t m = 0; m < m_nel; m++) {
652 for (
size_t j = 0; j < nr; j++) {
653 if (m_atoms(m_reac[i][j],m) > 0) {
657 for (
size_t j = 0; j < np; j++) {
658 if (m_atoms(m_prod[i][j],m) > 0) {
662 if (nar > 1 && nap > 1) {
663 m_determinate[i] =
false;
669 findGroups(logfile, kin);
673 string reactionLabel(
size_t i,
size_t kr,
size_t nr,
674 const std::vector<size_t>& slist,
const Kinetics& s)
677 for (
size_t j = 0; j < nr; j++) {
679 label +=
" + "+ s.kineticsSpeciesName(slist[j]);
690 int ReactionPathBuilder::build(Kinetics& s,
const string& element,
691 ostream& output, ReactionPathDiagram& r,
bool quiet)
693 map<size_t, int> warn;
694 doublereal threshold = 0.0;
695 size_t m = m_enamemap[element]-1;
701 s.getFwdRatesOfProgress(m_ropf.data());
702 s.getRevRatesOfProgress(m_ropr.data());
705 vector<string>& in_nodes = r.included();
706 vector<string>& out_nodes = r.excluded();
709 for (
size_t ni = 0; ni < in_nodes.size(); ni++) {
710 status[s.kineticsSpeciesIndex(in_nodes[ni])] = 1;
712 for (
size_t ne = 0; ne < out_nodes.size(); ne++) {
713 status[s.kineticsSpeciesIndex(out_nodes[ne])] = -1;
716 for (
size_t i = 0; i < m_nr; i++) {
717 double ropf = m_ropf[i];
718 double ropr = m_ropr[i];
721 if (m_elatoms(m, i) > 0) {
722 size_t nr = m_reac[i].size();
723 size_t np = m_prod[i].size();
725 for (
size_t kr = 0; kr < nr; kr++) {
726 size_t kkr = m_reac[i][kr];
727 string fwdlabel = reactionLabel(i, kr, nr, m_reac[i], s);
729 for (
size_t kp = 0; kp < np; kp++) {
730 size_t kkp = m_prod[i][kp];
731 string revlabel =
"";
732 for (
size_t j = 0; j < np; j++) {
734 revlabel +=
" + "+ s.kineticsSpeciesName(m_prod[i][j]);
740 revlabel +=
" (+ M)";
746 if ((kkr != kkp) && (m_atoms(kkr,m) > 0
747 && m_atoms(kkp,m) > 0)
748 && status[kkr] >= 0 && status[kkp] >= 0) {
755 if ((m_atoms(kkp,m) < m_elatoms(m, i)) &&
756 (m_atoms(kkr,m) < m_elatoms(m, i))) {
757 map<size_t, map<size_t, Group> >& g = m_transfer[i];
759 if (!warn[i] && !quiet) {
761 output <<
"*************** REACTION IGNORED ***************" << endl;
762 output <<
"Warning: no rule to determine partitioning of " << element
763 << endl <<
" in reaction " << s.reactionString(i) <<
"." << endl
764 <<
"*************** REACTION IGNORED **************" << endl;
773 f = g[kr][kp].nAtoms(m);
782 f = m_atoms(kkp,m) * m_atoms(kkr,m) / m_elatoms(m, i);
787 bool force_incl = ((status[kkr] == 1) || (status[kkp] == 1));
789 bool fwd_incl = ((fwd > threshold) ||
790 (fwd > 0.0 && force_incl));
791 bool rev_incl = ((rev > threshold) ||
792 (rev > 0.0 && force_incl));
793 if (fwd_incl || rev_incl) {
794 if (!r.hasNode(kkr)) {
795 r.addNode(kkr, s.kineticsSpeciesName(kkr), m_x[kkr]);
797 if (!r.hasNode(kkp)) {
798 r.addNode(kkp, s.kineticsSpeciesName(kkp), m_x[kkp]);
802 r.linkNodes(kkr, kkp,
int(i), fwd, fwdlabel);
805 r.linkNodes(kkp, kkr, -
int(i), rev, revlabel);
Classes for reaction path analysis.
Base class for exceptions thrown by Cantera classes.
doublereal netFlow(size_t k1, size_t k2)
The net flow from node k1 to node k2.
void exportToDot(std::ostream &s)
Export the reaction path diagram.
virtual ~ReactionPathDiagram()
Destructor.
doublereal flow(size_t k1, size_t k2)
The one-way flow from node k1 to node k2.
Nodes in reaction path graphs.
std::string name
Label on graph.
bool visible
Visible on graph;.
const size_t npos
index returned by functions to indicate "no position"
std::vector< int > vector_int
Vector of ints.
Namespace for the Cantera kernel.
const int THREE_BODY_RXN
A gas-phase reaction that requires a third-body collision partner.
const int FALLOFF_RXN
The general form for a gas-phase association or dissociation reaction, with a pressure-dependent rate...
This file defines some constants used to specify reaction types.