1 |
/* ex: set syntax=cpp : */ |
2 |
/* |
3 |
SWIG interface routines to read a file into the library |
4 |
*/ |
5 |
|
6 |
%module ascend |
7 |
|
8 |
%include <python/std_string.i> |
9 |
%include <python/std_vector.i> |
10 |
%include <python/std_except.i> |
11 |
|
12 |
%{ |
13 |
#include "library.h" |
14 |
#include "type.h" |
15 |
#include "instance.h" |
16 |
#include "variable.h" |
17 |
#include "name.h" |
18 |
#include "reporter.h" |
19 |
#include "simulation.h" |
20 |
#include "solver.h" |
21 |
#include "symchar.h" |
22 |
#include "set.h" |
23 |
#include "dimensions.h" |
24 |
#include "units.h" |
25 |
#include "extmethod.h" |
26 |
#include "plot.h" |
27 |
#include "curve.h" |
28 |
#include "solverparameters.h" |
29 |
%} |
30 |
|
31 |
// All STL runtime_errors caught to Python |
32 |
|
33 |
%exception { |
34 |
try { |
35 |
$action |
36 |
} |
37 |
catch (std::runtime_error &e) { |
38 |
SWIG_exception(SWIG_RuntimeError,e.what()); |
39 |
} |
40 |
} |
41 |
|
42 |
// Import the preferences module |
43 |
%pythoncode { |
44 |
import preferences; |
45 |
} |
46 |
|
47 |
// Set-valued instance variable |
48 |
%pythoncode { |
49 |
class SetIter: |
50 |
def __init__(self,set): |
51 |
self.set=set |
52 |
self.index=0 |
53 |
def next(self): |
54 |
if self.index==self.set.length(): |
55 |
raise StopIteration |
56 |
self.index = self.index + 1 |
57 |
return self.set[self.index] |
58 |
} |
59 |
|
60 |
template<class T> |
61 |
class ASCXX_Set{ |
62 |
private: |
63 |
ASCXX_Set(); |
64 |
public: |
65 |
const T at(const unsigned long&) const; |
66 |
const unsigned long length() const; |
67 |
}; |
68 |
%extend ASCXX_Set<long>{ |
69 |
%pythoncode { |
70 |
def __getitem__(self, index): |
71 |
return self.at(index) |
72 |
def __iter__(self): |
73 |
return SetIter(self) |
74 |
} |
75 |
} |
76 |
%extend ASCXX_Set<SymChar>{ |
77 |
%pythoncode { |
78 |
def __getitem__(self, index): |
79 |
return self.at(index) |
80 |
def __iter__(self): |
81 |
return SetIter(self) |
82 |
} |
83 |
} |
84 |
|
85 |
|
86 |
%template(ModuleVector) std::vector<Module>; |
87 |
%template(TypeVector) std::vector<Type>; |
88 |
%template(VariableVector) std::vector<Variable>; |
89 |
%template(MethodVector) std::vector<Method>; |
90 |
%template(InstancVector) std::vector<Instanc>; |
91 |
%template(ExtMethodVector) std::vector<ExtMethod>; |
92 |
%template(SetInt) ASCXX_Set<long>; |
93 |
%template(SetString) ASCXX_Set<SymChar>; |
94 |
%template(DoubleVector) std::vector<double>; |
95 |
%template(CurveVector) std::vector<Curve>; |
96 |
%template(StringVector) std::vector<std::string>; |
97 |
|
98 |
%rename(Instance) Instanc; |
99 |
%rename(Name) Nam; |
100 |
%rename(getSetIntValue) Instanc::getSetValue<long>; |
101 |
%rename(getSetStringValue) Instanc::getSetValue<SymChar>; |
102 |
%rename(Units) UnitsM; |
103 |
|
104 |
|
105 |
// Grab a Python function object as a Python object. |
106 |
%typemap(python,in) PyObject *pyfunc { |
107 |
if (!PyCallable_Check($source)) { |
108 |
PyErr_SetString(PyExc_TypeError, "Need a callable object!"); |
109 |
return NULL; |
110 |
} |
111 |
$target = $input; |
112 |
} |
113 |
|
114 |
//---------------------------- |
115 |
// REPORTER: callbacks to python |
116 |
class Reporter{ |
117 |
private: |
118 |
~Reporter(); |
119 |
Reporter(); |
120 |
public: |
121 |
// use 'getReporter' instead of 'Reporter::Instance()' in python |
122 |
void setErrorCallback(error_reporter_callback_t callback, void *client_data); |
123 |
void setPythonErrorCallback(PyObject *pyfunc); |
124 |
void clearPythonErrorCallback(); |
125 |
}; |
126 |
|
127 |
%extend Reporter { |
128 |
void reportError(const char *msg){ |
129 |
error_reporter(ASC_USER_ERROR,NULL,0,"%s", msg); |
130 |
} |
131 |
void reportNote(const char *msg){ |
132 |
error_reporter(ASC_USER_NOTE,NULL,0,"%s",msg); |
133 |
} |
134 |
void reportWarning(const char *msg){ |
135 |
error_reporter(ASC_USER_WARNING,NULL,0,"%s",msg); |
136 |
} |
137 |
void reportSuccess(const char *msg){ |
138 |
error_reporter(ASC_USER_SUCCESS,NULL,0,"%s",msg); |
139 |
} |
140 |
} |
141 |
|
142 |
// There are problems with Instance(), so use this instead: |
143 |
Reporter *getReporter(); |
144 |
|
145 |
//---------------------------------------- |
146 |
// UNITS AND DIMENSIONS |
147 |
|
148 |
|
149 |
class UnitsM; |
150 |
|
151 |
class Dimensions{ |
152 |
public: |
153 |
static const unsigned MAX_DIMS; |
154 |
static const std::string getBaseUnit(const unsigned &); |
155 |
|
156 |
const bool operator==(const Dimensions &) const; |
157 |
const bool operator!=(const Dimensions &) const; |
158 |
const bool operator<(const Dimensions &) const; |
159 |
Dimensions(const Dimensions &); |
160 |
const bool isWild() const; |
161 |
const bool isDimensionless() const; |
162 |
const short getFractionNumerator(const unsigned &) const; |
163 |
const short getFractionDenominator(const unsigned &) const; |
164 |
}; |
165 |
|
166 |
class UnitsM{ |
167 |
public: |
168 |
UnitsM(const char *); |
169 |
const SymChar getName() const; // the units description string eg 'bar' or 'kJ/kg/K' |
170 |
const Dimensions getDimensions() const; |
171 |
const double getConversion() const; // multiplication factor to convert eg feet to SI (metres) |
172 |
}; |
173 |
|
174 |
%extend UnitsM{ |
175 |
%pythoncode{ |
176 |
def getConvertedValue(self,si_value): |
177 |
_u_value = si_value / self.getConversion() |
178 |
return str(_u_value) + " " + self.getName().toString(); |
179 |
} |
180 |
} |
181 |
|
182 |
/* |
183 |
This function creates default (SI) units for any dimension given. Most |
184 |
of the time you will want to use custom units in place of these, eg |
185 |
'N' instead of 'kg*m/s^2'. |
186 |
*/ |
187 |
%extend Dimensions{ |
188 |
%pythoncode { |
189 |
|
190 |
def getDefaultUnits(self): |
191 |
if self.isDimensionless(): |
192 |
return Units(""); |
193 |
|
194 |
if self.isWild(): |
195 |
return Units("?"); |
196 |
|
197 |
# create a string representation of the current dimensions |
198 |
numparts=[] |
199 |
denparts=[] |
200 |
for i in range(0, self.MAX_DIMS): |
201 |
baseunit = self.getBaseUnit(i); |
202 |
num = self.getFractionNumerator(i) |
203 |
den = self.getFractionDenominator(i) |
204 |
if num > 0: |
205 |
if den == 1: |
206 |
if num == 1: |
207 |
numparts.append(baseunit) |
208 |
else: |
209 |
numparts.append("%s^%d" % (baseunit, num) ) |
210 |
else: |
211 |
numparts.append("%s^(%d/%d)" % (baseunit, num, den) ) |
212 |
elif num < 0: |
213 |
if den == 1: |
214 |
if num == -1: |
215 |
denparts.append(baseunit) |
216 |
else: |
217 |
denparts.append("%s^%d" % (baseunit, -num) ) |
218 |
else: |
219 |
denparts.append("%s^(%d/%d)" % (baseunit, -num, den) ) |
220 |
|
221 |
if len(numparts): |
222 |
str = "*".join(numparts) |
223 |
else: |
224 |
str = "1" |
225 |
|
226 |
if len(denparts): |
227 |
str = str + "/" + "/".join(denparts) |
228 |
|
229 |
return Units(str) |
230 |
|
231 |
} |
232 |
} |
233 |
|
234 |
/* |
235 |
some python code for nice unicode unit strings, need to extend the units.c code as well though. |
236 |
|
237 |
elif num == 2: |
238 |
numparts.append(baseunit + ur'\u00b2') |
239 |
elif num == 3: |
240 |
numparts.append(baseunit + ur'\u00b3') |
241 |
|
242 |
str = ur'\u00b7'.join(numparts) |
243 |
*/ |
244 |
//---------------------------- |
245 |
|
246 |
class Library{ |
247 |
public: |
248 |
Library(); |
249 |
~Library(); |
250 |
|
251 |
void load(char *filename); |
252 |
void listModules(const int &module_type=0); |
253 |
Type &findType(const char *name); |
254 |
std::vector<Module> getModules(); |
255 |
std::vector<Type> getModuleTypes(const Module &); |
256 |
std::vector<ExtMethod> getExtMethods(); |
257 |
void clear(); |
258 |
}; |
259 |
|
260 |
class SymChar{ |
261 |
public: |
262 |
SymChar(const std::string &); |
263 |
const char *toString() const; |
264 |
}; |
265 |
%extend SymChar{ |
266 |
const char *__repr__(){ |
267 |
return self->toString(); |
268 |
} |
269 |
} |
270 |
|
271 |
class Module{ |
272 |
public: |
273 |
const char *getName() const; |
274 |
const char *getFilename() const; |
275 |
const struct tm *getMtime() const; |
276 |
}; |
277 |
|
278 |
class Method{ |
279 |
public: |
280 |
const char *getName() const; |
281 |
}; |
282 |
|
283 |
// Renamed in python as 'Name' |
284 |
class Nam{ |
285 |
public: |
286 |
Nam(const SymChar &); |
287 |
const std::string getName() const; |
288 |
}; |
289 |
|
290 |
class Type{ |
291 |
public: |
292 |
const SymChar getName(); |
293 |
const int getParameterCount(); |
294 |
Simulation getSimulation(const char *name); |
295 |
std::vector<Method> getMethods(); |
296 |
const bool isRefinedSolverVar() const; |
297 |
const bool isRefinedReal() const; |
298 |
const Dimensions getDimensions() const; |
299 |
}; |
300 |
%extend Type{ |
301 |
const char *__repr__(){ |
302 |
return self->getName().toString(); |
303 |
} |
304 |
|
305 |
%pythoncode{ |
306 |
def getPreferredUnits(self): |
307 |
if not self.isRefinedReal(): |
308 |
return None |
309 |
|
310 |
_pref = preferences.Preferences() |
311 |
#print "Checking for preferred units for %s" % self.getName() |
312 |
_u = _pref.getPreferredUnits(self.getName().toString()) |
313 |
if _u == None: |
314 |
# no preferred units set |
315 |
return None |
316 |
_units = Units(_u); |
317 |
|
318 |
if _units.getDimensions() != self.getDimensions(): |
319 |
getReporter().reportWarning("Preferred units '%s' for type '%s' are not dimensionally correct: ignoring." % (_u, self.getName()) ); |
320 |
return None |
321 |
|
322 |
return _units; |
323 |
} |
324 |
} |
325 |
|
326 |
class Instanc{ |
327 |
private: |
328 |
Instanc(); |
329 |
public: |
330 |
Instanc(Instance *); |
331 |
Instanc(Instance *, SymChar &name); |
332 |
std::vector<Instanc> getChildren(); |
333 |
const std::string getKindStr() const; |
334 |
const SymChar &getName(); |
335 |
const Type getType() const; |
336 |
const bool isAtom() const; |
337 |
const bool isFixed() const; |
338 |
const bool isFund() const; |
339 |
const bool isConst() const; |
340 |
const bool isAssigned() const; |
341 |
const bool isCompound() const; |
342 |
const bool isRelation() const; |
343 |
const bool isWhen() const; |
344 |
const bool isSet() const; // a set (group) of things |
345 |
const bool isSetInt() const; |
346 |
const bool isSetString() const; |
347 |
const bool isSetEmpty() const; |
348 |
const bool isDefined() const; |
349 |
const bool isBool() const; |
350 |
const bool isInt() const; |
351 |
const bool isSymbol() const; |
352 |
const bool isReal() const; |
353 |
const double getRealValue() const; |
354 |
const bool isDimensionless() const; |
355 |
const Dimensions getDimensions() const; |
356 |
const bool getBoolValue() const; |
357 |
const long getIntValue() const; |
358 |
const SymChar getSymbolValue() const; |
359 |
const std::string getValueAsString() const; ///< Use carefully: rounding will occur for doubles! |
360 |
const std::string getRelationAsString(const Instanc &relative_to) const; |
361 |
Plot getPlot() const; |
362 |
|
363 |
const bool isPlottable() const; |
364 |
const ASCXX_Set<long> getSetValue<long>() const; |
365 |
const ASCXX_Set<SymChar> getSetValue<SymChar>() const; |
366 |
const bool isChildless() const; |
367 |
void setFixed(const bool &val=true); |
368 |
void setRealValue(const double &val); |
369 |
void setRealValueWithUnits(const double &, const char *); |
370 |
void setBoolValue(const bool &val); |
371 |
void write(); |
372 |
}; |
373 |
|
374 |
%extend Instanc{ |
375 |
const char *__repr__(){ |
376 |
return self->getName().toString(); |
377 |
} |
378 |
%pythoncode { |
379 |
def getSetValue(self): |
380 |
if self.isSetInt(): |
381 |
return self.getSetIntValue() |
382 |
elif self.isSetString(): |
383 |
return self.getSetStringValue() |
384 |
elif self.isSetEmpty(): |
385 |
return set() |
386 |
else: |
387 |
raise RuntimeError("getSetValue: unknown set type"); |
388 |
|
389 |
def getValue(self): |
390 |
# print "GETTING VALUE OF %s" % self.getName() |
391 |
if self.isCompound(): |
392 |
return "" |
393 |
elif self.isRelation() or self.isWhen(): |
394 |
return "RELATION" |
395 |
elif self.isSet(): |
396 |
_s = set(self.getSetValue()); |
397 |
#for _v in self.getSetValue(): |
398 |
# _s.add( _v ) |
399 |
return _s |
400 |
|
401 |
elif ( self.isAtom() or self.isFund() ) and not self.isDefined(): |
402 |
return "undefined" |
403 |
elif self.isReal(): |
404 |
return self.getRealValueAndUnits() |
405 |
elif self.isBool(): |
406 |
return self.getBoolValue() |
407 |
elif self.isInt(): |
408 |
return self.getIntValue() |
409 |
elif self.isSymbol(): |
410 |
return self.getSymbolValue() |
411 |
else: |
412 |
return "UNKNOWN TYPE" #raise RuntimeError("Unknown value model type="+self.getType().getName().toString()+", instance kind=".getKindStr()) |
413 |
|
414 |
def getRealValueAndUnits(self): |
415 |
if not self.isReal(): |
416 |
raise TypeError |
417 |
if self.isDimensionless(): |
418 |
return self.getRealValue(); |
419 |
_u = self.getType().getPreferredUnits(); |
420 |
if _u == None: |
421 |
return str(self.getRealValue()) + ' ' + self.getDimensions().getDefaultUnits().getName().toString() |
422 |
return _u.getConvertedValue(self.getRealValue()) |
423 |
|
424 |
def setFixedValue(self,val): |
425 |
if not self.isFixed(): |
426 |
self.setFixed(); |
427 |
# getReporter().reportError("Setting value of %s to %s" % (self.getName().toString(),val)) |
428 |
self.setRealValue(val); |
429 |
} |
430 |
} |
431 |
|
432 |
%include "solver.i" |
433 |
|
434 |
class Simulation : public Instanc{ |
435 |
public: |
436 |
Simulation(Instance *&, const SymChar &name); |
437 |
Instanc &getModel(); |
438 |
std::vector<Variable> getFixableVariables(); |
439 |
void build(); |
440 |
const bool check(); |
441 |
void checkDoF() const; |
442 |
void run(const Method &); |
443 |
void setSolver(Solver&); |
444 |
void solve(Solver s); |
445 |
SolverParameters getSolverParameters() const; |
446 |
}; |
447 |
|
448 |
class Variable{ |
449 |
public: |
450 |
const std::string &getName(); |
451 |
~Variable(); |
452 |
|
453 |
}; |
454 |
|
455 |
%extend Variable { |
456 |
const std::string __repr__(){ |
457 |
return self->getName(); |
458 |
} |
459 |
} |
460 |
|
461 |
class ExtMethod{ |
462 |
public: |
463 |
ExtMethod(const ExtMethod &); |
464 |
const char *getName() const; |
465 |
const char *getHelp() const; |
466 |
const unsigned long getNumInputs() const; |
467 |
const unsigned long getNumOutputs() const; |
468 |
}; |
469 |
|
470 |
%include "plot.i" |
471 |
|
472 |
class Curve : public Instanc{ |
473 |
|
474 |
public: |
475 |
std::vector<double> x; |
476 |
std::vector<double> y; |
477 |
const std::string getLegend() const; |
478 |
|
479 |
}; |