proteus
1.9.1
C/C++/Fortran libraries
Toggle main menu visibility
Loading...
Searching...
No Matches
home
cekees
proteus-1.9.1-docs
proteus
m_comp_co2
co2_brine_flash.h
Go to the documentation of this file.
1
#ifndef CO2_BRINE_FLASH_H
2
#define CO2_BRINE_FLASH_H
3
// Analytic (p,z) flash for the 2-component CO2-brine model -- C++ port of
4
// co2_brine_flash.py. Header-only; the kernel includes this.
5
//
6
// flashPZ(p_Pa, z, T_C, m_NaCl, eps) -> FlashState
7
//
8
// All first AND second derivatives are exact, computed by 2nd-order forward-mode
9
// AD (Jet2) -- NO finite differencing. The composition clamps are hyperbolically
10
// smoothed so S_g is C1 across phase appearance/disappearance and the capillary
11
// value-block Jacobian (which needs d2S_g) is fully consistent.
12
#include "
co2_brine_eos.h
"
13
#include "
jet2.h
"
14
#include <cmath>
15
#include <algorithm>
16
17
namespace
m_comp_co2
{
18
namespace
flash
{
19
20
constexpr
double
PA_PER_BAR
= 1.0e5;
21
// composition-clamp smoothing band [mole fraction]. See co2_brine_flash.py for
22
// the full rationale: smooth_min undershoots X below z, and the lever rule
23
// amplifies that by rho_a/rho_g (~1400x) into S_g. At SI scale (Xeq~6e-4) the old
24
// 5e-4 leaked S_g~0.10 at z=1e-5; 1e-5 keeps spurious S_g ~5e-5 while staying C1.
25
constexpr
double
EPS_Z
= 1.0e-5;
// composition-clamp smoothing band
26
// z is evaluated inside [Z_MIN, 1 - Z_MIN] (CO2-free brine starts at z = 0 and
27
// Newton iterates can leave [0, 1]). Outside it the flash returns the bound's
28
// state with ZERO z-derivatives: the result does not change with z there, so a
29
// nonzero dS_g/dz, dX/dz, ... would put a derivative in the Jacobian that the
30
// residual does not have (FD probe of the 1D prototype: 515 vs 0 at CO2-free
31
// nodes, test/m_comp_co2/verification/rock_interface_1d). Callers pass the raw z.
32
constexpr
double
Z_MIN
= 1.0e-8;
33
34
// Immiscible / incompressible limit (verification only) -- must match
35
// co2_brine_flash.py. When enabled (flashPZ immiscible=true, set from the
36
// Coefficients 'immiscible' option) mutual solubility is suppressed (Xeq=0,
37
// Yeq=1) and both phase densities are pinned to these constants, so the species
38
// decouple into the classical immiscible two-phase saturation equations. The
39
// McWhorter-Sunada S_n similarity profile is invariant to the density values
40
// (they divide out of each saturation equation); any IC/BC converting S_n <-> z
41
// MUST use the SAME two constants.
42
constexpr
double
RHO_A_IMM
= 5.55e4;
// constant aqueous molar density [mol/m^3]
43
constexpr
double
RHO_G_IMM
= 2.00e4;
// constant gas molar density [mol/m^3]
44
45
struct
FlashState
{
46
double
S_g
,
X
,
Y
,
rho_a
,
rho_g
,
f_g
;
47
double
dS_g_dp
,
dS_g_dz
,
dX_dp
,
dX_dz
,
dY_dp
,
dY_dz
;
48
double
drho_a_dp
,
drho_a_dz
,
drho_g_dp
,
drho_g_dz
;
49
double
d2S_g_dp2
,
d2S_g_dpdz
,
d2S_g_dz2
;
// flash Hessian (capillary Jac)
50
int
phase
;
// 0 aqueous, 1 two-phase, 2 gas
51
};
52
53
// CO2-rich molar volume as a 2-jet: value from the float cubic solver, then
54
// Newton-refined in jet space against the RK residual so (p,z) derivs are exact.
55
inline
Jet2
_V_jet
(
double
T_C,
const
Jet2
& P_jet,
bool
liquid) {
56
using namespace
m_comp_co2::eos
;
57
double
T_K = T_C + 273.15;
58
double
a =
A_CO2_0
-
A_CO2_1
*T_K, b =
B_CO2
;
59
double
RTK =
R_CM3_BAR
*T_K, T05 = std::sqrt(T_K);
60
Jet2
V =
Jet2::cst
(
rk_molar_volume
(T_C, P_jet.
v
, liquid));
61
for
(
int
it = 0; it < 4; ++it) {
62
Jet2
VVb = V*(V + b);
63
Jet2
F = P_jet - RTK*
recip
(V - b) + (a/T05)*
recip
(VVb);
64
Jet2
dFdV = RTK*
recip
((V - b)*(V - b))
65
- (a/T05)*(2.0*V + b)*
recip
(VVb*VVb);
66
V = V - F*
recip
(dFdV);
67
}
68
return
V;
69
}
70
71
inline
Jet2
_ln_phi_jet
(
double
T_C,
const
Jet2
& P_jet,
const
Jet2
& V,
72
double
b_k,
double
sum_aik) {
73
using namespace
m_comp_co2::eos
;
74
double
T_K = T_C + 273.15;
75
double
a =
A_CO2_0
-
A_CO2_1
*T_K, b =
B_CO2
;
76
double
RTK =
R_CM3_BAR
*T_K, RT15 =
R_CM3_BAR
*std::pow(T_K, 1.5);
77
Jet2
lnr =
jlog
((V + b)/V);
78
return
jlog
(V/(V - b))
79
+ b_k*
recip
(V - b)
80
- (2.0*sum_aik/(RT15*b))*lnr
81
+ (a*b_k/(RT15*b*b))*(lnr - b*
recip
(V + b))
82
-
jlog
(P_jet*V/RTK);
83
}
84
85
inline
void
_solubility_jet
(
double
T_C,
const
Jet2
& P_jet,
bool
liquid,
86
Jet2
& x_CO2,
Jet2
& y_H2O,
Jet2
& V) {
87
using namespace
m_comp_co2::eos
;
88
double
T_K = T_C + 273.15, RTK =
R_CM3_BAR
*T_K;
89
V =
_V_jet
(T_C, P_jet, liquid);
90
Jet2
phi_CO2 =
jexp
(
_ln_phi_jet
(T_C, P_jet, V,
B_CO2
,
A_CO2_0
-
A_CO2_1
*T_K));
91
Jet2
phi_H2O =
jexp
(
_ln_phi_jet
(T_C, P_jet, V,
B_H2O
,
A_H2O_CO2
));
92
double
K0H = std::pow(10.0,
poly_logK0
(
K0_H2O
, T_C));
93
double
K0C = std::pow(10.0,
poly_logK0
(liquid ?
K0_CO2_L
:
K0_CO2_G
, T_C));
94
Jet2
dP = P_jet -
P0_BAR
;
95
Jet2
A = (K0H*
recip
(phi_H2O*P_jet))*
jexp
(dP*(
V_H2O
/RTK));
96
Jet2
B
= (phi_CO2*P_jet*(1.0/(
M_PER_KG_H2O
*K0C)))*
jexp
(dP*(-
V_CO2
/RTK));
97
y_H2O = (1.0 -
B
)*
recip
(
recip
(A) -
B
);
98
x_CO2 =
B
*(1.0 - y_H2O);
99
}
100
101
inline
FlashState
flashPZ
(
double
p_Pa,
double
z
,
double
T_C,
102
double
/*m_NaCl*/
= 0.0,
double
eps =
EPS_Z
,
103
bool
immiscible =
false
) {
104
using namespace
m_comp_co2::eos
;
105
// Immiscible/incompressible verification limit (Xeq=0, Yeq=1, const
106
// densities); driven by the Coefficients 'immiscible' option via argsDict.
107
Jet2
p_jet =
Jet2::var_p
(p_Pa);
108
const
double
z_in = std::min(std::max(
z
,
Z_MIN
), 1.0 -
Z_MIN
);
109
Jet2
z_jet = (z_in ==
z
) ?
Jet2::var_z
(
z
) :
Jet2::cst
(z_in);
110
111
Jet2
X,
Y
, rho_a, rho_g;
112
if
(immiscible) {
113
// No mutual solubility (Xeq=0, Yeq=1), constant densities: f_g = z and
114
// the lever rule maps z -> S_g with all p-derivatives exactly zero, so
115
// the species balances reduce to the two saturation equations.
116
X =
Jet2::cst
(0.0);
117
Y
=
Jet2::cst
(1.0);
118
rho_a =
Jet2::cst
(
RHO_A_IMM
);
119
rho_g =
Jet2::cst
(
RHO_G_IMM
);
120
}
else
{
121
Jet2
pb_jet = p_jet*(1.0/
PA_PER_BAR
);
122
bool
liquid =
co2_is_liquid
(T_C, pb_jet.
v
);
123
124
Jet2
Xeq, yH2O, V_jet;
125
_solubility_jet
(T_C, pb_jet, liquid, Xeq, yH2O, V_jet);
126
Jet2
Yeq = 1.0 - yH2O;
127
rho_g = 1.0e6*
recip
(V_jet);
128
129
Jet2
sx =
jsqrt
((z_jet - Xeq)*(z_jet - Xeq) + eps*eps);
130
X = 0.5*(z_jet + Xeq - sx);
131
Jet2
sy =
jsqrt
((z_jet - Yeq)*(z_jet - Yeq) + eps*eps);
132
Y
= 0.5*(z_jet + Yeq + sy);
133
134
double
rho_w =
pure_water_density_gcc
(T_C);
135
double
Vphi = 37.51 - 9.585e-2*T_C + 8.740e-4*T_C*T_C - 5.044e-7*T_C*T_C*T_C;
136
double
c0 = 18.015/rho_w;
137
rho_a = (1.0e6*
recip
(c0 + (Vphi - c0)*X))
138
*
jexp
((p_jet -
P_REF_BRINE
)*
C_F_BRINE
);
// brine compressibility
139
}
140
141
Jet2
f_g = (z_jet - X)*
recip
(
Y
- X);
142
Jet2
G = f_g*
recip
(rho_g);
143
Jet2
L
= (1.0 - f_g)*
recip
(rho_a);
144
Jet2
S_g = G*
recip
(G +
L
);
145
146
FlashState
s
;
147
s
.S_g = S_g.
v
;
s
.X = X.
v
;
s
.Y =
Y
.v;
s
.rho_a = rho_a.
v
;
s
.rho_g = rho_g.
v
;
148
s
.f_g = f_g.
v
;
149
s
.dS_g_dp = S_g.
dp
;
s
.dS_g_dz = S_g.
dz
;
150
s
.dX_dp = X.
dp
;
s
.dX_dz = X.
dz
;
s
.dY_dp =
Y
.dp;
s
.dY_dz =
Y
.dz;
151
s
.drho_a_dp = rho_a.
dp
;
s
.drho_a_dz = rho_a.
dz
;
152
s
.drho_g_dp = rho_g.
dp
;
s
.drho_g_dz = rho_g.
dz
;
153
s
.d2S_g_dp2 = S_g.
dpp
;
s
.d2S_g_dpdz = S_g.
dpz
;
s
.d2S_g_dz2 = S_g.
dzz
;
154
s
.phase = (f_g.
v
< 1.0e-6) ? 0 : (f_g.
v
> 1.0 - 1.0e-6 ? 2 : 1);
155
return
s
;
156
}
157
158
}
// namespace flash
159
}
// namespace m_comp_co2
160
#endif
L
Double L
Definition
Headers.h:72
B
Double * B
Definition
Headers.h:41
s
Double s
Definition
Headers.h:84
z
Double * z
Definition
Headers.h:49
Y
Double * Y
Definition
Headers.h:48
co2_brine_eos.h
jet2.h
m_comp_co2::eos
Definition
co2_brine_eos.h:14
m_comp_co2::eos::A_CO2_1
constexpr double A_CO2_1
Definition
co2_brine_eos.h:22
m_comp_co2::eos::R_CM3_BAR
constexpr double R_CM3_BAR
Definition
co2_brine_eos.h:17
m_comp_co2::eos::poly_logK0
double poly_logK0(const double c[5], double T_C)
Definition
co2_brine_eos.h:46
m_comp_co2::eos::C_F_BRINE
constexpr double C_F_BRINE
Definition
co2_brine_eos.h:32
m_comp_co2::eos::rk_molar_volume
double rk_molar_volume(double T_C, double P_bar, bool liquid)
Definition
co2_brine_eos.h:90
m_comp_co2::eos::B_H2O
constexpr double B_H2O
Definition
co2_brine_eos.h:25
m_comp_co2::eos::V_H2O
constexpr double V_H2O
Definition
co2_brine_eos.h:27
m_comp_co2::eos::co2_is_liquid
bool co2_is_liquid(double T_C, double P_bar)
Definition
co2_brine_eos.h:59
m_comp_co2::eos::P0_BAR
constexpr double P0_BAR
Definition
co2_brine_eos.h:18
m_comp_co2::eos::V_CO2
constexpr double V_CO2
Definition
co2_brine_eos.h:27
m_comp_co2::eos::K0_CO2_L
constexpr double K0_CO2_L[5]
Definition
co2_brine_eos.h:41
m_comp_co2::eos::B_CO2
constexpr double B_CO2
Definition
co2_brine_eos.h:24
m_comp_co2::eos::M_PER_KG_H2O
constexpr double M_PER_KG_H2O
Definition
co2_brine_eos.h:19
m_comp_co2::eos::pure_water_density_gcc
double pure_water_density_gcc(double T_C)
Definition
co2_brine_eos.h:147
m_comp_co2::eos::A_CO2_0
constexpr double A_CO2_0
Definition
co2_brine_eos.h:21
m_comp_co2::eos::K0_H2O
constexpr double K0_H2O[5]
Definition
co2_brine_eos.h:39
m_comp_co2::eos::P_REF_BRINE
constexpr double P_REF_BRINE
Definition
co2_brine_eos.h:33
m_comp_co2::eos::A_H2O_CO2
constexpr double A_H2O_CO2
Definition
co2_brine_eos.h:23
m_comp_co2::eos::K0_CO2_G
constexpr double K0_CO2_G[5]
Definition
co2_brine_eos.h:40
m_comp_co2::flash
Definition
co2_brine_flash.h:18
m_comp_co2::flash::_solubility_jet
void _solubility_jet(double T_C, const Jet2 &P_jet, bool liquid, Jet2 &x_CO2, Jet2 &y_H2O, Jet2 &V)
Definition
co2_brine_flash.h:85
m_comp_co2::flash::RHO_A_IMM
constexpr double RHO_A_IMM
Definition
co2_brine_flash.h:42
m_comp_co2::flash::PA_PER_BAR
constexpr double PA_PER_BAR
Definition
co2_brine_flash.h:20
m_comp_co2::flash::Z_MIN
constexpr double Z_MIN
Definition
co2_brine_flash.h:32
m_comp_co2::flash::RHO_G_IMM
constexpr double RHO_G_IMM
Definition
co2_brine_flash.h:43
m_comp_co2::flash::flashPZ
FlashState flashPZ(double p_Pa, double z, double T_C, double=0.0, double eps=EPS_Z, bool immiscible=false)
Definition
co2_brine_flash.h:101
m_comp_co2::flash::_ln_phi_jet
Jet2 _ln_phi_jet(double T_C, const Jet2 &P_jet, const Jet2 &V, double b_k, double sum_aik)
Definition
co2_brine_flash.h:71
m_comp_co2::flash::EPS_Z
constexpr double EPS_Z
Definition
co2_brine_flash.h:25
m_comp_co2::flash::_V_jet
Jet2 _V_jet(double T_C, const Jet2 &P_jet, bool liquid)
Definition
co2_brine_flash.h:55
m_comp_co2
Definition
co2_brine_eos.h:13
m_comp_co2::jsqrt
Jet2 jsqrt(const Jet2 &s)
Definition
jet2.h:63
m_comp_co2::recip
Jet2 recip(const Jet2 &s)
Definition
jet2.h:52
m_comp_co2::jexp
Jet2 jexp(const Jet2 &s)
Definition
jet2.h:70
m_comp_co2::jlog
Jet2 jlog(const Jet2 &s)
Definition
jet2.h:77
m_comp_co2::Jet2
Definition
jet2.h:21
m_comp_co2::Jet2::dp
double dp
Definition
jet2.h:22
m_comp_co2::Jet2::dpp
double dpp
Definition
jet2.h:22
m_comp_co2::Jet2::var_z
static Jet2 var_z(double z)
Definition
jet2.h:28
m_comp_co2::Jet2::dz
double dz
Definition
jet2.h:22
m_comp_co2::Jet2::dpz
double dpz
Definition
jet2.h:22
m_comp_co2::Jet2::dzz
double dzz
Definition
jet2.h:22
m_comp_co2::Jet2::var_p
static Jet2 var_p(double p)
Definition
jet2.h:27
m_comp_co2::Jet2::cst
static Jet2 cst(double c)
Definition
jet2.h:26
m_comp_co2::Jet2::v
double v
Definition
jet2.h:22
m_comp_co2::flash::FlashState
Definition
co2_brine_flash.h:45
m_comp_co2::flash::FlashState::S_g
double S_g
Definition
co2_brine_flash.h:46
m_comp_co2::flash::FlashState::Y
double Y
Definition
co2_brine_flash.h:46
m_comp_co2::flash::FlashState::dS_g_dz
double dS_g_dz
Definition
co2_brine_flash.h:47
m_comp_co2::flash::FlashState::phase
int phase
Definition
co2_brine_flash.h:50
m_comp_co2::flash::FlashState::drho_a_dp
double drho_a_dp
Definition
co2_brine_flash.h:48
m_comp_co2::flash::FlashState::dX_dp
double dX_dp
Definition
co2_brine_flash.h:47
m_comp_co2::flash::FlashState::rho_g
double rho_g
Definition
co2_brine_flash.h:46
m_comp_co2::flash::FlashState::dX_dz
double dX_dz
Definition
co2_brine_flash.h:47
m_comp_co2::flash::FlashState::d2S_g_dp2
double d2S_g_dp2
Definition
co2_brine_flash.h:49
m_comp_co2::flash::FlashState::rho_a
double rho_a
Definition
co2_brine_flash.h:46
m_comp_co2::flash::FlashState::X
double X
Definition
co2_brine_flash.h:46
m_comp_co2::flash::FlashState::drho_g_dp
double drho_g_dp
Definition
co2_brine_flash.h:48
m_comp_co2::flash::FlashState::drho_g_dz
double drho_g_dz
Definition
co2_brine_flash.h:48
m_comp_co2::flash::FlashState::drho_a_dz
double drho_a_dz
Definition
co2_brine_flash.h:48
m_comp_co2::flash::FlashState::d2S_g_dpdz
double d2S_g_dpdz
Definition
co2_brine_flash.h:49
m_comp_co2::flash::FlashState::dY_dz
double dY_dz
Definition
co2_brine_flash.h:47
m_comp_co2::flash::FlashState::dY_dp
double dY_dp
Definition
co2_brine_flash.h:47
m_comp_co2::flash::FlashState::dS_g_dp
double dS_g_dp
Definition
co2_brine_flash.h:47
m_comp_co2::flash::FlashState::d2S_g_dz2
double d2S_g_dz2
Definition
co2_brine_flash.h:49
m_comp_co2::flash::FlashState::f_g
double f_g
Definition
co2_brine_flash.h:46
Generated on
for proteus by
1.18.0