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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 254 - (show annotations) (download) (as text)
Thu Feb 2 06:27:17 2006 UTC (18 years, 4 months ago) by johnpye
File MIME type: text/x-c++src
File size: 4215 byte(s)
Added reporting of unconverged variables to the console
1 #include "incidencematrix.h"
2
3 #include <stdexcept>
4 #include <iostream>
5 using namespace std;
6
7 #include "variable.h"
8 #include "relation.h"
9
10 extern "C"{
11 #include <utilities/ascConfig.h>
12 #include <compiler/instance_enum.h>
13
14 #include <solver/var.h>
15 #include <solver/rel.h>
16 #include <solver/discrete.h>
17 #include <solver/conditional.h>
18 #include <solver/logrel.h>
19 #include <solver/bnd.h>
20 #include <solver/mtx.h>
21 #include <solver/linsol.h>
22 #include <solver/linsolqr.h>
23 #include <solver/slv_common.h>
24 #include <solver/slv_types.h>
25 #include <solver/slv_client.h>
26 }
27
28 IncidencePoint::IncidencePoint(const int&row, const int &col, const IncidencePointType &type) : row(row), col(col), type(type){
29 // constructor, IncidencePoint
30 }
31
32 IncidencePoint::IncidencePoint(const IncidencePoint &old) : row(old.row), col(old.col), type(old.type){
33 // copy ctor
34 }
35
36 IncidencePoint::IncidencePoint() : row(-1), col(-1), type(IM_NULL){
37 // default ctor... don't use. need this to keep swig happy for some strange reason.
38 }
39
40 IncidenceMatrix::IncidenceMatrix(Simulation &sim) : sim(sim){
41 // constructor
42 is_built = FALSE;
43 }
44
45 IncidenceMatrix::~IncidenceMatrix(){
46 if(is_built){
47 free_incidence_data(&i);
48 }
49 }
50
51 void
52 IncidenceMatrix::buildPlotData(){
53 int c=-1;
54
55 cerr << "BUILDPLOTDATA" << endl;
56
57 slv_system_t sys = sim.getSystem();
58
59 cerr << "GOT SYSTEM DATA" << endl;
60
61 if(build_incidence_data(sys,&i)) {
62 cerr << "FAILED TO BUILD INCIDENCE DATA" << endl;
63 free_incidence_data(&i);
64 throw runtime_error("IncidenceMatrix::buildPlotData error calculating grid");
65 return;
66 }
67
68 for (int r=0; r < i.nprow; r++) {
69 struct rel_relation *rel = i.rlist[i.pr2e[r]];
70 const struct var_variable **vp = rel_incidence_list(rel);
71
72 if(rel_active(rel)){
73 int nvars = rel_n_incidences(rel);
74 if(rel_included(rel)){
75 for(int v=0; v < nvars; v++ ) {
76 if(var_flags(vp[v]) & VAR_SVAR) {
77 int vndx = var_sindex(vp[v]);
78 c = i.v2pc[vndx];
79 if (i.vfixed[vndx]) {
80 data.push_back(IncidencePoint(r,c,IM_ACTIVE_FIXED));
81 }else{
82 data.push_back(IncidencePoint(r,c,IM_ACTIVE_FREE));
83 }
84 }
85 }
86 }else{ /* hollow squares */
87 for(int v=0; v < nvars; v++ ) {
88 if (var_flags(vp[v]) & VAR_SVAR) {
89 int vndx = var_sindex(vp[v]);
90 c = i.v2pc[vndx];
91 if (i.vfixed[vndx]) {
92 data.push_back(IncidencePoint(r,c,IM_DORMANT_FIXED));
93 } else {
94 data.push_back(IncidencePoint(r,c,IM_DORMANT_FREE));
95 }
96 }
97 }
98 }
99 }
100 }
101
102 is_built = TRUE;
103 }
104
105 const int &
106 IncidenceMatrix::getNumRows() const{
107 return i.nprow;
108 }
109
110 const int &
111 IncidenceMatrix::getNumCols() const{
112 return i.npcol;
113 }
114
115 const vector<IncidencePoint> &
116 IncidenceMatrix::getIncidenceData(){
117 cerr << "GET INCIDENCE DATA" << endl;
118 if(!is_built){
119 buildPlotData();
120 }
121 return data;
122 }
123
124 const Variable
125 IncidenceMatrix::getVariable(const int &col) const{
126 if(!is_built)throw runtime_error("Not built");
127 if(col < 0 || col >= getNumCols())throw range_error("Column out of range");
128 int vindex = i.pc2v[col];
129 struct var_variable *var = i.vlist[vindex];
130
131 return Variable(&sim, var);
132 }
133
134 const Relation
135 IncidenceMatrix::getRelation(const int &row) const{
136 if(!is_built)throw runtime_error("Not built");
137 if(row < 0 || row >= getNumRows())throw range_error("Row out of range");
138 int rindex = i.pr2e[row];
139 struct rel_relation *rel = i.rlist[rindex];
140 return Relation(&sim, rel);
141 }
142
143 const int
144 IncidenceMatrix::getBlockRow(const int &row) const{
145 if(!is_built)throw runtime_error("Not built");
146 if(row < 0 || row >= getNumRows())throw range_error("Row out of range");
147 const mtx_block_t *bb = slv_get_solvers_blocks(sim.getSystem());
148 for(int i=0; i < bb->nblocks; ++i){
149 if(row >= bb->block[i].row.low && row <= bb->block[i].row.high){
150 return i;
151 }
152 }
153 return -1;
154 }
155
156 const vector<Variable>
157 IncidenceMatrix::getBlockVars(const int &block){
158 if(!is_built){
159 buildPlotData();
160 }
161 const mtx_block_t *bb = slv_get_solvers_blocks(sim.getSystem());
162 if(block < 0 || block >= bb->nblocks)throw range_error("Block out of range");
163 int low = bb->block[block].col.low;
164 int high = bb->block[block].row.high;
165 vector<Variable> v;
166 for(int j=low; j<=high; ++j){
167 v.push_back(getVariable(j));
168 }
169 return v;
170 }
171
172

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