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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1810 - (show annotations) (download) (as text)
Tue Jul 29 07:45:48 2008 UTC (12 years, 1 month ago) by jpye
File MIME type: text/x-csrc
File size: 5824 byte(s)
Added new code for calculating properties according to modified BWR correlation, also some initial
work on Helmholtz free energy correlation.
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 mbwr.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 "mbwr.h"
44
45 #ifndef ASC_EXPORT
46 # error "Where is ASC_EXPORT?"
47 #endif
48
49
50 /*------------------------------------------------------------------------------
51 FORWARD DECLARATIONS
52 */
53
54 ExtBBoxInitFunc mbwr_p_prepare;
55 ExtBBoxFunc mbwr_p_calc;
56
57 /*------------------------------------------------------------------------------
58 GLOBALS
59 */
60
61 /* place to store symbols needed for accessing ASCEND's instance tree */
62 static symchar *mbwr_symbols[1];
63 #define COMPONENT_SYM mbwr_symbols[0]
64
65 /* property data for R123, from J. Phys. Chem. Ref. Data, 23:731-779, 1994. */
66 const MbwrData mbwr_r123 = {
67 1 /* ideal gas constant? */
68 ,13.2117771543124 /* rho_c */
69 ,{ /* beta[32] */
70 -0.657453133659E-02, 0.293479845842E+01, -0.989140469845E+02
71 , 0.201029776013E+05, -0.383566527886E+07, 0.227587641969E-02
72 ,-0.908726819450E+01, 0.434181417995E+04, 0.354116464954E+07
73 ,-0.635394849670E-03, 0.320786715274E+01, -0.131276484299E+04
74 ,-0.116360713718E+00, -0.113354409016E+02, -0.537543457327E+04
75 , 0.258112416120E+01, -0.106148632128E+00, 0.500026133667E+02
76 ,-0.204326706346E+01, -0.249438345685E+07, -0.463962781113E+09
77 ,-0.284903429588E+06, 0.974392239902E+10, -0.637314379308E+04
78 , 0.314121189813E+06, -0.145747968225E+03, -0.843830261449E+07
79 ,-0.241138441593E+01, 0.108508031257E+04, -0.106653193965E-01
80 ,-0.121343571084E+02, -0.257510383240E+03
81 }
82 };
83
84 /*------------------------------------------------------------------------------
85 REGISTRATION FUNCTION
86 */
87
88 /**
89 This is the function called from "IMPORT mbwr"
90
91 It sets up the functions contained in this external library
92 */
93 extern
94 ASC_EXPORT int mbwr_register(){
95 const char *mbwr_help = "Modified Benedict-Webb-Rubin correlation for thermodynamic properties";
96 int result = 0;
97
98 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Initialising MBWR...\n");
99
100 result += CreateUserFunctionBlackBox("mbwr_p"
101 , mbwr_p_prepare
102 , mbwr_p_calc /* value */
103 , (ExtBBoxFunc*)NULL /* derivatives not provided yet*/
104 , (ExtBBoxFunc*)NULL /* hessian not provided yet */
105 , (ExtBBoxFinalFunc*)NULL /* finalisation not implemented */
106 , 2,1 /* inputs, outputs */
107 , mbwr_help
108 , 0.0
109 ); /* returns 0 on success */
110
111 if(result){
112 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"CreateUserFunction result = %d\n",result);
113 }
114 return result;
115 }
116
117 /*------------------------------------------------------------------------------
118 WRAPPING FOR 'mbwr_p'...
119 */
120
121 int mbwr_p_prepare(struct BBoxInterp *bbox,
122 struct Instance *data,
123 struct gl_list_t *arglist
124 ){
125 struct Instance *compinst;
126 const char *comp;
127
128 mbwr_symbols[0] = AddSymbol("component");
129
130 /* get the data file name (we will look for this file in the ASCENDLIBRARY path) */
131 compinst = ChildByChar(data,COMPONENT_SYM);
132 if(!compinst){
133 ERROR_REPORTER_HERE(ASC_USER_ERROR
134 ,"Couldn't locate 'component' in DATA, please check usage of MBWR."
135 );
136 return 1;
137 }
138 if(InstanceKind(compinst)!=SYMBOL_CONSTANT_INST){
139 ERROR_REPORTER_HERE(ASC_USER_ERROR,"DATA member 'component' must be a symbol_constant");
140 return 1;
141 }
142 comp = SCP(SYMC_INST(compinst)->value);
143 CONSOLE_DEBUG("COMPONENT: %s",comp);
144 if(comp==NULL || strlen(comp)==0){
145 ERROR_REPORTER_HERE(ASC_USER_ERROR,"'component' is NULL or empty");
146 return 1;
147 }
148
149 if(strcmp(comp,"R123")!=0){
150 ERROR_REPORTER_HERE(ASC_USER_ERROR,"Component must be 'R123' at this stage (only one component supported)");
151 }
152
153 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"PREPARING MBWR_P...\n");
154
155 bbox->user_data = (void*)&mbwr_r123;
156
157 return 0;
158 }
159
160 /**
161 Evaluation function. This one does the actual calling to the
162 'mbwr_p' routine.
163 @param jacobian ignored
164 @return 0 on success
165 */
166 int mbwr_p_calc(struct BBoxInterp *bbox,
167 int ninputs, int noutputs,
168 double *inputs, double *outputs,
169 double *jacobian
170 ){
171 /* a few checks about the input requirements */
172 if(ninputs != 2)return -1;
173 if(noutputs != 1)return -2;
174 if(inputs==NULL)return -3;
175 if(outputs==NULL)return -4;
176 if(bbox==NULL)return -5;
177
178 /* the 'user_data' in the black box object will contain the coefficients
179 required for this fluid; cast it to the required form: */
180 MbwrData *mbwr_data = (MbwrData *)bbox->user_data;
181
182 /* first input is temperature, second is molar density */
183 outputs[0] = mbwr_p(inputs[0], inputs[1], mbwr_data);
184
185 /* no need to worry about error states etc. */
186
187 return 0;
188 }

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