Loading [MathJax]/extensions/tex2jax.js
Cantera  3.2.0a1
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
16namespace Cantera
17{
18enum flow_t { NetFlow, OneWayFlow };
19
20// forward references
21class Path;
22class ReactionPathBuilder;
23
24/**
25 * Nodes in reaction path graphs.
26 */
28{
29public:
30 //! Default constructor
31 SpeciesNode() = default;
32
33 //! Destructor
34 virtual ~SpeciesNode() = default;
35
36 // public attributes
37 size_t number = npos; //!< Species number
38 string name; //!< Label on graph
39 double value = 0.0; //!< May be used to set node appearance
40 bool visible = false; //!< Visible on graph;
41
42 //! @name References
43 //!
44 //! Return a reference to a path object connecting this node
45 //! to another node.
46 //! @{
47 Path* path(int n) {
48 return m_paths[n];
49 }
50 const Path* path(int n) const {
51 return m_paths[n];
52 }
53 //! @}
54
55 //! Total number of paths to or from this node
56 int nPaths() const {
57 return static_cast<int>(m_paths.size());
58 }
59
60 //! add a path to or from this node
61 void addPath(Path* path);
62
63 double outflow() {
64 return m_out;
65 }
66 double inflow() {
67 return m_in;
68 }
69 double netOutflow() {
70 return m_out - m_in;
71 }
72
73 void printPaths();
74
75protected:
76 double m_in = 0.0;
77 double m_out = 0.0;
78 vector<Path*> m_paths;
79};
80
81
82class Path
83{
84public:
85 typedef map<size_t, double> rxn_path_map;
86
87 /**
88 * Constructor. Construct a one-way path from @c begin to @c end.
89 */
91
92 //! Destructor
93 virtual ~Path() {}
94
95 /**
96 * Add a reaction to the path. Increment the flow from this reaction, the
97 * total flow, and the flow associated with this label.
98 */
99 void addReaction(size_t rxnNumber, double value, const string& label = "");
100
101 //! Upstream node.
102 const SpeciesNode* begin() const {
103 return m_a;
104 }
105 SpeciesNode* begin() {
106 return m_a;
107 }
108
109 //! Downstream node.
110 const SpeciesNode* end() const {
111 return m_b;
112 }
113 SpeciesNode* end() {
114 return m_b;
115 }
116
117 /**
118 * If @c n is one of the nodes this path connects, then
119 * the other node is returned. Otherwise zero is returned.
120 */
122 return (n == m_a ? m_b : (n == m_b ? m_a : 0));
123 }
124
125 //! The total flow in this path
126 double flow() {
127 return m_total;
128 }
129 void setFlow(double v) {
130 m_total = v;
131 }
132
133 //! Number of reactions contributing to this path
135 return static_cast<int>(m_rxn.size());
136 }
137
138 //! Map from reaction number to flow from that reaction in this path.
139 const rxn_path_map& reactionMap() {
140 return m_rxn;
141 }
142
143 /**
144 * Write the label for a path connecting two species, indicating
145 * the percent of the total flow due to each reaction.
146 */
147 void writeLabel(std::ostream& s, double threshold = 0.005);
148
149protected:
150 map<string, double> m_label;
151 SpeciesNode* m_a, *m_b;
152 rxn_path_map m_rxn;
153 double m_total = 0.0;
154};
155
156
157/**
158 * Reaction path diagrams (graphs).
159 */
161{
162public:
163 ReactionPathDiagram() = default;
164
165 //! Construct new reaction path diagram.
166 /**
167 * The method creates a reaction path diagram for the fluxes of `element`
168 * according to instantaneous net reaction rates.
169 * @param kin Shared pointer to Kinetics object.
170 * @param element_ Element used for the calculation of net reaction rates.
171 */
172 ReactionPathDiagram(shared_ptr<Kinetics> kin, const string& element_);
173
174 /**
175 * Destructor. Deletes all nodes and paths in the diagram.
176 */
177 virtual ~ReactionPathDiagram();
178
179 //! The largest one-way flow value in any path
180 double maxFlow() {
181 return m_flxmax;
182 }
183
184 //! The net flow from node @c k1 to node @c k2
185 double netFlow(size_t k1, size_t k2) {
186 return flow(k1, k2) - flow(k2, k1);
187 }
188
189 //! The one-way flow from node @c k1 to node @c k2
190 double flow(size_t k1, size_t k2) {
191 return (m_paths[k1][k2] ? m_paths[k1][k2]->flow() : 0.0);
192 }
193
194 //! True if a node for species k exists
195 bool hasNode(size_t k) {
196 return (m_nodes[k] != 0);
197 }
198
199 void writeData(std::ostream& s);
200
201 //! Get a (roughly) human-readable representation of the reaction path diagram.
202 /**
203 * @see writeData
204 */
205 string getData();
206
207 /**
208 * Export the reaction path diagram. This method writes to stream
209 * @c s the commands for the 'dot' program in the @c GraphViz
210 * package from AT&T. (GraphViz may be downloaded from www.graphviz.org.)
211 *
212 * To generate a postscript reaction path diagram from the output of this
213 * method saved in file paths.dot, for example, give the command:
214 * @code
215 * dot -Tps paths.dot > paths.ps
216 * @endcode
217 * To generate a GIF image, replace -Tps with -Tgif
218 */
219 void exportToDot(std::ostream& s);
220
221 //! Export string in @c dot format.
222 /**
223 * Return a string containing the reaction path diagram formatted for use
224 * by Graphviz's 'dot' program.
225 * @see exportToDot
226 */
227 string getDot();
228
229 void add(ReactionPathDiagram& d);
230
231 //! Add fluxes from other ReactionPathDiagram to this diagram.
232 void add(shared_ptr<ReactionPathDiagram> d);
233
234 SpeciesNode* node(size_t k) {
235 return m_nodes[k];
236 }
237 Path* path(size_t k1, size_t k2) {
238 return m_paths[k1][k2];
239 }
240 Path* path(size_t n) {
241 return m_pathlist[n];
242 }
243 size_t nPaths() {
244 return m_pathlist.size();
245 }
246 size_t nNodes() {
247 return m_nodes.size();
248 }
249
250 void addNode(size_t k, const string& nm, double x = 0.0);
251
252 //! Include only species and fluxes that are directly connected to a species.
253 //! Set to -1 to include all species.
254 void displayOnly(size_t k=npos) {
255 m_local = k;
256 }
257
258 void linkNodes(size_t k1, size_t k2, size_t rxn, double value, string legend = "");
259
260 void include(const string& aaname) {
261 m_include.push_back(aaname);
262 }
263 void exclude(const string& aaname) {
264 m_exclude.push_back(aaname);
265 }
266 void include(vector<string>& names) {
267 for (size_t i = 0; i < names.size(); i++) {
268 m_include.push_back(names[i]);
269 }
270 }
271 void exclude(vector<string>& names) {
272 for (size_t i = 0; i < names.size(); i++) {
273 m_exclude.push_back(names[i]);
274 }
275 }
276 vector<string>& included() {
277 return m_include;
278 }
279 vector<string>& excluded() {
280 return m_exclude;
281 }
282 vector<size_t> species();
283 vector<int> reactions();
284
285 //! Undocumented.
286 /**
287 * @todo Add documentation.
288 */
289 void findMajorPaths(double threshold, size_t lda, double* a);
290
291 //! Set name of the font used.
292 void setFont(const string& font) {
293 m_font = font;
294 }
295
296 //! Get the way flows are drawn. Either 'NetFlow' or 'OneWayFlow'
297 const string flowType() const;
298
299 //! Get the way flows are drawn. Either 'NetFlow' or 'OneWayFlow'
300 void setFlowType(const string& fType);
301
302 //! Build the reaction path diagram.
303 /**
304 * Called automatically by methods which return representations of the diagram,
305 * for example writeDot().
306 */
307 void build();
308
309 //! Get logging messages generated while building the reaction path diagram.
310 string getLog();
311
312 //! @name Public Attributes
313 //! @{
314
315 string title; //!< Reaction path diagram title.
316 string bold_color = "blue"; //!< Color for bold lines.
317 string normal_color = "steelblue"; //!< Color for normal-weight lines.
318 string dashed_color = "gray"; //!< Color for dashed lines.
319 string element; //!< Element used for the construction of a reaction path diagram.
320 string m_font = "Helvetica"; //!< Reaction path diagram font.
321 //! Threshold for the minimum flux relative value that will be plotted.
322 double threshold = 0.005;
323 double bold_min = 0.2; //!< Minimum relative flux for bold lines.
324 double dashed_max = 0.0; //!< Maximum relative flux for dashed lines.
325 double label_min = 0.0; //!< Minimum relative flux for labels.
326 double x_size = -1.0; //!< Maximum size (x-dimension).
327 double y_size = -1.0; //!< Maximum size (y-dimension).
328 string name = "reaction_paths"; //!< Name used for dot export.
329 string dot_options = "center=1;"; //!< Options for the 'dot' program.
330 //! The way flows are drawn. Either 'NetFlow' or 'OneWayFlow'
331 flow_t flow_type = NetFlow;
332 //! The scaling factor for the fluxes.
333 //! Set to -1 to normalize by the maximum net flux.
334 double scale = -1; //!< Scale to use for normalization.
335 //! The arrow width. If < 0, then scale with flux value.
336 double arrow_width = -5.0;
337 bool show_details = false; //!< Boolean flag to show details.
338 double arrow_hue = 0.6666; //!< Unused.
339
340 //! @}
341
342protected:
343 double m_flxmax = 0.0;
344 map<size_t, map<size_t, Path*>> m_paths;
345
346 //! map of species index to SpeciesNode
347 map<size_t, SpeciesNode*> m_nodes;
348 vector<Path*> m_pathlist;
349 vector<string> m_include;
350 vector<string> m_exclude;
351 vector<size_t> m_speciesNumber;
352
353 //! Indices of reactions that are included in the diagram
354 set<size_t> m_rxns;
355 size_t m_local = npos;
356
357 bool m_isBuilt = false; //!< Boolean indicating whether diagram is built.
358 shared_ptr<Kinetics> m_kin; //!< Kinetics used by ReactionPathBuilder
359 shared_ptr<ReactionPathBuilder> m_builder; //!< Shared pointer to ReactionPathBuilder
360 std::stringstream m_log; //!< Logging stream.
361};
362
363
365{
366public:
367 ReactionPathBuilder() = default;
368 virtual ~ReactionPathBuilder() = default;
369
370 int init(std::ostream& logfile, Kinetics& s);
371
372 int build(Kinetics& s, const string& element, std::ostream& output,
373 ReactionPathDiagram& r, bool quiet=false);
374
375 //! Analyze a reaction to determine which reactants lead to which products.
376 int findGroups(std::ostream& logfile, Kinetics& s);
377
378protected:
379 void findElements(Kinetics& kin);
380
381 size_t m_nr;
382 size_t m_ns;
383 size_t m_nel;
384 vector<double> m_ropf;
385 vector<double> m_ropr;
386 vector<double> m_x;
387 vector<vector<size_t>> m_reac;
388 vector<vector<size_t>> m_prod;
389 DenseMatrix m_elatoms;
390 vector<vector<int>> m_groups;
391 vector<Group> m_sgroup;
392 vector<string> m_elementSymbols;
393
394 //! m_transfer[reaction][reactant number][product number] where "reactant
395 //! number" means the number of the reactant in the reaction equation. For example,
396 //! for "A+B -> C+D", "B" is reactant number 1 and "C" is product number 0.
397 map<size_t, map<size_t, map<size_t, Group>>> m_transfer;
398
399 vector<bool> m_determinate;
400 Array2D m_atoms;
401 map<string, size_t> m_enamemap;
402};
403
404
405//! Create a new reaction path diagram.
406/**
407 * Returns a shared ReactionPath instance where the fluxes of `element`
408 * are calculated according to instantaneous net reaction rates.
409 * @param kin Shared pointer to Kinetics object.
410 * @param element Element used for the calculation of net reaction rates.
411 * @return shared_ptr<ReactionPathDiagram>
412 */
413shared_ptr<ReactionPathDiagram> newReactionPathDiagram(
414 shared_ptr<Kinetics> kin, const string& element);
415
416}
417
418#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
virtual ~Path()
Destructor.
const SpeciesNode * begin() const
Upstream node.
int nReactions()
Number of reactions contributing to this path.
void addReaction(size_t rxnNumber, double value, const string &label="")
Add a reaction to the path.
void writeLabel(std::ostream &s, double threshold=0.005)
Write the label for a path connecting two species, indicating the percent of the total flow due to ea...
double flow()
The total flow in this path.
SpeciesNode * otherNode(SpeciesNode *n)
If n is one of the nodes this path connects, then the other node is returned.
const rxn_path_map & reactionMap()
Map from reaction number to flow from that reaction in this path.
const SpeciesNode * end() const
Downstream node.
int findGroups(std::ostream &logfile, Kinetics &s)
Analyze a reaction to determine which reactants lead to which products.
map< size_t, map< size_t, map< size_t, Group > > > m_transfer
m_transfer[reaction][reactant number][product number] where "reactant number" means the number of the...
Reaction path diagrams (graphs).
map< size_t, SpeciesNode * > m_nodes
map of species index to SpeciesNode
shared_ptr< ReactionPathBuilder > m_builder
Shared pointer to ReactionPathBuilder.
string element
Element used for the construction of a reaction path diagram.
string getDot()
Export string in dot format.
double arrow_width
The arrow width. If < 0, then scale with flux value.
bool m_isBuilt
Boolean indicating whether diagram is built.
string title
Reaction path diagram title.
std::stringstream m_log
Logging stream.
void exportToDot(std::ostream &s)
Export the reaction path diagram.
string dashed_color
Color for dashed lines.
void findMajorPaths(double threshold, size_t lda, double *a)
Undocumented.
void displayOnly(size_t k=npos)
Include only species and fluxes that are directly connected to a species.
bool hasNode(size_t k)
True if a node for species k exists.
double y_size
Maximum size (y-dimension).
string dot_options
Options for the 'dot' program.
void build()
Build the reaction path diagram.
string normal_color
Color for normal-weight lines.
double scale
The scaling factor for the fluxes.
bool show_details
Boolean flag to show details.
double dashed_max
Maximum relative flux for dashed lines.
string m_font
Reaction path diagram font.
string name
Name used for dot export.
string getLog()
Get logging messages generated while building the reaction path diagram.
double x_size
Maximum size (x-dimension).
shared_ptr< Kinetics > m_kin
Kinetics used by ReactionPathBuilder.
double label_min
Minimum relative flux for labels.
set< size_t > m_rxns
Indices of reactions that are included in the diagram.
void setFlowType(const string &fType)
Get the way flows are drawn. Either 'NetFlow' or 'OneWayFlow'.
virtual ~ReactionPathDiagram()
Destructor.
double maxFlow()
The largest one-way flow value in any path.
double flow(size_t k1, size_t k2)
The one-way flow from node k1 to node k2.
double netFlow(size_t k1, size_t k2)
The net flow from node k1 to node k2.
double bold_min
Minimum relative flux for bold lines.
const string flowType() const
Get the way flows are drawn. Either 'NetFlow' or 'OneWayFlow'.
void setFont(const string &font)
Set name of the font used.
flow_t flow_type
The way flows are drawn. Either 'NetFlow' or 'OneWayFlow'.
string bold_color
Color for bold lines.
string getData()
Get a (roughly) human-readable representation of the reaction path diagram.
double threshold
Threshold for the minimum flux relative value that will be plotted.
Nodes in reaction path graphs.
virtual ~SpeciesNode()=default
Destructor.
string name
Label on graph.
bool visible
Visible on graph;.
int nPaths() const
Total number of paths to or from this node.
SpeciesNode()=default
Default constructor.
double value
May be used to set node appearance.
size_t number
Species number.
void addPath(Path *path)
add a path to or from this node
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
const size_t npos
index returned by functions to indicate "no position"
Definition ct_defs.h:180
shared_ptr< ReactionPathDiagram > newReactionPathDiagram(shared_ptr< Kinetics > kin, const string &element)
Create a new reaction path diagram.