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