/[ascend]/trunk/ascend/solver/test/test_qrslv.c
ViewVC logotype

Contents of /trunk/ascend/solver/test/test_qrslv.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2640 - (show annotations) (download) (as text)
Sun Sep 30 12:36:07 2012 UTC (10 years, 4 months ago) by jpye
File MIME type: text/x-csrc
File size: 6465 byte(s)
don't create name string unless needed
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
44 #include <test/common.h>
45
46 /*
47 Test solving a simple QRSlv model
48 */
49 static void test_qrslv(const char *filenamestem, int simplify){
50
51 Asc_CompilerInit(simplify);
52 CU_TEST(0 == Asc_PutEnv(ASC_ENV_LIBRARY "=models"));
53 CU_TEST(0 == Asc_PutEnv(ASC_ENV_SOLVERS "=solvers/qrslv"));
54 char *lib = Asc_GetEnv(ASC_ENV_LIBRARY);
55 CONSOLE_DEBUG("%s = %s\n",ASC_ENV_LIBRARY,lib);
56 ASC_FREE(lib);
57
58 package_load("qrslv",NULL);
59
60 /* load the file */
61 char path[PATH_MAX];
62 strcpy((char *)path,"test/qrslv/");
63 strncat(path, filenamestem, PATH_MAX - strlen(path));
64 strncat(path, ".a4c", PATH_MAX - strlen(path));
65 {
66 int status;
67 Asc_OpenModule(path,&status);
68 CU_ASSERT(status == 0);
69 if(status){
70 Asc_CompilerDestroy();
71 CU_FAIL_FATAL(failed to load module);
72 }
73 }
74
75 /* parse it */
76 CU_ASSERT(0 == zz_parse());
77
78 /* find the model */
79 CU_ASSERT(FindType(AddSymbol(filenamestem))!=NULL);
80
81 /* instantiate it */
82 struct Instance *siminst = SimsCreateInstance(AddSymbol(filenamestem), AddSymbol("sim1"), e_normal, NULL);
83 CU_ASSERT_FATAL(siminst!=NULL);
84
85 CONSOLE_DEBUG("RUNNING ON_LOAD");
86
87 /** Call on_load */
88 struct Name *name = CreateIdName(AddSymbol("on_load"));
89 enum Proc_enum pe = Initialize(GetSimulationRoot(siminst),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL);
90 CU_ASSERT(pe==Proc_all_ok);
91
92 /* assign solver */
93 const char *solvername = "QRSlv";
94 int index = slv_lookup_client(solvername);
95 CU_ASSERT_FATAL(index != -1);
96
97 slv_system_t sys = system_build(GetSimulationRoot(siminst));
98 CU_ASSERT_FATAL(sys != NULL);
99
100 CU_ASSERT_FATAL(slv_select_solver(sys,index));
101 CONSOLE_DEBUG("Assigned solver '%s'...",solvername);
102
103 CU_ASSERT_FATAL(0 == slv_presolve(sys));
104
105 slv_status_t status;
106 slv_get_status(sys, &status);
107 CU_ASSERT_FATAL(status.ready_to_solve);
108
109 slv_solve(sys);
110
111 slv_get_status(sys, &status);
112 CU_ASSERT(status.ok);
113
114 CONSOLE_DEBUG("Destroying system...");
115 if(sys)system_destroy(sys);
116 system_free_reused_mem();
117
118 /* run 'self_test' method */
119 CONSOLE_DEBUG("Running self-tests");
120 name = CreateIdName(AddSymbol("self_test"));
121 pe = Initialize(GetSimulationRoot(siminst),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL);
122 CU_ASSERT(pe==Proc_all_ok);
123
124 /* destroy all that stuff */
125 CONSOLE_DEBUG("Destroying instance tree");
126 CU_ASSERT(siminst != NULL);
127
128 solver_destroy_engines();
129 sim_destroy(siminst);
130 Asc_CompilerDestroy();
131 }
132
133 static void test_bug513_simplify(void){
134 test_qrslv("bug513",1);
135 }
136
137 static void test_bug513_no_simplify(void){
138 test_qrslv("bug513",0);
139 }
140
141
142 /* test for mysterious crash during solution (64-bit) which also results in
143 side-effect of FIXed values being changed by the solver (32-bit)! */
144
145 static void test_bug564(void){
146
147 Asc_CompilerInit(1);
148 CU_TEST(0 == Asc_PutEnv(ASC_ENV_LIBRARY "=models"));
149 CU_TEST(0 == Asc_PutEnv(ASC_ENV_SOLVERS "=solvers/qrslv"));
150 char *lib = Asc_GetEnv(ASC_ENV_LIBRARY);
151 CONSOLE_DEBUG("%s = %s\n",ASC_ENV_LIBRARY,lib);
152 ASC_FREE(lib);
153
154 package_load("qrslv",NULL);
155
156 /* load the file */
157 const char *path = "models/johnpye/fprops/brayton_split.a4c";
158 {
159 int status;
160 Asc_OpenModule(path,&status);
161 CU_ASSERT(status == 0);
162 if(status){
163 Asc_CompilerDestroy();
164 CU_FAIL_FATAL(failed to load module);
165 }
166 }
167
168 /* parse it */
169 CU_ASSERT(0 == zz_parse());
170
171 /* find the model */
172 const char *simtype = "brayton_split_co2";
173 CU_ASSERT(FindType(AddSymbol(simtype))!=NULL);
174
175 /* instantiate it */
176 struct Instance *siminst = SimsCreateInstance(AddSymbol(simtype), AddSymbol("sim1"), e_normal, NULL);
177 CU_ASSERT_FATAL(siminst!=NULL);
178
179 #if 0
180 CONSOLE_DEBUG("RUNNING ON_LOAD");
181
182 /** Call on_load */
183 struct Name *name = CreateIdName(AddSymbol("on_load"));
184 enum Proc_enum pe = Initialize(GetSimulationRoot(siminst),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL);
185 CU_ASSERT(pe==Proc_all_ok);
186
187 /* assign solver */
188 const char *solvername = "QRSlv";
189 int index = slv_lookup_client(solvername);
190 slv_status_t status;
191 CU_ASSERT_FATAL(index != -1);
192
193 slv_system_t sys = system_build(GetSimulationRoot(siminst));
194 CU_ASSERT_FATAL(sys != NULL);
195
196 CU_ASSERT_FATAL(slv_select_solver(sys,index));
197 CONSOLE_DEBUG("Assigned solver '%s'...",solvername);
198
199 CU_ASSERT_FATAL(0 == slv_presolve(sys));
200
201 slv_get_status(sys, &status);
202 CU_ASSERT_FATAL(status.ready_to_solve);
203
204 slv_solve(sys);
205 slv_get_status(sys, &status);
206 CU_ASSERT(status.ok);
207
208 CONSOLE_DEBUG("Destroying system...");
209 if(sys)system_destroy(sys);
210 system_free_reused_mem();
211
212 /* run 'self_test' method */
213 CONSOLE_DEBUG("Running self-tests");
214 name = CreateIdName(AddSymbol("self_test"));
215 pe = Initialize(GetSimulationRoot(siminst),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL);
216 CU_ASSERT(pe==Proc_all_ok);
217 #endif
218
219 /* destroy all that stuff */
220 CONSOLE_DEBUG("Destroying instance tree");
221 CU_ASSERT(siminst != NULL);
222
223 solver_destroy_engines();
224 sim_destroy(siminst);
225 Asc_CompilerDestroy();
226
227 }
228
229 /*===========================================================================*/
230 /* Registration information */
231
232 #define TESTS1(T,X) \
233 T(bug513_no_simplify) \
234 X T(bug513_simplify) \
235 X T(bug564)
236
237 #define X
238 #define TESTS(T) TESTS1(T,X)
239
240 REGISTER_TESTS_SIMPLE(solver_qrslv, TESTS)
241 #undef X
242

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