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

Contents of /trunk/pygtk/simulation.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 772 - (show annotations) (download) (as text)
Fri Jul 14 06:03:47 2006 UTC (14 years, 3 months ago) by johnpye
File MIME type: text/x-c++src
File size: 18922 byte(s)
Improved the reporting of structural singularities in PyGTK interface.
1 /* ASCEND modelling environment
2 Copyright (C) 2006 Carnegie Mellon University
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18 */
19 #include <iostream>
20 #include <iomanip>
21 #include <stdexcept>
22 #include <sstream>
23 using namespace std;
24
25 #include "config.h"
26
27 extern "C"{
28 #include <utilities/ascConfig.h>
29 #include <utilities/error.h>
30 #include <utilities/ascSignal.h>
31 #include <utilities/ascMalloc.h>
32 #include <general/dstring.h>
33 #include <general/tm_time.h>
34 #include <compiler/instance_enum.h>
35 #include <compiler/fractions.h>
36 #include <compiler/compiler.h>
37 #include <compiler/dimen.h>
38 #include <compiler/symtab.h>
39 #include <compiler/instance_io.h>
40 #include <compiler/instantiate.h>
41 #include <compiler/bintoken.h>
42 #include <compiler/instance_enum.h>
43 #include <compiler/instquery.h>
44 #include <compiler/check.h>
45 #include <compiler/name.h>
46 #include <compiler/pending.h>
47
48 #include <utilities/readln.h>
49 #include <solver/mtx.h>
50 #include <solver/slv_types.h>
51 #include <solver/var.h>
52 #include <solver/rel.h>
53 #include <solver/discrete.h>
54 #include <solver/conditional.h>
55 #include <solver/logrel.h>
56 #include <solver/bnd.h>
57 #include <solver/calc.h>
58 #include <solver/relman.h>
59 #include <solver/slv_common.h>
60 #include <solver/linsol.h>
61 #include <solver/linsolqr.h>
62 #include <solver/slv_client.h>
63 #include <solver/system.h>
64 #include <solver/slv_interface.h>
65 #include <solver/slvDOF.h>
66 #include <solver/slv3.h>
67 #include <solver/slv_stdcalls.h>
68 #include <solver/slv_server.h>
69 }
70
71 #include "simulation.h"
72 #include "solver.h"
73 #include "solverparameters.h"
74 #include "name.h"
75 #include "incidencematrix.h"
76 #include "variable.h"
77 #include "solverstatus.h"
78 #include "solverreporter.h"
79
80 /**
81 Create an instance of a type (call compiler etc)
82
83 @TODO fix mutex on compile command filenames
84 */
85 Simulation::Simulation(Instance *i, const SymChar &name) : Instanc(i, name), simroot(GetSimulationRoot(i),SymChar("simroot")){
86 is_built = false;
87 // Create an Instance object for the 'simulation root' (we'll call
88 // it the 'simulation model') and it can be fetched using 'getModel()'
89 // any time later.
90 //simroot = Instanc(GetSimulationRoot(i),name);
91 }
92
93 Simulation::Simulation(const Simulation &old) : Instanc(old), simroot(old.simroot){
94 is_built = old.is_built;
95 sys = old.sys;
96 bin_srcname = old.bin_srcname;
97 bin_objname = old.bin_objname;
98 bin_libname = old.bin_libname;
99 bin_cmd = old.bin_cmd;
100 bin_rm = old.bin_rm;
101 }
102
103 Simulation::~Simulation(){
104 //CONSOLE_DEBUG("Deleting simulation %s", getName().toString());
105 }
106
107 Instanc &
108 Simulation::getModel(){
109 if(!simroot.getInternalType()){
110 throw runtime_error("Simulation::getModel: simroot.getInternalType()is NULL");
111 }
112 return simroot;
113 }
114
115 void
116 Simulation::checkDoF() const{
117 cerr << "CHECKING DOF..." << endl;
118 int dof, status;
119 if(!sys){
120 throw runtime_error("System not yet built");
121 }
122 slvDOF_status(sys, &status, &dof);
123 switch(status){
124 case 1: ERROR_REPORTER_NOLINE(ASC_USER_ERROR,"Underspecified; %d degrees of freedom",dof); break;
125 case 2: ERROR_REPORTER_NOLINE(ASC_USER_NOTE,"Square"); break;
126 case 3: ERROR_REPORTER_NOLINE(ASC_USER_ERROR,"Structurally singular"); break;
127 case 4: ERROR_REPORTER_NOLINE(ASC_USER_ERROR,"Overspecified"); break;
128 case 5:
129 throw runtime_error("Unable to resolve degrees of freedom"); break;
130 default:
131 throw runtime_error("Invalid return status from slvDOF_status");
132 }
133 }
134
135 void
136 Simulation::checkConsistency() const{
137 cerr << "CHECKING CONSISTENCY..." << endl;
138 int *fixedarrayptr;
139
140 int res = consistency_analysis(sys, &fixedarrayptr);
141 struct var_variable **vp = slv_get_master_var_list(sys);
142
143 if(res==1){
144 cerr << "STRUCTURALLY CONSISTENT" << endl;
145 return;
146 }else{
147 ERROR_REPORTER_NOLINE(ASC_USER_ERROR,"Structurally inconsistent. Free the variables listed on the console\nin order to make system consistent.");
148 cerr << "INCONSISTENT: Free these vars:" << endl;
149 for(int i=0; fixedarrayptr[i]!=-1; ++i){
150 Instanc i1((struct Instance *)var_instance(vp[fixedarrayptr[i]]));
151 cerr << " " << getInstanceName(i1) << endl;
152 }
153 }
154 }
155
156 /** Returns TRUE if all is OK (not singular) */
157 bool
158 Simulation::checkStructuralSingularity(){
159 cerr << "CHECKING STRUCTURAL SINGULARITY..." << endl;
160
161 int *vil;
162 int *ril;
163 int *fil;
164
165 int res = slvDOF_structsing(sys, mtx_FIRST, &vil, &ril, &fil);
166 struct var_variable **varlist = slv_get_solvers_var_list(sys);
167 struct rel_relation **rellist = slv_get_solvers_rel_list(sys);
168
169 if(this->sing){
170 delete this->sing;
171 this->sing = NULL;
172 }
173
174 if(res==1){
175 CONSOLE_DEBUG("processing singularity data...");
176 sing = new SingularityInfo;
177
178 // 'singular relations'
179 for(int i=0; ril[i]!=-1; ++i){
180 sing->rels.push_back( Relation( this, rellist[ril[i]] ) );
181 }
182
183 // 'singular variables'
184 for(int i=0; vil[i]!=-1; ++i){
185 sing->vars.push_back( Variable( this, varlist[vil[i]] ) );
186 }
187
188 // 'free these variables'
189 for(int i=0; fil[i]!=-1; ++i){
190 sing->freeablevars.push_back( Variable( this, varlist[fil[i]] ) );
191 }
192
193 // we're done with those lists now
194 ASC_FREE(vil);
195 ASC_FREE(ril);
196 ASC_FREE(fil);
197
198 if(sing->isSingular()){
199 CONSOLE_DEBUG("singularity found");
200 this->sing = sing;
201 return FALSE;
202 }
203 CONSOLE_DEBUG("no singularity");
204 delete sing;
205 return TRUE;
206 }else{
207 if(res==0){
208 throw runtime_error("Unable to determine singularity lists");
209 }else{
210 throw runtime_error("Invalid return from slvDOF_structsing.");
211 }
212 }
213 }
214
215 const SingularityInfo &
216 Simulation::getSingularityInfo() const{
217 if(sing==NULL){
218 throw runtime_error("No singularity info present");
219 }
220 return *sing;
221 }
222
223 void
224 Simulation::run(const Method &method){
225 cerr << "RUNNING PROCEDURE " << method.getName() << endl;
226 Nam name = Nam(method.getSym());
227 //cerr << "CREATED NAME '" << name.getName() << "'" << endl;
228 Proc_enum pe;
229 pe = Initialize(
230 &*(getModel().getInternalType()) ,name.getInternalType(), "__not_named__"
231 ,ASCERR
232 ,0, NULL, NULL
233 );
234
235 if(pe == Proc_all_ok){
236 ERROR_REPORTER_NOLINE(ASC_PROG_NOTE,"Method '%s' was run (check above for errors)\n",method.getName());
237 //cerr << "METHOD " << method.getName() << " COMPLETED OK" << endl;
238 }else{
239 stringstream ss;
240 ss << "Simulation::run: Method '" << method.getName() << "' returned error: ";
241 switch(pe){
242 case Proc_CallOK: ss << "Call OK"; break;
243 case Proc_CallError: ss << "Error occurred in call"; break;
244 case Proc_CallReturn: ss << "Request that caller return (OK)"; break;
245 case Proc_CallBreak: ss << "Break out of enclosing loop"; break;
246 case Proc_CallContinue: ss << "Skip to next iteration"; break;
247
248 case Proc_break: ss << "Break"; break;
249 case Proc_continue: ss << "Continue"; break;
250 case Proc_fallthru: ss << "Fall-through"; break;
251 case Proc_return: ss << "Return"; break;
252 case Proc_stop: ss << "Stop"; break;
253 case Proc_stack_exceeded: ss << "Stack exceeded"; break;
254 case Proc_stack_exceeded_this_frame: ss << "Stack exceeded this frame"; break;
255 case Proc_case_matched: ss << "Case matched"; break;
256 case Proc_case_unmatched: ss << "Case unmatched"; break;
257
258 case Proc_case_undefined_value: ss << "Undefined value in case"; break;
259 case Proc_case_boolean_mismatch: ss << "Boolean mismatch in case"; break;
260 case Proc_case_integer_mismatch: ss << "Integer mismatch in case"; break;
261 case Proc_case_symbol_mismatch: ss << "Symbol mismatch in case"; break;
262 case Proc_case_wrong_index: ss << "Wrong index in case"; break;
263 case Proc_case_wrong_value: ss << "Wrong value in case"; break;
264 case Proc_case_extra_values: ss << "Extra values in case"; break;
265 case Proc_bad_statement: ss << "Bad statement"; break;
266 case Proc_bad_name: ss << "Bad name"; break;
267 case Proc_for_duplicate_index: ss << "Duplicate index"; break;
268 case Proc_for_set_err: ss << "For set error"; break;
269 case Proc_for_not_set: ss << "For not set"; break;
270 case Proc_illegal_name_use: ss << "Illegal name use"; break;
271 case Proc_name_not_found: ss << "Name not found"; break;
272 case Proc_instance_not_found: ss << "Instance not found"; break;
273 case Proc_type_not_found: ss << "Type not found"; break;
274 case Proc_illegal_type_use: ss << "Illegal use"; break;
275 case Proc_proc_not_found: ss << "Method not found"; break;
276 case Proc_if_expr_error_typeconflict: ss << "Type conflict in 'if' expression"; break;
277 case Proc_if_expr_error_nameunfound: ss << "Name not found in 'if' expression"; break;
278 case Proc_if_expr_error_incorrectname: ss << "Incorrect name in 'if' expression"; break;
279 case Proc_if_expr_error_undefinedvalue: ss << "Undefined value in 'if' expression"; break;
280 case Proc_if_expr_error_dimensionconflict: ss << "Dimension conflict in 'if' expression"; break;
281 case Proc_if_expr_error_emptychoice: ss << "Empty choice in 'if' expression"; break;
282 case Proc_if_expr_error_emptyintersection: ss << "Empty intersection in 'if' expression"; break;
283 case Proc_if_expr_error_confused: ss << "Confused in 'if' expression"; break;
284 case Proc_if_real_expr: ss << "Real-valued result in 'if' expression"; break;
285 case Proc_if_integer_expr: ss << "Integeter-valued result in 'if' expression"; break;
286 case Proc_if_symbol_expr: ss << "Symbol-valued result in 'if' expression"; break;
287 case Proc_if_set_expr: ss << "Set-valued result in 'if' expression"; break;
288 case Proc_if_not_logical: ss << "If expression is not logical"; break;
289 case Proc_user_interrupt: ss << "User interrupt"; break;
290 case Proc_infinite_loop: ss << "Infinite loop"; break;
291 case Proc_declarative_constant_assignment: ss << "Declarative constant assignment"; break;
292 case Proc_nonsense_assignment: ss << "Nonsense assginment (bogus)"; break;
293 case Proc_nonconsistent_assignment: ss << "Inconsistent assignment"; break;
294 case Proc_nonatom_assignment: ss << "Non-atom assignment"; break;
295 case Proc_nonboolean_assignment: ss << "Non-boolean assignment"; break;
296 case Proc_noninteger_assignment: ss << "Non-integer assignment"; break;
297 case Proc_nonreal_assignment: ss << "Non-real assignment"; break;
298 case Proc_nonsymbol_assignment: ss << "Non-symbol assignment"; break;
299 case Proc_lhs_error: ss << "Left-hand-side error"; break;
300 case Proc_rhs_error: ss << "Right-hand-side error"; break;
301 case Proc_unknown_error: ss << "Unknown error"; break;
302 default:
303 ss << "Invalid error code";
304 }
305
306
307 ss << " (" << int(pe) << ")";
308 throw runtime_error(ss.str());
309 }
310 }
311
312 /**
313 @return TRUE if all is OK
314 */
315 const bool
316 Simulation::check(){
317 cerr << "CHECKING SIMULATION" << endl;
318 Instance *i1 = getModel().getInternalType();
319 CheckInstance(stderr, &*i1);
320 cerr << "...DONE CHECKING" << endl;
321 this->checkConsistency();
322
323 return this->checkStructuralSingularity();
324 }
325
326 void
327 Simulation::build(){
328 cerr << "BUILDING SIMULATION..." << endl;
329 Instance *i1 = getModel().getInternalType();
330 sys = system_build(&*i1);
331 if(!sys){
332 throw runtime_error("Unable to build system");
333 }
334 is_built = true;
335 cerr << "...DONE BUILDING" << endl;
336 }
337
338 vector<Variable>
339 Simulation::getFixableVariables(){
340 cerr << "GETTING FIXABLE VARIABLES..." << endl;
341 vector<Variable> vars;
342
343 if(!sys){
344 throw runtime_error("Simulation system not yet built");
345 }
346
347 int32 *vip; /** TODO ensure 32 bit integers are used */
348
349 // Get IDs of elegible variables in array at vip...
350 if(!slvDOF_eligible(sys,&vip)){
351 ERROR_REPORTER_NOLINE(ASC_USER_NOTE,"No fixable variables found.");
352 }else{
353 struct var_variable **vp = slv_get_solvers_var_list(sys);
354
355 if(vp==NULL){
356 throw runtime_error("Simulation variable list is null");
357 }
358
359 // iterate through this list until we find a -1:
360 int i=0;
361 int var_index = vip[i];
362 while(var_index >= 0){
363 struct var_variable *var = vp[var_index];
364 vars.push_back( Variable(this, var) );
365 ++i;
366 var_index = vip[i];
367 }
368 ERROR_REPORTER_NOLINE(ASC_USER_NOTE,"Found %d fixable variables.",i);
369 ascfree(vip);
370 }
371
372 return vars;
373 }
374
375 vector<Variable>
376 Simulation::getVariablesNearBounds(const double &epsilon){
377 cerr << "GETTING VARIABLES NEAR BOUNDS..." << endl;
378 vector<Variable> vars;
379
380 if(!sys){
381 throw runtime_error("Simulation system not yet built");
382 }
383
384 int *vip;
385 if(slv_near_bounds(sys,epsilon,&vip)){
386 struct var_variable **vp = slv_get_solvers_var_list(sys);
387 struct var_variable *var;
388 cerr << "VARS FOUND NEAR BOUNDS" << endl;
389 int nlow = vip[0];
390 int nhigh = vip[1];
391 int lim1 = 2 + nlow;
392 for(int i=2; i<lim1; ++i){
393 var = vp[vip[i]];
394 char *var_name = var_make_name(sys,var);
395 cerr << "AT LOWER BOUND: " << var_name << endl;
396 ascfree(var_name);
397 vars.push_back(Variable(this,var));
398 };
399 int lim2 = lim1 + nhigh;
400 for(int i=lim1; i<lim2; ++i){
401 var = vp[vip[i]];
402 char *var_name = var_make_name(sys,var);
403 cerr << "AT UPPER BOUND: " << var_name << endl;
404 ascfree(var_name);
405 vars.push_back(Variable(this,var));
406 }
407 }
408 ascfree(vip);
409 return vars;
410 }
411
412 void
413 Simulation::solve(Solver solver, SolverReporter &reporter){
414 if(!is_built){
415 throw runtime_error("Simulation::solver: simulation is not yet built, can't start solving.");
416 }
417
418 cerr << "SIMULATION::SOLVE STARTING..." << endl;
419 enum inst_t k = getModel().getKind();
420 if(k!=MODEL_INST)throw runtime_error("Can't solve: not an instance of type MODEL_INST");
421
422 Instance *i1 = getInternalType();
423 int npend = NumberPendingInstances(&*i1);
424 if(npend)throw runtime_error("Can't solve: There are still %d pending instances");
425
426 if(!sys)throw runtime_error("Can't solve: Simulation system has not been built yet.");
427
428 cerr << "SIMULATION::SOLVE: SET SOLVER..." << endl;
429 setSolver(solver);
430
431
432 cerr << "PRESOLVING SYSTEM...";
433 slv_presolve(sys);
434 cerr << "DONE" << endl;
435
436 cerr << "SOLVING SYSTEM..." << endl;
437 // Add some stuff here for cleverer iteration....
438 unsigned niter = 1000;
439 //double updateinterval = 0.02;
440
441 double starttime = tm_cpu_time();
442 //double lastupdate = starttime;
443 SolverStatus status;
444 //int solved_vars=0;
445 bool stop=false;
446
447 status.getSimulationStatus(*this);
448 reporter.report(&status);
449
450 for(unsigned iter = 1; iter <= niter && !stop; ++iter){
451
452 if(status.isReadyToSolve()){
453 slv_iterate(sys);
454 }
455
456 status.getSimulationStatus(*this);
457
458 if(reporter.report(&status)){
459 stop = true;
460 }
461 }
462
463 double elapsed = tm_cpu_time() - starttime;
464
465
466 activeblock = status.getCurrentBlockNum();
467
468 reporter.finalise(&status);
469
470 // Just a little bit of console output:
471
472 if(status.isOK()){
473 cerr << "... SOLVED, STATUS OK" << endl;
474 }else{
475 cerr << "... SOLVER FAILED" << endl;
476 }
477
478 cerr << "SOLVER PERFORMED " << status.getIterationNum() << " ITERATIONS IN " << elapsed << "s" << endl;
479 }
480
481 void
482 Simulation::write(){
483 simroot.write();
484 }
485
486 //------------------------------------------
487 // ASSIGNING SOLVER TO SIMULATION
488
489 void
490 Simulation::setSolver(Solver &solver){
491 cerr << "SETTING SOLVER ON SIMULATION TO " << solver.getName() << endl;
492
493 if(!sys)throw runtime_error("Can't solve: Simulation system has not been built yet.");
494 // Update the solver object because sometimes an alternative solver can be returned, apparently.
495
496 int selected = slv_select_solver(sys, solver.getIndex());
497 //cerr << "Simulation::setSolver: slv_select_solver returned " << selected << endl;
498
499 if(selected<0){
500 ERROR_REPORTER_NOLINE(ASC_PROG_ERROR,"Failed to select solver");
501 throw runtime_error("Failed to select solver");
502 }
503
504 if(selected!=solver.getIndex()){
505 solver = Solver(slv_solver_name(selected));
506 ERROR_REPORTER_NOLINE(ASC_PROG_NOTE,"Substitute solver '%s' (index %d) selected.\n", solver.getName().c_str(), selected);
507 }
508
509 if( slv_eligible_solver(sys) <= 0){
510 ERROR_REPORTER_NOLINE(ASC_PROG_ERROR,"Inelegible solver '%s'", solver.getName().c_str() );
511 throw runtime_error("Inelegible solver");
512 }
513 }
514
515 const Solver
516 Simulation::getSolver() const{
517 int index = slv_get_selected_solver(sys);
518 //cerr << "Simulation::getSolver: index = " << index << endl;
519 if(index<0)throw runtime_error("No solver selected");
520
521 return Solver(slv_solver_name(index));
522 }
523
524
525 /**
526 Get solver parameters struct wrapped up as a SolverParameters class.
527 */
528 SolverParameters
529 Simulation::getSolverParameters() const{
530 if(!sys)throw runtime_error("Can't getSolverParameters: Simulation system has not been built yet.");
531
532 slv_parameters_t p;
533 slv_get_parameters(sys,&p);
534 return SolverParameters(p);
535 }
536
537 /**
538 Update the solver parameters by passing a new set back
539 */
540 void
541 Simulation::setSolverParameters(SolverParameters &P){
542 if(!sys)throw runtime_error("Can't set solver parameters: simulation has not been built yet.");
543 slv_set_parameters(sys, &(P.getInternalType()));
544 }
545
546 slv_system_structure *
547 Simulation::getSystem(){
548 if(!sys)throw runtime_error("Can't getSystem: simulation not yet built");
549 return sys;
550 }
551
552 IncidenceMatrix
553 Simulation::getIncidenceMatrix(){
554 return IncidenceMatrix(*this);
555 }
556
557 const string
558 Simulation::getInstanceName(const Instanc &i) const{
559 char *n;
560 n = WriteInstanceNameString(i.getInternalType(),simroot.getInternalType());
561 string s(n);
562 ascfree(n);
563 return s;
564 }
565
566 const int
567 Simulation::getNumVars(){
568 return slv_get_num_solvers_vars(getSystem());
569 }
570
571 void
572 Simulation::processVarStatus(){
573
574 // this is a cheap function call:
575 const mtx_block_t *bb = slv_get_solvers_blocks(getSystem());
576
577 var_variable **vlist = slv_get_solvers_var_list(getSystem());
578 int nvars = slv_get_num_solvers_vars(getSystem());
579
580 slv_status_t status;
581 slv_get_status(getSystem(), &status);
582
583 if(status.block.number_of == 0){
584 cerr << "Variable statuses can't be set: block structure not yet determined." << endl;
585 return;
586 }
587
588 int activeblock = status.block.current_block;
589 int low = bb->block[activeblock].col.low;
590 int high = bb->block[activeblock].col.high;
591 bool allsolved = status.converged;
592 for(int c=0; c < nvars; ++c){
593 var_variable *v = vlist[c];
594 Instanc i((Instance *)var_instance(v));
595 VarStatus s = ASCXX_VAR_STATUS_UNKNOWN;
596 if(i.isFixed()){
597 s = ASCXX_VAR_FIXED;
598 }else if(var_incident(v) && var_active(v)){
599 if(allsolved || c < low){
600 s = ASCXX_VAR_SOLVED;
601 }else if(c <= high){
602 s = ASCXX_VAR_ACTIVE;
603 }else{
604 s = ASCXX_VAR_UNSOLVED;
605 }
606 }
607 i.setVarStatus(s);
608 }
609 }
610
611 const int
612 Simulation::getActiveBlock() const{
613 return activeblock;
614 }
615
616 bool
617 SingularityInfo::isSingular() const{
618 if(vars.size()||rels.size()){
619 return true;
620 }
621 return false;
622 }

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