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

Contents of /trunk/pygtk/incidencematrix.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1109 - (show annotations) (download) (as text)
Thu Jan 11 04:07:02 2007 UTC (15 years, 5 months ago) by johnpye
File MIME type: text/x-c++src
File size: 6107 byte(s)
Added some more user error messages from the solver.
Added detection for the MFGRAPH library, which will be used to output GraphViz files based on the incidence matrix.
1 #include "incidencematrix.h"
2
3 #include <stdexcept>
4 #include <iostream>
5 using namespace std;
6
7 #ifdef ASC_WITH_MFGRAPH
8 # include <mfg_draw_graph.h>
9 # include <mfg_graph_drawer.h>
10 #endif
11
12 #include "variable.h"
13 #include "relation.h"
14
15 extern "C"{
16 #include <utilities/ascConfig.h>
17 #include <compiler/instance_enum.h>
18
19 #include <solver/var.h>
20 #include <solver/rel.h>
21 #include <solver/discrete.h>
22 #include <solver/conditional.h>
23 #include <solver/logrel.h>
24 #include <solver/bnd.h>
25 #include <solver/mtx.h>
26 #include <solver/linsol.h>
27 #include <solver/linsolqr.h>
28 #include <solver/slv_common.h>
29 #include <solver/slv_types.h>
30 #include <solver/slv_client.h>
31 }
32
33 IncidencePoint::IncidencePoint(const int&row, const int &col, const IncidencePointType &type) : row(row), col(col), type(type){
34 // constructor, IncidencePoint
35 }
36
37 IncidencePoint::IncidencePoint(const IncidencePoint &old) : row(old.row), col(old.col), type(old.type){
38 // copy ctor
39 }
40
41 IncidencePoint::IncidencePoint() : row(-1), col(-1), type(IM_NULL){
42 // default ctor... don't use. need this to keep swig happy for some strange reason.
43 }
44
45 IncidenceMatrix::IncidenceMatrix(Simulation &sim) : sim(sim){
46 // constructor
47 is_built = FALSE;
48 }
49
50 IncidenceMatrix::~IncidenceMatrix(){
51 if(is_built){
52 free_incidence_data(&i);
53 }
54 }
55
56 void
57 IncidenceMatrix::buildPlotData(){
58 int c=-1;
59
60 cerr << "BUILDPLOTDATA" << endl;
61
62 slv_system_t sys = sim.getSystem();
63
64 cerr << "GOT SYSTEM DATA" << endl;
65
66 if(build_incidence_data(sys,&i)) {
67 cerr << "FAILED TO BUILD INCIDENCE DATA" << endl;
68 free_incidence_data(&i);
69 throw runtime_error("IncidenceMatrix::buildPlotData error calculating grid");
70 return;
71 }
72
73 for (int r=0; r < i.nprow; r++) {
74 struct rel_relation *rel = i.rlist[i.pr2e[r]];
75 const struct var_variable **vp = rel_incidence_list(rel);
76
77 if(rel_active(rel)){
78 int nvars = rel_n_incidences(rel);
79 if(rel_included(rel)){
80 for(int v=0; v < nvars; v++ ) {
81 if(var_flags(vp[v]) & VAR_SVAR) {
82 int vndx = var_sindex(vp[v]);
83 c = i.v2pc[vndx];
84 if (i.vfixed[vndx]) {
85 data.push_back(IncidencePoint(r,c,IM_ACTIVE_FIXED));
86 }else{
87 data.push_back(IncidencePoint(r,c,IM_ACTIVE_FREE));
88 }
89 }
90 }
91 }else{ /* hollow squares */
92 for(int v=0; v < nvars; v++ ) {
93 if (var_flags(vp[v]) & VAR_SVAR) {
94 int vndx = var_sindex(vp[v]);
95 c = i.v2pc[vndx];
96 if (i.vfixed[vndx]) {
97 data.push_back(IncidencePoint(r,c,IM_DORMANT_FIXED));
98 } else {
99 data.push_back(IncidencePoint(r,c,IM_DORMANT_FREE));
100 }
101 }
102 }
103 }
104 }
105 }
106
107 is_built = TRUE;
108 }
109
110 const int &
111 IncidenceMatrix::getNumRows() const{
112 return i.nprow;
113 }
114
115 const int &
116 IncidenceMatrix::getNumCols() const{
117 return i.npcol;
118 }
119
120 const vector<IncidencePoint> &
121 IncidenceMatrix::getIncidenceData(){
122 cerr << "GET INCIDENCE DATA" << endl;
123 if(!is_built){
124 buildPlotData();
125 }
126 return data;
127 }
128
129 const Variable
130 IncidenceMatrix::getVariable(const int &col) const{
131 if(!is_built)throw runtime_error("Not built");
132 if(col < 0 || col >= getNumCols())throw range_error("Column out of range");
133 int vindex = i.pc2v[col];
134 struct var_variable *var = i.vlist[vindex];
135
136 return Variable(&sim, var);
137 }
138
139 const Relation
140 IncidenceMatrix::getRelation(const int &row) const{
141 if(!is_built)throw runtime_error("Not built");
142 if(row < 0 || row >= getNumRows())throw range_error("Row out of range");
143 int rindex = i.pr2e[row];
144 struct rel_relation *rel = i.rlist[rindex];
145 return Relation(&sim, rel);
146 }
147
148 const int
149 IncidenceMatrix::getBlockRow(const int &row) const{
150 if(!is_built)throw runtime_error("Not built");
151 if(row < 0 || row >= getNumRows())throw range_error("Row out of range");
152 const mtx_block_t *bb = slv_get_solvers_blocks(sim.getSystem());
153 for(int i=0; i < bb->nblocks; ++i){
154 if(row >= bb->block[i].row.low && row <= bb->block[i].row.high){
155 return i;
156 }
157 }
158 return -1;
159 }
160
161 /**
162 Returns location of specified block
163 @param block the block number
164 @return vector(ve row-low, col-low, row-high, col-high)
165 */
166 const vector<int>
167 IncidenceMatrix::getBlockLocation(const int &block) const{
168 if(!is_built)throw runtime_error("Not built");
169 const mtx_block_t *bb = slv_get_solvers_blocks(sim.getSystem());
170 if(block < 0 || block >= bb->nblocks){
171 throw range_error("Invalid block number");
172 }
173 vector<int> v;
174 v.push_back(bb->block[block].row.low);
175 v.push_back(bb->block[block].col.low);
176 v.push_back(bb->block[block].row.high);
177 v.push_back(bb->block[block].col.high);
178 return v;
179 }
180
181 const vector<Variable>
182 IncidenceMatrix::getBlockVars(const int &block){
183 if(!is_built){
184 buildPlotData();
185 }
186 vector<Variable> v;
187 const mtx_block_t *bb = slv_get_solvers_blocks(sim.getSystem());
188 if(block < 0 || block >= bb->nblocks){
189 ERROR_REPORTER_HERE(ASC_PROG_ERR,"Block out of range");
190 return v;
191 }
192 int low = bb->block[block].col.low;
193 int high = bb->block[block].col.high;
194 for(int j=low; j<=high; ++j){
195 v.push_back(getVariable(j));
196 }
197 return v;
198 }
199
200 const vector<Relation>
201 IncidenceMatrix::getBlockRels(const int &block){
202 CONSOLE_DEBUG("...");
203 if(!is_built){
204 buildPlotData();
205 }
206 vector<Relation> v;
207 const mtx_block_t *bb = slv_get_solvers_blocks(sim.getSystem());
208 if(block < 0 || block >= bb->nblocks){
209 ERROR_REPORTER_HERE(ASC_PROG_ERR,"Block out of range");
210 return v;
211 }
212 int low = bb->block[block].row.low;
213 int high = bb->block[block].row.high;
214 for(int j=low; j<=high; ++j){
215 v.push_back(getRelation(j));
216 }
217 CONSOLE_DEBUG("...");
218 return v;
219 }
220
221 const int
222 IncidenceMatrix::getNumBlocks(){
223 if(!is_built){
224 buildPlotData();
225 }
226 const mtx_block_t *bb = slv_get_solvers_blocks(sim.getSystem());
227 return bb->nblocks;
228 }
229
230 #if ASC_WITH_MFGRAPH
231 void
232 IncidenceMatrix::writeBlockGraph(ostream &os, const int &block){
233 using namespace mfg;
234
235 DrawGraph g;
236 Node* a = g.CreateNode();
237 Node* x = g.CreateNode();
238 Edge* ax = g.CreateEdge(a, x);
239 ax->Attribs()["color"] = "red";
240 Subgraph* s = g.CreateSubgraph();
241 Node* b = g.CreateNode(s);
242 b->Attribs()["style"] = "filled";
243 b->Attribs()["fillcolor"] = "green";
244 Edge* ab = g.CreateEdge(a, b);
245
246 g.PrintAsDot(os);
247 }
248 #endif
249

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