-
Notifications
You must be signed in to change notification settings - Fork 0
/
steam_Tx.c
151 lines (131 loc) · 3.7 KB
/
steam_Tx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
freesteam - IAPWS-IF97 steam tables library
Copyright (C) 2004-2009 John Pye
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#define FREESTEAM_BUILDING_LIB
#include "steam_Tx.h"
#include "region4.h"
#include "region3.h"
#include "region2.h"
#include "region1.h"
#include "b23.h"
#include "zeroin.h"
/* .... */
#include <stdlib.h>
#include <assert.h>
#include <math.h>
int freesteam_bounds_Tx(MyDouble T, MyDouble x, int verbose){
#ifndef HAVE_CADNA_H
#define BOUND_WARN(MSG) \
if(verbose){\
fprintf(stderr,"%s (%s:%d): WARNING " MSG " (T = %g K, x = %g)\n"\
,__func__,__FILE__,__LINE__,T,x);\
}
#else
#define BOUND_WARN(MSG) \
if(verbose){\
fprintf(stderr,"%s (%s:%d): WARNING " MSG " (T = %s K, x = %s)\n"\
,__func__,__FILE__,__LINE__,strp(T),strp(x));\
}
#endif
if(T <= IAPWS97_TMIN){
BOUND_WARN("T <= TMIN");
return 1;
}
if(T > IAPWS97_TCRIT){
BOUND_WARN("T > TCRIT");
return 2;
}
if(x < 0){
BOUND_WARN("x < 0");
return 3;
}else if(x > 1){
BOUND_WARN("x > 1");
return 4;
}
return 0;
#undef BOUND_WARN
}
int freesteam_region_Tx(MyDouble T, MyDouble x){
if(T >= IAPWS97_TCRIT)return 3;
if(x <= 0){
if(T > REGION1_TMAX)return 3;
return 1;
}
if(x >= 1){
if(T > REGION1_TMAX)return 3;
return 2;
}
return 4;
}
typedef struct{
MyDouble T, s;
} SolveTSData;
static MyDouble Ts_region3_fn(MyDouble rho, void *user_data){
#define D ((SolveTSData *)user_data)
return D->s - freesteam_region3_s_rhoT(rho, D->T);
#undef D
}
/**
This function will always return saturated mixtures; no negative or >1
values of x are being 'understood' here (although one can give them meaning
based on extrapolated values of u or h of v, for example...)
*/
SteamState freesteam_set_Tx(MyDouble T, MyDouble x){
SteamState S;
if(T >= IAPWS97_TCRIT){
/* region 3 supercritical. just return a state with the specified
temperature and the critical point entropy. arbitrary. */
SolveTSData D = {T, freesteam_region3_s_rhoT(IAPWS97_RHOCRIT, IAPWS97_TCRIT)};
MyDouble ub = 1./freesteam_region1_v_pT(IAPWS97_PMAX,REGION1_TMAX);
MyDouble lb = 1./freesteam_region2_v_pT(freesteam_b23_p_T(T),T);
MyDouble tol = 1e-7;
MyDouble sol, err = 0;
if(zeroin_solve(&Ts_region3_fn, &D, lb, ub, tol, &sol, &err)){
fprintf(stderr,"%s (%s:%d): failed to solve for rho\n",__func__,__FILE__,__LINE__);
exit(1);
}
S.region = 3;
S.SteamState_U.R3.T = T;
S.SteamState_U.R3.rho = sol;
}else if(x <= 0){
if(T > REGION1_TMAX){
S.region = 3;
S.SteamState_U.R3.T = T;
S.SteamState_U.R3.rho = freesteam_region4_rhof_T(T);
/* FIXME iteratively refine the value */
}else{
S.region = 1;
S.SteamState_U.R1.p = freesteam_region4_psat_T(T);
S.SteamState_U.R1.T = T;
}
}else if(x >= 1){
if(T > REGION1_TMAX){
S.region = 3;
S.SteamState_U.R3.T = T;
S.SteamState_U.R3.rho = freesteam_region4_rhog_T(T);
/* FIXME iteratively refine the value */
}else{
S.region = 2;
S.SteamState_U.R1.p = freesteam_region4_psat_T(T);
S.SteamState_U.R1.T = T;
}
}else{
/* finally! */
S.region = 4;
S.SteamState_U.R4.T = T;
S.SteamState_U.R4.x = x;
}
return S;
}