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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2660 - (show annotations) (download) (as text)
Wed Jan 16 05:57:03 2013 UTC (5 years, 10 months ago) by jpye
File MIME type: text/x-csrc
File size: 7388 byte(s)
Working on updating rankine_fprops and associated models to work with new fprops2 code.
Some issue discovered with (p,h) for water (added python/solve_ph1.py to check it).
Next cunit tests to drive some ASCEND models embedding FPROPS (note use of slvreq for this).
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
46
47 typedef struct SlvReqC_struct{
48 struct Instance *siminst;
49 slv_system_t sys;
50 } SlvReqC;
51
52 static SlvReqSetSolverFn slvreq_c_set_solver;
53 static SlvReqSetOptionFn slvreq_c_set_option;
54 static SlvReqDoSolveFn slvreq_c_do_solve;
55
56 /*
57 This function actually does the job of setting the solver in our little
58 'test' simulation environment.
59 */
60 int slvreq_c_set_solver(const char *solvername, void *user_data){
61 SlvReqC *S = (SlvReqC *)user_data;
62 int index = slv_lookup_client(solvername);
63 if(index == -1){
64 CONSOLE_DEBUG("Failed to locate solver '%s'",solvername);
65 return 1;
66 }
67
68 if(S->sys == NULL){
69 CONSOLE_DEBUG("Building system...");
70 S->sys = system_build(GetSimulationRoot(S->siminst));
71 }
72
73 if(slv_select_solver(S->sys,index) == -1){
74 CONSOLE_DEBUG("Failed to select solver '%s' (solver was found, though)",solvername);
75 return 2;
76 }
77 CONSOLE_DEBUG("Assigned solver '%s'...",solvername);
78 return 0;
79 }
80
81 /*
82 This function actually does the job of setting solver options in our little
83 'test' simulation environment.
84 */
85 int slvreq_c_set_option(const char *optionname, struct value_t *val, void *user_data){
86 SlvReqC *S = (SlvReqC *)user_data;
87 slv_parameters_t pp;
88 if(S->sys == NULL){
89 /* no solver has been assigned so the system isn't built*/
90 return SLVREQ_OPTIONS_UNAVAILABLE;
91 }
92 slv_get_parameters(S->sys, &pp);
93 /* look for the parameter by name */
94 int i, index = -1;
95 for(i=0;i<pp.num_parms;++i){
96 if(strcmp(pp.parms[i].name,optionname)==0){
97 index = i;
98 }
99 }
100 if(index == -1){
101 /* not found */
102 return SLVREQ_INVALID_OPTION_NAME;
103 }
104 /* parameter has been found */
105 switch(SLV_PARAM_TYPE(&pp,index)){
106 case int_parm:
107 if(ValueKind(*val)!=integer_value){
108 CONSOLE_DEBUG("Wrong parameter type: expecting integer type");
109 return SLVREQ_WRONG_OPTION_VALUE_TYPE;
110 }
111 CONSOLE_DEBUG("Setting %s = %ld...",optionname, IntegerValue(*val));
112 SLV_PARAM_INT(&pp,index) = IntegerValue(*val);
113 break;
114 case bool_parm:
115 if(ValueKind(*val)!=boolean_value){
116 CONSOLE_DEBUG("Wrong parameter type: expecting boolean type");
117 return SLVREQ_WRONG_OPTION_VALUE_TYPE;
118 }
119 CONSOLE_DEBUG("Setting %s = %s...",optionname, BooleanValue(*val)?"TRUE":"FALSE");
120 SLV_PARAM_BOOL(&pp,index) = BooleanValue(*val);
121 break;
122 case real_parm:
123 if(ValueKind(*val)!=real_value){
124 CONSOLE_DEBUG("Wrong parameter type: expecting real type");
125 return SLVREQ_WRONG_OPTION_VALUE_TYPE;
126 }
127 CONSOLE_DEBUG("Setting %s = %f...",optionname, RealValue(*val));
128 SLV_PARAM_REAL(&pp,index) = RealValue(*val);
129 break;
130 case char_parm:
131 if(ValueKind(*val)!=symbol_value){
132 CONSOLE_DEBUG("Wrong parameter type: expecting string (i.e. symbol) type");
133 return SLVREQ_WRONG_OPTION_VALUE_TYPE;
134 }
135 CONSOLE_DEBUG("Setting %s = '%s'...",optionname, SCP(SymbolValue(*val)));
136 slv_set_char_parameter(&(SLV_PARAM_CHAR(&pp,index)),SCP(SymbolValue(*val)));
137 break;
138 }
139
140 slv_set_parameters(S->sys, &pp);
141
142 return 0;
143 }
144
145 /*
146 This function actually does the job of running solver in our little
147 'test' simulation environment, and reporting the result.
148 */
149 int slvreq_c_do_solve(struct Instance *instance, void *user_data){
150 SlvReqC *S = (SlvReqC *)user_data;
151 int res;
152 if(S->sys==NULL)return SLVREQ_NO_SOLVER_SELECTED;
153
154 res = slv_presolve(S->sys);
155 if(res)return SLVREQ_PRESOLVE_FAIL;
156
157 slv_status_t status;
158
159 slv_get_status(S->sys, &status);
160 if(status.ready_to_solve){
161 res = slv_solve(S->sys);
162 if(res){
163 CONSOLE_DEBUG("slv_solve returned %d",res);
164 }
165 }
166
167 slv_get_status(S->sys, &status);
168 if(status.ok){
169 CONSOLE_DEBUG("Solver completed OK");
170 return 0;
171 }
172
173 if(status.diverged)CONSOLE_DEBUG("Solver diverged");
174 if(status.inconsistent)CONSOLE_DEBUG("System is inconsistent");
175 if(status.iteration_limit_exceeded)CONSOLE_DEBUG("Solver exceeded iteration limit");
176 if(status.calc_ok != 0)CONSOLE_DEBUG("Solver had residual calculation errors");
177 if(status.over_defined)CONSOLE_DEBUG("Solver system is over-defined");
178 if(status.under_defined)CONSOLE_DEBUG("Solver system is under-defined");
179 if(status.iteration_limit_exceeded)CONSOLE_DEBUG("Solver exceeded time limit");
180
181 return SLVREQ_SOLVE_FAIL;
182 }
183
184
185
186 /*
187 Test that the slvreq mechanism works within the C layer
188 */
189 static void test_fprops_model(const char *modelname){
190 int status;
191
192 Asc_CompilerInit(1);
193 Asc_PutEnv(ASC_ENV_LIBRARY "=models");
194 Asc_PutEnv(ASC_ENV_SOLVERS "=solvers/qrslv");
195 char *lib = Asc_GetEnv(ASC_ENV_SOLVERS);
196 CONSOLE_DEBUG("%s = %s\n",ASC_ENV_SOLVERS,lib);
197 ASC_FREE(lib);
198
199 /* load the file */
200 Asc_OpenModule("test/fprops/fprops_test_base.a4c",&status);
201 CU_ASSERT(status == 0);
202
203 /* parse it */
204 CU_ASSERT(0 == zz_parse());
205
206 /* find the model */
207 CU_ASSERT(FindType(AddSymbol(modelname))!=NULL);
208
209 /* instantiate it */
210 SlvReqC S;
211 S.siminst = SimsCreateInstance(AddSymbol(modelname), AddSymbol("sim1"), e_normal, NULL);
212 CU_ASSERT_FATAL(S.siminst!=NULL);
213 #undef TESTFILE
214
215 /* do the solver hooks */
216 S.sys = NULL;
217 slvreq_assign_hooks(S.siminst, &slvreq_c_set_solver, &slvreq_c_set_option, &slvreq_c_do_solve, &S);
218
219 CONSOLE_DEBUG("RUNNING ON_LOAD");
220
221 /** Call on_load */
222 struct Name *name = CreateIdName(AddSymbol("on_load"));
223 enum Proc_enum pe = Initialize(GetSimulationRoot(S.siminst),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL);
224 CU_ASSERT(pe==Proc_all_ok);
225
226 /* assume that this clever model will have already solved and self-tested itself */
227
228 CONSOLE_DEBUG("Destroy solver engines");
229 solver_destroy_engines();
230
231 /* destroy all that stuff */
232 CONSOLE_DEBUG("Destroying instance tree");
233 CU_ASSERT(S.siminst != NULL);
234
235 Asc_CompilerDestroy();
236 }
237
238 /*===========================================================================*/
239 /* Registration information */
240
241
242 #define TESTS1(T,X) \
243 T(stream_state_test) \
244 X T(stream_state_test_co2) \
245 X T(stream_state_test_toluene) \
246 X T(pump_simple_test)
247
248 /* need a little function for each model we're testing... */
249 #define T(N) static void test_##N(void){\
250 test_fprops_model(#N);\
251 }
252 #define X
253 TESTS1(T,X)
254 #undef T
255 #undef X
256
257 #define X
258 #define TESTS(T) \
259 TESTS1(T,X)
260
261 REGISTER_TESTS_SIMPLE(solver_fprops, TESTS)
262 #undef X
263

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