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

Contents of /trunk/pygtk/incidencematrix.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1678 - (show annotations) (download) (as text)
Wed Dec 19 00:29:48 2007 UTC (16 years, 7 months ago) by jpye
File MIME type: text/x-c++src
File size: 5192 byte(s)
Removed references to MFGRAPH, seems better to use GraphViz directly.
Added configuration test for GraphViz.
Added configuration test for strdup, fixed mmio to not create its own strdup if other form is available already.

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 <linear/mtx.h>
12 #include <system/slv_client.h>
13 }
14
15 IncidencePoint::IncidencePoint(const int&row, const int &col, const IncidencePointType &type) : row(row), col(col), type(type){
16 // constructor, IncidencePoint
17 }
18
19 IncidencePoint::IncidencePoint(const IncidencePoint &old) : row(old.row), col(old.col), type(old.type){
20 // copy ctor
21 }
22
23 IncidencePoint::IncidencePoint() : row(-1), col(-1), type(IM_NULL){
24 // default ctor... don't use. need this to keep swig happy for some strange reason.
25 }
26
27 IncidenceMatrix::IncidenceMatrix(Simulation &sim) : sim(sim){
28 // constructor
29 is_built = FALSE;
30 }
31
32 IncidenceMatrix::~IncidenceMatrix(){
33 if(is_built){
34 free_incidence_data(&i);
35 }
36 }
37
38 void
39 IncidenceMatrix::buildPlotData(){
40 int c=-1;
41
42 //cerr << "BUILDPLOTDATA" << endl;
43
44 slv_system_t sys = sim.getSystem();
45
46 //cerr << "GOT SYSTEM DATA" << endl;
47
48 if(build_incidence_data(sys,&i)) {
49 cerr << "FAILED TO BUILD INCIDENCE DATA" << endl;
50 free_incidence_data(&i);
51 throw runtime_error("IncidenceMatrix::buildPlotData error calculating grid");
52 return;
53 }
54
55 for (int r=0; r < i.nprow; r++) {
56 struct rel_relation *rel = i.rlist[i.pr2e[r]];
57 const struct var_variable **vp = rel_incidence_list(rel);
58
59 if(rel_active(rel)){
60 int nvars = rel_n_incidences(rel);
61 if(rel_included(rel)){
62 for(int v=0; v < nvars; v++ ) {
63 if(var_flags(vp[v]) & VAR_SVAR) {
64 int vndx = var_sindex(vp[v]);
65 c = i.v2pc[vndx];
66 if (i.vfixed[vndx]) {
67 data.push_back(IncidencePoint(r,c,IM_ACTIVE_FIXED));
68 }else{
69 data.push_back(IncidencePoint(r,c,IM_ACTIVE_FREE));
70 }
71 }
72 }
73 }else{ /* hollow squares */
74 for(int v=0; v < nvars; v++ ) {
75 if (var_flags(vp[v]) & VAR_SVAR) {
76 int vndx = var_sindex(vp[v]);
77 c = i.v2pc[vndx];
78 if (i.vfixed[vndx]) {
79 data.push_back(IncidencePoint(r,c,IM_DORMANT_FIXED));
80 } else {
81 data.push_back(IncidencePoint(r,c,IM_DORMANT_FREE));
82 }
83 }
84 }
85 }
86 }
87 }
88
89 is_built = TRUE;
90 }
91
92 const int &
93 IncidenceMatrix::getNumRows() const{
94 return i.nprow;
95 }
96
97 const int &
98 IncidenceMatrix::getNumCols() const{
99 return i.npcol;
100 }
101
102 const vector<IncidencePoint> &
103 IncidenceMatrix::getIncidenceData(){
104 cerr << "GET INCIDENCE DATA" << endl;
105 if(!is_built){
106 buildPlotData();
107 }
108 return data;
109 }
110
111 const Variable
112 IncidenceMatrix::getVariable(const int &col) const{
113 if(!is_built)throw runtime_error("Not built");
114 if(col < 0 || col >= getNumCols())throw range_error("Column out of range");
115 int vindex = i.pc2v[col];
116 struct var_variable *var = i.vlist[vindex];
117
118 return Variable(&sim, var);
119 }
120
121 const Relation
122 IncidenceMatrix::getRelation(const int &row) const{
123 if(!is_built)throw runtime_error("Not built");
124 if(row < 0 || row >= getNumRows())throw range_error("Row out of range");
125 int rindex = i.pr2e[row];
126 struct rel_relation *rel = i.rlist[rindex];
127 return Relation(&sim, rel);
128 }
129
130 const int
131 IncidenceMatrix::getBlockRow(const int &row) const{
132 if(!is_built)throw runtime_error("Not built");
133 if(row < 0 || row >= getNumRows())throw range_error("Row out of range");
134 const mtx_block_t *bb = slv_get_solvers_blocks(sim.getSystem());
135 for(int i=0; i < bb->nblocks; ++i){
136 if(row >= bb->block[i].row.low && row <= bb->block[i].row.high){
137 return i;
138 }
139 }
140 return -1;
141 }
142
143 /**
144 Returns location of specified block
145 @param block the block number
146 @return vector(ve row-low, col-low, row-high, col-high)
147 */
148 const vector<int>
149 IncidenceMatrix::getBlockLocation(const int &block) const{
150 if(!is_built)throw runtime_error("Not built");
151 const mtx_block_t *bb = slv_get_solvers_blocks(sim.getSystem());
152 if(block < 0 || block >= bb->nblocks){
153 throw range_error("Invalid block number");
154 }
155 vector<int> v;
156 v.push_back(bb->block[block].row.low);
157 v.push_back(bb->block[block].col.low);
158 v.push_back(bb->block[block].row.high);
159 v.push_back(bb->block[block].col.high);
160 return v;
161 }
162
163 const vector<Variable>
164 IncidenceMatrix::getBlockVars(const int &block){
165 if(!is_built){
166 buildPlotData();
167 }
168 vector<Variable> v;
169 const mtx_block_t *bb = slv_get_solvers_blocks(sim.getSystem());
170 if(block < 0 || block >= bb->nblocks){
171 ERROR_REPORTER_HERE(ASC_PROG_ERR,"Block out of range");
172 return v;
173 }
174 int low = bb->block[block].col.low;
175 int high = bb->block[block].col.high;
176 for(int j=low; j<=high; ++j){
177 v.push_back(getVariable(j));
178 }
179 return v;
180 }
181
182 const vector<Relation>
183 IncidenceMatrix::getBlockRels(const int &block){
184 CONSOLE_DEBUG("...");
185 if(!is_built){
186 buildPlotData();
187 }
188 vector<Relation> v;
189 const mtx_block_t *bb = slv_get_solvers_blocks(sim.getSystem());
190 if(block < 0 || block >= bb->nblocks){
191 ERROR_REPORTER_HERE(ASC_PROG_ERR,"Block out of range");
192 return v;
193 }
194 int low = bb->block[block].row.low;
195 int high = bb->block[block].row.high;
196 for(int j=low; j<=high; ++j){
197 v.push_back(getRelation(j));
198 }
199 CONSOLE_DEBUG("...");
200 return v;
201 }
202
203 const int
204 IncidenceMatrix::getNumBlocks(){
205 if(!is_built){
206 buildPlotData();
207 }
208 const mtx_block_t *bb = slv_get_solvers_blocks(sim.getSystem());
209 return bb->nblocks;
210 }
211
212

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