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