Cantera  2.0
ReactionPath.h
Go to the documentation of this file.
1 /**
2  * @file ReactionPath.h
3  *
4  * Classes for reaction path analysis.
5  */
6 
7 // Copyright 2001 California Institute of Technology
8 
9 #ifndef CT_RXNPATH_H
10 #define CT_RXNPATH_H
11 
12 // Cantera includes
13 #include "cantera/base/ct_defs.h"
15 #include "Group.h"
16 #include "Kinetics.h"
17 
18 namespace Cantera
19 {
20 
21 enum flow_t { NetFlow, OneWayFlow };
22 
23 Group parseGroupString(std::string str, std::vector<std::string>& esyms);
24 
25 // forward references
26 class Path;
27 
28 /**
29  * Nodes in reaction path graphs.
30  */
32 {
33 public:
34  /// Default constructor
35  SpeciesNode() : number(npos), name(""), value(0.0),
36  visible(false), m_in(0.0), m_out(0.0) {}
37 
38  /// Destructor
39  virtual ~SpeciesNode() {}
40 
41  // public attributes
42  size_t number; ///< Species number
43  std::string name; ///< Label on graph
44  doublereal value; ///< May be used to set node appearance
45  bool visible; ///< Visible on graph;
46 
47 
48  // public methods
49 
50  /**
51  * @name References.
52  * Return a reference to a path object connecting this node
53  * to another node.
54  */
55  //@{
56  Path* path(int n) {
57  return m_paths[n];
58  }
59  const Path* path(int n) const {
60  return m_paths[n];
61  }
62  //@}
63 
64 
65  /// Total number of paths to or from this node
66  int nPaths() const {
67  return static_cast<int>(m_paths.size());
68  }
69 
70  /// add a path to or from this node
71  void addPath(Path* path);
72 
73  doublereal outflow() {
74  return m_out;
75  }
76  doublereal inflow() {
77  return m_in;
78  }
79  doublereal netOutflow() {
80  return m_out - m_in;
81  }
82 
83  void printPaths();
84 
85 
86 protected:
87  doublereal m_in;
88  doublereal m_out;
89  std::vector<Path*> m_paths;
90 };
91 
92 
93 
94 class Path
95 {
96 
97 public:
98 
99  typedef std::map<size_t, doublereal> rxn_path_map;
100 
101  /**
102  * Constructor. Construct a one-way path from
103  * \c begin to \c end.
104  */
105  Path(SpeciesNode* begin, SpeciesNode* end);
106 
107  /// Destructor
108  virtual ~Path() {}
109 
110  void addReaction(size_t rxnNumber, doublereal value, std::string label = "");
111 
112  /// Upstream node.
113  const SpeciesNode* begin() const {
114  return m_a;
115  }
116  SpeciesNode* begin() {
117  return m_a;
118  }
119 
120  /// Downstream node.
121  const SpeciesNode* end() const {
122  return m_b;
123  }
124  SpeciesNode* end() {
125  return m_b;
126  }
127 
128  /**
129  * If \c n is one of the nodes this path connects, then
130  * the other node is returned. Otherwise zero is returned.
131  */
132  SpeciesNode* otherNode(SpeciesNode* n) {
133  return (n == m_a ? m_b : (n == m_b ? m_a : 0));
134  }
135 
136  /// The total flow in this path
137  doublereal flow() {
138  return m_total;
139  }
140  void setFlow(doublereal v) {
141  m_total = v;
142  }
143 
144  /// Number of reactions contributing to this path
145  int nReactions() {
146  return static_cast<int>(m_rxn.size());
147  }
148 
149  /// Map from reaction number to flow from that reaction in this path.
150  const rxn_path_map& reactionMap() {
151  return m_rxn;
152  }
153 
154  void writeLabel(std::ostream& s, doublereal threshold = 0.005);
155 
156 protected:
157 
158  std::map<std::string, doublereal> m_label;
159  SpeciesNode* m_a, *m_b;
160  rxn_path_map m_rxn;
161  doublereal m_total;
162 };
163 
164 
165 /**
166  * Reaction path diagrams (graphs).
167  */
169 {
170 
171 public:
172 
174 
175  virtual ~ReactionPathDiagram();
176 
177  /// The largest one-way flow value in any path
178  doublereal maxFlow() {
179  return m_flxmax;
180  }
181 
182  /// The net flow from node \c k1 to node \c k2
183  doublereal netFlow(size_t k1, size_t k2) {
184  return flow(k1, k2) - flow(k2, k1);
185  }
186 
187  /// The one-way flow from node \c k1 to node \c k2
188  doublereal flow(size_t k1, size_t k2) {
189  return (m_paths[k1][k2] ? m_paths[k1][k2]->flow() : 0.0);
190  }
191 
192  /// True if a node for species k exists
193  bool hasNode(size_t k) {
194  return (m_nodes[k] != 0);
195  }
196 
197  void writeData(std::ostream& s);
198  void exportToDot(std::ostream& s);
199  void add(ReactionPathDiagram& d);
200  SpeciesNode* node(size_t k) {
201  return m_nodes[k];
202  }
203  Path* path(size_t k1, size_t k2) {
204  return m_paths[k1][k2];
205  }
206  Path* path(size_t n) {
207  return m_pathlist[n];
208  }
209  size_t nPaths() {
210  return m_pathlist.size();
211  }
212  size_t nNodes() {
213  return m_nodes.size();
214  }
215 
216  void addNode(size_t k, std::string nm, doublereal x = 0.0);
217 
218  void displayOnly(size_t k=npos) {
219  m_local = k;
220  }
221 
222  void linkNodes(size_t k1, size_t k2, size_t rxn, doublereal value,
223  std::string legend = "");
224 
225  void include(std::string aaname) {
226  m_include.push_back(aaname);
227  }
228  void exclude(std::string aaname) {
229  m_exclude.push_back(aaname);
230  }
231  void include(std::vector<std::string>& names) {
232  for (size_t i = 0; i < names.size(); i++) {
233  m_include.push_back(names[i]);
234  }
235  }
236  void exclude(std::vector<std::string>& names) {
237  for (size_t i = 0; i < names.size(); i++) {
238  m_exclude.push_back(names[i]);
239  }
240  }
241  std::vector<std::string>& included() {
242  return m_include;
243  }
244  std::vector<std::string>& excluded() {
245  return m_exclude;
246  }
247  std::vector<size_t> species();
248  vector_int reactions();
249  void findMajorPaths(doublereal threshold, size_t lda, doublereal* a);
250  void setFont(std::string font) {
251  m_font = font;
252  }
253  // public attributes
254 
255  std::string title;
256  std::string bold_color;
257  std::string normal_color;
258  std::string dashed_color;
259  std::string element;
260  std::string m_font;
261  doublereal threshold,
262  bold_min, dashed_max, label_min;
263  doublereal x_size, y_size;
264  std::string name, dot_options;
265  flow_t flow_type;
266  doublereal scale;
267  doublereal arrow_width;
268  bool show_details;
269  doublereal arrow_hue;
270 
271 protected:
272 
273  doublereal m_flxmax;
274  std::map<size_t, std::map<size_t, Path*> > m_paths;
275  std::map<size_t, SpeciesNode*> m_nodes;
276  std::vector<Path*> m_pathlist;
277  std::vector<std::string> m_include;
278  std::vector<std::string> m_exclude;
279  std::vector<size_t> m_speciesNumber;
280  std::map<size_t, int> m_rxns;
281  size_t m_local;
282 };
283 
284 
285 
286 class ReactionPathBuilder
287 {
288 
289 public:
290  ReactionPathBuilder() {}
291  virtual ~ReactionPathBuilder() {}
292 
293  int init(std::ostream& logfile, Kinetics& s);
294 
295  int build(Kinetics& s, std::string element, std::ostream& output,
296  ReactionPathDiagram& r, bool quiet=false);
297 
298  int findGroups(std::ostream& logfile, Kinetics& s);
299 
300  void writeGroup(std::ostream& out, const Group& g);
301 
302 protected:
303  void findElements(Kinetics& kin);
304 
305  size_t m_nr;
306  size_t m_ns;
307  size_t m_nel;
308  vector_fp m_ropf;
309  vector_fp m_ropr;
310  vector_fp m_x;
311  std::vector<std::vector<size_t> > m_reac;
312  std::vector<std::vector<size_t> > m_prod;
313  DenseMatrix m_elatoms;
314  std::vector<std::vector<int> > m_groups;
315  std::vector<Group> m_sgroup;
316  std::vector<std::string> m_elementSymbols;
317  // std::map<int, int> m_warn;
318  std::map<size_t, std::map<size_t, std::map<size_t, Group> > > m_transfer;
319  std::vector<bool> m_determinate;
320  Array2D m_atoms;
321  std::map<std::string, size_t> m_enamemap;
322 };
323 
324 }
325 
326 #endif