1 /**
2  * @file ctrpath.cpp
3  */
4 
5 // This file is part of Cantera. See License.txt in the top-level directory or
6 // at https://cantera.org/license.txt for license and copyright information.
7 
8 #define CANTERA_USE_INTERNAL
9 #include "cantera/clib/ctrpath.h"
10 
11 // Cantera includes
12 #include "cantera/kinetics/ReactionPath.h"
13 #include "Cabinet.h"
14 
15 #include <fstream>
16 
17 using namespace Cantera;
18 using namespace std;
19 
20 typedef Cabinet<ReactionPathBuilder> BuilderCabinet;
21 typedef Cabinet<ReactionPathDiagram> DiagramCabinet;
22 template<> DiagramCabinet* DiagramCabinet::s_storage = 0;
23 template<> BuilderCabinet* BuilderCabinet::s_storage = 0;
24 
25 typedef Cabinet<Kinetics> KineticsCabinet;
26 
27 extern "C" {
28 
rdiag_new()29     int rdiag_new()
30     {
31         try {
32             ReactionPathDiagram* d = new ReactionPathDiagram();
33             return DiagramCabinet::add(d);
34         } catch (...) {
35             return handleAllExceptions(-1, ERR);
36         }
37     }
38 
rdiag_del(int i)39     int rdiag_del(int i)
40     {
41         try {
42             DiagramCabinet::del(i);
43             return 0;
44         } catch (...) {
45             return handleAllExceptions(-1, ERR);
46         }
47     }
48 
rdiag_copy(int i)49     int rdiag_copy(int i)
50     {
51         try {
52             return DiagramCabinet::newCopy(i);
53         } catch (...) {
54             return handleAllExceptions(-1, ERR);
55         }
56     }
57 
rdiag_detailed(int i)58     int rdiag_detailed(int i)
59     {
60         try {
61             DiagramCabinet::item(i).show_details = true;
62             return 0;
63         } catch (...) {
64             return handleAllExceptions(-1, ERR);
65         }
66     }
67 
rdiag_brief(int i)68     int rdiag_brief(int i)
69     {
70         try {
71             DiagramCabinet::item(i).show_details = false;
72             return 0;
73         } catch (...) {
74             return handleAllExceptions(-1, ERR);
75         }
76     }
77 
rdiag_setThreshold(int i,double v)78     int rdiag_setThreshold(int i, double v)
79     {
80         try {
81             DiagramCabinet::item(i).threshold = v;
82             return 0;
83         } catch (...) {
84             return handleAllExceptions(-1, ERR);
85         }
86     }
87 
rdiag_setBoldColor(int i,const char * color)88     int rdiag_setBoldColor(int i, const char* color)
89     {
90         try {
91             DiagramCabinet::item(i).bold_color = color;
92             return 0;
93         } catch (...) {
94             return handleAllExceptions(-1, ERR);
95         }
96     }
97 
rdiag_setNormalColor(int i,const char * color)98     int rdiag_setNormalColor(int i, const char* color)
99     {
100         try {
101             DiagramCabinet::item(i).normal_color = color;
102             return 0;
103         } catch (...) {
104             return handleAllExceptions(-1, ERR);
105         }
106     }
107 
rdiag_setDashedColor(int i,const char * color)108     int rdiag_setDashedColor(int i, const char* color)
109     {
110         try {
111             DiagramCabinet::item(i).dashed_color = color;
112             return 0;
113         } catch (...) {
114             return handleAllExceptions(-1, ERR);
115         }
116     }
117 
rdiag_setDotOptions(int i,const char * opt)118     int rdiag_setDotOptions(int i, const char* opt)
119     {
120         try {
121             DiagramCabinet::item(i).dot_options = opt;
122             return 0;
123         } catch (...) {
124             return handleAllExceptions(-1, ERR);
125         }
126     }
127 
rdiag_setFont(int i,const char * font)128     int rdiag_setFont(int i, const char* font)
129     {
130         try {
131             DiagramCabinet::item(i).setFont(font);
132             return 0;
133         } catch (...) {
134             return handleAllExceptions(-1, ERR);
135         }
136     }
137 
rdiag_setBoldThreshold(int i,double v)138     int rdiag_setBoldThreshold(int i, double v)
139     {
140         try {
141             DiagramCabinet::item(i).bold_min = v;
142             return 0;
143         } catch (...) {
144             return handleAllExceptions(-1, ERR);
145         }
146     }
147 
rdiag_setNormalThreshold(int i,double v)148     int rdiag_setNormalThreshold(int i, double v)
149     {
150         try {
151             DiagramCabinet::item(i).dashed_max = v;
152             return 0;
153         } catch (...) {
154             return handleAllExceptions(-1, ERR);
155         }
156     }
157 
rdiag_setLabelThreshold(int i,double v)158     int rdiag_setLabelThreshold(int i, double v)
159     {
160         try {
161             DiagramCabinet::item(i).label_min = v;
162             return 0;
163         } catch (...) {
164             return handleAllExceptions(-1, ERR);
165         }
166     }
167 
rdiag_setScale(int i,double v)168     int rdiag_setScale(int i, double v)
169     {
170         try {
171             DiagramCabinet::item(i).scale = v;
172             return 0;
173         } catch (...) {
174             return handleAllExceptions(-1, ERR);
175         }
176     }
177 
rdiag_setFlowType(int i,int iflow)178     int rdiag_setFlowType(int i, int iflow)
179     {
180         try {
181             if (iflow == 0) {
182                 DiagramCabinet::item(i).flow_type = OneWayFlow;
183             } else {
184                 DiagramCabinet::item(i).flow_type = NetFlow;
185             }
186             return 0;
187         } catch (...) {
188             return handleAllExceptions(-1, ERR);
189         }
190     }
191 
rdiag_setArrowWidth(int i,double v)192     int rdiag_setArrowWidth(int i, double v)
193     {
194         try {
195             DiagramCabinet::item(i).arrow_width = v;
196             return 0;
197         } catch (...) {
198             return handleAllExceptions(-1, ERR);
199         }
200     }
201 
rdiag_setTitle(int i,const char * title)202     int rdiag_setTitle(int i, const char* title)
203     {
204         try {
205             DiagramCabinet::item(i).title = title;
206             return 0;
207         } catch (...) {
208             return handleAllExceptions(-1, ERR);
209         }
210     }
211 
rdiag_add(int i,int n)212     int rdiag_add(int i, int n)
213     {
214         try {
215             DiagramCabinet::item(i).add(DiagramCabinet::item(n));
216             return 0;
217         } catch (...) {
218             return handleAllExceptions(-1, ERR);
219         }
220     }
221 
rdiag_findMajor(int i,double threshold,size_t lda,double * a)222     int rdiag_findMajor(int i, double threshold,
223                         size_t lda, double* a)
224     {
225         try {
226             DiagramCabinet::item(i).findMajorPaths(threshold, lda, a);
227             return 0;
228         } catch (...) {
229             return handleAllExceptions(-1, ERR);
230         }
231     }
232 
rdiag_write(int i,int fmt,const char * fname)233     int rdiag_write(int i, int fmt, const char* fname)
234     {
235         try {
236             ofstream f(fname);
237             if (fmt == 0) {
238                 DiagramCabinet::item(i).exportToDot(f);
239             } else {
240                 DiagramCabinet::item(i).writeData(f);
241             }
242             return 0;
243         } catch (...) {
244             return handleAllExceptions(-1, ERR);
245         }
246     }
247 
rdiag_displayOnly(int i,int k)248     int rdiag_displayOnly(int i, int k)
249     {
250         try {
251             DiagramCabinet::item(i).displayOnly(k);
252             return 0;
253         } catch (...) {
254             return handleAllExceptions(-1, ERR);
255         }
256     }
257 
rbuild_new()258     int rbuild_new()
259     {
260         try {
261             ReactionPathBuilder* d = new ReactionPathBuilder();
262             return BuilderCabinet::add(d);
263         } catch (...) {
264             return handleAllExceptions(-1, ERR);
265         }
266     }
267 
rbuild_del(int i)268     int rbuild_del(int i)
269     {
270         try {
271             BuilderCabinet::del(i);
272             return 0;
273         } catch (...) {
274             return handleAllExceptions(-1, ERR);
275         }
276     }
277 
rbuild_init(int i,const char * logfile,int k)278     int rbuild_init(int i, const char* logfile, int k)
279     {
280         try {
281             ofstream flog(logfile);
282             BuilderCabinet::item(i).init(flog, KineticsCabinet::item(k));
283             return 0;
284         } catch (...) {
285             return handleAllExceptions(-1, ERR);
286         }
287     }
288 
rbuild_build(int i,int k,const char * el,const char * dotfile,int idiag,int iquiet)289     int rbuild_build(int i, int k, const char* el, const char* dotfile,
290                      int idiag, int iquiet)
291     {
292         try {
293             ofstream fdot(dotfile);
294             bool quiet = false;
295             if (iquiet > 0) {
296                 quiet = true;
297             }
298             BuilderCabinet::item(i).build(KineticsCabinet::item(k), el, fdot,
299                                           DiagramCabinet::item(idiag), quiet);
300             return 0;
301         } catch (...) {
302             return handleAllExceptions(-1, ERR);
303         }
304     }
305 
ct_clearReactionPath()306     int ct_clearReactionPath()
307     {
308         try {
309             DiagramCabinet::clear();
310             BuilderCabinet::clear();
311             return 0;
312         } catch (...) {
313             return handleAllExceptions(-1, ERR);
314         }
315     }
316 }
317