Cantera  3.1.0a1
ReactionPath.h
Go to the documentation of this file.
1 /**
2  * @file ReactionPath.h
3  * Classes for reaction path analysis.
4  */
5 
6 // This file is part of Cantera. See License.txt in the top-level directory or
7 // at https://cantera.org/license.txt for license and copyright information.
8 
9 #ifndef CT_RXNPATH_H
10 #define CT_RXNPATH_H
11 
13 #include "Group.h"
14 #include "Kinetics.h"
15 
16 namespace Cantera
17 {
18 enum flow_t { NetFlow, OneWayFlow };
19 
20 // forward references
21 class Path;
22 
23 /**
24  * Nodes in reaction path graphs.
25  */
27 {
28 public:
29  //! Default constructor
30  SpeciesNode() = default;
31 
32  //! Destructor
33  virtual ~SpeciesNode() = default;
34 
35  // public attributes
36  size_t number = npos; //!< Species number
37  string name; //!< Label on graph
38  double value = 0.0; //!< May be used to set node appearance
39  bool visible = false; //!< Visible on graph;
40 
41  //! @name References
42  //!
43  //! Return a reference to a path object connecting this node
44  //! to another node.
45  //! @{
46  Path* path(int n) {
47  return m_paths[n];
48  }
49  const Path* path(int n) const {
50  return m_paths[n];
51  }
52  //! @}
53 
54  //! Total number of paths to or from this node
55  int nPaths() const {
56  return static_cast<int>(m_paths.size());
57  }
58 
59  //! add a path to or from this node
60  void addPath(Path* path);
61 
62  double outflow() {
63  return m_out;
64  }
65  double inflow() {
66  return m_in;
67  }
68  double netOutflow() {
69  return m_out - m_in;
70  }
71 
72  void printPaths();
73 
74 protected:
75  double m_in = 0.0;
76  double m_out = 0.0;
77  vector<Path*> m_paths;
78 };
79 
80 
81 class Path
82 {
83 public:
84  typedef map<size_t, double> rxn_path_map;
85 
86  /**
87  * Constructor. Construct a one-way path from @c begin to @c end.
88  */
89  Path(SpeciesNode* begin, SpeciesNode* end);
90 
91  //! Destructor
92  virtual ~Path() {}
93 
94  /**
95  * Add a reaction to the path. Increment the flow from this reaction, the
96  * total flow, and the flow associated with this label.
97  */
98  void addReaction(size_t rxnNumber, double value, const string& label = "");
99 
100  //! Upstream node.
101  const SpeciesNode* begin() const {
102  return m_a;
103  }
104  SpeciesNode* begin() {
105  return m_a;
106  }
107 
108  //! Downstream node.
109  const SpeciesNode* end() const {
110  return m_b;
111  }
112  SpeciesNode* end() {
113  return m_b;
114  }
115 
116  /**
117  * If @c n is one of the nodes this path connects, then
118  * the other node is returned. Otherwise zero is returned.
119  */
120  SpeciesNode* otherNode(SpeciesNode* n) {
121  return (n == m_a ? m_b : (n == m_b ? m_a : 0));
122  }
123 
124  //! The total flow in this path
125  double flow() {
126  return m_total;
127  }
128  void setFlow(double v) {
129  m_total = v;
130  }
131 
132  //! Number of reactions contributing to this path
133  int nReactions() {
134  return static_cast<int>(m_rxn.size());
135  }
136 
137  //! Map from reaction number to flow from that reaction in this path.
138  const rxn_path_map& reactionMap() {
139  return m_rxn;
140  }
141 
142  /**
143  * Write the label for a path connecting two species, indicating
144  * the percent of the total flow due to each reaction.
145  */
146  void writeLabel(std::ostream& s, double threshold = 0.005);
147 
148 protected:
149  map<string, double> m_label;
150  SpeciesNode* m_a, *m_b;
151  rxn_path_map m_rxn;
152  double m_total = 0.0;
153 };
154 
155 
156 /**
157  * Reaction path diagrams (graphs).
158  */
160 {
161 public:
162  ReactionPathDiagram() = default;
163 
164  /**
165  * Destructor. Deletes all nodes and paths in the diagram.
166  */
167  virtual ~ReactionPathDiagram();
168 
169  //! The largest one-way flow value in any path
170  double maxFlow() {
171  return m_flxmax;
172  }
173 
174  //! The net flow from node @c k1 to node @c k2
175  double netFlow(size_t k1, size_t k2) {
176  return flow(k1, k2) - flow(k2, k1);
177  }
178 
179  //! The one-way flow from node @c k1 to node @c k2
180  double flow(size_t k1, size_t k2) {
181  return (m_paths[k1][k2] ? m_paths[k1][k2]->flow() : 0.0);
182  }
183 
184  //! True if a node for species k exists
185  bool hasNode(size_t k) {
186  return (m_nodes[k] != 0);
187  }
188 
189  void writeData(std::ostream& s);
190 
191  /**
192  * Export the reaction path diagram. This method writes to stream
193  * @c s the commands for the 'dot' program in the @c GraphViz
194  * package from AT&T. (GraphViz may be downloaded from www.graphviz.org.)
195  *
196  * To generate a postscript reaction path diagram from the output of this
197  * method saved in file paths.dot, for example, give the command:
198  * @code
199  * dot -Tps paths.dot > paths.ps
200  * @endcode
201  * To generate a GIF image, replace -Tps with -Tgif
202  */
203  void exportToDot(std::ostream& s);
204 
205  void add(ReactionPathDiagram& d);
206  SpeciesNode* node(size_t k) {
207  return m_nodes[k];
208  }
209  Path* path(size_t k1, size_t k2) {
210  return m_paths[k1][k2];
211  }
212  Path* path(size_t n) {
213  return m_pathlist[n];
214  }
215  size_t nPaths() {
216  return m_pathlist.size();
217  }
218  size_t nNodes() {
219  return m_nodes.size();
220  }
221 
222  void addNode(size_t k, const string& nm, double x = 0.0);
223 
224  void displayOnly(size_t k=npos) {
225  m_local = k;
226  }
227 
228  void linkNodes(size_t k1, size_t k2, size_t rxn, double value, string legend = "");
229 
230  void include(const string& aaname) {
231  m_include.push_back(aaname);
232  }
233  void exclude(const string& aaname) {
234  m_exclude.push_back(aaname);
235  }
236  void include(vector<string>& names) {
237  for (size_t i = 0; i < names.size(); i++) {
238  m_include.push_back(names[i]);
239  }
240  }
241  void exclude(vector<string>& names) {
242  for (size_t i = 0; i < names.size(); i++) {
243  m_exclude.push_back(names[i]);
244  }
245  }
246  vector<string>& included() {
247  return m_include;
248  }
249  vector<string>& excluded() {
250  return m_exclude;
251  }
252  vector<size_t> species();
253  vector<int> reactions();
254  void findMajorPaths(double threshold, size_t lda, double* a);
255  void setFont(const string& font) {
256  m_font = font;
257  }
258  // public attributes
259 
260  string title;
261  string bold_color = "blue";
262  string normal_color = "steelblue";
263  string dashed_color = "gray";
264  string element;
265  string m_font = "Helvetica";
266  double threshold = 0.005;
267  double bold_min = 0.2;
268  double dashed_max = 0.0;
269  double label_min = 0.0;
270  double x_size = -1.0;
271  double y_size = -1.0;
272  string name = "reaction_paths";
273  string dot_options = "center=1;";
274  flow_t flow_type = NetFlow;
275  double scale = -1;
276  double arrow_width = -5.0;
277  bool show_details = false;
278  double arrow_hue = 0.6666;
279 
280 protected:
281  double m_flxmax = 0.0;
282  map<size_t, map<size_t, Path*>> m_paths;
283 
284  //! map of species index to SpeciesNode
285  map<size_t, SpeciesNode*> m_nodes;
286  vector<Path*> m_pathlist;
287  vector<string> m_include;
288  vector<string> m_exclude;
289  vector<size_t> m_speciesNumber;
290 
291  //! Indices of reactions that are included in the diagram
292  set<size_t> m_rxns;
293  size_t m_local = npos;
294 };
295 
296 
297 class ReactionPathBuilder
298 {
299 public:
300  ReactionPathBuilder() = default;
301  virtual ~ReactionPathBuilder() = default;
302 
303  int init(std::ostream& logfile, Kinetics& s);
304 
305  int build(Kinetics& s, const string& element, std::ostream& output,
306  ReactionPathDiagram& r, bool quiet=false);
307 
308  //! Analyze a reaction to determine which reactants lead to which products.
309  int findGroups(std::ostream& logfile, Kinetics& s);
310 
311 protected:
312  void findElements(Kinetics& kin);
313 
314  size_t m_nr;
315  size_t m_ns;
316  size_t m_nel;
317  vector<double> m_ropf;
318  vector<double> m_ropr;
319  vector<double> m_x;
320  vector<vector<size_t>> m_reac;
321  vector<vector<size_t>> m_prod;
322  DenseMatrix m_elatoms;
323  vector<vector<int>> m_groups;
324  vector<Group> m_sgroup;
325  vector<string> m_elementSymbols;
326 
327  //! m_transfer[reaction][reactant number][product number] where "reactant
328  //! number" means the number of the reactant in the reaction equation. For example,
329  //! for "A+B -> C+D", "B" is reactant number 1 and "C" is product number 0.
330  map<size_t, map<size_t, map<size_t, Group>>> m_transfer;
331 
332  vector<bool> m_determinate;
333  Array2D m_atoms;
334  map<string, size_t> m_enamemap;
335 };
336 
337 }
338 
339 #endif
Headers for the DenseMatrix object, which deals with dense rectangular matrices and description of th...
Base class for kinetics managers and also contains the kineticsmgr module documentation (see Kinetics...
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Definition: Array.h:32
A class for full (non-sparse) matrices with Fortran-compatible data storage, which adds matrix operat...
Definition: DenseMatrix.h:55
Public interface for kinetics managers.
Definition: Kinetics.h:125
Reaction path diagrams (graphs).
Definition: ReactionPath.h:160
map< size_t, SpeciesNode * > m_nodes
map of species index to SpeciesNode
Definition: ReactionPath.h:285
void exportToDot(std::ostream &s)
Export the reaction path diagram.
bool hasNode(size_t k)
True if a node for species k exists.
Definition: ReactionPath.h:185
set< size_t > m_rxns
Indices of reactions that are included in the diagram.
Definition: ReactionPath.h:292
virtual ~ReactionPathDiagram()
Destructor.
double maxFlow()
The largest one-way flow value in any path.
Definition: ReactionPath.h:170
double flow(size_t k1, size_t k2)
The one-way flow from node k1 to node k2.
Definition: ReactionPath.h:180
double netFlow(size_t k1, size_t k2)
The net flow from node k1 to node k2.
Definition: ReactionPath.h:175
Nodes in reaction path graphs.
Definition: ReactionPath.h:27
virtual ~SpeciesNode()=default
Destructor.
string name
Label on graph.
Definition: ReactionPath.h:37
bool visible
Visible on graph;.
Definition: ReactionPath.h:39
int nPaths() const
Total number of paths to or from this node.
Definition: ReactionPath.h:55
SpeciesNode()=default
Default constructor.
double value
May be used to set node appearance.
Definition: ReactionPath.h:38
size_t number
Species number.
Definition: ReactionPath.h:36
void addPath(Path *path)
add a path to or from this node
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:564
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:180