~jdpipe/ascend/trunk-old

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*	ASCEND modelling environment
	Copyright (C) 2006-2010 Carnegie Mellon University

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2, or (at your option)
	any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330,
	Boston, MA 02111-1307, USA.
*/
#include <Python.h>

#include <iostream>
#include <stdexcept>
#include <string>
#include <sstream>
using namespace std;

extern "C"{
#include <ascend/general/platform.h>
#include <ascend/utilities/ascSignal.h>
#include <ascend/general/dstring.h>
#include <ascend/compiler/instance_enum.h>
#include <ascend/compiler/fractions.h>

#include <ascend/compiler/dimen.h>
#include <ascend/compiler/symtab.h>
#include <ascend/compiler/instance_io.h>
#include <ascend/compiler/type_desc.h>
#include <ascend/compiler/bintoken.h>
#include <ascend/compiler/library.h>
#include <ascend/linear/mtx.h>
#include <ascend/system/calc.h>
#include <ascend/system/relman.h>
#include <ascend/system/slv_client.h>
#include <ascend/system/system.h>
#include <ascend/compiler/simlist.h>
#include <ascend/compiler/child.h>
}

#include "type.h"
#include "simulation.h"
#include "library.h"
#include "dimensions.h"
#include "name.h"
#include "compiler.h"

/**
	@TODO FIXME for some reason there are a lot of empty Type objects being created
*/
Type::Type(){
	//cerr << "CREATED EMPTY TYPE" << endl;
	// throw runtime_error("Type::Type: Can't create new Types via C++ interface");
}

Type::Type(const TypeDescription *t) : t(t){
	//cerr << "CREATED TYPE '" << getName() << "'" << endl;
}

const SymChar
Type::getName() const{
	if(t==NULL){
		throw runtime_error("Type::getName: t is NULL");
	}
	switch(GetBaseType(t)){
		case array_type:
			return SymChar("array");
		case relation_type:
			return SymChar("relation");
		case logrel_type:
			return SymChar("logrel");
		case when_type:
			return SymChar("when");		
		case set_type:
			return SymChar("set");
		default:
			symchar *sym = GetName(t);
			if(sym==NULL){
				throw runtime_error("Unnamed type");
			}
			return SymChar(SCP(sym));
	}
}

const int
Type::getParameterCount() const{
	return GetModelParameterCount(t);
}

const TypeDescription *
Type::getInternalType() const{
	return t;
}

const Dimensions
Type::getDimensions() const{
	if( isRefinedConstant() ){
		return Dimensions( GetConstantDimens(getInternalType()) );
	}else if( isRefinedReal() ){
		return Dimensions( GetRealDimens(getInternalType()) );
	}else{
	if( !isRefinedAtom() )throw runtime_error("Type::getDimensions: called with non-atom type");
		throw runtime_error("Type::getDimensions: unrecognised type");
	}
}

const bool
Type::isRefinedAtom() const{
	return BaseTypeIsAtomic(t);
}

const bool
Type::isRefinedReal() const{
	return BaseTypeIsReal(t);
}

const bool
Type::isRefinedConstant() const{
	return BaseTypeIsConstant(t);
}

