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 <ascend/linear/mtx.h> |
12 |
#include <ascend/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 BlockStatusType |
164 |
IncidenceMatrix::getBlockStatus(const int &block) const{ |
165 |
if(!is_built)throw runtime_error("Not build"); |
166 |
SolverStatus st; |
167 |
st.getSimulationStatus(sim); |
168 |
|
169 |
if(st.isConverged() || st.getCurrentBlockNum() > block){ |
170 |
return IM_CONVERGED; |
171 |
} |
172 |
|
173 |
if(st.getCurrentBlockNum() < block){ |
174 |
return IM_NOT_YET_ATTEMPTED; |
175 |
} |
176 |
|
177 |
if(st.hasExceededIterationLimit())return IM_OVER_ITER; |
178 |
if(st.hasExceededTimeLimit())return IM_OVER_TIME; |
179 |
return IM_DIVERGED; |
180 |
} |
181 |
|
182 |
const vector<Variable> |
183 |
IncidenceMatrix::getBlockVars(const int &block){ |
184 |
if(!is_built){ |
185 |
buildPlotData(); |
186 |
} |
187 |
vector<Variable> v; |
188 |
const mtx_block_t *bb = slv_get_solvers_blocks(sim.getSystem()); |
189 |
if(block < 0 || block >= bb->nblocks){ |
190 |
ERROR_REPORTER_HERE(ASC_PROG_ERR,"Block out of range"); |
191 |
return v; |
192 |
} |
193 |
int low = bb->block[block].col.low; |
194 |
int high = bb->block[block].col.high; |
195 |
for(int j=low; j<=high; ++j){ |
196 |
v.push_back(getVariable(j)); |
197 |
} |
198 |
return v; |
199 |
} |
200 |
|
201 |
const vector<Relation> |
202 |
IncidenceMatrix::getBlockRels(const int &block){ |
203 |
CONSOLE_DEBUG("..."); |
204 |
if(!is_built){ |
205 |
buildPlotData(); |
206 |
} |
207 |
vector<Relation> v; |
208 |
const mtx_block_t *bb = slv_get_solvers_blocks(sim.getSystem()); |
209 |
if(block < 0 || block >= bb->nblocks){ |
210 |
ERROR_REPORTER_HERE(ASC_PROG_ERR,"Block out of range"); |
211 |
return v; |
212 |
} |
213 |
int low = bb->block[block].row.low; |
214 |
int high = bb->block[block].row.high; |
215 |
for(int j=low; j<=high; ++j){ |
216 |
v.push_back(getRelation(j)); |
217 |
} |
218 |
CONSOLE_DEBUG("..."); |
219 |
return v; |
220 |
} |
221 |
|
222 |
const int |
223 |
IncidenceMatrix::getNumBlocks(){ |
224 |
if(!is_built){ |
225 |
buildPlotData(); |
226 |
} |
227 |
const mtx_block_t *bb = slv_get_solvers_blocks(sim.getSystem()); |
228 |
return bb->nblocks; |
229 |
} |
230 |
|
231 |
|