/[ascend]/trunk/pygtk/integrator.cpp
ViewVC logotype

Contents of /trunk/pygtk/integrator.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 915 - (show annotations) (download) (as text)
Sat Oct 28 07:06:22 2006 UTC (15 years, 8 months ago) by johnpye
File MIME type: text/x-c++src
File size: 4462 byte(s)
Working on what the problem is with 'on_load' methods in the C++/Python code.
Seems that 'EXTERNAL solve(SELF);' puts the model into a state which 'works' but if that's
not done first, then that set values in the model don't work correctly...
1 #include "integrator.h"
2 #include "integratorreporter.h"
3 #include <stdexcept>
4 using namespace std;
5
6 /**
7 'creating' an integrator in the context of the GUI just means an object
8 we can store the parameters that will be later sent to the underlying
9 C-code API.
10 */
11 Integrator::Integrator(Simulation &simulation)
12 : simulation(simulation)
13 {
14 // create the C-level object
15 this->blsys = integrator_new(simulation.getSystem(),simulation.getModel().getInternalType());
16
17 samplelist = NULL;
18
19 // set default steps
20 setMinSubStep(0);
21 setMaxSubStep(0);
22 setMaxSubSteps(0);
23 setInitialSubStep(0);
24 }
25
26 Integrator::~Integrator(){
27 integrator_free(blsys);
28 samplelist_free(samplelist);
29 }
30
31
32 void
33 Integrator::setReporter(IntegratorReporterCxx *reporter){
34 this->blsys->clientdata = reporter;
35 integrator_set_reporter(blsys,reporter->getInternalType());
36 CONSOLE_DEBUG("REPORTER HAS BEEN SET");
37 (*(this->blsys->reporter->init))(blsys);
38 CONSOLE_DEBUG("DONE TESTING OUTPUT_INIT");
39 }
40
41 double
42 Integrator::getCurrentTime(){
43 return integrator_get_t(blsys);
44 }
45
46 long
47 Integrator::getCurrentStep(){
48 return integrator_getcurrentstep(blsys);
49 }
50
51 long
52 Integrator::getNumSteps(){
53 return integrator_getnsamples(blsys);
54 }
55
56 int
57 Integrator::findIndependentVar(){
58 return integrator_find_indep_var(blsys);
59 }
60
61 int
62 Integrator::analyse(){
63
64 int res;
65 res = integrator_analyse(blsys);
66
67 if(!res){
68 ERROR_REPORTER_NOLINE(ASC_USER_ERROR,"Failed system analysis");
69 return 0;
70 }
71
72 return 1;
73 }
74
75 /**
76 @TODO what about root detection?
77 */
78 int
79 Integrator::solve(){
80
81 // check the integration limits
82 // trigger of the solution process
83 // report errors?
84
85 assert(samplelist!=NULL);
86 assert(samplelist->ns>0);
87 assert(blsys->reporter!=NULL);
88 assert(blsys->clientdata!=NULL);
89
90 int res;
91 res = integrator_solve(blsys, 0, samplelist_length(samplelist)-1);
92
93 // communicate solver variable status back to the instance tree via 'interface_ptr'
94 simulation.processVarStatus();
95
96 if(!res){
97 ERROR_REPORTER_NOLINE(ASC_USER_ERROR,"Failed integration");
98 return 0;
99 }
100
101 return 1;
102 }
103
104 int
105 Integrator::setEngine(IntegratorEngine engine){
106 return integrator_set_engine(this->blsys, engine);
107 }
108
109 int
110 Integrator::setEngine(int engine){
111 return integrator_set_engine(this->blsys, (IntegratorEngine)engine);
112 }
113
114 /**
115 Ideally this list would be dynamically generated based on what solvers
116 are available or are in memory.
117 */
118 map<int,string>
119 Integrator::getEngines() const{
120 map<int,string> m;
121 #ifdef ASC_WITH_LSODE
122 m.insert(pair<int,string>(INTEG_LSODE,"LSODE"));
123 #endif
124 #ifdef ASC_WITH_IDA
125 m.insert(pair<int,string>(INTEG_IDA,"IDA"));
126 #endif
127 return m;
128 }
129
130 string
131 Integrator::getEngineName() const{
132 map<int,string> m=getEngines();
133 map<int,string>::iterator f = m.find(integrator_get_engine(blsys));
134 if(f==m.end()){
135 throw runtime_error("No engine selected");
136 }
137 return f->second;
138 }
139
140 void
141 Integrator::setLinearTimesteps(UnitsM units, double start, double end, unsigned long num){
142 if(samplelist!=NULL){
143 ASC_FREE(samplelist);
144 }
145 const dim_type *d = units.getDimensions().getInternalType();
146 samplelist = samplelist_new(num+1, d);
147 double val = start;
148 double inc = (end-start)/(num);
149 for(unsigned long i=0;i<=num;++i){
150 samplelist_set(samplelist,i,val);
151 val += inc;
152 }
153 integrator_set_samples(blsys,samplelist);
154 }
155
156 vector<double>
157 Integrator::getCurrentObservations(){
158 double *d = ASC_NEW_ARRAY(double,getNumObservedVars());
159 integrator_get_observations(blsys,d);
160 vector<double> v=vector<double>(d,d+getNumObservedVars());
161 // do I need to free d?
162 // can I do this in such a way as I avoid all this memory-copying?
163 return v;
164 }
165
166 Variable
167 Integrator::getObservedVariable(const long &i){
168 var_variable *v = integrator_get_observed_var(blsys,i);
169 return Variable(&simulation,v);
170 }
171
172 Variable
173 Integrator::getIndependentVariable(){
174 var_variable *v = integrator_get_independent_var(blsys);
175 if(v==NULL){
176 throw runtime_error("independent variable is null");
177 }
178 return Variable(&simulation,v);
179 }
180
181 int
182 Integrator::getNumVars(){
183 return blsys->n_y;
184 }
185
186 int
187 Integrator::getNumObservedVars(){
188 return blsys->n_obs;
189 }
190
191 void
192 Integrator::setMinSubStep(double n){
193 integrator_set_minstep(blsys,n);
194 }
195
196 void
197 Integrator::setMaxSubStep(double n){
198 integrator_set_maxstep(blsys,n);
199 }
200
201 void
202 Integrator::setInitialSubStep(double n){
203 integrator_set_stepzero(blsys,n);
204 }
205
206 void
207 Integrator::setMaxSubSteps(int n){
208 integrator_set_maxsubsteps(blsys,n);
209 }
210
211 IntegratorSystem *
212 Integrator::getInternalType(){
213 return blsys;
214 }

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