/**
	Instantiate a type. This *can be* expensive, if you have selected to 
	compile your model into C-code and load a dynamic library with native
	machine-code versions of your equations.

	Once you have an instance of your model, you can start
	to eliminate variables and attempt to solve it, see Instanc.

	Note that there is some kind of dastardly underhand reference to the
	Compiler class implicit here: the model instantiation call refers to
	g_use_copyanon which is sort-of owned by the Compiler class.
*/
Simulation
Type::getSimulation(const SymChar &sym
		, const bool rundefaultmethod
){
	/* notify the compiler of our bintoken options, if nec */
	Compiler::instance()->sendBinaryCompilationOptions();

#if 1
	/* removing the following line causes a crash on Windows 7 */
	//ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Starting tree...\n");
	error_reporter_tree_t *tree1 = error_reporter_tree_start(0);
	/* ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Started tree\n"); */
#endif

	Instance *i = SimsCreateInstance(getInternalType()->name, sym.getInternalType(), e_normal, NULL);
	Simulation sim(i,sym);

#if 1
	bool has_error = FALSE;
	if(error_reporter_tree_has_error(tree1)){
		has_error = TRUE;
	}

	error_reporter_tree_end(tree1);
	if(has_error){

		stringstream ss;
		ss << "Error(s) during instantiation of type '" << getName() << "'";
		throw runtime_error(ss.str());
	}/*else{
		ERROR_REPORTER_HERE(ASC_USER_NOTE,"Instantiated %s",SCP(getInternalType()->name));
	}*/
#endif

#if 1
	//CONSOLE_DEBUG("CHECKING INSTANCE...");
	sim.checkInstance(5);
	//CONSOLE_DEBUG("...DONE CHECKING INSTANCE");
#endif

#if 0
	CONSOLE_DEBUG("CHECKING TOKENS...");
	sim.checkTokens();
	CONSOLE_DEBUG("...DONE CHECKING TOKENS");
#endif

#if 0
	CONSOLE_DEBUG("CHECKING STATISTICS...");
	sim.checkStatistics();
	CONSOLE_DEBUG("...DONE CHECKING STATISTICS");
#endif

	if(i==NULL){
		throw runtime_error("Failed to create instance");
	}

	if(rundefaultmethod){
		//CONSOLE_DEBUG("RUNNING DEFAULT METHOD");
		sim.runDefaultMethod();
	}

	return sim;
}

vector<Method>
Type::getMethods() const{
	vector<Method> v;
	struct gl_list_t *l = GetInitializationList(getInternalType());
	if(l==NULL) return v;
	for(int i=1, end=gl_length(l); i<=end; ++i){
		v.push_back(Method((struct InitProcedure *)gl_fetch(l,i)));
	}
	return v;
}

Method
Type::getMethod(const SymChar &name) const{
	if(GetBaseType(t)!=model_type){
		stringstream ss;
		ss << "Type '" << getName() << "' is not a MODEL";
		throw runtime_error(ss.str());
	}

	struct InitProcedure *m;
	m = FindMethod(t,name.getInternalType());

	if(m==NULL){
		stringstream ss;
		ss << "No method named '" << name << "' in type '" << getName() << "'";
		throw runtime_error(ss.str());
		return NULL;
	}

	return Method(m);
}	

const bool
Type::isRefinedSolverVar() const{
	const TypeDescription *solver_var_type;
	Type t1 = Library().findType(SymChar("solver_var"));
	solver_var_type=t1.getInternalType();

	if(MoreRefined(t, solver_var_type)==t){
		//cerr << getName() << " IS A REFINED SOLVER_VAR" << endl;
		return true;
	}
	//cerr << getName() << "IS *NOT* A REFINED SOLVER_VAR" << endl;
	return false;
}

const bool
Type::isFundamental() const{
	return CheckFundamental(getName().getInternalType());
}

const bool
Type::isModel() const{
	return GetBaseType(t) == model_type;
}

const bool
Type::hasParameters() const{
	return GetModelParameterCount(t) > 0;
}

bool
Type::operator<(const Type &other) const{
	// modelled on the Unit_CmpAtomName function from UnitsProc.c in Tcl code...
	if(!getInternalType() || !other.getInternalType() || this->isFundamental()){
		return false;
	}
	return (this->getName() < other.getName());
}

Module
Type::getModule() const{
	return GetModule(getInternalType());
}

const Type &
Type::findMember(const SymChar &name){
	
	unsigned long pos;
	ChildListPtr CL;

	CL = GetChildList(t);
	pos = ChildPos(CL,name.getInternalType());
	
  unsigned long clsize = ChildListLen(CL);
  
  if((pos<1) || (pos>clsize))
  {
    stringstream ss;
		ss << "Library::findType: type '" << name << "' not found in library";
		throw runtime_error(ss.str());
  }
	
	const TypeDescription *t = ChildBaseTypePtr(CL,pos);

	if(t==NULL){
		stringstream ss;
		ss << "Library::findType: type '" << name << "' not found in library";
		throw runtime_error(ss.str());
	}
	Type *t2=new Type(t);
	return *t2;
}