Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

# -*- coding: utf-8 -*- 

'''Chemical Engineering Design Library (ChEDL). Utilities for process modeling. 

Copyright (C) 2016, Caleb Bell <Caleb.Andrew.Bell@gmail.com> 

 

Permission is hereby granted, free of charge, to any person obtaining a copy 

of this software and associated documentation files (the "Software"), to deal 

in the Software without restriction, including without limitation the rights 

to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 

copies of the Software, and to permit persons to whom the Software is 

furnished to do so, subject to the following conditions: 

 

The above copyright notice and this permission notice shall be included in all 

copies or substantial portions of the Software. 

 

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 

SOFTWARE.''' 

 

from __future__ import division 

 

__all__ = ['K', 'Rachford_Rice_flash_error', 'flash', 'dew_at_T', 

'bubble_at_T', 'identify_phase', 'mixture_phase_methods', 

'identify_phase_mixture', 'Pbubble_mixture', 'Pdew_mixture'] 

 

from scipy.optimize import fsolve 

from thermo.utils import exp, log 

import numpy as np 

import os 

from thermo.utils import none_and_length_check 

 

from scipy.constants import R 

 

folder = os.path.join(os.path.dirname(__file__), 'Phase Change') 

 

 

def K(P, Psat, fugacity=1, gamma=1): 

''' 

>>> K(101325, 3000.) 

0.029607698001480384 

>>> K(101325, 3000., fugacity=0.9, gamma=2.4) 

0.07895386133728102 

''' 

# http://www.jmcampbell.com/tip-of-the-month/2006/09/how-to-determine-k-values/ 

_K = Psat*gamma/P/fugacity 

return _K 

 

 

 

### Solutions using a existing algorithms 

def Rachford_Rice_flash_error(V_over_F, zs, ks): 

total = 0 

for i in range(len(zs)): 

total += zs[i]*(ks[i]-1)/(1+V_over_F*(ks[i]-1)) 

return total 

 

 

def flash(P, zs, Psats, fugacities=None, gammas=None): 

if not fugacities: 

fugacities = [1 for i in range(len(zs))] 

if not gammas: 

gammas = [1 for i in range(len(zs))] 

if not none_and_length_check((zs, Psats, fugacities, gammas)): 

raise Exception('Input dimentions are inconsistent or some input parameters are missing.') 

ks = [K(P, Psats[i], fugacities[i], gammas[i]) for i in range(len(zs))] 

 

def valid_range(zs, ks): 

valid = True 

if sum([zs[i]*ks[i] for i in range(len(ks))]) < 1: 

valid = False 

if sum([zs[i]/ks[i] for i in range(len(ks))]) < 1: 

valid = False 

return valid 

if not valid_range(zs, ks): 

raise Exception('Solution does not exist') 

# zs = [0.5, 0.3, 0.2] practice solution 

# ks = [1.685, 0.742, 0.532] 

x0 = np.array([.5]) 

V_over_F = fsolve(Rachford_Rice_flash_error, x0=x0, args=(zs, ks))[0] 

if V_over_F < 0: 

# print zs, ks 

raise Exception('V_over_F is negative!') 

xs = [zs[i]/(1+V_over_F*(ks[i]-1)) for i in range(len(zs))] 

ys = [ks[i]*xs[i] for i in range(len(zs))] 

return xs, ys, V_over_F 

 

 

 

 

def dew_at_T(zs, Psats, fugacities=None, gammas=None): 

''' 

>>> dew_at_T([0.5, 0.5], [1400, 7000]) 

2333.3333333333335 

>>> dew_at_T([0.5, 0.5], [1400, 7000], gammas=[1.1, .75]) 

2381.443298969072 

>>> dew_at_T([0.5, 0.5], [1400, 7000], gammas=[1.1, .75], fugacities=[.995, 0.98]) 

2401.621874512658 

''' 

if not fugacities: 

fugacities = [1 for i in range(len(Psats))] 

