1 |
/* ASCEND modelling environment |
2 |
Copyright (C) 2006-2010 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 |
#include <Python.h> |
20 |
|
21 |
#include <iostream> |
22 |
#include <stdexcept> |
23 |
#include <string> |
24 |
#include <sstream> |
25 |
using namespace std; |
26 |
|
27 |
extern "C"{ |
28 |
#include <ascend/general/platform.h> |
29 |
#include <ascend/utilities/ascSignal.h> |
30 |
#include <ascend/general/dstring.h> |
31 |
#include <ascend/compiler/instance_enum.h> |
32 |
#include <ascend/compiler/fractions.h> |
33 |
|
34 |
#include <ascend/compiler/dimen.h> |
35 |
#include <ascend/compiler/symtab.h> |
36 |
#include <ascend/compiler/instance_io.h> |
37 |
#include <ascend/compiler/type_desc.h> |
38 |
#include <ascend/compiler/bintoken.h> |
39 |
#include <ascend/compiler/library.h> |
40 |
#include <ascend/linear/mtx.h> |
41 |
#include <ascend/system/calc.h> |
42 |
#include <ascend/system/relman.h> |
43 |
#include <ascend/system/slv_client.h> |
44 |
#include <ascend/system/system.h> |
45 |
#include <ascend/compiler/simlist.h> |
46 |
#include <ascend/compiler/child.h> |
47 |
} |
48 |
|
49 |
#include "type.h" |
50 |
#include "simulation.h" |
51 |
#include "library.h" |
52 |
#include "dimensions.h" |
53 |
#include "name.h" |
54 |
#include "compiler.h" |
55 |
|
56 |
/** |
57 |
@TODO FIXME for some reason there are a lot of empty Type objects being created |
58 |
*/ |
59 |
Type::Type(){ |
60 |
//cerr << "CREATED EMPTY TYPE" << endl; |
61 |
// throw runtime_error("Type::Type: Can't create new Types via C++ interface"); |
62 |
} |
63 |
|
64 |
Type::Type(const TypeDescription *t) : t(t){ |
65 |
//cerr << "CREATED TYPE '" << getName() << "'" << endl; |
66 |
} |
67 |
|
68 |
const SymChar |
69 |
Type::getName() const{ |
70 |
if(t==NULL){ |
71 |
throw runtime_error("Type::getName: t is NULL"); |
72 |
} |
73 |
switch(GetBaseType(t)){ |
74 |
case array_type: |
75 |
return SymChar("array"); |
76 |
case relation_type: |
77 |
return SymChar("relation"); |
78 |
case logrel_type: |
79 |
return SymChar("logrel"); |
80 |
case when_type: |
81 |
return SymChar("when"); |
82 |
case set_type: |
83 |
return SymChar("set"); |
84 |
default: |
85 |
symchar *sym = GetName(t); |
86 |
if(sym==NULL){ |
87 |
throw runtime_error("Unnamed type"); |
88 |
} |
89 |
return SymChar(SCP(sym)); |
90 |
} |
91 |
} |
92 |
|
93 |
const int |
94 |
Type::getParameterCount() const{ |
95 |
return GetModelParameterCount(t); |
96 |
} |
97 |
|
98 |
const TypeDescription * |
99 |
Type::getInternalType() const{ |
100 |
return t; |
101 |
} |
102 |
|
103 |
const Dimensions |
104 |
Type::getDimensions() const{ |
105 |
if( isRefinedConstant() ){ |
106 |
return Dimensions( GetConstantDimens(getInternalType()) ); |
107 |
}else if( isRefinedReal() ){ |
108 |
return Dimensions( GetRealDimens(getInternalType()) ); |
109 |
}else{ |
110 |
if( !isRefinedAtom() )throw runtime_error("Type::getDimensions: called with non-atom type"); |
111 |
throw runtime_error("Type::getDimensions: unrecognised type"); |
112 |
} |
113 |
} |
114 |
|
115 |
const bool |
116 |
Type::isRefinedAtom() const{ |
117 |
return BaseTypeIsAtomic(t); |
118 |
} |
119 |
|
120 |
const bool |
121 |
Type::isRefinedReal() const{ |
122 |
return BaseTypeIsReal(t); |
123 |
} |
124 |
|
125 |
const bool |
126 |
Type::isRefinedConstant() const{ |
127 |
return BaseTypeIsConstant(t); |
128 |
} |
129 |
|
130 |
/** |
131 |
Instantiate a type. This *can be* expensive, if you have selected to |
132 |
compile your model into C-code and load a dynamic library with native |
133 |
machine-code versions of your equations. |
134 |
|
135 |
Once you have an instance of your model, you can start |
136 |
to eliminate variables and attempt to solve it, see Instanc. |
137 |
|
138 |
Note that there is some kind of dastardly underhand reference to the |
139 |
Compiler class implicit here: the model instantiation call refers to |
140 |
g_use_copyanon which is sort-of owned by the Compiler class. |
141 |
*/ |
142 |
Simulation |
143 |
Type::getSimulation(const SymChar &sym |
144 |
, const bool rundefaultmethod |
145 |
){ |
146 |
/* notify the compiler of our bintoken options, if nec */ |
147 |
Compiler::instance()->sendBinaryCompilationOptions(); |
148 |
|
149 |
/* removing the following line causes a crash on Windows 7 */ |
150 |
ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Starting tree...\n"); |
151 |
#if 1 |
152 |
error_reporter_tree_start(); |
153 |
#endif |
154 |
/* ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Started tree\n"); */ |
155 |
|
156 |
Instance *i = SimsCreateInstance(getInternalType()->name, sym.getInternalType(), e_normal, NULL); |
157 |
Simulation sim(i,sym); |
158 |
|
159 |
bool has_error = FALSE; |
160 |
#if 1 |
161 |
if(error_reporter_tree_has_error()){ |
162 |
has_error = TRUE; |
163 |
} |
164 |
|
165 |
error_reporter_tree_end(); |
166 |
#endif |
167 |
|
168 |
if(has_error){ |
169 |
stringstream ss; |
170 |
ss << "Error(s) during instantiation of type '" << getName() << "'"; |
171 |
throw runtime_error(ss.str()); |
172 |
}else{ |
173 |
ERROR_REPORTER_HERE(ASC_USER_NOTE,"Instantiated %s",SCP(getInternalType()->name)); |
174 |
} |
175 |
|
176 |
if(i==NULL){ |
177 |
throw runtime_error("Failed to create instance"); |
178 |
} |
179 |
|
180 |
if(rundefaultmethod){ |
181 |
//CONSOLE_DEBUG("RUNNING DEFAULT METHOD"); |
182 |
sim.runDefaultMethod(); |
183 |
} |
184 |
|
185 |
return sim; |
186 |
} |
187 |
|
188 |
vector<Method> |
189 |
Type::getMethods() const{ |
190 |
vector<Method> v; |
191 |
struct gl_list_t *l = GetInitializationList(getInternalType()); |
192 |
if(l==NULL) return v; |
193 |
for(int i=1, end=gl_length(l); i<=end; ++i){ |
194 |
v.push_back(Method((struct InitProcedure *)gl_fetch(l,i))); |
195 |
} |
196 |
return v; |
197 |
} |
198 |
|
199 |
Method |
200 |
Type::getMethod(const SymChar &name) const{ |
201 |
if(GetBaseType(t)!=model_type){ |
202 |
stringstream ss; |
203 |
ss << "Type '" << getName() << "' is not a MODEL"; |
204 |
throw runtime_error(ss.str()); |
205 |
} |
206 |
|
207 |
struct InitProcedure *m; |
208 |
m = FindMethod(t,name.getInternalType()); |
209 |
|
210 |
if(m==NULL){ |
211 |
stringstream ss; |
212 |
ss << "No method named '" << name << "' in type '" << getName() << "'"; |
213 |
throw runtime_error(ss.str()); |
214 |
return NULL; |
215 |
} |
216 |
|
217 |
return Method(m); |
218 |
} |
219 |
|
220 |
const bool |
221 |
Type::isRefinedSolverVar() const{ |
222 |
const TypeDescription *solver_var_type; |
223 |
Type t1 = Library().findType(SymChar("solver_var")); |
224 |
solver_var_type=t1.getInternalType(); |
225 |
|
226 |
if(MoreRefined(t, solver_var_type)==t){ |
227 |
//cerr << getName() << " IS A REFINED SOLVER_VAR" << endl; |
228 |
return true; |
229 |
} |
230 |
//cerr << getName() << "IS *NOT* A REFINED SOLVER_VAR" << endl; |
231 |
return false; |
232 |
} |
233 |
|
234 |
const bool |
235 |
Type::isFundamental() const{ |
236 |
return CheckFundamental(getName().getInternalType()); |
237 |
} |
238 |
|
239 |
const bool |
240 |
Type::isModel() const{ |
241 |
return GetBaseType(t) == model_type; |
242 |
} |
243 |
|
244 |
const bool |
245 |
Type::hasParameters() const{ |
246 |
return GetModelParameterCount(t) > 0; |
247 |
} |
248 |
|
249 |
bool |
250 |
Type::operator<(const Type &other) const{ |
251 |
// modelled on the Unit_CmpAtomName function from UnitsProc.c in Tcl code... |
252 |
if(!getInternalType() || !other.getInternalType() || this->isFundamental()){ |
253 |
return false; |
254 |
} |
255 |
return (this->getName() < other.getName()); |
256 |
} |
257 |
|
258 |
Module |
259 |
Type::getModule() const{ |
260 |
return GetModule(getInternalType()); |
261 |
} |
262 |
|
263 |
const Type & |
264 |
Type::findMember(const SymChar &name){ |
265 |
|
266 |
unsigned long pos; |
267 |
ChildListPtr CL; |
268 |
|
269 |
CL = GetChildList(t); |
270 |
pos = ChildPos(CL,name.getInternalType()); |
271 |
|
272 |
unsigned long clsize = ChildListLen(CL); |
273 |
|
274 |
if((pos<1) || (pos>clsize)) |
275 |
{ |
276 |
stringstream ss; |
277 |
ss << "Library::findType: type '" << name << "' not found in library"; |
278 |
throw runtime_error(ss.str()); |
279 |
} |
280 |
|
281 |
const TypeDescription *t = ChildBaseTypePtr(CL,pos); |
282 |
|
283 |
if(t==NULL){ |
284 |
stringstream ss; |
285 |
ss << "Library::findType: type '" << name << "' not found in library"; |
286 |
throw runtime_error(ss.str()); |
287 |
} |
288 |
Type *t2=new Type(t); |
289 |
return *t2; |
290 |
} |