14 COMP::COMP(
const vector<string>& comp)
28 MixtureComp::MixtureComp(
const EoSparam& param,
const USI& tar)
63 for (
USI i = 0; i < NC; i++) {
64 Vc[i] = 10.73159 * Zc[i] * Tc[i] / Pc[i];
67 OCP_ABORT(
"VCRIT or ZCRIT hasn't been input!");
80 OmegaA.resize(NC, 0.457235529);
84 OmegaB.resize(NC, 0.077796074);
88 for (
USI i = 0; i < NC; i++)
105 for (
USI i = 0; i < NC; i++) {
112 for (
auto& lbc : LBCcoef) {
117 if (miscible && !ParachorAct) {
118 OCP_ABORT(
"PARACHOR has not been Input!");
128 if (param.
BIC[tar].size() != len) {
130 for (
USI i = 1; i < NC; i++) {
131 for (
USI j = 0; j < i; j++) {
132 BIC[i * NC + j] = param.
BIC[tar][iter];
133 BIC[j * NC + i] = BIC[i * NC + j];
138 BIC = param.
BIC[tar];
141 for (
USI i = 0; i < NC; i++) {
142 for (
USI j = 0; j < NC; j++) {
143 cout << setw(10) << BIC[i * NC + j] <<
" ";
151 EoSctrl.SSMsta.
tol2 = EoSctrl.SSMsta.
tol * EoSctrl.SSMsta.
tol;
155 EoSctrl.NRsta.
tol2 = EoSctrl.NRsta.
tol * EoSctrl.NRsta.
tol;
159 EoSctrl.SSMsp.
tol2 = EoSctrl.SSMsp.
tol * EoSctrl.SSMsp.
tol;
163 EoSctrl.NRsp.
tol2 = EoSctrl.NRsp.
tol * EoSctrl.NRsp.
tol;
167 EoSctrl.RR.
tol2 = EoSctrl.RR.
tol * EoSctrl.RR.
tol;
199 Nh = Vpore * (1 - Sw) /
vf;
209 for (
USI i = 0; i < NC; i++) {
222 OCP_DBL bw = bw0 * (1 - cbw * (P - Pw0));
226 rho[Wpid] = std_RhoW / bw;
227 Ni[Wcid] = Vpore * Sw *
xi[Wpid];
240 void MixtureComp::InitFlashDer(
const OCP_DBL& Pin,
const OCP_DBL& Pbbin,
264 Nh = Vpore * (1 - Sw) /
vf;
274 for (
USI i = 0; i < NC; i++) {
287 OCP_DBL bw = bw0 * (1 - cbw * (P - Pw0));
291 rho[Wpid] = std_RhoW / bw;
292 muP[Wpid] = cdata[3];
293 xiP[Wpid] = -bwp / (bw * bw *
CONV1);
295 Ni[Wcid] = Vpore * Sw *
xi[Wpid];
328 void MixtureComp::InitFlashDer_n(
const OCP_DBL& Pin,
const OCP_DBL& Pbbin,
352 Nh = Vpore * (1 - Sw) /
vf;
360 for (
USI i = 0; i < NC; i++) {
364 CalVjpVfpVfn_partial();
374 OCP_DBL bw = bw0 * (1 - cbw * (P - Pw0));
378 rho[Wpid] = std_RhoW / bw;
379 muP[Wpid] = cdata[3];
380 xiP[Wpid] = -bwp / (bw * bw *
CONV1);
382 Ni[Wcid] = Vpore * Sw *
xi[Wpid];
409 const USI& Myftype,
const USI& lastNP,
415 Dcopy(NC, &lKs[0], lastKs);
418 CalFlash(Pin, Tin, Niin);
435 OCP_DBL bw = bw0 * (1 - cbw * (P - Pw0));
439 rho[Wpid] = std_RhoW / bw;
456 Dcopy(NC, &lKs[0], lastKs);
459 CalFlash(Pin, Tin, Niin);
476 OCP_DBL bw = bw0 * (1 - cbw * (P - Pw0));
480 rho[Wpid] = std_RhoW / bw;
481 muP[Wpid] = cdata[3];
482 xiP[Wpid] = -bwp / (bw * bw *
CONV1);
516 const OCP_DBL* njin,
const USI& myftype,
const USI* phaseExistin,
521 if (phaseExistin[j] == 1)
527 if (inputNP == 1 ||
true) {
531 Dcopy(NC, &lKs[0], lastKs);
533 CalFlash(Pin, Tin, Niin);
542 for (
USI j = 0; j < NP; j++) {
552 phaseLabel[0] =
OIL; phaseLabel[1] =
GAS;
561 CalVjpVfpVfn_partial();
574 OCP_DBL bw = bw0 * (1 - cbw * (P - Pw0));
578 rho[Wpid] = std_RhoW / bw;
579 muP[Wpid] = cdata[3];
580 xiP[Wpid] = -bwp / (bw * bw *
CONV1);
593 if (inputNP == NP && NP == 2 &&
false) {
594 cout << scientific << setprecision(3);
595 cout <<
"--------- " << NP <<
" --------- " << inputNP << endl;
596 for (
USI j = 0; j < NP; j++) {
597 USI j1 = phaseLabel[j];
598 cout << setw(10) <<
S[j1] - Sjin[j1] <<
" "
599 << setw(10) << (
nj[j1] - njin[j1]) / njin[j1] <<
" ";
600 for (
USI i = 0; i < NC; i++) {
601 cout << setw(10) <<
xij[j1 *
numCom + i] - xijin[j1 *
numCom + i] <<
" ";
605 cout <<
S[2] - Sjin[2] << endl;
648 if (Ziin[
numCom - 1] > 1 - 1e-6) {
654 OCP_DBL bw = bw0 * (1 - cbw * (P - Pw0));
663 CalAjBj(Aj[0], Bj[0], zi);
664 SolEoS(Zj[0], Aj[0], Bj[0]);
666 for (
USI i = 0; i < NC; i++) {
667 vtmp -= zi[i] * Vshift[i];
680 if (Ziin[
numCom - 1] > 1 - 1e-6) {
682 rhotmp = std_RhoW * (
CONV1 * xitmp);
688 rhotmp = MW[0] * xitmp;
699 OCP_DBL bw = (bw0 * (1 - cbw * (Pin - Pw0)));
701 return std_GammaW / bw;
712 void MixtureComp::CallId()
715 for (
USI i = 1; i < NC; i++) {
716 if (MWC[i] < MWC[lId]) lId = i;
720 void MixtureComp::CalSurfaceTension()
725 if (NP == 1) surTen = 100;
728 const OCP_DBL b0 = xiC[0] * unitF;
729 const OCP_DBL b1 = xiC[1] * unitF;
731 for (
USI i = 0; i < NC; i++)
732 surTen += Parachor[i] * (b0 * x[0][i] - b1 * x[1][i]);
733 surTen = pow(surTen, 4.0);
739 void MixtureComp::AllocateEoS()
748 delta1P2 = delta1 + delta2;
749 delta1M2 = delta1 - delta2;
750 delta1T2 = delta1 * delta2;
758 const OCP_DBL a = (delta1 + delta2 - 1) * bj - 1;
760 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1));
761 const OCP_DBL c = -(aj * bj + delta1 * delta2 * bj * bj * (bj + 1));
769 OCP_DBL dG = (zj2 - zj1) + log((zj1 - bj) / (zj2 - bj)) -
770 aj / (bj * (delta2 - delta1)) *
771 log((zj1 + delta1 * bj) * (zj2 + delta2 * bj) /
772 ((zj1 + delta2 * bj) * (zj2 + delta1 * bj)));
780 void MixtureComp::CalAiBi()
786 for (
USI i = 0; i < NC; i++) {
790 mwi = 0.37464 + 1.54226 * acf - 0.26992 * pow(acf, 2);
792 mwi = 0.379642 + 1.48503 * acf - 0.164423 * pow(acf, 2) +
793 0.016667 * pow(acf, 3);
798 Ai[i] = OmegaA[i] * Pri / pow(Tri, 2) * pow((1 + mwi * (1 - sqrt(Tri))), 2);
799 Bi[i] = OmegaB[i] * Pri / Tri;
803 void MixtureComp::CalAjBj(
OCP_DBL& AjT,
OCP_DBL& BjT,
const vector<OCP_DBL>& xj)
const
808 for (
USI i1 = 0; i1 < NC; i1++) {
809 BjT += Bi[i1] * xj[i1];
810 AjT += xj[i1] * xj[i1] * Ai[i1] * (1 - BIC[i1 * NC + i1]);
812 for (
USI i2 = 0; i2 < i1; i2++) {
814 2 * xj[i1] * xj[i2] * sqrt(Ai[i1] * Ai[i2]) * (1 - BIC[i1 * NC + i2]);
824 for (
USI i1 = 0; i1 < NC; i1++) {
825 BjT += Bi[i1] * xj[i1];
826 AjT += xj[i1] * xj[i1] * Ai[i1] * (1 - BIC[i1 * NC + i1]);
828 for (
USI i2 = 0; i2 < i1; i2++) {
830 2 * xj[i1] * xj[i2] * sqrt(Ai[i1] * Ai[i2]) * (1 - BIC[i1 * NC + i2]);
835 void MixtureComp::AllocatePhase()
843 phaseLabel.resize(NPmax);
848 for (
USI j = 0; j < NPmax; j++) {
857 void MixtureComp::CalFugPhi(vector<OCP_DBL>& phiT, vector<OCP_DBL>& fugT,
858 const vector<OCP_DBL>& xj)
869 for (
USI i = 0; i < NC; i++) {
871 for (
int k = 0; k < NC; k++) {
872 tmp += 2 * (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]) * xj[k];
874 phiT[i] = exp(Bi[i] / bj * (zj - 1) - log(zj - bj) -
875 aj / (m1 - m2) / bj * (tmp / aj - Bi[i] / bj) *
876 log((zj + m1 * bj) / (zj + m2 * bj)));
877 fugT[i] = phiT[i] * xj[i] * P;
906 const OCP_DBL m1Mm2 = delta1M2;
912 for (
USI i = 0; i < NC; i++) {
914 for (
int k = 0; k < NC; k++) {
915 tmp += 2 * (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]) * xj[k];
917 phiT[i] = exp(Bi[i] / bj * (zj - 1) - log(zj - bj) -
918 aj / (m1Mm2) / bj * (tmp / aj - Bi[i] / bj) *
919 log((zj + m1 * bj) / (zj + m2 * bj)));
920 fugT[i] = phiT[i] * xj[i] * P;
944 const OCP_DBL m1Mm2 = delta1M2;
950 for (
USI i = 0; i < NC; i++) {
952 for (
int k = 0; k < NC; k++) {
953 tmp += 2 * (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]) * xj[k];
955 tmp = exp(Bi[i] / bj * (zj - 1) - log(zj - bj) -
956 aj / (m1Mm2) / bj * (tmp / aj - Bi[i] / bj) *
957 log((zj + m1 * bj) / (zj + m2 * bj)));
958 fugT[i] = tmp * xj[i] * P;
966 void MixtureComp::CalFugPhiAll()
971 const OCP_DBL m1Mm2 = delta1M2;
973 for (
USI j = 0; j < NP; j++) {
974 const vector<OCP_DBL>& xj = x[j];
975 vector<OCP_DBL>& phiT = phi[j];
976 vector<OCP_DBL>& fugT = fug[j];
984 for (
USI i = 0; i < NC; i++) {
986 for (
int k = 0; k < NC; k++) {
987 tmp += 2 * (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]) * xj[k];
989 phiT[i] = exp(Bi[i] / bj * (zj - 1) - log(zj - bj) -
990 aj / (m1Mm2) / bj * (tmp / aj - Bi[i] / bj) *
991 log((zj + m1 * bj) / (zj + m2 * bj)));
992 fugT[i] = phiT[i] * xj[i] * P;
1010 void MixtureComp::CalMW()
1013 for (
USI j = 0; j < NP; j++) {
1015 for (
USI i = 0; i < NC; i++) {
1016 MW[j] += x[j][i] * MWC[i];
1021 void MixtureComp::CalVfXiRho()
1026 for (
USI j = 0; j < NP; j++) {
1028 vector<OCP_DBL>& xj = x[j];
1030 for (
USI i = 0; i < NC; i++) {
1031 tmp -= xj[i] * Vshift[i];
1033 vC[j] = tmp * nu[j];
1036 rhoC[j] = MW[j] * xiC[j];
1040 void MixtureComp::CalSaturation()
1050 USI MixtureComp::FindMWmax()
1055 for (
USI j = 1; j < NP; j++) {
1056 if (tmpMW < MW[j]) {
1067 for (
USI j = 0; j < NP; j++) {
1070 for (
USI i = 0; i < NC; i++) {
1071 n[j][i] = nu[j] * x[j][i];
1076 void MixtureComp::PrintX()
1078 for (
USI j = 0; j < NP; j++) {
1079 for (
USI i = 0; i < NC; i++) {
1080 cout << x[j][i] <<
" ";
1084 cout <<
"----------------------" << endl;
1087 void MixtureComp::AllocateMethod()
1090 for (
USI i = 0; i < 4; i++) {
1093 Ks.resize(NPmax - 1);
1094 for (
USI i = 0; i < NPmax - 1; i++) {
1104 JmatSTA.resize(NC * NC);
1108 phiN.resize(NC * NC);
1109 skipMatSTA.resize(NC * NC);
1110 eigenSkip.resize(NC);
1111 leigenWork = 2 * NC + 1;
1112 eigenWork.resize(leigenWork);
1115 resRR.resize(NPmax - 1);
1116 resSP.resize(NC * NPmax);
1117 JmatSP.resize(NC * NC * NPmax * NPmax);
1121 for (
USI j = 0; j < NPmax; j++) {
1122 fugX[j].resize(NC * NC);
1123 fugN[j].resize(NC * NC);
1128 pivot.resize((NC + 1) * NPmax, 1);
1129 lJmatWork = NC * (NPmax - 1);
1130 JmatWork.resize(lJmatWork);
1132 pivot.resize(NPmax * NC +
numPhase, 1);
1135 void MixtureComp::CalKwilson()
1137 for (
USI i = 0; i < NC; i++) {
1138 Kw[0][i] = (Pc[i] / P) * exp(5.373 * (1 + Acf[i]) * (1 - Tc[i] / T));
1139 Kw[1][i] = 1 / Kw[0][i];
1140 Kw[2][i] = pow(Kw[0][i], 1.0 / 3);
1141 Kw[3][i] = pow(Kw[1][i], 1.0 / 3);
1145 void MixtureComp::PhaseEquilibrium()
1156 CalAjBj(Aj[0], Bj[0], x[0]);
1157 SolEoS(Zj[0], Aj[0], Bj[0]);
1159 while (!PhaseStable()) {
1162 if (NP == NPmax || NP == 1)
break;
1168 else if (EoSctrl.SSMsta.
conflag)
1169 ePEC = EoSctrl.SSMsta.
realTol;
1176 else if (EoSctrl.SSMsp.
conflag)
1188 CalAjBj(Aj[0], Bj[0], x[0]);
1189 SolEoS(Zj[0], Aj[0], Bj[0]);
1204 else if (EoSctrl.SSMsp.
conflag)
1215 if (NP > 1) flagSkip =
false;
1216 if (NP == 1 && ftype == 0 && flagSkip) {
1218 AssembleSkipMatSTA();
1220 if (!
CheckNan(skipMatSTA.size(), &skipMatSTA[0])) {
1225 MinEigenSY(NC, &skipMatSTA[0], &eigenSkip[0], &eigenWork[0], leigenWork);
1231 bool MixtureComp::PhaseStable()
1237 testPId = FindMWmax();
1240 EoSctrl.SSMsta.
conflag =
false;
1241 EoSctrl.SSMsta.
curIt = 0;
1242 EoSctrl.NRsta.
conflag =
false;
1243 EoSctrl.NRsta.
curIt = 0;
1262 SSMSTAiters += EoSctrl.SSMsta.
curIt;
1263 NRSTAiters += EoSctrl.NRsta.
curIt;
1286 OCP_DBL dYtol = EoSctrl.SSMsta.dYtol;
1293 const vector<OCP_DBL>& xj = x[Id];
1294 CalFugPhi(phi[Id], fug[Id], xj);
1295 const vector<OCP_DBL>& fugId = fug[Id];
1296 vector<OCP_DBL>& ks = Ks[0];
1298 for (k = 0; k < 2; k++) {
1306 for (
USI i = 0; i < NC; i++) {
1307 Y[i] = xj[i] * ks[i];
1314 for (
USI i = 0; i < NC; i++) {
1315 dY += pow((Y[i] - di[i]), 2);
1324 CalFugPhi(&fugSta[0], &Y[0]);
1327 for (
USI i = 0; i < NC; i++) {
1328 di[i] = fugId[i] / (fugSta[i] * Yt);
1330 Se += pow(di[i] - 1, 2);
1332 Sk += pow(log(ks[i]), 2);
1359 EoSctrl.SSMsta.
curIt += iter;
1361 if (flag && Yt > 1 - 0.1 && Sk > 1) {
1365 if (flag && Yt > 1 + eYt) {
1366 EoSctrl.SSMsta.
conflag =
true;
1367 EoSctrl.SSMsta.
realTol = sqrt(Se);
1371 EoSctrl.SSMsta.
realTol = sqrt(Se);
1381 const vector<OCP_DBL>& xj = x[Id];
1382 CalFugPhi(phi[Id], fug[Id], xj);
1383 const vector<OCP_DBL>& fugId = fug[Id];
1385 for (
USI i = 0; i < NC; i++) {
1386 di[i] = phi[Id][i] * xj[i];
1399 for (k = 0; k < 4; k++) {
1402 for (
USI i = 0; i < NC; i++) {
1403 Y[i] = xj[i] / Kw[k][i];
1408 CalFugPhi(&phiSta[0], &fugSta[0], &Y[0]);
1411 for (
USI i = 0; i < NC; i++) {
1412 Se += pow(log(fugSta[i] / fugId[i] * Yt), 2);
1430 for (
USI i = 0; i < NC; i++) {
1431 Y[i] = di[i] / phiSta[i];
1436 CalFugPhi(&phiSta[0], &fugSta[0], &Y[0]);
1438 for (
USI i = 0; i < NC; i++) {
1439 Se += pow(log(fugSta[i] / fugId[i] * Yt), 2);
1448 EoSctrl.SSMsta.
curIt += iter;
1449 flag = StableNR(Id);
1454 if (flag && Yt > 1 + eYt) {
1459 EoSctrl.SSMsta.
conflag =
true;
1460 EoSctrl.SSMsta.
realTol = sqrt(Se);
1470 EoSctrl.SSMsta.
realTol = sqrt(Se);
1474 bool MixtureComp::StableNR(
const USI& Id)
1478 cout << endl <<
"Stable NR Begins !" << endl << endl;
1481 for (
USI i = 0; i < NC; i++) {
1482 resSTA[i] = log(fug[Id][i] / (fugSta[i] * Yt));
1501 SYSSolve(1, &uplo, NC, &JmatSTA[0], &resSTA[0], &pivot[0], &JmatWork[0], lJmatWork);
1503 Daxpy(NC, alpha, &resSTA[0], &Y[0]);
1505 for (
USI i = 0; i < NC; i++) {
1510 CalFugPhi(&fugSta[0], &Y[0]);
1511 for (
USI i = 0; i < NC; i++) {
1512 resSTA[i] = log(fug[Id][i] / (fugSta[i] * Yt));
1514 Se =
Dnorm2(NC, &resSTA[0]);
1524 EoSctrl.NRsta.
curIt += iter;
1525 EoSctrl.NRsta.
conflag =
false;
1552 EoSctrl.NRsta.
curIt += iter;
1562 vector<OCP_DBL>& fugx = fugX[0];
1570 const OCP_DBL m1Pm2 = delta1P2;
1571 const OCP_DBL m1Mm2 = delta1M2;
1572 const OCP_DBL m1Tm2 = delta1T2;
1575 for (
USI i = 0; i < NC; i++) {
1577 for (
USI k = 0; k < NC; k++) {
1578 tmp += Y[k] * (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]);
1581 Zx[i] = ((bj - zj) * Ax[i] + ((aj + m1Tm2 * (3 * bj * bj + 2 * bj)) +
1582 ((m1Pm2) * (2 * bj + 1) - 2 * m1Tm2 * bj) * zj -
1583 (m1Pm2 - 1) * zj * zj) *
1585 (3 * zj * zj + 2 * ((m1Pm2 - 1) * bj - 1) * zj +
1586 (aj + m1Tm2 * bj * bj - (m1Pm2)*bj * (bj + 1)));
1592 G = (zj + m1 * bj) / (zj + m2 * bj);
1594 for (
USI i = 0; i < NC; i++) {
1596 C = Y[i] * P / (zj - bj);
1599 E = -aj / ((m1Mm2)*bj) * (Ax[i] / aj - Bx[i] / bj);
1601 for (
USI k = 0; k < NC; k++) {
1602 aik = (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]);
1605 Cxk = ((zj - bj) * delta(i, k) - Y[i] * (Zx[k] - Bx[k])) * P /
1606 ((zj - bj) * (zj - bj));
1607 Dxk = Bx[i] / bj * (Zx[k] - Bx[k] * (zj - 1) / bj);
1611 Exk = (2 * (aj / bj * Bx[k] * Bx[i] + bj * aik) - Ax[i] * Bx[k] -
1615 Gxk = (m1Mm2) / (zj + m2 * bj) / (zj + m2 * bj) * (zj * Bx[k] - Zx[k] * bj);
1616 fugx[i * NC + k] = 1 / C * Cxk + Dxk + Exk * log(G) + E / G * Gxk;
1628 for (
USI j = 0; j < NP; j++) {
1629 if (!
CheckNan(fugX[j].size(), &fugX[j][0]))
1637 void MixtureComp::AssembleJmatSTA()
1639 vector<OCP_DBL>& fugx = fugX[0];
1640 fill(JmatSTA.begin(), JmatSTA.end(), 0.0);
1642 for (
USI i = 0; i < NC; i++) {
1645 for (
USI k = 0; k < NC; k++) {
1646 tmp += Y[k] * fugx[i * NC + k];
1649 for (
USI j = 0; j < NC; j++) {
1652 JmatSTA[i * NC + j] = (fugx[i * NC + j] - tmp + 1) / Yt;
1664 void MixtureComp::PhaseSplit()
1666 EoSctrl.SSMsp.
conflag =
false;
1667 EoSctrl.SSMsp.
curIt = 0;
1669 EoSctrl.NRsp.
curIt = 0;
1670 EoSctrl.RR.
curIt = 0;
1674 while (!EoSctrl.NRsp.
conflag) {
1677 if (!CheckSplit())
break;
1678 if (EoSctrl.SSMsp.
conflag)
break;
1682 SSMSPiters += EoSctrl.SSMsp.
curIt;
1683 NRSPiters += EoSctrl.NRsp.
curIt;
1684 RRiters += EoSctrl.RR.
curIt;
1703 bool MixtureComp::CheckSplit()
1708 OCP_DBL nuMax = max(nu[0], nu[1]);
1710 for (
USI i = 0; i < NC; i++) {
1711 tmp += (x[0][i] - x[1][i]) * (x[0][i] - x[1][i]);
1714 if (nuMax < 1 && EoSctrl.NRsp.
conflag && isfinite(tmp)) {
1717 if (!isfinite(tmp) || (1 - nuMax) < 1E-3) {
1733 CalAjBj(Aj[0], Bj[0], x[0]);
1734 SolEoS(Zj[0], Aj[0], Bj[0]);
1736 EoSctrl.SSMsta.
conflag =
false;
1737 EoSctrl.NRsta.
conflag =
false;
1745 void MixtureComp::SplitSSM(
const bool& flag)
1748 cout <<
"SSMSP Begins!" << endl;
1758 void MixtureComp::SplitSSM2(
const bool& flag)
1774 if (Yt < 1.1 ||
true) {
1778 for (
USI i = 0; i < NC; i++) {
1780 Ks[NP - 2][i] = Y[i] / x[testPId][i];
1805 for (
USI i = 0; i < NC; i++) {
1806 Se += pow(fug[1][i] / fug[0][i] - 1, 2);
1807 Ks[0][i] = phi[1][i] / phi[0][i];
1816 EoSctrl.SSMsp.
conflag =
false;
1821 EoSctrl.SSMsp.
realTol = sqrt(Se);
1822 EoSctrl.SSMsp.
curIt += iter;
1825 void MixtureComp::SplitSSM3(
const bool& flag) {}
1829 const vector<OCP_DBL>& Ktmp = Ks[0];
1833 for (
USI i = 1; i < NC; i++) {
1834 if (Ktmp[i] < Kmin) Kmin = Ktmp[i];
1835 if (Ktmp[i] > Kmax) Kmax = Ktmp[i];
1838 const OCP_DBL numin = 1 / (1 - Kmax);
1839 const OCP_DBL numax = 1 / (1 - Kmin);
1841 nu[0] = 0.5 * (numin + numax);
1844 OCP_DBL tmp, rj, J, dnuj, tmpnu;
1854 for (
USI i = 0; i < NC; i++) {
1855 tmp = 1 + nu[0] * (Ktmp[i] - 1);
1856 rj += zi[i] * (Ktmp[i] - 1) / tmp;
1857 J -= zi[i] * (Ktmp[i] - 1) * (Ktmp[i] - 1) / (tmp * tmp);
1860 if (fabs(rj) < tol || iter > maxIt)
break;
1863 tmpnu = nu[0] + dnuj;
1864 if (tmpnu < numax && tmpnu > numin) {
1868 nu[0] = (nu[0] + numax) / 2;
1870 nu[0] = (nu[0] + numin) / 2;
1876 EoSctrl.RR.
curIt += iter;
1888 cout << nu[0] << endl;
1898 const vector<OCP_DBL>& Ktmp = Ks[0];
1902 for (
USI i = 1; i < NC; i++) {
1903 if (Ktmp[i] < Kmin) Kmin = Ktmp[i];
1904 if (Ktmp[i] > Kmax) Kmax = Ktmp[i];
1907 const OCP_DBL numin = 1 / (1 - Kmax);
1908 const OCP_DBL numax = 1 / (1 - Kmin);
1910 nu[0] = 0.5 * (numin + numax);
1913 OCP_DBL tmp, rj, J, dnuj, tmpnu;
1923 for (
USI i = 0; i < NC; i++) {
1924 tmp = 1 + nu[0] * (Ktmp[i] - 1);
1925 rj += zi[i] * (Ktmp[i] - 1) / tmp;
1926 J -= zi[i] * (Ktmp[i] - 1) * (Ktmp[i] - 1) / (tmp * tmp);
1928 f = (nu[0] - numin) * (numax - nu[0]);
1929 df = -2 * nu[0] + (numax + numin);
1934 if (fabs(rj) < tol || iter > maxIt)
break;
1937 tmpnu = nu[0] + dnuj;
1938 if (tmpnu < numax && tmpnu > numin) {
1943 nu[0] = (nu[0] + numax) / 2;
1946 nu[0] = (nu[0] + numin) / 2;
1952 EoSctrl.RR.
curIt += iter;
1954 cout << iter <<
" " << scientific << setprecision(3) << fabs(rj)
1955 <<
" " << nu[0] <<
" " << numin <<
" " << numax << endl;
1964 cout << nu[0] << endl;
1976 for (
USI i = 0; i < NC; i++) {
1978 for (
USI j = 0; j < NP - 1; j++) {
1979 tmp += nu[j] * (Ks[j][i] - 1);
1981 x[NP - 1][i] = zi[i] / tmp;
1982 for (
USI j = 0; j < NP - 1; j++) {
1983 x[j][i] = Ks[j][i] * x[NP - 1][i];
2006 for (
USI j = 0; j < NP; j++) {
2007 nu[j] = fabs(nu[j]);
2010 USI len = NC * (NP - 1);
2019 cout <<
"NRSP Begins!\n";
2025 while (eNR > NRtol) {
2036 SYSSolve(1, &uplo, len, &JmatSP[0], &resSP[0], &pivot[0], &JmatWork[0], lJmatWork);
2039 alpha = CalStepNRsp();
2042 for (
USI j = 0; j < NP - 1; j++) {
2043 Daxpy(NC, alpha, &resSP[j * NC], &n[j][0]);
2044 Daxpy(NC, -1, &n[j][0], &n[NP - 1][0]);
2046 nu[j] =
Dnorm1(NC, &n[j][0]);
2047 for (
USI i = 0; i < NC; i++) {
2048 x[j][i] = n[j][i] / nu[j];
2054 for (
USI i = 0; i < NC; i++) {
2055 n[NP - 1][i] = fabs(n[NP - 1][i]);
2057 nu[NP - 1] =
Dnorm1(NC, &n[NP - 1][0]);
2058 for (
USI i = 0; i < NC; i++) {
2059 x[NP - 1][i] = n[NP - 1][i] / nu[NP - 1];
2064 cout <<
"---------------------" << endl;
2068 eNR =
Dnorm2(len, &resSP[0]);
2070 if (eNR > eNR0 || iter > EoSctrl.NRsp.
maxIt) {
2076 for (
USI j = 0; j < NP; j++) {
2077 Daxpy(NC, -1, &n[j][0], &ln[j][0]);
2078 en +=
Dnorm2(NC, &ln[j][0]);
2080 if (en / (NP * NC) < 1E-8) {
2086 if (eNR < NRtol) EoSctrl.NRsp.
conflag =
true;
2087 EoSctrl.NRsp.
curIt += iter;
2092 void MixtureComp::CalResSP()
2095 for (
USI j = 0; j < NP - 1; j++) {
2096 for (
USI i = 0; i < NC; i++) {
2098 resSP[j * NC + i] = log(fug[NP - 1][i] / fug[j][i]);
2103 void MixtureComp::CalFugNAll(
const bool& Znflag)
2109 for (
USI j = 0; j < NP; j++) {
2111 vector<OCP_DBL>& fugn = fugN[j];
2115 const vector<OCP_DBL>& xj = x[j];
2116 vector<OCP_DBL>& Znj = Zn[j];
2118 for (
USI i = 0; i < NC; i++) {
2120 for (
USI m = 0; m < NC; m++) {
2121 tmp += (1 - BIC[i * NC + m]) * sqrt(Ai[i] * Ai[m]) * xj[m];
2123 An[i] = 2 / nu[j] * (tmp - aj);
2124 Bn[i] = 1 / nu[j] * (Bi[i] - bj);
2127 ((bj - zj) * An[i] +
2128 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
2129 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
2130 (delta1 + delta2 - 1) * zj * zj) *
2132 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
2133 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
2137 G = (zj + delta1 * bj) / (zj + delta2 * bj);
2139 for (
USI i = 0; i < NC; i++) {
2141 C = xj[i] * P / (zj - bj);
2144 for (
USI k = 0; k < NC; k++) {
2145 tmp += (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]) * xj[k];
2147 E = -aj / ((delta1 - delta2) * bj) * (2 * tmp / aj - Bi[i] / bj);
2149 for (
USI k = 0; k <= i; k++) {
2152 aik = (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]);
2154 Cnk = P / (zj - bj) / (zj - bj) *
2155 ((zj - bj) / nu[j] * (delta(i, k) - xj[i]) -
2156 xj[i] * (Znj[k] - Bn[k]));
2157 Dnk = Bi[i] / bj * (Znj[k] - (Bi[k] - bj) * (zj - 1) / (nu[j] * bj));
2158 Gnk = (delta1 - delta2) / ((zj + delta2 * bj) * (zj + delta2 * bj)) *
2159 (Bn[k] * zj - Znj[k] * bj);
2164 Enk = -1 / (delta1 - delta2) / (bj * bj) *
2165 (2 * (bj * aik / nu[j] + Bn[k] * (Bi[i] * aj / bj - tmp)) -
2166 An[k] * Bi[i] - aj * Bi[i] / nu[j]);
2168 fugn[i * NC + k] = 1 / C * Cnk + Dnk + Enk * log(G) + E / G * Gnk;
2169 fugn[k * NC + i] = fugn[i * NC + k];
2177 for (
USI j = 0; j < NP; j++) {
2178 if (!
CheckNan(fugN[j].size(), &fugN[j][0]))
2186 void MixtureComp::PrintFugN()
2188 for (
USI j = 0; j < NP; j++) {
2189 for (
USI i = 0; i < NC; i++) {
2190 for (
USI k = 0; k < NC; k++) {
2191 cout << fugN[j][i * NC + k] <<
" ";
2195 cout <<
"---------------" << endl;
2199 void MixtureComp::AssembleJmatSP()
2203 fill(JmatSP.begin(), JmatSP.end(), 0);
2209 for (
USI j = 0; j < NP - 1; j++) {
2210 fugNp = &fugN[NP - 1][0];
2211 fugNj = &fugN[j][0];
2213 for (
USI i = 0; i < NC; i++) {
2215 for (
USI k = 0; k < NC; k++) {
2217 Jtmp[k] = fugNj[k] + fugNp[k];
2219 Jtmp += NC * (NP - 1);
2245 const vector<OCP_DBL>& xj = x[0];
2246 vector<OCP_DBL>& Znj = Zn[0];
2248 for (
USI i = 0; i < NC; i++) {
2250 for (
USI m = 0; m < NC; m++) {
2251 tmp += (1 - BIC[i * NC + m]) * sqrt(Ai[i] * Ai[m]) * xj[m];
2253 An[i] = 2 / nu[0] * (tmp - aj);
2254 Bn[i] = 1 / nu[0] * (Bi[i] - bj);
2256 ((bj - zj) * An[i] +
2257 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
2258 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
2259 (delta1 + delta2 - 1) * zj * zj) *
2261 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
2262 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
2265 G = (zj + delta1 * bj) / (zj + delta2 * bj);
2267 for (
USI i = 0; i < NC; i++) {
2272 for (
USI k = 0; k < NC; k++) {
2273 tmp += (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]) * xj[k];
2275 E = -aj / ((delta1 - delta2) * bj) * (2 * tmp / aj - Bi[i] / bj);
2277 for (
USI k = 0; k <= i; k++) {
2280 aik = (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]);
2282 Cnk = (Bn[k] - Znj[k]) / ((zj - bj) * (zj - bj));
2283 Dnk = Bi[i] / bj * (Znj[k] - (Bi[k] - bj) * (zj - 1) / (nu[0] * bj));
2284 Gnk = (delta1 - delta2) / ((zj + delta2 * bj) * (zj + delta2 * bj)) *
2285 (Bn[k] * zj - Znj[k] * bj);
2290 Enk = -1 / (delta1 - delta2) / (bj * bj) *
2291 (2 * (bj * aik / nu[0] + Bn[k] * (Bi[i] * aj / bj - tmp)) -
2292 An[k] * Bi[i] - aj * Bi[i] / nu[0]);
2294 phiN[i * NC + k] = 1 / C * Cnk + Dnk + Enk * log(G) + E / G * Gnk;
2295 phiN[k * NC + i] = phiN[i * NC + k];
2308 void MixtureComp::AssembleSkipMatSTA()
2312 vector<OCP_DBL>& xj = x[0];
2314 for (
USI i = 0; i < NC; i++) {
2315 for (
USI j = 0; j <= i; j++) {
2316 skipMatSTA[i * NC + j] = delta(i, j) + sqrt(xj[i] * xj[j]) * phiN[i * NC + j];
2317 skipMatSTA[j * NC + i] = skipMatSTA[i * NC + j];
2330 OCP_DBL MixtureComp::CalStepNRsp()
2335 for (
USI j = 0; j < NP - 1; j++) {
2338 const OCP_DBL* r = &resSP[j * NC];
2340 for (
USI i = 0; i < NC; i++) {
2341 tmp =
nj[i] + alpha * r[i];
2343 alpha *= 0.9 * fabs(
nj[i] / r[i]);
2351 void MixtureComp::AllocateOthers()
2354 for (
USI i = 0; i < NC; i++) sqrtMWi[i] = sqrt(MWC[i]);
2356 muAux.resize(NPmax);
2357 for (
USI i = 0; i < NPmax; i++) {
2361 for (
USI i = 0; i < NC; i++) {
2362 muAux1I[i] = 5.4402 * pow(Tc[i], 1.0 / 6) / pow(Pc[i], 2.0 / 3);
2365 for (
USI j = 0; j < NPmax; j++) {
2369 JmatDer.resize(NPmax * NPmax * (NC + 1) * (NC + 1));
2371 rhsDer.resize(NPmax * (NC + 1) * (NC + 1));
2372 xixC.resize(NPmax * NC);
2374 xiNC.resize(NPmax * NC);
2382 void MixtureComp::IdentifyPhase()
2390 for (
USI i = 0; i < NC; i++) {
2391 A += x[0][i] * Vc[i] * Tc[i];
2392 B += x[0][i] * Vc[i];
2396 phaseLabel[0] =
GAS;
2399 phaseLabel[0] =
OIL;
2404 if (MW[0] > MW[1]) {
2405 phaseLabel[0] =
OIL;
2406 phaseLabel[1] =
GAS;
2408 phaseLabel[0] =
GAS;
2409 phaseLabel[1] =
OIL;
2419 for (
USI j = 0; j < NP; j++) {
2420 const USI j1 = phaseLabel[j];
2430 void MixtureComp::CalViscosity() { CalViscoLBC(); }
2432 void MixtureComp::CalViscoLBC()
2458 for (
USI j = 0; j < NP; j++) {
2459 const vector<OCP_DBL>& xj = x[j];
2460 vector<OCP_DBL>& muA = muAux[j];
2461 fill(muA.begin(), muA.end(), 0.0);
2466 for (
USI i = 0; i < NC; i++) {
2467 tmp = 5.4402 * pow(Tc[i], 1.0 / 6) / sqrt(MW[j]) / pow(Pc[i], 2.0 / 3);
2471 tmp = 34 * 1E-5 * pow(Tri, 0.94) / tmp;
2473 tmp = 17.78 * 1E-5 * pow((4.58 * Tri - 1.67), 0.625) / tmp;
2475 muA[0] += xj[i] * sqrt(MWC[i]) * tmp;
2476 muA[1] += xj[i] * sqrt(MWC[i]);
2477 xijT += xj[i] * Tc[i];
2478 xijP += xj[i] * Pc[i];
2479 xijV += xj[i] * Vcvis[i];
2481 muA[2] = 5.4402 * pow(xijT, 1.0 / 6) / sqrt(MW[j]) / pow(xijP, 2.0 / 3);
2482 muA[3] = xiC[j] * xijV;
2484 if (muA[3] <= 0.18 &&
false) {
2485 muC[j] = muA[0] / muA[1] + 2.05 * 1E-4 * muA[3] / muA[2];
2487 muA[4] = muA[3] * (muA[3] * (muA[3] * (LBCcoef[4] * muA[3] + LBCcoef[3]) +
2491 muC[j] = muA[0] / muA[1] + 1E-4 * (pow(muA[4], 4) - 1) / muA[2];
2496 void MixtureComp::CalViscoHZYT() {}
2498 void MixtureComp::CalFugXAll()
2500 for (
USI j = 0; j < NP; j++) {
2502 vector<OCP_DBL>& fugx = fugX[j];
2503 vector<OCP_DBL>& xj = x[j];
2510 for (
USI i = 0; i < NC; i++) {
2512 for (
USI k = 0; k < NC; k++) {
2513 tmp += xj[k] * (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]);
2517 ((bj - zj) * Ax[i] +
2518 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
2519 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
2520 (delta1 + delta2 - 1) * zj * zj) *
2522 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
2523 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
2529 G = (zj + delta1 * bj) / (zj + delta2 * bj);
2531 for (
USI i = 0; i < NC; i++) {
2533 C = xj[i] * P / (zj - bj);
2536 E = -aj / ((delta1 - delta2) * bj) * (Ax[i] / aj - Bx[i] / bj);
2538 for (
USI k = 0; k < NC; k++) {
2539 aik = (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]);
2542 Cxk = ((zj - bj) * delta(i, k) - xj[i] * (Zx[k] - Bx[k])) * P /
2543 ((zj - bj) * (zj - bj));
2544 Dxk = Bx[i] / bj * (Zx[k] - Bx[k] * (zj - 1) / bj);
2548 Exk = (2 * (aj / bj * Bx[k] * Bx[i] + bj * aik) - Ax[i] * Bx[k] -
2551 Exk /= -(delta1 - delta2);
2552 Gxk = (delta1 - delta2) / (zj + delta2 * bj) / (zj + delta2 * bj) *
2553 (zj * Bx[k] - Zx[k] * bj);
2554 fugx[i * NC + k] = 1 / C * Cxk + Dxk + Exk * log(G) + E / G * Gxk;
2559 for (
USI j = 0; j < NP; j++) {
2560 if (!
CheckNan(fugX[j].size(), &fugX[j][0]))
2569 void MixtureComp::CalFugPAll(
const bool& Zpflag)
2576 for (
USI j = 0; j < NP; j++) {
2578 vector<OCP_DBL>& fugp = fugP[j];
2579 vector<OCP_DBL>& xj = x[j];
2587 Zp[j] = ((bj - zj) * Ap +
2588 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
2589 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
2590 (delta1 + delta2 - 1) * zj * zj) *
2592 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
2593 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
2596 G = (zj + delta1 * bj) / (zj + delta2 * bj);
2597 Gp = (delta1 - delta2) / ((zj + delta2 * bj) * (zj + delta2 * bj)) *
2598 (Bp * zj - Zp[j] * bj);
2599 for (
USI i = 0; i < NC; i++) {
2605 for (
USI m = 0; m < NC; m++) {
2606 tmp += (1 - BIC[i * NC + m]) * sqrt(Ai[i] * Ai[m]) * xj[m];
2609 E = -aj / ((delta1 - delta2) * bj) * (2 * tmp / aj - Bi[i] / bj);
2611 Cp = ((zj - bj) - P * (Zp[j] - Bp)) / ((zj - bj) * (zj - bj));
2612 Dp = Bi[i] / bj * Zp[j];
2617 fugp[i] = Cp / C + Dp + E / G * Gp;
2622 for (
USI j = 0; j < NP; j++) {
2623 if (!
CheckNan(fugP[j].size(), &fugP[j][0]))
2633 void MixtureComp::CalVjpVfpVfn_partial()
2639 fill(
vfi.begin(),
vfi.end(), 0.0);
2642 for (
USI j = 0; j < NP; j++) {
2646 const vector<OCP_DBL>& xj = x[j];
2647 vector<OCP_DBL>& Znj = Zn[j];
2649 const USI j1 = phaseLabel[j];
2650 for (
USI i = 0; i < NC; i++) {
2652 for (
USI m = 0; m < NC; m++) {
2653 tmp += (1 - BIC[i * NC + m]) * sqrt(Ai[i] * Ai[m]) * xj[m];
2655 An[i] = 2 / nu[j] * (tmp - aj);
2656 Bn[i] = 1 / nu[j] * (Bi[i] - bj);
2658 ((bj - zj) * An[i] +
2659 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
2660 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
2661 (delta1 + delta2 - 1) * zj * zj) *
2663 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
2664 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
2665 vji[j1][i] = CgTP * (zj + nu[j] * Znj[i]);
2667 Zp[j] = ((bj - zj) * aj +
2668 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
2669 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
2670 (delta1 + delta2 - 1) * zj * zj) *
2673 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
2674 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
2675 vjp[j1] = CgTP * nu[j] * (Zp[j] - zj / P);
2681 void MixtureComp::CalXiPn_partial()
2690 for (
USI j = 0; j < NP; j++) {
2691 const vector<OCP_DBL>& xj = x[j];
2695 const USI j1 = phaseLabel[j];
2697 tmp = -
xi[j1] *
xi[j1] * CgTP;
2698 for (
USI i = 0; i < NC; i++) {
2701 xiP[j1] = tmp * (Zp[j] - Zj[j] / P);
2706 void MixtureComp::CalRhoPn_partial()
2713 for (
USI j = 0; j < NP; j++) {
2716 for (
USI m = 0; m < NC; m++) {
2718 +
xi[j1] / nu[j] * (MWC[m] - MW[j]);
2724 void MixtureComp::CalMuPn_partial()
2726 CalMuPnLBC_partial();
2730 void MixtureComp::CalMuPnLBC_partial()
2739 OCP_DBL der1IJ, der2IJ, der3J, der4J, der6J, der7J, der8J;
2742 OCP_DBL derxTj, derxPj, derMWj, derxVj;
2748 for (
USI j = 0; j < NP; j++) {
2749 const USI j1 = phaseLabel[j];
2750 const vector<OCP_DBL>& xj = x[j];
2751 const vector<OCP_DBL>& muAuxj = muAux[j];
2753 xTj = xPj = xVj = 0;
2754 for (
USI i = 0; i < NC; i++) {
2755 xTj += xj[i] * Tc[i];
2756 xPj += xj[i] * Pc[i];
2757 xVj += xj[i] * Vcvis[i];
2759 der7J = xVj *
xiP[j1];
2760 if (muAuxj[3] <= 0.18 &&
false) {
2761 muP[j1] = (2.05 * 1E-4) * der7J / muAuxj[2];
2764 der8J = der7J * (LBCcoef[1] +
2765 muAuxj[3] * (2 * LBCcoef[2] +
2766 muAuxj[3] * (3 * LBCcoef[3] +
2767 muAuxj[3] * 4 * LBCcoef[4])));
2768 muP[j1] = (4 * 1E-4) * pow(muAuxj[4], 3) * der8J / muAuxj[2];
2773 for (
USI k = 0; k < NC; k++) {
2774 derxTj = (Tc[k] - xTj) / nu[j];
2775 derxPj = (Pc[k] - xPj) / nu[j];
2776 derMWj = (MWC[k] - MW[j]) / nu[j];
2779 for (
USI i = 0; i < NC; i++) {
2780 val1IJ = muAux1I[i] / sqrt(MW[j]);
2781 der1IJ = -(1 / 2) * muAux1I[i] * pow(MW[j], -1.5) * derMWj;
2784 tmp = 34 * 1E-5 * pow(Tri, 0.94);
2787 tmp = 17.78 * 1E-5 * pow(4.58 * Tri - 1.67, 0.625);
2789 val2IJ = tmp / val1IJ;
2790 der2IJ = -tmp * der1IJ / (val1IJ * val1IJ);
2791 der3J += sqrtMWi[i] * (xj[i] * der2IJ + (delta(i, k) - xj[i]) * val2IJ / nu[j]);
2792 der4J -= xj[i] * sqrtMWi[i];
2797 (1.0 / 6 * pow(xTj, -5.0 / 6) * derxTj -
2798 pow(xTj, 1.0 / 6) * (0.5 / MW[j] * derMWj + 2.0 / 3 / xPj * derxPj)) /
2799 (sqrt(MW[j]) * pow(xPj, 2.0 / 3));
2800 der7J =
xix[j1 *
numCom + k] * xVj + (Vcvis[k] - xVj) *
xi[j1] / nu[j];
2801 if (muAuxj[3] <= 0.18 &&
false) {
2803 (der3J * muAuxj[1] - muAuxj[0] * der4J) / (muAuxj[1] * muAuxj[1]) +
2804 2.05 * 1E-4 * (der7J * muAuxj[2] - muAuxj[3] * der6J) /
2805 (muAuxj[2] * muAuxj[2]);
2809 der7J * (LBCcoef[1] +
2810 muAuxj[3] * (2 * LBCcoef[2] +
2811 muAuxj[3] * (3 * LBCcoef[3] +
2812 muAuxj[3] * 4 * LBCcoef[4])));
2814 (der3J * muAuxj[1] - muAuxj[0] * der4J) / (muAuxj[1] * muAuxj[1]) +
2816 (4 * pow(muAuxj[4], 3) * der8J * muAuxj[2] -
2817 (pow(muAuxj[4], 4) - 1) * der6J) /
2818 (muAuxj[2] * muAuxj[2]);
2825 void MixtureComp::CalVjpVfpVfx_partial()
2833 fill(
vfi.begin(),
vfi.end(), 0.0);
2836 for (
USI j = 0; j < NP; j++) {
2837 const USI j1 = phaseLabel[j];
2838 const vector<OCP_DBL>& xj = x[j];
2845 for (
USI i = 0; i < NC; i++) {
2847 for (
USI k = 0; k < NC; k++) {
2848 tmp += xj[k] * (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]);
2852 ((bj - zj) * Ax[i] +
2853 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
2854 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
2855 (delta1 + delta2 - 1) * zj * zj) *
2857 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
2858 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
2860 vji[j1][i] = CgTP * nu[j] * Zx[i];
2862 Zp[j] = ((bj - zj) * aj +
2863 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
2864 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
2865 (delta1 + delta2 - 1) * zj * zj) *
2868 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
2869 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
2870 vjp[j1] = CgTP * nu[j] * (Zp[j] - zj / P);
2876 void MixtureComp::CaldXsdXpAPI04()
2889 fill(
res.begin(),
res.end(), 0.0);
2899 const USI j1 = phaseLabel[0];
2902 bId[0] = ((
vfp - vwp) *
vf -
vfp *
v[j1]) / vf2;
2905 for (
USI m = 0; m < NC; m++) {
2906 bId[m] =
vfi[m] * vw / vf2;
2913 for (
USI m = 0; m < ncol; m++) {
2918 for (
USI i = 0; i < NC; i++) {
2919 for (
USI m = 0; m < NC; m++) {
2920 bId[m] = (delta(i, m) * Nh -
Ni[i]) / (Nh * Nh);
2934 void MixtureComp::CaldXsdXp04()
2939 fill(JmatTmp.begin(), JmatTmp.end(), 0.0);
2944 const USI dim = NP + 1 + NP * NC;
2945 for (
USI i = 0; i < NC; i++) {
2947 for (
USI m = 0; m < NP; m++) {
2948 MTmp[m] =
vf *
xi[phaseLabel[m]] * x[m][i];
2954 for (
USI m = 0; m < NP; m++) {
2955 const USI m1 = phaseLabel[m];
2956 for (
USI n = 0; n < NC; n++) {
2958 vf *
S[m1] * (
xix[m1 *
numCom + n] * x[m][i] + delta(i, n) *
xi[m1]);
2959 for (
USI j = 0; j < NP; j++) {
2960 MTmp[n] +=
vji[m1][n] * x[m][i] *
S[phaseLabel[j]] *
xi[phaseLabel[j]];
2971 for (
USI m = 0; m < NP; m++) {
2972 for (
USI n = 0; n < NC; n++) {
2980 for (
USI j = 0; j < NP; j++) {
2985 for (
USI n = 0; n < NC; n++) {
2988 MTmp += (NP - j) * NC;
2992 for (
USI j = 0; j < NP - 1; j++) {
2993 const OCP_DBL* fugXJ = &fugX[j][0];
2994 const OCP_DBL* fugXNP = &fugX[NP - 1][0];
2995 for (
USI i = 0; i < NC; i++) {
3001 for (
USI n = 0; n < NC; n++) {
3002 MTmp[n] = -fugXJ[n];
3006 MTmp += (NP - 1 - j) * NC;
3007 for (
USI n = 0; n < NC; n++) {
3008 MTmp[n] = fugXNP[n];
3015 cout << endl <<
"dFdS" << endl;
3016 cout << scientific << setprecision(1);
3017 for (
USI i = 0; i < dim; i++) {
3018 for (
USI j = 0; j < dim; j++)
3019 cout << setw(8) << JmatTmp[i * dim + j] <<
" ";
3025 for (
USI i = 0; i < dim; i++) {
3026 for (
USI j = 0; j < dim; j++) {
3027 JmatDer[i * dim + j] = JmatTmp[j * dim + i];
3033 fill(JmatTmp.begin(), JmatTmp.end(), 0.0);
3039 const USI nrow = dim;
3041 for (
USI i = 0; i < NC; i++) {
3045 for (
USI j = 0; j < NP; j++) {
3047 tmp01 +=
S[j1] * x[j][i] *
xiP[j1];
3048 tmp02 +=
S[j1] * x[j][i] *
xi[j1];
3050 MTmp[0] = -(
vf * tmp01 +
vfp * tmp02);
3070 const vector<OCP_DBL>& fugPNP = fugP[NP - 1];
3071 for (
USI j = 0; j < NP - 1; j++) {
3072 const vector<OCP_DBL>& fugPj = fugP[j];
3073 for (
USI i = 0; i < NC; i++) {
3075 MTmp[0] = fugPj[i] - fugPNP[i];
3080 cout << endl <<
"dFdp" << endl;
3081 cout << scientific << setprecision(3);
3082 for (
USI i = 0; i < dim; i++) {
3084 cout << setw(10) << JmatTmp[i * (
numCom + 1) + j] <<
" ";
3090 for (
USI i = 0; i < nrhs; i++) {
3091 for (
USI j = 0; j < nrow; j++) {
3092 rhsDer[i * nrow + j] = JmatTmp[j * nrhs + i];
3096 LUSolve(nrhs, dim, &JmatDer[0], &rhsDer[0], &pivot[0]);
3099 cout << setprecision(6);
3100 cout << endl <<
"dXsdXp" << endl;
3101 for (
USI i = 0; i < nrow; i++) {
3102 for (
USI j = 0; j < nrhs; j++) {
3103 cout << setw(13) << rhsDer[j * nrow + i] <<
" ";
3112 void MixtureComp::CalXiPNX_partial()
3124 for (
USI j = 0; j < NP; j++) {
3125 const vector<OCP_DBL>& xj = x[j];
3131 for (
USI i = 0; i < NC; i++) {
3133 for (
USI k = 0; k < NC; k++) {
3134 tmp += xj[k] * (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]);
3138 ((bj - zj) * Ax[i] +
3139 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
3140 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
3141 (delta1 + delta2 - 1) * zj * zj) *
3143 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
3144 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
3147 tmp = -xiC[j] * xiC[j] * CgTP;
3148 for (
USI i = 0; i < NC; i++) {
3149 xixC[j * NC + i] = tmp * Zx[i];
3151 xiPC[j] = tmp * (Zp[j] - Zj[j] / P);
3155 fill(xiNC.begin(), xiNC.end(), 0.0);
3160 fill(
xiN.begin(),
xiN.end(), 0.0);
3162 for (
USI j = 0; j < NP; j++) {
3170 void MixtureComp::CalRhoPX_partial()
3183 for (
USI j = 0; j < NP; j++) {
3186 for (
USI i = 0; i < NC; i++) {
3193 void MixtureComp::CalMuPX_partial()
3199 CalMuPXLBC_partial();
3202 void MixtureComp::CalMuPXLBC_partial()
3211 OCP_DBL der1IJ, der2IJ, der3J, der4J, der6J, der7J, der8J;
3214 OCP_DBL derxTj, derxPj, derMWj, derxVj;
3220 for (
USI j = 0; j < NP; j++) {
3221 const USI j1 = phaseLabel[j];
3222 const vector<OCP_DBL>& xj = x[j];
3223 const vector<OCP_DBL>& muAuxj = muAux[j];
3225 xTj = xPj = xVj = 0;
3226 for (
USI i = 0; i < NC; i++) {
3227 xTj += xj[i] * Tc[i];
3228 xPj += xj[i] * Pc[i];
3229 xVj += xj[i] * Vcvis[i];
3231 der7J = xVj *
xiP[j1];
3232 if (muAuxj[3] <= 0.18 &&
false) {
3233 muP[j1] = (2.05 * 1E-4) * der7J / muAuxj[2];
3236 der8J = der7J * (LBCcoef[1] +
3237 muAuxj[3] * (2 * LBCcoef[2] +
3238 muAuxj[3] * (3 * LBCcoef[3] +
3239 muAuxj[3] * 4 * LBCcoef[4])));
3240 muP[j1] = (4 * 1E-4) * pow(muAuxj[4], 3) * der8J / muAuxj[2];
3245 for (
USI k = 0; k < NC; k++) {
3250 for (
USI i = 0; i < NC; i++) {
3251 val1IJ = muAux1I[i] / sqrt(MW[j]);
3252 der1IJ = -(1 / 2) * muAux1I[i] * pow(MW[j], -1.5) * derMWj;
3255 tmp = 34 * 1E-5 * pow(Tri, 0.94);
3258 tmp = 17.78 * 1E-5 * pow(4.58 * Tri - 1.67, 0.625);
3260 val2IJ = tmp / val1IJ;
3261 der2IJ = -tmp * der1IJ / (val1IJ * val1IJ);
3263 xj[i] * sqrtMWi[i] * der2IJ + delta(i, k) * sqrtMWi[k] * val2IJ;
3268 (1.0 / 6 * pow(xTj, -5.0 / 6) * derxTj -
3269 pow(xTj, 1.0 / 6) * (0.5 / MW[j] * derMWj + 2.0 / 3 / xPj * derxPj)) /
3270 (sqrt(MW[j]) * pow(xPj, 2.0 / 3));
3271 der7J =
xix[j1 *
numCom + k] * xVj +
xi[j1] * Vcvis[k];
3272 if (muAuxj[3] <= 0.18 &&
false) {
3274 (der3J * muAuxj[1] - muAuxj[0] * der4J) / (muAuxj[1] * muAuxj[1]) +
3275 2.05 * 1E-4 * (der7J * muAuxj[2] - muAuxj[3] * der6J) /
3276 (muAuxj[2] * muAuxj[2]);
3280 der7J * (LBCcoef[1] +
3281 muAuxj[3] * (2 * LBCcoef[2] +
3282 muAuxj[3] * (3 * LBCcoef[3] +
3283 muAuxj[3] * 4 * LBCcoef[4])));
3285 (der3J * muAuxj[1] - muAuxj[0] * der4J) / (muAuxj[1] * muAuxj[1]) +
3287 (4 * pow(muAuxj[4], 3) * der8J * muAuxj[2] -
3288 (pow(muAuxj[4], 4) - 1) * der6J) /
3289 (muAuxj[2] * muAuxj[2]);
3296 void MixtureComp::CalXiPNX_full01()
3303 for (
USI j = 0; j < NP; j++) {
3304 const vector<OCP_DBL>& xj = x[j];
3310 for (
USI i = 0; i < NC; i++) {
3312 for (
USI k = 0; k < NC; k++) {
3313 tmp += xj[k] * (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]);
3317 ((bj - zj) * Ax[i] +
3318 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
3319 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
3320 (delta1 + delta2 - 1) * zj * zj) *
3322 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
3323 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
3326 tmp = -xiC[j] * xiC[j] * CgTP;
3327 for (
USI i = 0; i < NC; i++) {
3328 xixC[j * NC + i] = tmp * Zx[i];
3334 tmp = -xiC[0] * xiC[0] * CgTP;
3335 xiPC[0] = tmp * (Zp[0] - Zj[0] / P);
3336 for (
USI i = 0; i < NC; i++) {
3337 xiNC[i] = tmp * Zn[0][i];
3341 const OCP_DBL* dnkjdNP = &rhsDer[0];
3346 for (
USI i = 0; i < NC; i++) {
3347 for (
USI j = 0; j < NP; j++) {
3350 tmp = -xiC[j] * xiC[j] * CgTP;
3351 const vector<OCP_DBL>& Znj = Zn[j];
3353 for (
USI k = 0; k < NC; k++) {
3354 dertmp += Znj[k] * dnkjdNP[k];
3356 xiNC[j * NC + i] = tmp * dertmp;
3362 for (
USI j = 0; j < NP; j++) {
3363 tmp = -xiC[j] * xiC[j] * CgTP;
3364 xiPC[j] = Zp[j] - Zj[j] / P;
3365 const vector<OCP_DBL>& Znj = Zn[j];
3368 for (
USI k = 0; k < NC; k++) {
3369 xiPC[j] += Znj[k] * dnkjdNP[k];
3381 for (
USI j = 0; j < NP; j++) {
3389 void MixtureComp::CalXiPNX_full02()
3399 for (
USI j = 0; j < NP; j++) {
3400 const vector<OCP_DBL>& xj = x[j];
3406 for (
USI i = 0; i < NC; i++) {
3408 for (
USI k = 0; k < NC; k++) {
3409 tmp += xj[k] * (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]);
3413 ((bj - zj) * Ax[i] +
3414 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
3415 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
3416 (delta1 + delta2 - 1) * zj * zj) *
3418 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
3419 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
3422 tmp = -xiC[j] * xiC[j] * CgTP;
3423 for (
USI i = 0; i < NC; i++) {
3424 xixC[j * NC + i] = tmp * Zx[i];
3431 tmp = -xiC[0] * xiC[0] * CgTP;
3432 xiPC[0] = tmp * (Zp[0] - Zj[0] / P);
3433 for (
USI i = 0; i < NC; i++) {
3434 xiNC[i] = tmp * Zn[0][i];
3441 xiPC[0] = (Zp[0] - Zj[0] / P);
3442 xiPC[1] = (Zp[1] - Zj[1] / P);
3443 for (
USI i = 0; i < NC; i++) {
3444 xiPC[0] += Zn[0][i] * nijPN[i];
3445 xiPC[1] -= Zn[1][i] * nijPN[i];
3448 xiPC[0] *= -xiC[0] * xiC[0] * CgTP;
3449 xiPC[1] *= -xiC[1] * xiC[1] * CgTP;
3451 OCP_DBL tmp0 = -xiC[0] * xiC[0] * CgTP;
3452 OCP_DBL tmp1 = -xiC[1] * xiC[1] * CgTP;
3453 for (
USI i = 0; i < NC; i++) {
3456 for (
USI k = 0; k < NC; k++) {
3457 xiNC[i] += Zn[0][k] * nijPN[k];
3458 xiNC[NC + i] -= Zn[1][k] * nijPN[k];
3460 xiNC[NC + i] += Zn[1][i];
3463 xiNC[NC + i] *= tmp1;
3471 for (
USI j = 0; j < NP; j++) {
3479 void MixtureComp::CalRhoPNX_full01()
3489 for (
USI j = 0; j < NP; j++) {
3493 for (
USI m = 0; m < NC; m++) {
3494 rhox[bId + m] =
xix[bId + m] * MW[j] +
xi[j1] * MWC[m];
3501 for (
USI m = 0; m < NC; m++) {
3502 rhoN[bId + m] =
xiN[bId + m] * MW[0];
3504 for (
USI i = 0; i < NC; i++) {
3505 tmp -= MWC[i] *
Ni[i];
3507 rhoN[bId + m] += tmp *
xi[j1] / (Nh * Nh);
3514 const OCP_DBL* xijP = &rhsDer[NP];
3515 for (
USI j = 0; j < NP; j++) {
3518 for (
USI i = 0; i < NC; i++) {
3519 tmp += MWC[i] * xijP[i];
3521 rhoP[j1] +=
xi[j1] * tmp;
3526 for (
USI m = 0; m < NC; m++) {
3528 for (
USI j = 0; j < NP; j++) {
3531 rhoN[bId + m] =
xiN[bId + m] * MW[j];
3533 for (
USI i = 0; i < NC; i++) {
3534 tmp += MWC[i] * xijN[i];
3536 rhoN[bId + m] += tmp *
xi[j1];
3543 void MixtureComp::CalRhoPNX_full()
3558 for (
USI m = 0; m < NC; m++) {
3559 rhox[bId + m] =
xix[bId + m] * MW[0] +
xi[j1] * MWC[m];
3560 rhoN[bId + m] =
xiN[bId + m] * MW[0];
3562 for (
USI i = 0; i < NC; i++) {
3563 tmp -= MWC[i] *
Ni[i];
3565 rhoN[bId + m] += tmp *
xi[j1] / (Nh * Nh);
3570 fill(
rhoP.begin(),
rhoP.end() - 1, 0.0);
3573 for (
USI j = 0; j < NP; j++) {
3578 for (
USI i = 0; i < NC; i++) {
3579 rhoP[j1] += MWC[i] * xijPN[0];
3581 for (
USI m = 0; m < NC; m++) {
3582 rhoN[bId + m] += MWC[i] * xijPN[m];
3589 for (
USI m = 0; m < NC; m++) {
3591 rhoN[bId + m] +=
xiN[bId + m] * MW[j];
3592 rhox[bId + m] =
xix[bId + m] * MW[j] +
xi[j1] * MWC[m];
3598 void MixtureComp::CalMuPX_full01()
3600 fill(
muP.begin(),
muP.end() - 1, 0.0);
3604 CalMuPXLBC_full01();
3607 void MixtureComp::CalMuPXLBC_full01()
3612 OCP_DBL der1IJ, der2IJ, der3J, der4J, der6J, der7J, der8J;
3615 OCP_DBL derxTj, derxPj, derMWj, derxVj;
3618 for (
USI j = 0; j < NP; j++) {
3619 const vector<OCP_DBL>& xj = x[j];
3620 const vector<OCP_DBL>& muAuxj = muAux[j];
3621 const USI j1 = phaseLabel[j];
3623 xTj = xPj = xVj = 0;
3624 for (
USI i = 0; i < NC; i++) {
3625 xTj += xj[i] * Tc[i];
3626 xPj += xj[i] * Pc[i];
3627 xVj += xj[i] * Vcvis[i];
3629 for (
USI k = 0; k < NC; k++) {
3635 for (
USI i = 0; i < NC; i++) {
3636 val1IJ = muAux1I[i] / sqrt(MW[j]);
3637 der1IJ = -(1 / 2) * muAux1I[i] * pow(MW[j], -1.5) * derMWj;
3640 tmp = 34 * 1E-5 * pow(Tri, 0.94);
3643 tmp = 17.78 * 1E-5 * pow(4.58 * Tri - 1.67, 0.625);
3645 val2IJ = tmp / val1IJ;
3646 der2IJ = -tmp * der1IJ / (val1IJ * val1IJ);
3647 der3J += sqrtMWi[i] * (delta(i, k) * val2IJ + xj[i] * der2IJ);
3651 (1.0 / 6 * pow(xTj, -5.0 / 6) * derxTj -
3653 (0.5 / MW[j] * derMWj + 2.0 / 3 / xPj * derxPj)) /
3654 (sqrt(MW[j]) * pow(xPj, 2.0 / 3));
3655 der7J =
xix[bId + k] * xVj +
xi[j1] * derxVj;
3656 if (muAuxj[3] <= 0.18 &&
false) {
3657 mux[bId + k] = (der3J * muAuxj[1] - muAuxj[0] * der4J) /
3658 (muAuxj[1] * muAuxj[1]) +
3660 (der7J * muAuxj[2] - muAuxj[3] * der6J) /
3661 (muAuxj[2] * muAuxj[2]);
3666 muAuxj[3] * (2 * LBCcoef[2] +
3667 muAuxj[3] * (3 * LBCcoef[3] +
3668 muAuxj[3] * 4 * LBCcoef[4])));
3669 mux[bId + k] = (der3J * muAuxj[1] - muAuxj[0] * der4J) /
3670 (muAuxj[1] * muAuxj[1]) +
3672 (4 * pow(muAuxj[4], 3) * der8J * muAuxj[2] -
3673 (pow(muAuxj[4], 4) - 1) * der6J) /
3674 (muAuxj[2] * muAuxj[2]);
3683 const vector<OCP_DBL>& xj = x[0];
3684 const vector<OCP_DBL>& muAuxj = muAux[0];
3685 xTj = xPj = xVj = 0;
3686 for (
USI i = 0; i < NC; i++) {
3687 xTj += xj[i] * Tc[i];
3688 xPj += xj[i] * Pc[i];
3689 xVj += xj[i] * Vcvis[i];
3691 der7J = xVj * xiPC[0];
3692 if (muAuxj[3] <= 0.18 &&
false) {
3693 muP[phaseLabel[0]] = (2.05 * 1E-4) * der7J / muAuxj[2];
3695 der8J = der7J * (LBCcoef[1] +
3696 muAuxj[3] * (2 * LBCcoef[2] +
3697 muAuxj[3] * (3 * LBCcoef[3] +
3698 muAuxj[3] * 4 * LBCcoef[4])));
3699 muP[phaseLabel[0]] = (4 * 1E-4) * pow(muAuxj[4], 3) * der8J / muAuxj[2];
3705 for (
USI j = 0; j < NP; j++) {
3706 const OCP_DBL* xijP = &rhsDer[NP + j * NC];
3707 const vector<OCP_DBL>& xj = x[j];
3708 const vector<OCP_DBL>& muAuxj = muAux[j];
3709 const USI j1 = phaseLabel[j];
3710 xTj = xPj = xVj = 0;
3711 derxTj = derxPj = derMWj = 0;
3712 for (
USI i = 0; i < NC; i++) {
3713 xTj += xj[i] * Tc[i];
3714 xPj += xj[i] * Pc[i];
3715 xVj += xj[i] * Vcvis[i];
3716 derxTj += xijP[i] * Tc[i];
3717 derxPj += xijP[i] * Pc[i];
3718 derMWj += xijP[i] * MWC[i];
3720 der3J = der4J = der7J = 0;
3721 for (
USI i = 0; i < NC; i++) {
3722 val1IJ = muAux1I[i] / sqrt(MW[j]);
3723 der1IJ = -(1 / 2) * muAux1I[i] * pow(MW[j], -1.5) * derMWj;
3726 tmp = 34 * 1E-5 * pow(Tri, 0.94);
3728 tmp = 17.78 * 1E-5 * pow(4.58 * Tri - 1.67, 0.625);
3730 val2IJ = tmp / val1IJ;
3731 der2IJ = -tmp * der1IJ / (val1IJ * val1IJ);
3732 der3J += sqrtMWi[i] * (xijP[i] * val2IJ + xj[i] * der2IJ);
3733 der4J += sqrtMWi[i] * xijP[i];
3734 der7J += xijP[i] * Vcvis[i];
3737 der7J +=
xiP[j1] * xVj;
3740 (1.0 / 6 * pow(xTj, -5.0 / 6) * derxTj -
3741 pow(xTj, 1.0 / 6) * (0.5 / MW[j] * derMWj + 2.0 / 3 / xPj * derxPj)) /
3742 (sqrt(MW[j]) * pow(xPj, 2.0 / 3));
3743 if (muAuxj[3] <= 0.18 &&
false) {
3745 (der3J * muAuxj[1] - muAuxj[0] * der4J) / (muAuxj[1] * muAuxj[1]) +
3746 2.05 * 1E-4 * (der7J * muAuxj[2] - muAuxj[3] * der6J) /
3747 (muAuxj[2] * muAuxj[2]);
3750 der7J * (LBCcoef[1] +
3751 muAuxj[3] * (2 * LBCcoef[2] +
3752 muAuxj[3] * (3 * LBCcoef[3] +
3753 muAuxj[3] * 4 * LBCcoef[4])));
3755 (der3J * muAuxj[1] - muAuxj[0] * der4J) / (muAuxj[1] * muAuxj[1]) +
3757 (4 * pow(muAuxj[4], 3) * der8J * muAuxj[2] -
3758 (pow(muAuxj[4], 4) - 1) * der6J) /
3759 (muAuxj[2] * muAuxj[2]);
3766 void MixtureComp::CalXiRhoMuPN_pfullx()
3777 const USI j1 = phaseLabel[0];
3779 xijPN += bId * ncol;
3781 for (
USI i = 0; i < NC; i++) {
3784 for (
USI m = 0; m < NC; m++) {
3785 xiN[bId + m] +=
xix[bId + i] * xijPN[m];
3786 rhoN[bId + m] +=
rhox[bId + i] * xijPN[m];
3787 muN[bId + m] +=
mux[bId + i] * xijPN[m];
3801 for (
USI i = 0; i < NC; i++) {
3803 xiP[j] +=
xix[bId + i] * xijPN[0];
3804 rhoP[j] +=
rhox[bId + i] * xijPN[0];
3805 muP[j] +=
mux[bId + i] * xijPN[0];
3808 for (
USI m = 0; m < NC; m++) {
3809 xiN[bId + m] +=
xix[bId + i] * xijPN[m];
3810 rhoN[bId + m] +=
rhox[bId + i] * xijPN[m];
3811 muN[bId + m] +=
mux[bId + i] * xijPN[m];
3821 void MixtureComp::CalXiRhoMuPN_pfullxn(
const bool& xflag)
3828 if (NP == 1 && !xflag) {
3829 const USI j1 = phaseLabel[0];
3833 for (
USI i = 0; i < NC; i++) {
3834 xiN[bId + i] =
xix[bId + i];
3836 muN[bId + i] =
mux[bId + i];
3849 for (
USI i = 0; i < NC; i++) {
3851 xiP[j] +=
xix[bId + i] * xijPN[0];
3852 rhoP[j] +=
rhox[bId + i] * xijPN[0];
3853 muP[j] +=
mux[bId + i] * xijPN[0];
3856 for (
USI m = 0; m < NC; m++) {
3857 xiN[bId + m] +=
xix[bId + i] * xijPN[m];
3858 rhoN[bId + m] +=
rhox[bId + i] * xijPN[m];
3859 muN[bId + m] +=
mux[bId + i] * xijPN[m];
3869 void MixtureComp::CalVfiVfp_full01()
3878 const vector<OCP_DBL>& xj = x[0];
3879 vector<OCP_DBL>& Znij = Zn[0];
3882 for (
USI i = 0; i < NC; i++) {
3884 for (
USI m = 0; m < NC; m++) {
3885 tmp += (1 - BIC[i * NC + m]) * sqrt(Ai[i] * Ai[m]) * xj[m];
3887 An[i] = 2 / nu[0] * (tmp - aj);
3888 Bn[i] = 1 / nu[0] * (Bi[i] - bj);
3890 ((bj - zj) * An[i] +
3891 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
3892 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
3893 (delta1 + delta2 - 1) * zj * zj) *
3895 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
3896 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
3897 vfi[i] = CgTP * (zj + nu[0] * Znij[i]);
3899 Zp[0] = ((bj - zj) * aj +
3900 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
3901 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
3902 (delta1 + delta2 - 1) * zj * zj) *
3905 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
3906 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
3907 vfp = CgTP * nu[0] * (Zp[0] - zj / P);
3912 AssembleMatVfiVfp_full01();
3913 AssembleRhsVfiVfp_full01();
3914 LUSolve(NC + 1, NC * NP, &JmatDer[0], &rhsDer[0], &pivot[0]);
3916 OCP_DBL* dnkjdNP = &rhsDer[0];
3917 for (
USI i = 0; i < NC; i++) {
3919 for (
USI j = 0; j < NP; j++) {
3920 for (
USI k = 0; k < NC; k++) {
3921 vfi[i] += dnkjdNP[j * NC + k] * (Zj[j] + nu[j] * Zn[j][k]);
3929 for (
USI j = 0; j < NP; j++) {
3930 vfp += nu[j] / P * (Zp[j] * P - Zj[j]);
3931 for (
USI k = 0; k < NC; k++) {
3932 vfp += dnkjdNP[j * NC + k] * (Zj[j] + nu[j] * Zn[j][k]);
3949 void MixtureComp::AssembleMatVfiVfp_full01()
3951 fill(JmatDer.begin(), JmatDer.end(), 0.0);
3955 for (
USI j = 0; j < NP - 1; j++) {
3958 for (
USI i = 0; i < NC; i++) {
3960 Dcopy(NC, bId, fugNj);
3961 bId += (NP - 1 - j) * NC;
3963 bId += (1 + j) * NC;
3969 bId = &JmatDer[(NP - 1) * (NP * NC * NC)];
3970 OCP_DBL* fugNj = &fugN[NP - 1][0];
3971 for (
USI i = 0; i < NC; i++) {
3972 for (
USI j = 0; j < NP - 1; j++) {
3973 Dcopy(NC, bId, fugNj);
3976 Dscalar((NP - 1) * NC, -1.0, bId - (NP - 1) * NC);
3983 void MixtureComp::AssembleRhsVfiVfp_full01()
3985 fill(rhsDer.begin(), rhsDer.end(), 0.0);
3987 for (
USI k = 0; k < NC; k++) {
3989 rhstmp[NC * (NP - 1) + k] = 1;
3993 for (
USI j = 0; j < NP; j++) {
3994 for (
USI i = 0; i < NC; i++) {
3995 rhstmp[j * NC + i] = fugP[NP - 1][i] - fugP[j][i];
4001 if (!
CheckNan(rhsDer.size(), &rhsDer[0])) {
4007 void MixtureComp::CalVfiVfp_full02()
4019 const vector<OCP_DBL>& xj = x[0];
4020 vector<OCP_DBL>& Znij = Zn[0];
4023 for (
USI i = 0; i < NC; i++) {
4025 for (
USI m = 0; m < NC; m++) {
4026 tmp += (1 - BIC[i * NC + m]) * sqrt(Ai[i] * Ai[m]) * xj[m];
4028 An[i] = 2 / nu[0] * (tmp - aj);
4029 Bn[i] = 1 / nu[0] * (Bi[i] - bj);
4031 ((bj - zj) * An[i] +
4032 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
4033 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
4034 (delta1 + delta2 - 1) * zj * zj) *
4036 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
4037 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
4038 vfi[i] = CgTP * (zj + nu[0] * Znij[i]);
4040 Zp[0] = ((bj - zj) * aj +
4041 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
4042 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
4043 (delta1 + delta2 - 1) * zj * zj) *
4046 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
4047 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
4048 vfp = CgTP * nu[0] * (Zp[0] - zj / P);
4054 AssembleMatVfiVfp_full02();
4055 AssembleRhsVfiVfp_full02();
4056 LUSolve(NC + 1, NC, &JmatDer[0], &rhsDer[0], &pivot[0]);
4058 const OCP_DBL* dnkjdNP = &rhsDer[0];
4060 const USI j0 = phaseLabel[0];
4061 const USI j1 = phaseLabel[1];
4062 vjp[j0] = nu[0] * (Zp[0] - Zj[0] / P);
4063 vjp[j1] = nu[1] * (Zp[1] - Zj[1] / P);
4064 for (
USI k = 0; k < NC; k++) {
4065 vjp[j0] += (Zj[0] + nu[0] * Zn[0][k]) * dnkjdNP[k];
4066 vjp[j1] -= (Zj[1] + nu[1] * Zn[1][k]) * dnkjdNP[k];
4074 for (
USI i = 0; i < NC; i++) {
4078 for (
USI k = 0; k < NC; k++) {
4079 vji[j0][i] += (Zj[0] + nu[0] * Zn[0][k]) * dnkjdNP[k];
4080 vji[j1][i] += (Zj[1] + nu[1] * Zn[1][k]) * (delta(i, k) - dnkjdNP[k]);
4090 void MixtureComp::AssembleMatVfiVfp_full02()
4093 fill(JmatDer.begin(), JmatDer.end(), 0.0);
4096 OCP_DBL* tmpMat = &JmatDer[0];
4097 for (
USI i = 0; i < NC; i++) {
4098 for (
USI m = 0; m < NC; m++) {
4099 tmpMat[m] = fugN[0][i * NC + m] + fugN[1][i * NC + m];
4105 void MixtureComp::AssembleRhsVfiVfp_full02()
4108 fill(rhsDer.begin(), rhsDer.end(), 0.0);
4112 for (
USI i = 0; i < NC; i++) {
4113 rhstmp[i] = fugP[1][i] - fugP[0][i];
4118 for (
USI k = 0; k < NC; k++) {
4119 for (
USI i = 0; i < NC; i++) {
4120 rhstmp[i] = fugN[1][k * NC + i];
4126 void MixtureComp::CaldXsdXp01()
4135 fill(JmatTmp.begin(), JmatTmp.end(), 0.0);
4140 for (
USI i = 0; i < NC; i++) {
4142 for (
USI m = 0; m < NP; m++) {
4143 MTmp[m] =
vf * xiC[m] * x[m][i];
4147 for (
USI m = 0; m < NP; m++) {
4148 for (
USI n = 0; n < NC; n++) {
4150 vf *
S[m] * (xixC[m * NC + n] * x[m][i] + delta(i, n) * xiC[m]);
4156 for (
USI j = 0; j < NP; j++) {
4161 for (
USI n = 0; n < NC; n++) {
4164 MTmp += (NP - j) * NC;
4167 for (
USI j = 0; j < NP - 1; j++) {
4168 const OCP_DBL* fugXJ = &fugX[j][0];
4169 const OCP_DBL* fugXNP = &fugX[NP - 1][0];
4170 for (
USI i = 0; i < NC; i++) {
4176 for (
USI n = 0; n < NC; n++) {
4181 MTmp += (NP - 1 - j) * NC;
4182 for (
USI n = 0; n < NC; n++) {
4183 MTmp[n] = -fugXNP[n];
4190 USI row01 = NP * (NC + 1);
4201 USI dim = NP * (NC + 1);
4202 for (
USI i = 0; i < dim; i++) {
4203 for (
USI j = 0; j < dim; j++) {
4204 JmatDer[i * dim + j] = JmatTmp[j * dim + i];
4210 fill(JmatTmp.begin(), JmatTmp.end(), 0.0);
4215 for (
USI i = 0; i < NC; i++) {
4219 for (
USI j = 0; j < NP; j++) {
4220 tmp01 +=
S[j] * x[j][i] * xiPC[j];
4221 tmp02 +=
S[j] * x[j][i] * xiC[j];
4223 MTmp[0] = -(
vf * tmp01 +
vfp * tmp02);
4228 for (
USI k = 0; k < NC; k++) {
4230 for (
USI j = 0; j < NP; j++) {
4231 tmp01 +=
S[j] * x[j][i] * xiNC[j * NC + k];
4233 MTmp[k] = -(
vf * tmp01 +
vfi[k] * tmp02 - delta(i, k));
4241 MTmp += NP * (NC + 1);
4244 const vector<OCP_DBL>& fugPNP = fugP[NP - 1];
4245 for (
USI j = 0; j < NP - 1; j++) {
4246 const vector<OCP_DBL>& fugPj = fugP[j];
4247 for (
USI i = 0; i < NC; i++) {
4249 MTmp[0] = -(fugPj[i] - fugPNP[i]);
4254 USI row02 = NP * (NC + 1);
4266 USI nrow = (NC + 1) * NP;
4267 for (
USI i = 0; i < nrhs; i++) {
4268 for (
USI j = 0; j < nrow; j++) {
4269 rhsDer[i * nrow + j] = JmatTmp[j * nrhs + i];
4272 LUSolve(NC + 1, (NC + 1) * NP, &JmatDer[0], &rhsDer[0], &pivot[0]);
4284 void MixtureComp::CaldXsdXpAPI01()
4298 for (
USI k = 0; k < NC; k++) {
4299 dXsdXp[bId + k + 1] = (
vfi[k] * vw) / tmp;
4303 for (
USI i = 0; i < NC; i++) {
4304 for (
USI k = 0; k < NC; k++) {
4305 dXsdXp[bId + k] = (delta(i, k) * Nh -
Ni[i]) / (Nh * Nh);
4312 for (
USI j = 0; j < NP; j++) {
4320 USI nrow = NP * (NC + 1);
4324 for (
USI j = 0; j < NP; j++) {
4326 DTmp = &
dXsdXp[phaseLabel[j] * ncol2];
4331 for (
USI k = 0; k < NC; k++) {
4339 for (
USI j = 0; j < NP; j++) {
4340 DTmp = DbId + phaseLabel[j] *
numCom * ncol2;
4341 for (
USI i = 0; i < NC; i++) {
4342 STmp = &rhsDer[NP + j * NC + i];
4347 for (
USI k = 0; k < NC; k++) {
4366 for (
USI j = 0; j < NP; j++) {
4367 dXsdXp[(phaseLabel[j] + 1) * ncol - 1] = -
S[phaseLabel[j]] *
vfi[Wcid] /
vf;
4372 Dtmp[0] = (vwp *
vf - vw *
vfp) / vf2;
4375 for (
USI k = 0; k < NC; k++) {
4376 Dtmp[k] = -vw *
vfi[k] / vf2;
4379 Dtmp[NC] = (
vfi[Wcid] *
vf - vw *
vfi[Wcid]) / vf2;
4382 void MixtureComp::CaldXsdXpAPI02()
4400 USI j0 = phaseLabel[0];
4403 bId[0] = ((
vfp - vwp) *
vf -
vfp *
v[j0]) / vf2;
4406 for (
USI m = 0; m < NC; m++) {
4407 bId[m] =
vfi[m] * vw / vf2;
4414 for (
USI m = 0; m < ncol; m++) {
4415 bId[m] = -
dXsdXp[j0 * ncol + m];
4419 for (
USI i = 0; i < NC; i++) {
4420 for (
USI m = 0; m < NC; m++) {
4421 bId[m] = (delta(i, m) * Nh -
Ni[i]) / (Nh * Nh);
4430 for (
USI j = 0; j < 2; j++) {
4431 const USI j1 = phaseLabel[j];
4432 bId = &
dXsdXp[j1 * ncol];
4434 bId[0] = (
vjp[j1] *
vf -
vfp *
v[j1]) / vf2;
4438 bId[m] = (
vji[j1][m] *
vf -
vfi[m] *
v[j1]) / vf2;
4444 bId[0] = (vwp *
vf -
vfp * vw) / vf2;
4453 const USI j0 = phaseLabel[0];
4454 const USI j1 = phaseLabel[1];
4455 const OCP_DBL* dnkjdNP = &rhsDer[0];
4460 for (
USI i = 0; i < NC; i++)
4461 njDerSum += dnkjdNP[i];
4462 for (
USI i = 0; i < NC; i++) {
4463 bId1[0] = (dnkjdNP[i] * nu[0] - njDerSum * Nh * n[0][i]) / (nu[0] * nu[0]);
4464 bId2[0] = (-dnkjdNP[i] * nu[1] + njDerSum * Nh * n[1][i]) / (nu[1] * nu[1]);
4470 for (
USI m = 0; m < NC; m++) {
4472 for (
USI i = 0; i < NC; i++)
4473 njDerSum += dnkjdNP[i];
4474 bId1 = bId + j0 *
numCom * ncol + m + 1;
4475 bId2 = bId + j1 *
numCom * ncol + m + 1;
4476 for (
USI i = 0; i < NC; i++) {
4477 bId1[0] = (dnkjdNP[i] * nu[0] - njDerSum * Nh * n[0][i]) / (nu[0] * nu[0]);
4478 bId2[0] = ((delta(i, m) - dnkjdNP[i]) * nu[1] - (1 - njDerSum) * Nh * n[1][i]) / (nu[1] * nu[1]);
4488 void MixtureComp::CaldXsdXpAPI02p()
4510 const USI j1 = phaseLabel[0];
4513 bId[0] = ((
vfp - vwp) *
vf -
vfp *
v[j1]) / vf2;
4516 for (
USI m = 0; m < NC; m++) {
4517 bId[m] =
vfi[m] * vw / vf2;
4524 for (
USI m = 0; m < ncol; m++) {
4529 for (
USI i = 0; i < NC; i++) {
4530 for (
USI m = 0; m < NC; m++) {
4531 bId[m] = (delta(i, m) * Nh -
Ni[i]) / (Nh * Nh);
4540 for (
USI j = 0; j < 2; j++) {
4541 const USI j1 = phaseLabel[j];
4542 bId = &
dXsdXp[j1 * ncol];
4544 bId[0] = (
vjp[j1] *
vf -
vfp *
v[j1]) / vf2;
4548 bId[m] = (
vji[j1][m] *
vf -
vfi[m] *
v[j1]) / vf2;
4554 bId[0] = (vwp *
vf -
vfp * vw) / vf2;
4563 const USI j0 = phaseLabel[0];
4564 const USI j1 = phaseLabel[1];
4565 const OCP_DBL* dnkjdNP = &rhsDer[0];
4566 OCP_DBL* bId1 = bId + j0 * NC * ncol;
4567 OCP_DBL* bId2 = bId + j1 * NC * ncol;
4570 for (
USI i = 0; i < NC; i++)
4571 njDerSum += dnkjdNP[i];
4572 for (
USI i = 0; i < NC; i++) {
4573 bId1[0] = (dnkjdNP[i] * nu[0] - njDerSum * Nh * n[0][i]) / (nu[0] * nu[0]);
4574 bId2[0] = (-dnkjdNP[i] * nu[1] + njDerSum * Nh * n[1][i]) / (nu[1] * nu[1]);
4580 for (
USI m = 0; m < NC; m++) {
4582 for (
USI i = 0; i < NC; i++)
4583 njDerSum += dnkjdNP[i];
4584 bId1 = bId + j0 * NC * ncol + m + 1;
4585 bId2 = bId + j1 * NC * ncol + m + 1;
4586 for (
USI i = 0; i < NC; i++) {
4587 bId1[0] = (dnkjdNP[i] * nu[0] - njDerSum * Nh * n[0][i]) / (nu[0] * nu[0]);
4588 bId2[0] = ((delta(i, m) - dnkjdNP[i]) * nu[1] - (1 - njDerSum) * Nh * n[1][i]) / (nu[1] * nu[1]);
4598 void MixtureComp::CaldXsdXpAPI03()
4608 fill(
res.begin(),
res.end(), 0.0);
4618 const USI j0 = phaseLabel[0];
4621 bId[0] = ((
vfp - vwp) *
vf -
vfp *
v[j0]) / vf2;
4624 for (
USI m = 0; m < NC; m++) {
4625 bId[m] =
vji[j0][m] * vw / vf2;
4632 for (
USI m = 0; m < ncol; m++) {
4637 bId = &
dXsdXp[2 * ncol + 1];
4638 for (
USI i = 0; i < NC; i++) {
4664 const OCP_DBL* STmp = &rhsDer[0];
4666 const USI wNP = NP + 1;
4667 const USI nrow = wNP + NP * NC;
4670 for (
USI i = 0; i < nrhs; i++) {
4672 for (
USI j = 0; j < NP; j++) {
4673 dXsdXp[sIndex[phaseLabel[j]] * nrhs + i] = STmp[j];
4677 dXsdXp[NP * nrhs + i] = STmp[0];
4680 for (
USI j = 0; j < NP; j++) {
4681 bId = wNP + sIndex[phaseLabel[j]] * NC;
4682 for (
USI k = 0; k < NC; k++) {
4683 dXsdXp[(bId + k) * nrhs + i] = STmp[k];
4691 for (
USI j = 0; j < NP; j++) {
4692 res[sIndex[phaseLabel[j]]] = STmp[j];
4699 for (
USI j = 0; j < NP; j++) {
4700 bId = wNP + sIndex[phaseLabel[j]] * NC;
4701 for (
USI k = 0; k < NC; k++) {
4702 res[bId + k] = STmp[k];
4710 for (
USI j = 0; j < NP; j++) {
4711 const USI j1 = phaseLabel[j];
4712 for (
USI i = 0; i < NC; i++) {
4741 void MixtureComp::CaldXsdXp03()
4746 fill(JmatTmp.begin(), JmatTmp.end(), 0.0);
4748 const USI dim = NP + 1 + NP * NC;
4753 for (
USI j = 0; j < NP - 1; j++) {
4754 const OCP_DBL* fugNJ = &fugN[j][0];
4755 const OCP_DBL* fugNNP = &fugN[NP - 1][0];
4756 for (
USI i = 0; i < NC; i++) {
4761 for (
USI k = 0; k < NC; k++) {
4762 MTmp[k] = -fugNJ[k];
4766 MTmp += (NP - 1 - j) * NC;
4767 for (
USI k = 0; k < NC; k++) {
4768 MTmp[k] = fugNNP[k];
4775 for (
USI i = 0; i < NC; i++) {
4779 for (
USI j = 0; j < NP; j++) {
4785 for (
USI j = 0; j < NP; j++) {
4786 const USI j1 = phaseLabel[j];
4791 for (
USI m = 0; m < NP; m++) {
4792 const USI m1 = phaseLabel[m];
4794 for (
USI k = 0; k < NC; k++) {
4795 MTmp[k] =
vji[j1][k] * (
vf -
v[j1]) / vf2;
4799 for (
USI k = 0; k < NC; k++) {
4800 MTmp[k] = -
vji[m1][k] *
v[j1] / vf2;
4811 for (
USI m = 0; m < NP; m++) {
4812 const USI m1 = phaseLabel[m];
4813 for (
USI k = 0; k < NC; k++) {
4831 for (
USI i = 0; i < dim; i++) {
4832 for (
USI j = 0; j < dim; j++) {
4833 JmatDer[i * dim + j] = JmatTmp[j * dim + i];
4837 fill(JmatTmp.begin(), JmatTmp.end(), 0.0);
4843 for (
USI j = 0; j < NP - 1; j++) {
4844 for (
USI i = 0; i < NC; i++) {
4845 MTmp[0] = fugP[j][i] - fugP[NP - 1][i];
4850 for (
USI i = 0; i < NC; i++) {
4856 for (
USI j = 0; j < NP; j++) {
4857 const USI j1 = phaseLabel[j];
4859 MTmp[0] = (
vfp *
v[j1] -
vjp[j1] *
vf) / vf2;
4885 const USI nrow = NP * NC + NP + 1;
4886 for (
USI i = 0; i < nrhs; i++) {
4887 for (
USI j = 0; j < nrow; j++) {
4888 rhsDer[i * nrow + j] = JmatTmp[j * nrhs + i];
4894 OCP_DBL* myRes = &rhsDer[nrhs * nrow];
4895 for (
USI j = 0; j < NP - 1; j++) {
4896 const OCP_DBL* fugJ = &fug[j][0];
4897 const OCP_DBL* fugNP = &fug[NP - 1][0];
4898 for (
USI i = 0; i < NC; i++) {
4899 myRes[i] = fugJ[i] - fugNP[i];
4904 for (
USI i = 0; i < NC; i++) {
4906 for (
USI j = 0; j < NP; j++) {
4907 myRes[i] -= x[j][i] * nu[j];
4912 for (
USI j = 0; j < NP; j++) {
4913 myRes[j] =
S[phaseLabel[j]] -
v[phaseLabel[j]] /
vf;
4928 LUSolve(
numCom + 1 + 1, nrow, &JmatDer[0], &rhsDer[0], &pivot[0]);
4931 void MixtureComp::CalVfiVfp_full03()
4939 for (
USI i = 0; i < NC; i++) {
4951 for (
USI i = 0; i < NC; i++) {
4952 vfp +=
vji[j][i] * nijPN[0];
4954 for (
USI m = 0; m < NC; m++) {
4955 vfi[m] +=
vji[j][i] * nijPN[m];
4965 void MixtureComp::CalKeyDerx()
4992 tmp =
xi[j] / (
mu[j] *
mu[j]);
4993 for (
USI i = 0; i < NC; i++) {
4995 dTmp[0] = (sTmp[0] *
xi[j] + xijtmp[i] *
xiP[j]) /
mu[j] - xijtmp[i] *
muP[j] * tmp;
4999 for (
USI k = 0; k < NC; k++) {
5000 dTmp[k] = (sTmp[k] *
xi[j] + xijtmp[i] * xiNtmp[k]) /
mu[j] - xijtmp[i] * muNtmp[k] * tmp;
5015 keyDer[NP * NC * ncol] = (
xiP[wpid] *
mu[wpid] -
muP[wpid] *
xi[wpid]) / (
mu[wpid] *
mu[wpid]);
5019 void MixtureComp::CalKeyDern()
5028 vector<OCP_DBL> njPN(NC + 1, 0);
5040 const USI j = phaseLabel[0];
5046 for (
USI i = 0; i < NC; i++) {
5048 dTmp[0] = (xijtmp[i] *
xiP[j]) /
mu[j] - xijtmp[i] *
muP[j] * tmp;
5051 for (
USI k = 0; k < NC; k++) {
5052 dTmp[k] = ((delta(i, k) - xijtmp[i]) *
xi[j] /
nj[j] + xijtmp[i] * xiNtmp[k]) /
mu[j] - xijtmp[i] * muNtmp[k] * tmp;
5067 fill(njPN.begin(), njPN.end(), 0.0);
5068 for (
USI i = 0; i < NC; i++) {
5069 for (
USI k = 0; k < NC + 1; k++) {
5076 tmp01 =
xi[j] / (
mu[j] *
mu[j]);
5077 for (
USI i = 0; i < NC; i++) {
5078 tmp02 = (sTmp[0] - njPN[0] * xijtmp[i]) /
nj[j];
5080 dTmp[0] = (tmp02 *
xi[j] + xijtmp[i] *
xiP[j]) /
mu[j] - xijtmp[i] *
muP[j] * tmp01;
5084 for (
USI k = 0; k < NC; k++) {
5085 tmp02 = (sTmp[k] - njPN[k + 1] * xijtmp[i]) /
nj[j];
5086 dTmp[k] = (tmp02 *
xi[j] + xijtmp[i] * xiNtmp[k]) /
mu[j] - xijtmp[i] * muNtmp[k] * tmp01;
5100 keyDer[NP * NC * ncol] = (
xiP[wpid] *
mu[wpid] -
muP[wpid] *
xi[wpid]) / (
mu[wpid] *
mu[wpid]);
5106 const bool& NTflag)
const
5109 OCP_DBL Q = (a * a - 3 * b) / 9;
5110 OCP_DBL R = (2 * a * a * a - 9 * a * b + 27 * c) / 54;
5117 OCP_DBL theta = acos(R / sqrt(Q3));
5118 Ztmp[0] = -2 * sqrt(Q) * cos(theta / 3) - a / 3;
5119 Ztmp[1] = -2 * sqrt(Q) * cos((theta + 2 *
PI) / 3) - a / 3;
5120 Ztmp[2] = -2 * sqrt(Q) * cos((theta - 2 *
PI) / 3) - a / 3;
5123 NTcubicroot(Ztmp[0], a, b, c);
5124 NTcubicroot(Ztmp[1], a, b, c);
5125 NTcubicroot(Ztmp[2], a, b, c);
5128 sort(Ztmp.begin(), Ztmp.end());
5144 Ztmp[0] =
S + T - a / 3;
5147 NTcubicroot(Ztmp[0], a, b, c);
5184 OCP_DBL e = root * (root * (root + a) + b) + c;
5190 while (fabs(e) > 1E-8) {
5192 df = root * (3 * root + 2 * a) + b;
5193 root = root - e / df;
5199 e = root * (root * (root + a) + b) + c;
5200 if (fabs(e) <= opte) {
void SYSSolve(const int &nrhs, const char *uplo, const int &N, double *A, double *b, int *pivot, double *work, const int &lwork)
Calls dsysy to solve the linear system for symm matrices.
void Dscalar(const int &n, const double &alpha, double *x)
Scales a vector by a constant.
void LUSolve(const int &nrhs, const int &N, double *A, double *b, int *pivot)
Calls dgesv to solve the linear system for general matrices.
bool CheckNan(const int &N, const T *x)
check NaN
void Daxpy(const int &n, const double &alpha, const double *x, double *y)
Constant times a vector plus a vector.
void MinEigenSY(const int &N, float *A, float *w, float *work, const int &lwork)
Calculate the minimal eigenvalue for sysmetric matrix with mkl lapack.
double Dnorm1(const int &N, double *x)
Computes the L1-norm of a vector.
void PrintDX(const int &N, const T *x)
Prints a vector.
void Dcopy(const int &N, double *dst, const double *src)
Calculate the minimal eigenvalue for sysmetric matrix with mkl lapack.
double Dnorm2(const int &N, double *x)
Computes the L2-norm of a vector.
OCP_DBL signD(const OCP_DBL &d)
Return the sign of double di.
MixtureComp class declaration.
unsigned int USI
Generic unsigned integer.
const USI GAS
Fluid type = gas.
double OCP_DBL
Double precision.
const OCP_DBL CONV1
1 bbl = CONV1 ft3
const OCP_DBL GRAVITY_FACTOR
0.00694444 ft2 psi / lb
const USI OIL
Fluid type = oil.
const OCP_DBL CONV3
1 lb = CONV3 kg
const OCP_DBL GAS_CONSTANT
Gas Constant.
const OCP_DBL CONV4
1 ft3 = CONV4 m3
#define OCP_WARNING(msg)
Log warning messages.
#define OCP_ABORT(msg)
Abort if critical error happens.
OCP_DBL MW
Molecular Weight.
OCP_DBL OmegaA
Param A of Components.
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.
EoSParam contains the params for Compositional Model and functions to read them.
Type_A_r< vector< OCP_DBL > > Zcvis
Critical Z-factor used for viscosity calculations only.
Type_A_r< vector< OCP_DBL > > Vcvis
Critical volume used for viscosity calculations only.
vector< string > RRparam
Params for Solving Rachford-Rice equations.
Type_A_r< vector< OCP_DBL > > Vshift
Volume shift of hydrocarbon components.
vector< string > SSMparamSP
Params for Solving Phase Spliting with SSM.
USI numCom
num of components, water is excluded.
vector< OCP_DBL > LBCcoef
LBC coefficients for viscosity calculation.
Type_A_r< vector< OCP_DBL > > MW
Molecular Weight of hydrocarbon components.
vector< string > NRparamSTA
Params for Solving Phase Spliting with NR.
Type_A_r< vector< OCP_DBL > > Acf
Acentric factor of hydrocarbon components.
vector< string > SSMparamSTA
Params for Solving Phase Spliting with SSM.
vector< string > NRparamSP
Params for Solving Phase Spliting with NR.
vector< string > Cname
Name of hydrocarbon components.
Type_A_r< vector< OCP_DBL > > OmegaB
OMEGA_B of hydrocarbon components.
USI numPhase
num of phase, water is excluded, constant now.
Type_A_r< vector< OCP_DBL > > Pc
Critical pressure of hydrocarbon components.
Type_A_r< vector< OCP_DBL > > Vc
Critical volume of hydrocarbon components.
Type_A_r< vector< OCP_DBL > > OmegaA
OMEGA_A of hydrocarbon components.
vector< vector< OCP_DBL > > BIC
Binary interaction.
Type_A_r< vector< OCP_DBL > > Tc
Critical temperature of hydrocarbon components.
Type_A_r< vector< OCP_DBL > > Parachor
PARACHOR of hydrocarbon components.
Type_A_r< vector< OCP_DBL > > Zc
Critical Z-factor of hydrocarbon components.
bool miscible
Miscible treatment of hydrocarbons, used in compositional Model.
void CopyPhase()
Copy the basic properties from MixtureComp to Mixture.
void RachfordRice2P()
Used when NP = 2, improved RachfordRice2.
bool StableSSM01(const USI &Id)
relaxable SSM
USI CubicRoot(const OCP_DBL &a, const OCP_DBL &b, const OCP_DBL &c, const bool &NTflag=false) const
Result is stored in Ztmp.
void FlashDeriv(const OCP_DBL &Pin, const OCP_DBL &Tin, const OCP_DBL *Niin, const USI &ftype, const USI &lastNP, const OCP_DBL *lastKs) override
Flash calculation with moles of components and Calculate the derivative.
void RachfordRice3()
Used when NP > 2.
void CalPhiNSTA()
Calculate d ln phi[i][j] / d n[k][j].
void SplitBFGS()
Use BFGS to calculate phase splitting.
void CalFugXSTA()
Calculate d ln(Fug) / dx for Y.
OCP_DBL GammaPhaseW(const OCP_DBL &Pin) override
return gamma of water phase, gamma equals to mass density times gravity factor.
void InitFlash(const OCP_DBL &Pin, const OCP_DBL &Pbbin, const OCP_DBL &Tin, const OCP_DBL *Sjin, const OCP_DBL &Vpore, const OCP_DBL *Ziin) override
flash calculation with saturation of phases.
void Flash(const OCP_DBL &Pin, const OCP_DBL &Tin, const OCP_DBL *Niin, const USI &ftype, const USI &lastNP, const OCP_DBL *lastKs) override
Flash calculation with moles of components.
void UpdateXRR()
Update X according to RR.
OCP_DBL GammaPhaseOG(const OCP_DBL &Pin, const OCP_DBL &Tin, const OCP_DBL *Ziin) override
void RachfordRice2()
Used when NP = 2.
OCP_DBL XiPhase(const OCP_DBL &Pin, const OCP_DBL &Tin, const OCP_DBL *Ziin) override
OCP_DBL RhoPhase(const OCP_DBL &Pin, const OCP_DBL &Tin, const OCP_DBL *Ziin) override
return mass density of phase.
bool StableSSM(const USI &Id)
strict SSM
void FlashDeriv_n(const OCP_DBL &Pin, const OCP_DBL &Tin, const OCP_DBL *Niin, const OCP_DBL *Sjin, const OCP_DBL *xijin, const OCP_DBL *njin, const USI &ftype, const USI *phaseExistin, const USI &lastNP, const OCP_DBL *lastKs) override
void x2n()
x[j][i] -> n[j][i]
void SplitNR()
Use NR to calculate phase splitting.
OCP_DBL resPc
a precalculated value
vector< OCP_DBL > rhoP
d rho / dP: numphase
vector< USI > pEnumCom
see pEnumCom in bulk
USI numCom
num of components.
vector< OCP_DBL > dXsdXp
the derivates of second variables wrt. primary variables
vector< OCP_DBL > rho
mass density of phase: numPhase
vector< OCP_DBL > mu
viscosity of phase: numPhase
vector< bool > phaseExist
existence of phase: numPhase
vector< OCP_DBL > vjp
dvj / dp, used in 2 hydrocarbon phase in EOS
USI numPhase
num of phases.
vector< OCP_DBL > xiN
d xi[j] / d N[i]: numphase * numCom
vector< vector< OCP_DBL > > vji
dvj / dNi, used in 2 hydrocarbon phase in EOS; or dvj / dnij
vector< OCP_DBL > xix
d xi[j] / d x[i][j]: numphase * numCom
vector< OCP_DBL > rhox
d rho[j] / d x[i][j]: numphase * numCom
vector< OCP_DBL > v
volume of phase: numPhase;
vector< OCP_DBL > S
saturation of phase: numPhase
vector< OCP_DBL > muN
d mu[j] / d N[i]: numphase * numCom
OCP_DBL Nt
Total moles of Components.
vector< OCP_DBL > nj
mole number of phase j
vector< OCP_DBL > Ni
moles of component: numCom
vector< OCP_DBL > keyDer
d (xij*xi/mu) / dP or dNk
vector< OCP_DBL > xiP
d xi / dP: numphase
OCP_DBL vf
volume of total fluids.
vector< OCP_DBL > rhoN
d rho[j] / d N[i]: numphase * numCom
vector< OCP_DBL > muP
d mu / dP: numPhase
vector< OCP_DBL > mux
d mu[j] / d x[i][j]: numphase * numCom
vector< OCP_DBL > res
residual of a set of equations
void Allocate()
Allocate memory for common variables for basic class.
vector< OCP_DBL > xi
molar density of phase: numPhase
bool conflag
convergence flag, if converges, conflag = true
bool conflag
convergence flag, if converges, conflag = true
USI Eval_All(const USI &j, const OCP_DBL &val, vector< OCP_DBL > &outdata, vector< OCP_DBL > &slope)
bool conflag
convergence flag, if converges, conflag = true
USI curIt
current Iterations
OCP_DBL eYt
if Yt > 1 + eYt, then single phase is unstable
OCP_DBL Ktol
tolerace^2 for K
bool conflag
convergence flag, if converges, conflag = true
vector< T > data
Data of param.
bool activity
If false, this param is not given.