14 COMP::COMP(
const vector<string>& comp)
67 for (
USI i = 0; i < NC; i++) {
68 Vc[i] = 10.73159 * Zc[i] * Tc[i] / Pc[i];
71 OCP_ABORT(
"VCRIT or ZCRIT hasn't been input!");
74 MWC = param.
MW.
data[tarId];
84 OmegaA.resize(NC, 0.457235529);
88 OmegaB.resize(NC, 0.077796074);
92 for (
USI i = 0; i < NC; i++)
102 for (
USI i = 0; i < NC; i++) {
109 for (
auto& lbc : LBCcoef) {
113 InputMiscibleParam(param, tarId);
120 if (param.
BIC[tarId].size() != len) {
122 for (
USI i = 1; i < NC; i++) {
123 for (
USI j = 0; j < i; j++) {
124 BIC[i * NC + j] = param.
BIC[tarId][iter];
125 BIC[j * NC + i] = BIC[i * NC + j];
130 BIC = param.
BIC[tarId];
133 for (
USI i = 0; i < NC; i++) {
134 for (
USI j = 0; j < NC; j++) {
135 cout << setw(10) << BIC[i * NC + j] <<
" ";
143 EoSctrl.SSMsta.
tol2 = EoSctrl.SSMsta.
tol * EoSctrl.SSMsta.
tol;
147 EoSctrl.NRsta.
tol2 = EoSctrl.NRsta.
tol * EoSctrl.NRsta.
tol;
151 EoSctrl.SSMsp.
tol2 = EoSctrl.SSMsp.
tol * EoSctrl.SSMsp.
tol;
155 EoSctrl.NRsp.
tol2 = EoSctrl.NRsp.
tol * EoSctrl.NRsp.
tol;
159 EoSctrl.RR.
tol2 = EoSctrl.RR.
tol * EoSctrl.RR.
tol;
171 InitPTN(Pin, Tin +
CONV5, Niin);
184 OCP_DBL bw = bw0 * (1 - cbw * (P - Pw0));
187 rho[Wpid] = std_RhoW / bw;
195 void MixtureComp::InitFlashIMPEC(
const OCP_DBL& Pin,
208 InitPTZ(Pin, Tin +
CONV5, Ziin);
220 Nh = Vpore * (1 - Sw) /
vf;
230 for (
USI i = 0; i < NC; i++) {
243 OCP_DBL bw = bw0 * (1 - cbw * (P - Pw0));
247 rho[Wpid] = std_RhoW / bw;
248 Ni[Wcid] = Vpore * Sw *
xi[Wpid];
261 void MixtureComp::InitFlashFIM(
const OCP_DBL& Pin,
274 InitPTZ(Pin, Tin +
CONV5, Ziin);
286 Nh = Vpore * (1 - Sw) /
vf;
296 for (
USI i = 0; i < NC; i++) {
309 OCP_DBL bw = bw0 * (1 - cbw * (P - Pw0));
313 rho[Wpid] = std_RhoW / bw;
314 muP[Wpid] = cdata[3];
315 xiP[Wpid] = -bwp / (bw * bw *
CONV1);
317 Ni[Wcid] = Vpore * Sw *
xi[Wpid];
355 void MixtureComp::InitFlashFIMn(
const OCP_DBL& Pin,
368 InitPTZ(Pin, Tin +
CONV5, Ziin);
380 Nh = Vpore * (1 - Sw) /
vf;
388 for (
USI i = 0; i < NC; i++) {
392 CalVjpVfpVfn_partial();
402 OCP_DBL bw = bw0 * (1 - cbw * (P - Pw0));
406 rho[Wpid] = std_RhoW / bw;
407 muP[Wpid] = cdata[3];
408 xiP[Wpid] = -bwp / (bw * bw *
CONV1);
410 Ni[Wcid] = Vpore * Sw *
xi[Wpid];
453 lNP = lastNP > 0 ? lastNP - 1 : 0;
455 for (
USI i = 0; i < NC; i++) {
456 lKs[i] = xijin[i] / xijin[i +
numCom];
460 InitPTN(Pin, Tin +
CONV5, Niin);
478 OCP_DBL bw = bw0 * (1 - cbw * (P - Pw0));
482 rho[Wpid] = std_RhoW / bw;
506 lNP = lastNP > 0 ? lastNP - 1 : 0;
508 for (
USI i = 0; i < NC; i++) {
509 lKs[i] = xijin[i] / xijin[i +
numCom];
513 InitPTN(Pin, Tin +
CONV5, Niin);
532 OCP_DBL bw = bw0 * (1 - cbw * (P - Pw0));
536 rho[Wpid] = std_RhoW / bw;
537 muP[Wpid] = cdata[3];
538 xiP[Wpid] = -bwp / (bw * bw *
CONV1);
581 const USI* phaseExistin,
588 if (phaseExistin[j] == 1) inputNP++;
592 if (inputNP == 1 || OCP_TRUE) {
594 lNP = lastNP > 0 ? lastNP - 1 : 0;
596 for (
USI i = 0; i < NC; i++) {
597 lKs[i] = xijin[i] / xijin[i +
numCom];
600 InitPTN(Pin, Tin +
CONV5, Niin);
606 InitPTN(Pin, Tin +
CONV5, Niin);
608 for (
USI j = 0; j < NP; j++) {
627 CalVjpVfpVfn_partial();
640 OCP_DBL bw = bw0 * (1 - cbw * (P - Pw0));
644 rho[Wpid] = std_RhoW / bw;
645 muP[Wpid] = cdata[3];
646 xiP[Wpid] = -bwp / (bw * bw *
CONV1);
659 if (inputNP == NP && NP == 2 && OCP_FALSE) {
660 cout << scientific << setprecision(3);
661 cout <<
"--------- " << NP <<
" --------- " << inputNP << endl;
662 for (
USI j = 0; j < NP; j++) {
663 USI j1 = phaseLabel[j];
664 cout << setw(10) <<
S[j1] - Sjin[j1] <<
" " << setw(10)
665 << (
nj[j1] - njin[j1]) / njin[j1] <<
" ";
666 for (
USI i = 0; i < NC; i++) {
672 cout <<
S[2] - Sjin[2] << endl;
697 void MixtureComp::CalFlash()
717 if (tarPhase ==
WATER) {
723 OCP_DBL bw = bw0 * (1 - cbw * (Pin - Pw0));
728 InitPTZ(Pin, Tin +
CONV5, Ziin);
731 CalAjBj(Aj[0], Bj[0], zi);
732 SolEoS(Zj[0], Aj[0], Bj[0]);
734 for (
USI i = 0; i < NC; i++) {
735 vtmp -= zi[i] * Vshift[i];
751 if (tarPhase ==
WATER) {
757 OCP_DBL bw = (bw0 * (1 - cbw * (Pin - Pw0)));
759 return std_RhoW / bw;
766 return MW[0] * xitmp;
771 const vector<SolventINJ>& sols,
775 const USI wellType = opt.WellType();
776 if (wellType ==
INJ) {
777 const string fluidName = opt.InjFluidType();
778 vector<OCP_DBL> tmpZi(
numCom, 0);
779 if (fluidName ==
"WAT") {
781 opt.SetInjProdPhase(
WATER);
784 opt.SetInjProdPhase(
GAS);
785 const USI len = sols.size();
786 for (
USI i = 0; i < len; i++) {
787 if (fluidName == sols[i].name) {
788 tmpZi = sols[i].data;
793 opt.SetInjFactor(tmp);
802 }
else if (wellType ==
PROD) {
803 vector<OCP_DBL> tmpWght(
numPhase, 0);
804 switch (opt.OptMode()) {
824 opt.SetProdPhaseWeight(tmpWght);
833 const vector<OCP_DBL>& prodPhase,
834 vector<OCP_DBL>& prodWeight)
836 Flash(Pin, Tin, Niin);
839 vector<OCP_DBL> factor(
numPhase, 0);
841 factor[0] =
vj[0] / qt /
CONV1;
842 factor[1] =
vj[1] / qt / 1000;
843 factor[2] =
xi[2] *
vj[2] / qt;
846 for (
USI i = 0; i < 3; i++) {
847 tmp += factor[i] * prodPhase[i];
849 if (tmp < 1E-12 || !isfinite(tmp)) {
852 fill(prodWeight.begin(), prodWeight.end(), tmp);
858 vector<OCP_DBL>& prodRate)
860 Flash(Pin, Tin, Niin);
863 prodRate[1] =
vj[1] / 1000;
864 prodRate[2] =
vj[2] *
xi[2];
867 void MixtureComp::CallId()
870 for (
USI i = 1; i < NC; i++) {
871 if (MWC[i] < MWC[lId]) lId = i;
875 void MixtureComp::AllocateEoS()
884 delta1P2 = delta1 + delta2;
885 delta1M2 = delta1 - delta2;
886 delta1T2 = delta1 * delta2;
894 const OCP_DBL a = (delta1 + delta2 - 1) * bj - 1;
896 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1));
897 const OCP_DBL c = -(aj * bj + delta1 * delta2 * bj * bj * (bj + 1));
905 OCP_DBL dG = (zj2 - zj1) + log((zj1 - bj) / (zj2 - bj)) -
906 aj / (bj * (delta2 - delta1)) *
907 log((zj1 + delta1 * bj) * (zj2 + delta2 * bj) /
908 ((zj1 + delta2 * bj) * (zj2 + delta1 * bj)));
916 void MixtureComp::CalAiBi()
922 for (
USI i = 0; i < NC; i++) {
926 mwi = 0.37464 + 1.54226 * acf - 0.26992 * pow(acf, 2);
928 mwi = 0.379642 + 1.48503 * acf - 0.164423 * pow(acf, 2) +
929 0.016667 * pow(acf, 3);
934 Ai[i] = OmegaA[i] * Pri / pow(Tri, 2) * pow((1 + mwi * (1 - sqrt(Tri))), 2);
935 Bi[i] = OmegaB[i] * Pri / Tri;
939 void MixtureComp::CalAjBj(
OCP_DBL& AjT,
OCP_DBL& BjT,
const vector<OCP_DBL>& xj)
const
944 for (
USI i1 = 0; i1 < NC; i1++) {
945 BjT += Bi[i1] * xj[i1];
946 AjT += xj[i1] * xj[i1] * Ai[i1] * (1 - BIC[i1 * NC + i1]);
948 for (
USI i2 = 0; i2 < i1; i2++) {
950 2 * xj[i1] * xj[i2] * sqrt(Ai[i1] * Ai[i2]) * (1 - BIC[i1 * NC + i2]);
960 for (
USI i1 = 0; i1 < NC; i1++) {
961 BjT += Bi[i1] * xj[i1];
962 AjT += xj[i1] * xj[i1] * Ai[i1] * (1 - BIC[i1 * NC + i1]);
964 for (
USI i2 = 0; i2 < i1; i2++) {
966 2 * xj[i1] * xj[i2] * sqrt(Ai[i1] * Ai[i2]) * (1 - BIC[i1 * NC + i2]);
971 void MixtureComp::AllocatePhase()
979 phaseLabel.resize(NPmax);
984 for (
USI j = 0; j < NPmax; j++) {
993 void MixtureComp::CalFugPhi(vector<OCP_DBL>& phiT,
994 vector<OCP_DBL>& fugT,
995 const vector<OCP_DBL>& xj)
1006 for (
USI i = 0; i < NC; i++) {
1008 for (
USI k = 0; k < NC; k++) {
1009 tmp += 2 * (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]) * xj[k];
1011 phiT[i] = exp(Bi[i] / bj * (zj - 1) - log(zj - bj) -
1012 aj / (m1 - m2) / bj * (tmp / aj - Bi[i] / bj) *
1013 log((zj + m1 * bj) / (zj + m2 * bj)));
1014 fugT[i] = phiT[i] * xj[i] * P;
1038 CalAjBj(aj, bj, xj);
1043 const OCP_DBL m1Mm2 = delta1M2;
1049 for (
USI i = 0; i < NC; i++) {
1051 for (
USI k = 0; k < NC; k++) {
1052 tmp += 2 * (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]) * xj[k];
1054 phiT[i] = exp(Bi[i] / bj * (zj - 1) - log(zj - bj) -
1055 aj / (m1Mm2) / bj * (tmp / aj - Bi[i] / bj) *
1056 log((zj + m1 * bj) / (zj + m2 * bj)));
1057 fugT[i] = phiT[i] * xj[i] * P;
1076 CalAjBj(aj, bj, xj);
1081 const OCP_DBL m1Mm2 = delta1M2;
1087 for (
USI i = 0; i < NC; i++) {
1089 for (
USI k = 0; k < NC; k++) {
1090 tmp += 2 * (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]) * xj[k];
1092 tmp = exp(Bi[i] / bj * (zj - 1) - log(zj - bj) -
1093 aj / (m1Mm2) / bj * (tmp / aj - Bi[i] / bj) *
1094 log((zj + m1 * bj) / (zj + m2 * bj)));
1095 fugT[i] = tmp * xj[i] * P;
1103 void MixtureComp::CalFugPhiAll()
1108 const OCP_DBL m1Mm2 = delta1M2;
1110 for (
USI j = 0; j < NP; j++) {
1111 const vector<OCP_DBL>& xj = x[j];
1112 vector<OCP_DBL>& phiT = phi[j];
1113 vector<OCP_DBL>& fugT = fug[j];
1118 CalAjBj(aj, bj, xj);
1121 for (
USI i = 0; i < NC; i++) {
1123 for (
USI k = 0; k < NC; k++) {
1124 tmp += 2 * (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]) * xj[k];
1126 phiT[i] = exp(Bi[i] / bj * (zj - 1) - log(zj - bj) -
1127 aj / (m1Mm2) / bj * (tmp / aj - Bi[i] / bj) *
1128 log((zj + m1 * bj) / (zj + m2 * bj)));
1129 fugT[i] = phiT[i] * xj[i] * P;
1147 void MixtureComp::CalMW()
1150 for (
USI j = 0; j < NP; j++) {
1152 for (
USI i = 0; i < NC; i++) {
1153 MW[j] += x[j][i] * MWC[i];
1158 void MixtureComp::CalVfXiRho()
1163 for (
USI j = 0; j < NP; j++) {
1165 vector<OCP_DBL>& xj = x[j];
1167 for (
USI i = 0; i < NC; i++) {
1168 tmp -= xj[i] * Vshift[i];
1171 vC[j] = tmp * nu[j];
1174 rhoC[j] = MW[j] * xiC[j];
1178 void MixtureComp::CalSaturation()
1188 USI MixtureComp::FindMWmax()
1193 for (
USI j = 1; j < NP; j++) {
1194 if (tmpMW < MW[j]) {
1205 for (
USI j = 0; j < NP; j++) {
1208 for (
USI i = 0; i < NC; i++) {
1209 n[j][i] = nu[j] * x[j][i];
1214 void MixtureComp::PrintX()
1216 for (
USI j = 0; j < NP; j++) {
1217 for (
USI i = 0; i < NC; i++) {
1218 cout << x[j][i] <<
" ";
1222 cout <<
"----------------------" << endl;
1225 void MixtureComp::AllocateMethod()
1229 for (
USI i = 0; i < 4; i++) {
1232 Ks.resize(NPmax - 1);
1233 for (
USI i = 0; i < NPmax - 1; i++) {
1243 JmatSTA.resize(NC * NC);
1249 resRR.resize(NPmax - 1);
1250 resSP.resize(
static_cast<size_t>(NC) * NPmax);
1251 JmatSP.resize(
static_cast<size_t>(NC * NC) * NPmax * NPmax);
1255 for (
USI j = 0; j < NPmax; j++) {
1256 fugX[j].resize(NC * NC);
1257 fugN[j].resize(NC * NC);
1262 pivot.resize(
static_cast<size_t>(NC + 1) * NPmax, 1);
1263 lJmatWork = NC * (NPmax - 1);
1264 JmatWork.resize(lJmatWork);
1266 pivot.resize(NPmax *
static_cast<size_t>(NC) +
numPhase, 1);
1269 void MixtureComp::CalKwilson()
1271 for (
USI i = 0; i < NC; i++) {
1272 Kw[0][i] = (Pc[i] / P) * exp(5.373 * (1 + Acf[i]) * (1 - Tc[i] / T));
1273 Kw[1][i] = 1 / Kw[0][i];
1274 Kw[2][i] = pow(Kw[0][i], 1.0 / 3);
1275 Kw[3][i] = pow(Kw[1][i], 1.0 / 3);
1279 void MixtureComp::PhaseEquilibrium()
1290 CalAjBj(Aj[0], Bj[0], x[0]);
1291 SolEoS(Zj[0], Aj[0], Bj[0]);
1293 while (!PhaseStable()) {
1296 if (NP == NPmax || NP == 1)
break;
1305 else if (EoSctrl.SSMsta.
conflag)
1306 ePEC = EoSctrl.SSMsta.
realTol;
1312 else if (EoSctrl.SSMsp.
conflag)
1326 CalAjBj(Aj[0], Bj[0], x[0]);
1327 SolEoS(Zj[0], Aj[0], Bj[0]);
1343 else if (EoSctrl.SSMsp.
conflag)
1355 OCP_BOOL MixtureComp::PhaseStable()
1361 testPId = FindMWmax();
1364 EoSctrl.SSMsta.
conflag = OCP_FALSE;
1365 EoSctrl.SSMsta.
curIt = 0;
1366 EoSctrl.NRsta.
conflag = OCP_FALSE;
1367 EoSctrl.NRsta.
curIt = 0;
1385 itersSSMSTA += EoSctrl.SSMsta.
curIt;
1386 itersNRSTA += EoSctrl.NRsta.
curIt;
1401 OCP_DBL dYtol = EoSctrl.SSMsta.dYtol;
1408 const vector<OCP_DBL>& xj = x[Id];
1409 CalFugPhi(phi[Id], fug[Id], xj);
1410 const vector<OCP_DBL>& fugId = fug[Id];
1412 vector<OCP_DBL>& ks = Ks[0];
1413 for (k = 0; k < 2; k++) {
1421 for (
USI i = 0; i < NC; i++) {
1422 Y[i] = xj[i] * ks[i];
1429 for (
USI i = 0; i < NC; i++) {
1430 dY += pow((Y[i] - di[i]), 2);
1439 CalFugPhi(&fugSta[0], &Y[0]);
1442 for (
USI i = 0; i < NC; i++) {
1443 di[i] = fugId[i] / (fugSta[i] * Yt);
1445 Se += pow(di[i] - 1, 2);
1447 Sk += pow(log(ks[i]), 2);
1476 EoSctrl.SSMsta.
curIt += iter;
1478 if (flag && Yt > 1 - 0.1 && Sk > 1) {
1482 if (flag && Yt > 1 + eYt) {
1483 EoSctrl.SSMsta.
conflag = OCP_TRUE;
1484 EoSctrl.SSMsta.
realTol = sqrt(Se);
1488 EoSctrl.SSMsta.
realTol = sqrt(Se);
1497 const vector<OCP_DBL>& xj = x[Id];
1498 CalFugPhi(phi[Id], fug[Id], xj);
1499 const vector<OCP_DBL>& fugId = fug[Id];
1501 for (
USI i = 0; i < NC; i++) {
1502 di[i] = phi[Id][i] * xj[i];
1515 for (k = 0; k < 4; k++) {
1518 for (
USI i = 0; i < NC; i++) {
1519 Y[i] = xj[i] / Kw[k][i];
1524 CalFugPhi(&phiSta[0], &fugSta[0], &Y[0]);
1527 for (
USI i = 0; i < NC; i++) {
1528 Se += pow(log(fugSta[i] / fugId[i] * Yt), 2);
1546 for (
USI i = 0; i < NC; i++) {
1547 Y[i] = di[i] / phiSta[i];
1552 CalFugPhi(&phiSta[0], &fugSta[0], &Y[0]);
1554 for (
USI i = 0; i < NC; i++) {
1555 Se += pow(log(fugSta[i] / fugId[i] * Yt), 2);
1564 EoSctrl.SSMsta.
curIt += iter;
1565 flag = StableNR(Id);
1570 if (flag && Yt > 1 + eYt) {
1575 EoSctrl.SSMsta.
conflag = OCP_TRUE;
1576 EoSctrl.SSMsta.
realTol = sqrt(Se);
1586 EoSctrl.SSMsta.
realTol = sqrt(Se);
1593 for (
USI i = 0; i < NC; i++) {
1594 resSTA[i] = log(fug[Id][i] / (fugSta[i] * Yt));
1609 SYSSolve(1, &uplo, NC, &JmatSTA[0], &resSTA[0], &pivot[0], &JmatWork[0],
1612 Daxpy(NC, alpha, &resSTA[0], &Y[0]);
1614 for (
USI i = 0; i < NC; i++) {
1619 CalFugPhi(&fugSta[0], &Y[0]);
1620 for (
USI i = 0; i < NC; i++) {
1621 resSTA[i] = log(fug[Id][i] / (fugSta[i] * Yt));
1623 Se =
Dnorm2(NC, &resSTA[0]);
1626 EoSctrl.NRsta.
curIt += iter;
1627 EoSctrl.NRsta.
conflag = OCP_FALSE;
1632 EoSctrl.NRsta.
curIt += iter;
1633 EoSctrl.NRsta.
conflag = OCP_TRUE;
1642 vector<OCP_DBL>& fugx = fugX[0];
1650 const OCP_DBL m1Pm2 = delta1P2;
1651 const OCP_DBL m1Mm2 = delta1M2;
1652 const OCP_DBL m1Tm2 = delta1T2;
1655 for (
USI i = 0; i < NC; i++) {
1657 for (
USI k = 0; k < NC; k++) {
1658 tmp += Y[k] * (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]);
1661 Zx[i] = ((bj - zj) * Ax[i] + ((aj + m1Tm2 * (3 * bj * bj + 2 * bj)) +
1662 ((m1Pm2) * (2 * bj + 1) - 2 * m1Tm2 * bj) * zj -
1663 (m1Pm2 - 1) * zj * zj) *
1665 (3 * zj * zj + 2 * ((m1Pm2 - 1) * bj - 1) * zj +
1666 (aj + m1Tm2 * bj * bj - (m1Pm2)*bj * (bj + 1)));
1672 G = (zj + m1 * bj) / (zj + m2 * bj);
1674 for (
USI i = 0; i < NC; i++) {
1676 C = Y[i] * P / (zj - bj);
1679 E = -aj / ((m1Mm2)*bj) * (Ax[i] / aj - Bx[i] / bj);
1681 for (
USI k = 0; k < NC; k++) {
1682 aik = (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]);
1685 Cxk = ((zj - bj) * delta(i, k) - Y[i] * (Zx[k] - Bx[k])) * P /
1686 ((zj - bj) * (zj - bj));
1687 Dxk = Bx[i] / bj * (Zx[k] - Bx[k] * (zj - 1) / bj);
1691 Exk = (2 * (aj / bj * Bx[k] * Bx[i] + bj * aik) - Ax[i] * Bx[k] -
1695 Gxk = (m1Mm2) / (zj + m2 * bj) / (zj + m2 * bj) * (zj * Bx[k] - Zx[k] * bj);
1696 fugx[i * NC + k] = 1 / C * Cxk + Dxk + Exk * log(G) + E / G * Gxk;
1708 for (
USI j = 0; j < NP; j++) {
1709 if (!
CheckNan(fugX[j].size(), &fugX[j][0])) {
1716 void MixtureComp::AssembleJmatSTA()
1718 vector<OCP_DBL>& fugx = fugX[0];
1719 fill(JmatSTA.begin(), JmatSTA.end(), 0.0);
1721 for (
USI i = 0; i < NC; i++) {
1724 for (
USI k = 0; k < NC; k++) {
1725 tmp += Y[k] * fugx[i * NC + k];
1728 for (
USI j = 0; j < NC; j++) {
1731 JmatSTA[i * NC + j] = (fugx[i * NC + j] - tmp + 1) / Yt;
1743 void MixtureComp::PhaseSplit()
1745 EoSctrl.SSMsp.
conflag = OCP_FALSE;
1746 EoSctrl.SSMsp.
curIt = 0;
1747 EoSctrl.NRsp.
conflag = OCP_FALSE;
1748 EoSctrl.NRsp.
curIt = 0;
1749 EoSctrl.RR.
curIt = 0;
1752 SplitSSM(OCP_FALSE);
1754 while (!EoSctrl.NRsp.
conflag) {
1757 if (!CheckSplit())
break;
1758 if (EoSctrl.SSMsp.
conflag)
break;
1762 itersSSMSP += EoSctrl.SSMsp.
curIt;
1763 itersNRSP += EoSctrl.NRsp.
curIt;
1764 itersRR += EoSctrl.RR.
curIt;
1789 OCP_DBL nuMax = max(nu[0], nu[1]);
1791 for (
USI i = 0; i < NC; i++) {
1792 eX += (x[0][i] - x[1][i]) * (x[0][i] - x[1][i]);
1798 CalFugPhi(phiSta, fugSta, zi);
1801 for (
USI i = 0; i < NC; i++) {
1802 GibbsEnergyB += zi[i] * log(fugSta[i]);
1803 GibbsEnergyE += (n[0][i] * log(fug[0][i]) + n[1][i] * log(fug[1][i]));
1806 cout << scientific << setprecision(6);
1808 if (GibbsEnergyE > GibbsEnergyB) {
1809 cout <<
ftype <<
" ";
1810 cout << GibbsEnergyB <<
" ";
1811 cout << GibbsEnergyE <<
" ";
1812 cout << nuMax <<
" ";
1814 cout << EoSctrl.NRsp.
conflag <<
" ";
1819 if (nuMax < 1 && EoSctrl.NRsp.
conflag && isfinite(eX)) {
1822 if (!isfinite(eX) || 1 - nuMax < 1E-3) {
1826 CalAjBj(Aj[0], Bj[0], x[0]);
1827 SolEoS(Zj[0], Aj[0], Bj[0]);
1829 EoSctrl.SSMsta.
conflag = OCP_FALSE;
1830 EoSctrl.NRsta.
conflag = OCP_FALSE;
1838 void MixtureComp::SplitSSM(
const OCP_BOOL& flag)
1847 void MixtureComp::SplitSSM2(
const OCP_BOOL& flag)
1853 EoSctrl.SSMsp.
conflag = OCP_TRUE;
1862 if (Yt < 1.1 || OCP_TRUE) {
1865 for (
USI i = 0; i < NC; i++) {
1867 Ks[NP - 2][i] = Y[i] / x[testPId][i];
1888 for (
USI i = 0; i < NC; i++) {
1889 Se += pow(fug[1][i] / fug[0][i] - 1, 2);
1890 Ks[0][i] = phi[1][i] / phi[0][i];
1896 EoSctrl.SSMsp.
conflag = OCP_FALSE;
1901 EoSctrl.SSMsp.
realTol = sqrt(Se);
1902 EoSctrl.SSMsp.
curIt += iter;
1905 void MixtureComp::SplitSSM3(
const OCP_BOOL& flag) {}
1909 const vector<OCP_DBL>& Ktmp = Ks[0];
1913 for (
USI i = 1; i < NC; i++) {
1914 if (Ktmp[i] < Kmin) Kmin = Ktmp[i];
1915 if (Ktmp[i] > Kmax) Kmax = Ktmp[i];
1918 const OCP_DBL numin = 1 / (1 - Kmax);
1919 const OCP_DBL numax = 1 / (1 - Kmin);
1921 nu[0] = 0.5 * (numin + numax);
1924 OCP_DBL tmp, rj, J, dnuj, tmpnu;
1933 for (
USI i = 0; i < NC; i++) {
1934 tmp = 1 + nu[0] * (Ktmp[i] - 1);
1935 rj += zi[i] * (Ktmp[i] - 1) / tmp;
1936 J -= zi[i] * (Ktmp[i] - 1) * (Ktmp[i] - 1) / (tmp * tmp);
1939 if (fabs(rj) < tol || iter > maxIt)
break;
1942 tmpnu = nu[0] + dnuj;
1943 if (tmpnu < numax && tmpnu > numin) {
1947 nu[0] = (nu[0] + numax) / 2;
1949 nu[0] = (nu[0] + numin) / 2;
1955 EoSctrl.RR.
curIt += iter;
1966 const vector<OCP_DBL>& Ktmp = Ks[0];
1970 for (
USI i = 1; i < NC; i++) {
1971 if (Ktmp[i] < Kmin) Kmin = Ktmp[i];
1972 if (Ktmp[i] > Kmax) Kmax = Ktmp[i];
1975 const OCP_DBL numin = 1 / (1 - Kmax);
1976 const OCP_DBL numax = 1 / (1 - Kmin);
1978 nu[0] = 0.5 * (numin + numax);
1981 OCP_DBL tmp, rj, J, dnuj, tmpnu;
1991 for (
USI i = 0; i < NC; i++) {
1992 tmp = 1 + nu[0] * (Ktmp[i] - 1);
1993 rj += zi[i] * (Ktmp[i] - 1) / tmp;
1994 J -= zi[i] * (Ktmp[i] - 1) * (Ktmp[i] - 1) / (tmp * tmp);
1996 f = (nu[0] - numin) * (numax - nu[0]);
1997 df = -2 * nu[0] + (numax + numin);
2002 if (fabs(rj) < tol || iter > maxIt)
break;
2005 tmpnu = nu[0] + dnuj;
2006 if (tmpnu < numax && tmpnu > numin) {
2010 nu[0] = (nu[0] + numax) / 2;
2012 nu[0] = (nu[0] + numin) / 2;
2018 EoSctrl.RR.
curIt += iter;
2020 cout << iter <<
" " << scientific << setprecision(3) << fabs(rj) <<
" "
2021 << nu[0] <<
" " << numin <<
" " << numax << endl;
2033 for (
USI i = 0; i < NC; i++) {
2035 for (
USI j = 0; j < NP - 1; j++) {
2036 tmp += nu[j] * (Ks[j][i] - 1);
2038 x[NP - 1][i] = zi[i] / tmp;
2039 for (
USI j = 0; j < NP - 1; j++) {
2040 x[j][i] = Ks[j][i] * x[NP - 1][i];
2059 EoSctrl.NRsp.
conflag = OCP_FALSE;
2064 USI len = NC * (NP - 1);
2075 while (eNR > NRtol) {
2085 int info =
SYSSolve(1, &uplo, len, &JmatSP[0], &resSP[0], &pivot[0],
2086 &JmatWork[0], lJmatWork);
2087 if (info > 0 &&
false) {
2088 for (
USI i = 0; i < len; i++) {
2089 for (
USI j = 0; j < len; j++) {
2090 cout << scientific << setprecision(9) << JmatSP[i * len + j]
2098 alpha = CalStepNRsp();
2101 for (
USI j = 0; j < NP - 1; j++) {
2102 Daxpy(NC, alpha, &resSP[j * NC], &n[j][0]);
2103 Daxpy(NC, -1, &n[j][0], &n[NP - 1][0]);
2107 for (
USI i = 0; i < NC; i++) {
2111 for (
USI i = 0; i < NC; i++) {
2113 x[j][i] = fabs(n[j][i] / nu[j]);
2125 for (
USI i = 0; i < NC; i++) {
2126 nu[NP - 1] += n[NP - 1][i];
2128 for (
USI i = 0; i < NC; i++) {
2129 x[NP - 1][i] = fabs(n[NP - 1][i] / nu[NP - 1]);
2134 eNR =
Dnorm2(len, &resSP[0]);
2136 if (eNR > eNR0 || iter > EoSctrl.NRsp.
maxIt) {
2142 for (
USI j = 0; j < NP; j++) {
2143 Daxpy(NC, -1, &n[j][0], &ln[j][0]);
2144 en +=
Dnorm2(NC, &ln[j][0]);
2146 if (en / (NP * NC) < 1E-8) {
2147 EoSctrl.NRsp.
conflag = OCP_TRUE;
2152 if (eNR < NRtol) EoSctrl.NRsp.
conflag = OCP_TRUE;
2153 EoSctrl.NRsp.
curIt += iter;
2158 void MixtureComp::CalResSP()
2161 for (
USI j = 0; j < NP - 1; j++) {
2162 for (
USI i = 0; i < NC; i++) {
2163 resSP[j * NC + i] = log(fug[NP - 1][i] / fug[j][i]);
2168 void MixtureComp::CalFugNAll(
const OCP_BOOL& Znflag)
2174 for (
USI j = 0; j < NP; j++) {
2176 vector<OCP_DBL>& fugn = fugN[j];
2180 const vector<OCP_DBL>& xj = x[j];
2181 vector<OCP_DBL>& Znj = Zn[j];
2183 for (
USI i = 0; i < NC; i++) {
2185 for (
USI m = 0; m < NC; m++) {
2186 tmp += (1 - BIC[i * NC + m]) * sqrt(Ai[i] * Ai[m]) * xj[m];
2188 An[i] = 2 / nu[j] * (tmp - aj);
2189 Bn[i] = 1 / nu[j] * (Bi[i] - bj);
2192 ((bj - zj) * An[i] +
2193 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
2194 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) *
2196 (delta1 + delta2 - 1) * zj * zj) *
2198 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
2199 (aj + delta1 * delta2 * bj * bj -
2200 (delta1 + delta2) * bj * (bj + 1)));
2204 G = (zj + delta1 * bj) / (zj + delta2 * bj);
2206 for (
USI i = 0; i < NC; i++) {
2208 C = xj[i] * P / (zj - bj);
2211 for (
USI k = 0; k < NC; k++) {
2212 tmp += (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]) * xj[k];
2214 E = -aj / ((delta1 - delta2) * bj) * (2 * tmp / aj - Bi[i] / bj);
2216 for (
USI k = 0; k <= i; k++) {
2219 aik = (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]);
2221 Cnk = P / (zj - bj) / (zj - bj) *
2222 ((zj - bj) / nu[j] * (delta(i, k) - xj[i]) -
2223 xj[i] * (Znj[k] - Bn[k]));
2224 Dnk = Bi[i] / bj * (Znj[k] - (Bi[k] - bj) * (zj - 1) / (nu[j] * bj));
2225 Gnk = (delta1 - delta2) / ((zj + delta2 * bj) * (zj + delta2 * bj)) *
2226 (Bn[k] * zj - Znj[k] * bj);
2231 Enk = -1 / (delta1 - delta2) / (bj * bj) *
2232 (2 * (bj * aik / nu[j] + Bn[k] * (Bi[i] * aj / bj - tmp)) -
2233 An[k] * Bi[i] - aj * Bi[i] / nu[j]);
2235 fugn[i * NC + k] = 1 / C * Cnk + Dnk + Enk * log(G) + E / G * Gnk;
2236 fugn[k * NC + i] = fugn[i * NC + k];
2244 for (
USI j = 0; j < NP; j++) {
2245 if (!
CheckNan(fugN[j].size(), &fugN[j][0])) {
2252 void MixtureComp::PrintFugN()
2254 for (
USI j = 0; j < NP; j++) {
2255 for (
USI i = 0; i < NC; i++) {
2256 for (
USI k = 0; k < NC; k++) {
2257 cout << fugN[j][i * NC + k] <<
" ";
2261 cout <<
"---------------" << endl;
2265 void MixtureComp::AssembleJmatSP()
2269 fill(JmatSP.begin(), JmatSP.end(), 0);
2275 for (
USI j = 0; j < NP - 1; j++) {
2276 fugNp = &fugN[NP - 1][0];
2277 fugNj = &fugN[j][0];
2279 for (
USI i = 0; i < NC; i++) {
2281 for (
USI k = 0; k < NC; k++) {
2283 Jtmp[k] = fugNj[k] + fugNp[k];
2285 Jtmp += NC * (NP - 1);
2301 OCP_DBL MixtureComp::CalStepNRsp()
2306 for (
USI j = 0; j < NP - 1; j++) {
2309 const OCP_DBL* r = &resSP[j * NC];
2311 for (
USI i = 0; i < NC; i++) {
2312 tmp =
nj[i] + alpha * r[i];
2314 alpha *= 0.9 * fabs(
nj[i] / r[i]);
2321 void MixtureComp::AllocateOthers()
2324 for (
USI i = 0; i < NC; i++) sqrtMWi[i] = sqrt(MWC[i]);
2326 muAux.resize(NPmax);
2327 for (
USI i = 0; i < NPmax; i++) {
2331 for (
USI i = 0; i < NC; i++) {
2332 muAux1I[i] = 5.4402 * pow(Tc[i], 1.0 / 6) / pow(Pc[i], 2.0 / 3);
2335 for (
USI j = 0; j < NPmax; j++) {
2339 JmatDer.resize(NPmax * NPmax * (NC + 1) * (NC + 1));
2341 rhsDer.resize(NPmax * (NC + 1) * (NC + 1));
2342 xixC.resize(NPmax * NC);
2344 xiNC.resize(NPmax * NC);
2352 void MixtureComp::IdentifyPhase()
2360 for (
USI i = 0; i < NC; i++) {
2361 A += x[0][i] * Vc[i] * Tc[i];
2362 B += x[0][i] * Vc[i];
2366 phaseLabel[0] =
GAS;
2369 phaseLabel[0] =
OIL;
2374 if (MW[0] > MW[1]) {
2375 phaseLabel[0] =
OIL;
2376 phaseLabel[1] =
GAS;
2378 phaseLabel[0] =
GAS;
2379 phaseLabel[1] =
OIL;
2389 for (
USI j = 0; j < NP; j++) {
2390 const USI j1 = phaseLabel[j];
2400 void MixtureComp::CalViscosity() { CalViscoLBC(); }
2402 void MixtureComp::CalViscoLBC()
2410 for (
USI j = 0; j < NP; j++) {
2411 const vector<OCP_DBL>& xj = x[j];
2412 vector<OCP_DBL>& muA = muAux[j];
2413 fill(muA.begin(), muA.end(), 0.0);
2418 for (
USI i = 0; i < NC; i++) {
2419 tmp = 5.4402 * pow(Tc[i], 1.0 / 6) / sqrt(MW[j]) / pow(Pc[i], 2.0 / 3);
2423 tmp = 34 * 1E-5 * pow(Tri, 0.94) / tmp;
2425 tmp = 17.78 * 1E-5 * pow((4.58 * Tri - 1.67), 0.625) / tmp;
2427 muA[0] += xj[i] * sqrt(MWC[i]) * tmp;
2428 muA[1] += xj[i] * sqrt(MWC[i]);
2429 xijT += xj[i] * Tc[i];
2430 xijP += xj[i] * Pc[i];
2431 xijV += xj[i] * Vcvis[i];
2433 muA[2] = 5.4402 * pow(xijT, 1.0 / 6) / sqrt(MW[j]) / pow(xijP, 2.0 / 3);
2434 muA[3] = xiC[j] * xijV;
2436 if (muA[3] <= 0.18 && OCP_FALSE) {
2437 muC[j] = muA[0] / muA[1] + 2.05 * 1E-4 * muA[3] / muA[2];
2439 muA[4] = muA[3] * (muA[3] * (muA[3] * (LBCcoef[4] * muA[3] + LBCcoef[3]) +
2443 muC[j] = muA[0] / muA[1] + 1E-4 * (pow(muA[4], 4) - 1) / muA[2];
2448 void MixtureComp::CalViscoHZYT() {}
2450 void MixtureComp::CalFugXAll()
2452 for (
USI j = 0; j < NP; j++) {
2454 vector<OCP_DBL>& fugx = fugX[j];
2455 vector<OCP_DBL>& xj = x[j];
2462 for (
USI i = 0; i < NC; i++) {
2464 for (
USI k = 0; k < NC; k++) {
2465 tmp += xj[k] * (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]);
2469 ((bj - zj) * Ax[i] +
2470 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
2471 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
2472 (delta1 + delta2 - 1) * zj * zj) *
2474 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
2475 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
2481 G = (zj + delta1 * bj) / (zj + delta2 * bj);
2483 for (
USI i = 0; i < NC; i++) {
2485 C = xj[i] * P / (zj - bj);
2488 E = -aj / ((delta1 - delta2) * bj) * (Ax[i] / aj - Bx[i] / bj);
2490 for (
USI k = 0; k < NC; k++) {
2491 aik = (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]);
2494 Cxk = ((zj - bj) * delta(i, k) - xj[i] * (Zx[k] - Bx[k])) * P /
2495 ((zj - bj) * (zj - bj));
2496 Dxk = Bx[i] / bj * (Zx[k] - Bx[k] * (zj - 1) / bj);
2500 Exk = (2 * (aj / bj * Bx[k] * Bx[i] + bj * aik) - Ax[i] * Bx[k] -
2503 Exk /= -(delta1 - delta2);
2504 Gxk = (delta1 - delta2) / (zj + delta2 * bj) / (zj + delta2 * bj) *
2505 (zj * Bx[k] - Zx[k] * bj);
2506 fugx[i * NC + k] = 1 / C * Cxk + Dxk + Exk * log(G) + E / G * Gxk;
2511 for (
USI j = 0; j < NP; j++) {
2512 if (!
CheckNan(fugX[j].size(), &fugX[j][0])) {
2519 void MixtureComp::CalFugPAll(
const OCP_BOOL& Zpflag)
2526 for (
USI j = 0; j < NP; j++) {
2528 vector<OCP_DBL>& fugp = fugP[j];
2529 vector<OCP_DBL>& xj = x[j];
2539 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
2540 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
2541 (delta1 + delta2 - 1) * zj * zj) *
2543 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
2544 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
2547 G = (zj + delta1 * bj) / (zj + delta2 * bj);
2548 Gp = (delta1 - delta2) / ((zj + delta2 * bj) * (zj + delta2 * bj)) *
2549 (Bp * zj - Zp[j] * bj);
2550 for (
USI i = 0; i < NC; i++) {
2556 for (
USI m = 0; m < NC; m++) {
2557 tmp += (1 - BIC[i * NC + m]) * sqrt(Ai[i] * Ai[m]) * xj[m];
2560 E = -aj / ((delta1 - delta2) * bj) * (2 * tmp / aj - Bi[i] / bj);
2562 Cp = ((zj - bj) - P * (Zp[j] - Bp)) / ((zj - bj) * (zj - bj));
2563 Dp = Bi[i] / bj * Zp[j];
2568 fugp[i] = Cp / C + Dp + E / G * Gp;
2573 for (
USI j = 0; j < NP; j++) {
2574 if (!
CheckNan(fugP[j].size(), &fugP[j][0])) {
2581 void MixtureComp::CalVjpVfpVfn_partial()
2587 fill(
vfi.begin(),
vfi.end(), 0.0);
2590 for (
USI j = 0; j < NP; j++) {
2594 const vector<OCP_DBL>& xj = x[j];
2595 vector<OCP_DBL>& Znj = Zn[j];
2597 const USI j1 = phaseLabel[j];
2598 for (
USI i = 0; i < NC; i++) {
2600 for (
USI m = 0; m < NC; m++) {
2601 tmp += (1 - BIC[i * NC + m]) * sqrt(Ai[i] * Ai[m]) * xj[m];
2603 An[i] = 2 / nu[j] * (tmp - aj);
2604 Bn[i] = 1 / nu[j] * (Bi[i] - bj);
2606 ((bj - zj) * An[i] +
2607 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
2608 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
2609 (delta1 + delta2 - 1) * zj * zj) *
2611 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
2612 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
2613 vji[j1][i] = CgTP * (zj + nu[j] * Znj[i]) - Vshift[i];
2615 Zp[j] = ((bj - zj) * aj +
2616 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
2617 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
2618 (delta1 + delta2 - 1) * zj * zj) *
2621 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
2622 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
2623 vjp[j1] = CgTP * nu[j] * (Zp[j] - zj / P);
2628 void MixtureComp::CalXiPn_partial()
2635 fill(xiN.begin(), xiN.end() -
numCom, 0.0);
2637 for (
USI j = 0; j < NP; j++) {
2638 const USI j1 = phaseLabel[j];
2640 tmp = -
xi[j1] *
xi[j1] * CgTP;
2641 for (
USI i = 0; i < NC; i++) {
2644 xiP[j1] = tmp * (Zp[j] - Zj[j] / P);
2648 void MixtureComp::CalRhoPn_partial()
2653 fill(rhoN.begin(), rhoN.end() -
numCom, 0.0);
2655 for (
USI j = 0; j < NP; j++) {
2658 for (
USI m = 0; m < NC; m++) {
2660 xix[j1 *
numCom + m] * MW[j] +
xi[j1] / nu[j] * (MWC[m] - MW[j]);
2665 void MixtureComp::CalMuPn_partial() { CalMuPnLBC_partial(); }
2667 void MixtureComp::CalMuPnLBC_partial()
2673 fill(muN.begin(), muN.end() -
numCom, 0.0);
2676 OCP_DBL der1IJ, der2IJ, der3J, der4J, der6J, der7J, der8J;
2679 OCP_DBL derxTj, derxPj, derMWj;
2685 for (
USI j = 0; j < NP; j++) {
2686 const USI j1 = phaseLabel[j];
2687 const vector<OCP_DBL>& xj = x[j];
2688 const vector<OCP_DBL>& muAuxj = muAux[j];
2690 xTj = xPj = xVj = 0;
2691 for (
USI i = 0; i < NC; i++) {
2692 xTj += xj[i] * Tc[i];
2693 xPj += xj[i] * Pc[i];
2694 xVj += xj[i] * Vcvis[i];
2696 der7J = xVj *
xiP[j1];
2697 if (muAuxj[3] <= 0.18 && OCP_FALSE) {
2698 muP[j1] = (2.05 * 1E-4) * der7J / muAuxj[2];
2700 der8J = der7J * (LBCcoef[1] +
2701 muAuxj[3] * (2 * LBCcoef[2] +
2702 muAuxj[3] * (3 * LBCcoef[3] +
2703 muAuxj[3] * 4 * LBCcoef[4])));
2704 muP[j1] = (4 * 1E-4) * pow(muAuxj[4], 3) * der8J / muAuxj[2];
2709 for (
USI k = 0; k < NC; k++) {
2710 derxTj = (Tc[k] - xTj) / nu[j];
2711 derxPj = (Pc[k] - xPj) / nu[j];
2712 derMWj = (MWC[k] - MW[j]) / nu[j];
2715 for (
USI i = 0; i < NC; i++) {
2716 val1IJ = muAux1I[i] / sqrt(MW[j]);
2717 der1IJ = -(1 / 2) * muAux1I[i] * pow(MW[j], -1.5) * derMWj;
2720 tmp = 34 * 1E-5 * pow(Tri, 0.94);
2722 tmp = 17.78 * 1E-5 * pow(4.58 * Tri - 1.67, 0.625);
2724 val2IJ = tmp / val1IJ;
2725 der2IJ = -tmp * der1IJ / (val1IJ * val1IJ);
2726 der3J += sqrtMWi[i] *
2727 (xj[i] * der2IJ + (delta(i, k) - xj[i]) * val2IJ / nu[j]);
2728 der4J -= xj[i] * sqrtMWi[i];
2733 (1.0 / 6 * pow(xTj, -5.0 / 6) * derxTj -
2734 pow(xTj, 1.0 / 6) * (0.5 / MW[j] * derMWj + 2.0 / 3 / xPj * derxPj)) /
2735 (sqrt(MW[j]) * pow(xPj, 2.0 / 3));
2736 der7J =
xix[j1 *
numCom + k] * xVj + (Vcvis[k] - xVj) *
xi[j1] / nu[j];
2737 if (muAuxj[3] <= 0.18 && OCP_FALSE) {
2739 (der3J * muAuxj[1] - muAuxj[0] * der4J) / (muAuxj[1] * muAuxj[1]) +
2740 2.05 * 1E-4 * (der7J * muAuxj[2] - muAuxj[3] * der6J) /
2741 (muAuxj[2] * muAuxj[2]);
2744 der7J * (LBCcoef[1] +
2745 muAuxj[3] * (2 * LBCcoef[2] +
2746 muAuxj[3] * (3 * LBCcoef[3] +
2747 muAuxj[3] * 4 * LBCcoef[4])));
2749 (der3J * muAuxj[1] - muAuxj[0] * der4J) / (muAuxj[1] * muAuxj[1]) +
2751 (4 * pow(muAuxj[4], 3) * der8J * muAuxj[2] -
2752 (pow(muAuxj[4], 4) - 1) * der6J) /
2753 (muAuxj[2] * muAuxj[2]);
2759 void MixtureComp::CalVjpVfpVfx_partial()
2767 fill(
vfi.begin(),
vfi.end(), 0.0);
2770 for (
USI j = 0; j < NP; j++) {
2771 const USI j1 = phaseLabel[j];
2772 const vector<OCP_DBL>& xj = x[j];
2779 for (
USI i = 0; i < NC; i++) {
2781 for (
USI k = 0; k < NC; k++) {
2782 tmp += xj[k] * (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]);
2786 ((bj - zj) * Ax[i] +
2787 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
2788 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
2789 (delta1 + delta2 - 1) * zj * zj) *
2791 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
2792 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
2794 vji[j1][i] = CgTP * nu[j] * Zx[i];
2796 Zp[j] = ((bj - zj) * aj +
2797 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
2798 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
2799 (delta1 + delta2 - 1) * zj * zj) *
2802 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
2803 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
2804 vjp[j1] = CgTP * nu[j] * (Zp[j] - zj / P);
2809 void MixtureComp::CaldXsdXpAPI04()
2822 fill(
res.begin(),
res.end(), 0.0);
2832 const USI j1 = phaseLabel[0];
2835 bId[0] = ((
vfP - vwp) *
vf -
vfP *
vj[j1]) / vf2;
2838 for (
USI m = 0; m < NC; m++) {
2839 bId[m] =
vfi[m] * vw / vf2;
2846 for (
USI m = 0; m < ncol; m++) {
2851 for (
USI i = 0; i < NC; i++) {
2852 for (
USI m = 0; m < NC; m++) {
2853 bId[m] = (delta(i, m) * Nh -
Ni[i]) / (Nh * Nh);
2860 CalFugPAll(OCP_FALSE);
2865 void MixtureComp::CaldXsdXp04()
2869 fill(JmatTmp.begin(), JmatTmp.end(), 0.0);
2874 const USI dim = NP + 1 + NP * NC;
2875 for (
USI i = 0; i < NC; i++) {
2877 for (
USI m = 0; m < NP; m++) {
2878 MTmp[m] =
vf *
xi[phaseLabel[m]] * x[m][i];
2884 for (
USI m = 0; m < NP; m++) {
2885 const USI m1 = phaseLabel[m];
2886 for (
USI n = 0; n < NC; n++) {
2887 MTmp[n] =
vf *
S[m1] *
2888 (
xix[m1 *
numCom + n] * x[m][i] + delta(i, n) *
xi[m1]);
2889 for (
USI j = 0; j < NP; j++) {
2891 vji[m1][n] * x[m][i] *
S[phaseLabel[j]] *
xi[phaseLabel[j]];
2902 for (
USI m = 0; m < NP; m++) {
2903 for (
USI n = 0; n < NC; n++) {
2910 for (
USI j = 0; j < NP; j++) {
2915 for (
USI n = 0; n < NC; n++) {
2918 MTmp += (NP - j) * NC;
2922 for (
USI j = 0; j < NP - 1; j++) {
2923 const OCP_DBL* fugXJ = &fugX[j][0];
2924 const OCP_DBL* fugXNP = &fugX[NP - 1][0];
2925 for (
USI i = 0; i < NC; i++) {
2931 for (
USI n = 0; n < NC; n++) {
2932 MTmp[n] = -fugXJ[n];
2936 MTmp += (NP - 1 - j) * NC;
2937 for (
USI n = 0; n < NC; n++) {
2938 MTmp[n] = fugXNP[n];
2945 cout << endl <<
"dFdS" << endl;
2946 cout << scientific << setprecision(1);
2947 for (
USI i = 0; i < dim; i++) {
2948 for (
USI j = 0; j < dim; j++) cout << setw(8) << JmatTmp[i * dim + j] <<
" ";
2954 for (
USI i = 0; i < dim; i++) {
2955 for (
USI j = 0; j < dim; j++) {
2956 JmatDer[i * dim + j] = JmatTmp[j * dim + i];
2962 fill(JmatTmp.begin(), JmatTmp.end(), 0.0);
2968 const USI nrow = dim;
2970 for (
USI i = 0; i < NC; i++) {
2974 for (
USI j = 0; j < NP; j++) {
2976 tmp01 +=
S[j1] * x[j][i] *
xiP[j1];
2977 tmp02 +=
S[j1] * x[j][i] *
xi[j1];
2979 MTmp[0] = -(
vf * tmp01 +
vfP * tmp02);
2999 const vector<OCP_DBL>& fugPNP = fugP[NP - 1];
3000 for (
USI j = 0; j < NP - 1; j++) {
3001 const vector<OCP_DBL>& fugPj = fugP[j];
3002 for (
USI i = 0; i < NC; i++) {
3004 MTmp[0] = fugPj[i] - fugPNP[i];
3009 cout << endl <<
"dFdp" << endl;
3010 cout << scientific << setprecision(3);
3011 for (
USI i = 0; i < dim; i++) {
3013 cout << setw(10) << JmatTmp[i * (
numCom + 1) + j] <<
" ";
3018 for (
USI i = 0; i < nrhs; i++) {
3019 for (
USI j = 0; j < nrow; j++) {
3020 rhsDer[i * nrow + j] = JmatTmp[j * nrhs + i];
3024 LUSolve(nrhs, dim, &JmatDer[0], &rhsDer[0], &pivot[0]);
3026 cout << setprecision(6);
3027 cout << endl <<
"dXsdXp" << endl;
3028 for (
USI i = 0; i < nrow; i++) {
3029 for (
USI j = 0; j < nrhs; j++) {
3030 cout << setw(13) << rhsDer[j * nrow + i] <<
" ";
3037 void MixtureComp::CalXiPNX_partial()
3049 for (
USI j = 0; j < NP; j++) {
3050 const vector<OCP_DBL>& xj = x[j];
3056 for (
USI i = 0; i < NC; i++) {
3058 for (
USI k = 0; k < NC; k++) {
3059 tmp += xj[k] * (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]);
3063 ((bj - zj) * Ax[i] +
3064 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
3065 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
3066 (delta1 + delta2 - 1) * zj * zj) *
3068 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
3069 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
3072 tmp = -xiC[j] * xiC[j];
3073 for (
USI i = 0; i < NC; i++) {
3074 xixC[j * NC + i] = tmp * (CgTP * Zx[i] - Vshift[i]);
3076 xiPC[j] = tmp * CgTP * (Zp[j] - Zj[j] / P);
3080 fill(xiNC.begin(), xiNC.end(), 0.0);
3085 fill(xiN.begin(), xiN.end(), 0.0);
3087 for (
USI j = 0; j < NP; j++) {
3094 void MixtureComp::CalRhoPX_partial()
3103 fill(rhoN.begin(), rhoN.end() -
numCom, 0.0);
3107 for (
USI j = 0; j < NP; j++) {
3110 for (
USI i = 0; i < NC; i++) {
3116 void MixtureComp::CalMuPX_partial()
3119 fill(muN.begin(), muN.end() -
numCom, 0.0);
3122 CalMuPXLBC_partial();
3125 void MixtureComp::CalMuPXLBC_partial()
3134 OCP_DBL der1IJ, der2IJ, der3J, der4J, der6J, der7J, der8J;
3137 OCP_DBL derxTj, derxPj, derMWj;
3143 for (
USI j = 0; j < NP; j++) {
3144 const USI j1 = phaseLabel[j];
3145 const vector<OCP_DBL>& xj = x[j];
3146 const vector<OCP_DBL>& muAuxj = muAux[j];
3148 xTj = xPj = xVj = 0;
3149 for (
USI i = 0; i < NC; i++) {
3150 xTj += xj[i] * Tc[i];
3151 xPj += xj[i] * Pc[i];
3152 xVj += xj[i] * Vcvis[i];
3154 der7J = xVj *
xiP[j1];
3155 if (muAuxj[3] <= 0.18 && OCP_FALSE) {
3156 muP[j1] = (2.05 * 1E-4) * der7J / muAuxj[2];
3158 der8J = der7J * (LBCcoef[1] +
3159 muAuxj[3] * (2 * LBCcoef[2] +
3160 muAuxj[3] * (3 * LBCcoef[3] +
3161 muAuxj[3] * 4 * LBCcoef[4])));
3162 muP[j1] = (4 * 1E-4) * pow(muAuxj[4], 3) * der8J / muAuxj[2];
3167 for (
USI k = 0; k < NC; k++) {
3172 for (
USI i = 0; i < NC; i++) {
3173 val1IJ = muAux1I[i] / sqrt(MW[j]);
3174 der1IJ = -(1 / 2) * muAux1I[i] * pow(MW[j], -1.5) * derMWj;
3177 tmp = 34 * 1E-5 * pow(Tri, 0.94);
3179 tmp = 17.78 * 1E-5 * pow(4.58 * Tri - 1.67, 0.625);
3181 val2IJ = tmp / val1IJ;
3182 der2IJ = -tmp * der1IJ / (val1IJ * val1IJ);
3184 xj[i] * sqrtMWi[i] * der2IJ + delta(i, k) * sqrtMWi[k] * val2IJ;
3189 (1.0 / 6 * pow(xTj, -5.0 / 6) * derxTj -
3190 pow(xTj, 1.0 / 6) * (0.5 / MW[j] * derMWj + 2.0 / 3 / xPj * derxPj)) /
3191 (sqrt(MW[j]) * pow(xPj, 2.0 / 3));
3192 der7J =
xix[j1 *
numCom + k] * xVj +
xi[j1] * Vcvis[k];
3193 if (muAuxj[3] <= 0.18 && OCP_FALSE) {
3195 (der3J * muAuxj[1] - muAuxj[0] * der4J) / (muAuxj[1] * muAuxj[1]) +
3196 2.05 * 1E-4 * (der7J * muAuxj[2] - muAuxj[3] * der6J) /
3197 (muAuxj[2] * muAuxj[2]);
3200 der7J * (LBCcoef[1] +
3201 muAuxj[3] * (2 * LBCcoef[2] +
3202 muAuxj[3] * (3 * LBCcoef[3] +
3203 muAuxj[3] * 4 * LBCcoef[4])));
3205 (der3J * muAuxj[1] - muAuxj[0] * der4J) / (muAuxj[1] * muAuxj[1]) +
3207 (4 * pow(muAuxj[4], 3) * der8J * muAuxj[2] -
3208 (pow(muAuxj[4], 4) - 1) * der6J) /
3209 (muAuxj[2] * muAuxj[2]);
3215 void MixtureComp::CalXiPNX_full01()
3222 for (
USI j = 0; j < NP; j++) {
3223 const vector<OCP_DBL>& xj = x[j];
3229 for (
USI i = 0; i < NC; i++) {
3231 for (
USI k = 0; k < NC; k++) {
3232 tmp += xj[k] * (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]);
3236 ((bj - zj) * Ax[i] +
3237 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
3238 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
3239 (delta1 + delta2 - 1) * zj * zj) *
3241 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
3242 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
3245 tmp = -xiC[j] * xiC[j] * CgTP;
3246 for (
USI i = 0; i < NC; i++) {
3247 xixC[j * NC + i] = tmp * Zx[i];
3253 tmp = -xiC[0] * xiC[0] * CgTP;
3254 xiPC[0] = tmp * (Zp[0] - Zj[0] / P);
3255 for (
USI i = 0; i < NC; i++) {
3256 xiNC[i] = tmp * Zn[0][i];
3260 const OCP_DBL* dnkjdNP = &rhsDer[0];
3265 for (
USI i = 0; i < NC; i++) {
3266 for (
USI j = 0; j < NP; j++) {
3269 tmp = -xiC[j] * xiC[j] * CgTP;
3270 const vector<OCP_DBL>& Znj = Zn[j];
3272 for (
USI k = 0; k < NC; k++) {
3273 dertmp += Znj[k] * dnkjdNP[k];
3275 xiNC[j * NC + i] = tmp * dertmp;
3281 for (
USI j = 0; j < NP; j++) {
3282 tmp = -xiC[j] * xiC[j] * CgTP;
3283 xiPC[j] = Zp[j] - Zj[j] / P;
3284 const vector<OCP_DBL>& Znj = Zn[j];
3287 for (
USI k = 0; k < NC; k++) {
3288 xiPC[j] += Znj[k] * dnkjdNP[k];
3299 for (
USI j = 0; j < NP; j++) {
3307 void MixtureComp::CalXiPNX_full02()
3317 for (
USI j = 0; j < NP; j++) {
3318 const vector<OCP_DBL>& xj = x[j];
3324 for (
USI i = 0; i < NC; i++) {
3326 for (
USI k = 0; k < NC; k++) {
3327 tmp += xj[k] * (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]);
3331 ((bj - zj) * Ax[i] +
3332 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
3333 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
3334 (delta1 + delta2 - 1) * zj * zj) *
3336 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
3337 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
3340 tmp = -xiC[j] * xiC[j] * CgTP;
3341 for (
USI i = 0; i < NC; i++) {
3342 xixC[j * NC + i] = tmp * Zx[i];
3349 tmp = -xiC[0] * xiC[0] * CgTP;
3350 xiPC[0] = tmp * (Zp[0] - Zj[0] / P);
3351 for (
USI i = 0; i < NC; i++) {
3352 xiNC[i] = tmp * Zn[0][i];
3358 xiPC[0] = (Zp[0] - Zj[0] / P);
3359 xiPC[1] = (Zp[1] - Zj[1] / P);
3360 for (
USI i = 0; i < NC; i++) {
3361 xiPC[0] += Zn[0][i] * nijPN[i];
3362 xiPC[1] -= Zn[1][i] * nijPN[i];
3365 xiPC[0] *= -xiC[0] * xiC[0] * CgTP;
3366 xiPC[1] *= -xiC[1] * xiC[1] * CgTP;
3368 OCP_DBL tmp0 = -xiC[0] * xiC[0] * CgTP;
3369 OCP_DBL tmp1 = -xiC[1] * xiC[1] * CgTP;
3370 for (
USI i = 0; i < NC; i++) {
3373 for (
USI k = 0; k < NC; k++) {
3374 xiNC[i] += Zn[0][k] * nijPN[k];
3375 xiNC[NC + i] -= Zn[1][k] * nijPN[k];
3377 xiNC[NC + i] += Zn[1][i];
3380 xiNC[NC + i] *= tmp1;
3388 for (
USI j = 0; j < NP; j++) {
3396 void MixtureComp::CalRhoPNX_full01()
3406 for (
USI j = 0; j < NP; j++) {
3410 for (
USI m = 0; m < NC; m++) {
3411 rhox[bId + m] =
xix[bId + m] * MW[j] +
xi[j1] * MWC[m];
3418 for (
USI m = 0; m < NC; m++) {
3419 rhoN[bId + m] = xiN[bId + m] * MW[0];
3421 for (
USI i = 0; i < NC; i++) {
3422 tmp -= MWC[i] *
Ni[i];
3424 rhoN[bId + m] += tmp *
xi[j1] / (Nh * Nh);
3431 const OCP_DBL* xijP = &rhsDer[NP];
3432 for (
USI j = 0; j < NP; j++) {
3435 for (
USI i = 0; i < NC; i++) {
3436 tmp += MWC[i] * xijP[i];
3438 rhoP[j1] +=
xi[j1] * tmp;
3443 for (
USI m = 0; m < NC; m++) {
3445 for (
USI j = 0; j < NP; j++) {
3448 rhoN[bId + m] = xiN[bId + m] * MW[j];
3450 for (
USI i = 0; i < NC; i++) {
3451 tmp += MWC[i] * xijN[i];
3453 rhoN[bId + m] += tmp *
xi[j1];
3460 void MixtureComp::CalRhoPNX_full()
3475 for (
USI m = 0; m < NC; m++) {
3476 rhox[bId + m] =
xix[bId + m] * MW[0] +
xi[j1] * MWC[m];
3477 rhoN[bId + m] = xiN[bId + m] * MW[0];
3479 for (
USI i = 0; i < NC; i++) {
3480 tmp -= MWC[i] *
Ni[i];
3482 rhoN[bId + m] += tmp *
xi[j1] / (Nh * Nh);
3486 fill(
rhoP.begin(),
rhoP.end() - 1, 0.0);
3487 fill(rhoN.begin(), rhoN.end() -
numCom, 0.0);
3489 for (
USI j = 0; j < NP; j++) {
3494 for (
USI i = 0; i < NC; i++) {
3495 rhoP[j1] += MWC[i] * xijPN[0];
3497 for (
USI m = 0; m < NC; m++) {
3498 rhoN[bId + m] += MWC[i] * xijPN[m];
3504 for (
USI m = 0; m < NC; m++) {
3505 rhoN[bId + m] *=
xi[j1];
3506 rhoN[bId + m] += xiN[bId + m] * MW[j];
3507 rhox[bId + m] =
xix[bId + m] * MW[j] +
xi[j1] * MWC[m];
3513 void MixtureComp::CalMuPX_full01()
3515 fill(
muP.begin(),
muP.end() - 1, 0.0);
3516 fill(muN.begin(), muN.end() -
numCom, 0.0);
3519 CalMuPXLBC_full01();
3522 void MixtureComp::CalMuPXLBC_full01()
3527 OCP_DBL der1IJ, der2IJ, der3J, der4J, der6J, der7J, der8J;
3530 OCP_DBL derxTj, derxPj, derMWj, derxVj;
3533 for (
USI j = 0; j < NP; j++) {
3534 const vector<OCP_DBL>& xj = x[j];
3535 const vector<OCP_DBL>& muAuxj = muAux[j];
3536 const USI j1 = phaseLabel[j];
3538 xTj = xPj = xVj = 0;
3539 for (
USI i = 0; i < NC; i++) {
3540 xTj += xj[i] * Tc[i];
3541 xPj += xj[i] * Pc[i];
3542 xVj += xj[i] * Vcvis[i];
3544 for (
USI k = 0; k < NC; k++) {
3550 for (
USI i = 0; i < NC; i++) {
3551 val1IJ = muAux1I[i] / sqrt(MW[j]);
3552 der1IJ = -(1 / 2) * muAux1I[i] * pow(MW[j], -1.5) * derMWj;
3555 tmp = 34 * 1E-5 * pow(Tri, 0.94);
3557 tmp = 17.78 * 1E-5 * pow(4.58 * Tri - 1.67, 0.625);
3559 val2IJ = tmp / val1IJ;
3560 der2IJ = -tmp * der1IJ / (val1IJ * val1IJ);
3561 der3J += sqrtMWi[i] * (delta(i, k) * val2IJ + xj[i] * der2IJ);
3566 (1.0 / 6 * pow(xTj, -5.0 / 6) * derxTj -
3567 pow(xTj, 1.0 / 6) * (0.5 / MW[j] * derMWj + 2.0 / 3 / xPj * derxPj)) /
3568 (sqrt(MW[j]) * pow(xPj, 2.0 / 3));
3569 der7J =
xix[bId + k] * xVj +
xi[j1] * derxVj;
3570 if (muAuxj[3] <= 0.18 && OCP_FALSE) {
3572 (der3J * muAuxj[1] - muAuxj[0] * der4J) / (muAuxj[1] * muAuxj[1]) +
3573 2.05 * 1E-4 * (der7J * muAuxj[2] - muAuxj[3] * der6J) /
3574 (muAuxj[2] * muAuxj[2]);
3577 der7J * (LBCcoef[1] +
3578 muAuxj[3] * (2 * LBCcoef[2] +
3579 muAuxj[3] * (3 * LBCcoef[3] +
3580 muAuxj[3] * 4 * LBCcoef[4])));
3582 (der3J * muAuxj[1] - muAuxj[0] * der4J) / (muAuxj[1] * muAuxj[1]) +
3584 (4 * pow(muAuxj[4], 3) * der8J * muAuxj[2] -
3585 (pow(muAuxj[4], 4) - 1) * der6J) /
3586 (muAuxj[2] * muAuxj[2]);
3595 const vector<OCP_DBL>& xj = x[0];
3596 const vector<OCP_DBL>& muAuxj = muAux[0];
3597 xTj = xPj = xVj = 0;
3598 for (
USI i = 0; i < NC; i++) {
3599 xTj += xj[i] * Tc[i];
3600 xPj += xj[i] * Pc[i];
3601 xVj += xj[i] * Vcvis[i];
3603 der7J = xVj * xiPC[0];
3604 if (muAuxj[3] <= 0.18 && OCP_FALSE) {
3605 muP[phaseLabel[0]] = (2.05 * 1E-4) * der7J / muAuxj[2];
3607 der8J = der7J * (LBCcoef[1] +
3608 muAuxj[3] * (2 * LBCcoef[2] +
3609 muAuxj[3] * (3 * LBCcoef[3] +
3610 muAuxj[3] * 4 * LBCcoef[4])));
3611 muP[phaseLabel[0]] = (4 * 1E-4) * pow(muAuxj[4], 3) * der8J / muAuxj[2];
3617 for (
USI j = 0; j < NP; j++) {
3618 const OCP_DBL* xijP = &rhsDer[NP + j * NC];
3619 const vector<OCP_DBL>& xj = x[j];
3620 const vector<OCP_DBL>& muAuxj = muAux[j];
3621 const USI j1 = phaseLabel[j];
3622 xTj = xPj = xVj = 0;
3623 derxTj = derxPj = derMWj = 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];
3628 derxTj += xijP[i] * Tc[i];
3629 derxPj += xijP[i] * Pc[i];
3630 derMWj += xijP[i] * MWC[i];
3632 der3J = der4J = der7J = 0;
3633 for (
USI i = 0; i < NC; i++) {
3634 val1IJ = muAux1I[i] / sqrt(MW[j]);
3635 der1IJ = -(1 / 2) * muAux1I[i] * pow(MW[j], -1.5) * derMWj;
3638 tmp = 34 * 1E-5 * pow(Tri, 0.94);
3640 tmp = 17.78 * 1E-5 * pow(4.58 * Tri - 1.67, 0.625);
3642 val2IJ = tmp / val1IJ;
3643 der2IJ = -tmp * der1IJ / (val1IJ * val1IJ);
3644 der3J += sqrtMWi[i] * (xijP[i] * val2IJ + xj[i] * der2IJ);
3645 der4J += sqrtMWi[i] * xijP[i];
3646 der7J += xijP[i] * Vcvis[i];
3649 der7J +=
xiP[j1] * xVj;
3652 (1.0 / 6 * pow(xTj, -5.0 / 6) * derxTj -
3653 pow(xTj, 1.0 / 6) * (0.5 / MW[j] * derMWj + 2.0 / 3 / xPj * derxPj)) /
3654 (sqrt(MW[j]) * pow(xPj, 2.0 / 3));
3655 if (muAuxj[3] <= 0.18 && OCP_FALSE) {
3657 (der3J * muAuxj[1] - muAuxj[0] * der4J) / (muAuxj[1] * muAuxj[1]) +
3658 2.05 * 1E-4 * (der7J * muAuxj[2] - muAuxj[3] * der6J) /
3659 (muAuxj[2] * muAuxj[2]);
3662 der7J * (LBCcoef[1] +
3663 muAuxj[3] * (2 * LBCcoef[2] +
3664 muAuxj[3] * (3 * LBCcoef[3] +
3665 muAuxj[3] * 4 * LBCcoef[4])));
3667 (der3J * muAuxj[1] - muAuxj[0] * der4J) / (muAuxj[1] * muAuxj[1]) +
3669 (4 * pow(muAuxj[4], 3) * der8J * muAuxj[2] -
3670 (pow(muAuxj[4], 4) - 1) * der6J) /
3671 (muAuxj[2] * muAuxj[2]);
3677 void MixtureComp::CalXiRhoMuPN_pfullx()
3687 const USI j1 = phaseLabel[0];
3689 xijPN += bId * ncol;
3691 for (
USI i = 0; i < NC; i++) {
3694 for (
USI m = 0; m < NC; m++) {
3695 xiN[bId + m] +=
xix[bId + i] * xijPN[m];
3696 rhoN[bId + m] +=
rhox[bId + i] * xijPN[m];
3697 muN[bId + m] +=
mux[bId + i] * xijPN[m];
3710 for (
USI i = 0; i < NC; i++) {
3712 xiP[j] +=
xix[bId + i] * xijPN[0];
3713 rhoP[j] +=
rhox[bId + i] * xijPN[0];
3714 muP[j] +=
mux[bId + i] * xijPN[0];
3717 for (
USI m = 0; m < NC; m++) {
3718 xiN[bId + m] +=
xix[bId + i] * xijPN[m];
3719 rhoN[bId + m] +=
rhox[bId + i] * xijPN[m];
3720 muN[bId + m] +=
mux[bId + i] * xijPN[m];
3730 void MixtureComp::CalXiRhoMuPN_pfullxn(
const OCP_BOOL& xflag)
3736 if (NP == 1 && !xflag) {
3737 const USI j1 = phaseLabel[0];
3741 for (
USI i = 0; i < NC; i++) {
3742 xiN[bId + i] =
xix[bId + i];
3743 rhoN[bId + i] =
rhox[bId + i];
3744 muN[bId + i] =
mux[bId + i];
3756 for (
USI i = 0; i < NC; i++) {
3758 xiP[j] +=
xix[bId + i] * xijPN[0];
3759 rhoP[j] +=
rhox[bId + i] * xijPN[0];
3760 muP[j] +=
mux[bId + i] * xijPN[0];
3763 for (
USI m = 0; m < NC; m++) {
3764 xiN[bId + m] +=
xix[bId + i] * xijPN[m];
3765 rhoN[bId + m] +=
rhox[bId + i] * xijPN[m];
3766 muN[bId + m] +=
mux[bId + i] * xijPN[m];
3775 void MixtureComp::CalVfiVfp_full01()
3784 const vector<OCP_DBL>& xj = x[0];
3785 vector<OCP_DBL>& Znij = Zn[0];
3788 for (
USI i = 0; i < NC; i++) {
3790 for (
USI m = 0; m < NC; m++) {
3791 tmp += (1 - BIC[i * NC + m]) * sqrt(Ai[i] * Ai[m]) * xj[m];
3793 An[i] = 2 / nu[0] * (tmp - aj);
3794 Bn[i] = 1 / nu[0] * (Bi[i] - bj);
3796 ((bj - zj) * An[i] +
3797 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
3798 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
3799 (delta1 + delta2 - 1) * zj * zj) *
3801 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
3802 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
3803 vfi[i] = CgTP * (zj + nu[0] * Znij[i]);
3805 Zp[0] = ((bj - zj) * aj +
3806 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
3807 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
3808 (delta1 + delta2 - 1) * zj * zj) *
3811 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
3812 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
3813 vfP = CgTP * nu[0] * (Zp[0] - zj / P);
3818 AssembleMatVfiVfp_full01();
3819 AssembleRhsVfiVfp_full01();
3820 LUSolve(NC + 1, NC * NP, &JmatDer[0], &rhsDer[0], &pivot[0]);
3822 OCP_DBL* dnkjdNP = &rhsDer[0];
3823 for (
USI i = 0; i < NC; i++) {
3825 for (
USI j = 0; j < NP; j++) {
3826 for (
USI k = 0; k < NC; k++) {
3827 vfi[i] += dnkjdNP[j * NC + k] * (Zj[j] + nu[j] * Zn[j][k]);
3835 for (
USI j = 0; j < NP; j++) {
3836 vfP += nu[j] / P * (Zp[j] * P - Zj[j]);
3837 for (
USI k = 0; k < NC; k++) {
3838 vfP += dnkjdNP[j * NC + k] * (Zj[j] + nu[j] * Zn[j][k]);
3854 void MixtureComp::AssembleMatVfiVfp_full01()
3856 fill(JmatDer.begin(), JmatDer.end(), 0.0);
3860 for (
USI j = 0; j < NP - 1; j++) {
3863 for (
USI i = 0; i < NC; i++) {
3865 Dcopy(NC, bId, fugNj);
3866 bId += (NP - 1 - j) * NC;
3868 bId += (1 + j) * NC;
3874 bId = &JmatDer[(NP - 1) * (NP * NC * NC)];
3875 OCP_DBL* fugNj = &fugN[NP - 1][0];
3876 for (
USI i = 0; i < NC; i++) {
3877 for (
USI j = 0; j < NP - 1; j++) {
3878 Dcopy(NC, bId, fugNj);
3881 Dscalar((NP - 1) * NC, -1.0, bId - (NP - 1) * NC);
3888 void MixtureComp::AssembleRhsVfiVfp_full01()
3890 fill(rhsDer.begin(), rhsDer.end(), 0.0);
3892 for (
USI k = 0; k < NC; k++) {
3894 rhstmp[NC * (NP - 1) + k] = 1;
3898 for (
USI j = 0; j < NP; j++) {
3899 for (
USI i = 0; i < NC; i++) {
3900 rhstmp[j * NC + i] = fugP[NP - 1][i] - fugP[j][i];
3905 if (!
CheckNan(rhsDer.size(), &rhsDer[0])) {
3911 void MixtureComp::CalVfiVfp_full02()
3923 const vector<OCP_DBL>& xj = x[0];
3924 vector<OCP_DBL>& Znij = Zn[0];
3927 for (
USI i = 0; i < NC; i++) {
3929 for (
USI m = 0; m < NC; m++) {
3930 tmp += (1 - BIC[i * NC + m]) * sqrt(Ai[i] * Ai[m]) * xj[m];
3932 An[i] = 2 / nu[0] * (tmp - aj);
3933 Bn[i] = 1 / nu[0] * (Bi[i] - bj);
3935 ((bj - zj) * An[i] +
3936 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
3937 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
3938 (delta1 + delta2 - 1) * zj * zj) *
3940 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
3941 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
3942 vfi[i] = CgTP * (zj + nu[0] * Znij[i]) - Vshift[i];
3944 Zp[0] = ((bj - zj) * aj +
3945 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
3946 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
3947 (delta1 + delta2 - 1) * zj * zj) *
3950 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
3951 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
3952 vfP = CgTP * nu[0] * (Zp[0] - zj / P);
3957 AssembleMatVfiVfp_full02();
3958 AssembleRhsVfiVfp_full02();
3959 LUSolve(NC + 1, NC, &JmatDer[0], &rhsDer[0], &pivot[0]);
3961 const OCP_DBL* dnkjdNP = &rhsDer[0];
3963 const USI j0 = phaseLabel[0];
3964 const USI j1 = phaseLabel[1];
3965 vjp[j0] = CgTP * nu[0] * (Zp[0] - Zj[0] / P);
3966 vjp[j1] = CgTP * nu[1] * (Zp[1] - Zj[1] / P);
3967 for (
USI k = 0; k < NC; k++) {
3968 vjp[j0] += (CgTP * (Zj[0] + nu[0] * Zn[0][k]) - Vshift[k]) * dnkjdNP[k];
3969 vjp[j1] -= (CgTP * (Zj[1] + nu[1] * Zn[1][k]) - Vshift[k]) * dnkjdNP[k];
3971 vfP = vjp[j0] + vjp[j1];
3975 for (
USI i = 0; i < NC; i++) {
3979 for (
USI k = 0; k < NC; k++) {
3981 (CgTP * (Zj[0] + nu[0] * Zn[0][k]) - Vshift[k]) * dnkjdNP[k];
3982 vji[j1][i] += (CgTP * (Zj[1] + nu[1] * Zn[1][k]) - Vshift[k]) *
3983 (delta(i, k) - dnkjdNP[k]);
3985 vfi[i] = vji[j0][i] + vji[j1][i];
3991 void MixtureComp::AssembleMatVfiVfp_full02()
3994 fill(JmatDer.begin(), JmatDer.end(), 0.0);
3997 OCP_DBL* tmpMat = &JmatDer[0];
3998 for (
USI i = 0; i < NC; i++) {
3999 for (
USI m = 0; m < NC; m++) {
4000 tmpMat[m] = fugN[0][i * NC + m] + fugN[1][i * NC + m];
4006 void MixtureComp::AssembleRhsVfiVfp_full02()
4009 fill(rhsDer.begin(), rhsDer.end(), 0.0);
4013 for (
USI i = 0; i < NC; i++) {
4014 rhstmp[i] = fugP[1][i] - fugP[0][i];
4019 for (
USI k = 0; k < NC; k++) {
4020 for (
USI i = 0; i < NC; i++) {
4021 rhstmp[i] = fugN[1][k * NC + i];
4027 void MixtureComp::CaldXsdXp01()
4035 fill(JmatTmp.begin(), JmatTmp.end(), 0.0);
4040 for (
USI i = 0; i < NC; i++) {
4042 for (
USI m = 0; m < NP; m++) {
4043 MTmp[m] =
vf * xiC[m] * x[m][i];
4047 for (
USI m = 0; m < NP; m++) {
4048 for (
USI n = 0; n < NC; n++) {
4050 vf *
S[m] * (xixC[m * NC + n] * x[m][i] + delta(i, n) * xiC[m]);
4056 for (
USI j = 0; j < NP; j++) {
4061 for (
USI n = 0; n < NC; n++) {
4064 MTmp += (NP - j) * NC;
4067 for (
USI j = 0; j < NP - 1; j++) {
4068 const OCP_DBL* fugXJ = &fugX[j][0];
4069 const OCP_DBL* fugXNP = &fugX[NP - 1][0];
4070 for (
USI i = 0; i < NC; i++) {
4076 for (
USI n = 0; n < NC; n++) {
4081 MTmp += (NP - 1 - j) * NC;
4082 for (
USI n = 0; n < NC; n++) {
4083 MTmp[n] = -fugXNP[n];
4101 USI dim = NP * (NC + 1);
4102 for (
USI i = 0; i < dim; i++) {
4103 for (
USI j = 0; j < dim; j++) {
4104 JmatDer[i * dim + j] = JmatTmp[j * dim + i];
4110 fill(JmatTmp.begin(), JmatTmp.end(), 0.0);
4115 for (
USI i = 0; i < NC; i++) {
4119 for (
USI j = 0; j < NP; j++) {
4120 tmp01 +=
S[j] * x[j][i] * xiPC[j];
4121 tmp02 +=
S[j] * x[j][i] * xiC[j];
4123 MTmp[0] = -(
vf * tmp01 +
vfP * tmp02);
4128 for (
USI k = 0; k < NC; k++) {
4130 for (
USI j = 0; j < NP; j++) {
4131 tmp01 +=
S[j] * x[j][i] * xiNC[j * NC + k];
4133 MTmp[k] = -(
vf * tmp01 +
vfi[k] * tmp02 - delta(i, k));
4141 MTmp += NP * (NC + 1);
4144 const vector<OCP_DBL>& fugPNP = fugP[NP - 1];
4145 for (
USI j = 0; j < NP - 1; j++) {
4146 const vector<OCP_DBL>& fugPj = fugP[j];
4147 for (
USI i = 0; i < NC; i++) {
4149 MTmp[0] = -(fugPj[i] - fugPNP[i]);
4166 USI nrow = (NC + 1) * NP;
4167 for (
USI i = 0; i < nrhs; i++) {
4168 for (
USI j = 0; j < nrow; j++) {
4169 rhsDer[i * nrow + j] = JmatTmp[j * nrhs + i];
4172 LUSolve(NC + 1, (NC + 1) * NP, &JmatDer[0], &rhsDer[0], &pivot[0]);
4184 void MixtureComp::CaldXsdXpAPI01()
4198 for (
USI k = 0; k < NC; k++) {
4199 dXsdXp[bId + k + 1] = (
vfi[k] * vw) / tmp;
4203 for (
USI i = 0; i < NC; i++) {
4204 for (
USI k = 0; k < NC; k++) {
4205 dXsdXp[bId + k] = (delta(i, k) * Nh -
Ni[i]) / (Nh * Nh);
4212 for (
USI j = 0; j < NP; j++) {
4220 USI nrow = NP * (NC + 1);
4223 for (
USI j = 0; j < NP; j++) {
4225 DTmp = &
dXsdXp[phaseLabel[j] * ncol2];
4230 for (
USI k = 0; k < NC; k++) {
4238 for (
USI j = 0; j < NP; j++) {
4239 DTmp = DbId + phaseLabel[j] *
numCom * ncol2;
4240 for (
USI i = 0; i < NC; i++) {
4241 STmp = &rhsDer[NP + j * NC + i];
4246 for (
USI k = 0; k < NC; k++) {
4265 for (
USI j = 0; j < NP; j++) {
4266 dXsdXp[(phaseLabel[j] + 1) * ncol - 1] = -
S[phaseLabel[j]] *
vfi[Wcid] /
vf;
4271 Dtmp[0] = (vwp *
vf - vw *
vfP) / vf2;
4274 for (
USI k = 0; k < NC; k++) {
4275 Dtmp[k] = -vw *
vfi[k] / vf2;
4278 Dtmp[NC] = (
vfi[Wcid] *
vf - vw *
vfi[Wcid]) / vf2;
4281 void MixtureComp::CaldXsdXpAPI02()
4299 USI j0 = phaseLabel[0];
4302 bId[0] = ((
vfP - vwp) *
vf -
vfP *
vj[j0]) / vf2;
4305 for (
USI m = 0; m < NC; m++) {
4306 bId[m] =
vfi[m] * vw / vf2;
4313 for (
USI m = 0; m < ncol; m++) {
4314 bId[m] = -
dXsdXp[j0 * ncol + m];
4318 for (
USI i = 0; i < NC; i++) {
4319 for (
USI m = 0; m < NC; m++) {
4320 bId[m] = (delta(i, m) * Nh -
Ni[i]) / (Nh * Nh);
4328 for (
USI j = 0; j < 2; j++) {
4329 const USI j1 = phaseLabel[j];
4330 bId = &
dXsdXp[j1 * ncol];
4332 bId[0] = (vjp[j1] *
vf -
vfP *
vj[j1]) / vf2;
4336 bId[m] = (vji[j1][m] *
vf -
vfi[m] *
vj[j1]) / vf2;
4342 bId[0] = (vwp *
vf -
vfP * vw) / vf2;
4351 const USI j0 = phaseLabel[0];
4352 const USI j1 = phaseLabel[1];
4353 const OCP_DBL* dnkjdNP = &rhsDer[0];
4358 for (
USI i = 0; i < NC; i++) njDerSum += dnkjdNP[i];
4359 for (
USI i = 0; i < NC; i++) {
4360 bId1[0] = (dnkjdNP[i] * nu[0] - njDerSum * Nh * n[0][i]) / (nu[0] * nu[0]);
4361 bId2[0] = (-dnkjdNP[i] * nu[1] + njDerSum * Nh * n[1][i]) / (nu[1] * nu[1]);
4367 for (
USI m = 0; m < NC; m++) {
4369 for (
USI i = 0; i < NC; i++) njDerSum += dnkjdNP[i];
4370 bId1 = bId + j0 *
numCom * ncol + m + 1;
4371 bId2 = bId + j1 *
numCom * ncol + m + 1;
4372 for (
USI i = 0; i < NC; i++) {
4374 (dnkjdNP[i] * nu[0] - njDerSum * Nh * n[0][i]) / (nu[0] * nu[0]);
4375 bId2[0] = ((delta(i, m) - dnkjdNP[i]) * nu[1] -
4376 (1 - njDerSum) * Nh * n[1][i]) /
4386 void MixtureComp::CaldXsdXpAPI02p()
4408 const USI j1 = phaseLabel[0];
4411 bId[0] = ((
vfP - vwp) *
vf -
vfP *
vj[j1]) / vf2;
4414 for (
USI m = 0; m < NC; m++) {
4415 bId[m] =
vfi[m] * vw / vf2;
4422 for (
USI m = 0; m < ncol; m++) {
4427 for (
USI i = 0; i < NC; i++) {
4428 for (
USI m = 0; m < NC; m++) {
4429 bId[m] = (delta(i, m) * Nh -
Ni[i]) / (Nh * Nh);
4437 for (
USI j = 0; j < 2; j++) {
4438 const USI j1 = phaseLabel[j];
4439 bId = &
dXsdXp[j1 * ncol];
4441 bId[0] = (vjp[j1] *
vf -
vfP *
vj[j1]) / vf2;
4445 bId[m] = (vji[j1][m] *
vf -
vfi[m] *
vj[j1]) / vf2;
4451 bId[0] = (vwp *
vf -
vfP * vw) / vf2;
4460 const USI j0 = phaseLabel[0];
4461 const USI j1 = phaseLabel[1];
4462 const OCP_DBL* dnkjdNP = &rhsDer[0];
4463 OCP_DBL* bId1 = bId + j0 * NC * ncol;
4464 OCP_DBL* bId2 = bId + j1 * NC * ncol;
4467 for (
USI i = 0; i < NC; i++) njDerSum += dnkjdNP[i];
4468 for (
USI i = 0; i < NC; i++) {
4469 bId1[0] = (dnkjdNP[i] * nu[0] - njDerSum * Nh * n[0][i]) / (nu[0] * nu[0]);
4470 bId2[0] = (-dnkjdNP[i] * nu[1] + njDerSum * Nh * n[1][i]) / (nu[1] * nu[1]);
4476 for (
USI m = 0; m < NC; m++) {
4478 for (
USI i = 0; i < NC; i++) njDerSum += dnkjdNP[i];
4479 bId1 = bId + j0 * NC * ncol + m + 1;
4480 bId2 = bId + j1 * NC * ncol + m + 1;
4481 for (
USI i = 0; i < NC; i++) {
4483 (dnkjdNP[i] * nu[0] - njDerSum * Nh * n[0][i]) / (nu[0] * nu[0]);
4484 bId2[0] = ((delta(i, m) - dnkjdNP[i]) * nu[1] -
4485 (1 - njDerSum) * Nh * n[1][i]) /
4495 void MixtureComp::CaldXsdXpAPI03()
4505 fill(
res.begin(),
res.end(), 0.0);
4515 const USI j0 = phaseLabel[0];
4518 bId[0] = ((
vfP - vwp) *
vf -
vfP *
vj[j0]) / vf2;
4521 for (
USI m = 0; m < NC; m++) {
4522 bId[m] = vji[j0][m] * vw / vf2;
4529 for (
USI m = 0; m < ncol; m++) {
4534 bId = &
dXsdXp[2 * ncol + 1];
4535 for (
USI i = 0; i < NC; i++) {
4542 CalFugNAll(OCP_FALSE);
4543 CalFugPAll(OCP_FALSE);
4560 const OCP_DBL* STmp = &rhsDer[0];
4562 const USI wNP = NP + 1;
4565 for (
USI i = 0; i < nrhs; i++) {
4567 for (
USI j = 0; j < NP; j++) {
4568 dXsdXp[sIndex[phaseLabel[j]] * nrhs + i] = STmp[j];
4572 dXsdXp[NP * nrhs + i] = STmp[0];
4575 for (
USI j = 0; j < NP; j++) {
4576 bId = wNP + sIndex[phaseLabel[j]] * NC;
4577 for (
USI k = 0; k < NC; k++) {
4578 dXsdXp[(bId + k) * nrhs + i] = STmp[k];
4586 for (
USI j = 0; j < NP; j++) {
4587 res[sIndex[phaseLabel[j]]] = STmp[j];
4594 for (
USI j = 0; j < NP; j++) {
4595 bId = wNP + sIndex[phaseLabel[j]] * NC;
4596 for (
USI k = 0; k < NC; k++) {
4597 res[bId + k] = STmp[k];
4604 for (
USI j = 0; j < NP; j++) {
4605 const USI j1 = phaseLabel[j];
4606 for (
USI i = 0; i < NC; i++) {
4607 resPc += vji[j1][i] * myres[i];
4634 void MixtureComp::CaldXsdXp03()
4639 fill(JmatTmp.begin(), JmatTmp.end(), 0.0);
4641 const USI dim = NP + 1 + NP * NC;
4646 for (
USI j = 0; j < NP - 1; j++) {
4647 const OCP_DBL* fugNJ = &fugN[j][0];
4648 const OCP_DBL* fugNNP = &fugN[NP - 1][0];
4649 for (
USI i = 0; i < NC; i++) {
4654 for (
USI k = 0; k < NC; k++) {
4655 MTmp[k] = -fugNJ[k];
4659 MTmp += (NP - 1 - j) * NC;
4660 for (
USI k = 0; k < NC; k++) {
4661 MTmp[k] = fugNNP[k];
4668 for (
USI i = 0; i < NC; i++) {
4672 for (
USI j = 0; j < NP; j++) {
4678 for (
USI j = 0; j < NP; j++) {
4679 const USI j1 = phaseLabel[j];
4684 for (
USI m = 0; m < NP; m++) {
4685 const USI m1 = phaseLabel[m];
4687 for (
USI k = 0; k < NC; k++) {
4688 MTmp[k] = vji[j1][k] * (
vf -
vj[j1]) / vf2;
4691 for (
USI k = 0; k < NC; k++) {
4692 MTmp[k] = -vji[m1][k] *
vj[j1] / vf2;
4703 for (
USI m = 0; m < NP; m++) {
4704 const USI m1 = phaseLabel[m];
4705 for (
USI k = 0; k < NC; k++) {
4706 MTmp[k] = -vji[m1][k] *
vj[
numPhase - 1] / vf2;
4723 for (
USI i = 0; i < dim; i++) {
4724 for (
USI j = 0; j < dim; j++) {
4725 JmatDer[i * dim + j] = JmatTmp[j * dim + i];
4729 fill(JmatTmp.begin(), JmatTmp.end(), 0.0);
4735 for (
USI j = 0; j < NP - 1; j++) {
4736 for (
USI i = 0; i < NC; i++) {
4737 MTmp[0] = fugP[j][i] - fugP[NP - 1][i];
4742 for (
USI i = 0; i < NC; i++) {
4748 for (
USI j = 0; j < NP; j++) {
4749 const USI j1 = phaseLabel[j];
4751 MTmp[0] = (
vfP *
vj[j1] - vjp[j1] *
vf) / vf2;
4777 const USI nrow = NP * NC + NP + 1;
4778 for (
USI i = 0; i < nrhs; i++) {
4779 for (
USI j = 0; j < nrow; j++) {
4780 rhsDer[i * nrow + j] = JmatTmp[j * nrhs + i];
4786 OCP_DBL* myRes = &rhsDer[nrhs * nrow];
4787 for (
USI j = 0; j < NP - 1; j++) {
4788 const OCP_DBL* fugJ = &fug[j][0];
4789 const OCP_DBL* fugNP = &fug[NP - 1][0];
4790 for (
USI i = 0; i < NC; i++) {
4791 myRes[i] = fugJ[i] - fugNP[i];
4796 for (
USI i = 0; i < NC; i++) {
4798 for (
USI j = 0; j < NP; j++) {
4799 myRes[i] -= x[j][i] * nu[j];
4804 for (
USI j = 0; j < NP; j++) {
4805 myRes[j] =
S[phaseLabel[j]] -
vj[phaseLabel[j]] /
vf;
4819 LUSolve(
numCom + 1 + 1, nrow, &JmatDer[0], &rhsDer[0], &pivot[0]);
4822 void MixtureComp::CalVfiVfp_full03()
4830 for (
USI i = 0; i < NC; i++) {
4831 vfi[i] = vji[j1][i];
4841 for (
USI i = 0; i < NC; i++) {
4842 vfP += vji[j][i] * nijPN[0];
4844 for (
USI m = 0; m < NC; m++) {
4845 vfi[m] += vji[j][i] * nijPN[m];
4854 void MixtureComp::CalKeyDerx()
4867 const OCP_DBL* xiNtmp = &xiN[0];
4868 const OCP_DBL* muNtmp = &muN[0];
4881 tmp =
xi[j] / (
mu[j] *
mu[j]);
4882 for (
USI i = 0; i < NC; i++) {
4884 dTmp[0] = (sTmp[0] *
xi[j] + xijtmp[i] *
xiP[j]) /
mu[j] -
4885 xijtmp[i] *
muP[j] * tmp;
4889 for (
USI k = 0; k < NC; k++) {
4890 dTmp[k] = (sTmp[k] *
xi[j] + xijtmp[i] * xiNtmp[k]) /
mu[j] -
4891 xijtmp[i] * muNtmp[k] * tmp;
4907 (
xiP[wpid] *
mu[wpid] -
muP[wpid] *
xi[wpid]) / (
mu[wpid] *
mu[wpid]);
4910 void MixtureComp::CalKeyDern()
4919 vector<OCP_DBL> njPN(NC + 1, 0);
4924 const OCP_DBL* xiNtmp = &xiN[0];
4925 const OCP_DBL* muNtmp = &muN[0];
4931 const USI j = phaseLabel[0];
4937 for (
USI i = 0; i < NC; i++) {
4939 dTmp[0] = (xijtmp[i] *
xiP[j]) /
mu[j] - xijtmp[i] *
muP[j] * tmp;
4942 for (
USI k = 0; k < NC; k++) {
4943 dTmp[k] = ((delta(i, k) - xijtmp[i]) *
xi[j] /
nj[j] +
4944 xijtmp[i] * xiNtmp[k]) /
4946 xijtmp[i] * muNtmp[k] * tmp;
4960 fill(njPN.begin(), njPN.end(), 0.0);
4961 for (
USI i = 0; i < NC; i++) {
4962 for (
USI k = 0; k < NC + 1; k++) {
4969 tmp01 =
xi[j] / (
mu[j] *
mu[j]);
4970 for (
USI i = 0; i < NC; i++) {
4971 tmp02 = (sTmp[0] - njPN[0] * xijtmp[i]) /
nj[j];
4973 dTmp[0] = (tmp02 *
xi[j] + xijtmp[i] *
xiP[j]) /
mu[j] -
4974 xijtmp[i] *
muP[j] * tmp01;
4978 for (
USI k = 0; k < NC; k++) {
4979 tmp02 = (sTmp[k] - njPN[k + 1] * xijtmp[i]) /
nj[j];
4980 dTmp[k] = (tmp02 *
xi[j] + xijtmp[i] * xiNtmp[k]) /
mu[j] -
4981 xijtmp[i] * muNtmp[k] * tmp01;
4996 (
xiP[wpid] *
mu[wpid] -
muP[wpid] *
xi[wpid]) / (
mu[wpid] *
mu[wpid]);
5005 OCP_DBL Q = (a * a - 3 * b) / 9;
5006 OCP_DBL R = (2 * a * a * a - 9 * a * b + 27 * c) / 54;
5013 OCP_DBL theta = acos(R / sqrt(Q3));
5014 Ztmp[0] = -2 * sqrt(Q) * cos(theta / 3) - a / 3;
5015 Ztmp[1] = -2 * sqrt(Q) * cos((theta + 2 *
PI) / 3) - a / 3;
5016 Ztmp[2] = -2 * sqrt(Q) * cos((theta - 2 *
PI) / 3) - a / 3;
5019 NTcubicroot(Ztmp[0], a, b, c);
5020 NTcubicroot(Ztmp[1], a, b, c);
5021 NTcubicroot(Ztmp[2], a, b, c);
5024 sort(Ztmp.begin(), Ztmp.end());
5040 Ztmp[0] =
S + T - a / 3;
5043 NTcubicroot(Ztmp[0], a, b, c);
5080 OCP_DBL e = root * (root * (root + a) + b) + c;
5086 while (fabs(e) > 1E-8) {
5088 df = root * (3 * root + 2 * a) + b;
5089 root = root - e / df;
5095 e = root * (root * (root + a) + b) + c;
5096 if (fabs(e) <= opte) {
5108 void MixtureComp::OutMixtureIters()
const
5110 cout <<
"SSMSTA: " << setw(12) << itersSSMSTA << setw(15)
5111 << itersSSMSTA * 1.0 / countsSSMSTA << endl;
5112 cout <<
"NRSTA: " << setw(12) << itersNRSTA << setw(15)
5113 << itersNRSTA * 1.0 / countsNRSTA << endl;
5114 cout <<
"SSMSP: " << setw(12) << itersSSMSP << setw(15)
5115 << itersSSMSP * 1.0 / countsSSMSP << endl;
5116 cout <<
"NRSP: " << setw(12) << itersNRSP << setw(15)
5117 << itersNRSP * 1.0 / countsNRSP << endl;
5118 cout <<
"NRRR: " << setw(12) << itersRR << setw(15)
5119 << itersRR * 1.0 / countsRR << endl;
5139 "Keywords MISCIBLE, PARACHOR, MISCSTR aren't been given simultaneously!");
5149 phiN.resize(NC * NC);
5165 const vector<OCP_DBL>& xj = x[0];
5166 vector<OCP_DBL>& Znj = Zn[0];
5168 for (
USI i = 0; i < NC; i++) {
5170 for (
USI m = 0; m < NC; m++) {
5171 tmp += (1 - BIC[i * NC + m]) * sqrt(Ai[i] * Ai[m]) * xj[m];
5173 An[i] = 2 / nu[0] * (tmp - aj);
5174 Bn[i] = 1 / nu[0] * (Bi[i] - bj);
5175 Znj[i] = ((bj - zj) * An[i] +
5176 ((aj + delta1 * delta2 * (3 * bj * bj + 2 * bj)) +
5177 ((delta1 + delta2) * (2 * bj + 1) - 2 * delta1 * delta2 * bj) * zj -
5178 (delta1 + delta2 - 1) * zj * zj) *
5180 (3 * zj * zj + 2 * ((delta1 + delta2 - 1) * bj - 1) * zj +
5181 (aj + delta1 * delta2 * bj * bj - (delta1 + delta2) * bj * (bj + 1)));
5184 G = (zj + delta1 * bj) / (zj + delta2 * bj);
5186 for (
USI i = 0; i < NC; i++) {
5191 for (
USI k = 0; k < NC; k++) {
5192 tmp += (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]) * xj[k];
5194 E = -aj / ((delta1 - delta2) * bj) * (2 * tmp / aj - Bi[i] / bj);
5196 for (
USI k = 0; k <= i; k++) {
5199 aik = (1 - BIC[i * NC + k]) * sqrt(Ai[i] * Ai[k]);
5201 Cnk = (Bn[k] - Znj[k]) / ((zj - bj) * (zj - bj));
5202 Dnk = Bi[i] / bj * (Znj[k] - (Bi[k] - bj) * (zj - 1) / (nu[0] * bj));
5203 Gnk = (delta1 - delta2) / ((zj + delta2 * bj) * (zj + delta2 * bj)) *
5204 (Bn[k] * zj - Znj[k] * bj);
5209 Enk = -1 / (delta1 - delta2) / (bj * bj) *
5210 (2 * (bj * aik / nu[0] + Bn[k] * (Bi[i] * aj / bj - tmp)) -
5211 An[k] * Bi[i] - aj * Bi[i] / nu[0]);
5213 phiN[i * NC + k] = 1 / C * Cnk + Dnk + Enk * log(G) + E / G * Gnk;
5214 phiN[k * NC + i] =
phiN[i * NC + k];
5223 vector<OCP_DBL>& xj = x[0];
5225 for (
USI i = 0; i < NC; i++) {
5226 for (
USI j = 0; j <= i; j++) {
5228 delta(i, j) + nu[0] * sqrt(xj[i] * xj[j]) *
phiN[i * NC + j];
5260 void MixtureComp::InputMiscibleParam(
const ComponentParam& param,
const USI& tarId)
5265 OCP_ABORT(
"PARACHOR has not been Input!");
5269 void MixtureComp::CalSurfaceTension()
5280 for (
USI i = 0; i < NC; i++)
void Dscalar(const int &n, const double &alpha, double *x)
Scales a vector by a constant.
void CalEigenSY(const int &N, float *A, float *w, float *work, const int &lwork)
Calculate the minimal eigenvalue for symmetric matrix with mkl lapack.
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.
int 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 Dcopy(const int &N, double *dst, const double *src)
Calculate the minimal eigenvalue for symmetric 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.
const USI EOS_PVTW
Mixture model = equation-of-state.
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 USI LRATE_MODE
Well option = fixed fluid rate???
const USI WRATE_MODE
Well option = fixed water rate.
const USI WATER
Fluid type = water.
const USI ORATE_MODE
Well option = fixed oil rate.
const OCP_DBL CONV7
lbm/ft3 -> gm-M/cc
const USI OIL
Fluid type = oil.
unsigned int OCP_USI
Long unsigned integer.
const OCP_DBL CONV5
0 F = CONV5 R
const USI PROD
Well type = producer.
const OCP_DBL GAS_CONSTANT
Gas Constant.
const USI INJ
Well type = injector.
const USI BHP_MODE
Well option = fixed bottom-hole-pressure.
unsigned int OCP_BOOL
OCP_BOOL in OCP.
const USI GRATE_MODE
Well option = fixed gas rate.
#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.
ComponentParam contains information of components.
Type_A_r< vector< OCP_DBL > > Tc
Critical temperature of hydrocarbon components.
Type_A_r< vector< OCP_DBL > > Vcvis
Critical volume used for viscosity calculations only.
USI numPhase
num of phase, water is excluded, constant now.
Type_A_r< vector< OCP_DBL > > Zc
Critical Z-factor of hydrocarbon components.
vector< string > SSMparamSP
Params for Solving Phase Spliting with SSM.
Type_A_r< vector< OCP_DBL > > MW
Molecular Weight of hydrocarbon components.
vector< string > SSMparamSTA
Params for Solving Phase Spliting with SSM.
USI numCom
num of components, water is excluded.
vector< string > Cname
Name of hydrocarbon components.
Type_A_r< vector< OCP_DBL > > Acf
Acentric factor of hydrocarbon components.
Type_A_r< vector< OCP_DBL > > Vshift
Volume shift of hydrocarbon components.
vector< string > NRparamSP
Params for Solving Phase Spliting with NR.
Type_A_r< vector< OCP_DBL > > Zcvis
Critical Z-factor used for viscosity calculations only.
Type_A_r< vector< OCP_DBL > > OmegaB
OMEGA_B of hydrocarbon components.
vector< string > NRparamSTA
Params for Solving Phase Spliting with NR.
vector< string > RRparam
Params for Solving Rachford-Rice equations.
Type_A_r< vector< OCP_DBL > > Vc
Critical volume of hydrocarbon components.
Type_A_r< vector< OCP_DBL > > Pc
Critical pressure of hydrocarbon components.
Type_A_r< vector< OCP_DBL > > OmegaA
OMEGA_A of hydrocarbon components.
vector< vector< OCP_DBL > > BIC
Binary interaction.
vector< OCP_DBL > LBCcoef
LBC coefficients for viscosity calculation.
Type_A_r< vector< OCP_DBL > > Parachor
PARACHOR of hydrocarbon components.
OCP_BOOL IfUseMiscible() const
Return ifUseMiscible.
void AssignValue(const OCP_USI &n, const OCP_DBL &v)
Assign value to surTen.
void Setup(const OCP_USI &numBulk)
Allocate memory for Miscible term.
void CopyPhase()
Copy the basic properties from MixtureComp to Mixture.
OCP_BOOL StableSSM(const USI &Id)
strict SSM
void FlashIMPEC(const OCP_DBL &Pin, const OCP_DBL &Tin, const OCP_DBL *Niin, const USI &lastNP, const OCP_DBL *xijin, const OCP_USI &bId) override
Flash calculation with moles of components.
void CalFtypeIMPEC()
Calculate Flash type for IMPEC.
void RachfordRice2P()
Used when NP = 2, improved Rachford-Rice2.
void CalProdWeight(const OCP_DBL &Pin, const OCP_DBL &Tin, const OCP_DBL *Niin, const vector< OCP_DBL > &prodPhase, vector< OCP_DBL > &prodWeight) override
Calculate ProdWeight for PROD well.
void CalFtypeFIM(const OCP_DBL *Sjin)
Calculate Flash type for FIM.
void CalProdRate(const OCP_DBL &Pin, const OCP_DBL &Tin, const OCP_DBL *Niin, vector< OCP_DBL > &prodRate) override
Calculate Production rate for PROD well.
vector< OCP_DBL > parachor
Parachor params of hydrocarbon components.
USI CubicRoot(const OCP_DBL &a, const OCP_DBL &b, const OCP_DBL &c, const OCP_BOOL &NTflag=OCP_FALSE) const
Result is stored in Ztmp.
OCP_DBL XiPhase(const OCP_DBL &Pin, const OCP_DBL &Tin, const OCP_DBL *Ziin, const USI &tarPhase) override
return mass density of phase
void RachfordRice3()
Used when NP > 2.
void FlashFIMn(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 *phaseExistin, const USI &lastNP, const OCP_USI &bId) override
void Flash(const OCP_DBL &Pin, const OCP_DBL &Tin, const OCP_DBL *Niin) override
flash calculation with saturation of phases.
void AssembleSkipMatSTA()
Assemble matrix to Calculated eigen value used for skipping.
void CalPhiNSkip()
Calculate d ln phi[i][j] / d n[k][j].
vector< OCP_SIN > skipMatSTA
void SplitBFGS()
Use BFGS to calculate phase splitting.
OCP_BOOL flagSkip
If ture, then skipping could be try,.
void AllocateSkip()
Allocate memory for variables used in skipping stability analysis.
vector< OCP_DBL > phiN
d ln phi[i][j] / d n[k][j]
Miscible * misTerm
Miscible term pointing to OptionalFeature.
void CalFugXSTA()
Calculate d ln(Fug) / dx for Y.
SkipStaAnaly * skipSta
Skip analysis Term pointing to OptionalFeature.
void SetupWellOpt(WellOpt &opt, const vector< SolventINJ > &sols, const OCP_DBL &Psurf, const OCP_DBL &Tsurf) override
void FlashFIM(const OCP_DBL &Pin, const OCP_DBL &Tin, const OCP_DBL *Niin, const OCP_DBL *Sjin, const USI &lastNP, const OCP_DBL *xijin, const OCP_USI &bId) override
Flash calculation with moles of components and Calculate the derivative.
vector< OCP_SIN > eigenWork
work space for computing eigenvalues with ssyevd_
void UpdateXRR()
Update X according to RR.
OCP_DBL RhoPhase(const OCP_DBL &Pin, const OCP_DBL &Pbb, const OCP_DBL &Tin, const OCP_DBL *Ziin, const USI &tarPhase) override
return mass density of phase
vector< OCP_SIN > eigenSkip
eigen values of matrix for skipping Skip Stability Analysis.
void RachfordRice2()
Used when NP = 2.
OCP_DBL surTen
Surface tension between hydrocarbons phases.
OCP_BOOL StableSSM01(const USI &Id)
relaxed SSM
void x2n()
x[j][i] -> n[j][i]
void SplitNR()
Use NR to calculate phase splitting.
void CalSkipForNextStep()
Calculate skip info for next step.
OCP_DBL resPc
a precalculated value
vector< OCP_DBL > rhoP
d rho / dP: numphase
USI numCom
num of components.
vector< OCP_DBL > dXsdXp
derivatives of second variables wrt. primary variables
vector< OCP_DBL > rho
mass density of phase: numPhase
vector< OCP_DBL > mu
viscosity of phase: numPhase
vector< OCP_BOOL > pSderExist
Existence of derivative of phase saturation.
USI numPhase
num of phases.
vector< OCP_DBL > xix
d xi[j] / d x[i][j]: numphase * numCom
vector< OCP_DBL > vj
volume of phase: numPhase;
OCP_USI bulkId
index of current bulk
vector< OCP_DBL > rhox
d rho[j] / d x[i][j]: numphase * numCom
vector< OCP_DBL > S
saturation of phase: numPhase
vector< OCP_DBL > xij
Nij / nj: 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_BOOL > phaseExist
existence of phase: numPhase
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< USI > pVnumCom
num of variable components in the phase
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
OCP_BOOL conflag
convergence flag, if converges, conflag = OCP_TRUE
OCP_BOOL conflag
convergence flag, if converges, conflag = OCP_TRUE
USI Eval_All(const USI &j, const OCP_DBL &val, vector< OCP_DBL > &outdata, vector< OCP_DBL > &slope)
Careful: the memory outdata and slope have not be allocated before.
Miscible miscible
Miscible term for Compositional Model.
SkipStaAnaly skipStaAnaly
Skip Stability Analysis term.
OCP_BOOL conflag
convergence flag, if converges, conflag = OCP_TRUE
USI curIt
current Iterations
OCP_DBL eYt
if Yt > 1 + eYt, then single phase is unstable
OCP_DBL Ktol
tolerace^2 for K
OCP_BOOL conflag
convergence flag, if converges, conflag = OCP_TRUE
OCP_BOOL IfUseSkip() const
Return ifUseSkip.
void Setup(const OCP_USI &numBulk, const USI &np, const USI &nc)
Allocate memory for SkipStaAnaly term.
void SetFlagSkip(const OCP_USI &n, const OCP_BOOL &flagSkip)
Set flag for skipping.
void AssignValue(const OCP_USI &n, const OCP_DBL &minEigenSkip, const OCP_DBL &PSkip, const OCP_DBL &TSkip, const vector< OCP_DBL > &ziSkip)
Update variables used for determine if skipping will happen.
OCP_BOOL activity
If OCP_FALSE, this param is not given.
vector< T > data
Data of param.