1 |
/* |
2 |
SWIG interface for accessing Solver and choosing solver parameters |
3 |
*/ |
4 |
|
5 |
%include <python/std_vector.i> |
6 |
%include <python/std_except.i> |
7 |
|
8 |
%import "ascpy.i" |
9 |
|
10 |
%{ |
11 |
#include "config.h" |
12 |
#include "integrator.h" |
13 |
#include "integratorreporter.h" |
14 |
#include "solver.h" |
15 |
#include "incidencematrix.h" |
16 |
#include "solverparameter.h" |
17 |
#include "solverparameters.h" |
18 |
#include "solverreporter.h" |
19 |
#include "solverhooks.h" |
20 |
#include "curve.h" |
21 |
#include "matrix.h" |
22 |
%} |
23 |
|
24 |
%pythoncode{ |
25 |
import types |
26 |
} |
27 |
|
28 |
%typemap(in) FILE * { |
29 |
%#if defined(__MINGW32__) && !defined(HAVE_MSVCR71) |
30 |
PyErr_SetString(PyExc_TypeError,"File passing from python to ASCEND not yet implemented on Windows"); |
31 |
return NULL; |
32 |
%#else |
33 |
if (!PyFile_Check($input)) { |
34 |
PyErr_SetString(PyExc_TypeError, "Need a file!"); |
35 |
return NULL; |
36 |
} |
37 |
$1 = PyFile_AsFile($input); |
38 |
%#endif |
39 |
} |
40 |
|
41 |
%ignore registerSolver; |
42 |
%ignore registerStandardSolvers; |
43 |
%include "solver.h" |
44 |
|
45 |
%include "simulation.h" |
46 |
%extend Simulation{ |
47 |
Instanc __getitem__(const long &index){ |
48 |
return self->getModel().getChild(index); |
49 |
} |
50 |
Instanc __getattr__(const char *name){ |
51 |
return self->getModel().getChild(SymChar(name)); |
52 |
} |
53 |
%pythoncode{ |
54 |
def setParameter(self,name,value): |
55 |
""" set the value of a parameter for this integrator """ |
56 |
P = self.getParameters() |
57 |
P.set(name,value) |
58 |
self.setParameters(P) |
59 |
def getParameterValue(self,name): |
60 |
""" retrieve the *value* of the specified parameter """ |
61 |
P = self.getParameters() |
62 |
for p in P: |
63 |
if p.getName()==name: |
64 |
return p.getValue() |
65 |
raise KeyError |
66 |
} |
67 |
} |
68 |
|
69 |
%include "matrix.h" |
70 |
|
71 |
// SOLVER PARAMETERS |
72 |
%pythoncode{ |
73 |
class SolverParameterIter: |
74 |
def __init__(self, params): |
75 |
self.params = params; |
76 |
self.index = 0; |
77 |
|
78 |
def __iter__(self): |
79 |
return self |
80 |
|
81 |
def next(self): |
82 |
if self.index >= len(self.params): |
83 |
raise StopIteration |
84 |
p = self.params.getParameter(self.index) |
85 |
self.index = self.index +1 |
86 |
return p |
87 |
} |
88 |
|
89 |
class SolverParameters{ |
90 |
public: |
91 |
const std::string toString(); |
92 |
SolverParameters(const SolverParameters &); |
93 |
const int getLength() const; |
94 |
SolverParameter getParameter(const int &) const; |
95 |
}; |
96 |
|
97 |
%extend SolverParameters{ |
98 |
%pythoncode{ |
99 |
def __iter__(self): |
100 |
return SolverParameterIter(self) |
101 |
def __getattr(self,index): |
102 |
for p in self: |
103 |
if p.getName()==index: |
104 |
return p |
105 |
raise KeyError |
106 |
def __getitem__(self,index): |
107 |
if type(index) != types.IntType: |
108 |
raise TypeError |
109 |
return self.getParameter(index) |
110 |
def __len__(self): |
111 |
return self.getLength() |
112 |
def getValue(self,codename): |
113 |
for p in self: |
114 |
if p.getName()==codename: |
115 |
return p.getValue() |
116 |
raise KeyError |
117 |
def set(self,codename,value): |
118 |
for p in self: |
119 |
if p.getName()==codename: |
120 |
p.setValue(value) |
121 |
return |
122 |
raise KeyError |
123 |
} |
124 |
} |
125 |
|
126 |
class SolverParameter{ |
127 |
public: |
128 |
explicit SolverParameter(slv_parameter *); |
129 |
|
130 |
const std::string getName() const; |
131 |
const std::string getDescription() const; |
132 |
const std::string getLabel() const; |
133 |
const int &getNumber() const; |
134 |
const int &getPage() const; |
135 |
|
136 |
const bool isInt() const; |
137 |
const bool isBool() const; |
138 |
const bool isStr() const; |
139 |
const bool isReal() const; |
140 |
|
141 |
// The following throw execeptions unless the parameter type is correct |
142 |
const int &getIntValue() const; |
143 |
const int &getIntLowerBound() const; |
144 |
const int &getIntUpperBound() const; |
145 |
void setIntValue(const int&); |
146 |
|
147 |
const bool getBoolValue() const; |
148 |
void setBoolValue(const bool&); |
149 |
|
150 |
const std::string getStrValue() const; |
151 |
const std::vector<std::string> getStrOptions() const; |
152 |
void setStrValue(const std::string &); |
153 |
void setStrOption(const int &opt); |
154 |
|
155 |
const double &getRealValue() const; |
156 |
const double &getRealLowerBound() const; |
157 |
const double &getRealUpperBound() const; |
158 |
void setRealValue(const double&); |
159 |
|
160 |
const bool isBounded() const; |
161 |
|
162 |
const std::string toString() const; |
163 |
|
164 |
void setValueValue(const Value &); |
165 |
}; |
166 |
|
167 |
%extend SolverParameter{ |
168 |
%pythoncode{ |
169 |
def __str__(self): |
170 |
if self.isInt(): return "%s = %d" %(self.getName(),self.getIntValue()) |
171 |
if self.isBool(): return "%s = %s" %(self.getName(),self.getBoolValue()) |
172 |
if self.isStr(): return "%s = %s" %(self.getName(),self.getStrValue()) |
173 |
if self.isReal(): return "%s = %f" %(self.getName(),self.getRealValue()) |
174 |
raise TypeError |
175 |
def getValue(self): |
176 |
if self.isBool():return self.getBoolValue() |
177 |
if self.isReal():return self.getRealValue() |
178 |
if self.isInt(): return self.getIntValue() |
179 |
if self.isStr(): return self.getStrValue() |
180 |
raise TypeError |
181 |
def setValue(self,value): |
182 |
if self.isBool(): |
183 |
self.setBoolValue(value) |
184 |
return |
185 |
if self.isReal(): |
186 |
self.setRealValue(value) |
187 |
return |
188 |
if self.isInt(): |
189 |
self.setIntValue(value) |
190 |
return |
191 |
if self.isStr(): |
192 |
self.setStrValue(value) |
193 |
return |
194 |
raise TypeError |
195 |
} |
196 |
} |
197 |
|
198 |
%template(IncidencePointVector) std::vector<IncidencePoint>; |
199 |
%include "incidencematrix.h" |
200 |
|
201 |
%extend IncidencePoint{ |
202 |
%pythoncode{ |
203 |
def __repr__(self): |
204 |
return str([ self.row, self.col, int(self.type) ]); |
205 |
} |
206 |
} |
207 |
|
208 |
/* Variables and relations belong to solvers, so they're here: */ |
209 |
|
210 |
%include "variable.h" |
211 |
|
212 |
%extend Variable { |
213 |
%pythoncode{ |
214 |
def __repr__(self): |
215 |
return self.getName() |
216 |
} |
217 |
} |
218 |
|
219 |
//%include "relation.h" |
220 |
class Relation{ |
221 |
public: |
222 |
explicit Relation(const Relation &old); |
223 |
const std::string getName(); |
224 |
double getResidual() const; |
225 |
const std::vector<Variable> getIncidentVariables() const; |
226 |
const int getNumIncidentVariables() const; |
227 |
Instanc getInstance() const; |
228 |
std::string getRelationAsString() const; |
229 |
}; |
230 |
|
231 |
%extend Relation { |
232 |
%pythoncode{ |
233 |
def __repr__(self): |
234 |
return self.getName() |
235 |
} |
236 |
} |
237 |
|
238 |
%template(VariableVector) std::vector<Variable>; |
239 |
%template(RelationVector) std::vector<Relation>; |
240 |
%template(SolverVector) std::vector<Solver>; |
241 |
|
242 |
class SolverStatus{ |
243 |
public: |
244 |
SolverStatus(); |
245 |
explicit SolverStatus(const SolverStatus &old); |
246 |
void getSimulationStatus(Simulation &); |
247 |
|
248 |
const bool isOK() const; |
249 |
const bool isOverDefined() const; |
250 |
const bool isUnderDefined() const; |
251 |
const bool isStructurallySingular() const; |
252 |
const bool isInconsistent() const; |
253 |
const bool isReadyToSolve() const; |
254 |
const bool isConverged() const; |
255 |
const bool isDiverged() const; |
256 |
const bool hasResidualCalculationErrors() const; |
257 |
const bool hasExceededIterationLimit() const; |
258 |
const bool hasExceededTimeLimit() const; |
259 |
const bool isInterrupted() const; |
260 |
const int getIterationNum() const; |
261 |
|
262 |
// block structure stuff... |
263 |
|
264 |
const int getNumBlocks() const; |
265 |
const int getCurrentBlockNum() const; |
266 |
const int getCurrentBlockSize() const; |
267 |
const int getCurrentBlockIteration() const; |
268 |
const int getNumConverged() const; /* previous total size */ |
269 |
const int getNumJacobianEvals() const; |
270 |
const int getNumResidualEvals() const; |
271 |
const double getBlockResidualRMS() const; |
272 |
|
273 |
}; |
274 |
|
275 |
%feature("director") SolverReporter; |
276 |
|
277 |
class SolverReporter{ |
278 |
public: |
279 |
SolverReporter(); |
280 |
virtual ~SolverReporter(); |
281 |
virtual int report(SolverStatus *status); |
282 |
virtual void finalise(SolverStatus *status); |
283 |
}; |
284 |
|
285 |
%apply SWIGTYPE *DISOWN { IntegratorReporterCxx *reporter }; |
286 |
|
287 |
%feature("autodoc", "Return dict of available integration engines {id:name,...}") Integrator::getEngines; |
288 |
%include "integrator.h" |
289 |
/* findIndependentVar has changed to return void, throw exception */ |
290 |
|
291 |
%extend Integrator{ |
292 |
%pythoncode{ |
293 |
def setParameter(self,name,value): |
294 |
""" set the value of a parameter for this integrator """ |
295 |
P = self.getParameters() |
296 |
P.set(name,value) |
297 |
self.setParameters(P) |
298 |
def getParameterValue(self,name): |
299 |
""" retrieve the *value* of the specified parameter """ |
300 |
P = self.getParameters() |
301 |
for p in P: |
302 |
if p.getName()==name: |
303 |
return p.getValue() |
304 |
raise KeyError |
305 |
} |
306 |
} |
307 |
|
308 |
%feature("director") IntegratorReporterCxx; |
309 |
%ignore ascxx_integratorreporter_init; |
310 |
%ignore ascxx_integratorreporter_write; |
311 |
%ignore ascxx_integratorreporter_write_obs; |
312 |
%ignore ascxx_integratorreporter_close; |
313 |
%include "integratorreporter.h" |
314 |
|
315 |
%feature("director") SolverHooks; |
316 |
%ignore ascxx_slvreq_set_solver; |
317 |
%ignore ascxx_slvreq_set_option; |
318 |
%ignore ascxx_slvreq_do_solve; |
319 |
%apply SWIGTYPE *DISOWN {SolverReporter *reporter}; |
320 |
%apply SWIGTYPE *DISOWN {SolverHooks *hooks}; |
321 |
%include "solverhooks.h" |