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