/[ascend]/trunk/pygtk/interface/simulation.cpp
ViewVC logotype

Contents of /trunk/pygtk/interface/simulation.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 151 - (show annotations) (download) (as text)
Thu Dec 22 10:58:33 2005 UTC (18 years, 6 months ago) by johnpye
File MIME type: text/x-c++src
File size: 12481 byte(s)
Working on a test case for bug #178
1 #include <iostream>
2 #include <iomanip>
3 #include <stdexcept>
4 #include <sstream>
5 using namespace std;
6
7 extern "C"{
8 #include <utilities/ascConfig.h>
9 #include <utilities/ascSignal.h>
10 #include <utilities/ascMalloc.h>
11 #include <general/dstring.h>
12 #include <compiler/instance_enum.h>
13 #include <compiler/fractions.h>
14 #include <compiler/compiler.h>
15 #include <compiler/dimen.h>
16 #include <compiler/symtab.h>
17 #include <compiler/instance_io.h>
18 #include <compiler/instantiate.h>
19 #include <compiler/bintoken.h>
20 #include <compiler/instance_enum.h>
21 #include <compiler/instquery.h>
22 #include <compiler/check.h>
23 #include <compiler/name.h>
24 #include <compiler/pending.h>
25
26 #include <utilities/readln.h>
27 #include <solver/mtx.h>
28 #include <solver/slv_types.h>
29 #include <solver/var.h>
30 #include <solver/rel.h>
31 #include <solver/discrete.h>
32 #include <solver/conditional.h>
33 #include <solver/logrel.h>
34 #include <solver/bnd.h>
35 #include <solver/calc.h>
36 #include <solver/relman.h>
37 #include <solver/slv_common.h>
38 #include <solver/linsol.h>
39 #include <solver/linsolqr.h>
40 #include <solver/slv_client.h>
41 #include <solver/system.h>
42 #include <solver/slv_interface.h>
43 #include <solver/slvDOF.h>
44 #include <solver/slv3.h>
45 #include <solver/slv_stdcalls.h>
46 }
47
48 #include "simulation.h"
49 #include "solver.h"
50 #include "name.h"
51
52 /**
53 Create an instance of a type (call compiler etc)
54
55 @TODO fix mutex on compile command filenames
56 */
57 Simulation::Simulation(Instance *i, const SymChar &name) : Instanc(i, name), simroot(GetSimulationRoot(i),SymChar("simroot")){
58 // Create an Instance object for the 'simulation root' (we'll call
59 // it the 'simulation model') and it can be fetched using 'getModel()'
60 // any time later.
61 //simroot = Instanc(GetSimulationRoot(i),name);
62 }
63
64 Instanc &
65 Simulation::getModel(){
66 if(!simroot.getInternalType()){
67 throw runtime_error("Simulation::getModel: simroot.getInternalType()is NULL");
68 }
69 return simroot;
70 }
71
72 void
73 Simulation::checkDoF() const{
74 cerr << "CHECKING DOF..." << endl;
75 int dof, status;
76 if(sys==NULL){
77 throw runtime_error("System not yet built");
78 }
79 slvDOF_status(sys, &status, &dof);
80 switch(status){
81 case 1: error_reporter(ASC_USER_ERROR,NULL,0,"Underspecified; %d degrees of freedom",dof); break;
82 case 2: error_reporter(ASC_USER_NOTE,NULL,0,"Square"); break;
83 case 3: error_reporter(ASC_USER_ERROR,NULL,0,"Structurally singular"); break;
84 case 4: error_reporter(ASC_USER_ERROR,NULL,0,"Overspecified"); break;
85 case 5:
86 throw runtime_error("Unable to resolve degrees of freedom"); break;
87 default:
88 throw runtime_error("Invalid return status from slvDOF_status");
89 }
90 }
91
92 void
93 Simulation::run(const Method &method){
94 cerr << "RUNNING PROCEDURE " << method.getName() << endl;
95 Nam name = Nam(method.getSym());
96 cerr << "CREATED NAME '" << name.getName() << "'" << endl;
97 Proc_enum pe;
98 pe = Initialize(
99 getModel().getInternalType() ,name.getInternalType(), "__not_named__"
100 ,ASCERR
101 ,0, NULL, NULL
102 );
103
104 if(pe == Proc_all_ok){
105 error_reporter(ASC_PROG_NOTE,NULL,0,"Method '%s' was run (check above for errors)",method.getName());
106 //cerr << "METHOD " << method.getName() << " COMPLETED OK" << endl;
107 }else{
108 stringstream ss;
109 ss << "Simulation::run: Method '" << method.getName() << "' returned error: ";
110 switch(pe){
111 case Proc_CallOK: ss << "Call OK"; break;
112 case Proc_CallError: ss << "Error occurred in call"; break;
113 case Proc_CallReturn: ss << "Request that caller return (OK)"; break;
114 case Proc_CallBreak: ss << "Break out of enclosing loop"; break;
115 case Proc_CallContinue: ss << "Skip to next iteration"; break;
116
117 case Proc_break: ss << "Break"; break;
118 case Proc_continue: ss << "Continue"; break;
119 case Proc_fallthru: ss << "Fall-through"; break;
120 case Proc_return: ss << "Return"; break;
121 case Proc_stop: ss << "Stop"; break;
122 case Proc_stack_exceeded: ss << "Stack exceeded"; break;
123 case Proc_stack_exceeded_this_frame: ss << "Stack exceeded this frame"; break;
124 case Proc_case_matched: ss << "Case matched"; break;
125 case Proc_case_unmatched: ss << "Case unmatched"; break;
126
127 case Proc_case_undefined_value: ss << "Undefined value in case"; break;
128 case Proc_case_boolean_mismatch: ss << "Boolean mismatch in case"; break;
129 case Proc_case_integer_mismatch: ss << "Integer mismatch in case"; break;
130 case Proc_case_symbol_mismatch: ss << "Symbol mismatch in case"; break;
131 case Proc_case_wrong_index: ss << "Wrong index in case"; break;
132 case Proc_case_wrong_value: ss << "Wrong value in case"; break;
133 case Proc_case_extra_values: ss << "Extra values in case"; break;
134 case Proc_bad_statement: ss << "Bad statement"; break;
135 case Proc_bad_name: ss << "Bad name"; break;
136 case Proc_for_duplicate_index: ss << "Duplicate index"; break;
137 case Proc_for_set_err: ss << "For set error"; break;
138 case Proc_for_not_set: ss << "For not set"; break;
139 case Proc_illegal_name_use: ss << "Illegal name use"; break;
140 case Proc_name_not_found: ss << "Name not found"; break;
141 case Proc_instance_not_found: ss << "Instance not found"; break;
142 case Proc_type_not_found: ss << "Type not found"; break;
143 case Proc_illegal_type_use: ss << "Illegal use"; break;
144 case Proc_proc_not_found: ss << "Method not found"; break;
145 case Proc_if_expr_error_typeconflict: ss << "Type conflict in 'if' expression"; break;
146 case Proc_if_expr_error_nameunfound: ss << "Name not found in 'if' expression"; break;
147 case Proc_if_expr_error_incorrectname: ss << "Incorrect name in 'if' expression"; break;
148 case Proc_if_expr_error_undefinedvalue: ss << "Undefined value in 'if' expression"; break;
149 case Proc_if_expr_error_dimensionconflict: ss << "Dimension conflict in 'if' expression"; break;
150 case Proc_if_expr_error_emptychoice: ss << "Empty choice in 'if' expression"; break;
151 case Proc_if_expr_error_emptyintersection: ss << "Empty intersection in 'if' expression"; break;
152 case Proc_if_expr_error_confused: ss << "Confused in 'if' expression"; break;
153 case Proc_if_real_expr: ss << "Real-valued result in 'if' expression"; break;
154 case Proc_if_integer_expr: ss << "Integeter-valued result in 'if' expression"; break;
155 case Proc_if_symbol_expr: ss << "Symbol-valued result in 'if' expression"; break;
156 case Proc_if_set_expr: ss << "Set-valued result in 'if' expression"; break;
157 case Proc_if_not_logical: ss << "If expression is not logical"; break;
158 case Proc_user_interrupt: ss << "User interrupt"; break;
159 case Proc_infinite_loop: ss << "Infinite loop"; break;
160 case Proc_declarative_constant_assignment: ss << "Declarative constant assignment"; break;
161 case Proc_nonsense_assignment: ss << "Nonsense assginment (bogus)"; break;
162 case Proc_nonconsistent_assignment: ss << "Inconsistent assignment"; break;
163 case Proc_nonatom_assignment: ss << "Non-atom assignment"; break;
164 case Proc_nonboolean_assignment: ss << "Non-boolean assignment"; break;
165 case Proc_noninteger_assignment: ss << "Non-integer assignment"; break;
166 case Proc_nonreal_assignment: ss << "Non-real assignment"; break;
167 case Proc_nonsymbol_assignment: ss << "Non-symbol assignment"; break;
168 case Proc_lhs_error: ss << "Left-hand-side error"; break;
169 case Proc_rhs_error: ss << "Right-hand-side error"; break;
170 case Proc_unknown_error: ss << "Unknown error"; break;
171 default:
172 ss << "Invalid error code";
173 }
174
175
176 ss << " (" << int(pe) << ")";
177 throw runtime_error(ss.str());
178 }
179 }
180
181 const bool
182 Simulation::check(){
183 cerr << "CHECKING SIMULATION" << endl;
184 Instance *i1 = getModel().getInternalType();
185 CheckInstance(stderr, i1);
186 cerr << "...DONE CHECKING" << endl;
187 }
188
189 void
190 Simulation::build(){
191 cerr << "BUILDING SIMULATION..." << endl;
192 sys = system_build(getModel().getInternalType());
193 if(sys == NULL){
194 throw runtime_error("Unable to build system");
195 }
196 cerr << "...DONE BUILDING" << endl;
197 }
198
199 vector<Variable>
200 Simulation::getFixableVariables(){
201 cerr << "GETTING FIXABLE VARIABLES..." << endl;
202 vector<Variable> vars;
203 vars.reserve(100);
204
205 if(sys==NULL){
206 throw runtime_error("Simulation system not yet built");
207 }
208
209 int32 **vip; /** TODO ensure 32 bit integers are used */
210
211 // Get IDs of elegible variables in array at vip...
212 if(!slvDOF_eligible(sys,vip)){
213 error_reporter(ASC_USER_NOTE,NULL,0,"No fixable variables found.");
214 }else{
215 //cerr << "FIXABLE VARS FOUND" << endl;
216 struct var_variable **vp = slv_get_solvers_var_list(sys);
217
218 /*struct var_variable *first_var = vp[0];
219 char *first_var_name = var_make_name(sys,first_var);
220 cerr << "FIRST SYS VAR IS NAMED " << var_make_name(s,first_var) << endl;
221 ascfree(first_var_name);*/
222
223 if(vp==NULL){
224 throw runtime_error("Simulation variable list is null");
225 }
226
227 // iterate through this list until we find a -1:
228 int i=0;
229 int var_index = (*vip)[i];
230 while(var_index >= 0){
231 //cerr << "FOUND VARIABLE var_index = " << var_index << endl;
232 struct var_variable *var = vp[var_index];
233 //cerr << "VARIABLE " << var_index << " IS ELIGIBLE" << endl;
234 char *var_name = var_make_name(sys,var);
235 //cerr << "ELIGIBLE VAR: " << var_name << endl;
236 ascfree(var_name);
237 vars.push_back(Variable( sys,var ));
238 ++i;
239 var_index = (*vip)[i];
240 }
241 error_reporter(ASC_USER_NOTE,NULL,0,"Found %d fixable variables.",i);
242 //cerr << "END ELEGIBLE VARS LIST" << endl;
243 ascfree(*vip);
244 //cerr << "FREED VIP LIST" << endl;
245 }
246
247 //cerr << "FINISHED WITH FINDING ELEGIBLE VARIABLES" << endl;
248 return vars;
249 }
250
251 void
252 Simulation::solve(Solver solver){
253 cerr << "SIMULATION::SOLVE STARTING..." << endl;
254 enum inst_t k = getModel().getKind();
255 if(k!=MODEL_INST)throw runtime_error("Can't solve: not an instance of type MODEL_INST");
256
257 int npend = NumberPendingInstances(getInternalType());
258 if(npend)throw runtime_error("Can't solve: There are still %d pending instances");
259
260 if(!sys)throw runtime_error("Can't solve: Simulation system has not been built yet.");
261
262 cerr << "SIMULATION::SOLVE: SET SOLVER..." << endl;
263 setSolver(solver);
264
265
266 cerr << "PRESOLVING SYSTEM..." << endl;
267 slv_presolve(sys);
268 cerr << "... DONE PRESOLVING" << endl;
269
270 cerr << "SOLVING SYSTEM..." << endl;
271 slv_solve(sys);
272
273 slv_status_t slvstat;
274 slv_get_status(sys,&slvstat);
275 if(slvstat.ok){
276 cerr << "... DONE SOLVING SYSTEM" << endl;
277 }else{
278 error_reporter(ASC_USER_ERROR,NULL,0,"Solver failed");
279 }
280
281 cerr << "SOLVER PERFORMED " << slvstat.iteration << " ITERATIONS IN " << slvstat.cpu_elapsed << "s" << endl;
282
283 if(slvstat.iteration_limit_exceeded){
284 error_reporter(ASC_USER_ERROR,NULL,0,"Exceeded interation limit");
285 }
286
287 if(slvstat.converged){
288 error_reporter(ASC_USER_SUCCESS,NULL,0,"Solver converged: %d iterations, %3.2e s"
289 ,slvstat.iteration,slvstat.cpu_elapsed);
290 }else{
291 error_reporter(ASC_USER_ERROR,NULL,0,"Solver not converged after %d iteratoins.",slvstat.iteration);
292 }
293
294
295 //slv_print_output(stderr,solver);
296
297 /*
298 if(s==NULL){
299 throw runtime_error("System not yet built");
300 }
301 cerr << "PRESOLVING SYSTEM..." << endl;
302 slv_presolve(s);
303 cerr << "... DONE PRESOLVING" << endl;
304 */
305 }
306
307 void
308 Simulation::write(){
309 simroot.write();
310 }
311
312 //------------------------------------------
313 // ASSIGNING SOLVER TO SIMULATION
314
315 void
316 Simulation::setSolver(Solver &solver){
317 cerr << "SETTING SOLVER ON SIMULATION TO " << solver.getName() << endl;
318
319 if(!sys)throw runtime_error("Can't solve: Simulation system has not been built yet.");
320 // Update the solver object because sometimes an alternative solver can be returned, apparently.
321
322 int selected = slv_select_solver(sys, solver.getIndex());
323 cerr << "Simulation::setSolver: slv_select_solver returned " << selected << endl;
324
325 if(selected<0){
326 error_reporter(ASC_PROG_ERROR,NULL,0,"Failed to select solver");
327 throw runtime_error("Failed to select solver");
328 }
329
330 if(selected!=solver.getIndex()){
331 solver = Solver(slv_solver_name(selected));
332 error_reporter(ASC_PROG_NOTE,NULL,0,"Substitute solver '%s' (index %d) selected.\n", solver.getName().c_str(), selected);
333 }
334
335 if( slv_eligible_solver(sys) <= 0){
336 error_reporter(ASC_PROG_ERROR,NULL,0,"Inelegible solver '%s'", solver.getName().c_str() );
337 throw runtime_error("Inelegible solver");
338 }
339 }
340
341 const Solver
342 Simulation::getSolver() const{
343 int index = slv_get_selected_solver(sys);
344 cerr << "Simulation::getSolver: index = " << index << endl;
345 if(index<0)throw runtime_error("No solver selected");
346
347 return Solver(slv_solver_name(index));
348 }
349
350
351

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