if not gammas: 

gammas = [1 for i in range(len(Psats))] 

if not none_and_length_check((zs, Psats, fugacities, gammas)): 

raise Exception('Input dimentions are inconsistent or some input parameters are missing.') 

P = 1/sum(zs[i]*fugacities[i]/Psats[i]/gammas[i] for i in range(len(zs))) 

return P 

 

 

def bubble_at_T(zs, Psats, fugacities=None, gammas=None): 

''' 

>>> bubble_at_T([0.5, 0.5], [1400, 7000]) 

4200.0 

>>> bubble_at_T([0.5, 0.5], [1400, 7000], gammas=[1.1, .75]) 

3395.0 

>>> bubble_at_T([0.5, 0.5], [1400, 7000], gammas=[1.1, .75], fugacities=[.995, 0.98]) 

3452.440775305097 

''' 

if not fugacities: 

fugacities = [1 for i in range(len(Psats))] 

if not gammas: 

gammas = [1 for i in range(len(Psats))] 

if not none_and_length_check((zs, Psats, fugacities, gammas)): 

raise Exception('Input dimentions are inconsistent or some input parameters are missing.') 

P = sum(zs[i]*Psats[i]*gammas[i]/fugacities[i] for i in range(len(zs))) 

return P 

 

 

def identify_phase(T=None, P=None, Tm=None, Tb=None, Tc=None, Psat=None): 

''' 

>>> identify_phase(T=280, P=101325, Tm=273.15, Psat=991) 

'l' 

>>> identify_phase(T=480, P=101325, Tm=273.15, Psat=1791175) 

'g' 

>>> identify_phase(T=650, P=10132500000, Tm=273.15, Psat=None, Tc=647.3) 

'g' 

>>> identify_phase(T=250, P=100, Tm=273.15) 

's' 

>>> identify_phase(T=500, P=101325) 

''' 

phase = None 

if not T or not P: 

raise Exception('Phase identification requires T and P.') 

if Tm and T <= Tm: 

phase = 's' 

elif Tc and T > Tc: 

phase = 'g' 

elif Psat: 

if P < Psat: 

phase = 'g' 

elif P > Psat: 

phase = 'l' 

elif Tb and Tm: 

if 9E4 < P < 1.1E5: # mild tolerance 

if T < Tb: 

phase = 'l' 

else: 

phase = 'g' 

elif P > 1.1E5 and T < Tb: 

phase = 'l' 

else: 

phase = None 

else: 

phase = None 

return phase 

 

 

mixture_phase_methods = ['IDEAL_VLE', 'SUPERCRITICAL_T', 'SUPERCRITICAL_P', 'IDEAL_VLE_SUPERCRITICAL'] 

 

def identify_phase_mixture(T=None, P=None, zs=None, Tcs=None, Pcs=None, 

Psats=None, CASRNs=None, 

AvailableMethods=False, Method=None): # pragma: no cover 

''' 

>>> identify_phase_mixture(T=280, P=5000., zs=[0.5, 0.5], Psats=[1400, 7000]) 

('l', [0.5, 0.5], None, 0) 

>>> identify_phase_mixture(T=280, P=3000., zs=[0.5, 0.5], Psats=[1400, 7000]) 

('two-phase', [0.7142857142857143, 0.2857142857142857], [0.33333333333333337, 0.66666666666666663], 0.5625) 

>>> identify_phase_mixture(T=280, P=800., zs=[0.5, 0.5], Psats=[1400, 7000]) 

('g', None, [0.5, 0.5], 1) 

>>> identify_phase_mixture(T=280, P=800., zs=[0.5, 0.5]) 

(None, None, None, None) 

''' 

def list_methods(): 

methods = [] 

if none_and_length_check((Psats, zs)): 

methods.append('IDEAL_VLE') 

if none_and_length_check([Tcs]) and all([T >= i for i in Tcs]): 

methods.append('SUPERCRITICAL_T') 

