Cantera  2.4.0
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 http://www.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
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 \c begin to \c end.
90  */
91  Path(SpeciesNode* begin, SpeciesNode* end);
92 
93  /// Destructor
94  virtual ~Path() {}
95 
96  /**
97  * Add a reaction to the path. Increment the flow from this reaction, the
98  * total flow, and the flow associated with this label.
99  */
100  void addReaction(size_t rxnNumber, doublereal value,
101  const std::string& label = "");
102 
103  /// Upstream node.
104  const SpeciesNode* begin() const {
105  return m_a;
106  }
107  SpeciesNode* begin() {
108  return m_a;
109  }
110 
111  /// Downstream node.
112  const SpeciesNode* end() const {
113  return m_b;
114  }
115  SpeciesNode* end() {
116  return m_b;
117  }
118 
119  /**
120  * If \c n is one of the nodes this path connects, then
121  * the other node is returned. Otherwise zero is returned.
122  */
123  SpeciesNode* otherNode(SpeciesNode* n) {
124  return (n == m_a ? m_b : (n == m_b ? m_a : 0));
125  }
126 
127  /// The total flow in this path
128  doublereal flow() {
129  return m_total;
130  }
131  void setFlow(doublereal v) {
132  m_total = v;
133  }
134 
135  /// Number of reactions contributing to this path
136  int nReactions() {
137  return static_cast<int>(m_rxn.size());
138  }
139 
140  /// Map from reaction number to flow from that reaction in this path.
141  const rxn_path_map& reactionMap() {
142  return m_rxn;
143  }
144 
145  /**
146  * Write the label for a path connecting two species, indicating
147  * the percent of the total flow due to each reaction.
148  */
149  void writeLabel(std::ostream& s, doublereal threshold = 0.005);
150 
151 protected:
152  std::map<std::string, doublereal> m_label;
153  SpeciesNode* m_a, *m_b;
154  rxn_path_map m_rxn;
155  doublereal m_total;
156 };
157 
158 
159 /**
160  * Reaction path diagrams (graphs).
161  */
163 {
164 public:
166 
167  /**
168  * Destructor. Deletes all nodes and paths in the diagram.
169  */
170  virtual ~ReactionPathDiagram();
171 
172  /// The largest one-way flow value in any path
173  doublereal maxFlow() {
174  return m_flxmax;
175  }
176 
177  /// The net flow from node \c k1 to node \c k2
178  doublereal netFlow(size_t k1, size_t k2) {
179  return flow(k1, k2) - flow(k2, k1);
180  }
181 
182  /// The one-way flow from node \c k1 to node \c k2
183  doublereal flow(size_t k1, size_t k2) {
184  return (m_paths[k1][k2] ? m_paths[k1][k2]->flow() : 0.0);
185  }
186 
187  /// True if a node for species k exists
188  bool hasNode(size_t k) {
189  return (m_nodes[k] != 0);
190  }
191 
192  void writeData(std::ostream& s);
193 
194  /**
195  * Export the reaction path diagram. This method writes to stream
196  * \c s the commands for the 'dot' program in the \c GraphViz
197  * package from AT&T. (GraphViz may be downloaded from www.graphviz.org.)
198  *
199  * To generate a postscript reaction path diagram from the output of this
200  * method saved in file paths.dot, for example, give the command:
201  * \code
202  * dot -Tps paths.dot > paths.ps
203  * \endcode
204  * To generate a GIF image, replace -Tps with -Tgif
205  */
206  void exportToDot(std::ostream& s);
207 
208  void add(ReactionPathDiagram& d);
209  SpeciesNode* node(size_t k) {
210  return m_nodes[k];
211  }
212  Path* path(size_t k1, size_t k2) {
213  return m_paths[k1][k2];
214  }
215  Path* path(size_t n) {
216  return m_pathlist[n];
217  }
218  size_t nPaths() {
219  return m_pathlist.size();
220  }
221  size_t nNodes() {
222  return m_nodes.size();
223  }
224 
225  void addNode(size_t k, const std::string& nm, doublereal x = 0.0);
226 
227  void displayOnly(size_t k=npos) {
228  m_local = k;
229  }
230 
231  void linkNodes(size_t k1, size_t k2, size_t rxn, doublereal value,
232  std::string legend = "");
233 
234  void include(const std::string& aaname) {
235  m_include.push_back(aaname);
236  }
237  void exclude(const std::string& aaname) {
238  m_exclude.push_back(aaname);
239  }
240  void include(std::vector<std::string>& names) {
241  for (size_t i = 0; i < names.size(); i++) {
242  m_include.push_back(names[i]);
243  }
244  }
245  void exclude(std::vector<std::string>& names) {
246  for (size_t i = 0; i < names.size(); i++) {
247  m_exclude.push_back(names[i]);
248  }
249  }
250  std::vector<std::string>& included() {
251  return m_include;
252  }
253  std::vector<std::string>& excluded() {
254  return m_exclude;
255  }
256  std::vector<size_t> species();
257  vector_int reactions();
258  void findMajorPaths(doublereal threshold, size_t lda, doublereal* a);
259  void setFont(const std::string& font) {
260  m_font = font;
261  }
262  // public attributes
263 
264  std::string title;
265  std::string bold_color;
266  std::string normal_color;
267  std::string dashed_color;
268  std::string element;
269  std::string m_font;
270  doublereal threshold, bold_min, dashed_max, label_min;
271  doublereal x_size, y_size;
272  std::string name, dot_options;
273  flow_t flow_type;
274  doublereal scale;
275  doublereal arrow_width;
276  bool show_details;
277  doublereal arrow_hue;
278 
279 protected:
280  doublereal m_flxmax;
281  std::map<size_t, std::map<size_t, Path*> > m_paths;
282  std::map<size_t, SpeciesNode*> m_nodes;
283  std::vector<Path*> m_pathlist;
284  std::vector<std::string> m_include;
285  std::vector<std::string> m_exclude;
286  std::vector<size_t> m_speciesNumber;
287  std::map<size_t, int> m_rxns;
288  size_t m_local;
289 };
290 
291 
292 class ReactionPathBuilder
293 {
294 public:
295  ReactionPathBuilder() {}
296  virtual ~ReactionPathBuilder() {}
297 
298  int init(std::ostream& logfile, Kinetics& s);
299 
300  int build(Kinetics& s, const std::string& element, std::ostream& output,
301  ReactionPathDiagram& r, bool quiet=false);
302 
303  //! Analyze a reaction to determine which reactants lead to which products.
304  int findGroups(std::ostream& logfile, Kinetics& s);
305 
306 protected:
307  void findElements(Kinetics& kin);
308 
309  size_t m_nr;
310  size_t m_ns;
311  size_t m_nel;
312  vector_fp m_ropf;
313  vector_fp m_ropr;
314  vector_fp m_x;
315  std::vector<std::vector<size_t> > m_reac;
316  std::vector<std::vector<size_t> > m_prod;
317  DenseMatrix m_elatoms;
318  std::vector<vector_int> m_groups;
319  std::vector<Group> m_sgroup;
320  std::vector<std::string> m_elementSymbols;
321 
322  //! m_transfer[reaction][reactant number][product number] where "reactant
323  //! number" means the number of the reactant in the reaction equation, e.g.
324  //! for "A+B -> C+D", "B" is reactant number 1 and "C" is product number 0.
325  std::map<size_t, std::map<size_t, std::map<size_t, Group> > > m_transfer;
326 
327  std::vector<bool> m_determinate;
328  Array2D m_atoms;
329  std::map<std::string, size_t> m_enamemap;
330 };
331 
332 }
333 
334 #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
int nPaths() const
Total number of paths to or from this node.
Definition: ReactionPath.h:57
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:183
virtual ~ReactionPathDiagram()
Destructor.
bool hasNode(size_t k)
True if a node for species k exists.
Definition: ReactionPath.h:188
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:162
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:173
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...
doublereal netFlow(size_t k1, size_t k2)
The net flow from node k1 to node k2.
Definition: ReactionPath.h:178
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:8