~jdpipe/ascend/trunk-old

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
	SWIG interface for accessing Solver and choosing solver parameters
*/

%include <python/std_vector.i>
%include <python/std_except.i>

%import "ascpy.i"

%{
#include "config.h"
#include "integrator.h"
#include "integratorreporter.h"
#include "solver.h"
#include "incidencematrix.h"
#include "solverparameter.h"
#include "solverparameters.h"
#include "solverreporter.h"
#include "solverhooks.h"
#include "curve.h"
#include "matrix.h"
%}

%pythoncode{
	import types
}

%typemap(in) FILE * {
%#if defined(__MINGW32__) && !defined(HAVE_MSVCR71)
	PyErr_SetString(PyExc_TypeError,"File passing from python to ASCEND not yet implemented on Windows");
	return NULL;
%#else
    if (!PyFile_Check($input)) {
        PyErr_SetString(PyExc_TypeError, "Need a file!");
        return NULL;
    }
    $1 = PyFile_AsFile($input);
%#endif
}

%ignore registerSolver;
%ignore registerStandardSolvers;
%include "solver.h"

%include "simulation.h"
%extend Simulation{
	Instanc __getitem__(const long &index){
		return self->getModel().getChild(index);
	}
	Instanc __getattr__(const char *name){
		return self->getModel().getChild(SymChar(name));
	}
	%pythoncode{
		def setParameter(self,name,value):
			""" set the value of a parameter for this integrator """
			P = self.getParameters()
			P.set(name,value)
			self.setParameters(P)
		def getParameterValue(self,name):
			""" retrieve the *value* of the specified parameter """
			P = self.getParameters()
			for p in P:
				if p.getName()==name:
					return p.getValue()
			raise KeyError
	}
}

%include "matrix.h"

// SOLVER PARAMETERS
%pythoncode{
	class SolverParameterIter:
		def __init__(self, params):
			self.params = params;
			self.index = 0;

		def __iter__(self):
			return self

		def next(self):
			if self.index >= len(self.params):
				raise StopIteration
			p = self.params.getParameter(self.index)
			self.index = self.index +1
			return p
}

class SolverParameters{
public:
	const std::string toString();
	SolverParameters(const SolverParameters &);
	const int getLength() const;
	SolverParameter getParameter(const int &) const;
};

%extend SolverParameters{
	%pythoncode{
		def __iter__(self):
			return SolverParameterIter(self)
		def __getattr(self,index):
			for p in self:
				if p.getName()==index:
					return p
			raise KeyError
		def __getitem__(self,index):
			if type(index) != types.IntType:
				raise TypeError
			return self.getParameter(index)
		def __len__(self):
			return self.getLength()
		def getValue(self,codename):
			for p in self:
				if p.getName()==codename:
					return p.getValue()
			raise KeyError
		def set(self,codename,value):
			for p in self:
				if p.getName()==codename:
					p.setValue(value)
					return
			raise KeyError
	}
}

class SolverParameter{
public:
	explicit SolverParameter(slv_parameter *);

	const std::string getName() const;
	const std::string getDescription() const;
	const std::string getLabel() const;
	const int &getNumber() const;
	const int &getPage() const;

	const bool isInt() const;
	const bool isBool() const;
	const bool isStr() const;
	const bool isReal() const;

	// The following throw execeptions unless the parameter type is correct
	const int &getIntValue() const;
	const int &getIntLowerBound() const;
	const int &getIntUpperBound() const;
	void setIntValue(const int&);

	const bool getBoolValue() const;
	void setBoolValue(const bool&);

	const std::string getStrValue() const;
	const std::vector<std::string> getStrOptions() const;
	void setStrValue(const std::string &);
	void setStrOption(const int &opt);

	const double &getRealValue() const;
	const double &getRealLowerBound() const;
	const double &getRealUpperBound() const;
	void setRealValue(const double&);

	const bool isBounded() const;

	const std::string toString() const;

	void setValueValue(const Value &);
};

