/[ascend]/trunk/runqrslv.c
ViewVC logotype

Contents of /trunk/runqrslv.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2874 - (show annotations) (download) (as text)
Thu Mar 26 08:22:56 2015 UTC (2 years, 6 months ago) by jpye
File MIME type: text/x-csrc
File size: 6126 byte(s)
update test names.
commenting about compiler.h functions.
revised comment about building runqrslv.

1 /**
2 A minimal driver for ASCEND, using QRSlv. Console-based; no GUI. This can
3 be useful for checking low-level features in ASCEND, or working around
4 problems with reporting of errors/messages etc from the GUI.
5
6 See also our CUnit test suites eg in the directory ascend/solver/test.
7 See also some C++ driver code in directory ascxx. See also
8 ascend/solver/slv_interface.c for another 'lite' command-line interface, no
9 longer maintained but possibly of interest/useful.
10
11 Starting in the directory where this file is located, it can be compiled
12 using
13 gcc -I. -orunqrslv runqrslv.c -L. -lascend
14 providing ASCEND itself has already been compiled
15 (see http://ascend4.org/Building_ASCEND for details on how to do that)
16 */
17 #include <string.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <assert.h>
21
22 #include <ascend/utilities/config.h>
23
24 #include <ascend/general/env.h>
25 #include <ascend/general/ospath.h>
26 #include <ascend/general/list.h>
27 #include <ascend/general/ltmatrix.h>
28
29 #include <ascend/general/platform.h>
30 #include <ascend/utilities/ascEnvVar.h>
31 #include <ascend/utilities/error.h>
32
33 /* FIXME check whether we need all these includes? */
34 #include <ascend/compiler/module.h>
35 #include <ascend/compiler/parser.h>
36 #include <ascend/compiler/instquery.h>
37 #include <ascend/compiler/parentchild.h>
38 #include <ascend/compiler/atomvalue.h>
39 #include <ascend/compiler/relation_io.h>
40 //#include <ascend/compiler/reverse_ad.h>
41 //#include <ascend/compiler/relation_util.h>
42 //#include <ascend/compiler/mathinst.h>
43 //#include <ascend/compiler/watchpt.h>
44 #include <ascend/compiler/name.h>
45 #include <ascend/compiler/visitinst.h>
46 #include <ascend/compiler/functype.h>
47 #include <ascend/compiler/safe.h>
48 #include <ascend/compiler/qlfdid.h>
49 #include <ascend/compiler/instance_io.h>
50 #include <ascend/compiler/initialize.h>
51 #include <ascend/compiler/symtab.h>
52 #include <ascend/compiler/library.h>
53 #include <ascend/compiler/simlist.h>
54 #include <ascend/compiler/ascCompiler.h>
55 #include <ascend/compiler/packages.h>
56
57 #include <ascend/compiler/slvreq.h>
58
59 #include <ascend/system/system.h>
60 #include <ascend/system/slv_client.h>
61 #include <ascend/solver/solver.h>
62 #include <ascend/system/slv_server.h>
63
64 void usage(char *n){
65 fprintf(stderr,"%s FILENAME\n",n);
66 fprintf(stderr,
67 " Simple ascend driver\n"
68 " Loads and parses the model file FILENAME (eg path/to/myfile.a4c)\n"
69 " then attempts to instantiate the model with the name 'myfile'.\n"
70 " If the model file loads, it runs the 'on_load' method, then attempts\n"
71 " to solve the model using the default QRSlv solver. If it solves,\n"
72 " then the method 'self_test' will be run, which can be used to check\n"
73 " whether the expected results were found via ASSERT statements in that\n"
74 " method.\n");
75 }
76
77 int main(int argc, char *argv[]){
78 char env1[2*PATH_MAX];
79 int status;
80 int qrslv_index;
81 int simplify = 1;
82
83 /* get the filename from the commandline */
84 if(argc<2){
85 usage(argv[0]);
86 return 1;
87 }
88 const char *modelfile = argv[1];
89 const char *librarypath = ".:models";
90
91 /* get the file stem as the model name we'll look for */
92 struct FilePath *fp = ospath_new(modelfile);
93 char *modelname = ospath_getfilestem(fp);
94
95 /* initialise the compiler from scratch */
96 Asc_CompilerInit(simplify);
97
98 /* set the needed environment variables so that models, solvers can be found */
99 snprintf(env1,2*PATH_MAX,ASC_ENV_LIBRARY "=%s",librarypath);
100 assert(0 == Asc_PutEnv(env1));
101 assert(0 == Asc_PutEnv(ASC_ENV_SOLVERS "=solvers/qrslv"));
102 /* read back and display the ASCENDLIBRARY setting */
103 char *lib = Asc_GetEnv(ASC_ENV_LIBRARY);
104 CONSOLE_DEBUG("%s = %s",ASC_ENV_LIBRARY,lib);
105 ASC_FREE(lib);
106
107 /* load the QRSlv solver, presumably from the ASCENDSOLVERS path */
108 package_load("qrslv",NULL);
109 qrslv_index = slv_lookup_client("QRSlv");
110 assert(qrslv_index != -1);
111
112 /* load the model file */
113 Asc_OpenModule(modelfile,&status);
114 assert(status == 0);
115
116 /* parse it */
117 assert(0 == zz_parse());
118 /* FIXME, somehow this is failing to detect parser errors; we need to review
119 this assertion */
120
121 /* find the model */
122 assert(NULL != FindType(AddSymbol(modelname)));
123
124 /* instantiate it */
125 struct Instance *siminst = SimsCreateInstance(AddSymbol(modelname), AddSymbol("sim1"), e_normal, NULL);
126 assert(siminst!=NULL);
127
128 /* call the on_load method */
129 CONSOLE_DEBUG("RUNNING METHOD 'on_load'");
130 struct Name *name = CreateIdName(AddSymbol("on_load"));
131 /* TODO do we check that the method exists first? */
132 /* TODO, note that we have not hooked 'slvreq', here, so any calls to SOLVE, OPTION, SOLVER will fail */
133 enum Proc_enum pe = Initialize(GetSimulationRoot(siminst),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL);
134 assert(pe==Proc_all_ok);
135
136 /* 'build' the 'system' -- the flattened system of equations */
137 slv_system_t sys = system_build(GetSimulationRoot(siminst));
138 assert(sys != NULL);
139
140 /* assign the solver to the system */
141 CONSOLE_DEBUG("Assigning solver");
142 assert(slv_select_solver(sys,qrslv_index));
143 CONSOLE_DEBUG("Assigned solver '%s'...",slv_solver_name(slv_get_selected_solver(sys)));
144
145 /* presolve, check it's ready, then solve */
146 CONSOLE_DEBUG("Presolve...");
147 assert(0 == slv_presolve(sys));
148 slv_status_t status1;
149 slv_get_status(sys, &status1);
150 assert(status1.ready_to_solve);
151 CONSOLE_DEBUG("Solve...");
152 slv_solve(sys);
153 /* check that solver status was 'ok' */
154 slv_get_status(sys, &status1);
155 assert(status1.ok);
156
157 /* clean up the 'system' -- we don't need that any more */
158 CONSOLE_DEBUG("Destroying system...");
159 if(sys)system_destroy(sys);
160 system_free_reused_mem();
161
162 /* run 'self_test' method -- we can check there that the results are as expected */
163 CONSOLE_DEBUG("Running self-tests");
164 name = CreateIdName(AddSymbol("self_test"));
165 pe = Initialize(GetSimulationRoot(siminst),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL);
166 assert(pe==Proc_all_ok);
167
168 /* destroy the compiler data structures, hopefully all dynamically allocated memory */
169 CONSOLE_DEBUG("Destroying instance tree");
170 assert(siminst != NULL);
171 solver_destroy_engines();
172 sim_destroy(siminst);
173 Asc_CompilerDestroy();
174
175 /* clean up some locally declared vars */
176 ASC_FREE(modelname);
177 }
178

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