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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2538 - (show annotations) (download) (as text)
Thu Jan 26 20:21:06 2012 UTC (12 years, 4 months ago) by jpye
File MIME type: text/x-csrc
File size: 6981 byte(s)
Fix Win64 issue with test solver_slvreq, relates to bug 501.
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
36 #include <ascend/compiler/slvreq.h>
37
38 #include <ascend/system/system.h>
39 #include <ascend/system/slv_client.h>
40 #include <ascend/solver/solver.h>
41 #include <ascend/system/slv_server.h>
42
43 #include <test/common.h>
44
45 typedef struct SlvReqC_struct{
46 struct Instance *siminst;
47 slv_system_t sys;
48 } SlvReqC;
49
50 SlvReqSetSolverFn slvreq_c_set_solver;
51 SlvReqSetOptionFn slvreq_c_set_option;
52 SlvReqDoSolveFn slvreq_c_do_solve;
53
54 /*
55 This function actually does the job of setting the solver in our little
56 'test' simulation environment.
57 */
58 int slvreq_c_set_solver(const char *solvername, void *user_data){
59 SlvReqC *S = (SlvReqC *)user_data;
60 int index = slv_lookup_client(solvername);
61 if(index == -1){
62 CONSOLE_DEBUG("Failed to locate solve '%s'",solvername);
63 return 1;
64 }
65
66 if(S->sys == NULL){
67 CONSOLE_DEBUG("Building system...");
68 S->sys = system_build(GetSimulationRoot(S->siminst));
69 }
70
71 if(slv_select_solver(S->sys,index) == -1){
72 CONSOLE_DEBUG("Failed to select solver '%s' (solver was found, though)",solvername);
73 return 2;
74 }
75 CONSOLE_DEBUG("Assigned solver '%s'...",solvername);
76 return 0;
77 }
78
79 /*
80 This function actually does the job of setting solver options in our little
81 'test' simulation environment.
82 */
83 int slvreq_c_set_option(const char *optionname, struct value_t *val, void *user_data){
84 SlvReqC *S = (SlvReqC *)user_data;
85 slv_parameters_t pp;
86 if(S->sys == NULL){
87 /* no solver has been assigned so the system isn't built*/
88 return SLVREQ_OPTIONS_UNAVAILABLE;
89 }
90 slv_get_parameters(S->sys, &pp);
91 /* look for the parameter by name */
92 int i, index = -1;
93 for(i=0;i<pp.num_parms;++i){
94 if(strcmp(pp.parms[i].name,optionname)==0){
95 index = i;
96 }
97 }
98 if(index == -1){
99 /* not found */
100 return SLVREQ_INVALID_OPTION_NAME;
101 }
102 /* parameter has been found */
103 switch(SLV_PARAM_TYPE(&pp,index)){
104 case int_parm:
105 if(ValueKind(*val)!=integer_value){
106 CONSOLE_DEBUG("Wrong parameter type: expecting integer type");
107 return SLVREQ_WRONG_OPTION_VALUE_TYPE;
108 }
109 CONSOLE_DEBUG("Setting %s = %ld...",optionname, IntegerValue(*val));
110 SLV_PARAM_INT(&pp,index) = IntegerValue(*val);
111 break;
112 case bool_parm:
113 if(ValueKind(*val)!=boolean_value){
114 CONSOLE_DEBUG("Wrong parameter type: expecting boolean type");
115 return SLVREQ_WRONG_OPTION_VALUE_TYPE;
116 }
117 CONSOLE_DEBUG("Setting %s = %s...",optionname, BooleanValue(*val)?"TRUE":"FALSE");
118 SLV_PARAM_BOOL(&pp,index) = BooleanValue(*val);
119 break;
120 case real_parm:
121 if(ValueKind(*val)!=real_value){
122 CONSOLE_DEBUG("Wrong parameter type: expecting real type");
123 return SLVREQ_WRONG_OPTION_VALUE_TYPE;
124 }
125 CONSOLE_DEBUG("Setting %s = %f...",optionname, RealValue(*val));
126 SLV_PARAM_REAL(&pp,index) = RealValue(*val);
127 break;
128 case char_parm:
129 if(ValueKind(*val)!=symbol_value){
130 CONSOLE_DEBUG("Wrong parameter type: expecting string (i.e. symbol) type");
131 return SLVREQ_WRONG_OPTION_VALUE_TYPE;
132 }
133 CONSOLE_DEBUG("Setting %s = '%s'...",optionname, SCP(SymbolValue(*val)));
134 slv_set_char_parameter(&(SLV_PARAM_CHAR(&pp,index)),SCP(SymbolValue(*val)));
135 break;
136 }
137
138 slv_set_parameters(S->sys, &pp);
139
140 return 0;
141 }
142
143 /*
144 This function actually does the job of running solver in our little
145 'test' simulation environment, and reporting the result.
146 */
147 int slvreq_c_do_solve(struct Instance *instance, void *user_data){
148 SlvReqC *S = (SlvReqC *)user_data;
149 int res;
150 if(S->sys==NULL)return SLVREQ_NO_SOLVER_SELECTED;
151
152 res = slv_presolve(S->sys);
153 if(res)return SLVREQ_PRESOLVE_FAIL;
154
155 slv_status_t status;
156
157 slv_get_status(S->sys, &status);
158 if(status.ready_to_solve){
159 res = slv_solve(S->sys);
160 if(res){
161 CONSOLE_DEBUG("slv_solve returned %d",res);
162 }
163 }
164
165 slv_get_status(S->sys, &status);
166 if(status.ok){
167 CONSOLE_DEBUG("Solver completed OK");
168 return 0;
169 }
170
171 if(status.diverged)CONSOLE_DEBUG("Solver diverged");
172 if(status.inconsistent)CONSOLE_DEBUG("System is inconsistent");
173 if(status.iteration_limit_exceeded)CONSOLE_DEBUG("Solver exceeded iteration limit");
174 if(status.calc_ok != 0)CONSOLE_DEBUG("Solver had residual calculation errors");
175 if(status.over_defined)CONSOLE_DEBUG("Solver system is over-defined");
176 if(status.under_defined)CONSOLE_DEBUG("Solver system is under-defined");
177 if(status.iteration_limit_exceeded)CONSOLE_DEBUG("Solver exceeded time limit");
178
179 return SLVREQ_SOLVE_FAIL;
180 }
181
182 /*
183 Test that the slvreq mechanism works within the C layer
184 */
185 static void test_slvreq_c(void){
186
187 struct module_t *m;
188 int status;
189
190 Asc_CompilerInit(1);
191 Asc_PutEnv(ASC_ENV_LIBRARY "=models");
192 Asc_PutEnv(ASC_ENV_SOLVERS "=solvers/qrslv");
193
194 /* load the file */
195 #define TESTFILE "test2"
196 m = Asc_OpenModule("test/slvreq/" TESTFILE ".a4c",&status);
197 CU_ASSERT(status == 0);
198
199 /* parse it */
200 CU_ASSERT(0 == zz_parse());
201
202 /* find the model */
203 CU_ASSERT(FindType(AddSymbol(TESTFILE))!=NULL);
204
205 /* instantiate it */
206 SlvReqC S;
207 S.siminst = SimsCreateInstance(AddSymbol(TESTFILE), AddSymbol("sim1"), e_normal, NULL);
208 CU_ASSERT_FATAL(S.siminst!=NULL);
209 #undef TESTFILE
210
211 /* do the solver hooks */
212 S.sys = NULL;
213 slvreq_assign_hooks(S.siminst, &slvreq_c_set_solver, &slvreq_c_set_option, &slvreq_c_do_solve, &S);
214
215 CONSOLE_DEBUG("RUNNING ON_LOAD");
216
217 /** Call on_load */
218 struct Name *name = CreateIdName(AddSymbol("on_load"));
219 enum Proc_enum pe = Initialize(GetSimulationRoot(S.siminst),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL);
220 CU_ASSERT(pe==Proc_all_ok);
221
222 /* maybe if the model is really clever, it will have solved itself now! */
223
224 /* all sorts of destruction */
225 CONSOLE_DEBUG("DESTROYING NOW...");
226 CU_ASSERT(NULL != S.siminst)
227 if(S.sys)system_destroy(S.sys);
228
229 system_free_reused_mem();
230 sim_destroy(S.siminst);
231 solver_destroy_engines();
232 Asc_CompilerDestroy();
233 }
234
235
236
237 /*===========================================================================*/
238 /* Registration information */
239
240 #define TESTS(T) \
241 T(slvreq_c)
242
243 REGISTER_TESTS_SIMPLE(solver_slvreq, TESTS)
244

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