/[ascend]/branches/relerrorlist/ascxx/type.cpp
ViewVC logotype

Contents of /branches/relerrorlist/ascxx/type.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3197 - (show annotations) (download) (as text)
Sun Apr 23 03:30:25 2017 UTC (13 months, 4 weeks ago) by jpye
File MIME type: text/x-c++src
File size: 7625 byte(s)
test suite almost done, one memory leak still to be fixed.

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 #if 1
150 /* removing the following line causes a crash on Windows 7 */
151 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Starting tree...\n");
152 error_reporter_tree_t *tree1 = error_reporter_tree_start(0);
153 /* ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Started tree\n"); */
154 #endif
155
156 Instance *i = SimsCreateInstance(getInternalType()->name, sym.getInternalType(), e_normal, NULL);
157 Simulation sim(i,sym);
158
159 #if 1
160 bool has_error = FALSE;
161 if(error_reporter_tree_has_error(tree1)){
162 has_error = TRUE;
163 }
164
165 error_reporter_tree_end(tree1);
166 if(has_error){
167
168 stringstream ss;
169 ss << "Error(s) during instantiation of type '" << getName() << "'";
170 throw runtime_error(ss.str());
171 }else{
172 ERROR_REPORTER_HERE(ASC_USER_NOTE,"Instantiated %s",SCP(getInternalType()->name));
173 }
174 #endif
175
176 #if 1
177 //CONSOLE_DEBUG("CHECKING INSTANCE...");
178 sim.checkInstance(5);
179 //CONSOLE_DEBUG("...DONE CHECKING INSTANCE");
180 #endif
181
182 #if 0
183 CONSOLE_DEBUG("CHECKING TOKENS...");
184 sim.checkTokens();
185 CONSOLE_DEBUG("...DONE CHECKING TOKENS");
186 #endif
187
188 #if 0
189 CONSOLE_DEBUG("CHECKING STATISTICS...");
190 sim.checkStatistics();
191 CONSOLE_DEBUG("...DONE CHECKING STATISTICS");
192 #endif
193
194 if(i==NULL){
195 throw runtime_error("Failed to create instance");
196 }
197
198 if(rundefaultmethod){
199 //CONSOLE_DEBUG("RUNNING DEFAULT METHOD");
200 sim.runDefaultMethod();
201 }
202
203 return sim;
204 }
205
206 vector<Method>
207 Type::getMethods() const{
208 vector<Method> v;
209 struct gl_list_t *l = GetInitializationList(getInternalType());
210 if(l==NULL) return v;
211 for(int i=1, end=gl_length(l); i<=end; ++i){
212 v.push_back(Method((struct InitProcedure *)gl_fetch(l,i)));
213 }
214 return v;
215 }
216
217 Method
218 Type::getMethod(const SymChar &name) const{
219 if(GetBaseType(t)!=model_type){
220 stringstream ss;
221 ss << "Type '" << getName() << "' is not a MODEL";
222 throw runtime_error(ss.str());
223 }
224
225 struct InitProcedure *m;
226 m = FindMethod(t,name.getInternalType());
227
228 if(m==NULL){
229 stringstream ss;
230 ss << "No method named '" << name << "' in type '" << getName() << "'";
231 throw runtime_error(ss.str());
232 return NULL;
233 }
234
235 return Method(m);
236 }
237
238 const bool
239 Type::isRefinedSolverVar() const{
240 const TypeDescription *solver_var_type;
241 Type t1 = Library().findType(SymChar("solver_var"));
242 solver_var_type=t1.getInternalType();
243
244 if(MoreRefined(t, solver_var_type)==t){
245 //cerr << getName() << " IS A REFINED SOLVER_VAR" << endl;
246 return true;
247 }
248 //cerr << getName() << "IS *NOT* A REFINED SOLVER_VAR" << endl;
249 return false;
250 }
251
252 const bool
253 Type::isFundamental() const{
254 return CheckFundamental(getName().getInternalType());
255 }
256
257 const bool
258 Type::isModel() const{
259 return GetBaseType(t) == model_type;
260 }
261
262 const bool
263 Type::hasParameters() const{
264 return GetModelParameterCount(t) > 0;
265 }
266
267 bool
268 Type::operator<(const Type &other) const{
269 // modelled on the Unit_CmpAtomName function from UnitsProc.c in Tcl code...
270 if(!getInternalType() || !other.getInternalType() || this->isFundamental()){
271 return false;
272 }
273 return (this->getName() < other.getName());
274 }
275
276 Module
277 Type::getModule() const{
278 return GetModule(getInternalType());
279 }
280
281 const Type &
282 Type::findMember(const SymChar &name){
283
284 unsigned long pos;
285 ChildListPtr CL;
286
287 CL = GetChildList(t);
288 pos = ChildPos(CL,name.getInternalType());
289
290 unsigned long clsize = ChildListLen(CL);
291
292 if((pos<1) || (pos>clsize))
293 {
294 stringstream ss;
295 ss << "Library::findType: type '" << name << "' not found in library";
296 throw runtime_error(ss.str());
297 }
298
299 const TypeDescription *t = ChildBaseTypePtr(CL,pos);
300
301 if(t==NULL){
302 stringstream ss;
303 ss << "Library::findType: type '" << name << "' not found in library";
304 throw runtime_error(ss.str());
305 }
306 Type *t2=new Type(t);
307 return *t2;
308 }

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