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

Annotation of /trunk/pygtk/solver.i

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1112 - (hide annotations) (download)
Thu Jan 11 05:38:15 2007 UTC (15 years, 7 months ago) by johnpye
File size: 7218 byte(s)
Added writeBlockGraph to ascpy API: writes out a .dot file.
1 johnpye 221 /*
2     SWIG interface for accessing Solver and choosing solver parameters
3     */
4    
5 johnpye 969 %include <python/std_vector.i>
6     %include <python/std_except.i>
7    
8     %import "ascpy.i"
9    
10 johnpye 669 %{
11 johnpye 1112 #include "config.h"
12 johnpye 669 #include "integrator.h"
13     #include "integratorreporter.h"
14 johnpye 969 #include "solver.h"
15     #include "incidencematrix.h"
16     #include "solverparameter.h"
17     #include "solverparameters.h"
18     #include "solverreporter.h"
19     #include "curve.h"
20 johnpye 1112
21     #ifdef ASC_WITH_MFGRAPH
22     # include <fstream>
23     # include <mfgraph/mfg_draw_graph.h>
24     #endif
25 johnpye 669 %}
26    
27 johnpye 943 %pythoncode{
28     import types
29     }
30    
31 johnpye 735 %template(VariableVector) std::vector<Variable>;
32     %template(RelationVector) std::vector<Relation>;
33 johnpye 785 %template(SolverVector) std::vector<Solver>;
34 johnpye 735
35 johnpye 785 %ignore registerSolver;
36     %ignore registerStandardSolvers;
37     %include "solver.h"
38 johnpye 735
39 johnpye 969
40 johnpye 772 %include "simulation.h"
41 johnpye 979 %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 johnpye 221
50 johnpye 979 // SOLVER PARAMETERS
51 johnpye 221 %pythoncode{
52     class SolverParameterIter:
53     def __init__(self, params):
54     self.params = params;
55     self.index = 0;
56    
57 johnpye 942 def __iter__(self):
58     return self
59    
60 johnpye 221 def next(self):
61 johnpye 943 if self.index >= len(self.params):
62 johnpye 221 raise StopIteration
63 johnpye 943 p = self.params.getParameter(self.index)
64     self.index = self.index +1
65     return p
66 johnpye 221 }
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 johnpye 943 def __getattr(self,index):
81     for p in self:
82     if p.getName()==index:
83     return p
84     raise KeyError
85 johnpye 942 def __getitem__(self,index):
86 johnpye 943 if type(index) != types.IntType:
87     raise TypeError
88 johnpye 942 return self.getParameter(index)
89     def __len__(self):
90     return self.getLength()
91 johnpye 943 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 johnpye 221 }
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 johnpye 481
120 johnpye 221 // 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 johnpye 225 void setIntValue(const int&);
125 johnpye 221
126     const bool getBoolValue() const;
127 johnpye 225 void setBoolValue(const bool&);
128 johnpye 221
129     const std::string getStrValue() const;
130     const std::vector<std::string> getStrOptions() const;
131 johnpye 225 void setStrValue(const std::string &);
132     void setStrOption(const int &opt);
133 johnpye 221
134     const double &getRealValue() const;
135     const double &getRealLowerBound() const;
136     const double &getRealUpperBound() const;
137 johnpye 225 void setRealValue(const double&);
138    
139     const bool isBounded() const;
140    
141     const std::string toString() const;
142 johnpye 221 };
143    
144 johnpye 942 %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 johnpye 943 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 johnpye 1016 if self.isBool():
160 johnpye 943 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 johnpye 942 }
173     }
174    
175 johnpye 1112 %template(IncidencePointVector) std::vector<IncidencePoint>;
176     %include "incidencematrix.h"
177 johnpye 233
178     %extend IncidencePoint{
179     %pythoncode{
180     def __repr__(self):
181     return str([ self.row, self.col, int(self.type) ]);
182     }
183     }
184    
185 johnpye 1112 %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 johnpye 233
195 johnpye 254 /* Variables and relations belong to solvers, so they're here: */
196    
197 johnpye 854 %include "variable.h"
198    
199 johnpye 254 %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 johnpye 669 const std::string getName();
210 johnpye 286 const double &getResidual();
211 johnpye 735 const std::vector<Variable> getIncidentVariables() const;
212     const int getNumIncidentVariables() const;
213 johnpye 736 Instanc getInstance() const;
214     std::string getRelationAsString() const;
215 johnpye 254 };
216    
217     %extend Relation {
218     %pythoncode{
219     def __repr__(self):
220     return self.getName()
221     }
222     }
223    
224 johnpye 307
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 johnpye 481
245 johnpye 307 // 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 johnpye 481 %feature("director") SolverReporter;
259 johnpye 307
260 johnpye 310 class SolverReporter{
261     public:
262     SolverReporter();
263     virtual ~SolverReporter();
264 johnpye 312 virtual int report(SolverStatus *status);
265 johnpye 319 virtual void finalise(SolverStatus *status);
266 johnpye 310 };
267    
268 johnpye 669 %apply SWIGTYPE *DISOWN { IntegratorReporterCxx *reporter };
269    
270 johnpye 1068 %feature("autodoc", "Return dict of available integration engines {id:name,...}") Integrator::getEngines;
271 johnpye 669 %include "integrator.h"
272 johnpye 1095 /* findIndependentVar has changed to return void, throw exception */
273 johnpye 669
274 johnpye 943 %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 johnpye 669 %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