My Project
PengRobinson.hpp
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18
19 Consult the COPYING file in the top-level source directory of this
20 module for the precise wording of the license and the list of
21 copyright holders.
22*/
28#ifndef OPM_PENG_ROBINSON_HPP
29#define OPM_PENG_ROBINSON_HPP
30
32
36
38
39#include <csignal>
40#include <string>
41
42namespace Opm {
43
57template <class Scalar>
59{
61 static const Scalar R;
62
64 { }
65
66public:
67 static void init(Scalar /*aMin*/, Scalar /*aMax*/, unsigned /*na*/,
68 Scalar /*bMin*/, Scalar /*bMax*/, unsigned /*nb*/)
69 {
70/*
71 // resize the tabulation for the critical points
72 criticalTemperature_.resize(aMin, aMax, na, bMin, bMax, nb);
73 criticalPressure_.resize(aMin, aMax, na, bMin, bMax, nb);
74 criticalMolarVolume_.resize(aMin, aMax, na, bMin, bMax, nb);
75
76 Scalar VmCrit, pCrit, TCrit;
77 for (unsigned i = 0; i < na; ++i) {
78 Scalar a = criticalTemperature_.iToX(i);
79 assert(std::abs(criticalTemperature_.xToI(criticalTemperature_.iToX(i)) - i) < 1e-10);
80
81 for (unsigned j = 0; j < nb; ++j) {
82 Scalar b = criticalTemperature_.jToY(j);
83 assert(std::abs(criticalTemperature_.yToJ(criticalTemperature_.jToY(j)) - j) < 1e-10);
84
85 findCriticalPoint_(TCrit, pCrit, VmCrit, a, b);
86
87 criticalTemperature_.setSamplePoint(i, j, TCrit);
88 criticalPressure_.setSamplePoint(i, j, pCrit);
89 criticalMolarVolume_.setSamplePoint(i, j, VmCrit);
90 }
91 }
92 */
93 }
94
103 template <class Evaluation, class Params>
104 static Evaluation computeVaporPressure(const Params& params, const Evaluation& T)
105 {
106 typedef typename Params::Component Component;
109
110 // initial guess of the vapor pressure
111 Evaluation Vm[3];
112 const Scalar eps = Component::criticalPressure()*1e-10;
113
114 // use the Ambrose-Walton method to get an initial guess of
115 // the vapor pressure
116 Evaluation pVap = ambroseWalton_(params, T);
117
118 // Newton-Raphson method
119 for (unsigned i = 0; i < 5; ++i) {
120 // calculate the molar densities
121 assert(molarVolumes(Vm, params, T, pVap) == 3);
122
123 const Evaluation& f = fugacityDifference_(params, T, pVap, Vm[0], Vm[2]);
124 Evaluation df_dp =
125 fugacityDifference_(params, T, pVap + eps, Vm[0], Vm[2])
126 -
127 fugacityDifference_(params, T, pVap - eps, Vm[0], Vm[2]);
128 df_dp /= 2*eps;
129
130 const Evaluation& delta = f/df_dp;
131 pVap = pVap - delta;
132
133 if (std::abs(scalarValue(delta/pVap)) < 1e-10)
134 break;
135 }
136
137 return pVap;
138 }
139
144 template <class FluidState, class Params>
145 static
146 typename FluidState::Scalar
147 computeMolarVolume(const FluidState& fs,
148 Params& params,
149 unsigned phaseIdx,
150 bool isGasPhase)
151 {
152 Valgrind::CheckDefined(fs.temperature(phaseIdx));
153 Valgrind::CheckDefined(fs.pressure(phaseIdx));
154
155 typedef typename FluidState::Scalar Evaluation;
156
157 Evaluation Vm = 0;
158 Valgrind::SetUndefined(Vm);
159
160 const Evaluation& T = fs.temperature(phaseIdx);
161 const Evaluation& p = fs.pressure(phaseIdx);
162
163 const Evaluation& a = params.a(phaseIdx); // "attractive factor"
164 const Evaluation& b = params.b(phaseIdx); // "co-volume"
165
166 if (!std::isfinite(scalarValue(a))
167 || std::abs(scalarValue(a)) < 1e-30)
168 return std::numeric_limits<Scalar>::quiet_NaN();
169 if (!std::isfinite(scalarValue(b)) || b <= 0)
170 return std::numeric_limits<Scalar>::quiet_NaN();
171
172 const Evaluation& RT= Constants<Scalar>::R*T;
173 const Evaluation& Astar = a*p/(RT*RT);
174 const Evaluation& Bstar = b*p/RT;
175
176 const Evaluation& a1 = 1.0;
177 const Evaluation& a2 = - (1 - Bstar);
178 const Evaluation& a3 = Astar - Bstar*(3*Bstar + 2);
179 const Evaluation& a4 = Bstar*(- Astar + Bstar*(1 + Bstar));
180
181 // ignore the first two results if the smallest
182 // compressibility factor is <= 0.0. (this means that if we
183 // would get negative molar volumes for the liquid phase, we
184 // consider the liquid phase non-existant.)
185 Evaluation Z[3] = {0.0,0.0,0.0};
186 Valgrind::CheckDefined(a1);
187 Valgrind::CheckDefined(a2);
188 Valgrind::CheckDefined(a3);
189 Valgrind::CheckDefined(a4);
190
191 int numSol = cubicRoots(Z, a1, a2, a3, a4);
192 if (numSol == 3) {
193 // the EOS has three intersections with the pressure,
194 // i.e. the molar volume of gas is the largest one and the
195 // molar volume of liquid is the smallest one
196 if (isGasPhase)
197 Vm = max(1e-7, Z[2]*RT/p);
198 else
199 Vm = max(1e-7, Z[0]*RT/p);
200 }
201 else if (numSol == 1) {
202 // the EOS only has one intersection with the pressure,
203 // for the other phase, we take the extremum of the EOS
204 // with the largest distance from the intersection.
205 Evaluation VmCubic = max(1e-7, Z[0]*RT/p);
206 Vm = VmCubic;
207
208 // find the extrema (if they are present)
209 Evaluation Vmin, Vmax, pmin, pmax;
210 if (findExtrema_(Vmin, Vmax,
211 pmin, pmax,
212 a, b, T))
213 {
214 if (isGasPhase)
215 Vm = std::max(Vmax, VmCubic);
216 else {
217 if (Vmin > 0)
218 Vm = std::min(Vmin, VmCubic);
219 else
220 Vm = VmCubic;
221 }
222 }
223 else {
224 // the EOS does not exhibit any physically meaningful
225 // extrema, and the fluid is critical...
226 Vm = VmCubic;
227 handleCriticalFluid_(Vm, fs, params, phaseIdx, isGasPhase);
228 }
229 }
230
231 Valgrind::CheckDefined(Vm);
232 assert(std::isfinite(scalarValue(Vm)));
233 assert(Vm > 0);
234 return Vm;
235 }
236
247 template <class Evaluation, class Params>
248 static Evaluation computeFugacityCoeffient(const Params& params)
249 {
250 const Evaluation& T = params.temperature();
251 const Evaluation& p = params.pressure();
252 const Evaluation& Vm = params.molarVolume();
253
254 const Evaluation& RT = Constants<Scalar>::R*T;
255 const Evaluation& Z = p*Vm/RT;
256 const Evaluation& Bstar = p*params.b() / RT;
257
258 const Evaluation& tmp =
259 (Vm + params.b()*(1 + std::sqrt(2))) /
260 (Vm + params.b()*(1 - std::sqrt(2)));
261 const Evaluation& expo = - params.a()/(RT * 2 * params.b() * std::sqrt(2));
262 const Evaluation& fugCoeff =
263 exp(Z - 1) / (Z - Bstar) *
264 pow(tmp, expo);
265
266 return fugCoeff;
267 }
268
279 template <class Evaluation, class Params>
280 static Evaluation computeFugacity(const Params& params)
281 { return params.pressure()*computeFugacityCoeff(params); }
282
283protected:
284 template <class FluidState, class Params, class Evaluation = typename FluidState::Scalar>
285 static void handleCriticalFluid_(Evaluation& Vm,
286 const FluidState& /*fs*/,
287 const Params& params,
288 unsigned phaseIdx,
289 bool isGasPhase)
290 {
291 Evaluation Tcrit, pcrit, Vcrit;
292 findCriticalPoint_(Tcrit,
293 pcrit,
294 Vcrit,
295 params.a(phaseIdx),
296 params.b(phaseIdx));
297
298
299 //Evaluation Vcrit = criticalMolarVolume_.eval(params.a(phaseIdx), params.b(phaseIdx));
300
301 if (isGasPhase)
302 Vm = max(Vm, Vcrit);
303 else
304 Vm = min(Vm, Vcrit);
305 }
306
307 template <class Evaluation>
308 static void findCriticalPoint_(Evaluation& Tcrit,
309 Evaluation& pcrit,
310 Evaluation& Vcrit,
311 const Evaluation& a,
312 const Evaluation& b)
313 {
314 Evaluation minVm(0);
315 Evaluation maxVm(1e30);
316
317 Evaluation minP(0);
318 Evaluation maxP(1e30);
319
320 // first, we need to find an isotherm where the EOS exhibits
321 // a maximum and a minimum
322 Evaluation Tmin = 250; // [K]
323 for (unsigned i = 0; i < 30; ++i) {
324 bool hasExtrema = findExtrema_(minVm, maxVm, minP, maxP, a, b, Tmin);
325 if (hasExtrema)
326 break;
327 Tmin /= 2;
328 };
329
330 Evaluation T = Tmin;
331
332 // Newton's method: Start at minimum temperature and minimize
333 // the "gap" between the extrema of the EOS
334 unsigned iMax = 100;
335 for (unsigned i = 0; i < iMax; ++i) {
336 // calculate function we would like to minimize
337 Evaluation f = maxVm - minVm;
338
339 // check if we're converged
340 if (f < 1e-10 || (i == iMax - 1 && f < 1e-8)) {
341 Tcrit = T;
342 pcrit = (minP + maxP)/2;
343 Vcrit = (maxVm + minVm)/2;
344 return;
345 }
346
347 // backward differences. Forward differences are not
348 // robust, since the isotherm could be critical if an
349 // epsilon was added to the temperature. (this is case
350 // rarely happens, though)
351 const Scalar eps = - 1e-11;
352 assert(findExtrema_(minVm, maxVm, minP, maxP, a, b, T + eps));
353 assert(std::isfinite(scalarValue(maxVm)));
354 Evaluation fStar = maxVm - minVm;
355
356 // derivative of the difference between the maximum's
357 // molar volume and the minimum's molar volume regarding
358 // temperature
359 Evaluation fPrime = (fStar - f)/eps;
360 if (std::abs(scalarValue(fPrime)) < 1e-40) {
361 Tcrit = T;
362 pcrit = (minP + maxP)/2;
363 Vcrit = (maxVm + minVm)/2;
364 return;
365 }
366
367 // update value for the current iteration
368 Evaluation delta = f/fPrime;
369 assert(std::isfinite(scalarValue(delta)));
370 if (delta > 0)
371 delta = -10;
372
373 // line search (we have to make sure that both extrema
374 // still exist after the update)
375 for (unsigned j = 0; ; ++j) {
376 if (j >= 20) {
377 if (f < 1e-8) {
378 Tcrit = T;
379 pcrit = (minP + maxP)/2;
380 Vcrit = (maxVm + minVm)/2;
381 return;
382 }
383
384 const std::string msg =
385 "Could not determine the critical point for a="
386 + std::to_string(getValue(a))
387 + ", b=" + std::to_string(getValue(b));
388 throw NumericalProblem(msg);
389 }
390
391 if (findExtrema_(minVm, maxVm, minP, maxP, a, b, T - delta)) {
392 // if the isotherm for T - delta exhibits two
393 // extrema the update is finished
394 T -= delta;
395 break;
396 }
397 else
398 delta /= 2;
399 };
400 }
401
402 assert(false);
403 }
404
405 // find the two molar volumes where the EOS exhibits extrema and
406 // which are larger than the covolume of the phase
407 template <class Evaluation>
408 static bool findExtrema_(Evaluation& Vmin,
409 Evaluation& Vmax,
410 Evaluation& /*pMin*/,
411 Evaluation& /*pMax*/,
412 const Evaluation& a,
413 const Evaluation& b,
414 const Evaluation& T)
415 {
416 Scalar u = 2;
417 Scalar w = -1;
418
419 const Evaluation& RT = Constants<Scalar>::R*T;
420 // calculate coefficients of the 4th order polynominal in
421 // monomial basis
422 const Evaluation& a1 = RT;
423 const Evaluation& a2 = 2*RT*u*b - 2*a;
424 const Evaluation& a3 = 2*RT*w*b*b + RT*u*u*b*b + 4*a*b - u*a*b;
425 const Evaluation& a4 = 2*RT*u*w*b*b*b + 2*u*a*b*b - 2*a*b*b;
426 const Evaluation& a5 = RT*w*w*b*b*b*b - u*a*b*b*b;
427
428 assert(std::isfinite(scalarValue(a1)));
429 assert(std::isfinite(scalarValue(a2)));
430 assert(std::isfinite(scalarValue(a3)));
431 assert(std::isfinite(scalarValue(a4)));
432 assert(std::isfinite(scalarValue(a5)));
433
434 // Newton method to find first root
435
436 // if the values which we got on Vmin and Vmax are usefull, we
437 // will reuse them as initial value, else we will start 10%
438 // above the covolume
439 Evaluation V = b*1.1;
440 Evaluation delta = 1.0;
441 for (unsigned i = 0; std::abs(scalarValue(delta)) > 1e-12; ++i) {
442 const Evaluation& f = a5 + V*(a4 + V*(a3 + V*(a2 + V*a1)));
443 const Evaluation& fPrime = a4 + V*(2*a3 + V*(3*a2 + V*4*a1));
444
445 if (std::abs(scalarValue(fPrime)) < 1e-20) {
446 // give up if the derivative is zero
447 return false;
448 }
449
450
451 delta = f/fPrime;
452 V -= delta;
453
454 if (i > 200) {
455 // give up after 200 iterations...
456 return false;
457 }
458 }
459 assert(std::isfinite(scalarValue(V)));
460
461 // polynomial division
462 Evaluation b1 = a1;
463 Evaluation b2 = a2 + V*b1;
464 Evaluation b3 = a3 + V*b2;
465 Evaluation b4 = a4 + V*b3;
466
467 // invert resulting cubic polynomial analytically
468 std::array<Evaluation,4> allV;
469 allV[0] = V;
470 int numSol = 1 + invertCubicPolynomial<Evaluation>(allV.data() + 1, b1, b2, b3, b4);
471
472 // sort all roots of the derivative
473 std::sort(allV.begin(), allV.begin() + numSol);
474
475 // check whether the result is physical
476 if (numSol != 4 || allV[numSol - 2] < b) {
477 // the second largest extremum is smaller than the phase's
478 // covolume which is physically impossible
479 return false;
480 }
481
482
483 // it seems that everything is okay...
484 Vmin = allV[numSol - 2];
485 Vmax = allV[numSol - 1];
486 return true;
487 }
488
501 template <class Evaluation, class Params>
502 static Evaluation ambroseWalton_(const Params& /*params*/, const Evaluation& T)
503 {
504 typedef typename Params::Component Component;
505
506 const Evaluation& Tr = T / Component::criticalTemperature();
507 const Evaluation& tau = 1 - Tr;
508 const Evaluation& omega = Component::acentricFactor();
509
510 const Evaluation& f0 = (tau*(-5.97616 + sqrt(tau)*(1.29874 - tau*0.60394)) - 1.06841*pow(tau, 5))/Tr;
511 const Evaluation& f1 = (tau*(-5.03365 + sqrt(tau)*(1.11505 - tau*5.41217)) - 7.46628*pow(tau, 5))/Tr;
512 const Evaluation& f2 = (tau*(-0.64771 + sqrt(tau)*(2.41539 - tau*4.26979)) + 3.25259*pow(tau, 5))/Tr;
513
514 return Component::criticalPressure()*std::exp(f0 + omega * (f1 + omega*f2));
515 }
516
527 template <class Evaluation, class Params>
528 static Evaluation fugacityDifference_(const Params& params,
529 const Evaluation& T,
530 const Evaluation& p,
531 const Evaluation& VmLiquid,
532 const Evaluation& VmGas)
533 { return fugacity(params, T, p, VmLiquid) - fugacity(params, T, p, VmGas); }
534
535
536};
537
538} // namespace Opm
539
540#endif
Provides the OPM specific exception classes.
Provides free functions to invert polynomials of degree 1, 2 and 3.
Implements a scalar function that depends on two variables and which is sampled on an uniform X-Y gri...
Abstract base class of a pure chemical species.
Definition: Component.hpp:42
static Scalar acentricFactor()
Returns the acentric factor of the component.
Definition: Component.hpp:110
static Scalar criticalPressure()
Returns the critical pressure in of the component.
Definition: Component.hpp:103
static Scalar criticalTemperature()
Returns the critical temperature in of the component.
Definition: Component.hpp:97
A central place for various physical constants occuring in some equations.
Definition: Constants.hpp:41
static const Scalar R
The ideal gas constant [J/(mol K)].
Definition: Constants.hpp:45
Implements the Peng-Robinson equation of state for liquids and gases.
Definition: PengRobinson.hpp:59
static Evaluation computeFugacityCoeffient(const Params &params)
Returns the fugacity coefficient for a given pressure and molar volume.
Definition: PengRobinson.hpp:248
static Evaluation fugacityDifference_(const Params &params, const Evaluation &T, const Evaluation &p, const Evaluation &VmLiquid, const Evaluation &VmGas)
Returns the difference between the liquid and the gas phase fugacities in [bar].
Definition: PengRobinson.hpp:528
static Evaluation computeVaporPressure(const Params &params, const Evaluation &T)
Predicts the vapor pressure for the temperature given in setTP().
Definition: PengRobinson.hpp:104
static Evaluation ambroseWalton_(const Params &, const Evaluation &T)
The Ambrose-Walton method to estimate the vapor pressure.
Definition: PengRobinson.hpp:502
static FluidState::Scalar computeMolarVolume(const FluidState &fs, Params &params, unsigned phaseIdx, bool isGasPhase)
Computes molar volumes where the Peng-Robinson EOS is true.
Definition: PengRobinson.hpp:147
static Evaluation computeFugacity(const Params &params)
Returns the fugacity coefficient for a given pressure and molar volume.
Definition: PengRobinson.hpp:280
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
unsigned cubicRoots(SolContainer *sol, Scalar a, Scalar b, Scalar c, Scalar d)
Invert a cubic polynomial analytically.
Definition: PolynomialUtils.hpp:331