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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2343 - (show annotations) (download) (as text)
Mon Dec 27 07:05:04 2010 UTC (13 years, 5 months ago) by jpye
File MIME type: text/x-csrc
File size: 4451 byte(s)
Added test solver_ipopt.formula, failing.
1 #include <CUnit/CUnit.h>
2
3 #include "test_slvreq.h"
4
5 #include <string.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8
9 #include <ascend/general/env.h>
10 #include <ascend/general/ospath.h>
11 #include <ascend/general/list.h>
12 #include <ascend/general/ltmatrix.h>
13
14 #include <ascend/general/platform.h>
15 #include <ascend/utilities/ascEnvVar.h>
16 #include <ascend/utilities/error.h>
17
18 #include <ascend/compiler/ascCompiler.h>
19 #include <ascend/compiler/module.h>
20 #include <ascend/compiler/parser.h>
21 #include <ascend/compiler/library.h>
22 #include <ascend/compiler/symtab.h>
23 #include <ascend/compiler/simlist.h>
24 #include <ascend/compiler/instquery.h>
25 #include <ascend/compiler/parentchild.h>
26 #include <ascend/compiler/atomvalue.h>
27 #include <ascend/compiler/relation_io.h>
28 #include <ascend/compiler/reverse_ad.h>
29 #include <ascend/compiler/relation_util.h>
30 #include <ascend/compiler/mathinst.h>
31 #include <ascend/compiler/watchpt.h>
32 #include <ascend/compiler/initialize.h>
33 #include <ascend/compiler/name.h>
34 #include <ascend/compiler/visitinst.h>
35 #include <ascend/compiler/functype.h>
36 #include <ascend/compiler/safe.h>
37 #include <ascend/compiler/qlfdid.h>
38 #include <ascend/compiler/instance_io.h>
39
40 #include <ascend/compiler/slvreq.h>
41
42 #include <ascend/system/system.h>
43 #include <ascend/system/slv_client.h>
44 #include <ascend/solver/solver.h>
45 #include <ascend/system/slv_server.h>
46
47 /*
48 Test solving a simple IPOPT model
49 */
50 static void test_ipopt(const char *filenamestem){
51
52 struct module_t *m;
53
54 Asc_CompilerInit(1);
55 Asc_PutEnv(ASC_ENV_LIBRARY "=models:solvers/conopt:solvers/qrslv:solvers/cmslv:solvers/ida:solvers/lsode:solvers/ipopt");
56
57 /* load the file */
58 char path[PATH_MAX];
59 strcpy((char *)path,"test/ipopt/");
60 strncat(path, filenamestem, PATH_MAX - strlen(path));
61 strncat(path, ".a4c", PATH_MAX - strlen(path));
62 {
63 int status;
64 m = Asc_OpenModule(path,&status);
65 CU_ASSERT(status == 0);
66 }
67
68 /* parse it */
69 CU_ASSERT(0 == zz_parse());
70
71 /* find the model */
72 CU_ASSERT(FindType(AddSymbol(filenamestem))!=NULL);
73
74 /* instantiate it */
75 struct Instance *siminst = SimsCreateInstance(AddSymbol(filenamestem), AddSymbol("sim1"), e_normal, NULL);
76 CU_ASSERT_FATAL(siminst!=NULL);
77
78 CONSOLE_DEBUG("RUNNING ON_LOAD");
79
80 /** Call on_load */
81 struct Name *name = CreateIdName(AddSymbol("on_load"));
82 enum Proc_enum pe = Initialize(GetSimulationRoot(siminst),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL);
83 CU_ASSERT(pe==Proc_all_ok);
84
85 /* assign solver */
86 const char *solvername = "IPOPT";
87 int index = slv_lookup_client(solvername);
88 CU_ASSERT_FATAL(index != -1);
89
90 slv_system_t sys = system_build(GetSimulationRoot(siminst));
91 CU_ASSERT_FATAL(sys != NULL);
92
93 CU_ASSERT_FATAL(slv_select_solver(sys,index));
94 CONSOLE_DEBUG("Assigned solver '%s'...",solvername);
95
96 CU_ASSERT_FATAL(0 == slv_presolve(sys));
97
98 slv_status_t status;
99 slv_get_status(sys, &status);
100 CU_ASSERT_FATAL(status.ready_to_solve);
101
102 slv_solve(sys);
103
104 slv_get_status(sys, &status);
105 CU_ASSERT(status.ok);
106
107 CONSOLE_DEBUG("Destroying system...");
108 if(sys)system_destroy(sys);
109 system_free_reused_mem();
110
111 /* run 'self_test' method */
112 CONSOLE_DEBUG("Running self-tests");
113 name = CreateIdName(AddSymbol("self_test"));
114 pe = Initialize(GetSimulationRoot(siminst),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL);
115 CU_ASSERT(pe==Proc_all_ok);
116
117 /* destroy all that stuff */
118 CONSOLE_DEBUG("Destroying instance tree");
119 CU_ASSERT(siminst != NULL);
120
121 solver_destroy_engines();
122
123 sim_destroy(siminst);
124 Asc_CompilerDestroy();
125 }
126
127 /*===========================================================================*/
128 /* Registration information */
129
130 #define TESTS(T,X) \
131 T(test2) \
132 X T(test5) \
133 X T(test6) \
134 X T(test7) \
135 X T(test8) \
136 X T(test9) \
137 X T(test10) \
138 X T(test11) \
139 X T(test12) \
140 X T(test13) \
141 X T(test14) \
142 X T(test15) \
143 X T(formula)
144
145 /* define the tests: each test loads the model, solves with IPOPT, then runs the
146 self_test method. */
147 #define T(N) static void test_ipopt_##N(void){\
148 test_ipopt(#N);\
149 }
150 #define X
151 TESTS(T,X)
152 #undef T
153 #undef X
154
155 /* register the tests to CUnit */
156 #define T(N) {#N, test_ipopt_##N }
157 #define X ,
158 static CU_TestInfo ipopt_test_list[] = {
159 TESTS(T,X)
160 , CU_TEST_INFO_NULL
161 };
162 #undef T
163 #undef X
164
165 static CU_SuiteInfo suites[] = {
166 {"solver_ipopt", NULL, NULL, ipopt_test_list},
167 CU_SUITE_INFO_NULL
168 };
169
170 /*-------------------------------------------------------------------*/
171 CU_ErrorCode test_register_solver_ipopt(void)
172 {
173 return CU_register_suites(suites);
174 }
175

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