if none_and_length_check([Pcs]) and all([P >= i for i in Pcs]): 

methods.append('SUPERCRITICAL_P') 

if none_and_length_check((zs, Tcs)) and any([T > Tc for Tc in Tcs]): 

methods.append('IDEAL_VLE_SUPERCRITICAL') 

methods.append('NONE') 

return methods 

if AvailableMethods: 

return list_methods() 

if not Method: 

Method = list_methods()[0] 

# This is the calculate, given the method section 

xs, ys, phase, V_over_F = None, None, None, None 

if Method == 'IDEAL_VLE': 

Pdew = dew_at_T(zs, Psats) 

Pbubble = bubble_at_T(zs, Psats) 

if P >= Pbubble: 

phase = 'l' 

ys = None 

xs = zs 

V_over_F = 0 

elif P <= Pdew: 

phase = 'g' 

ys = zs 

xs = None 

V_over_F = 1 

elif Pdew < P < Pbubble: 

xs, ys, V_over_F = flash(P, zs, Psats) 

phase = 'two-phase' 

elif Method == 'SUPERCRITICAL_T': 

if all([T >= i for i in Tcs]): 

phase = 'g' 

else: # The following is nonsensical 

phase = 'two-phase' 

elif Method == 'SUPERCRITICAL_P': 

if all([P >= i for i in Pcs]): 

phase = 'g' 

else: # The following is nonsensical 

phase = 'two-phase' 

elif Method == 'IDEAL_VLE_SUPERCRITICAL': 

Psats = list(Psats) 

for i in range(len(Psats)): 

if not Psats[i] and Tcs[i] and Tcs[i] <= T: 

Psats[i] = 1E8 

Pdew = dew_at_T(zs, Psats) 

Pbubble = 1E99 

if P >= Pbubble: 

phase = 'l' 

ys = None 

xs = zs 

V_over_F = 0 

elif P <= Pdew: 

phase = 'g' 

ys = zs 

xs = None 

V_over_F = 1 

elif Pdew < P < Pbubble: 

xs, ys, V_over_F = flash(P, zs, Psats) 

phase = 'two-phase' 

 

elif Method == 'NONE': 

pass 

else: 

raise Exception('Failure in in function') 

return phase, xs, ys, V_over_F 

 

 

def Pbubble_mixture(T=None, zs=None, Psats=None, CASRNs=None, 

AvailableMethods=False, Method=None): # pragma: no cover 

''' 

>>> Pbubble_mixture(zs=[0.5, 0.5], Psats=[1400, 7000]) 

4200.0 

''' 

def list_methods(): 

methods = [] 

if none_and_length_check((Psats, zs)): 

methods.append('IDEAL_VLE') 

methods.append('NONE') 

return methods 

if AvailableMethods: 

return list_methods() 

if not Method: 

Method = list_methods()[0] 

# This is the calculate, given the method section 

if Method == 'IDEAL_VLE': 

Pbubble = bubble_at_T(zs, Psats) 

elif Method == 'NONE': 

Pbubble = None 

else: 

raise Exception('Failure in in function') 

return Pbubble 

 

 

def Pdew_mixture(T=None, zs=None, Psats=None, CASRNs=None, 

AvailableMethods=False, Method=None): # pragma: no cover 

''' 

>>> Pdew_mixture(zs=[0.5, 0.5], Psats=[1400, 7000]) 

2333.3333333333335 

''' 

def list_methods(): 

methods = [] 

if none_and_length_check((Psats, zs)): 

methods.append('IDEAL_VLE') 

methods.append('NONE') 

return methods 

if AvailableMethods: 

return list_methods() 

if not Method: 

Method = list_methods()[0] 

# This is the calculate, given the method section 

if Method == 'IDEAL_VLE': 

Pdew = dew_at_T(zs, Psats) 

elif Method == 'NONE': 

Pdew = None 

else: 

raise Exception('Failure in in function') 

return Pdew