1 |
/* ASCEND modelling environment |
2 |
Copyright (C) 1996-2007 Carnegie Mellon University |
3 |
|
4 |
This program is free software; you can redistribute it and/or modify |
5 |
it under the terms of the GNU General Public License as published by |
6 |
the Free Software Foundation; either version 2, or (at your option) |
7 |
any later version. |
8 |
|
9 |
This program is distributed in the hope that it will be useful, |
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
GNU General Public License for more details. |
13 |
|
14 |
You should have received a copy of the GNU General Public License |
15 |
along with this program; if not, write to the Free Software |
16 |
Foundation, Inc., 59 Temple Place - Suite 330, |
17 |
Boston, MA 02111-1307, USA. |
18 |
*//** @file |
19 |
|
20 |
A little EXTERNAL method for solving models. Useful for cases where |
21 |
model initialisation is a bit tricky. See also models/johnpye/extpy and |
22 |
models/johnpye/solve.py. |
23 |
|
24 |
Should just work: |
25 |
|
26 |
IMPORT "sensitivity/solve"; |
27 |
: : |
28 |
MODEL mymodel; |
29 |
: : |
30 |
METHODS |
31 |
: : |
32 |
METHOD mysolve; |
33 |
EXTERNAL solve(SELF); |
34 |
END mysolve; |
35 |
: : |
36 |
END mymodel; |
37 |
|
38 |
Sliced out of sensitivity.c because it's independent code. |
39 |
*/ |
40 |
|
41 |
#include <math.h> |
42 |
|
43 |
#include <packages/sensitivity.h> |
44 |
#include <compiler/instquery.h> |
45 |
#include <compiler/atomvalue.h> |
46 |
#include <utilities/ascMalloc.h> |
47 |
#include <compiler/extfunc.h> |
48 |
#include <general/mathmacros.h> |
49 |
|
50 |
ExtMethodRun do_solve_eval; |
51 |
ASC_EXPORT int solve_register(void); |
52 |
|
53 |
/** |
54 |
Build then presolve the solve an instance... |
55 |
*/ |
56 |
int DoSolve(struct Instance *inst){ |
57 |
slv_system_t sys; |
58 |
|
59 |
sys = system_build(inst); |
60 |
if (!sys) { |
61 |
ERROR_REPORTER_HERE(ASC_PROG_ERR,"Failed to build system"); |
62 |
return 1; |
63 |
} |
64 |
(void)slv_select_solver(sys,0); |
65 |
slv_presolve(sys); |
66 |
slv_solve(sys); |
67 |
system_destroy(sys); |
68 |
return 0; |
69 |
} |
70 |
|
71 |
/** |
72 |
Calls 'DoSolve' |
73 |
|
74 |
@see DoSolve |
75 |
*/ |
76 |
int do_solve_eval( struct Instance *i, |
77 |
struct gl_list_t *arglist, void *user_data |
78 |
){ |
79 |
unsigned long len; |
80 |
int result; |
81 |
struct Instance *inst; |
82 |
len = gl_length(arglist); |
83 |
|
84 |
(void)i; /* not used */ |
85 |
|
86 |
if (len!=1) { |
87 |
ERROR_REPORTER_HERE(ASC_USER_ERROR,"Wrong number of args in (expected 1, got %d)",len); |
88 |
return 1; |
89 |
} |
90 |
inst = FetchElement(arglist,1,1); |
91 |
if (!inst) |
92 |
return 1; |
93 |
result = DoSolve(inst); |
94 |
return result; |
95 |
} |
96 |
|
97 |
|
98 |
#if 0 |
99 |
static int ReSolve(slv_system_t sys) |
100 |
{ |
101 |
if (!sys) |
102 |
return 1; |
103 |
slv_solve(sys); |
104 |
return 0; |
105 |
} |
106 |
#endif |
107 |
|
108 |
/** Registration function */ |
109 |
int solve_register(void){ |
110 |
int result; |
111 |
result = CreateUserFunctionMethod("do_solve", |
112 |
do_solve_eval, |
113 |
1,NULL,NULL,NULL |
114 |
); |
115 |
return result; |
116 |
} |