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

Contents of /trunk/pygtk/solver.i

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1678 - (show annotations) (download)
Wed Dec 19 00:29:48 2007 UTC (16 years, 7 months ago) by jpye
File size: 7666 byte(s)
Removed references to MFGRAPH, seems better to use GraphViz directly.
Added configuration test for GraphViz.
Added configuration test for strdup, fixed mmio to not create its own strdup if other form is available already.

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
23 %pythoncode{
24 import types
25 }
26
27 %typemap(in) FILE * {
28 %#ifdef __MINGW32__
29 PyErr_SetString(PyExc_TypeError,"File passing from python to ASCEND not yet implemented on Windows");
30 return NULL;
31 %#else
32 if (!PyFile_Check($input)) {
33 PyErr_SetString(PyExc_TypeError, "Need a file!");
34 return NULL;
35 }
36 $1 = PyFile_AsFile($input);
37 %#endif
38 }
39
40 %template(VariableVector) std::vector<Variable>;
41 %template(RelationVector) std::vector<Relation>;
42 %template(SolverVector) std::vector<Solver>;
43
44 %ignore registerSolver;
45 %ignore registerStandardSolvers;
46 %include "solver.h"
47
48 %include "simulation.h"
49 %extend Simulation{
50 Instanc __getitem__(const long &index){
51 return self->getModel().getChild(index);
52 }
53 Instanc __getattr__(const char *name){
54 return self->getModel().getChild(SymChar(name));
55 }
56 %pythoncode{
57 def setParameter(self,name,value):
58 """ set the value of a parameter for this integrator """
59 P = self.getParameters()
60 P.set(name,value)
61 self.setParameters(P)
62 def getParameterValue(self,name):
63 """ retrieve the *value* of the specified parameter """
64 P = self.getParameters()
65 for p in P:
66 if p.getName()==name:
67 return p.getValue()
68 raise KeyError
69 }
70 }
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 /* Variables and relations belong to solvers, so they're here: */
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"

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