1 |
/* ASCEND modelling environment |
2 |
Copyright (C) 2008 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 |
Wrapper for helmholtz.c to allow access to the routine from ASCEND. |
20 |
*/ |
21 |
|
22 |
/* include the external function API from libascend... */ |
23 |
#include <compiler/extfunc.h> |
24 |
|
25 |
/* include error reporting API as well, so we can send messages to user */ |
26 |
#include <utilities/error.h> |
27 |
|
28 |
/* for accessing the DATA instance */ |
29 |
#include <compiler/child.h> |
30 |
#include <general/list.h> |
31 |
#include <compiler/module.h> |
32 |
#include <compiler/childinfo.h> |
33 |
#include <compiler/parentchild.h> |
34 |
#include <compiler/slist.h> |
35 |
#include <compiler/type_desc.h> |
36 |
#include <compiler/packages.h> |
37 |
#include <compiler/symtab.h> |
38 |
#include <compiler/instquery.h> |
39 |
#include <compiler/instmacro.h> |
40 |
#include <compiler/instance_types.h> |
41 |
|
42 |
/* the code that we're wrapping... */ |
43 |
#include "helmholtz.h" |
44 |
|
45 |
/* for the moment, species data are defined in C code, we'll implement something |
46 |
better later on, hopefully. */ |
47 |
#include "ammonia.h" |
48 |
#include "nitrogen.h" |
49 |
|
50 |
#ifndef ASC_EXPORT |
51 |
# error "Where is ASC_EXPORT?" |
52 |
#endif |
53 |
|
54 |
|
55 |
/*------------------------------------------------------------------------------ |
56 |
FORWARD DECLARATIONS |
57 |
*/ |
58 |
|
59 |
ExtBBoxInitFunc helmholtz_prepare; |
60 |
ExtBBoxFunc helmholtz_p_calc; |
61 |
ExtBBoxFunc helmholtz_u_calc; |
62 |
ExtBBoxFunc helmholtz_h_calc; |
63 |
|
64 |
/*------------------------------------------------------------------------------ |
65 |
GLOBALS |
66 |
*/ |
67 |
|
68 |
/* place to store symbols needed for accessing ASCEND's instance tree */ |
69 |
static symchar *helmholtz_symbols[1]; |
70 |
#define COMPONENT_SYM helmholtz_symbols[0] |
71 |
|
72 |
static const char *helmholtz_p_help = "Calculate pressure from temperature and density, using Helmholtz fundamental correlation"; |
73 |
static const char *helmholtz_u_help = "Calculate specific internal energy from temperature and density, using Helmholtz fundamental correlation"; |
74 |
static const char *helmholtz_h_help = "Calculate specific enthalpy from temperature and density, using Helmholtz fundamental correlation"; |
75 |
|
76 |
/*------------------------------------------------------------------------------ |
77 |
REGISTRATION FUNCTION |
78 |
*/ |
79 |
|
80 |
/** |
81 |
This is the function called from "IMPORT helmholtz" |
82 |
|
83 |
It sets up the functions contained in this external library |
84 |
*/ |
85 |
extern |
86 |
ASC_EXPORT int helmholtz_register(){ |
87 |
int result = 0; |
88 |
|
89 |
ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Initialising HELMHOLTZ...\n"); |
90 |
|
91 |
#define CALCFN(NAME,INPUTS,OUTPUTS) \ |
92 |
result += CreateUserFunctionBlackBox(#NAME \ |
93 |
, helmholtz_prepare \ |
94 |
, NAME##_calc /* value */ \ |
95 |
, (ExtBBoxFunc*)NULL /* derivatives not provided yet*/ \ |
96 |
, (ExtBBoxFunc*)NULL /* hessian not provided yet */ \ |
97 |
, (ExtBBoxFinalFunc*)NULL /* finalisation not implemented */ \ |
98 |
, INPUTS,OUTPUTS /* inputs, outputs */ \ |
99 |
, NAME##_help /* help text */ \ |
100 |
, 0.0 \ |
101 |
) /* returns 0 on success */ |
102 |
|
103 |
CALCFN(helmholtz_p,2,1); |
104 |
CALCFN(helmholtz_u,2,1); |
105 |
CALCFN(helmholtz_h,2,1); |
106 |
|
107 |
#undef CALCFN |
108 |
|
109 |
if(result){ |
110 |
ERROR_REPORTER_HERE(ASC_PROG_NOTE,"CreateUserFunction result = %d\n",result); |
111 |
} |
112 |
return result; |
113 |
} |
114 |
|
115 |
/** |
116 |
'helmholtz_prepare' just gets the data member and checks that it's |
117 |
valid, and stores it in the blackbox data field. |
118 |
*/ |
119 |
int helmholtz_prepare(struct BBoxInterp *bbox, |
120 |
struct Instance *data, |
121 |
struct gl_list_t *arglist |
122 |
){ |
123 |
struct Instance *compinst; |
124 |
const char *comp; |
125 |
|
126 |
helmholtz_symbols[0] = AddSymbol("component"); |
127 |
|
128 |
/* get the data file name (we will look for this file in the ASCENDLIBRARY path) */ |
129 |
compinst = ChildByChar(data,COMPONENT_SYM); |
130 |
if(!compinst){ |
131 |
ERROR_REPORTER_HERE(ASC_USER_ERROR |
132 |
,"Couldn't locate 'component' in DATA, please check usage of HELMHOLTZ." |
133 |
); |
134 |
return 1; |
135 |
} |
136 |
if(InstanceKind(compinst)!=SYMBOL_CONSTANT_INST){ |
137 |
ERROR_REPORTER_HERE(ASC_USER_ERROR,"DATA member 'component' must be a symbol_constant"); |
138 |
return 1; |
139 |
} |
140 |
comp = SCP(SYMC_INST(compinst)->value); |
141 |
CONSOLE_DEBUG("COMPONENT: %s",comp); |
142 |
if(comp==NULL || strlen(comp)==0){ |
143 |
ERROR_REPORTER_HERE(ASC_USER_ERROR,"'component' is NULL or empty"); |
144 |
return 1; |
145 |
} |
146 |
|
147 |
if(strcmp(comp,"ammonia")==0){ |
148 |
bbox->user_data = (void*)&helmholtz_data_ammonia; |
149 |
}else if(strcmp(comp,"nitrogen")==0){ |
150 |
bbox->user_data = (void*)&helmholtz_data_nitrogen; |
151 |
}else{ |
152 |
ERROR_REPORTER_HERE(ASC_USER_ERROR,"Component must be 'ammonia' or 'nitrogen' at this stage (only two components supported)"); |
153 |
} |
154 |
|
155 |
ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Prepared component '%s' OK.\n",comp); |
156 |
return 0; |
157 |
} |
158 |
|
159 |
/*------------------------------------------------------------------------------ |
160 |
EVALULATION ROUTINES |
161 |
*/ |
162 |
|
163 |
#define CALCPREPARE \ |
164 |
/* a few checks about the input requirements */ \ |
165 |
if(ninputs != 2)return -1; \ |
166 |
if(noutputs != 1)return -2; \ |
167 |
if(inputs==NULL)return -3; \ |
168 |
if(outputs==NULL)return -4; \ |
169 |
if(bbox==NULL)return -5; \ |
170 |
\ |
171 |
/* the 'user_data' in the black box object will contain the */\ |
172 |
/* coefficients required for this fluid; cast it to the required form: */\ |
173 |
HelmholtzData *helmholtz_data = (HelmholtzData *)bbox->user_data |
174 |
|
175 |
/** |
176 |
Evaluation function for 'helmholtz_p'. |
177 |
@param jacobian ignored |
178 |
@return 0 on success |
179 |
*/ |
180 |
int helmholtz_p_calc(struct BBoxInterp *bbox, |
181 |
int ninputs, int noutputs, |
182 |
double *inputs, double *outputs, |
183 |
double *jacobian |
184 |
){ |
185 |
CALCPREPARE; |
186 |
|
187 |
/* first input is temperature, second is molar density */ |
188 |
outputs[0] = helmholtz_p(inputs[0], inputs[1], helmholtz_data); |
189 |
|
190 |
/* no need to worry about error states etc. */ |
191 |
return 0; |
192 |
} |
193 |
|
194 |
|
195 |
/** |
196 |
Evaluation function for 'helmholtz_u' |
197 |
@param jacobian ignored |
198 |
@return 0 on success |
199 |
*/ |
200 |
int helmholtz_u_calc(struct BBoxInterp *bbox, |
201 |
int ninputs, int noutputs, |
202 |
double *inputs, double *outputs, |
203 |
double *jacobian |
204 |
){ |
205 |
CALCPREPARE; |
206 |
|
207 |
/* first input is temperature, second is molar density */ |
208 |
outputs[0] = helmholtz_u(inputs[0], inputs[1], helmholtz_data); |
209 |
|
210 |
/* no need to worry about error states etc. */ |
211 |
return 0; |
212 |
} |
213 |
|
214 |
|
215 |
/** |
216 |
Evaluation function for 'helmholtz_h' |
217 |
@param jacobian ignored |
218 |
@return 0 on success |
219 |
*/ |
220 |
int helmholtz_h_calc(struct BBoxInterp *bbox, |
221 |
int ninputs, int noutputs, |
222 |
double *inputs, double *outputs, |
223 |
double *jacobian |
224 |
){ |
225 |
CALCPREPARE; |
226 |
|
227 |
/* first input is temperature, second is molar density */ |
228 |
outputs[0] = helmholtz_h(inputs[0], inputs[1], helmholtz_data); |
229 |
|
230 |
/* no need to worry about error states etc. */ |
231 |
return 0; |
232 |
} |
233 |
|
234 |
|
235 |
|
236 |
|