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

Contents of /trunk/pygtk/solver.i

Parent Directory Parent Directory | Revision Log Revision Log


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

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