%extend SolverParameter{
	%pythoncode{
		def __str__(self):
			if self.isInt(): return "%s = %d" %(self.getName(),self.getIntValue())
			if self.isBool(): return "%s = %s" %(self.getName(),self.getBoolValue())
			if self.isStr(): return "%s = %s" %(self.getName(),self.getStrValue())
			if self.isReal(): return "%s = %f" %(self.getName(),self.getRealValue())
			raise TypeError
		def getValue(self):
			if self.isBool():return self.getBoolValue()
			if self.isReal():return self.getRealValue()
			if self.isInt(): return self.getIntValue()
			if self.isStr(): return self.getStrValue()
			raise TypeError
		def setValue(self,value):
			if self.isBool():
				self.setBoolValue(value)
				return
			if self.isReal():
				self.setRealValue(value)
				return
			if self.isInt():
				self.setIntValue(value)
				return
			if self.isStr():
				self.setStrValue(value)
				return
			raise TypeError
	}
}

%template(IncidencePointVector) std::vector<IncidencePoint>;
%include "incidencematrix.h"

%extend IncidencePoint{
	%pythoncode{
		def __repr__(self):
			return str([ self.row, self.col, int(self.type) ]);
	}
}

/* Variables and relations belong to solvers, so they're here: */

%include "variable.h"

%extend Variable {
	%pythoncode{
		def __repr__(self):
			return self.getName()
	}
}

//%include "relation.h"
class Relation{
public:
	explicit Relation(const Relation &old);
	const std::string getName();
	double getResidual() const;
	const std::vector<Variable> getIncidentVariables() const;
	const int getNumIncidentVariables() const;
	Instanc getInstance() const;
	std::string getRelationAsString() const;
};

%extend Relation {
	%pythoncode{
		def __repr__(self):
			return self.getName()
	}
}

%template(VariableVector) std::vector<Variable>;
%template(RelationVector) std::vector<Relation>;
%template(SolverVector) std::vector<Solver>;

class SolverStatus{
public:
	SolverStatus();
	explicit SolverStatus(const SolverStatus &old);
	void getSimulationStatus(Simulation &);

	const bool isOK() const;
	const bool isOverDefined() const;
	const bool isUnderDefined() const;
	const bool isStructurallySingular() const;
	const bool isInconsistent() const;
	const bool isReadyToSolve() const;
	const bool isConverged() const;
	const bool isDiverged() const;
	const bool hasResidualCalculationErrors() const;
	const bool hasExceededIterationLimit() const;
	const bool hasExceededTimeLimit() const;
	const bool isInterrupted() const;
	const int getIterationNum() const;

	// block structure stuff...

	const int getNumBlocks() const;
	const int getCurrentBlockNum() const;
	const int getCurrentBlockSize() const;
	const int getCurrentBlockIteration() const;
	const int getNumConverged() const; /* previous total size */
	const int getNumJacobianEvals() const;
	const int getNumResidualEvals() const;
	const double getBlockResidualRMS() const;

};

%feature("director") SolverReporter;

class SolverReporter{
public:
	SolverReporter();
	virtual ~SolverReporter();
	virtual int report(SolverStatus *status);
	virtual void finalise(SolverStatus *status);
};

%apply SWIGTYPE *DISOWN { IntegratorReporterCxx *reporter };

%feature("autodoc", "Return dict of available integration engines {id:name,...}") Integrator::getEngines;
%include "integrator.h"
/* findIndependentVar has changed to return void, throw exception */

%extend Integrator{
	%pythoncode{
		def setParameter(self,name,value):
			""" set the value of a parameter for this integrator """
			P = self.getParameters()
			P.set(name,value)
			self.setParameters(P)
		def getParameterValue(self,name):
			""" retrieve the *value* of the specified parameter """
			P = self.getParameters()
			for p in P:
				if p.getName()==name:
					return p.getValue()
			raise KeyError
	}
}

%feature("director") IntegratorReporterCxx;
%ignore ascxx_integratorreporter_init;
%ignore ascxx_integratorreporter_write;
%ignore ascxx_integratorreporter_write_obs;
%ignore ascxx_integratorreporter_close;
%include "integratorreporter.h"

%feature("director") SolverHooks;
%ignore ascxx_slvreq_set_solver;
%ignore ascxx_slvreq_set_option;
%ignore ascxx_slvreq_do_solve;
%apply SWIGTYPE *DISOWN {SolverReporter *reporter};
%apply SWIGTYPE *DISOWN {SolverHooks *hooks};
%include "solverhooks.h"