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

Contents of /trunk/pygtk/type.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 932 - (show annotations) (download) (as text)
Thu Nov 23 13:23:33 2006 UTC (13 years, 3 months ago) by johnpye
File MIME type: text/x-c++src
File size: 6913 byte(s)
Working on bugs in the 'error_reporter_tree' stuff.
Removed binary files from base/generic/utilities/test (these are built as needed by SCons now)
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 #include <compiler/compiler.h>
16 #include <compiler/dimen.h>
17 #include <compiler/symtab.h>
18 #include <compiler/instance_io.h>
19 #include <compiler/instantiate.h>
20 #include <compiler/bintoken.h>
21 #include <utilities/readln.h>
22 #include <solver/mtx.h>
23 #include <solver/slv_types.h>
24 #include <solver/var.h>
25 #include <solver/rel.h>
26 #include <solver/discrete.h>
27 #include <solver/conditional.h>
28 #include <solver/logrel.h>
29 #include <solver/bnd.h>
30 #include <solver/calc.h>
31 #include <solver/relman.h>
32 #include <solver/slv_common.h>
33 #include <solver/linsol.h>
34 #include <solver/linsolqr.h>
35 #include <solver/slv_client.h>
36 #include <solver/system.h>
37 #include <solver/slv_interface.h>
38 #include <compiler/simlist.h>
39 }
40
41 #include "type.h"
42 #include "simulation.h"
43 #include "library.h"
44 #include "dimensions.h"
45 #include "name.h"
46
47 /**
48 @TODO FIXME for some reason there are a lot of empty Type objects being created
49 */
50 Type::Type(){
51 //cerr << "CREATED EMPTY TYPE" << endl;
52 // throw runtime_error("Type::Type: Can't create new Types via C++ interface");
53 }
54
55 Type::Type(const TypeDescription *t) : t(t){
56 //cerr << "CREATED TYPE '" << getName() << "'" << endl;
57 }
58
59 const SymChar
60 Type::getName() const{
61 if(t==NULL){
62 throw runtime_error("Type::getName: t is NULL");
63 }
64 switch(GetBaseType(t)){
65 case array_type:
66 return SymChar("array");
67 case relation_type:
68 return SymChar("relation");
69 case logrel_type:
70 return SymChar("logrel");
71 case when_type:
72 return SymChar("when");
73 case set_type:
74 return SymChar("set");
75 default:
76 symchar *sym = GetName(t);
77 if(sym==NULL){
78 throw runtime_error("Unnamed type");
79 }
80 return SymChar(SCP(sym));
81 }
82 }
83
84 const int
85 Type::getParameterCount() const{
86 return GetModelParameterCount(t);
87 }
88
89 const TypeDescription *
90 Type::getInternalType() const{
91 return t;
92 }
93
94 const Dimensions
95 Type::getDimensions() const{
96 if( isRefinedConstant() ){
97 return Dimensions( GetConstantDimens(getInternalType()) );
98 }else if( isRefinedReal() ){
99 return Dimensions( GetRealDimens(getInternalType()) );
100 }else{
101 if( !isRefinedAtom() )throw runtime_error("Type::getDimensions: called with non-atom type");
102 throw runtime_error("Type::getDimensions: unrecognised type");
103 }
104 }
105
106 const bool
107 Type::isRefinedAtom() const{
108 return BaseTypeIsAtomic(t);
109 }
110
111 const bool
112 Type::isRefinedReal() const{
113 return BaseTypeIsReal(t);
114 }
115
116 const bool
117 Type::isRefinedConstant() const{
118 return BaseTypeIsConstant(t);
119 }
120
121 /**
122 Instantiate a type. This expensive: it will compile your
123 model as C-code and load a dynamic library with native
124 machine-code versions of your equations.
125
126 Once you have an instance of your model, you can start
127 to eliminate variables and attempt to solve it, see Instanc.
128
129 Note that there is some kind of dastardly underhand reference to the
130 Compiler class implicit here: the model instantiation call refers to
131 g_use_copyanon which is sort-of owned by the Compiler class.
132 */
133 Simulation
134 Type::getSimulation(const SymChar &sym, const bool &rundefaultmethod){
135 static bool have_bintoken_setup;
136 static string bin_targetstem;
137 static string bin_srcname;
138 static string bin_objname;
139 static string bin_libname;
140 static string bin_cmd;
141 static string bin_rm;
142
143 //CONSOLE_DEBUG("Type %s, getSimulation('%s')",getName().toString(), sym.toString());
144
145 // Tell ASCEND file locations and compiler commands:
146 if(0 && !have_bintoken_setup){
147 CONSOLE_DEBUG("SETUP BINTOKENS...");
148
149 /*
150 THE FOLLOWING STUFF IS CURRENTLY BROKEN.
151 The reason is that we need to add configuration options with SCons that
152 pass in the correct values here. These values need to correspond with
153 the *installed* location of things like 'Makefile.bt' (if that is what is
154 to be used, that is), where the ASCEND libs are, etc, whatever.
155 */
156 // bin_targetstem = ASCEND_TMPDIR "/asc_bintoken";
157 bin_targetstem = "/tmp/asc_bintoken";
158 bin_srcname = bin_targetstem + ".c";
159 bin_objname = bin_targetstem + ".o";
160 bin_libname = bin_targetstem + ".so";
161 bin_rm = "/bin/rm";
162 #if 0
163 /* bin_cmd = "make -C " ASCEND_TMPDIR " -f " ASCEND_MAKEFILEDIR_1 "/Makefile.bt" \
164 " SO=" + bin_targetstem + " ASCEND_INCDIR=\"" ASCEND_INCDIR "\" ASCEND_LIBDIR=\"" ASCEND_LIBDIR "\"";
165
166 cerr << "BINTOKEN COMMAND" << endl << "----" << bin_cmd << endl << "----" << endl;
167 */
168 #elif 0
169 /*
170 # define BTINCLUDES "-I" ASCEND_INCDIR
171 bin_cmd = "cd " ASCEND_INCDIR " && make BTTARGET=" + bin_targetstem + " BTINCLUDES=" BTINCLUDES \
172 " -f " ASCEND_MAKEFILEDIR "/Makefile.bt " + bin_targetstem;
173 */
174 #else
175 bin_cmd = "echo \"FIXME: BINTOKEN COMMANDLINE PARAMETERS\" && exit 1";
176 #endif
177 BinTokenSetOptions(bin_srcname.c_str(), bin_objname.c_str(), bin_libname.c_str()
178 , bin_cmd.c_str(), bin_rm.c_str(), 1000, 1, 0);
179
180 CONSOLE_DEBUG("...SETUP BINTOKENS");
181 have_bintoken_setup = true;
182 }
183
184 //cerr << "CREATING INSTANCE..." << endl;
185 // Perform the instantiation (C compile etc):
186 /*Instance *i = Instantiate(getInternalType()->name, sym.getInternalType(),
187 0, SymChar("on_load").getInternalType()); */
188 Instance *i = SimsCreateInstance(getInternalType()->name, sym.getInternalType(), e_normal, NULL);
189 Simulation sim(i,sym);
190
191 if(i==NULL){
192 throw runtime_error("Failed to create instance");
193 }
194
195 if(rundefaultmethod){
196 CONSOLE_DEBUG("RUNNING DEFAULT METHOD");
197 sim.runDefaultMethod();
198 }
199
200 //cerr << "CREATED INSTANCE " << sym << " OF " << getName() << endl;
201 return sim;
202 }
203
204 vector<Method>
205 Type::getMethods() const{
206 vector<Method> v;
207 struct gl_list_t *l = GetInitializationList(getInternalType());
208 if(l==NULL) return v;
209 for(int i=1, end=gl_length(l); i<=end; ++i){
210 v.push_back(Method((struct InitProcedure *)gl_fetch(l,i)));
211 }
212 return v;
213 }
214
215 Method
216 Type::getMethod(const SymChar &name) const{
217 struct gl_list_t *l = GetInitializationList(getInternalType());
218 if(l==NULL){
219 stringstream ss;
220 ss << "No methods present in type '" << getName() << "'";
221 throw runtime_error(ss.str());
222 }
223 struct InitProcedure *m;
224 m = SearchProcList(l,name.getInternalType());
225 if(m==NULL){
226 stringstream ss;
227 ss << "No method named '" << name << "' in type '" << getName() << "'";
228 throw runtime_error(ss.str());
229 return NULL;
230 }
231 return Method(m);
232 }
233
234 const bool
235 Type::isRefinedSolverVar() const{
236 static const TypeDescription *solver_var_type;
237 if(!solver_var_type){
238 Type t1 = Library().findType(SymChar("solver_var"));
239 solver_var_type=t1.getInternalType();
240 }
241 if(MoreRefined(t, solver_var_type)==t){
242 //cerr << getName() << " IS A REFINED SOLVER_VAR" << endl;
243 return true;
244 }
245 //cerr << getName() << "IS *NOT* A REFINED SOLVER_VAR" << endl;
246 return false;
247 }
248
249
250 const bool
251 Type::hasParameters() const{
252 return TypeHasParameterizedInsts(t);
253 }

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