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

Contents of /trunk/pygtk/type.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 770 - (show annotations) (download) (as text)
Fri Jul 14 03:35:16 2006 UTC (13 years, 8 months ago) by johnpye
File MIME type: text/x-c++src
File size: 6255 byte(s)
Added 'use relation sharing' option to the PyGTK GUI.
Changed some debug output calls.
Tidied up headers in anoncopy, anonmerg, etc.
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 Note that there is some kind of dastardly underhand reference to the
126 Compiler class implicit here: the model instantiation call refers to
127 g_use_copyanon which is sort-of owned by the Compiler class.
128 */
129 Simulation
130 Type::getSimulation(SymChar sym){
131 static bool have_bintoken_setup;
132 static string bin_targetstem;
133 static string bin_srcname;
134 static string bin_objname;
135 static string bin_libname;
136 static string bin_cmd;
137 static string bin_rm;
138
139 cerr << "Type " << getName().toString() << ", getSimulation('" << sym.toString() << "')" << endl;
140
141 // Tell ASCEND file locations and compiler commands:
142 if(0 && !have_bintoken_setup){
143 cerr << "SETUP BINTOKENS..." << endl;
144
145 /*
146 THE FOLLOWING STUFF IS CURRENTLY BROKEN.
147 The reason is that we need to add configuration options with SCons that
148 pass in the correct values here. These values need to correspond with
149 the *installed* location of things like 'Makefile.bt' (if that is what is
150 to be used, that is), where the ASCEND libs are, etc, whatever.
151 */
152 // bin_targetstem = ASCEND_TMPDIR "/asc_bintoken";
153 bin_targetstem = "/tmp/asc_bintoken";
154 bin_srcname = bin_targetstem + ".c";
155 bin_objname = bin_targetstem + ".o";
156 bin_libname = bin_targetstem + ".so";
157 bin_rm = "/bin/rm";
158 #if 0
159 /* bin_cmd = "make -C " ASCEND_TMPDIR " -f " ASCEND_MAKEFILEDIR_1 "/Makefile.bt" \
160 " SO=" + bin_targetstem + " ASCEND_INCDIR=\"" ASCEND_INCDIR "\" ASCEND_LIBDIR=\"" ASCEND_LIBDIR "\"";
161
162 cerr << "BINTOKEN COMMAND" << endl << "----" << bin_cmd << endl << "----" << endl;
163 */
164 #elif 0
165 /*
166 # define BTINCLUDES "-I" ASCEND_INCDIR
167 bin_cmd = "cd " ASCEND_INCDIR " && make BTTARGET=" + bin_targetstem + " BTINCLUDES=" BTINCLUDES \
168 " -f " ASCEND_MAKEFILEDIR "/Makefile.bt " + bin_targetstem;
169 */
170 #else
171 bin_cmd = "echo \"FIXME: BINTOKEN COMMANDLINE PARAMETERS\" && exit 1";
172 #endif
173 BinTokenSetOptions(bin_srcname.c_str(), bin_objname.c_str(), bin_libname.c_str()
174 , bin_cmd.c_str(), bin_rm.c_str(), 1000, 1, 0);
175
176 cerr << "...SETUP BINTOKENS" << endl;
177 have_bintoken_setup = true;
178 }
179
180 cerr << "CREATING INSTANCE..." << endl;
181 // Perform the instantiation (C compile etc):
182 /*Instance *i = Instantiate(getInternalType()->name, sym.getInternalType(),
183 0, SymChar("on_load").getInternalType()); */
184 Instance *i = SimsCreateInstance(getInternalType()->name, sym.getInternalType(), e_normal, SymChar("on_load").getInternalType());
185
186 if(i==NULL){
187 throw runtime_error("Failed to create instance");
188 }
189
190 cerr << "CREATED INSTANCE " << sym << " OF " << getName() << endl;
191 return Simulation(i,sym);
192 }
193
194 vector<Method>
195 Type::getMethods() const{
196 vector<Method> v;
197 struct gl_list_t *l = GetInitializationList(getInternalType());
198 if(l==NULL) return v;
199 for(int i=1, end=gl_length(l); i<=end; ++i){
200 v.push_back(Method((struct InitProcedure *)gl_fetch(l,i)));
201 }
202 return v;
203 }
204
205 const bool
206 Type::isRefinedSolverVar() const{
207 static const TypeDescription *solver_var_type;
208 if(!solver_var_type){
209 Type t1 = Library().findType(SymChar("solver_var"));
210 solver_var_type=t1.getInternalType();
211 }
212 if(MoreRefined(t, solver_var_type)==t){
213 //cerr << getName() << " IS A REFINED SOLVER_VAR" << endl;
214 return true;
215 }
216 //cerr << getName() << "IS *NOT* A REFINED SOLVER_VAR" << endl;
217 return false;
218 }
219
220
221 const bool
222 Type::hasParameters() const{
223 return TypeHasParameterizedInsts(t);
224 }

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