13#include <boost/algorithm/string.hpp>
15namespace ba = boost::algorithm;
24 m_paths.push_back(path);
25 if (path->begin() ==
this) {
26 m_out += path->flow();
27 }
else if (path->end() ==
this) {
30 throw CanteraError(
"SpeciesNode::addPath",
"path added to wrong node");
34void SpeciesNode::printPaths()
36 for (
size_t i = 0; i < m_paths.size(); i++) {
37 cout << m_paths[i]->begin()->name <<
" --> "
38 << m_paths[i]->end()->name <<
": "
39 << m_paths[i]->flow() << endl;
43Path::Path(SpeciesNode* begin, SpeciesNode* end)
44 : m_a(begin), m_b(end), m_total(0.0)
50void Path::addReaction(
size_t rxnNumber, doublereal value,
53 m_rxn[rxnNumber] += value;
56 m_label[label] += value;
60void Path::writeLabel(ostream& s, doublereal threshold)
62 if (m_label.size() == 0) {
66 for (
const auto& label : m_label) {
67 v = label.second/m_total;
68 if (m_label.size() == 1) {
69 s << label.first <<
"\\l";
70 }
else if (v > threshold) {
72 int percent = int(100*v + 0.5);
74 s <<
" (" << percent <<
"%)\\l";
82ReactionPathDiagram::ReactionPathDiagram()
84 name =
"reaction_paths";
87 normal_color =
"steelblue";
88 dashed_color =
"gray";
89 dot_options =
"center=1;";
100 show_details =
false;
109 for (
const auto& node : m_nodes) {
114 size_t nn = nPaths();
115 for (
size_t n = 0; n < nn; n++) {
116 delete m_pathlist[n];
122 doublereal flmax = 0.0;
123 for (
size_t i = 0; i < nPaths(); i++) {
125 flmax = std::max(p->flow(), flmax);
128 for (
size_t i = 0; i < nPaths(); i++) {
129 for (
const auto& rxn : path(i)->reactionMap()) {
130 double flxratio = rxn.second/flmax;
131 if (flxratio > threshold) {
132 m_rxns[rxn.first] = 1;
137 for (
const auto& rxn : m_rxns) {
138 r.push_back(
int(rxn.first));
143void ReactionPathDiagram::add(ReactionPathDiagram& d)
145 for (
size_t n = 0; n < nPaths(); n++) {
147 size_t k1 = p->begin()->number;
148 size_t k2 = p->end()->number;
149 p->setFlow(p->flow() + d.flow(k1,k2));
153void ReactionPathDiagram::findMajorPaths(doublereal athreshold,
size_t lda,
157 for (
size_t n = 0; n < nNodes(); n++) {
158 for (
size_t m = n+1; m < nNodes(); m++) {
159 size_t k1 = m_speciesNumber[n];
160 size_t k2 = m_speciesNumber[m];
161 double fl = fabs(
netFlow(k1,k2));
162 netmax = std::max(fl, netmax);
165 for (
size_t n = 0; n < nNodes(); n++) {
166 for (
size_t m = n+1; m < nNodes(); m++) {
167 size_t k1 = m_speciesNumber[n];
168 size_t k2 = m_speciesNumber[m];
169 double fl = fabs(
netFlow(k1,k2));
170 if (fl > athreshold*netmax) {
177void ReactionPathDiagram::writeData(ostream& s)
180 for (
size_t i1 = 0; i1 < nNodes(); i1++) {
181 size_t k1 = m_speciesNumber[i1];
182 s << m_nodes[k1]->name <<
" ";
185 for (
size_t i1 = 0; i1 < nNodes(); i1++) {
186 size_t k1 = m_speciesNumber[i1];
187 for (
size_t i2 = i1+1; i2 < nNodes(); i2++) {
188 size_t k2 = m_speciesNumber[i2];
189 double f1 =
flow(k1, k2);
190 double f2 =
flow(k2, k1);
191 s << m_nodes[k1]->name <<
" " << m_nodes[k2]->name
192 <<
" " << f1 <<
" " << -f2 << endl;
199 doublereal flmax = 0.0;
203 s <<
"digraph " << name <<
" {" << endl;
216 if (dot_options !=
"") {
217 s << dot_options << endl;
221 if (flow_type == NetFlow) {
225 for (
size_t i1 = 0; i1 < nNodes(); i1++) {
226 size_t k1 = m_speciesNumber[i1];
228 for (
size_t i2 = i1+1; i2 < nNodes(); i2++) {
229 size_t k2 = m_speciesNumber[i2];
234 flmax = std::max(flx, flmax);
240 flmax = std::max(flmax, 1e-10);
243 for (
size_t i1 = 0; i1 < nNodes(); i1++) {
244 size_t k1 = m_speciesNumber[i1];
245 for (
size_t i2 = i1+1; i2 < nNodes(); i2++) {
246 size_t k2 = m_speciesNumber[i2];
248 if (m_local !=
npos && k1 != m_local && k2 != m_local) {
259 flxratio = flx/flmax;
263 flxratio = -flx/flmax;
268 if (flxratio >= threshold) {
273 s <<
"s" << kbegin <<
" -> s" << kend;
274 s <<
"[fontname=\""+m_font+
"\", penwidth=";
276 if (arrow_width < 0) {
277 double lwidth = 1.0 - 4.0
278 * log10(flxratio/threshold)/log10(threshold) + 1.0;
281 << std::min(6.0, 0.5*lwidth);
284 s <<
", arrowsize=" << flxratio + 1;
287 doublereal hue = 0.7;
288 doublereal bright = 0.9;
289 s <<
", color=" <<
"\"" << hue <<
", "
291 <<
", " << bright <<
"\"" << endl;
293 if (flxratio > label_min) {
294 s <<
", label=\" " << flxratio;
296 if (
flow(kbegin, kend) > 0.0) {
298 <<
flow(kbegin, kend)/flmax <<
"\\l";
299 path(kbegin, kend)->writeLabel(s);
301 if (
flow(kend, kbegin) > 0.0) {
303 <<
flow(kend,kbegin)/flmax <<
"\\l";
304 path(kend, kbegin)->writeLabel(s);
316 for (
size_t i = 0; i < nPaths(); i++) {
317 flmax = std::max(path(i)->
flow(), flmax);
323 for (
size_t i = 0; i < nPaths(); i++) {
325 double flxratio = p->flow()/flmax;
326 if (m_local !=
npos) {
327 if (p->begin()->number != m_local
328 && p->end()->number != m_local) {
332 if (flxratio > threshold) {
333 p->begin()->visible =
true;
334 p->end()->visible =
true;
335 s <<
"s" << p->begin()->number
336 <<
" -> s" << p->end()->number;
338 if (arrow_width < 0) {
339 double lwidth = 1.0 - 4.0 * log10(flxratio/threshold)/log10(threshold)
341 s <<
"[fontname=\""+m_font+
"\", penwidth="
344 << std::min(6.0, 0.5*lwidth);
348 s <<
", arrowsize=" << flxratio + 1;
350 doublereal hue = 0.7;
351 doublereal bright = 0.9;
352 s <<
", color=" <<
"\"" << hue <<
", " << flxratio + 0.5
353 <<
", " << bright <<
"\"" << endl;
355 if (flxratio > label_min) {
356 s <<
", label = \" " << flxratio;
368 for (
const auto& node : m_nodes) {
370 s <<
"s" << node.first <<
" [ fontname=\""+m_font+
"\", label=\"" << node.second->
name
374 s <<
" label = " <<
"\"" <<
"Scale = "
375 << flmax <<
"\\l " << title <<
"\";" << endl;
376 s <<
" fontname = \""+m_font+
"\";" << endl <<
"}" << endl;
380void ReactionPathDiagram::addNode(
size_t k,
const string& nm, doublereal x)
384 m_nodes[k]->number = k;
385 m_nodes[k]->name = nm;
386 m_nodes[k]->value = x;
387 m_speciesNumber.push_back(k);
391void ReactionPathDiagram::linkNodes(
size_t k1,
size_t k2,
size_t rxn,
392 doublereal value,
string legend)
394 Path* ff = m_paths[k1][k2];
396 ff=
new Path(m_nodes[k1], m_nodes[k2]);
397 m_paths[k1][k2] = ff;
398 m_pathlist.push_back(ff);
400 ff->addReaction(rxn, value, legend);
402 m_flxmax = std::max(ff->flow(), m_flxmax);
405std::vector<size_t> ReactionPathDiagram::species()
407 return m_speciesNumber;
410int ReactionPathBuilder::findGroups(ostream& logfile, Kinetics& s)
412 m_groups.resize(m_nr);
413 for (
size_t i = 0; i < m_nr; i++) {
414 logfile << endl <<
"Reaction " << i+1 <<
": "
415 << s.reactionString(i);
417 if (m_determinate[i]) {
418 logfile <<
" ... OK." << endl;
419 }
else if (m_reac[i].size() == 2 && m_prod[i].size() == 2) {
421 size_t kr0 = m_reac[i][0];
422 size_t kr1 = m_reac[i][1];
425 size_t kp0 = m_prod[i][0];
426 size_t kp1 = m_prod[i][1];
429 const Group& r0 = m_sgroup[kr0];
430 const Group& r1 = m_sgroup[kr1];
431 const Group& p0 = m_sgroup[kp0];
432 const Group& p1 = m_sgroup[kp1];
434 const Group* group_a0=0, *group_b0=0, *group_c0=0,
435 *group_a1=0, *group_b1=0, *group_c1=0;
438 if (b0.valid() && b1.valid()) {
439 logfile <<
" ... ambiguous." << endl;
440 }
else if (!b0.valid() && !b1.valid()) {
441 logfile <<
" ... cannot express as A + BC = AB + C" << endl;
451 m_transfer[i][0][0] = r0;
452 m_transfer[i][1][0] = b0;
453 m_transfer[i][1][1] = p1;
459 m_transfer[i][1][1] = r1;
460 m_transfer[i][0][1] = b0;
461 m_transfer[i][0][0] = p0;
464 group_a0->fmt(logfile, m_elementSymbols);
466 group_b0->fmt(logfile,m_elementSymbols);
467 group_c0->fmt(logfile, m_elementSymbols);
469 group_a0->fmt(logfile, m_elementSymbols);
470 group_b0->fmt(logfile, m_elementSymbols);
472 group_c0->fmt(logfile, m_elementSymbols);
474 logfile <<
" [<= default] " << endl;
486 m_transfer[i][0][1] = r0;
487 m_transfer[i][1][1] = b0;
488 m_transfer[i][1][0] = p0;
496 m_transfer[i][1][0] = r1;
497 m_transfer[i][0][0] = b0;
498 m_transfer[i][0][1] = p1;
502 group_a1->fmt(logfile, m_elementSymbols);
504 group_b1->fmt(logfile, m_elementSymbols);
505 group_c1->fmt(logfile, m_elementSymbols);
507 group_a1->fmt(logfile, m_elementSymbols);
508 group_b1->fmt(logfile, m_elementSymbols);
510 group_c1->fmt(logfile, m_elementSymbols);
514 logfile <<
"... cannot parse. [ignored]" << endl;
520void ReactionPathBuilder::findElements(Kinetics& kin)
524 for (
size_t i = 0; i < kin.nPhases(); i++) {
525 ThermoPhase* p = &kin.thermo(i);
527 for (
size_t m = 0; m < p->nElements(); m++) {
528 string ename = p->elementName(m);
534 if (m_enamemap.find(ename) == m_enamemap.end()) {
535 m_enamemap[ename] = m_nel + 1;
536 m_elementSymbols.push_back(ename);
541 m_atoms.resize(kin.nTotalSpecies(), m_nel, 0.0);
543 for (
size_t m = 0; m < m_nel; m++) {
546 for (
size_t ip = 0; ip < kin.nPhases(); ip++) {
547 ThermoPhase* p = &kin.thermo(ip);
548 size_t mlocal = p->elementIndex(m_elementSymbols[m]);
549 for (
size_t kp = 0; kp < p->nSpecies(); kp++) {
550 if (mlocal !=
npos) {
551 m_atoms(k, m) = p->nAtoms(kp, mlocal);
559int ReactionPathBuilder::init(ostream& logfile, Kinetics& kin)
562 m_elementSymbols.clear();
564 m_ns = kin.nTotalSpecies();
565 m_nr = kin.nReactions();
569 vector<vector<size_t> > allProducts(m_nr);
570 vector<vector<size_t> > allReactants(m_nr);
571 for (
size_t i = 0; i < m_nr; i++) {
572 for (
size_t k = 0; k < m_ns; k++) {
573 for (
int n = 0; n < kin.reactantStoichCoeff(k, i); n++) {
574 allReactants[i].push_back(k);
576 for (
int n = 0; n < kin.productStoichCoeff(k, i); n++) {
577 allProducts[i].push_back(k);
588 m_determinate.resize(m_nr);
590 m_elatoms.resize(m_nel, m_nr);
592 for (
size_t i = 0; i < m_nr; i++) {
597 map<size_t, int> net;
598 size_t nr = allReactants[i].size();
599 size_t np = allProducts[i].size();
600 for (
size_t ir = 0; ir < nr; ir++) {
601 net[allReactants[i][ir]]--;
603 for (
size_t ip = 0; ip < np; ip++) {
604 net[allProducts[i][ip]]++;
607 for (
size_t k = 0; k < m_ns; k++) {
609 size_t nmol = -net[k];
610 for (
size_t jr = 0; jr < nmol; jr++) {
611 m_reac[i].push_back(k);
613 }
else if (net[k] > 0) {
614 size_t nmol = net[k];
615 for (
size_t jp = 0; jp < nmol; jp++) {
616 m_prod[i].push_back(k);
621 size_t nrnet = m_reac[i].size();
626 for (
size_t n = 0; n < nrnet; n++) {
627 size_t k = m_reac[i][n];
628 for (
size_t m = 0; m < m_nel; m++) {
629 m_elatoms(m,i) += m_atoms(k,m);
636 m_sgroup.resize(m_ns);
637 for (
size_t j = 0; j < m_ns; j++) {
638 for (
size_t m = 0; m < m_nel; m++) {
639 comp[m] = int(m_atoms(j,m));
641 m_sgroup[j] = Group(comp);
650 for (
size_t i = 0; i < m_nr; i++) {
651 size_t nr = m_reac[i].size();
652 size_t np = m_prod[i].size();
653 m_determinate[i] =
true;
654 for (
size_t m = 0; m < m_nel; m++) {
657 for (
size_t j = 0; j < nr; j++) {
658 if (m_atoms(m_reac[i][j],m) > 0) {
662 for (
size_t j = 0; j < np; j++) {
663 if (m_atoms(m_prod[i][j],m) > 0) {
667 if (nar > 1 && nap > 1) {
668 m_determinate[i] =
false;
674 findGroups(logfile, kin);
678string reactionLabel(
size_t i,
size_t kr,
size_t nr,
679 const std::vector<size_t>& slist,
const Kinetics& s)
682 for (
size_t j = 0; j < nr; j++) {
684 label +=
" + "+ s.kineticsSpeciesName(slist[j]);
687 if (ba::starts_with(s.reactionTypeStr(i),
"three-body")) {
689 }
else if (ba::starts_with(s.reactionTypeStr(i),
"falloff")) {
695int ReactionPathBuilder::build(Kinetics& s,
const string& element,
696 ostream& output, ReactionPathDiagram& r,
bool quiet)
698 map<size_t, int>
warn;
699 doublereal threshold = 0.0;
700 size_t m = m_enamemap[element]-1;
706 s.getFwdRatesOfProgress(m_ropf.data());
707 s.getRevRatesOfProgress(m_ropr.data());
710 vector<string>& in_nodes = r.included();
711 vector<string>& out_nodes = r.excluded();
714 for (
size_t ni = 0; ni < in_nodes.size(); ni++) {
715 status[s.kineticsSpeciesIndex(in_nodes[ni])] = 1;
717 for (
size_t ne = 0; ne < out_nodes.size(); ne++) {
718 status[s.kineticsSpeciesIndex(out_nodes[ne])] = -1;
721 for (
size_t i = 0; i < m_nr; i++) {
722 double ropf = m_ropf[i];
723 double ropr = m_ropr[i];
726 if (m_elatoms(m, i) > 0) {
727 size_t nr = m_reac[i].size();
728 size_t np = m_prod[i].size();
730 for (
size_t kr = 0; kr < nr; kr++) {
731 size_t kkr = m_reac[i][kr];
732 string fwdlabel = reactionLabel(i, kr, nr, m_reac[i], s);
734 for (
size_t kp = 0; kp < np; kp++) {
735 size_t kkp = m_prod[i][kp];
736 string revlabel =
"";
737 for (
size_t j = 0; j < np; j++) {
739 revlabel +=
" + "+ s.kineticsSpeciesName(m_prod[i][j]);
742 if (ba::starts_with(s.reactionTypeStr(i),
"three-body")) {
744 }
else if (ba::starts_with(s.reactionTypeStr(i),
"falloff")) {
745 revlabel +=
" (+ M)";
751 if ((kkr != kkp) && (m_atoms(kkr,m) > 0
752 && m_atoms(kkp,m) > 0)
753 && status[kkr] >= 0 && status[kkp] >= 0) {
760 if ((m_atoms(kkp,m) < m_elatoms(m, i)) &&
761 (m_atoms(kkr,m) < m_elatoms(m, i))) {
762 map<size_t, map<size_t, Group> >& g = m_transfer[i];
764 if (!
warn[i] && !quiet) {
766 output <<
"*************** REACTION IGNORED ***************" << endl;
767 output <<
"Warning: no rule to determine partitioning of " << element
768 << endl <<
" in reaction " << s.reactionString(i) <<
"." << endl
769 <<
"*************** REACTION IGNORED **************" << endl;
778 f = g[kr][kp].nAtoms(m);
787 f = m_atoms(kkp,m) * m_atoms(kkr,m) / m_elatoms(m, i);
792 bool force_incl = ((status[kkr] == 1) || (status[kkp] == 1));
794 bool fwd_incl = ((fwd > threshold) ||
795 (fwd > 0.0 && force_incl));
796 bool rev_incl = ((rev > threshold) ||
797 (rev > 0.0 && force_incl));
798 if (fwd_incl || rev_incl) {
799 if (!r.hasNode(kkr)) {
800 r.addNode(kkr, s.kineticsSpeciesName(kkr), m_x[kkr]);
802 if (!r.hasNode(kkp)) {
803 r.addNode(kkp, s.kineticsSpeciesName(kkp), m_x[kkp]);
807 r.linkNodes(kkr, kkp,
int(i), fwd, fwdlabel);
810 r.linkNodes(kkp, kkr, -
int(i), rev, revlabel);
Classes for reaction path analysis.
Header file for class ThermoPhase, the base class for phases with thermodynamic properties,...
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;.
void addPath(Path *path)
add a path to or from this node
Namespace for the Cantera kernel.
const size_t npos
index returned by functions to indicate "no position"
std::vector< int > vector_int
Vector of ints.
void warn(const std::string &warning, const std::string &method, const std::string &msg, const Args &... args)
Print a generic warning raised from method.
This file defines some constants used to specify reaction types.