OpenCAEPoro  v0.5.0
A simulator for multicomponent porous media flow
MixtureComp.hpp
Go to the documentation of this file.
1 
12 #ifndef __MIXTURECOMP_HEADER__
13 #define __MIXTURECOMP_HEADER__
14 
15 // Standard header files
16 #include <algorithm>
17 #include <math.h>
18 #include <vector>
19 
20 // OpenCAEPoro header files
21 #include "DenseMat.hpp"
22 #include "Mixture.hpp"
23 #include "OCPTable.hpp"
24 
25 using namespace std;
26 
29 {
30 public:
33  OCP_DBL Ktol{1E-4};
34  OCP_DBL dYtol{1E-6};
35  OCP_DBL eYt{1E-8};
39  // test
41  OCP_DBL curSk;
42 };
43 
46 {
47 public:
53  // test
55 };
56 
59 {
60 public:
66  // test
68 };
69 
71 class NRparamSP
72 {
73 public:
79  // test
81 };
82 
84 class RRparam
85 {
86 public:
90  // test
92 };
93 
95 {
96  friend class MixtureComp;
97 
98 private:
99  SSMparamSTA SSMsta;
100  NRparamSTA NRsta;
101  SSMparamSP SSMsp;
102  NRparamSP NRsp;
103  RRparam RR;
104 };
105 
106 class COMP
107 {
108 public:
109  COMP() = default;
110  COMP(const vector<string>& comp);
111 
112 public:
113  string name;
123 };
124 
125 class MixtureComp : public Mixture
126 {
127 
128 public:
129  OCP_DBL GetErrorPEC() override { return ePEC; }
130  void OutMixtureIters() const override;
131 
132 private:
133  // total iters
134  OCP_ULL itersSSMSTA{0};
135  OCP_ULL itersNRSTA{0};
136  OCP_ULL itersSSMSP{0};
137  OCP_ULL itersNRSP{0};
138  OCP_ULL itersRR{0};
139  // total counts, one count may contain many iters
140  OCP_ULL countsSSMSTA{0};
141  OCP_ULL countsNRSTA{0};
142  OCP_ULL countsSSMSP{0};
143  OCP_ULL countsNRSP{0};
144  OCP_ULL countsRR{0};
145 
146  OCP_ULL countsFailed{0};
147  // phase equilibrium calculation error
148  // if NP = 1, it's from phase stable analysis, if skiped, it's 0
149  // if NP > 1, it's from phase spliting calculation
150  OCP_DBL ePEC;
151 
152 public:
153  MixtureComp() = default;
154 
155  MixtureComp(const ParamReservoir& rs_param, const USI& i)
156  : MixtureComp(rs_param.comsParam, i)
157  {
158  // water property
159  if (rs_param.PVTW_T.data.size() != 0) {
160  PVTW.Setup(rs_param.PVTW_T.data[i]);
161  if (rs_param.gravity.activity)
162  std_RhoW = RHOW_STD * rs_param.gravity.data[1];
163  if (rs_param.density.activity) std_RhoW = RHOW_STD;
164  data.resize(5);
165  cdata.resize(5);
166  }
167  };
168 
169  MixtureComp(const ComponentParam& param, const USI& i);
170 
171  void InitPTZ(const OCP_DBL& Pin, const OCP_DBL& Tin, const OCP_DBL* Ziin)
172  {
173  P = Pin;
174  T = Tin;
175  Dcopy(NC, &zi[0], Ziin);
176  }
177  void InitPTN(const OCP_DBL& Pin, const OCP_DBL& Tin, const OCP_DBL* Niin)
178  {
179  P = Pin;
180  T = Tin;
181  Dcopy(numCom, &Ni[0], Niin);
182  Nh = Dnorm1(NC, &Ni[0]);
183  for (USI i = 0; i < NC; i++) zi[i] = Ni[i] / Nh;
184  }
185 
186  void Flash(const OCP_DBL& Pin, const OCP_DBL& Tin, const OCP_DBL* Niin) override;
187 
188  void InitFlashIMPEC(const OCP_DBL& Pin,
189  const OCP_DBL& Pbbin,
190  const OCP_DBL& Tin,
191  const OCP_DBL* Sjin,
192  const OCP_DBL& Vpore,
193  const OCP_DBL* Ziin,
194  const OCP_USI& bId) override;
195 
196  void InitFlashFIM(const OCP_DBL& Pin,
197  const OCP_DBL& Pbbin,
198  const OCP_DBL& Tin,
199  const OCP_DBL* Sjin,
200  const OCP_DBL& Vpore,
201  const OCP_DBL* Ziin,
202  const OCP_USI& bId) override;
203 
204  void InitFlashFIMn(const OCP_DBL& Pin,
205  const OCP_DBL& Pbbin,
206  const OCP_DBL& Tin,
207  const OCP_DBL* Sjin,
208  const OCP_DBL& Vpore,
209  const OCP_DBL* Ziin,
210  const OCP_USI& bId) override;
211 
212  // ftype = 0, flash from single phase
213  // ftype = 1, skip phase stability analysis and num of phase = 1
214  // ftype = 1, skip phase stability analysis and num of phase = 2
215  void FlashIMPEC(const OCP_DBL& Pin,
216  const OCP_DBL& Tin,
217  const OCP_DBL* Niin,
218  const USI& lastNP,
219  const OCP_DBL* xijin,
220  const OCP_USI& bId) override;
221 
222  void CalFlash();
223 
224  void FlashFIM(const OCP_DBL& Pin,
225  const OCP_DBL& Tin,
226  const OCP_DBL* Niin,
227  const OCP_DBL* Sjin,
228  const USI& lastNP,
229  const OCP_DBL* xijin,
230  const OCP_USI& bId) override;
231 
232  void FlashFIMn(const OCP_DBL& Pin,
233  const OCP_DBL& Tin,
234  const OCP_DBL* Niin,
235  const OCP_DBL* Sjin,
236  const OCP_DBL* xijin,
237  const OCP_DBL* njin,
238  const USI* phaseExistin,
239  const USI& lastNP,
240  const OCP_USI& bId) override;
241 
242  OCP_DBL
243  XiPhase(const OCP_DBL& Pin,
244  const OCP_DBL& Tin,
245  const OCP_DBL* Ziin,
246  const USI& tarPhase) override;
247 
248  OCP_DBL
249  RhoPhase(const OCP_DBL& Pin,
250  const OCP_DBL& Pbb,
251  const OCP_DBL& Tin,
252  const OCP_DBL* Ziin,
253  const USI& tarPhase) override;
254 
255  // For Well
256  void SetupWellOpt(WellOpt& opt,
257  const vector<SolventINJ>& sols,
258  const OCP_DBL& Psurf,
259  const OCP_DBL& Tsurf) override;
260  void CalProdWeight(const OCP_DBL& Pin,
261  const OCP_DBL& Tin,
262  const OCP_DBL* Niin,
263  const vector<OCP_DBL>& prodPhase,
264  vector<OCP_DBL>& prodWeight) override;
265 
266  void CalProdRate(const OCP_DBL& Pin,
267  const OCP_DBL& Tin,
268  const OCP_DBL* Niin,
269  vector<OCP_DBL>& prodRate) override;
270 
271  OCP_DBL CalInjWellEnthalpy(const OCP_DBL& Tin, const OCP_DBL* Ziin) override
272  {
273  OCP_ABORT("Can not be used in Compositional Model!");
274  }
275 
276  void CallId();
277 
278 private:
279  // Basic Components Informations
280  vector<string> Cname;
281  vector<OCP_DBL> Tc;
282  vector<OCP_DBL> Pc;
283  vector<OCP_DBL> Vc;
284  vector<OCP_DBL> MWC;
285  vector<OCP_DBL> Acf;
286  vector<OCP_DBL> OmegaA;
287  vector<OCP_DBL> OmegaB;
288  vector<OCP_DBL> Vshift;
289  vector<OCP_DBL> Zc;
290  // for viscosity calculation
291  vector<OCP_DBL> Vcvis;
292  vector<OCP_DBL> Zcvis;
293  vector<OCP_DBL> LBCcoef;
294  vector<OCP_DBL> BIC;
295 
296  // Initial properties
297  USI NC;
298  USI NPmax;
299  OCP_DBL P;
300  OCP_DBL T;
301  vector<OCP_DBL> zi;
302  vector<COMP> comp;
303  USI lId;
304  EoScontrol EoSctrl;
305 
306  vector<OCP_DBL> Plist;
307  vector<OCP_DBL> Tlist;
308  vector<OCP_DBL> Ytlist;
309 
310 private:
311  OCPTable PVTW;
312  OCP_DBL std_RhoW;
313  vector<OCP_DBL> data;
315  vector<OCP_DBL> cdata;
317 
318 public:
319  // EoS Function
320  // Allocate memory for EoS
321  void AllocateEoS();
322  // Setup and Solve EoS for specified A,B
323  void SolEoS(OCP_DBL& ZjT, const OCP_DBL& AjT, const OCP_DBL& BjT) const;
324  // Calculate Ai and Bi
325  void CalAiBi();
326  // Calculate Aj and Bj with specified xj
327  void CalAjBj(OCP_DBL& AjT, OCP_DBL& BjT, const vector<OCP_DBL>& xj) const;
328  void CalAjBj(OCP_DBL& AjT, OCP_DBL& BjT, const OCP_DBL* xj) const;
330  USI CubicRoot(const OCP_DBL& a,
331  const OCP_DBL& b,
332  const OCP_DBL& c,
333  const OCP_BOOL& NTflag = OCP_FALSE) const;
335  void PrintZtmp()
336  {
337  for (auto z : Ztmp) cout << z << "\t";
338  }
339 
340 private:
341  // EoS Variables
342  vector<OCP_DBL> Ai;
343  vector<OCP_DBL> Bi;
344  vector<OCP_DBL> Aj;
345  vector<OCP_DBL> Bj;
346  vector<OCP_DBL> Zj;
347  mutable vector<OCP_DBL> Ztmp;
348 
349  // PR default
350  OCP_DBL delta1 = 2.41421356237;
351  OCP_DBL delta2 = -0.41421356237;
352  OCP_DBL delta1P2;
353  OCP_DBL delta1M2;
354  OCP_DBL delta1T2;
355 
356 public:
357  // Phase Function
358  // Allocate memoery for phase variables
359  void AllocatePhase();
360  void
361  CalFugPhi(vector<OCP_DBL>& phiT, vector<OCP_DBL>& fugT, const vector<OCP_DBL>& xj);
362  void CalFugPhi(OCP_DBL* phiT, OCP_DBL* fugT, const OCP_DBL* xj);
363  void CalFugPhi(OCP_DBL* fugT, const OCP_DBL* xj);
364  void CalFugPhiAll();
365  void CalMW();
366  void CalVfXiRho();
367  void CalSaturation();
368  USI FindMWmax();
369  void x2n();
370  void PrintX();
371 
372 private:
373  // Phase Variables
374  USI lNP{0};
375  USI NP;
376  USI inputNP;
377  OCP_DBL Nh;
378  vector<OCP_DBL> vC;
379  vector<OCP_DBL>
380  nu;
381  vector<vector<OCP_DBL>>
382  x;
383  vector<vector<OCP_DBL>> phi;
385  vector<vector<OCP_DBL>>
386  fug;
387  vector<vector<OCP_DBL>>
388  n;
389  vector<vector<OCP_DBL>> ln;
390  OCP_DBL GibbsEnergyB;
391  OCP_DBL GibbsEnergyE;
392  vector<OCP_DBL> xiC;
393  vector<OCP_DBL> rhoC;
394  vector<OCP_DBL> MW;
395  vector<USI> phaseLabel;
396 
397 public:
398  // Method Function
399  // Allocate memoery for Method variables
400  void AllocateMethod();
401  void PhaseEquilibrium();
402  void CalKwilson();
403  OCP_BOOL PhaseStable();
404  OCP_BOOL StableSSM(const USI& Id);
405  OCP_BOOL StableSSM01(const USI& Id);
406  OCP_BOOL StableNR(const USI& Id);
407  void CalFugXSTA();
408  void AssembleJmatSTA();
409  OCP_BOOL CheckSplit();
410  void PhaseSplit();
411  void SplitSSM(const OCP_BOOL& flag);
412  void SplitSSM2(const OCP_BOOL& flag);
413  void SplitSSM3(const OCP_BOOL& flag);
414  void RachfordRice2();
415  void RachfordRice2P();
416  void RachfordRice3();
417  void UpdateXRR();
418  void SplitBFGS();
419  void SplitNR();
420  void CalResSP();
421  void CalFugNAll(const OCP_BOOL& Znflag = OCP_TRUE);
422  void PrintFugN();
423  void AssembleJmatSP();
424  OCP_DBL CalStepNRsp();
425 
426 private:
427  // Method Variables
428  USI testPId;
429  vector<vector<OCP_DBL>> Kw;
430  vector<vector<OCP_DBL>> Ks;
431  vector<OCP_DBL> lKs;
432  OCP_DBL Asta, Bsta, Zsta;
433  vector<OCP_DBL> phiSta;
434  vector<OCP_DBL> fugSta;
435  // SSM in Stability Analysis
436  vector<OCP_DBL> Y;
437  OCP_DBL Yt;
438  vector<OCP_DBL> di;
439  // NR in Stability Analysis
440  vector<OCP_DBL> resSTA;
441  vector<OCP_DBL> JmatSTA;
442  vector<vector<OCP_DBL>> fugX;
443  vector<OCP_DBL> Ax;
444  vector<OCP_DBL> Bx;
445  vector<OCP_DBL> Zx;
446 
447  // SSM in Phase Split
448  vector<OCP_DBL> resRR;
449  // NR in Phase Split
450  vector<OCP_DBL> lresSP;
451  vector<OCP_DBL> resSP;
452  vector<OCP_DBL> JmatSP;
453  vector<vector<OCP_DBL>>
454  fugN;
455  vector<OCP_DBL> An;
456  vector<OCP_DBL> Bn;
457  vector<vector<OCP_DBL>> Zn;
458  // for linearsolve with lapack
459  vector<OCP_INT> pivot;
460  vector<OCP_DBL> JmatWork;
461  OCP_INT lJmatWork;
462  char uplo{'U'};
463 
464 public:
465  // After Phase Equilibrium Calculation finishs, properties and some auxiliary
466  // variables will be calculated.
467  void AllocateOthers();
468  void IdentifyPhase();
470  void CopyPhase();
471  void CalViscosity();
472  void CalViscoLBC();
473  void CalViscoHZYT();
474  void CalFugXAll();
475  void CalFugPAll(const OCP_BOOL& Zpflag = OCP_TRUE);
476 
477  void CalVjpVfpVfx_partial();
478  void CalXiPNX_partial();
479  void CalRhoPX_partial();
480  void CalMuPX_partial();
481  void CalMuPXLBC_partial();
482  void CalXiRhoMuPN_pfullx();
483  void CaldXsdXpAPI04();
484  void CaldXsdXp04();
485 
486  void CalRhoPNX_full();
487 
488  void CalXiPNX_full01();
489  void CalRhoPNX_full01();
490  void CalMuPX_full01();
491  void CalMuPXLBC_full01();
492  void CalVfiVfp_full01();
493  void AssembleMatVfiVfp_full01();
494  void AssembleRhsVfiVfp_full01();
495  void CaldXsdXp01();
496  void CaldXsdXpAPI01();
497 
498  void CalXiPNX_full02();
499  void CalVfiVfp_full02();
500  void AssembleMatVfiVfp_full02();
501  void AssembleRhsVfiVfp_full02();
502  void CaldXsdXpAPI02();
503  void CaldXsdXpAPI02p();
504 
505  void CalVjpVfpVfn_partial();
506  void CalXiPn_partial();
507  void CalRhoPn_partial();
508  void CalMuPn_partial();
509  void CalMuPnLBC_partial();
510  void CalXiRhoMuPN_pfullxn(const OCP_BOOL& xflag = OCP_TRUE);
511 
512  void CaldXsdXpAPI03();
513  void CaldXsdXp03();
514  void CalVfiVfp_full03();
515 
516  void CalKeyDerx();
517  void CalKeyDern();
518 
519 private:
520  // Phase properties and auxiliary variables
521  vector<OCP_DBL> muC;
522  vector<vector<OCP_DBL>>
523  muAux;
524  vector<OCP_DBL>
525  muAux1I;
526  vector<OCP_DBL> sqrtMWi;
527  vector<vector<OCP_DBL>> fugP;
528  vector<OCP_DBL> Zp;
529 
530  vector<OCP_DBL> JmatTmp;
531  vector<OCP_DBL> JmatDer;
534  vector<OCP_DBL> rhsDer;
535 
536  vector<OCP_DBL> vjp;
537  vector<vector<OCP_DBL>>
538  vji;
539  vector<OCP_DBL> xixC;
540  vector<OCP_DBL> xiPC;
541  vector<OCP_DBL> xiNC;
542  vector<OCP_DBL> muN;
543  vector<OCP_DBL> xiN;
544  vector<OCP_DBL> rhoN;
545 
547  // Optional Features
549 
550 public:
551  void SetupOptionalFeatures(OptionalFeatures& optFeatures,
552  const OCP_USI& numBulk) override;
553 
555  // Accelerate PVT
557 
558 protected:
560  void AllocateSkip();
562  void CalPhiNSkip();
564  void AssembleSkipMatSTA();
566  void CalSkipForNextStep();
568  void CalFtypeIMPEC() { ftype = skipSta->CalFtypeIMPEC(P, T, Nh, Ni, bulkId); }
570  void CalFtypeFIM(const OCP_DBL* Sjin)
571  {
572  ftype = skipSta->CalFtypeFIM(P, T, Nh, Ni, Sjin, lNP, bulkId);
573  }
574 
575 protected:
577 
578  USI ftype{0};
580  // if ftype = 0 also, then new range should be calculated
582  vector<OCP_DBL> phiN;
583  vector<OCP_SIN> skipMatSTA;
585  // Only the minimum eigen value will be used
586  vector<OCP_SIN> eigenSkip;
587  vector<OCP_SIN> eigenWork;
588 
590  // Miscible
592 
593 protected:
594  void InputMiscibleParam(const ComponentParam& param, const USI& tarId);
595  void CalSurfaceTension();
596 
597 protected:
599 
602 
604 
605  vector<OCP_DBL> parachor;
606 };
607 
609 OCP_DBL signD(const OCP_DBL& d);
610 
611 OCP_DBL delta(const USI& i, const USI& j);
612 
613 void NTcubicroot(OCP_DBL& root, const OCP_DBL& a, const OCP_DBL& b, const OCP_DBL& c);
614 
615 #endif //__MIXTURECOMP_HEADER__
Operations about small dense mat.
double Dnorm1(const int &N, double *x)
Computes the L1-norm of a vector.
Definition: DenseMat.cpp:49
void Dcopy(const int &N, double *dst, const double *src)
Calculate the minimal eigenvalue for symmetric matrix with mkl lapack.
Definition: DenseMat.cpp:36
OCP_DBL signD(const OCP_DBL &d)
Return the sign of double di.
Mixture class declaration.
unsigned int USI
Generic unsigned integer.
Definition: OCPConst.hpp:23
double OCP_DBL
Double precision.
Definition: OCPConst.hpp:27
const OCP_DBL RHOW_STD
Water density at surface cond: lb/ft3.
Definition: OCPConst.hpp:57
unsigned int OCP_USI
Long unsigned integer.
Definition: OCPConst.hpp:25
int OCP_INT
Long integer.
Definition: OCPConst.hpp:26
unsigned int OCP_BOOL
OCP_BOOL in OCP.
Definition: OCPConst.hpp:29
unsigned long long OCP_ULL
Long long unsigned integer.
Definition: OCPConst.hpp:24
OCPTable class declaration.
#define OCP_ABORT(msg)
Abort if critical error happens.
Definition: UtilError.hpp:47
OCP_DBL MW
Molecular Weight.
OCP_DBL OmegaA
Param A of Components.
OCP_DBL Vc
VcMW * MW.
OCP_DBL VcMW
Critical Volume / MW.
OCP_DBL acf
Acentric Factor.
OCP_DBL Tc
Critical Temperature.
OCP_DBL OmegaB
Param B of Components.
OCP_DBL Vshift
shift volume
OCP_DBL Pc
Critical Pressure.
string name
Name of components.
ComponentParam contains information of components.
void CalFtypeIMPEC()
Calculate Flash type for IMPEC.
void CalFtypeFIM(const OCP_DBL *Sjin)
Calculate Flash type for FIM.
vector< OCP_DBL > parachor
Parachor params of hydrocarbon components.
vector< OCP_SIN > skipMatSTA
OCP_BOOL flagSkip
If ture, then skipping could be try,.
vector< OCP_DBL > phiN
d ln phi[i][j] / d n[k][j]
Miscible * misTerm
Miscible term pointing to OptionalFeature.
SkipStaAnaly * skipSta
Skip analysis Term pointing to OptionalFeature.
OCP_BOOL ifUseMiscible
vector< OCP_SIN > eigenWork
work space for computing eigenvalues with ssyevd_
vector< OCP_SIN > eigenSkip
eigen values of matrix for skipping Skip Stability Analysis.
void PrintZtmp()
test
OCP_DBL surTen
Surface tension between hydrocarbons phases.
Params for NR in Phase Split.
Definition: MixtureComp.hpp:72
USI curIt
current Iters
Definition: MixtureComp.hpp:80
OCP_DBL tol2
tol*tol
Definition: MixtureComp.hpp:76
USI maxIt
Max Iteration.
Definition: MixtureComp.hpp:74
OCP_DBL tol
Tolerance.
Definition: MixtureComp.hpp:75
OCP_BOOL conflag
convergence flag, if converges, conflag = OCP_TRUE
Definition: MixtureComp.hpp:78
OCP_DBL realTol
Real tol.
Definition: MixtureComp.hpp:77
Params for NR in Phase Stability Analysis.
Definition: MixtureComp.hpp:46
OCP_BOOL conflag
convergence flag, if converges, conflag = OCP_TRUE
Definition: MixtureComp.hpp:52
OCP_DBL tol
Tolerance.
Definition: MixtureComp.hpp:49
OCP_DBL realTol
Real tol.
Definition: MixtureComp.hpp:51
USI curIt
current Iters
Definition: MixtureComp.hpp:54
USI maxIt
Max Iteration.
Definition: MixtureComp.hpp:48
OCP_DBL tol2
tol*tol
Definition: MixtureComp.hpp:50
ComponentParam comsParam
information for components
Type_A_r< OCP_DBL > density
Density of oil, water, gas in standard conditions.
Type_A_r< OCP_DBL > gravity
Gravity of oil, water, gas in standard conditions.
TableSet PVTW_T
Table set of PVTW.
Param for Solving Rachford-Rice Equations.
Definition: MixtureComp.hpp:85
OCP_DBL tol2
tol*tol
Definition: MixtureComp.hpp:89
USI maxIt
Max Iteration.
Definition: MixtureComp.hpp:87
USI curIt
current Iters
Definition: MixtureComp.hpp:91
OCP_DBL tol
Tolerance.
Definition: MixtureComp.hpp:88
Params for SSM in Phase Split.
Definition: MixtureComp.hpp:59
USI curIt
current Iters
Definition: MixtureComp.hpp:67
OCP_DBL tol
Tolerance.
Definition: MixtureComp.hpp:62
OCP_BOOL conflag
convergence flag, if converges, conflag = OCP_TRUE
Definition: MixtureComp.hpp:65
OCP_DBL tol2
tol*tol
Definition: MixtureComp.hpp:63
OCP_DBL realTol
Real tol.
Definition: MixtureComp.hpp:64
USI maxIt
Max Iteration.
Definition: MixtureComp.hpp:61
Params for SSM in Phase Stability Analysis.
Definition: MixtureComp.hpp:29
OCP_DBL realTol
Real tol.
Definition: MixtureComp.hpp:37
USI curIt
current Iterations
Definition: MixtureComp.hpp:40
OCP_DBL tol2
tol*tol
Definition: MixtureComp.hpp:36
OCP_DBL tol
Tolerance.
Definition: MixtureComp.hpp:32
OCP_BOOL conflag
convergence flag, if converges, conflag = OCP_TRUE
Definition: MixtureComp.hpp:38
USI maxIt
Max Iteration.
Definition: MixtureComp.hpp:31
vector< vector< vector< OCP_DBL > > > data
All table with the same name.
OCP_BOOL activity
If OCP_FALSE, this param is not given.
vector< T > data
Data of param.