-
Notifications
You must be signed in to change notification settings - Fork 0
/
steam_pu.c
143 lines (126 loc) · 4.01 KB
/
steam_pu.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
/*
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_pu.h"
#include "region1.h"
#include "region2.h"
#include "region3.h"
#include "region4.h"
#include "b23.h"
#include "backwards.h"
#include "zeroin.h"
#include "solver2.h"
#include "common.h"
#include <stdio.h>
#include <stdlib.h>
int freesteam_region_pu(MyDouble p, MyDouble u){
MyDouble p13 = freesteam_region4_psat_T(REGION1_TMAX);
if(p < p13){
MyDouble Tsat = freesteam_region4_Tsat_p(p);
MyDouble uf = freesteam_region1_u_pT(p,Tsat);
if(u < uf)return 1;
MyDouble ug = freesteam_region2_u_pT(p,Tsat);
if(u > ug)return 2;
return 4;
}
MyDouble u13 = freesteam_region1_u_pT(p,REGION1_TMAX);
if(u < u13){
return 1;
}
MyDouble T23 = freesteam_b23_T_p(p);
MyDouble u23 = freesteam_region2_u_pT(p,T23);
if(u > u23){
return 2;
}
if(p > IAPWS97_PCRIT)return 3;
/* FIXME what we really need here is a psat(u) function! The current method
is singular. */
MyDouble Tsat = freesteam_region4_Tsat_p(p);
MyDouble rhof = freesteam_region4_rhof_T(Tsat);
MyDouble uf = freesteam_region3_u_rhoT(rhof,Tsat);
if(u<uf) return 3;
MyDouble rhog = freesteam_region4_rhog_T(Tsat);
MyDouble ug = freesteam_region3_u_rhoT(rhog,Tsat);
if(u>ug)return 3;
return 4;
}
typedef struct{
MyDouble p, u, T;
} SolvePUData;
#define D ((SolvePUData *)user_data)
static ZeroInSubjectFunction pu_region1_fn, pu_region2_fn, pu_region4_fn;
MyDouble pu_region1_fn(MyDouble T, void *user_data){
return D->u - freesteam_region1_u_pT(D->p, T);
}
MyDouble pu_region2_fn(MyDouble T, void *user_data){
return D->u - freesteam_region2_u_pT(D->p, T);
}
MyDouble pu_region4_fn(MyDouble x, void *user_data){
return D->u - freesteam_region4_u_Tx(D->T, x);
}
#undef D
SteamState freesteam_set_pu(MyDouble p, MyDouble u){
MyDouble lb, ub, tol, sol, err;
SolvePUData D = {p, u, 0.};
SteamState S;
int region = freesteam_region_pu(p,u);
switch(region){
case 1:
lb = IAPWS97_TMIN;
ub = REGION1_TMAX;
tol = 1e-9; /* ??? */
zeroin_solve(&pu_region1_fn, &D, lb, ub, tol, &sol, &err);
S = freesteam_region1_set_pT(p,sol);
break;
case 2:
lb = IAPWS97_TMIN;
ub = REGION2_TMAX;
tol = 1e-9; /* ??? */
zeroin_solve(&pu_region2_fn, &D, lb, ub, tol, &sol, &err);
S = freesteam_region2_set_pT(p,sol);
break;
case 4:
lb = 0.;
ub = 1.;
tol = 1e-9; /* ??? */
D.T = freesteam_region4_Tsat_p(p);
//fprintf(stderr,"%s: (%s:%d): p = %g\n",__func__,__FILE__,__LINE__,D.p);
zeroin_solve(&pu_region4_fn, &D, lb, ub, tol, &sol, &err);
S = freesteam_region4_set_Tx(D.T,sol);
break;
case 3:
/* FIXME looks like a problem with the derivative routines here? */
{
int status;
SteamState guess = freesteam_region3_set_rhoT(IAPWS97_RHOCRIT,IAPWS97_TCRIT);
S = freesteam_solver2_region3('p','u', p, u, guess, &status);
if(status){
#ifndef HAVE_CADNA_H
fprintf(stderr,"%s (%s:%d): Failed solve in region 3 for (p = %g MPa, u = %g kJ/kg\n"
,__func__,__FILE__,__LINE__,p/1e6,u/1e3);
#else
fprintf(stderr,"%s (%s:%d): Failed solve in region 3 for (p = %s MPa, u = %s kJ/kg\n"
,__func__,__FILE__,__LINE__,strp(p/1e6),strp(u/1e3));
#endif
}
}
break;
default:
fprintf(stderr,"%s (%s:%d): Region '%d' not implemented\n",__func__,__FILE__,__LINE__,region);
exit(1);
}
return S;
}