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

Annotation of /trunk/pygtk/simulation.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 746 - (hide annotations) (download) (as text)
Fri Jul 7 11:01:26 2006 UTC (13 years, 7 months ago) by johnpye
File MIME type: text/x-c++src
File size: 19288 byte(s)
Fixed small int/unsigned warnings.
Switched to preferring 'numarray' over 'numpy' (perhaps temporarily)
1 johnpye 669 /* 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 johnpye 132 #include <iostream>
20     #include <iomanip>
21     #include <stdexcept>
22     #include <sstream>
23     using namespace std;
24    
25 johnpye 506 #include "config.h"
26    
27 johnpye 132 extern "C"{
28     #include <utilities/ascConfig.h>
29 johnpye 506 #include <utilities/error.h>
30 johnpye 132 #include <utilities/ascSignal.h>
31     #include <utilities/ascMalloc.h>
32     #include <general/dstring.h>
33 johnpye 196 #include <general/tm_time.h>
34 johnpye 132 #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 johnpye 328 #include <solver/slv_server.h>
69 johnpye 132 }
70    
71     #include "simulation.h"
72     #include "solver.h"
73 johnpye 208 #include "solverparameters.h"
74 johnpye 132 #include "name.h"
75 johnpye 233 #include "incidencematrix.h"
76 johnpye 237 #include "variable.h"
77 johnpye 307 #include "solverstatus.h"
78 johnpye 310 #include "solverreporter.h"
79 johnpye 132
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 johnpye 153 is_built = false;
87 johnpye 132 // Create an Instance object for the 'simulation root' (we'll call
88 johnpye 190 // it the 'simulation model') and it can be fetched using 'getModel()'
89 johnpye 132 // any time later.
90     //simroot = Instanc(GetSimulationRoot(i),name);
91     }
92    
93 johnpye 164 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 johnpye 190 bin_rm = old.bin_rm;
101 johnpye 164 }
102    
103     Simulation::~Simulation(){
104     //CONSOLE_DEBUG("Deleting simulation %s", getName().toString());
105     }
106    
107 johnpye 132 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 johnpye 164 if(!sys){
120 johnpye 132 throw runtime_error("System not yet built");
121     }
122     slvDOF_status(sys, &status, &dof);
123     switch(status){
124 johnpye 190 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 johnpye 132 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 johnpye 295 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     void
157     Simulation::checkStructuralSingularity() const{
158     cerr << "CHECKING STRUCTURAL SINGULARITY..." << endl;
159    
160     int *vil;
161     int *ril;
162     int *fil;
163    
164     int res = slvDOF_structsing(sys, mtx_FIRST, &vil, &ril, &fil);
165     struct var_variable **varlist = slv_get_solvers_var_list(sys);
166     struct rel_relation **rellist = slv_get_solvers_rel_list(sys);
167    
168     if(res==0){
169     cerr << "UNABLE TO DETERMINE SINGULARITY LISTS" << endl;
170     return;
171     }else if(res==1){
172     ERROR_REPORTER_NOLINE(ASC_USER_ERROR,"Structurally singular. Check the listing on the console.");
173     cerr << "STRUCTURALLY SINGULAR: The found singularity involves these relations:" << endl;
174     for(int i=0; ril[i]!=-1; ++i){
175     Instanc i1((struct Instance *)rel_instance(rellist[ril[i]]));
176     cerr << " " << getInstanceName(i1) << endl;
177     }
178    
179     cerr << "STRUCTURALLY SINGULAR: ... and these variables:" << endl;
180     for(int i=0; vil[i]!=-1; ++i){
181     Instanc i1((struct Instance *)var_instance(varlist[vil[i]]));
182     cerr << " " << getInstanceName(i1) << endl;
183     }
184    
185     cerr << "STRUCTURALLY SINGULAR: ... and may be mitigated by freeing these variables:" << endl;
186     for(int i=0; fil[i]!=-1; ++i){
187     Instanc i1((struct Instance *)var_instance(varlist[fil[i]]));
188     cerr << " " << getInstanceName(i1) << endl;
189     }
190     }else{
191     throw runtime_error("Invalid return from slvDOF_structsing.");
192     }
193     ascfree(vil);
194     ascfree(ril);
195 johnpye 502 ascfree(fil);
196 johnpye 295 }
197    
198     void
199 johnpye 132 Simulation::run(const Method &method){
200     cerr << "RUNNING PROCEDURE " << method.getName() << endl;
201     Nam name = Nam(method.getSym());
202 johnpye 154 //cerr << "CREATED NAME '" << name.getName() << "'" << endl;
203 johnpye 132 Proc_enum pe;
204     pe = Initialize(
205 johnpye 164 &*(getModel().getInternalType()) ,name.getInternalType(), "__not_named__"
206 johnpye 132 ,ASCERR
207     ,0, NULL, NULL
208     );
209    
210     if(pe == Proc_all_ok){
211 johnpye 190 ERROR_REPORTER_NOLINE(ASC_PROG_NOTE,"Method '%s' was run (check above for errors)\n",method.getName());
212 johnpye 132 //cerr << "METHOD " << method.getName() << " COMPLETED OK" << endl;
213     }else{
214     stringstream ss;
215     ss << "Simulation::run: Method '" << method.getName() << "' returned error: ";
216     switch(pe){
217     case Proc_CallOK: ss << "Call OK"; break;
218     case Proc_CallError: ss << "Error occurred in call"; break;
219     case Proc_CallReturn: ss << "Request that caller return (OK)"; break;
220     case Proc_CallBreak: ss << "Break out of enclosing loop"; break;
221     case Proc_CallContinue: ss << "Skip to next iteration"; break;
222    
223     case Proc_break: ss << "Break"; break;
224     case Proc_continue: ss << "Continue"; break;
225     case Proc_fallthru: ss << "Fall-through"; break;
226     case Proc_return: ss << "Return"; break;
227     case Proc_stop: ss << "Stop"; break;
228     case Proc_stack_exceeded: ss << "Stack exceeded"; break;
229     case Proc_stack_exceeded_this_frame: ss << "Stack exceeded this frame"; break;
230     case Proc_case_matched: ss << "Case matched"; break;
231     case Proc_case_unmatched: ss << "Case unmatched"; break;
232    
233     case Proc_case_undefined_value: ss << "Undefined value in case"; break;
234     case Proc_case_boolean_mismatch: ss << "Boolean mismatch in case"; break;
235     case Proc_case_integer_mismatch: ss << "Integer mismatch in case"; break;
236     case Proc_case_symbol_mismatch: ss << "Symbol mismatch in case"; break;
237     case Proc_case_wrong_index: ss << "Wrong index in case"; break;
238     case Proc_case_wrong_value: ss << "Wrong value in case"; break;
239     case Proc_case_extra_values: ss << "Extra values in case"; break;
240     case Proc_bad_statement: ss << "Bad statement"; break;
241     case Proc_bad_name: ss << "Bad name"; break;
242     case Proc_for_duplicate_index: ss << "Duplicate index"; break;
243     case Proc_for_set_err: ss << "For set error"; break;
244     case Proc_for_not_set: ss << "For not set"; break;
245     case Proc_illegal_name_use: ss << "Illegal name use"; break;
246     case Proc_name_not_found: ss << "Name not found"; break;
247     case Proc_instance_not_found: ss << "Instance not found"; break;
248     case Proc_type_not_found: ss << "Type not found"; break;
249     case Proc_illegal_type_use: ss << "Illegal use"; break;
250     case Proc_proc_not_found: ss << "Method not found"; break;
251     case Proc_if_expr_error_typeconflict: ss << "Type conflict in 'if' expression"; break;
252     case Proc_if_expr_error_nameunfound: ss << "Name not found in 'if' expression"; break;
253     case Proc_if_expr_error_incorrectname: ss << "Incorrect name in 'if' expression"; break;
254     case Proc_if_expr_error_undefinedvalue: ss << "Undefined value in 'if' expression"; break;
255     case Proc_if_expr_error_dimensionconflict: ss << "Dimension conflict in 'if' expression"; break;
256     case Proc_if_expr_error_emptychoice: ss << "Empty choice in 'if' expression"; break;
257     case Proc_if_expr_error_emptyintersection: ss << "Empty intersection in 'if' expression"; break;
258     case Proc_if_expr_error_confused: ss << "Confused in 'if' expression"; break;
259     case Proc_if_real_expr: ss << "Real-valued result in 'if' expression"; break;
260     case Proc_if_integer_expr: ss << "Integeter-valued result in 'if' expression"; break;
261     case Proc_if_symbol_expr: ss << "Symbol-valued result in 'if' expression"; break;
262     case Proc_if_set_expr: ss << "Set-valued result in 'if' expression"; break;
263     case Proc_if_not_logical: ss << "If expression is not logical"; break;
264     case Proc_user_interrupt: ss << "User interrupt"; break;
265     case Proc_infinite_loop: ss << "Infinite loop"; break;
266     case Proc_declarative_constant_assignment: ss << "Declarative constant assignment"; break;
267     case Proc_nonsense_assignment: ss << "Nonsense assginment (bogus)"; break;
268     case Proc_nonconsistent_assignment: ss << "Inconsistent assignment"; break;
269     case Proc_nonatom_assignment: ss << "Non-atom assignment"; break;
270     case Proc_nonboolean_assignment: ss << "Non-boolean assignment"; break;
271     case Proc_noninteger_assignment: ss << "Non-integer assignment"; break;
272     case Proc_nonreal_assignment: ss << "Non-real assignment"; break;
273     case Proc_nonsymbol_assignment: ss << "Non-symbol assignment"; break;
274     case Proc_lhs_error: ss << "Left-hand-side error"; break;
275     case Proc_rhs_error: ss << "Right-hand-side error"; break;
276     case Proc_unknown_error: ss << "Unknown error"; break;
277     default:
278     ss << "Invalid error code";
279     }
280    
281    
282     ss << " (" << int(pe) << ")";
283     throw runtime_error(ss.str());
284     }
285     }
286    
287     const bool
288     Simulation::check(){
289     cerr << "CHECKING SIMULATION" << endl;
290     Instance *i1 = getModel().getInternalType();
291 johnpye 164 CheckInstance(stderr, &*i1);
292 johnpye 132 cerr << "...DONE CHECKING" << endl;
293 johnpye 295 this->checkConsistency();
294     this->checkStructuralSingularity();
295 johnpye 502 return true;
296 johnpye 190 }
297 johnpye 132
298     void
299     Simulation::build(){
300     cerr << "BUILDING SIMULATION..." << endl;
301 johnpye 164 Instance *i1 = getModel().getInternalType();
302     sys = system_build(&*i1);
303     if(!sys){
304 johnpye 132 throw runtime_error("Unable to build system");
305     }
306 johnpye 153 is_built = true;
307 johnpye 132 cerr << "...DONE BUILDING" << endl;
308     }
309    
310     vector<Variable>
311     Simulation::getFixableVariables(){
312     cerr << "GETTING FIXABLE VARIABLES..." << endl;
313     vector<Variable> vars;
314    
315 johnpye 164 if(!sys){
316 johnpye 132 throw runtime_error("Simulation system not yet built");
317     }
318    
319 johnpye 316 int32 *vip; /** TODO ensure 32 bit integers are used */
320 johnpye 132
321     // Get IDs of elegible variables in array at vip...
322 johnpye 316 if(!slvDOF_eligible(sys,&vip)){
323 johnpye 190 ERROR_REPORTER_NOLINE(ASC_USER_NOTE,"No fixable variables found.");
324 johnpye 132 }else{
325     //cerr << "FIXABLE VARS FOUND" << endl;
326     struct var_variable **vp = slv_get_solvers_var_list(sys);
327    
328     /*struct var_variable *first_var = vp[0];
329     char *first_var_name = var_make_name(sys,first_var);
330     cerr << "FIRST SYS VAR IS NAMED " << var_make_name(s,first_var) << endl;
331     ascfree(first_var_name);*/
332    
333     if(vp==NULL){
334     throw runtime_error("Simulation variable list is null");
335     }
336 johnpye 190
337 johnpye 132 // iterate through this list until we find a -1:
338     int i=0;
339 johnpye 316 int var_index = vip[i];
340 johnpye 132 while(var_index >= 0){
341     //cerr << "FOUND VARIABLE var_index = " << var_index << endl;
342     struct var_variable *var = vp[var_index];
343     //cerr << "VARIABLE " << var_index << " IS ELIGIBLE" << endl;
344 johnpye 328
345     //char *var_name = var_make_name(sys,var);
346 johnpye 132 //cerr << "ELIGIBLE VAR: " << var_name << endl;
347 johnpye 328 //ascfree(var_name);
348    
349 johnpye 237 vars.push_back( Variable(this, var) );
350 johnpye 132 ++i;
351 johnpye 316 var_index = vip[i];
352 johnpye 132 }
353 johnpye 190 ERROR_REPORTER_NOLINE(ASC_USER_NOTE,"Found %d fixable variables.",i);
354 johnpye 132 //cerr << "END ELEGIBLE VARS LIST" << endl;
355 johnpye 316 ascfree(vip);
356 johnpye 132 //cerr << "FREED VIP LIST" << endl;
357     }
358    
359     //cerr << "FINISHED WITH FINDING ELEGIBLE VARIABLES" << endl;
360     return vars;
361     }
362    
363 johnpye 328 vector<Variable>
364     Simulation::getVariablesNearBounds(const double &epsilon){
365     cerr << "GETTING VARIABLES NEAR BOUNDS..." << endl;
366     vector<Variable> vars;
367    
368     if(!sys){
369     throw runtime_error("Simulation system not yet built");
370     }
371    
372     int *vip;
373     if(slv_near_bounds(sys,epsilon,&vip)){
374     struct var_variable **vp = slv_get_solvers_var_list(sys);
375     struct var_variable *var;
376     cerr << "VARS FOUND NEAR BOUNDS" << endl;
377     int nlow = vip[0];
378     int nhigh = vip[1];
379     int lim1 = 2 + nlow;
380     for(int i=2; i<lim1; ++i){
381     var = vp[vip[i]];
382     char *var_name = var_make_name(sys,var);
383     cerr << "AT LOWER BOUND: " << var_name << endl;
384     ascfree(var_name);
385     vars.push_back(Variable(this,var));
386     };
387     int lim2 = lim1 + nhigh;
388     for(int i=lim1; i<lim2; ++i){
389     var = vp[vip[i]];
390     char *var_name = var_make_name(sys,var);
391     cerr << "AT UPPER BOUND: " << var_name << endl;
392     ascfree(var_name);
393     vars.push_back(Variable(this,var));
394     }
395     }
396     ascfree(vip);
397     return vars;
398     }
399 johnpye 502
400 johnpye 132 void
401 johnpye 310 Simulation::solve(Solver solver, SolverReporter &reporter){
402 johnpye 153 if(!is_built){
403     throw runtime_error("Simulation::solver: simulation is not yet built, can't start solving.");
404     }
405    
406 johnpye 132 cerr << "SIMULATION::SOLVE STARTING..." << endl;
407     enum inst_t k = getModel().getKind();
408     if(k!=MODEL_INST)throw runtime_error("Can't solve: not an instance of type MODEL_INST");
409 johnpye 190
410 johnpye 164 Instance *i1 = getInternalType();
411     int npend = NumberPendingInstances(&*i1);
412 johnpye 190 if(npend)throw runtime_error("Can't solve: There are still %d pending instances");
413 johnpye 132
414     if(!sys)throw runtime_error("Can't solve: Simulation system has not been built yet.");
415 johnpye 190
416 johnpye 132 cerr << "SIMULATION::SOLVE: SET SOLVER..." << endl;
417     setSolver(solver);
418 johnpye 190
419    
420 johnpye 207 cerr << "PRESOLVING SYSTEM...";
421 johnpye 132 slv_presolve(sys);
422 johnpye 207 cerr << "DONE" << endl;
423 johnpye 190
424 johnpye 132 cerr << "SOLVING SYSTEM..." << endl;
425 johnpye 196 // Add some stuff here for cleverer iteration....
426 johnpye 207 unsigned niter = 1000;
427 johnpye 714 //double updateinterval = 0.02;
428 johnpye 132
429 johnpye 196 double starttime = tm_cpu_time();
430 johnpye 714 //double lastupdate = starttime;
431 johnpye 307 SolverStatus status;
432 johnpye 714 //int solved_vars=0;
433 johnpye 196 bool stop=false;
434    
435 johnpye 307 status.getSimulationStatus(*this);
436 johnpye 314 reporter.report(&status);
437 johnpye 307
438 johnpye 746 for(unsigned iter = 1; iter <= niter && !stop; ++iter){
439 johnpye 285
440 johnpye 307 if(status.isReadyToSolve()){
441 johnpye 196 slv_iterate(sys);
442     }
443 johnpye 307
444     status.getSimulationStatus(*this);
445 johnpye 502
446 johnpye 317 if(reporter.report(&status)){
447     stop = true;
448     }
449 johnpye 196 }
450 johnpye 317
451 johnpye 230 double elapsed = tm_cpu_time() - starttime;
452 johnpye 196
453 johnpye 319
454 johnpye 307 activeblock = status.getCurrentBlockNum();
455 johnpye 285
456 johnpye 319 reporter.finalise(&status);
457    
458 johnpye 322 // Just a little bit of console output:
459    
460 johnpye 307 if(status.isOK()){
461 johnpye 260 cerr << "... SOLVED, STATUS OK" << endl;
462 johnpye 132 }else{
463 johnpye 321 cerr << "... SOLVER FAILED" << endl;
464 johnpye 132 }
465    
466 johnpye 307 cerr << "SOLVER PERFORMED " << status.getIterationNum() << " ITERATIONS IN " << elapsed << "s" << endl;
467 johnpye 132 }
468    
469     void
470     Simulation::write(){
471     simroot.write();
472     }
473    
474     //------------------------------------------
475     // ASSIGNING SOLVER TO SIMULATION
476 johnpye 190
477 johnpye 132 void
478     Simulation::setSolver(Solver &solver){
479 johnpye 151 cerr << "SETTING SOLVER ON SIMULATION TO " << solver.getName() << endl;
480    
481 johnpye 132 if(!sys)throw runtime_error("Can't solve: Simulation system has not been built yet.");
482     // Update the solver object because sometimes an alternative solver can be returned, apparently.
483    
484     int selected = slv_select_solver(sys, solver.getIndex());
485 johnpye 154 //cerr << "Simulation::setSolver: slv_select_solver returned " << selected << endl;
486 johnpye 132
487     if(selected<0){
488 johnpye 190 ERROR_REPORTER_NOLINE(ASC_PROG_ERROR,"Failed to select solver");
489 johnpye 132 throw runtime_error("Failed to select solver");
490     }
491    
492     if(selected!=solver.getIndex()){
493 johnpye 150 solver = Solver(slv_solver_name(selected));
494 johnpye 190 ERROR_REPORTER_NOLINE(ASC_PROG_NOTE,"Substitute solver '%s' (index %d) selected.\n", solver.getName().c_str(), selected);
495 johnpye 132 }
496 johnpye 190
497 johnpye 132 if( slv_eligible_solver(sys) <= 0){
498 johnpye 190 ERROR_REPORTER_NOLINE(ASC_PROG_ERROR,"Inelegible solver '%s'", solver.getName().c_str() );
499 johnpye 132 throw runtime_error("Inelegible solver");
500     }
501     }
502    
503     const Solver
504     Simulation::getSolver() const{
505     int index = slv_get_selected_solver(sys);
506 johnpye 154 //cerr << "Simulation::getSolver: index = " << index << endl;
507 johnpye 132 if(index<0)throw runtime_error("No solver selected");
508    
509 johnpye 150 return Solver(slv_solver_name(index));
510 johnpye 132 }
511    
512    
513 johnpye 208 /**
514     Get solver parameters struct wrapped up as a SolverParameters class.
515     */
516     SolverParameters
517     Simulation::getSolverParameters() const{
518 johnpye 225 if(!sys)throw runtime_error("Can't getSolverParameters: Simulation system has not been built yet.");
519 johnpye 132
520 johnpye 208 slv_parameters_t p;
521     slv_get_parameters(sys,&p);
522     return SolverParameters(p);
523     }
524    
525 johnpye 225 /**
526     Update the solver parameters by passing a new set back
527     */
528     void
529     Simulation::setSolverParameters(SolverParameters &P){
530     if(!sys)throw runtime_error("Can't set solver parameters: simulation has not been built yet.");
531     slv_set_parameters(sys, &(P.getInternalType()));
532     }
533    
534 johnpye 233 slv_system_structure *
535     Simulation::getSystem(){
536     if(!sys)throw runtime_error("Can't getSystem: simulation not yet built");
537     return sys;
538     }
539    
540     IncidenceMatrix
541     Simulation::getIncidenceMatrix(){
542     return IncidenceMatrix(*this);
543     }
544 johnpye 252
545 johnpye 502 const string
546 johnpye 252 Simulation::getInstanceName(const Instanc &i) const{
547     char *n;
548     n = WriteInstanceNameString(i.getInternalType(),simroot.getInternalType());
549     string s(n);
550     ascfree(n);
551     return s;
552     }
553 johnpye 317
554     const int
555     Simulation::getNumVars(){
556     return slv_get_num_solvers_vars(getSystem());
557     }
558    
559 johnpye 255 void
560     Simulation::processVarStatus(){
561    
562     // this is a cheap function call:
563     const mtx_block_t *bb = slv_get_solvers_blocks(getSystem());
564 johnpye 297
565 johnpye 255 var_variable **vlist = slv_get_solvers_var_list(getSystem());
566     int nvars = slv_get_num_solvers_vars(getSystem());
567    
568     slv_status_t status;
569     slv_get_status(getSystem(), &status);
570    
571 johnpye 297 if(status.block.number_of == 0){
572     cerr << "Variable statuses can't be set: block structure not yet determined." << endl;
573     return;
574     }
575    
576 johnpye 255 int activeblock = status.block.current_block;
577     int low = bb->block[activeblock].col.low;
578     int high = bb->block[activeblock].col.high;
579 johnpye 258 bool allsolved = status.converged;
580 johnpye 255 for(int c=0; c < nvars; ++c){
581     var_variable *v = vlist[c];
582     Instanc i((Instance *)var_instance(v));
583     VarStatus s = ASCXX_VAR_STATUS_UNKNOWN;
584 johnpye 258 if(i.isFixed()){
585     s = ASCXX_VAR_FIXED;
586     }else if(var_incident(v) && var_active(v)){
587     if(allsolved || c < low){
588 johnpye 255 s = ASCXX_VAR_SOLVED;
589     }else if(c <= high){
590     s = ASCXX_VAR_ACTIVE;
591     }else{
592     s = ASCXX_VAR_UNSOLVED;
593     }
594     }
595     i.setVarStatus(s);
596     }
597     }
598 johnpye 285
599     const int
600     Simulation::getActiveBlock() const{
601     return activeblock;
602     }

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