/[ascend]/branches/jacob/models/johnpye/fprops/asc_mixture.h
ViewVC logotype

Contents of /branches/jacob/models/johnpye/fprops/asc_mixture.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3085 - (show annotations) (download) (as text)
Fri Aug 21 19:00:30 2015 UTC (4 years ago) by jacob
File MIME type: text/x-chdr
File size: 8561 byte(s)
remove some obsolete functions, and move test functions
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, August 14-, 2015
23
24 Includes other file headers, defines function headers and some constant
25 char* documentation strings to reduce the clutter in 'asc_mixture.c'
26 */
27
28 #ifndef MIX_LIBRARY_HEADER
29 #define MIX_LIBRARY_HEADER
30
31 #include <ascend/utilities/error.h>
32 #include <ascend/general/platform.h>
33 #include <ascend/general/list.h>
34 #include <ascend/compiler/extfunc.h>
35 #include <ascend/compiler/parentchild.h>
36 #include <ascend/compiler/symtab.h>
37 #include <ascend/compiler/instance_enum.h>
38 #include <ascend/compiler/instance_types.h>
39 #include <ascend/compiler/instmacro.h>
40 #include <ascend/compiler/instquery.h>
41 #include <ascend/compiler/sets.h>
42 #include <ascend/compiler/arrayinst.h>
43
44 #include <stdio.h>
45
46 /* Code that is wrapped up in this file */
47 #include "mixtures/mixture_properties.h"
48 #include "mixtures/mixture_generics.h"
49 #include "mixtures/mixture_phases.h"
50 #include "mixtures/mixture_struct.h"
51
52 #ifndef ASC_EXPORT
53 #error "ASC_EXPORT not found -- where is it?"
54 #endif
55
56 #define NULL_STR(STR) (STR==NULL) ? "NULL" : STR
57 #define MIN_P 0.001 /* minimum pressure */
58 #define MIN_T 1.e-6 /* minimum temperature */
59 #define MIN_S 0.0 /* minimum entropy */
60 #define MIN_HEAT_CAPAC 0.0 /* minimum heat capacity */
61
62 /* ---------------------------------------------------------------------
63 Forward Declarations
64 */
65 ExtBBoxInitFunc asc_mixture_prepare;
66
67 /*
68 Macros to declare external functions (forward declarations)
69 */
70 #define MIX_EXTDECL(NAME) ExtBBoxFunc mixture_##NAME##_calc;
71 #define MIX_PROP_EXTDECL(PROP) MIX_EXTDECL(PROP) MIX_EXTDECL(phase_##PROP) \
72 MIX_EXTDECL(comps_##PROP)
73
74 MIX_PROP_EXTDECL(rho); MIX_PROP_EXTDECL(u); MIX_PROP_EXTDECL(h); MIX_PROP_EXTDECL(cp);
75 MIX_PROP_EXTDECL(cv); MIX_PROP_EXTDECL(s); MIX_PROP_EXTDECL(g); MIX_PROP_EXTDECL(a);
76
77 MIX_EXTDECL(count_phases); MIX_EXTDECL(count_components); MIX_EXTDECL(component_frac);
78 MIX_EXTDECL(component_index); MIX_EXTDECL(dew_p); MIX_EXTDECL(bubble_p);
79 MIX_EXTDECL(dew_T); MIX_EXTDECL(bubble_T); MIX_EXTDECL(state_T_ph);
80
81 /* ---------------------------------------------------------------------
82 Global Variables
83 */
84 static symchar *mix_symbols[6];
85 enum Symbol_Enum {NPURE_SYM, COMP_SYM, X_SYM, TYPE_SYM, SOURCE_SYM};
86
87 /*
88 Macros to define help texts for external functions
89 */
90 #define MIX_HELP_TEXT(PROP) "Calculate mixture " PROP ", using ideal-solution assumption."
91 #define MIX_PHASE_HELP_TEXT(PROP) "Calculate mixture " PROP " for a single phase, using ideal-solution assumption."
92 #define MIX_COMPS_HELP_TEXT(PROP) "Calculate " PROP " for a single component in one phase of the mixture, using ideal-solution assumption."
93
94 #define MIX_HELP_DECL(PROP,MESSAGE) static const char *mixture_##PROP##_help = MESSAGE;
95 #define MIX_PH_HELP_DECL(PROP,MESSAGE) static const char *mixture_phase_##PROP##_help = MESSAGE;
96 #define MIX_CMP_HELP_DECL(PROP,MESSAGE) static const char *mixture_comps_##PROP##_help = MESSAGE;
97
98 #define MIX_HELP_DOUBLE(PROP,NAME) MIX_HELP_DECL(PROP, MIX_HELP_TEXT(NAME)) \
99 MIX_PH_HELP_DECL(PROP, MIX_PHASE_HELP_TEXT(NAME)) \
100 MIX_CMP_HELP_DECL(PROP, MIX_COMPS_HELP_TEXT(NAME))
101
102 /*
103 Define help texts
104 */
105 MIX_HELP_DOUBLE(rho, "density");
106 MIX_HELP_DOUBLE(u, "internal energy");
107 MIX_HELP_DOUBLE(h, "enthalpy");
108 MIX_HELP_DOUBLE(cp, "constant-pressure heat capacity");
109 MIX_HELP_DOUBLE(cv, "contant-volume heat capacity");
110 MIX_HELP_DOUBLE(s, "entropy");
111 MIX_HELP_DOUBLE(g, "Gibbs energy");
112 MIX_HELP_DOUBLE(a, "Helmholtz energy");
113
114 MIX_HELP_DECL(count_phases, "Calculate and return number of phases in the mixture, and "
115 "mass fraction of the mixture in each phase.");
116 MIX_HELP_DECL(count_components, "Return number of components within a phase of the mixture.");
117 MIX_HELP_DECL(component_frac, "Return mass fractions of a component in all phases throughout the mixture.");
118 MIX_HELP_DECL(component_index, "Return indexes that give the location of a component in all phases of the mixture");
119 MIX_HELP_DECL(dew_p, MIX_HELP_TEXT("dew pressure"));
120 MIX_HELP_DECL(bubble_p, MIX_HELP_TEXT("bubble pressure"));
121 MIX_HELP_DECL(dew_T, MIX_HELP_TEXT("dew temperature"));
122 MIX_HELP_DECL(bubble_T, MIX_HELP_TEXT("bubble temperature"));
123 MIX_HELP_DECL(state_T_ph, "Calculate mixture temperature from the pressure and enthalpy, "
124 "using ideal-solution assumption");
125
126 /* ---------------------------------------------------------------------
127 Macros used in creating mixture-property and other ASCEND functions
128 */
129 #define CALCPREP(NIN,NOUT) \
130 if(ninputs!=NIN){ \
131 return -1; \
132 } \
133 if(noutputs!=NOUT){ \
134 return -2; \
135 } \
136 if(inputs==NULL){ \
137 return -3; \
138 } \
139 if(outputs==NULL){ \
140 return -4; \
141 } \
142 if(bbox==NULL){ \
143 return -5; \
144 } \
145 FpropsError err=FPROPS_NO_ERROR; \
146 MixtureSpec *MS = (MixtureSpec *)bbox->user_data;
147
148 /*
149 Perform switch on the output of a function that seeks some property,
150 outputting different error messages depending on the value of the output.
151 */
152 #define ROOTSOLVE_SWITCH(RESULT,PROP) \
153 switch(RESULT){ \
154 case 1: \
155 ERROR_REPORTER_HERE(ASC_USER_ERROR \
156 , "The " PROP " converged on a non-solution point."); \
157 break; \
158 case 2: \
159 ERROR_REPORTER_HERE(ASC_USER_ERROR \
160 , "The " PROP " fractions converged on Infinity or NaN."); \
161 break; \
162 case 3: \
163 ERROR_REPORTER_HERE(ASC_USER_ERROR \
164 , "The search for the " PROP " failed to converge " \
165 "\nin the maximum number of iterations."); \
166 break; \
167 }
168
169 /*
170 Perform switch on the output of seeking some property, like ROOTSOLVE_SWITCH
171 with the added possibility that no flash occurred.
172 */
173 #define ROOT_VLE_SWITCH(RESULT,PROP1,PROP2,PROP2_UNIT,PROP2_VAL) \
174 switch(RESULT){ \
175 case 1: \
176 ERROR_REPORTER_HERE(ASC_USER_ERROR \
177 , "The " PROP " converged on a non-solution point."); \
178 break; \
179 case 2: \
180 ERROR_REPORTER_HERE(ASC_USER_ERROR \
181 , "The " PROP " converged on Infinity or NaN."); \
182 break; \
183 case 3: \
184 ERROR_REPORTER_HERE(ASC_USER_ERROR \
185 , "The search for the " PROP " failed to converge" \
186 "\nin the maximum number of iterations."); \
187 break; \
188 case 4: \
189 ERROR_REPORTER_HERE(ASC_USER_ERROR \
190 , "There is no " PROP "; all components are supercritical" \
191 "\nat " PROP2 " %g " PROP2_UNIT, PROP2_VAL); \
192 break; \
193 }
194
195 #define CALCFLASH \
196 PhaseSpec *PS = new_PhaseSpec(MS->pures, 3); \
197 int fl_result = 0 \
198 , mx_result = 0; \
199 double T = inputs[0] /* mixture temperature */ \
200 , p = inputs[1] /* mixture pressure */ \
201 , tol = MIX_XTOL; /* tolerance used when solving for component densities */ \
202 fl_result = mixture_flash(PS, MS, T, p, tol, &err); \
203 ROOTSOLVE_SWITCH(fl_result,"vapor/liquid phase fractions"); \
204 mx_result = mixture_rhos_sat(PS, T, p, tol, &err); \
205 ROOTSOLVE_SWITCH(mx_result,"density");
206
207 #define CALC_PH_FLASH \
208 PhaseSpec *PS = new_PhaseSpec(MS->pures, 3); \
209 int ph_result = 0 /* result of finding temperature from (p,h) conditions */ \
210 , fl_result = 0 /* result of performing flash */ \
211 , mx_result = 0; /* result of finding mixture component densities */ \
212 double p = inputs[0] /* pressure */ \
213 , h = inputs[1] /* enthalpy */ \
214 , T /* temperature, determined from 'p' and 'h' */ \
215 , tol = MIX_XTOL; /* tolerance used when solving for component densities */ \
216 ph_result = mixture_T_ph(&T, MS, p, h, tol, &err); \
217 ROOTSOLVE_SWITCH(ph_result, "system temperature"); \
218 fl_result = mixture_flash(PS, MS, T, p, tol, &err); \
219 ROOTSOLVE_SWITCH(fl_result, "vapor/liquid phase fractions"); \
220 mx_result = mixture_rhos_sat(PS, T, p, tol, &err); \
221 ROOTSOLVE_SWITCH(mx_result,"density");
222
223 #endif

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