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

Contents of /trunk/pygtk/type.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1290 - (show annotations) (download) (as text)
Mon Feb 26 04:54:45 2007 UTC (17 years, 5 months ago) by johnpye
File MIME type: text/x-c++src
File size: 5135 byte(s)
Added new 'default' functionality plus test case
1 #include <Python.h>
2
3 #include <iostream>
4 #include <stdexcept>
5 #include <string>
6 #include <sstream>
7 using namespace std;
8
9 extern "C"{
10 #include <utilities/ascConfig.h>
11 #include <utilities/ascSignal.h>
12 #include <general/dstring.h>
13 #include <compiler/instance_enum.h>
14 #include <compiler/fractions.h>
15
16 #include <compiler/dimen.h>
17 #include <compiler/symtab.h>
18 #include <compiler/instance_io.h>
19 #include <compiler/type_desc.h>
20 #include <compiler/bintoken.h>
21 #include <linear/mtx.h>
22 #include <solver/calc.h>
23 #include <solver/relman.h>
24 #include <solver/slv_common.h>
25 #include <solver/slv_client.h>
26 #include <solver/system.h>
27 #include <solver/slv_interface.h>
28 #include <compiler/simlist.h>
29 }
30
31 #include "type.h"
32 #include "simulation.h"
33 #include "library.h"
34 #include "dimensions.h"
35 #include "name.h"
36 #include "compiler.h"
37
38 /**
39 @TODO FIXME for some reason there are a lot of empty Type objects being created
40 */
41 Type::Type(){
42 //cerr << "CREATED EMPTY TYPE" << endl;
43 // throw runtime_error("Type::Type: Can't create new Types via C++ interface");
44 }
45
46 Type::Type(const TypeDescription *t) : t(t){
47 //cerr << "CREATED TYPE '" << getName() << "'" << endl;
48 }
49
50 const SymChar
51 Type::getName() const{
52 if(t==NULL){
53 throw runtime_error("Type::getName: t is NULL");
54 }
55 switch(GetBaseType(t)){
56 case array_type:
57 return SymChar("array");
58 case relation_type:
59 return SymChar("relation");
60 case logrel_type:
61 return SymChar("logrel");
62 case when_type:
63 return SymChar("when");
64 case set_type:
65 return SymChar("set");
66 default:
67 symchar *sym = GetName(t);
68 if(sym==NULL){
69 throw runtime_error("Unnamed type");
70 }
71 return SymChar(SCP(sym));
72 }
73 }
74
75 const int
76 Type::getParameterCount() const{
77 return GetModelParameterCount(t);
78 }
79
80 const TypeDescription *
81 Type::getInternalType() const{
82 return t;
83 }
84
85 const Dimensions
86 Type::getDimensions() const{
87 if( isRefinedConstant() ){
88 return Dimensions( GetConstantDimens(getInternalType()) );
89 }else if( isRefinedReal() ){
90 return Dimensions( GetRealDimens(getInternalType()) );
91 }else{
92 if( !isRefinedAtom() )throw runtime_error("Type::getDimensions: called with non-atom type");
93 throw runtime_error("Type::getDimensions: unrecognised type");
94 }
95 }
96
97 const bool
98 Type::isRefinedAtom() const{
99 return BaseTypeIsAtomic(t);
100 }
101
102 const bool
103 Type::isRefinedReal() const{
104 return BaseTypeIsReal(t);
105 }
106
107 const bool
108 Type::isRefinedConstant() const{
109 return BaseTypeIsConstant(t);
110 }
111
112 /**
113 Instantiate a type. This *can be* expensive, if you have selected to
114 compile your model into C-code and load a dynamic library with native
115 machine-code versions of your equations.
116
117 Once you have an instance of your model, you can start
118 to eliminate variables and attempt to solve it, see Instanc.
119
120 Note that there is some kind of dastardly underhand reference to the
121 Compiler class implicit here: the model instantiation call refers to
122 g_use_copyanon which is sort-of owned by the Compiler class.
123 */
124 Simulation
125 Type::getSimulation(const SymChar &sym
126 , const bool &rundefaultmethod
127 ){
128 /* notify the compiler of our bintoken options, if nec */
129 Compiler::instance()->sendBinaryCompilationOptions();
130
131 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Starting tree...\n");
132 error_reporter_tree_start();
133 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Started tree\n");
134
135 Instance *i = SimsCreateInstance(getInternalType()->name, sym.getInternalType(), e_normal, NULL);
136 Simulation sim(i,sym);
137
138 bool has_error;
139 if(error_reporter_tree_has_error()){
140 has_error = TRUE;
141 }else{
142 has_error = FALSE;
143 }
144
145 error_reporter_tree_end();
146
147 if(has_error){
148 stringstream ss;
149 ss << "Error(s) during instantiation of type '" << getName() << "'";
150 throw runtime_error(ss.str());
151 }else{
152 ERROR_REPORTER_HERE(ASC_USER_NOTE,"Instantiated %s",SCP(getInternalType()->name));
153 }
154
155 if(i==NULL){
156 throw runtime_error("Failed to create instance");
157 }
158
159 if(rundefaultmethod){
160 CONSOLE_DEBUG("RUNNING DEFAULT METHOD");
161 sim.runDefaultMethod();
162 }
163
164 return sim;
165 }
166
167 vector<Method>
168 Type::getMethods() const{
169 vector<Method> v;
170 struct gl_list_t *l = GetInitializationList(getInternalType());
171 if(l==NULL) return v;
172 for(int i=1, end=gl_length(l); i<=end; ++i){
173 v.push_back(Method((struct InitProcedure *)gl_fetch(l,i)));
174 }
175 return v;
176 }
177
178 Method
179 Type::getMethod(const SymChar &name) const{
180 if(GetBaseType(t)!=model_type){
181 stringstream ss;
182 ss << "Type '" << getName() << "' is not a MODEL";
183 throw runtime_error(ss.str());
184 }
185
186 struct InitProcedure *m;
187 m = FindMethod(t,name.getInternalType());
188
189 if(m==NULL){
190 stringstream ss;
191 ss << "No method named '" << name << "' in type '" << getName() << "'";
192 throw runtime_error(ss.str());
193 return NULL;
194 }
195
196 return Method(m);
197 }
198
199 const bool
200 Type::isRefinedSolverVar() const{
201 const TypeDescription *solver_var_type;
202 Type t1 = Library().findType(SymChar("solver_var"));
203 solver_var_type=t1.getInternalType();
204
205 if(MoreRefined(t, solver_var_type)==t){
206 //cerr << getName() << " IS A REFINED SOLVER_VAR" << endl;
207 return true;
208 }
209 //cerr << getName() << "IS *NOT* A REFINED SOLVER_VAR" << endl;
210 return false;
211 }
212
213
214 const bool
215 Type::hasParameters() const{
216 return TypeHasParameterizedInsts(t);
217 }

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