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 |
/* http://ascend4.org/b567, sim_destroy crash (seen at r4354 in trunk). */ |
142 |
static void test_bug567(void){ |
143 |
|
144 |
Asc_CompilerInit(1); |
145 |
CU_TEST(0 == Asc_PutEnv(ASC_ENV_LIBRARY "=models")); |
146 |
CU_TEST(0 == Asc_PutEnv(ASC_ENV_SOLVERS "=solvers/qrslv")); |
147 |
char *lib = Asc_GetEnv(ASC_ENV_LIBRARY); |
148 |
CONSOLE_DEBUG("%s = %s\n",ASC_ENV_LIBRARY,lib); |
149 |
ASC_FREE(lib); |
150 |
|
151 |
package_load("qrslv",NULL); |
152 |
|
153 |
/* load the file */ |
154 |
const char *path = "models/test/bug567/combinedcycle_fprops.a4c"; |
155 |
int status; |
156 |
Asc_OpenModule(path,&status); |
157 |
CU_ASSERT(status == 0); |
158 |
if(status){ |
159 |
Asc_CompilerDestroy(); |
160 |
CU_FAIL_FATAL(failed to load module); |
161 |
} |
162 |
|
163 |
/* parse it */ |
164 |
CU_ASSERT(0 == zz_parse()); |
165 |
|
166 |
/* find the model */ |
167 |
const char *simtype = "combinedcycle_toluene"; |
168 |
CU_ASSERT(FindType(AddSymbol(simtype))!=NULL); |
169 |
|
170 |
/* instantiate it */ |
171 |
struct Instance *siminst = SimsCreateInstance(AddSymbol(simtype), AddSymbol("sim1"), e_normal, NULL); |
172 |
CU_ASSERT_FATAL(siminst!=NULL); |
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 |
/* http://ascend4.org/b564 error in solution of model; the |
184 |
solver causes fixed variables to change to crazy values, looks like a memory |
185 |
management problem of some sort. */ |
186 |
static void test_bug564(void){ |
187 |
|
188 |
Asc_CompilerInit(1); |
189 |
CU_TEST(0 == Asc_PutEnv(ASC_ENV_LIBRARY "=models")); |
190 |
CU_TEST(0 == Asc_PutEnv(ASC_ENV_SOLVERS "=solvers/qrslv")); |
191 |
char *lib = Asc_GetEnv(ASC_ENV_LIBRARY); |
192 |
CONSOLE_DEBUG("%s = %s\n",ASC_ENV_LIBRARY,lib); |
193 |
ASC_FREE(lib); |
194 |
|
195 |
package_load("qrslv",NULL); |
196 |
|
197 |
/* load the file */ |
198 |
const char *path = "models/johnpye/fprops/brayton_split_salt.a4c"; |
199 |
{ |
200 |
int status; |
201 |
Asc_OpenModule(path,&status); |
202 |
CU_ASSERT(status == 0); |
203 |
if(status){ |
204 |
Asc_CompilerDestroy(); |
205 |
CU_FAIL_FATAL(failed to load module); |
206 |
} |
207 |
} |
208 |
|
209 |
/* parse it */ |
210 |
CU_ASSERT(0 == zz_parse()); |
211 |
|
212 |
/* find the model */ |
213 |
const char *simtype = "brayton_split_salt_co2"; |
214 |
CU_ASSERT(FindType(AddSymbol(simtype))!=NULL); |
215 |
|
216 |
/* instantiate it */ |
217 |
struct Instance *siminst = SimsCreateInstance(AddSymbol(simtype), AddSymbol("sim1"), e_normal, NULL); |
218 |
CU_ASSERT_FATAL(siminst!=NULL); |
219 |
|
220 |
CONSOLE_DEBUG("RUNNING ON_LOAD"); |
221 |
|
222 |
/** Call on_load */ |
223 |
struct Name *name = CreateIdName(AddSymbol("on_load")); |
224 |
enum Proc_enum pe = Initialize(GetSimulationRoot(siminst),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL); |
225 |
CU_ASSERT(pe==Proc_all_ok); |
226 |
|
227 |
/* assign solver */ |
228 |
const char *solvername = "QRSlv"; |
229 |
int index = slv_lookup_client(solvername); |
230 |
slv_status_t status; |
231 |
CU_ASSERT_FATAL(index != -1); |
232 |
|
233 |
slv_system_t sys = system_build(GetSimulationRoot(siminst)); |
234 |
CU_ASSERT_FATAL(sys != NULL); |
235 |
|
236 |
CU_ASSERT_FATAL(slv_select_solver(sys,index)); |
237 |
CONSOLE_DEBUG("Assigned solver '%s'...",solvername); |
238 |
|
239 |
CU_ASSERT_FATAL(0 == slv_presolve(sys)); |
240 |
|
241 |
slv_get_status(sys, &status); |
242 |
CU_ASSERT_FATAL(status.ready_to_solve); |
243 |
|
244 |
slv_solve(sys); |
245 |
slv_get_status(sys, &status); |
246 |
CU_ASSERT(status.ok); |
247 |
|
248 |
CONSOLE_DEBUG("Destroying system..."); |
249 |
if(sys)system_destroy(sys); |
250 |
system_free_reused_mem(); |
251 |
|
252 |
/* run 'self_test' method */ |
253 |
CONSOLE_DEBUG("Running self-tests"); |
254 |
name = CreateIdName(AddSymbol("self_test")); |
255 |
pe = Initialize(GetSimulationRoot(siminst),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL); |
256 |
CU_ASSERT(pe==Proc_all_ok); |
257 |
|
258 |
CONSOLE_DEBUG("Destroy solver engines"); |
259 |
solver_destroy_engines(); |
260 |
|
261 |
/* destroy all that stuff */ |
262 |
CONSOLE_DEBUG("Destroying instance tree"); |
263 |
CU_ASSERT(siminst != NULL); |
264 |
|
265 |
Asc_CompilerDestroy(); |
266 |
} |
267 |
|
268 |
/*===========================================================================*/ |
269 |
/* Registration information */ |
270 |
|
271 |
#define TESTS1(T,X) \ |
272 |
T(bug513_no_simplify) \ |
273 |
X T(bug513_simplify) \ |
274 |
X T(bug567) \ |
275 |
X T(bug564) |
276 |
|
277 |
#define X |
278 |
#define TESTS(T) TESTS1(T,X) |
279 |
|
280 |
REGISTER_TESTS_SIMPLE(solver_qrslv, TESTS) |
281 |
#undef X |
282 |
|