/[ascend]/trunk/ascend/integrator/test/test_lsode.c
ViewVC logotype

Contents of /trunk/ascend/integrator/test/test_lsode.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2552 - (show annotations) (download) (as text)
Thu Feb 2 20:54:40 2012 UTC (6 years, 7 months ago) by jpye
File MIME type: text/x-csrc
File size: 5389 byte(s)
Fix detection of SUNDIALS in MinGW-w64.
Fix paths in test_ida and test_lsode.
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include <ascend/general/env.h>
6 #include <ascend/general/ospath.h>
7 #include <ascend/general/list.h>
8 #include <ascend/general/ltmatrix.h>
9
10 #include <ascend/general/platform.h>
11 #include <ascend/utilities/ascEnvVar.h>
12 #include <ascend/utilities/error.h>
13
14 #include <ascend/compiler/ascCompiler.h>
15 #include <ascend/compiler/module.h>
16 #include <ascend/compiler/parser.h>
17 #include <ascend/compiler/library.h>
18 #include <ascend/compiler/symtab.h>
19 #include <ascend/compiler/simlist.h>
20 #include <ascend/compiler/instquery.h>
21 #include <ascend/compiler/parentchild.h>
22 #include <ascend/compiler/atomvalue.h>
23 #include <ascend/compiler/relation_io.h>
24 #include <ascend/compiler/reverse_ad.h>
25 #include <ascend/compiler/relation_util.h>
26 #include <ascend/compiler/mathinst.h>
27 #include <ascend/compiler/watchpt.h>
28 #include <ascend/compiler/initialize.h>
29 #include <ascend/compiler/name.h>
30 #include <ascend/compiler/visitinst.h>
31 #include <ascend/compiler/functype.h>
32 #include <ascend/compiler/safe.h>
33 #include <ascend/compiler/qlfdid.h>
34 #include <ascend/compiler/instance_io.h>
35 #include <ascend/compiler/packages.h>
36
37 #include <ascend/compiler/slvreq.h>
38
39 #include <ascend/system/system.h>
40 #include <ascend/system/slv_client.h>
41 #include <ascend/solver/solver.h>
42 #include <ascend/system/slv_server.h>
43 #include <ascend/system/slv_param.h>
44 #include <ascend/integrator/integrator.h>
45
46 #include <test/common.h>
47
48 /* a simple integrator reporter for testing */
49 static int test_lsode_reporter_init(struct IntegratorSystemStruct *integ){
50 return 0;
51 }
52
53 static int test_lsode_reporter_write(struct IntegratorSystemStruct *integ){
54 return 0; /* no interrupt */
55 }
56
57 static int test_lsode_reporter_writeobs(struct IntegratorSystemStruct *integ){
58 CONSOLE_DEBUG("x = %f", var_value(integ->x));
59 return 0;
60 }
61
62 static int test_lsode_reporter_close(struct IntegratorSystemStruct *integ){
63 return 0;
64 }
65
66 static IntegratorReporter test_lsode_reporter = {
67 test_lsode_reporter_init
68 ,test_lsode_reporter_write
69 ,test_lsode_reporter_writeobs
70 ,test_lsode_reporter_close
71 };
72
73 /*
74 Test solving a simple IPOPT model
75 */
76 static void test_bounds(){
77 Asc_CompilerInit(1);
78 CU_TEST(0 == Asc_PutEnv(ASC_ENV_LIBRARY "=models"));
79 CU_TEST(0 == Asc_PutEnv(ASC_ENV_SOLVERS "=solvers/qrslv" OSPATH_DIV "solvers/lsode"));
80 char *lib = Asc_GetEnv(ASC_ENV_SOLVERS);
81 CONSOLE_DEBUG("%s = %s\n",ASC_ENV_SOLVERS,lib);
82 ASC_FREE(lib);
83
84 CU_TEST_FATAL(0 == package_load("qrslv",NULL));
85
86 /* load the file */
87 char path[PATH_MAX];
88 strcpy((char *)path,"test/lsode/");
89 #define FILESTEM "bounds"
90 strncat(path, FILESTEM, PATH_MAX - strlen(path));
91 strncat(path, ".a4c", PATH_MAX - strlen(path));
92 {
93 int status;
94 Asc_OpenModule(path,&status);
95 CU_ASSERT_FATAL(status == 0);
96 }
97
98 /* parse it */
99 CU_ASSERT(0 == zz_parse());
100
101 /* find the model */
102 #define MODELNAME "boundsfail"
103 CU_ASSERT(FindType(AddSymbol(MODELNAME))!=NULL);
104
105 /* instantiate it */
106 struct Instance *siminst = SimsCreateInstance(AddSymbol(MODELNAME), AddSymbol("sim1"), e_normal, NULL);
107 CU_ASSERT_FATAL(siminst!=NULL);
108
109 CONSOLE_DEBUG("RUNNING ON_LOAD");
110
111 /** Call on_load */
112 struct Name *name = CreateIdName(AddSymbol("on_load"));
113 enum Proc_enum pe = Initialize(GetSimulationRoot(siminst),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL);
114 CU_ASSERT(pe==Proc_all_ok);
115
116 /* assign solver */
117 const char *solvername = "QRSlv";
118 int index = slv_lookup_client(solvername);
119 CU_ASSERT_FATAL(index != -1);
120
121 slv_system_t sys = system_build(GetSimulationRoot(siminst));
122 CU_ASSERT_FATAL(sys != NULL);
123
124 CU_ASSERT_FATAL(slv_select_solver(sys,index));
125 CONSOLE_DEBUG("Assigned solver '%s'...",solvername);
126
127 /* create the integrator */
128
129 IntegratorSystem *integ = integrator_new(sys,siminst);
130
131 CU_ASSERT_FATAL(0 == integrator_set_engine(integ,"LSODE"));
132 CONSOLE_DEBUG("Assigned integrator '%s'...",integ->internals->name);
133
134 slv_parameters_t p;
135 CU_ASSERT(0 == integrator_params_get(integ,&p));
136 /* TODO set some parameters? */
137
138 /* perform problem analysis */
139 CU_ASSERT_FATAL(0 == integrator_analyse(integ));
140
141 CONSOLE_DEBUG("Assigning reporter and step sizes...");
142 integrator_set_reporter(integ, &test_lsode_reporter);
143 integrator_set_minstep(integ,0);
144 integrator_set_maxstep(integ,0);
145 integrator_set_stepzero(integ,0);
146 integrator_set_maxsubsteps(integ,0);
147
148 /* set a linearly-distributed samplelist */
149 CONSOLE_DEBUG("Preparing samplelist...");
150 double start = 0, end = 10;
151 int num = 20;
152 dim_type d;
153 SetDimFraction(d,D_TIME,CreateFraction(1,1));
154 SampleList *samplelist = samplelist_new(num+1, &d);
155 double val = start;
156 double inc = (end-start)/(num);
157 unsigned long i;
158 for(i=0; i<=num; ++i){
159 samplelist_set(samplelist,i,val);
160 val += inc;
161 }
162 integrator_set_samples(integ,samplelist);
163
164 CONSOLE_DEBUG("Commencing solve...");
165 CU_ASSERT_FATAL(0 == integrator_solve(integ, 0, samplelist_length(samplelist)-1));
166
167 integrator_free(integ);
168 samplelist_free(samplelist);
169
170 CU_ASSERT_FATAL(NULL != sys);
171 system_destroy(sys);
172 system_free_reused_mem();
173
174 /* destroy all that stuff */
175 CONSOLE_DEBUG("Destroying instance tree");
176 CU_ASSERT(siminst != NULL);
177
178 solver_destroy_engines();
179 sim_destroy(siminst);
180 Asc_CompilerDestroy();
181 }
182
183 /*===========================================================================*/
184 /* Registration information */
185
186 #define TESTS(T) \
187 T(bounds)
188
189 REGISTER_TESTS_SIMPLE(integrator_lsode, TESTS)
190

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