1 |
#include <string.h> |
2 |
#include <stdlib.h> |
3 |
#include <stdio.h> |
4 |
#include <unistd.h> |
5 |
|
6 |
#include <ascend/general/env.h> |
7 |
#include <ascend/general/ospath.h> |
8 |
#include <ascend/general/list.h> |
9 |
#include <ascend/general/ltmatrix.h> |
10 |
|
11 |
#include <ascend/general/platform.h> |
12 |
#include <ascend/utilities/ascEnvVar.h> |
13 |
#include <ascend/utilities/error.h> |
14 |
|
15 |
#include <ascend/compiler/ascCompiler.h> |
16 |
#include <ascend/compiler/module.h> |
17 |
#include <ascend/compiler/parser.h> |
18 |
#include <ascend/compiler/library.h> |
19 |
#include <ascend/compiler/symtab.h> |
20 |
#include <ascend/compiler/simlist.h> |
21 |
#include <ascend/compiler/instquery.h> |
22 |
#include <ascend/compiler/parentchild.h> |
23 |
#include <ascend/compiler/atomvalue.h> |
24 |
#include <ascend/compiler/relation_io.h> |
25 |
#include <ascend/compiler/reverse_ad.h> |
26 |
#include <ascend/compiler/relation_util.h> |
27 |
#include <ascend/compiler/mathinst.h> |
28 |
#include <ascend/compiler/watchpt.h> |
29 |
#include <ascend/compiler/initialize.h> |
30 |
#include <ascend/compiler/name.h> |
31 |
#include <ascend/compiler/visitinst.h> |
32 |
#include <ascend/compiler/functype.h> |
33 |
#include <ascend/compiler/safe.h> |
34 |
#include <ascend/compiler/qlfdid.h> |
35 |
#include <ascend/compiler/instance_io.h> |
36 |
#include <ascend/compiler/packages.h> |
37 |
|
38 |
#include <ascend/compiler/slvreq.h> |
39 |
|
40 |
#include <ascend/system/system.h> |
41 |
#include <ascend/system/slv_client.h> |
42 |
#include <ascend/solver/solver.h> |
43 |
#include <ascend/system/slv_server.h> |
44 |
#include <ascend/system/slv_param.h> |
45 |
#include <ascend/integrator/integrator.h> |
46 |
|
47 |
#include <test/common.h> |
48 |
#include <test/test_globals.h> |
49 |
|
50 |
#ifndef PI |
51 |
# define PI 3.14159265358979 |
52 |
#endif |
53 |
|
54 |
/* a simple integrator reporter for testing */ |
55 |
static int test_ida_reporter_init(struct IntegratorSystemStruct *integ) { |
56 |
return 0; |
57 |
} |
58 |
|
59 |
static int test_ida_reporter_write(struct IntegratorSystemStruct *integ) { |
60 |
double val; |
61 |
val = var_value(integ->y[0]); |
62 |
CONSOLE_DEBUG("y[0] = %g", val); |
63 |
return 0; /* no interrupt */ |
64 |
} |
65 |
|
66 |
static int test_ida_reporter_writeobs(struct IntegratorSystemStruct *integ) { |
67 |
CONSOLE_DEBUG("x = %f", var_value(integ->x)); |
68 |
return 0; |
69 |
} |
70 |
|
71 |
static int test_ida_reporter_close(struct IntegratorSystemStruct *integ) { |
72 |
return 0; |
73 |
} |
74 |
|
75 |
static IntegratorReporter test_ida_reporter = { test_ida_reporter_init, |
76 |
test_ida_reporter_write, test_ida_reporter_writeobs, |
77 |
test_ida_reporter_close }; |
78 |
|
79 |
static void test_shmder(){ |
80 |
|
81 |
Asc_CompilerInit(1); |
82 |
|
83 |
/* set paths relative to test executable */ |
84 |
Asc_PutEnv(ASC_ENV_LIBRARY "=models"); |
85 |
Asc_PutEnv(ASC_ENV_SOLVERS "=solvers/ida" OSPATH_DIV "solvers/lsode" OSPATH_DIV "solvers/qrslv"); |
86 |
|
87 |
/* load the file */ |
88 |
#define FILESTEM "shm_der" |
89 |
char path[PATH_MAX] = "ksenija/" FILESTEM ".a4c"; |
90 |
{ |
91 |
int status; |
92 |
Asc_OpenModule(path, &status); |
93 |
CU_ASSERT_FATAL(status == 0); |
94 |
} |
95 |
|
96 |
/* parse it */ |
97 |
CU_ASSERT(0 == zz_parse()); |
98 |
|
99 |
/* find the model */ |
100 |
CU_ASSERT(FindType(AddSymbol(FILESTEM))!=NULL); |
101 |
|
102 |
/* instantiate it */ |
103 |
struct Instance *siminst = SimsCreateInstance(AddSymbol(FILESTEM), AddSymbol("sim1"), e_normal, NULL); |
104 |
CU_ASSERT_FATAL(siminst!=NULL); |
105 |
#undef FILESTEM |
106 |
|
107 |
CONSOLE_DEBUG("RUNNING ON_LOAD"); |
108 |
|
109 |
/** Call on_load */ |
110 |
struct Name *name = CreateIdName(AddSymbol("on_load")); |
111 |
enum Proc_enum pe = Initialize(GetSimulationRoot(siminst),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL); |
112 |
CU_ASSERT(pe==Proc_all_ok); |
113 |
|
114 |
/* create the integrator */ |
115 |
|
116 |
g_use_dersyntax = 1; |
117 |
slv_system_t sys = system_build(GetSimulationRoot(siminst)); |
118 |
CU_ASSERT_FATAL(sys != NULL); |
119 |
|
120 |
IntegratorSystem *integ = integrator_new(sys,siminst); |
121 |
|
122 |
CU_ASSERT_FATAL(0 == integrator_set_engine(integ,"IDA")); |
123 |
CONSOLE_DEBUG("Assigned integrator '%s'...",integ->internals->name); |
124 |
|
125 |
slv_parameters_t p; |
126 |
CU_ASSERT(0 == integrator_params_get(integ,&p)); |
127 |
/* TODO set some parameters? */ |
128 |
|
129 |
/* perform problem analysis */ |
130 |
CU_ASSERT_FATAL(0 == integrator_analyse(integ)); |
131 |
|
132 |
/* TODO assign an integrator reporter */ |
133 |
integrator_set_reporter(integ, &test_ida_reporter); |
134 |
|
135 |
integrator_set_minstep(integ,0.0001); |
136 |
integrator_set_maxstep(integ,0.1); |
137 |
integrator_set_stepzero(integ,0.001); |
138 |
integrator_set_maxsubsteps(integ,200); |
139 |
|
140 |
/* set a linearly-distributed samplelist */ |
141 |
double start = 0, end = PI; |
142 |
int num = 20; |
143 |
dim_type d; |
144 |
SetDimFraction(d,D_TIME,CreateFraction(1,1)); |
145 |
SampleList *samplelist = samplelist_new(num+1, &d); |
146 |
double val = start; |
147 |
double inc = (end-start)/(num); |
148 |
unsigned long i; |
149 |
for(i=0; i<=num; ++i){ |
150 |
samplelist_set(samplelist,i,val); |
151 |
val += inc; |
152 |
} |
153 |
integrator_set_samples(integ,samplelist); |
154 |
|
155 |
CU_ASSERT_FATAL(0 == integrator_solve(integ, 0, samplelist_length(samplelist)-1)); |
156 |
|
157 |
integrator_free(integ); |
158 |
samplelist_free(samplelist); |
159 |
|
160 |
CU_ASSERT_FATAL(NULL != sys); |
161 |
system_destroy(sys); |
162 |
system_free_reused_mem(); |
163 |
|
164 |
struct Instance *simroot = GetSimulationRoot(siminst); |
165 |
CU_TEST(simroot != NULL); |
166 |
struct Instance *ix = ChildByChar(simroot,AddSymbol("x")); |
167 |
struct Instance *iv = ChildByChar(simroot,AddSymbol("v")); |
168 |
CU_TEST(ix != NULL); |
169 |
CU_TEST(iv != NULL); |
170 |
|
171 |
CONSOLE_DEBUG("Final x = %e",RealAtomValue(ix)); |
172 |
CONSOLE_DEBUG("Final v = %e",RealAtomValue(iv)); |
173 |
|
174 |
CU_TEST(fabs(RealAtomValue(ix) - 10 < 2e-3)); |
175 |
CU_TEST(fabs(RealAtomValue(iv) - 0 < 4e-4)); |
176 |
|
177 |
/* destroy all that stuff */ |
178 |
CU_ASSERT(siminst != NULL); |
179 |
|
180 |
solver_destroy_engines(); |
181 |
integrator_free_engines(); |
182 |
sim_destroy(siminst); |
183 |
Asc_CompilerDestroy(); |
184 |
} |
185 |
|
186 |
static void test_boundaryder(){ |
187 |
Asc_CompilerInit(1); |
188 |
|
189 |
/* set paths relative to test executable */ |
190 |
Asc_PutEnv(ASC_ENV_LIBRARY "=models"); |
191 |
Asc_PutEnv(ASC_ENV_SOLVERS "=solvers/ida" OSPATH_DIV "solvers/lrslv"); |
192 |
CU_TEST_FATAL(0 == package_load("lrslv",NULL)); |
193 |
|
194 |
/* load the file */ |
195 |
char path[PATH_MAX]; |
196 |
strcpy((char *) path, "ksenija/"); |
197 |
#define FILESTEM "bouncingball_der" |
198 |
strncat(path, FILESTEM, PATH_MAX - strlen(path)); |
199 |
strncat(path, ".a4c", PATH_MAX - strlen(path)); |
200 |
{ |
201 |
int status; |
202 |
Asc_OpenModule(path, &status); |
203 |
CU_ASSERT_FATAL(status == 0); |
204 |
} |
205 |
|
206 |
/* parse it */ |
207 |
CU_ASSERT(0 == zz_parse()); |
208 |
|
209 |
/* find the model */ |
210 |
CU_ASSERT(FindType(AddSymbol(FILESTEM))!=NULL); |
211 |
|
212 |
/* instantiate it */ |
213 |
struct Instance *siminst = SimsCreateInstance(AddSymbol(FILESTEM), |
214 |
AddSymbol("sim1"), e_normal, NULL); |
215 |
CU_ASSERT_FATAL(siminst!=NULL); |
216 |
#undef FILESTEM |
217 |
|
218 |
CONSOLE_DEBUG("RUNNING ON_LOAD"); |
219 |
|
220 |
/** Call on_load */ |
221 |
struct Name *name = CreateIdName(AddSymbol("on_load")); |
222 |
enum Proc_enum pe = Initialize(GetSimulationRoot(siminst), name, "sim1", |
223 |
ASCERR, WP_STOPONERR, NULL, NULL); |
224 |
CU_ASSERT(pe==Proc_all_ok); |
225 |
|
226 |
g_use_dersyntax = 1; |
227 |
slv_system_t sys = system_build(GetSimulationRoot(siminst)); |
228 |
CU_ASSERT_FATAL(sys != NULL); |
229 |
|
230 |
/* create the integrator */ |
231 |
|
232 |
IntegratorSystem *integ = integrator_new(sys,GetSimulationRoot(siminst)); |
233 |
|
234 |
CU_ASSERT_FATAL(0 == integrator_set_engine(integ,"IDA")); |
235 |
CONSOLE_DEBUG("Assigned integrator '%s'...",integ->internals->name); |
236 |
|
237 |
slv_parameters_t p; |
238 |
CU_ASSERT(0 == integrator_params_get(integ,&p)); |
239 |
/* TODO set some parameters? */ |
240 |
|
241 |
/* perform problem analysis */ |
242 |
CU_ASSERT_FATAL(0 == integrator_analyse(integ)); |
243 |
|
244 |
integrator_set_reporter(integ, &test_ida_reporter); |
245 |
|
246 |
integrator_set_minstep(integ, .01); |
247 |
integrator_set_maxstep(integ, 1); |
248 |
integrator_set_stepzero(integ, .001); |
249 |
integrator_set_maxsubsteps(integ, 200); |
250 |
|
251 |
/* set a linearly-distributed samplelist */ |
252 |
double start = 0, end = 30; |
253 |
int num = 100; |
254 |
dim_type d; |
255 |
SetDimFraction(d,D_TIME,CreateFraction(1,1)); |
256 |
SampleList *samplelist = samplelist_new(num + 1, &d); |
257 |
double val = start; |
258 |
double inc = (end - start) / (num); |
259 |
unsigned long i; |
260 |
for (i = 0; i <= num; ++i) { |
261 |
samplelist_set(samplelist, i, val); |
262 |
val += inc; |
263 |
} |
264 |
integrator_set_samples(integ, samplelist); |
265 |
|
266 |
CU_ASSERT_FATAL(0 == integrator_solve(integ, 0, samplelist_length(samplelist)-1)); |
267 |
|
268 |
integrator_free(integ); |
269 |
samplelist_free(samplelist); |
270 |
|
271 |
CU_ASSERT_FATAL(NULL != sys); |
272 |
system_destroy(sys); |
273 |
system_free_reused_mem(); |
274 |
|
275 |
struct Instance *simroot = GetSimulationRoot(siminst); |
276 |
CU_TEST(simroot != NULL); |
277 |
struct Instance *iy = ChildByChar(simroot,AddSymbol("y")); |
278 |
CU_TEST(iy != NULL); |
279 |
|
280 |
CONSOLE_DEBUG("Final y = %e",RealAtomValue(iy)); |
281 |
|
282 |
CU_TEST(fabs(RealAtomValue(iy) - 10.10353 < 2e-3)); |
283 |
|
284 |
/* destroy all that stuff */ |
285 |
CONSOLE_DEBUG("Destroying instance tree"); |
286 |
CU_ASSERT(siminst != NULL); |
287 |
|
288 |
solver_destroy_engines(); |
289 |
integrator_free_engines(); |
290 |
sim_destroy(siminst); |
291 |
Asc_CompilerDestroy(); |
292 |
} |
293 |
|
294 |
|
295 |
|
296 |
/*===========================================================================*/ |
297 |
/* Registration information */ |
298 |
|
299 |
#define TESTS(T) \ |
300 |
T(shmder) \ |
301 |
T(boundaryder) |
302 |
|
303 |
REGISTER_TESTS_SIMPLE(integrator_idader, TESTS) |
304 |
|