/[ascend]/trunk/pygtk/solver.i
ViewVC logotype

Contents of /trunk/pygtk/solver.i

Parent Directory Parent Directory | Revision Log Revision Log


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

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22