/[ascend]/branches/jacob/models/johnpye/fprops/mixtures/asc_idealsolution.c
ViewVC logotype

Contents of /branches/jacob/models/johnpye/fprops/mixtures/asc_idealsolution.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2988 - (show annotations) (download) (as text)
Tue Jun 30 04:00:24 2015 UTC (3 years, 4 months ago) by jacob
File MIME type: text/x-csrc
File size: 6928 byte(s)
expanded new library file to export mixture functions, and test .a4c file to check that the functions are exported correctly, and receive correct data.

minor changes to <ascend/compiler/instance_enum.h>, adding a few macro definitions of ASCEND data types for library file to be able to mention by name.

1 /* ASCEND modelling environment
2 Copyright (C) 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, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 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 --
17
18 Free Software Foundation, Inc.
19 59 Temple Place - Suite 330
20 Boston, MA 02111-1307, USA.
21 *//*
22 by Jacob Shealy, June 25-, 2015
23
24 Function exports for initial model of ideal-solution mixing
25 */
26
27 #include <ascend/utilities/error.h>
28 #include <ascend/general/platform.h>
29 #include <ascend/general/list.h>
30 #include <ascend/compiler/extfunc.h>
31 #include <ascend/compiler/parentchild.h>
32 /*
33 #include <ascend/compiler/child.h>
34 #include <ascend/compiler/childinfo.h>
35 */
36 /* #include <ascend/compiler/slist.h> */
37 /* #include <ascend/compiler/type_desc.h> */
38 #include <ascend/compiler/symtab.h>
39 #include <ascend/compiler/instance_enum.h>
40 #include <ascend/compiler/instance_types.h>
41 #include <ascend/compiler/instmacro.h>
42 #include <ascend/compiler/instquery.h>
43 #include <ascend/compiler/sets.h>
44 #include <ascend/compiler/arrayinst.h>
45
46 /* Code that is wrapped */
47 #include "init_mixfuncs.h" /* rewrite to ideal_solution.h, ideal_solution.c */
48
49 #ifndef ASC_EXPORT
50 #error "ASC_EXPORT not found -- where is it?"
51 #endif
52
53 /*
54 Forward Declarations
55 */
56 ExtBBoxInitFunc asc_mixture_prepare;
57 ExtBBoxFunc mixture_p_calc;
58 ExtBBoxFunc mixture_rho_calc;
59
60 /*
61 Global Variables
62 */
63 static symchar *mix_symbols[5];
64 enum Symbol_Enum {NPURE_SYM, COMP_SYM, X_SYM, TYPE_SYM, SOURCE_SYM};
65
66 static const char *mixture_p_help = "Calculate pressure for the mixture, using ideal solution assumption, and report if pressure is inconsistent among the densities";
67 static const char *mixture_rho_help = "Calculate overall density of the solution, using ideal-solution assumption";
68
69 /*
70 Register all functions that will be exported
71 */
72 extern ASC_EXPORT int mixture_register(){
73 int result = 0;
74
75 ERROR_REPORTER_HERE(ASC_USER_WARNING,
76 "FPROPS in general, and IN PARTICULAR this mixture module, "
77 "are still EXPERIMENTAL. Use with caution.");
78
79 #define QQZX 3
80
81 #define CALCFN(NAME,INPUTS,OUTPUTS) \
82 result += CreateUserFunctionBlackBox( #NAME \
83 , asc_mixture_prepare \
84 , NAME##_calc /* value */ \
85 , (ExtBBoxFunc *)NULL /* derivatives -- none */ \
86 , (ExtBBoxFunc *)NULL /* hessian -- none */ \
87 , (ExtBBoxFinalFunc *)NULL /* finalization -- none */ \
88 , INPUTS,OUTPUTS \
89 , NAME##_help /* help text */ \
90 , 0.0 \
91 )
92
93 CALCFN(mixture_p,1,1);
94 CALCFN(mixture_rho,2,1);
95
96 if(result){
97 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"CreateUserFunctionBlackBox result is %d\n",result);
98 }
99 return result;
100 }
101
102 /*
103 Function which prepares persistent data
104 */
105 int asc_mixture_prepare(struct BBoxInterp *bbox, struct Instance *data, struct gl_list_t *arglist){
106
107 #define CHECK_TYPE(VAR,TYPE,NAME,TYPENAME) \
108 if(InstanceKind(VAR)!=TYPE){ \
109 ERROR_REPORTER_HERE(ASC_USER_ERROR \
110 , "DATA member '%s' has type-value %#o, but must have %s (value %#o)" \
111 , NAME, InstanceKind(VAR), TYPENAME, TYPE); \
112 return 1; \
113 }
114
115 #define CHECK_EXIST_TYPE(VAR,TYPE,NAME,TYPENAME) \
116 if(! VAR){ \
117 ERROR_REPORTER_HERE(ASC_USER_ERROR \
118 , "Couldn't locate '%s' in DATA, please check usage", NAME); \
119 } \
120 CHECK_TYPE(VAR,TYPE,NAME,TYPENAME)
121
122 struct Instance *npureinst, *compinst, *xinst, *typeinst, *srcinst;
123 unsigned npure;
124
125 mix_symbols[0] = AddSymbol("npure");
126 mix_symbols[1] = AddSymbol("components");
127 mix_symbols[2] = AddSymbol("xs");
128 mix_symbols[3] = AddSymbol("type");
129 mix_symbols[4] = AddSymbol("source");
130
131 /* number of pure components -- required */
132 npureinst = ChildByChar(data, mix_symbols[NPURE_SYM]);
133 CHECK_EXIST_TYPE(npureinst,INTEGER_CONSTANT_INST, "npure", "integer_constant");
134 /* if(!npureinst){
135 ERROR_REPORTER_HERE(ASC_USER_ERROR
136 , "Couldn't locate 'npure' in DATA, please check usage");
137 return 1;
138 }
139 if(InstanceKind(npureinst)!=INTEGER_CONSTANT_INST){
140 ERROR_REPORTER_HERE(ASC_USER_ERROR
141 , "DATA member 'npure' was %x, but must be an integer_constant %x",
142 InstanceKind(npureinst), INTEGER_CONSTANT_INST);
143 } */
144 npure = (int *)IC_INST(npureinst)->value;
145
146 const struct gl_list_t *comps_gl;
147 const char *comps[npure], *type=NULL, *source=NULL;
148 const double *xs[npure];
149
150 /* component names -- required */
151 compinst = ChildByChar(data, mix_symbols[COMP_SYM]);
152 CHECK_EXIST_TYPE(compinst, ARRAY_ENUM_INST, "components",
153 "array of symbol_constant(s)");
154 comps_gl = ARY_INST(compinst)->children;
155
156 /* component correlation type (equation of state) -- not required */
157 typeinst = ChildByChar(data, mix_symbols[TYPE_SYM]);
158 if(typeinst){
159 CHECK_TYPE(typeinst, SYMBOL_CONSTANT_INST, "type", "symbol_constant");
160 type = SCP(SYMC_INST(typeinst)->value);
161 if(type && strlen(type)==0){
162 char t[] = "helmholtz";
163 type = t;
164 }
165 }
166
167 /* source data string -- not required */
168 srcinst = ChildByChar(data, mix_symbols[SOURCE_SYM]);
169 if(srcinst){
170 CHECK_TYPE(srcinst, SYMBOL_CONSTANT_INST, "source", "symbol_constant");
171 source = SCP(SYMC_INST(typeinst)->value);
172 if(source && strlen(source)==0){
173 source = NULL;
174 }
175 }
176
177 /* mass fractions -- required */
178 /*
179 May extend this to handle mole fractions, at a later date?
180 */
181 xinst = ChildByChar(data, mix_symbols[X_SYM]);
182 ERROR_REPORTER_HERE(ASC_USER_ERROR
183 , "The type of the mass-fraction array is %x", InstanceKind(xinst));
184
185 /* mixture model (ideal or real) -- not required */
186
187 /* bbox->user_data = (void *)mixture_specify(npure, xs, comps, type, src); */
188 bbox->user_data = NULL;
189 }
190
191 /*
192 Functions which calculate results
193 */
194 #define CALCPREP(NIN,NOUT) \
195 if(ninputs!=NIN){ \
196 return -1; \
197 } \
198 if(noutputs!=NOUT){ \
199 return -2; \
200 } \
201 if(inputs==NULL){ \
202 return -3; \
203 } \
204 if(outputs==NULL){ \
205 return -4; \
206 } \
207 if(bbox==NULL){ \
208 return -5; \
209 } \
210 const MixtureSpec *MX = (const MixtureSpec *)bbox->user_data; \
211 FpropsError err=FPROPS_NO_ERROR;
212
213 int mixture_p_calc(struct BBoxInterp *bbox, int ninputs, int noutputs,
214 double *inputs, double *outputs, double *jacobian){
215 CALCPREP(1,1);
216
217 ERROR_REPORTER_HERE(ASC_USER_NOTE
218 , "Function 'mixture_p_calc' -- this function has no contents as yet.");
219
220 outputs[0] = 0.0;
221
222 return 0;
223 }
224
225 int mixture_rho_calc(struct BBoxInterp *bbox, int ninputs, int noutputs,
226 double *inputs, double *outputs, double *jacobian){
227 CALCPREP(2,1);
228
229 ERROR_REPORTER_HERE(ASC_USER_NOTE
230 , "Function 'mixture_rho_calc' -- this function has no contents as yet.");
231
232 outputs[0] = 0.0;
233
234 return 0;
235 }

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