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

Contents of /trunk/pygtk/type.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 900 - (show annotations) (download) (as text)
Wed Oct 25 06:03:04 2006 UTC (13 years, 11 months ago) by johnpye
File MIME type: text/x-c++src
File size: 6773 byte(s)
The 'sim' object is correctly accessible from script methods now, even if run during on_load.
Added 'getSimulation' to registry.cpp, added 'runDefaultMethod' to Simulation, added 'getMethod' to Type.
Running of 'on_load' is instigated at the Python level now, so that relevent python variables are set at simulations are built, etc. This appears to have cause some changes to the way the solver behaves, possibly.
Added SearchProcList to exports in libascend.

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

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