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

Annotation of /trunk/pygtk/simulation.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

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