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

Contents of /trunk/pygtk/type.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

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