/[ascend]/trunk/models/johnpye/fprops/asc_helmholtz.c
ViewVC logotype

Contents of /trunk/models/johnpye/fprops/asc_helmholtz.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1903 - (show annotations) (download) (as text)
Thu Sep 25 08:47:22 2008 UTC (15 years, 8 months ago) by jpye
File MIME type: text/x-csrc
File size: 8121 byte(s)
Expanded the ASCEND interface for fprops to include p,u,s,h,a.
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_s_calc;
63 ExtBBoxFunc helmholtz_h_calc;
64 ExtBBoxFunc helmholtz_a_calc;
65
66 /*------------------------------------------------------------------------------
67 GLOBALS
68 */
69
70 /* place to store symbols needed for accessing ASCEND's instance tree */
71 static symchar *helmholtz_symbols[1];
72 #define COMPONENT_SYM helmholtz_symbols[0]
73
74 static const char *helmholtz_p_help = "Calculate pressure from temperature and density, using Helmholtz fundamental correlation";
75 static const char *helmholtz_u_help = "Calculate specific internal energy from temperature and density, using Helmholtz fundamental correlation";
76 static const char *helmholtz_s_help = "Calculate specific entropy from temperature and density, using Helmholtz fundamental correlation";
77 static const char *helmholtz_h_help = "Calculate specific enthalpy from temperature and density, using Helmholtz fundamental correlation";
78 static const char *helmholtz_a_help = "Calculate specific Helmholtz energy from temperature and density, using Helmholtz fundamental correlation";
79
80 /*------------------------------------------------------------------------------
81 REGISTRATION FUNCTION
82 */
83
84 /**
85 This is the function called from "IMPORT helmholtz"
86
87 It sets up the functions contained in this external library
88 */
89 extern
90 ASC_EXPORT int helmholtz_register(){
91 int result = 0;
92
93 ERROR_REPORTER_HERE(ASC_USER_WARNING,"HELMHOLTZ external relations are still EXPERIMENTAL and BUGGY...\n");
94
95 #define CALCFN(NAME,INPUTS,OUTPUTS) \
96 result += CreateUserFunctionBlackBox(#NAME \
97 , helmholtz_prepare \
98 , NAME##_calc /* value */ \
99 , (ExtBBoxFunc*)NULL /* derivatives not provided yet*/ \
100 , (ExtBBoxFunc*)NULL /* hessian not provided yet */ \
101 , (ExtBBoxFinalFunc*)NULL /* finalisation not implemented */ \
102 , INPUTS,OUTPUTS /* inputs, outputs */ \
103 , NAME##_help /* help text */ \
104 , 0.0 \
105 ) /* returns 0 on success */
106
107 CALCFN(helmholtz_p,2,1);
108 CALCFN(helmholtz_u,2,1);
109 CALCFN(helmholtz_s,2,1);
110 CALCFN(helmholtz_h,2,1);
111 CALCFN(helmholtz_a,2,1);
112
113 #undef CALCFN
114
115 if(result){
116 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"CreateUserFunction result = %d\n",result);
117 }
118 return result;
119 }
120
121 /**
122 'helmholtz_prepare' just gets the data member and checks that it's
123 valid, and stores it in the blackbox data field.
124 */
125 int helmholtz_prepare(struct BBoxInterp *bbox,
126 struct Instance *data,
127 struct gl_list_t *arglist
128 ){
129 struct Instance *compinst;
130 const char *comp;
131
132 helmholtz_symbols[0] = AddSymbol("component");
133
134 /* get the data file name (we will look for this file in the ASCENDLIBRARY path) */
135 compinst = ChildByChar(data,COMPONENT_SYM);
136 if(!compinst){
137 ERROR_REPORTER_HERE(ASC_USER_ERROR
138 ,"Couldn't locate 'component' in DATA, please check usage of HELMHOLTZ."
139 );
140 return 1;
141 }
142 if(InstanceKind(compinst)!=SYMBOL_CONSTANT_INST){
143 ERROR_REPORTER_HERE(ASC_USER_ERROR,"DATA member 'component' must be a symbol_constant");
144 return 1;
145 }
146 comp = SCP(SYMC_INST(compinst)->value);
147 CONSOLE_DEBUG("COMPONENT: %s",comp);
148 if(comp==NULL || strlen(comp)==0){
149 ERROR_REPORTER_HERE(ASC_USER_ERROR,"'component' is NULL or empty");
150 return 1;
151 }
152
153 if(strcmp(comp,"ammonia")==0){
154 bbox->user_data = (void*)&helmholtz_data_ammonia;
155 }else if(strcmp(comp,"nitrogen")==0){
156 bbox->user_data = (void*)&helmholtz_data_nitrogen;
157 }else{
158 ERROR_REPORTER_HERE(ASC_USER_ERROR,"Component must be 'ammonia' or 'nitrogen' at this stage (only two components supported)");
159 }
160
161 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Prepared component '%s' OK.\n",comp);
162 return 0;
163 }
164
165 /*------------------------------------------------------------------------------
166 EVALULATION ROUTINES
167 */
168
169 #define CALCPREPARE \
170 /* a few checks about the input requirements */ \
171 if(ninputs != 2)return -1; \
172 if(noutputs != 1)return -2; \
173 if(inputs==NULL)return -3; \
174 if(outputs==NULL)return -4; \
175 if(bbox==NULL)return -5; \
176 \
177 /* the 'user_data' in the black box object will contain the */\
178 /* coefficients required for this fluid; cast it to the required form: */\
179 HelmholtzData *helmholtz_data = (HelmholtzData *)bbox->user_data
180
181 /**
182 Evaluation function for 'helmholtz_p'.
183 @param jacobian ignored
184 @return 0 on success
185 */
186 int helmholtz_p_calc(struct BBoxInterp *bbox,
187 int ninputs, int noutputs,
188 double *inputs, double *outputs,
189 double *jacobian
190 ){
191 CALCPREPARE;
192
193 /* first input is temperature, second is molar density */
194 outputs[0] = helmholtz_p(inputs[0], inputs[1], helmholtz_data);
195
196 /* no need to worry about error states etc. */
197 return 0;
198 }
199
200
201 /**
202 Evaluation function for 'helmholtz_u'
203 @param jacobian ignored
204 @return 0 on success
205 */
206 int helmholtz_u_calc(struct BBoxInterp *bbox,
207 int ninputs, int noutputs,
208 double *inputs, double *outputs,
209 double *jacobian
210 ){
211 CALCPREPARE;
212
213 /* first input is temperature, second is molar density */
214 outputs[0] = helmholtz_u(inputs[0], inputs[1], helmholtz_data);
215
216 /* no need to worry about error states etc. */
217 return 0;
218 }
219
220
221 /**
222 Evaluation function for 'helmholtz_h'
223 @param jacobian ignored
224 @return 0 on success
225 */
226 int helmholtz_s_calc(struct BBoxInterp *bbox,
227 int ninputs, int noutputs,
228 double *inputs, double *outputs,
229 double *jacobian
230 ){
231 CALCPREPARE;
232
233 /* first input is temperature, second is molar density */
234 outputs[0] = helmholtz_s(inputs[0], inputs[1], helmholtz_data);
235
236 /* no need to worry about error states etc. */
237 return 0;
238 }
239
240
241 /**
242 Evaluation function for 'helmholtz_h'
243 @param jacobian ignored
244 @return 0 on success
245 */
246 int helmholtz_h_calc(struct BBoxInterp *bbox,
247 int ninputs, int noutputs,
248 double *inputs, double *outputs,
249 double *jacobian
250 ){
251 CALCPREPARE;
252
253 /* first input is temperature, second is molar density */
254 outputs[0] = helmholtz_h(inputs[0], inputs[1], helmholtz_data);
255
256 /* no need to worry about error states etc. */
257 return 0;
258 }
259
260
261 /**
262 Evaluation function for 'helmholtz_h'
263 @param jacobian ignored
264 @return 0 on success
265 */
266 int helmholtz_a_calc(struct BBoxInterp *bbox,
267 int ninputs, int noutputs,
268 double *inputs, double *outputs,
269 double *jacobian
270 ){
271 CALCPREPARE;
272
273 /* first input is temperature, second is molar density */
274 outputs[0] = helmholtz_a(inputs[0], inputs[1], helmholtz_data);
275
276 /* no need to worry about error states etc. */
277 return 0;
278 }
279
280
281

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