30 numBulk = myGrid.activeGridNum;
32 neighbor.resize(numBulk);
33 selfPtr.resize(numBulk);
34 neighborNum.resize(numBulk);
36 vector<GPair> tmp1, tmp2;
40 for (
OCP_USI n = 0; n < myGrid.numGrid; n++) {
41 const GB_Pair& GBtmp = myGrid.activeMap_G2B[n];
47 tmp1 = myGrid.gNeighbor[n];
50 for (
USI i = 0; i < len; i++) {
51 const GB_Pair& GBtmp2 = myGrid.activeMap_G2B[tmp1[i].id];
54 tmp1[i].id = GBtmp2.
GetId();
55 tmp2.push_back(tmp1[i]);
59 tmp2.push_back(
GPair(bIdb, 0.0));
61 sort(tmp2.begin(), tmp2.end(), GPair::lessG);
64 for (
USI i = 0; i < len; i++) {
65 neighbor[bIdb].push_back(tmp2[i].
id);
66 if (tmp2[i].
id == bIdb) {
70 for (
USI j = selfPtr[bIdb] + 1; j < len; j++) {
71 iteratorConn.push_back(
BulkPair(bIdb, tmp2[j].
id, tmp2[j].area));
73 neighborNum[bIdb] = len;
77 numConn = iteratorConn.size();
87 USI len = myBulk.wellBulkId.size();
88 for (
USI n = 0; n < len; n++) {
89 for (
auto& v : neighbor[n]) {
90 USI clen = myBulk.wellBulkId.size();
92 for (
USI i = 0; i < clen; i++) {
93 if (v == myBulk.wellBulkId[n]) {
99 myBulk.wellBulkId.push_back(v);
111 for (
OCP_USI n = 0; n < numBulk; n++) {
112 MySolver.rowCapacity[n] += neighborNum[n];
121 for (
OCP_USI n = 0; n < numBulk; n++) {
122 myLS.colId[n].assign(neighbor[n].begin(), neighbor[n].end());
123 myLS.diagPtr[n] = selfPtr[n];
131 lastUpblock = upblock;
132 lastUpblock_Rho = upblock_Rho;
133 lastUpblock_Trans = upblock_Trans;
134 lastUpblock_Velocity = upblock_Velocity;
141 upblock = lastUpblock;
142 upblock_Rho = lastUpblock_Rho;
143 upblock_Trans = lastUpblock_Trans;
144 upblock_Velocity = lastUpblock_Velocity;
153 cout << setprecision(18);
154 for (
OCP_USI c = 0; c < numConn; c++) {
155 tmp = fabs(upblock[c] - lastUpblock[c]);
157 cout <<
">> Difference in upblock index at \t" << tmp <<
"\n";
159 tmp = fabs(upblock_Rho[c] - lastUpblock_Rho[c]);
161 cout <<
">> Difference in upblock Rho at \t" << tmp <<
"\n";
163 tmp = fabs(upblock_Trans[c] - lastUpblock_Trans[c]);
165 cout <<
">> Difference in upblock Trans at \t" << tmp <<
"\n";
167 tmp = fabs(upblock_Velocity[c] - lastUpblock_Velocity[c]);
169 cout <<
">> Difference in upblock Velocity at \t" << tmp <<
"\n";
176 for (
OCP_USI i = 0; i < numBulk; i++) {
177 cout <<
"(" << myGrid.activeMap_B2G[i] <<
")"
180 for (
auto& v : neighbor[i]) {
181 cout << myGrid.activeMap_B2G[v] <<
"\t";
183 cout <<
"[" << selfPtr[i] <<
"]";
184 cout <<
"\t" << neighborNum[i];
188 for (
OCP_USI i = 0; i < numConn; i++) {
189 cout << myGrid.activeMap_B2G[iteratorConn[i].BId]
190 <<
"\t" << myGrid.activeMap_B2G[iteratorConn[i].EId] <<
"\n";
194 void BulkConn::PrintConnectionInfoCoor(
const Grid& myGrid)
const
199 USI sp = myGrid.GetNumDigitIJK();
200 cout <<
"BulkConn : " << numConn << endl;
201 for (
OCP_USI c = 0; c < numConn; c++) {
202 bIdb = iteratorConn[c].BId;
203 eIdb = iteratorConn[c].EId;
204 bIdg = myGrid.activeMap_B2G[bIdb];
205 eIdg = myGrid.activeMap_B2G[eIdb];
207 cout << GetIJKformat(to_string(I), to_string(J), to_string(K), sp) <<
" ";
208 cout << setw(6) << bIdg;
210 cout << setw(6) << bIdb;
213 cout << GetIJKformat(to_string(I), to_string(J), to_string(K), sp) <<
" ";
214 cout << setw(6) << eIdg;
216 cout << setw(6) << eIdb;
217 cout << setw(20) << setprecision(8) << fixed << iteratorConn[c].area *
CONV2;
231 upblock.resize(numConn * np);
232 upblock_Rho.resize(numConn * np);
233 upblock_Trans.resize(numConn * np);
234 upblock_Velocity.resize(numConn * np);
235 lastUpblock.resize(numConn * np);
236 lastUpblock_Rho.resize(numConn * np);
237 lastUpblock_Trans.resize(numConn * np);
238 lastUpblock_Velocity.resize(numConn * np);
249 for (
OCP_USI n = 0; n < numBulk; n++) {
253 Vp0 = myBulk.rockVpInit[n];
254 Vp = myBulk.rockVp[n];
257 myLS.diagVal[n] = temp;
258 myLS.b[n] = temp * P + dt * (vf - Vp);
264 USI np = myBulk.numPhase;
265 USI nc = myBulk.numCom;
267 OCP_DBL valup, rhsup, valdown, rhsdown;
270 OCP_USI lastbId = iteratorConn[0].EId;
271 for (
OCP_USI c = 0; c < numConn; c++) {
272 bId = iteratorConn[c].BId;
273 eId = iteratorConn[c].EId;
279 for (
USI j = 0; j < np; j++) {
280 uId = upblock[c * np + j];
281 if (!myBulk.phaseExist[uId * np + j])
continue;
286 for (
USI i = 0; i < nc; i++) {
288 myBulk.vfi[bId * nc + i] * myBulk.xij[uId * np * nc + j * nc + i];
290 myBulk.vfi[eId * nc + i] * myBulk.xij[uId * np * nc + j * nc + i];
292 OCP_DBL dD = myBulk.depth[bId] - myBulk.depth[eId];
293 OCP_DBL dPc = myBulk.Pc[bId * np + j] - myBulk.Pc[eId * np + j];
294 OCP_DBL temp = myBulk.xi[uId * np + j] * upblock_Trans[c * np + j] * dt;
295 valup += temp * valupi;
302 valdown += temp * valdowni;
304 rhsup += temp * valupi;
305 rhsdown -= temp * valdowni;
308 USI diagptr = myLS.diagPtr[bId];
309 if (bId != lastbId) {
311 assert(myLS.val[bId].size() == diagptr);
312 myLS.val[bId].push_back(myLS.diagVal[bId]);
320 myLS.val[bId][diagptr] += valup;
321 myLS.val[bId].push_back(-valup);
322 myLS.val[eId].push_back(-valdown);
323 myLS.diagVal[eId] += valdown;
324 myLS.b[bId] += rhsup;
325 myLS.b[eId] += rhsdown;
329 for (
OCP_USI n = 0; n < numBulk; n++) {
330 if (myLS.val[n].size() == myLS.diagPtr[n]) myLS.val[n].push_back(myLS.diagVal[n]);
339 USI np = myBulk.numPhase;
340 for (
OCP_USI c = 0; c < numConn; c++) {
342 for (
USI j = 0; j < np; j++) {
343 OCP_USI uId = upblock[c * np + j];
345 if (myBulk.phaseExist[uId * np + j]) {
346 myBulk.cfl[uId * np + j] += fabs(upblock_Velocity[c * np + j]) * dt;
363 USI np = myBulk.numPhase;
365 for (
OCP_USI c = 0; c < numConn; c++) {
366 bId = iteratorConn[c].BId;
367 eId = iteratorConn[c].EId;
370 for (
USI j = 0; j < np; j++) {
371 bId_np_j = bId * np + j;
372 eId_np_j = eId * np + j;
374 bool exbegin = myBulk.phaseExist[bId_np_j];
375 bool exend = myBulk.phaseExist[eId_np_j];
377 if ((exbegin) && (exend)) {
378 Pbegin = myBulk.Pj[bId_np_j];
379 Pend = myBulk.Pj[eId_np_j];
380 rho = (myBulk.rho[bId_np_j] + myBulk.rho[eId_np_j]) / 2;
381 }
else if (exbegin && (!exend)) {
382 Pbegin = myBulk.Pj[bId_np_j];
383 Pend = myBulk.P[eId];
384 rho = myBulk.rho[bId_np_j];
385 }
else if ((!exbegin) && (exend)) {
386 Pbegin = myBulk.P[bId];
387 Pend = myBulk.Pj[eId_np_j];
388 rho = myBulk.rho[eId_np_j];
390 upblock[c * np + j] = bId;
391 upblock_Trans[c * np + j] = 0;
392 upblock_Velocity[c * np + j] = 0;
409 upblock_Rho[c * np + j] = rho;
410 upblock[c * np + j] = uId;
413 OCP_USI uId_np_j = uId * np + j;
414 OCP_DBL trans = Akd * myBulk.kr[uId_np_j] / myBulk.mu[uId_np_j];
415 upblock_Trans[c * np + j] = trans;
416 upblock_Velocity[c * np + j] = trans * dP;
419 upblock_Trans[c * np + j] = 0;
420 upblock_Velocity[c * np + j] = 0;
430 USI np = myBulk.numPhase;
431 USI nc = myBulk.numCom;
433 for (
OCP_USI c = 0; c < numConn; c++) {
434 OCP_USI bId = iteratorConn[c].BId;
435 OCP_USI eId = iteratorConn[c].EId;
437 for (
USI j = 0; j < np; j++) {
438 OCP_USI uId = upblock[c * np + j];
439 OCP_USI uId_np_j = uId * np + j;
441 if (!myBulk.phaseExist[uId_np_j])
continue;
443 OCP_DBL phaseVelocity = upblock_Velocity[c * np + j];
444 for (
USI i = 0; i < nc; i++) {
445 OCP_DBL dNi = dt * phaseVelocity * myBulk.xi[uId_np_j] *
446 myBulk.xij[uId_np_j * nc + i];
447 myBulk.Ni[eId * nc + i] += dNi;
448 myBulk.Ni[bId * nc + i] -= dNi;
463 upblock.resize(numConn * np);
464 upblock_Rho.resize(numConn * np);
472 const USI np = myBulk.numPhase;
473 const USI nc = myBulk.numCom;
474 const USI ncol = nc + 1;
475 const USI ncol2 = np * nc + np;
476 const USI bsize = ncol * ncol;
477 const USI bsize2 = ncol * ncol2;
479 vector<OCP_DBL> bmat(bsize, 0);
482 for (
USI i = 1; i < ncol; i++) {
483 bmat[i * ncol + i] = 1;
485 for (
OCP_USI n = 0; n < numBulk; n++) {
486 bmat[0] = myBulk.rockC1 * myBulk.rockVpInit[n] - myBulk.vfp[n];
487 for (
USI i = 0; i < nc; i++) {
488 bmat[i + 1] = -myBulk.vfi[n * nc + i];
490 for (
USI i = 0; i < bsize; i++) {
491 myLS.diagVal[n * bsize + i] = bmat[i];
499 vector<OCP_DBL> dFdXpB(bsize, 0);
500 vector<OCP_DBL> dFdXpE(bsize, 0);
501 vector<OCP_DBL> dFdXsB(bsize2, 0);
502 vector<OCP_DBL> dFdXsE(bsize2, 0);
505 OCP_USI bId_np_j, eId_np_j, uId_np_j;
506 bool phaseExistBj, phaseExistEj;
507 OCP_DBL kr, mu, xi, xij, rhoP, xiP, muP, rhox, xix, mux;
512 OCP_USI lastbId = iteratorConn[0].EId;
513 for (
OCP_USI c = 0; c < numConn; c++) {
514 bId = iteratorConn[c].BId;
515 eId = iteratorConn[c].EId;
517 fill(dFdXpB.begin(), dFdXpB.end(), 0.0);
518 fill(dFdXpE.begin(), dFdXpE.end(), 0.0);
519 fill(dFdXsB.begin(), dFdXsB.end(), 0.0);
520 fill(dFdXsE.begin(), dFdXsE.end(), 0.0);
521 dGamma =
GRAVITY_FACTOR * (myBulk.depth[bId] - myBulk.depth[eId]);
523 for (
USI j = 0; j < np; j++) {
524 uId = upblock[c * np + j];
525 bId_np_j = bId * np + j;
526 eId_np_j = eId * np + j;
527 uId_np_j = uId * np + j;
529 if (!myBulk.phaseExist[uId_np_j])
continue;
531 phaseExistBj = myBulk.phaseExist[bId_np_j];
532 phaseExistEj = myBulk.phaseExist[eId_np_j];
534 dP = myBulk.Pj[bId_np_j] - myBulk.Pj[eId_np_j] -
535 upblock_Rho[c * np + j] * dGamma;
536 xi = myBulk.xi[uId_np_j];
537 kr = myBulk.kr[uId_np_j];
538 mu = myBulk.mu[uId_np_j];
539 muP = myBulk.muP[uId_np_j];
540 xiP = myBulk.xiP[uId_np_j];
541 rhoP = myBulk.rhoP[uId_np_j];
542 transJ = Akd * kr / mu;
544 for (
USI i = 0; i < nc; i++) {
545 xij = myBulk.xij[uId_np_j * nc + i];
546 transIJ = xij * xi * transJ;
549 dFdXpB[(i + 1) * ncol] += transIJ;
550 dFdXpE[(i + 1) * ncol] -= transIJ;
552 tmp = xij * transJ * xiP * dP;
553 tmp += -transIJ * muP / mu * dP;
555 tmp += transIJ * (-rhoP * dGamma);
556 dFdXpB[(i + 1) * ncol] += tmp;
558 else if (!phaseExistBj) {
559 tmp += transIJ * (-rhoP * dGamma);
560 dFdXpE[(i + 1) * ncol] += tmp;
563 dFdXpB[(i + 1) * ncol] +=
564 transIJ * (-myBulk.rhoP[bId_np_j] * dGamma) / 2;
565 dFdXpE[(i + 1) * ncol] +=
566 transIJ * (-myBulk.rhoP[eId_np_j] * dGamma) / 2;
568 dFdXpB[(i + 1) * ncol] += tmp;
571 dFdXpE[(i + 1) * ncol] += tmp;
577 for (
USI k = 0; k < np; k++) {
578 dFdXsB[(i + 1) * ncol2 + k] +=
579 transIJ * myBulk.dPcj_dS[bId_np_j * np + k];
580 tmp = Akd * xij * xi / mu *
581 myBulk.dKr_dS[uId_np_j * np + k] * dP;
582 dFdXsB[(i + 1) * ncol2 + k] += tmp;
583 dFdXsE[(i + 1) * ncol2 + k] -=
584 transIJ * myBulk.dPcj_dS[eId_np_j * np + k];
588 for (
USI k = 0; k < nc; k++) {
589 rhox = myBulk.rhox[uId_np_j * nc + k];
590 xix = myBulk.xix[uId_np_j * nc + k];
591 mux = myBulk.mux[uId_np_j * nc + k];
592 tmp = -transIJ * rhox * dGamma;
593 tmp += xij * transJ * xix * dP;
594 tmp += -transIJ * mux / mu * dP;
595 dFdXsB[(i + 1) * ncol2 + np + j * nc + k] += tmp;
597 dFdXsB[(i + 1) * ncol2 + np + j * nc + i] += xi * transJ * dP;
600 for (
USI k = 0; k < nc; k++) {
601 rhox = myBulk.rhox[bId_np_j * nc + k] / 2;
602 xix = myBulk.xix[uId_np_j * nc + k];
603 mux = myBulk.mux[uId_np_j * nc + k];
604 tmp = -transIJ * rhox * dGamma;
605 tmp += xij * transJ * xix * dP;
606 tmp += -transIJ * mux / mu * dP;
607 dFdXsB[(i + 1) * ncol2 + np + j * nc + k] += tmp;
608 dFdXsE[(i + 1) * ncol2 + np + j * nc + k] += -transIJ * myBulk.rhox[eId_np_j * nc + k] / 2 * dGamma;
610 dFdXsB[(i + 1) * ncol2 + np + j * nc + i] += xi * transJ * dP;
615 for (
USI k = 0; k < np; k++) {
616 dFdXsB[(i + 1) * ncol2 + k] +=
617 transIJ * myBulk.dPcj_dS[bId_np_j * np + k];
618 dFdXsE[(i + 1) * ncol2 + k] -=
619 transIJ * myBulk.dPcj_dS[eId_np_j * np + k];
620 tmp = Akd * xij * xi / mu *
621 myBulk.dKr_dS[uId_np_j * np + k] * dP;
622 dFdXsE[(i + 1) * ncol2 + k] += tmp;
626 for (
USI k = 0; k < nc; k++) {
627 rhox = myBulk.rhox[uId_np_j * nc + k];
628 xix = myBulk.xix[uId_np_j * nc + k];
629 mux = myBulk.mux[uId_np_j * nc + k];
630 tmp = -transIJ * rhox * dGamma;
631 tmp += xij * transJ * xix * dP;
632 tmp += -transIJ * mux / mu * dP;
633 dFdXsE[(i + 1) * ncol2 + np + j * nc + k] += tmp;
635 dFdXsE[(i + 1) * ncol2 + np + j * nc + i] += xi * transJ * dP;
638 for (
USI k = 0; k < nc; k++) {
639 rhox = myBulk.rhox[eId_np_j * nc + k] / 2;
640 xix = myBulk.xix[uId_np_j * nc + k];
641 mux = myBulk.mux[uId_np_j * nc + k];
642 tmp = -transIJ * rhox * dGamma;
643 tmp += xij * transJ * xix * dP;
644 tmp += -transIJ * mux / mu * dP;
645 dFdXsE[(i + 1) * ncol2 + np + j * nc + k] += tmp;
646 dFdXsB[(i + 1) * ncol2 + np + j * nc + k] += -transIJ * myBulk.rhox[bId_np_j * nc + k] / 2 * dGamma;
648 dFdXsE[(i + 1) * ncol2 + np + j * nc + i] += xi * transJ * dP;
654 USI diagptr = myLS.diagPtr[bId];
656 if (bId != lastbId) {
658 assert(myLS.val[bId].size() == diagptr * bsize);
660 myLS.val[bId].insert(myLS.val[bId].end(), myLS.diagVal.data() +
id,
661 myLS.diagVal.data() +
id + bsize);
672 DaABpbC(ncol, ncol, ncol2, 1, dFdXsB.data(), &myBulk.dSec_dPri[bId * bsize2], 1,
674 Dscalar(bsize, dt, bmat.data());
677 for (
USI i = 0; i < bsize; i++) {
678 myLS.val[bId][diagptr * bsize + i] += bmat[i];
682 Dscalar(bsize, -1, bmat.data());
683 myLS.val[eId].insert(myLS.val[eId].end(), bmat.begin(), bmat.end());
686 if (!
CheckNan(bmat.size(), &bmat[0]))
697 DaABpbC(ncol, ncol, ncol2, 1, dFdXsE.data(), &myBulk.dSec_dPri[eId * bsize2], 1,
699 Dscalar(bsize, dt, bmat.data());
702 myLS.val[bId].insert(myLS.val[bId].end(), bmat.begin(), bmat.end());
704 Dscalar(bsize, -1, bmat.data());
705 for (
USI i = 0; i < bsize; i++) {
706 myLS.diagVal[eId * bsize + i] += bmat[i];
710 if (!
CheckNan(bmat.size(), &bmat[0]))
718 for (
OCP_USI n = 0; n < numBulk; n++) {
719 if (myLS.val[n].size() == myLS.diagPtr[n] * bsize)
720 myLS.val[n].insert(myLS.val[n].end(), myLS.diagVal.data() + n * bsize,
721 myLS.diagVal.data() + n * bsize + bsize);
731 const USI np = myBulk.numPhase;
736 for (
OCP_USI c = 0; c < numConn; c++) {
737 bId = iteratorConn[c].BId;
738 eId = iteratorConn[c].EId;
740 for (
USI j = 0; j < np; j++) {
741 bId_np_j = bId * np + j;
742 eId_np_j = eId * np + j;
744 bool exbegin = myBulk.phaseExist[bId_np_j];
745 bool exend = myBulk.phaseExist[eId_np_j];
747 if ((exbegin) && (exend)) {
748 Pbegin = myBulk.Pj[bId_np_j];
749 Pend = myBulk.Pj[eId_np_j];
750 rho = (myBulk.rho[bId_np_j] + myBulk.rho[eId_np_j]) / 2;
751 }
else if (exbegin && (!exend)) {
752 Pbegin = myBulk.Pj[bId_np_j];
753 Pend = myBulk.P[eId];
754 rho = myBulk.rho[bId_np_j];
755 }
else if ((!exbegin) && (exend)) {
756 Pbegin = myBulk.P[bId];
757 Pend = myBulk.Pj[eId_np_j];
758 rho = myBulk.rho[eId_np_j];
760 upblock[c * np + j] = bId;
761 upblock_Rho[c * np + j] = 0;
771 upblock[c * np + j] = uId;
772 upblock_Rho[c * np + j] = rho;
781 const USI np = myBulk.numPhase;
782 const USI nc = myBulk.numCom;
783 const USI len = nc + 1;
787 for (
OCP_USI n = 0; n < numBulk; n++) {
792 res[bId] = myBulk.rockVp[n] - myBulk.vf[n];
793 for (
USI i = 0; i < nc; i++) {
794 res[bId + 1 + i] = myBulk.Ni[bIdb + i] - myBulk.lNi[bIdb + i];
798 OCP_USI bId_np_j, eId_np_j, uId_np_j;
804 for (
OCP_USI c = 0; c < numConn; c++) {
805 bId = iteratorConn[c].BId;
806 eId = iteratorConn[c].EId;
809 for (
USI j = 0; j < np; j++) {
810 bId_np_j = bId * np + j;
811 eId_np_j = eId * np + j;
813 bool exbegin = myBulk.phaseExist[bId_np_j];
814 bool exend = myBulk.phaseExist[eId_np_j];
816 if ((exbegin) && (exend)) {
817 Pbegin = myBulk.Pj[bId_np_j];
818 Pend = myBulk.Pj[eId_np_j];
819 rho = (myBulk.rho[bId_np_j] + myBulk.rho[eId_np_j]) / 2;
820 }
else if (exbegin && (!exend)) {
821 Pbegin = myBulk.Pj[bId_np_j];
822 Pend = myBulk.P[eId];
823 rho = myBulk.rho[bId_np_j];
824 }
else if ((!exbegin) && (exend)) {
825 Pbegin = myBulk.P[bId];
826 Pend = myBulk.Pj[eId_np_j];
827 rho = myBulk.rho[eId_np_j];
829 upblock[c * np + j] = bId;
830 upblock_Rho[c * np + j] = 0;
840 upblock_Rho[c * np + j] = rho;
841 upblock[c * np + j] = uId;
843 uId_np_j = uId * np + j;
844 if (!myBulk.phaseExist[uId_np_j])
continue;
845 tmp = dt * Akd * myBulk.xi[uId_np_j] *
846 myBulk.kr[uId_np_j] / myBulk.mu[uId_np_j] * dP;
848 for (
USI i = 0; i < nc; i++) {
849 dNi = tmp * myBulk.xij[uId_np_j * nc + i];
850 res[bId * len + 1 + i] += dNi;
851 res[eId * len + 1 + i] -= dNi;
863 const USI np = myBulk.numPhase;
868 for (
OCP_USI c = 0; c < numConn; c++) {
869 bId = iteratorConn[c].BId;
870 eId = iteratorConn[c].EId;
872 for (
USI j = 0; j < np; j++) {
873 bId_np_j = bId * np + j;
874 eId_np_j = eId * np + j;
876 bool exbegin = myBulk.phaseExist[bId_np_j];
877 bool exend = myBulk.phaseExist[eId_np_j];
879 if ((exbegin) && (exend)) {
880 Pbegin = myBulk.Pj[bId_np_j];
881 Pend = myBulk.Pj[eId_np_j];
882 rho = (myBulk.rho[bId_np_j]* myBulk.S[bId_np_j] + myBulk.rho[eId_np_j] * myBulk.S[eId_np_j])
883 / (myBulk.S[bId_np_j] + myBulk.S[eId_np_j]);
885 else if (exbegin && (!exend)) {
886 Pbegin = myBulk.Pj[bId_np_j];
887 Pend = myBulk.P[eId];
888 rho = myBulk.rho[bId_np_j];
890 else if ((!exbegin) && (exend)) {
891 Pbegin = myBulk.P[bId];
892 Pend = myBulk.Pj[eId_np_j];
893 rho = myBulk.rho[eId_np_j];
896 upblock[c * np + j] = bId;
897 upblock_Rho[c * np + j] = 0;
907 upblock[c * np + j] = uId;
908 upblock_Rho[c * np + j] = rho;
913 void BulkConn::CalResFIMS(vector<OCP_DBL>& res,
const Bulk& myBulk,
const OCP_DBL& dt)
917 const USI np = myBulk.numPhase;
918 const USI nc = myBulk.numCom;
919 const USI len = nc + 1;
923 for (
OCP_USI n = 0; n < numBulk; n++) {
928 res[bId] = myBulk.rockVp[n] - myBulk.vf[n];
929 for (
USI i = 0; i < nc; i++) {
930 res[bId + 1 + i] = myBulk.Ni[bIdb + i] - myBulk.lNi[bIdb + i];
934 OCP_USI bId_np_j, eId_np_j, uId_np_j;
940 for (
OCP_USI c = 0; c < numConn; c++) {
941 bId = iteratorConn[c].BId;
942 eId = iteratorConn[c].EId;
945 for (
USI j = 0; j < np; j++) {
946 bId_np_j = bId * np + j;
947 eId_np_j = eId * np + j;
949 bool exbegin = myBulk.phaseExist[bId_np_j];
950 bool exend = myBulk.phaseExist[eId_np_j];
952 if ((exbegin) && (exend)) {
953 Pbegin = myBulk.Pj[bId_np_j];
954 Pend = myBulk.Pj[eId_np_j];
955 rho = (myBulk.rho[bId_np_j] * myBulk.S[bId_np_j] + myBulk.rho[eId_np_j] * myBulk.S[eId_np_j])
956 / (myBulk.S[bId_np_j] + myBulk.S[eId_np_j]);
958 else if (exbegin && (!exend)) {
959 Pbegin = myBulk.Pj[bId_np_j];
960 Pend = myBulk.P[eId];
961 rho = myBulk.rho[bId_np_j];
963 else if ((!exbegin) && (exend)) {
964 Pbegin = myBulk.P[bId];
965 Pend = myBulk.Pj[eId_np_j];
966 rho = myBulk.rho[eId_np_j];
969 upblock[c * np + j] = bId;
970 upblock_Rho[c * np + j] = 0;
980 upblock_Rho[c * np + j] = rho;
981 upblock[c * np + j] = uId;
983 uId_np_j = uId * np + j;
984 if (!myBulk.phaseExist[uId_np_j])
continue;
985 tmp = dt * Akd * myBulk.xi[uId_np_j] *
986 myBulk.kr[uId_np_j] / myBulk.mu[uId_np_j] * dP;
988 for (
USI i = 0; i < nc; i++) {
989 dNi = tmp * myBulk.xij[uId_np_j * nc + i];
990 res[bId * len + 1 + i] += dNi;
991 res[eId * len + 1 + i] -= dNi;
1007 const USI np = myBulk.numPhase;
1008 const USI nc = myBulk.numCom;
1009 const USI nch = nc - 1;
1010 const USI ncol = nc + 1;
1011 const USI ncol2 = np * nc + np;
1012 const USI bsize = ncol * ncol;
1013 const USI bsize2 = ncol * ncol2;
1015 vector<OCP_DBL> bmat(bsize, 0);
1018 for (
USI i = 1; i < ncol; i++) {
1019 bmat[i * ncol + i] = 1;
1021 for (
OCP_USI n = 0; n < numBulk; n++) {
1022 bmat[0] = myBulk.rockC1 * myBulk.rockVpInit[n] - myBulk.vfp[n];
1023 for (
USI i = 0; i < nc; i++) {
1024 bmat[i + 1] = -myBulk.vfi[n * nc + i];
1026 for (
USI i = 0; i < bsize; i++) {
1027 myLS.diagVal[n * bsize + i] = bmat[i];
1034 vector<OCP_DBL> dFdXpB(bsize, 0);
1035 vector<OCP_DBL> dFdXpE(bsize, 0);
1036 vector<OCP_DBL> dFdXsB(bsize2, 0);
1037 vector<OCP_DBL> dFdXsE(bsize2, 0);
1038 vector<bool> phaseExistB(np,
false);
1039 vector<bool> phaseExistE(np,
false);
1041 vector<USI> pEnumComB(np, 0);
1042 vector<USI> pEnumComE(np, 0);
1046 OCP_USI bId_np_j, eId_np_j, uId_np_j;
1047 OCP_DBL kr, mu, xi, xij, rhoP, xiP, muP, rhox, xix, mux;
1053 OCP_USI lastbId = iteratorConn[0].EId;
1054 for (
OCP_USI c = 0; c < numConn; c++) {
1055 bId = iteratorConn[c].BId;
1056 eId = iteratorConn[c].EId;
1058 fill(dFdXpB.begin(), dFdXpB.end(), 0.0);
1059 fill(dFdXpE.begin(), dFdXpE.end(), 0.0);
1060 fill(dFdXsB.begin(), dFdXsB.end(), 0.0);
1061 fill(dFdXsE.begin(), dFdXsE.end(), 0.0);
1062 dGamma =
GRAVITY_FACTOR * (myBulk.depth[bId] - myBulk.depth[eId]);
1064 const USI npB = myBulk.phaseNum[bId] + 1; ncolB = npB;
1065 const USI npE = myBulk.phaseNum[eId] + 1; ncolE = npE;
1067 for (
USI j = 0; j < np; j++) {
1068 phaseExistB[j] = myBulk.phaseExist[bId * np + j];
1069 phaseExistE[j] = myBulk.phaseExist[eId * np + j];
1070 pEnumComB[j] = myBulk.pEnumCom[bId * np + j];
1071 pEnumComE[j] = myBulk.pEnumCom[eId * np + j];
1072 ncolB += pEnumComB[j];
1073 ncolE += pEnumComE[j];
1077 USI jxB = npB;
USI jxE = npE;
1078 for (
USI j = 0; j < np; j++) {
1079 uId = upblock[c * np + j];
1081 phaseExistU = (uId == bId ? phaseExistB[j] : phaseExistE[j]);
1083 jxB += pEnumComB[j];
1084 jxE += pEnumComE[j];
1088 bId_np_j = bId * np + j;
1089 eId_np_j = eId * np + j;
1090 uId_np_j = uId * np + j;
1091 dP = myBulk.Pj[bId_np_j] - myBulk.Pj[eId_np_j] -
1092 upblock_Rho[c * np + j] * dGamma;
1093 xi = myBulk.xi[uId_np_j];
1094 kr = myBulk.kr[uId_np_j];
1095 mu = myBulk.mu[uId_np_j];
1096 muP = myBulk.muP[uId_np_j];
1097 xiP = myBulk.xiP[uId_np_j];
1098 rhoP = myBulk.rhoP[uId_np_j];
1099 transJ = Akd * kr / mu;
1102 for (
USI i = 0; i < nc; i++) {
1103 xij = myBulk.xij[uId_np_j * nc + i];
1104 transIJ = xij * xi * transJ;
1107 dFdXpB[(i + 1) * ncol] += transIJ;
1108 dFdXpE[(i + 1) * ncol] -= transIJ;
1110 tmp = xij * transJ * xiP * dP;
1111 tmp += -transIJ * muP / mu * dP;
1112 if (!phaseExistE[j]) {
1113 tmp += transIJ * (-rhoP * dGamma);
1114 dFdXpB[(i + 1) * ncol] += tmp;
1115 }
else if (!phaseExistB[j]) {
1116 tmp += transIJ * (-rhoP * dGamma);
1117 dFdXpE[(i + 1) * ncol] += tmp;
1119 dFdXpB[(i + 1) * ncol] +=
1120 transIJ * (-myBulk.rhoP[bId_np_j] * dGamma) / 2;
1121 dFdXpE[(i + 1) * ncol] +=
1122 transIJ * (-myBulk.rhoP[eId_np_j] * dGamma) / 2;
1124 dFdXpB[(i + 1) * ncol] += tmp;
1126 dFdXpE[(i + 1) * ncol] += tmp;
1131 USI j1SB = 0;
USI j1SE = 0;
1134 for (
USI j1 = 0; j1 < np; j1++) {
1135 if (phaseExistB[j1]) {
1136 dFdXsB[(i + 1) * ncolB + j1SB] +=
1137 transIJ * myBulk.dPcj_dS[bId_np_j * np + j1];
1138 tmp = Akd * xij * xi / mu *
1139 myBulk.dKr_dS[uId_np_j * np + j1] * dP;
1140 dFdXsB[(i + 1) * ncolB + j1SB] += tmp;
1143 if (phaseExistE[j1]) {
1144 dFdXsE[(i + 1) * ncolE + j1SE] -=
1145 transIJ * myBulk.dPcj_dS[eId_np_j * np + j1];
1150 if (!phaseExistE[j]) {
1151 for (
USI k = 0; k < pEnumComB[j]; k++) {
1152 rhox = myBulk.rhox[uId_np_j * nc + k];
1153 xix = myBulk.xix[uId_np_j * nc + k];
1154 mux = myBulk.mux[uId_np_j * nc + k];
1155 tmp = -transIJ * rhox * dGamma;
1156 tmp += xij * transJ * xix * dP;
1157 tmp += -transIJ * mux / mu * dP;
1158 dFdXsB[(i + 1) * ncolB + jxB + k] += tmp;
1161 if (i < pEnumComB[j])
1162 dFdXsB[(i + 1) * ncolB + jxB + i] += xi * transJ * dP;
1165 for (
USI k = 0; k < pEnumComB[j]; k++) {
1166 rhox = myBulk.rhox[bId_np_j * nc + k] / 2;
1167 xix = myBulk.xix[uId_np_j * nc + k];
1168 mux = myBulk.mux[uId_np_j * nc + k];
1169 tmp = -transIJ * rhox * dGamma;
1170 tmp += xij * transJ * xix * dP;
1171 tmp += -transIJ * mux / mu * dP;
1172 dFdXsB[(i + 1) * ncolB + jxB + k] += tmp;
1173 dFdXsE[(i + 1) * ncolE + jxE + k] += -transIJ * myBulk.rhox[eId_np_j * nc + k] / 2 * dGamma;
1176 if (i < pEnumComB[j])
1177 dFdXsB[(i + 1) * ncolB + jxB + i] += xi * transJ * dP;
1182 for (
USI j1 = 0; j1 < np; j1++) {
1183 if (phaseExistB[j1]) {
1184 dFdXsB[(i + 1) * ncolB + j1SB] +=
1185 transIJ * myBulk.dPcj_dS[bId_np_j * np + j1];
1188 if (phaseExistE[j1]) {
1189 dFdXsE[(i + 1) * ncolE + j1SE] -=
1190 transIJ * myBulk.dPcj_dS[eId_np_j * np + j1];
1191 tmp = Akd * xij * xi / mu *
1192 myBulk.dKr_dS[uId_np_j * np + j1] * dP;
1193 dFdXsE[(i + 1) * ncolE + j1SE] += tmp;
1198 if (!phaseExistB[j]) {
1199 for (
USI k = 0; k < pEnumComE[j]; k++) {
1200 rhox = myBulk.rhox[uId_np_j * nc + k];
1201 xix = myBulk.xix[uId_np_j * nc + k];
1202 mux = myBulk.mux[uId_np_j * nc + k];
1203 tmp = -transIJ * rhox * dGamma;
1204 tmp += xij * transJ * xix * dP;
1205 tmp += -transIJ * mux / mu * dP;
1206 dFdXsE[(i + 1) * ncolE + jxE + k] += tmp;
1209 if (i < pEnumComE[j])
1210 dFdXsE[(i + 1) * ncolE + jxE + i] += xi * transJ * dP;
1213 for (
USI k = 0; k < pEnumComE[j]; k++) {
1214 rhox = myBulk.rhox[eId_np_j * nc + k] / 2;
1215 xix = myBulk.xix[uId_np_j * nc + k];
1216 mux = myBulk.mux[uId_np_j * nc + k];
1217 tmp = -transIJ * rhox * dGamma;
1218 tmp += xij * transJ * xix * dP;
1219 tmp += -transIJ * mux / mu * dP;
1220 dFdXsE[(i + 1) * ncolE + jxE + k] += tmp;
1221 dFdXsB[(i + 1) * ncolB + jxB + k] += -transIJ * myBulk.rhox[bId_np_j * nc + k] / 2 * dGamma;
1224 if (i < pEnumComE[j])
1225 dFdXsE[(i + 1) * ncolE + jxE + i] += xi * transJ * dP;
1229 jxB += pEnumComB[j];
1230 jxE += pEnumComE[j];
1233 USI diagptr = myLS.diagPtr[bId];
1235 if (bId != lastbId) {
1237 assert(myLS.val[bId].size() == diagptr * bsize);
1239 myLS.val[bId].insert(myLS.val[bId].end(), myLS.diagVal.data() +
id,
1240 myLS.diagVal.data() +
id + bsize);
1248 DaABpbC(ncol, ncol, ncolB, 1, dFdXsB.data(), &myBulk.dSec_dPri[myBulk.dSdPindex[bId]], 1,
1250 Dscalar(bsize, dt, bmat.data());
1253 for (
USI i = 0; i < bsize; i++) {
1254 myLS.val[bId][diagptr * bsize + i] += bmat[i];
1258 Dscalar(bsize, -1, bmat.data());
1259 myLS.val[eId].insert(myLS.val[eId].end(), bmat.begin(), bmat.end());
1262 if (!
CheckNan(bmat.size(), &bmat[0]))
1271 DaABpbC(ncol, ncol, ncolE, 1, dFdXsE.data(), &myBulk.dSec_dPri[myBulk.dSdPindex[eId]], 1,
1273 Dscalar(bsize, dt, bmat.data());
1276 myLS.val[bId].insert(myLS.val[bId].end(), bmat.begin(), bmat.end());
1278 Dscalar(bsize, -1, bmat.data());
1279 for (
USI i = 0; i < bsize; i++) {
1280 myLS.diagVal[eId * bsize + i] += bmat[i];
1284 if (!
CheckNan(bmat.size(), &bmat[0]))
1291 for (
OCP_USI n = 0; n < numBulk; n++) {
1292 if (myLS.val[n].size() == myLS.diagPtr[n] * bsize)
1293 myLS.val[n].insert(myLS.val[n].end(), myLS.diagVal.data() + n * bsize,
1294 myLS.diagVal.data() + n * bsize + bsize);
1304 const USI np = myBulk.numPhase;
1305 const USI nc = myBulk.numCom;
1306 const USI nch = nc - 1;
1307 const USI ncol = nc + 1;
1308 const USI ncol2 = np * nc + np;
1309 const USI bsize = ncol * ncol;
1310 const USI bsize2 = ncol * ncol2;
1312 vector<OCP_DBL> bmat(bsize, 0);
1315 for (
USI i = 1; i < ncol; i++) {
1316 bmat[i * ncol + i] = 1;
1318 for (
OCP_USI n = 0; n < numBulk; n++) {
1319 bmat[0] = myBulk.rockC1 * myBulk.rockVpInit[n] - myBulk.vfp[n];
1320 for (
USI i = 0; i < nc; i++) {
1321 bmat[i + 1] = -myBulk.vfi[n * nc + i];
1323 for (
USI i = 0; i < bsize; i++) {
1324 myLS.diagVal[n * bsize + i] = bmat[i];
1331 vector<OCP_DBL> dFdXpB(bsize, 0);
1332 vector<OCP_DBL> dFdXpE(bsize, 0);
1333 vector<OCP_DBL> dFdXsB(bsize2, 0);
1334 vector<OCP_DBL> dFdXsE(bsize2, 0);
1335 vector<bool> phaseExistB(np,
false);
1336 vector<bool> phaseExistE(np,
false);
1338 vector<USI> pEnumComB(np, 0);
1339 vector<USI> pEnumComE(np, 0);
1343 OCP_USI bId_np_j, eId_np_j, uId_np_j;
1344 OCP_DBL kr, mu, xi, xij, rhoP, xiP, muP, rhox, xix, mux;
1350 OCP_USI lastbId = iteratorConn[0].EId;
1351 for (
OCP_USI c = 0; c < numConn; c++) {
1352 bId = iteratorConn[c].BId;
1353 eId = iteratorConn[c].EId;
1355 fill(dFdXpB.begin(), dFdXpB.end(), 0.0);
1356 fill(dFdXpE.begin(), dFdXpE.end(), 0.0);
1357 fill(dFdXsB.begin(), dFdXsB.end(), 0.0);
1358 fill(dFdXsE.begin(), dFdXsE.end(), 0.0);
1359 dGamma =
GRAVITY_FACTOR * (myBulk.depth[bId] - myBulk.depth[eId]);
1361 const USI npB = myBulk.phaseNum[bId] + 1; ncolB = npB;
1362 const USI npE = myBulk.phaseNum[eId] + 1; ncolE = npE;
1364 for (
USI j = 0; j < np; j++) {
1365 phaseExistB[j] = myBulk.phaseExist[bId * np + j];
1366 phaseExistE[j] = myBulk.phaseExist[eId * np + j];
1367 pEnumComB[j] = myBulk.pEnumCom[bId * np + j];
1368 pEnumComE[j] = myBulk.pEnumCom[eId * np + j];
1369 ncolB += pEnumComB[j];
1370 ncolE += pEnumComE[j];
1374 USI jxB = npB;
USI jxE = npE;
1375 for (
USI j = 0; j < np; j++) {
1376 uId = upblock[c * np + j];
1378 phaseExistU = (uId == bId ? phaseExistB[j] : phaseExistE[j]);
1380 jxB += pEnumComB[j];
1381 jxE += pEnumComE[j];
1385 bId_np_j = bId * np + j;
1386 eId_np_j = eId * np + j;
1387 uId_np_j = uId * np + j;
1388 dP = myBulk.Pj[bId_np_j] - myBulk.Pj[eId_np_j] -
1389 upblock_Rho[c * np + j] * dGamma;
1390 xi = myBulk.xi[uId_np_j];
1391 kr = myBulk.kr[uId_np_j];
1392 mu = myBulk.mu[uId_np_j];
1393 muP = myBulk.muP[uId_np_j];
1394 xiP = myBulk.xiP[uId_np_j];
1395 rhoP = myBulk.rhoP[uId_np_j];
1396 transJ = Akd * kr / mu;
1399 for (
USI i = 0; i < nc; i++) {
1400 xij = myBulk.xij[uId_np_j * nc + i];
1401 transIJ = xij * xi * transJ;
1404 dFdXpB[(i + 1) * ncol] += transIJ;
1405 dFdXpE[(i + 1) * ncol] -= transIJ;
1407 tmp = xij * transJ * xiP * dP;
1408 tmp += -transIJ * muP / mu * dP;
1409 if (!phaseExistE[j]) {
1410 tmp += transIJ * (-rhoP * dGamma);
1411 dFdXpB[(i + 1) * ncol] += tmp;
1413 else if (!phaseExistB[j]) {
1414 tmp += transIJ * (-rhoP * dGamma);
1415 dFdXpE[(i + 1) * ncol] += tmp;
1418 dFdXpB[(i + 1) * ncol] +=
1419 transIJ * dGamma * (-myBulk.rhoP[bId_np_j] * myBulk.S[bId_np_j])
1420 / (myBulk.S[bId_np_j] + myBulk.S[eId_np_j]);
1421 dFdXpE[(i + 1) * ncol] +=
1422 transIJ * dGamma * (-myBulk.rhoP[eId_np_j] * myBulk.S[eId_np_j])
1423 / (myBulk.S[bId_np_j] + myBulk.S[eId_np_j]);
1425 dFdXpB[(i + 1) * ncol] += tmp;
1428 dFdXpE[(i + 1) * ncol] += tmp;
1433 USI j1SB = 0;
USI j1SE = 0;
1436 for (
USI j1 = 0; j1 < np; j1++) {
1438 wghtb = 0; wghte = 0;
1439 if (j1 == j && phaseExistE[j]) {
1440 tmp = -dGamma / ((myBulk.S[bId_np_j] + myBulk.S[eId_np_j]) * (myBulk.S[bId_np_j] + myBulk.S[eId_np_j]));
1441 wghtb = tmp * myBulk.rho[bId_np_j] * myBulk.S[eId_np_j];
1442 wghte = tmp * myBulk.rho[eId_np_j] * myBulk.S[bId_np_j];
1445 if (phaseExistB[j1]) {
1446 dFdXsB[(i + 1) * ncolB + j1SB] +=
1447 transIJ * (myBulk.dPcj_dS[bId_np_j * np + j1] + wghtb);
1448 tmp = Akd * xij * xi / mu *
1449 myBulk.dKr_dS[uId_np_j * np + j1] * dP;
1450 dFdXsB[(i + 1) * ncolB + j1SB] += tmp;
1453 if (phaseExistE[j1]) {
1454 dFdXsE[(i + 1) * ncolE + j1SE] -=
1455 transIJ * (myBulk.dPcj_dS[eId_np_j * np + j1] + wghte);
1460 if (!phaseExistE[j]) {
1461 for (
USI k = 0; k < pEnumComB[j]; k++) {
1462 rhox = myBulk.rhox[uId_np_j * nc + k];
1463 xix = myBulk.xix[uId_np_j * nc + k];
1464 mux = myBulk.mux[uId_np_j * nc + k];
1465 tmp = -transIJ * rhox * dGamma;
1466 tmp += xij * transJ * xix * dP;
1467 tmp += -transIJ * mux / mu * dP;
1468 dFdXsB[(i + 1) * ncolB + jxB + k] += tmp;
1471 if (i < pEnumComB[j])
1472 dFdXsB[(i + 1) * ncolB + jxB + i] += xi * transJ * dP;
1475 wghtb = myBulk.S[bId_np_j] / (myBulk.S[bId_np_j] + myBulk.S[eId_np_j]);
1476 wghte = myBulk.S[eId_np_j] / (myBulk.S[bId_np_j] + myBulk.S[eId_np_j]);
1477 for (
USI k = 0; k < pEnumComB[j]; k++) {
1478 rhox = myBulk.rhox[bId_np_j * nc + k] * wghtb;
1479 xix = myBulk.xix[uId_np_j * nc + k];
1480 mux = myBulk.mux[uId_np_j * nc + k];
1481 tmp = -transIJ * rhox * dGamma;
1482 tmp += xij * transJ * xix * dP;
1483 tmp += -transIJ * mux / mu * dP;
1484 dFdXsB[(i + 1) * ncolB + jxB + k] += tmp;
1485 dFdXsE[(i + 1) * ncolE + jxE + k] += -transIJ * dGamma * myBulk.rhox[eId_np_j * nc + k] * wghte;
1488 if (i < pEnumComB[j])
1489 dFdXsB[(i + 1) * ncolB + jxB + i] += xi * transJ * dP;
1494 for (
USI j1 = 0; j1 < np; j1++) {
1496 wghtb = 0; wghte = 0;
1497 if (j1 == j && phaseExistB[j]) {
1498 tmp = -dGamma / ((myBulk.S[bId_np_j] + myBulk.S[eId_np_j]) * (myBulk.S[bId_np_j] + myBulk.S[eId_np_j]));
1499 wghtb = tmp * myBulk.rho[bId_np_j] * myBulk.S[eId_np_j];
1500 wghte = tmp * myBulk.rho[eId_np_j] * myBulk.S[bId_np_j];
1503 if (phaseExistB[j1]) {
1504 dFdXsB[(i + 1) * ncolB + j1SB] +=
1505 transIJ * (myBulk.dPcj_dS[bId_np_j * np + j1] + wghtb);
1508 if (phaseExistE[j1]) {
1509 dFdXsE[(i + 1) * ncolE + j1SE] -=
1510 transIJ * (myBulk.dPcj_dS[eId_np_j * np + j1] + wghte);
1511 tmp = Akd * xij * xi / mu *
1512 myBulk.dKr_dS[uId_np_j * np + j1] * dP;
1513 dFdXsE[(i + 1) * ncolE + j1SE] += tmp;
1518 if (!phaseExistB[j]) {
1519 for (
USI k = 0; k < pEnumComE[j]; k++) {
1520 rhox = myBulk.rhox[uId_np_j * nc + k];
1521 xix = myBulk.xix[uId_np_j * nc + k];
1522 mux = myBulk.mux[uId_np_j * nc + k];
1523 tmp = -transIJ * rhox * dGamma;
1524 tmp += xij * transJ * xix * dP;
1525 tmp += -transIJ * mux / mu * dP;
1526 dFdXsE[(i + 1) * ncolE + jxE + k] += tmp;
1529 if (i < pEnumComE[j])
1530 dFdXsE[(i + 1) * ncolE + jxE + i] += xi * transJ * dP;
1533 wghtb = myBulk.S[bId_np_j] / (myBulk.S[bId_np_j] + myBulk.S[eId_np_j]);
1534 wghte = myBulk.S[eId_np_j] / (myBulk.S[bId_np_j] + myBulk.S[eId_np_j]);
1535 for (
USI k = 0; k < pEnumComE[j]; k++) {
1536 rhox = myBulk.rhox[eId_np_j * nc + k] * wghte;
1537 xix = myBulk.xix[uId_np_j * nc + k];
1538 mux = myBulk.mux[uId_np_j * nc + k];
1539 tmp = -transIJ * rhox * dGamma;
1540 tmp += xij * transJ * xix * dP;
1541 tmp += -transIJ * mux / mu * dP;
1542 dFdXsE[(i + 1) * ncolE + jxE + k] += tmp;
1543 dFdXsB[(i + 1) * ncolB + jxB + k] += -transIJ * dGamma * myBulk.rhox[bId_np_j * nc + k] * wghtb;
1546 if (i < pEnumComE[j])
1547 dFdXsE[(i + 1) * ncolE + jxE + i] += xi * transJ * dP;
1551 jxB += pEnumComB[j];
1552 jxE += pEnumComE[j];
1555 USI diagptr = myLS.diagPtr[bId];
1557 if (bId != lastbId) {
1559 assert(myLS.val[bId].size() == diagptr * bsize);
1561 myLS.val[bId].insert(myLS.val[bId].end(), myLS.diagVal.data() +
id,
1562 myLS.diagVal.data() +
id + bsize);
1570 DaABpbC(ncol, ncol, ncolB, 1, dFdXsB.data(), &myBulk.dSec_dPri[myBulk.dSdPindex[bId]], 1,
1572 Dscalar(bsize, dt, bmat.data());
1575 for (
USI i = 0; i < bsize; i++) {
1576 myLS.val[bId][diagptr * bsize + i] += bmat[i];
1580 Dscalar(bsize, -1, bmat.data());
1581 myLS.val[eId].insert(myLS.val[eId].end(), bmat.begin(), bmat.end());
1584 if (!
CheckNan(bmat.size(), &bmat[0]))
1593 DaABpbC(ncol, ncol, ncolE, 1, dFdXsE.data(), &myBulk.dSec_dPri[myBulk.dSdPindex[eId]], 1,
1595 Dscalar(bsize, dt, bmat.data());
1598 myLS.val[bId].insert(myLS.val[bId].end(), bmat.begin(), bmat.end());
1600 Dscalar(bsize, -1, bmat.data());
1601 for (
USI i = 0; i < bsize; i++) {
1602 myLS.diagVal[eId * bsize + i] += bmat[i];
1606 if (!
CheckNan(bmat.size(), &bmat[0]))
1613 for (
OCP_USI n = 0; n < numBulk; n++) {
1614 if (myLS.val[n].size() == myLS.diagPtr[n] * bsize)
1615 myLS.val[n].insert(myLS.val[n].end(), myLS.diagVal.data() + n * bsize,
1616 myLS.diagVal.data() + n * bsize + bsize);
1626 const USI np = myBulk.numPhase;
1627 const USI nc = myBulk.numCom;
1628 const USI nch = nc - 1;
1629 const USI ncol = nc + 1;
1630 const USI ncol2 = np * nc + np;
1631 const USI bsize = ncol * ncol;
1632 const USI bsize2 = ncol * ncol2;
1634 vector<OCP_DBL> bmat(bsize, 0);
1637 for (
USI i = 1; i < ncol; i++) {
1638 bmat[i * ncol + i] = 1;
1640 for (
OCP_USI n = 0; n < numBulk; n++) {
1641 bmat[0] = myBulk.rockC1 * myBulk.rockVpInit[n] - myBulk.vfp[n];
1642 for (
USI i = 0; i < nc; i++) {
1643 bmat[i + 1] = -myBulk.vfi[n * nc + i];
1645 for (
USI i = 0; i < bsize; i++) {
1646 myLS.diagVal[n * bsize + i] = bmat[i];
1648 myLS.b[n * ncol] -= myBulk.resPc[n];
1654 vector<OCP_DBL> dFdXpB(bsize, 0);
1655 vector<OCP_DBL> dFdXpE(bsize, 0);
1656 vector<OCP_DBL> dFdXsB(bsize2, 0);
1657 vector<OCP_DBL> dFdXsE(bsize2, 0);
1658 vector<bool> phaseExistB(np,
false);
1659 vector<bool> phaseExistE(np,
false);
1661 vector<USI> pEnumComB(np, 0);
1662 vector<USI> pEnumComE(np, 0);
1666 OCP_USI bId_np_j, eId_np_j, uId_np_j;
1667 OCP_DBL kr, mu, xi, xij, rhoP, xiP, muP, rhox, xix, mux;
1673 OCP_USI lastbId = iteratorConn[0].EId;
1674 for (
OCP_USI c = 0; c < numConn; c++) {
1675 bId = iteratorConn[c].BId;
1676 eId = iteratorConn[c].EId;
1678 fill(dFdXpB.begin(), dFdXpB.end(), 0.0);
1679 fill(dFdXpE.begin(), dFdXpE.end(), 0.0);
1680 fill(dFdXsB.begin(), dFdXsB.end(), 0.0);
1681 fill(dFdXsE.begin(), dFdXsE.end(), 0.0);
1682 dGamma =
GRAVITY_FACTOR * (myBulk.depth[bId] - myBulk.depth[eId]);
1684 const USI npB = myBulk.phaseNum[bId] + 1; ncolB = npB;
1685 const USI npE = myBulk.phaseNum[eId] + 1; ncolE = npE;
1687 for (
USI j = 0; j < np; j++) {
1688 phaseExistB[j] = myBulk.phaseExist[bId * np + j];
1689 phaseExistE[j] = myBulk.phaseExist[eId * np + j];
1690 pEnumComB[j] = myBulk.pEnumCom[bId * np + j];
1691 pEnumComE[j] = myBulk.pEnumCom[eId * np + j];
1692 ncolB += pEnumComB[j];
1693 ncolE += pEnumComE[j];
1697 USI jxB = npB;
USI jxE = npE;
1698 for (
USI j = 0; j < np; j++) {
1699 uId = upblock[c * np + j];
1701 phaseExistU = (uId == bId ? phaseExistB[j] : phaseExistE[j]);
1703 jxB += pEnumComB[j];
1704 jxE += pEnumComE[j];
1708 bId_np_j = bId * np + j;
1709 eId_np_j = eId * np + j;
1710 uId_np_j = uId * np + j;
1711 dP = myBulk.Pj[bId_np_j] - myBulk.Pj[eId_np_j] -
1712 upblock_Rho[c * np + j] * dGamma;
1715 xi = myBulk.xi[uId_np_j];
1716 kr = myBulk.kr[uId_np_j];
1717 mu = myBulk.mu[uId_np_j];
1718 muP = myBulk.muP[uId_np_j];
1719 xiP = myBulk.xiP[uId_np_j];
1720 rhoP = myBulk.rhoP[uId_np_j];
1721 transJ = Akd * kr / mu;
1724 for (
USI i = 0; i < nc; i++) {
1725 xij = myBulk.xij[uId_np_j * nc + i];
1726 transIJ = xij * xi * transJ;
1729 dFdXpB[(i + 1) * ncol] += transIJ;
1730 dFdXpE[(i + 1) * ncol] -= transIJ;
1733 tmp = xij * transJ * xiP * dP;
1734 tmp += -transIJ * muP / mu * dP;
1735 if (!phaseExistE[j]) {
1736 tmp += transIJ * (-rhoP * dGamma);
1737 dFdXpB[(i + 1) * ncol] += tmp;
1739 else if (!phaseExistB[j]) {
1740 tmp += transIJ * (-rhoP * dGamma);
1741 dFdXpE[(i + 1) * ncol] += tmp;
1744 dFdXpB[(i + 1) * ncol] +=
1745 transIJ * (-myBulk.rhoP[bId_np_j] * dGamma) / 2;
1746 dFdXpE[(i + 1) * ncol] +=
1747 transIJ * (-myBulk.rhoP[eId_np_j] * dGamma) / 2;
1749 dFdXpB[(i + 1) * ncol] += tmp;
1752 dFdXpE[(i + 1) * ncol] += tmp;
1757 USI j1SB = 0;
USI j1SE = 0;
1760 for (
USI j1 = 0; j1 < np; j1++) {
1761 if (phaseExistB[j1]) {
1762 dFdXsB[(i + 1) * ncolB + j1SB] +=
1763 transIJ * myBulk.dPcj_dS[bId_np_j * np + j1];
1764 tmp = Akd * xij * xi / mu *
1765 myBulk.dKr_dS[uId_np_j * np + j1] * dP;
1766 dFdXsB[(i + 1) * ncolB + j1SB] += tmp;
1769 if (phaseExistE[j1]) {
1770 dFdXsE[(i + 1) * ncolE + j1SE] -=
1771 transIJ * myBulk.dPcj_dS[eId_np_j * np + j1];
1776 if (!phaseExistE[j]) {
1777 for (
USI k = 0; k < pEnumComB[j]; k++) {
1778 rhox = myBulk.rhox[uId_np_j * nc + k];
1779 xix = myBulk.xix[uId_np_j * nc + k];
1780 mux = myBulk.mux[uId_np_j * nc + k];
1781 tmp = -transIJ * rhox * dGamma;
1782 tmp += xij * transJ * xix * dP;
1783 tmp += -transIJ * mux / mu * dP;
1784 tmp -= transIJ * dP / myBulk.nj[uId_np_j];
1785 dFdXsB[(i + 1) * ncolB + jxB + k] += tmp;
1788 if (i < pEnumComB[j])
1789 dFdXsB[(i + 1) * ncolB + jxB + i] += xi * transJ * dP / myBulk.nj[uId_np_j];
1792 for (
USI k = 0; k < pEnumComB[j]; k++) {
1793 rhox = myBulk.rhox[bId_np_j * nc + k] / 2;
1794 xix = myBulk.xix[uId_np_j * nc + k];
1795 mux = myBulk.mux[uId_np_j * nc + k];
1796 tmp = -transIJ * rhox * dGamma;
1797 tmp += xij * transJ * xix * dP;
1798 tmp += -transIJ * mux / mu * dP;
1799 tmp -= transIJ * dP / myBulk.nj[uId_np_j];
1800 dFdXsB[(i + 1) * ncolB + jxB + k] += tmp;
1801 dFdXsE[(i + 1) * ncolE + jxE + k] += -transIJ * myBulk.rhox[eId_np_j * nc + k] / 2 * dGamma;
1804 if (i < pEnumComB[j])
1805 dFdXsB[(i + 1) * ncolB + jxB + i] += xi * transJ * dP / myBulk.nj[uId_np_j];
1810 for (
USI j1 = 0; j1 < np; j1++) {
1811 if (phaseExistB[j1]) {
1812 dFdXsB[(i + 1) * ncolB + j1SB] +=
1813 transIJ * myBulk.dPcj_dS[bId_np_j * np + j1];
1816 if (phaseExistE[j1]) {
1817 dFdXsE[(i + 1) * ncolE + j1SE] -=
1818 transIJ * myBulk.dPcj_dS[eId_np_j * np + j1];
1819 tmp = Akd * xij * xi / mu *
1820 myBulk.dKr_dS[uId_np_j * np + j1] * dP;
1821 dFdXsE[(i + 1) * ncolE + j1SE] += tmp;
1826 if (!phaseExistB[j]) {
1827 for (
USI k = 0; k < pEnumComE[j]; k++) {
1828 rhox = myBulk.rhox[uId_np_j * nc + k];
1829 xix = myBulk.xix[uId_np_j * nc + k];
1830 mux = myBulk.mux[uId_np_j * nc + k];
1831 tmp = -transIJ * rhox * dGamma;
1832 tmp += xij * transJ * xix * dP;
1833 tmp += -transIJ * mux / mu * dP;
1834 tmp -= transIJ * dP / myBulk.nj[uId_np_j];
1835 dFdXsE[(i + 1) * ncolE + jxE + k] += tmp;
1838 if (i < pEnumComE[j])
1839 dFdXsE[(i + 1) * ncolE + jxE + i] += xi * transJ * dP / myBulk.nj[uId_np_j];
1842 for (
USI k = 0; k < pEnumComE[j]; k++) {
1843 rhox = myBulk.rhox[eId_np_j * nc + k] / 2;
1844 xix = myBulk.xix[uId_np_j * nc + k];
1845 mux = myBulk.mux[uId_np_j * nc + k];
1846 tmp = -transIJ * rhox * dGamma;
1847 tmp += xij * transJ * xix * dP;
1848 tmp += -transIJ * mux / mu * dP;
1849 tmp -= transIJ * dP / myBulk.nj[uId_np_j];
1850 dFdXsE[(i + 1) * ncolE + jxE + k] += tmp;
1851 dFdXsB[(i + 1) * ncolB + jxB + k] += -transIJ * myBulk.rhox[bId_np_j * nc + k] / 2 * dGamma;
1854 if (i < pEnumComE[j])
1855 dFdXsE[(i + 1) * ncolE + jxE + i] += xi * transJ * dP / myBulk.nj[uId_np_j];
1859 jxB += pEnumComB[j];
1860 jxE += pEnumComE[j];
1863 USI diagptr = myLS.diagPtr[bId];
1865 if (bId != lastbId) {
1867 assert(myLS.val[bId].size() == diagptr * bsize);
1869 myLS.val[bId].insert(myLS.val[bId].end(), myLS.diagVal.data() +
id,
1870 myLS.diagVal.data() +
id + bsize);
1878 DaAxpby(ncol, ncolB, -1.0, dFdXsB.data(),
1879 &myBulk.res_n[myBulk.resIndex[bId]], 1.0, &myLS.b[bId * ncol]);
1888 DaABpbC(ncol, ncol, ncolB, 1, dFdXsB.data(), &myBulk.dSec_dPri[myBulk.dSdPindex[bId]], 1,
1890 Dscalar(bsize, dt, bmat.data());
1893 for (
USI i = 0; i < bsize; i++) {
1894 myLS.val[bId][diagptr * bsize + i] += bmat[i];
1898 Dscalar(bsize, -1, bmat.data());
1899 myLS.val[eId].insert(myLS.val[eId].end(), bmat.begin(), bmat.end());
1902 if (!
CheckNan(bmat.size(), &bmat[0]))
1911 DaAxpby(ncol, ncolE, -1.0, dFdXsE.data(),
1912 &myBulk.res_n[myBulk.resIndex[eId]], 1.0, &myLS.b[eId * ncol]);
1921 DaABpbC(ncol, ncol, ncolE, 1, dFdXsE.data(), &myBulk.dSec_dPri[myBulk.dSdPindex[eId]], 1,
1923 Dscalar(bsize, dt, bmat.data());
1927 myLS.val[bId].insert(myLS.val[bId].end(), bmat.begin(), bmat.end());
1929 Dscalar(bsize, -1, bmat.data());
1930 for (
USI i = 0; i < bsize; i++) {
1931 myLS.diagVal[eId * bsize + i] += bmat[i];
1935 if (!
CheckNan(bmat.size(), &bmat[0]))
1942 for (
OCP_USI n = 0; n < numBulk; n++) {
1943 if (myLS.val[n].size() == myLS.diagPtr[n] * bsize)
1944 myLS.val[n].insert(myLS.val[n].end(), myLS.diagVal.data() + n * bsize,
1945 myLS.diagVal.data() + n * bsize + bsize);
1954 void BulkConn::SetupFIMBulk(
Bulk& myBulk,
const bool& NRflag)
const
1956 const USI np = myBulk.numPhase;
1957 const USI nc = myBulk.numCom;
1959 myBulk.FIMBulk.clear();
1960 fill(myBulk.map_Bulk2FIM.begin(), myBulk.map_Bulk2FIM.end(), -1);
1965 for (
OCP_USI n = 0; n < numBulk; n++) {
1970 for (
USI j = 0; j < np; j++) {
1971 if (myBulk.cfl[bIdp + j] > 0.8) {
1978 if ((fabs(myBulk.vf[n] - myBulk.rockVp[n]) / myBulk.rockVp[n]) > 1E-3) {
1984 if (!flag && NRflag) {
1987 if (fabs(myBulk.dPNR[n] / myBulk.P[n]) > 1E-3) {
1995 for (
USI i = 0; i < myBulk.numCom; i++) {
1996 if (fabs(myBulk.dNNR[bIdc + i] / myBulk.Ni[bIdc + i]) > 1E-3) {
2010 for (
auto& v : neighbor[n]) {
2012 myBulk.map_Bulk2FIM[v] = 1;
2018 for (
auto& p : myBulk.wellBulkId) {
2019 for (
auto& v : neighbor[p]) {
2020 for (
auto& v1 : neighbor[v])
2021 myBulk.map_Bulk2FIM[v1] = 1;
2025 for (
OCP_USI n = 0; n < myBulk.numBulk; n++) {
2026 if (myBulk.map_Bulk2FIM[n] > 0) {
2027 myBulk.map_Bulk2FIM[n] = iter;
2029 myBulk.FIMBulk.push_back(n);
2032 myBulk.numFIMBulk = myBulk.FIMBulk.size();
2042 void BulkConn::AddFIMBulk(
Bulk& myBulk)
2044 const USI np = myBulk.numPhase;
2045 const USI nc = myBulk.numCom;
2047 fill(myBulk.map_Bulk2FIM.begin(), myBulk.map_Bulk2FIM.end(), -1);
2052 for (
OCP_USI n = 0; n < numBulk; n++) {
2065 for (
USI i = 0; i < nc; i++) {
2066 if (myBulk.Ni[bIdc + i] < 0) {
2074 if ((fabs(myBulk.vf[n] - myBulk.rockVp[n]) / myBulk.rockVp[n]) > 0.001) {
2081 for (
auto& v : neighbor[n]) {
2083 myBulk.map_Bulk2FIM[v] = 1;
2088 for (
USI i = 0; i < myBulk.numFIMBulk; i++) {
2089 myBulk.map_Bulk2FIM[myBulk.FIMBulk[i]] = 1;
2093 for (
auto& p : myBulk.wellBulkId) {
2094 for (
auto& v : neighbor[p]) {
2095 for (
auto& v1 : neighbor[v])
2096 myBulk.map_Bulk2FIM[v1] = 1;
2100 myBulk.FIMBulk.clear();
2102 for (
OCP_USI n = 0; n < myBulk.numBulk; n++) {
2103 if (myBulk.map_Bulk2FIM[n] > 0) {
2104 myBulk.map_Bulk2FIM[n] = iter;
2106 myBulk.FIMBulk.push_back(n);
2109 myBulk.numFIMBulk = myBulk.FIMBulk.size();
2113 void BulkConn::SetupFIMBulkBoundAIMs(
Bulk& myBulk)
2117 for (
USI fn = 0; fn < myBulk.numFIMBulk; fn++) {
2118 n = myBulk.FIMBulk[fn];
2119 for (
auto& v : neighbor[n]) {
2120 if (myBulk.map_Bulk2FIM[v] < 0) {
2121 myBulk.FIMBulk.push_back(v);
2122 myBulk.map_Bulk2FIM[v] = myBulk.numFIMBulk + iter;
2131 neighborNumGacc.resize(numBulk, 0);
2133 for (
OCP_USI n = 1; n < numBulk - 1; n++) {
2134 neighborNumGacc[n] += neighborNum[n - 1] - selfPtr[n - 1] - 1;
2135 neighborNumGacc[n + 1] += neighborNumGacc[n];
2137 neighborNumGacc[numBulk - 1] = numConn;
2142 for (
USI bIde = 0; bIde < myBulk.numFIMBulk; bIde++) {
2143 OCP_USI bId = myBulk.FIMBulk[bIde];
2145 for (
auto& v : neighbor[bId]) {
2146 if (myBulk.map_Bulk2FIM[v] > -1) {
2147 myLS.colId[bIde].push_back(myBulk.map_Bulk2FIM[v]);
2149 myLS.diagPtr[bIde] = iter;
2155 myLS.dim = myBulk.numFIMBulk;
2163 const USI np = myBulk.numPhase;
2164 const USI nc = myBulk.numCom;
2165 const USI ncol = nc + 1;
2166 const USI ncol2 = np * nc + np;
2167 const USI bsize = ncol * ncol;
2168 const USI bsize2 = ncol * ncol2;
2170 vector<OCP_DBL> bmat(bsize, 0);
2173 for (
USI i = 1; i < nc + 1; i++) {
2174 bmat[i * ncol + i] = 1;
2176 for (
OCP_USI fn = 0; fn < myBulk.numFIMBulk; fn++) {
2177 OCP_USI n = myBulk.FIMBulk[fn];
2178 bmat[0] = myBulk.rockC1 * myBulk.rockVpInit[n] - myBulk.vfp[n];
2179 for (
USI i = 0; i < nc; i++) {
2180 bmat[i + 1] = -myBulk.vfi[n * nc + i];
2182 for (
USI i = 0; i < bsize; i++) {
2183 myLS.diagVal[fn * bsize + i] = bmat[i];
2189 vector<OCP_DBL> dFdXpB(bsize, 0);
2190 vector<OCP_DBL> dFdXpE(bsize, 0);
2191 vector<OCP_DBL> dFdXsB(bsize2, 0);
2192 vector<OCP_DBL> dFdXsE(bsize2, 0);
2199 OCP_DBL kr, mu, xi, xij, rhoP, xiP, muP, rhox, xix, mux;
2205 for (
OCP_USI fn = 0; fn < myBulk.numFIMBulk; fn++) {
2209 bId = myBulk.FIMBulk[fn];
2210 for (
auto& v : neighbor[bId]) {
2215 minId = (bId > eId ? eId : bId);
2216 maxId = (bId < eId ? eId : bId);
2217 c = neighborNumGacc[minId];
2219 for (
USI i = selfPtr[minId] + 1; i < neighborNum[minId]; i++) {
2220 if (neighbor[minId][i] == maxId) {
2228 eIde = myBulk.map_Bulk2FIM[eId];
2229 if (eIde < 0) flagFIM =
false;
2230 else flagFIM =
true;
2232 fill(dFdXpB.begin(), dFdXpB.end(), 0.0);
2233 fill(dFdXpE.begin(), dFdXpE.end(), 0.0);
2234 fill(dFdXsB.begin(), dFdXsB.end(), 0.0);
2235 fill(dFdXsE.begin(), dFdXsE.end(), 0.0);
2236 dGamma =
GRAVITY_FACTOR * (myBulk.depth[bId] - myBulk.depth[eId]);
2238 for (
USI j = 0; j < np; j++) {
2239 uId = upblock[c * np + j];
2240 uId_np_j = uId * np + j;
2241 if (!myBulk.phaseExist[uId_np_j])
continue;
2242 dP = myBulk.Pj[bId * np + j] - myBulk.Pj[eId * np + j] -
2243 myBulk.rho[bId * np + j] * dGamma;
2244 xi = myBulk.xi[uId_np_j];
2245 kr = myBulk.kr[uId_np_j];
2246 mu = myBulk.mu[uId_np_j];
2247 transJ = Akd * kr / mu;
2249 uIde = (bId == uId ? bIde : eIde);
2251 uIde_np_j = uIde * np + j;
2252 muP = myBulk.muP[uIde_np_j];
2253 xiP = myBulk.xiP[uIde_np_j];
2254 rhoP = myBulk.rhoP[uIde_np_j];
2257 for (
USI i = 0; i < nc; i++) {
2258 xij = myBulk.xij[uId_np_j * nc + i];
2259 transIJ = xij * xi * transJ;
2262 dFdXpB[(i + 1) * ncol] += transIJ;
2264 dFdXpE[(i + 1) * ncol] -= transIJ;
2267 tmp = transIJ * (-rhoP * dGamma);
2268 tmp += xij * transJ * xiP * dP;
2269 tmp += -transIJ * muP / mu * dP;
2271 dFdXpB[(i + 1) * ncol] += tmp;
2274 dFdXpE[(i + 1) * ncol] += tmp;
2279 for (
USI k = 0; k < np; k++) {
2280 dFdXsB[(i + 1) * ncol2 + k] +=
2281 transIJ * myBulk.dPcj_dS[bIde * np * np + j * np + k];
2283 dFdXsE[(i + 1) * ncol2 + k] -=
2284 transIJ * myBulk.dPcj_dS[eIde * np * np + j * np + k];
2287 tmp = Akd * xij * xi / mu *
2288 myBulk.dKr_dS[uIde * np * np + j * np + k] * dP;
2291 dFdXsB[(i + 1) * ncol2 + k] += tmp;
2294 dFdXsE[(i + 1) * ncol2 + k] += tmp;
2301 for (
USI k = 0; k < nc; k++) {
2302 rhox = myBulk.rhox[uIde_np_j * nc + k];
2303 xix = myBulk.xix[uIde_np_j * nc + k];
2304 mux = myBulk.mux[uIde_np_j * nc + k];
2305 tmp = -transIJ * rhox * dGamma;
2306 tmp += xij * transJ * xix * dP;
2307 tmp += -transIJ * mux / mu * dP;
2309 tmp += xi * transJ * dP;
2312 dFdXsB[(i + 1) * ncol2 + np + j * nc + k] += tmp;
2315 dFdXsE[(i + 1) * ncol2 + np + j * nc + k] += tmp;
2321 USI diagptr = myLS.diagPtr[bIde];
2326 DaABpbC(ncol, ncol, ncol2, 1, dFdXsB.data(), &myBulk.dSec_dPri[bIde * bsize2], 1,
2328 Dscalar(bsize, dt, bmat.data());
2330 if (count < diagptr) {
2332 for (
USI i = 0; i < bsize; i++) {
2333 myLS.diagVal[bIde * bsize + i] += bmat[i];
2336 else if (count == diagptr) {
2338 myLS.val[bIde].insert(myLS.val[bIde].end(), myLS.diagVal.data() +
id,
2339 myLS.diagVal.data() +
id + bsize);
2341 for (
USI i = 0; i < bsize; i++) {
2342 myLS.val[bIde][diagptr * bsize + i] += bmat[i];
2348 for (
USI i = 0; i < bsize; i++) {
2349 myLS.val[bIde][diagptr * bsize + i] += bmat[i];
2356 DaABpbC(ncol, ncol, ncol2, 1, dFdXsE.data(), &myBulk.dSec_dPri[eIde * bsize2], 1,
2358 Dscalar(bsize, dt, bmat.data());
2361 myLS.val[bIde].insert(myLS.val[bIde].end(), bmat.begin(), bmat.end());
2367 for (
OCP_USI n = 0; n < myBulk.numFIMBulk; n++) {
2368 if (myLS.val[n].size() == myLS.diagPtr[n] * bsize)
2369 myLS.val[n].insert(myLS.val[n].end(), myLS.diagVal.data() + n * bsize,
2370 myLS.diagVal.data() + (n + 1) * bsize);
2378 const USI np = myBulk.numPhase;
2379 const USI nc = myBulk.numCom;
2380 const USI len = nc + 1;
2385 for (
OCP_USI fn = 0; fn < myBulk.numFIMBulk; fn++) {
2387 OCP_USI n = myBulk.FIMBulk[fn];
2392 res[bIde] = myBulk.rockVp[n] - myBulk.vf[n];
2393 for (
USI i = 0; i < nc; i++) {
2394 res[bIde + 1 + i] = myBulk.Ni[bIdc + i] - myBulk.lNi[bIdc + i];
2398 OCP_USI bId_np_j, eId_np_j, uId_np_j;
2400 OCP_DBL Pbegin, Pend, rho, dP;
2406 for (
OCP_USI fn = 0; fn < myBulk.numFIMBulk; fn++) {
2408 bId = myBulk.FIMBulk[fn];
2409 for (
auto& v : neighbor[bId]) {
2415 minId = (bId > eId ? eId : bId);
2416 maxId = (bId < eId ? eId : bId);
2417 c = neighborNumGacc[minId];
2419 for (
USI i = selfPtr[minId] + 1; i < neighborNum[minId]; i++) {
2420 if (neighbor[minId][i] == maxId) {
2427 for (
USI j = 0; j < np; j++) {
2428 bId_np_j = bId * np + j;
2429 eId_np_j = eId * np + j;
2431 bool exbegin = myBulk.phaseExist[bId_np_j];
2432 bool exend = myBulk.phaseExist[eId_np_j];
2434 if ((exbegin) && (exend)) {
2435 Pbegin = myBulk.Pj[bId_np_j];
2436 Pend = myBulk.Pj[eId_np_j];
2437 rho = (myBulk.rho[bId_np_j] + myBulk.rho[eId_np_j]) / 2;
2439 else if (exbegin && (!exend)) {
2440 Pbegin = myBulk.Pj[bId_np_j];
2441 Pend = myBulk.P[eId];
2442 rho = myBulk.rho[bId_np_j];
2444 else if ((!exbegin) && (exend)) {
2445 Pbegin = myBulk.P[bId];
2446 Pend = myBulk.Pj[eId_np_j];
2447 rho = myBulk.rho[eId_np_j];
2450 upblock[c * np + j] = bId;
2462 upblock[c * np + j] = uId;
2464 uId_np_j = uId * np + j;
2465 if (!myBulk.phaseExist[uId_np_j])
continue;
2466 tmp = dt * Akd * myBulk.xi[uId_np_j] *
2467 myBulk.kr[uId_np_j] / myBulk.mu[uId_np_j] * dP;
2468 for (
USI i = 0; i < nc; i++) {
2469 dNi = tmp * myBulk.xij[uId_np_j * nc + i];
2470 res[bIde * len + 1 + i] += dNi;
2481 const USI np = myBulk.numPhase;
2482 const USI nc = myBulk.numCom;
2483 const USI len = nc + 1;
2487 for (
OCP_USI fn = 0; fn < myBulk.numFIMBulk; fn++) {
2489 OCP_USI n = myBulk.FIMBulk[fn];
2493 res[bId] = myBulk.rockVp[n] - myBulk.vf[n];
2494 for (
USI i = 0; i < nc; i++) {
2495 res[bId + 1 + i] = myBulk.Ni[bIdc + i] - myBulk.lNi[bIdc + i];
2499 OCP_USI bId_np_j, eId_np_j, uId_np_j;
2501 OCP_DBL Pbegin, Pend, rho, dP;
2507 for (
OCP_USI fn = 0; fn < myBulk.numFIMBulk; fn++) {
2508 bId = myBulk.FIMBulk[fn];
2509 for (
auto& v : neighbor[bId]) {
2515 minId = (bId > eId ? eId : bId);
2516 maxId = (bId < eId ? eId : bId);
2517 c = neighborNumGacc[minId];
2519 for (
USI i = selfPtr[minId] + 1; i < neighborNum[minId]; i++) {
2520 if (neighbor[minId][i] == maxId) {
2527 for (
USI j = 0; j < np; j++) {
2528 bId_np_j = bId * np + j;
2529 eId_np_j = eId * np + j;
2531 bool exbegin = myBulk.phaseExist[bId_np_j];
2532 bool exend = myBulk.phaseExist[eId_np_j];
2534 if ((exbegin) && (exend)) {
2535 Pbegin = myBulk.Pj[bId_np_j];
2536 Pend = myBulk.Pj[eId_np_j];
2537 rho = (myBulk.rho[bId_np_j] + myBulk.rho[eId_np_j]) / 2;
2539 else if (exbegin && (!exend)) {
2540 Pbegin = myBulk.Pj[bId_np_j];
2541 Pend = myBulk.P[eId];
2542 rho = myBulk.rho[bId_np_j];
2544 else if ((!exbegin) && (exend)) {
2545 Pbegin = myBulk.P[bId];
2546 Pend = myBulk.Pj[eId_np_j];
2547 rho = myBulk.rho[eId_np_j];
2560 uId_np_j = uId * np + j;
2561 if (!myBulk.phaseExist[uId_np_j])
continue;
2562 tmp = dt * Akd * myBulk.xi[uId_np_j] *
2563 myBulk.kr[uId_np_j] / myBulk.mu[uId_np_j] * dP;
2564 for (
USI i = 0; i < nc; i++) {
2565 dNi = tmp * myBulk.xij[uId_np_j * nc + i];
2566 res[bId * len + 1 + i] += dNi;
2580 const USI np = myBulk.numPhase;
2581 const USI nc = myBulk.numCom;
2582 const USI ncol = nc + 1;
2583 const USI ncol2 = np * nc + np;
2584 const USI bsize = ncol * ncol;
2585 const USI bsize2 = ncol * ncol2;
2587 vector<OCP_DBL> bmat(bsize, 0);
2588 for (
USI i = 1; i < nc + 1; i++) {
2589 bmat[i * ncol + i] = 1;
2591 vector<OCP_DBL> bmatTmp(bmat);
2593 for (
OCP_USI n = 0; n < numBulk; n++) {
2594 Vp0 = myBulk.rockVpInit[n];
2595 Vp = myBulk.rockVp[n];
2596 vfp = myBulk.vfp[n];
2598 if (myBulk.map_Bulk2FIM[n] < 0 || myBulk.map_Bulk2FIM[n] >= myBulk.numFIMBulk) {
2600 bmat[0] = cr * Vp0 - vfp;
2603 res[n * ncol] = bmat[0] * (myBulk.lP[n] - myBulk.P[n]) + dt * (vf - Vp);
2606 for (
USI i = 0; i < bsize; i++) {
2607 myLS.diagVal[n * bsize + i] = bmat[i];
2612 bmatTmp[0] = cr * Vp0 - vfp;
2613 for (
USI i = 0; i < nc; i++) {
2614 bmatTmp[i + 1] = -myBulk.vfi[n * nc + i];
2616 for (
USI i = 0; i < bsize; i++) {
2617 myLS.diagVal[n * bsize + i] = bmatTmp[i];
2626 OCP_DBL valup, rhsup, valdown, rhsdown;
2628 OCP_DBL kr, mu, xi, xij, rhoP, xiP, muP, rhox, xix, mux;
2631 bool bIdFIM, eIdFIM;
2633 OCP_USI FIMbId, FIMeId, FIMbIde, FIMeIde;
2639 vector<OCP_DBL> dFdXpB(bsize, 0);
2640 vector<OCP_DBL> dFdXpE(bsize, 0);
2641 vector<OCP_DBL> dFdXsB(bsize2, 0);
2642 vector<OCP_DBL> dFdXsE(bsize2, 0);
2643 vector<OCP_DBL> IMPECbmat(bsize, 0);
2646 OCP_USI lastbId = iteratorConn[0].EId;
2647 for (
OCP_USI c = 0; c < numConn; c++) {
2648 bId = iteratorConn[c].BId;
2649 eId = iteratorConn[c].EId;
2652 bIde = myBulk.map_Bulk2FIM[bId];
2653 eIde = myBulk.map_Bulk2FIM[eId];
2655 bIdFIM = eIdFIM =
false;
2656 if (bIde > -1 && bIde < myBulk.numFIMBulk) bIdFIM =
true;
2657 if (eIde > -1 && eIde < myBulk.numFIMBulk) eIdFIM =
true;
2659 if (bIdFIM || eIdFIM) {
2661 fill(dFdXpB.begin(), dFdXpB.end(), 0.0);
2662 fill(dFdXpE.begin(), dFdXpE.end(), 0.0);
2663 fill(dFdXsB.begin(), dFdXsB.end(), 0.0);
2664 fill(dFdXsE.begin(), dFdXsE.end(), 0.0);
2678 dGamma =
GRAVITY_FACTOR * (myBulk.depth[FIMbId] - myBulk.depth[FIMeId]);
2679 for (
USI j = 0; j < np; j++) {
2680 uId = upblock[c * np + j];
2681 uId_np_j = uId * np + j;
2682 uIde = myBulk.map_Bulk2FIM[uId];
2683 uIde_np_j = uIde * np + j;
2685 if (!myBulk.phaseExist[uId_np_j])
continue;
2686 dP = myBulk.Pj[FIMbId * np + j] - myBulk.Pj[FIMeId * np + j] -
2687 myBulk.rho[FIMbId * np + j] * dGamma;
2688 xi = myBulk.xi[uId_np_j];
2689 kr = myBulk.kr[uId_np_j];
2690 mu = myBulk.mu[uId_np_j];
2691 muP = myBulk.muP[uIde_np_j];
2692 xiP = myBulk.xiP[uIde_np_j];
2693 rhoP = myBulk.rhoP[uIde_np_j];
2694 transJ = Akd * kr / mu;
2696 for (
USI i = 0; i < nc; i++) {
2697 xij = myBulk.xij[uId_np_j * nc + i];
2699 transIJ = xij * xi * transJ;
2702 dFdXpB[(i + 1) * ncol] += transIJ;
2703 dFdXpE[(i + 1) * ncol] -= transIJ;
2704 tmp = transIJ * (-rhoP * dGamma);
2705 tmp += xij * transJ * xiP * dP;
2706 tmp += -transIJ * muP / mu * dP;
2707 if (FIMbId == uId) {
2708 dFdXpB[(i + 1) * ncol] += tmp;
2711 dFdXpE[(i + 1) * ncol] += tmp;
2715 for (
USI k = 0; k < np; k++) {
2716 dFdXsB[(i + 1) * ncol2 + k] +=
2717 transIJ * myBulk.dPcj_dS[FIMbIde * np * np + j * np + k];
2718 dFdXsE[(i + 1) * ncol2 + k] -=
2719 transIJ * myBulk.dPcj_dS[FIMeIde * np * np + j * np + k];
2720 tmp = Akd * xij * xi / mu *
2721 myBulk.dKr_dS[uIde * np * np + j * np + k] * dP;
2722 if (FIMbId == uId) {
2723 dFdXsB[(i + 1) * ncol2 + k] += tmp;
2726 dFdXsE[(i + 1) * ncol2 + k] += tmp;
2730 for (
USI k = 0; k < nc; k++) {
2731 rhox = myBulk.rhox[uIde_np_j * nc + k];
2732 xix = myBulk.xix[uIde_np_j * nc + k];
2733 mux = myBulk.mux[uIde_np_j * nc + k];
2734 tmp = -transIJ * rhox * dGamma;
2735 tmp += xij * transJ * xix * dP;
2736 tmp += -transIJ * mux / mu * dP;
2738 tmp += xi * transJ * dP;
2740 if (FIMbId == uId) {
2741 dFdXsB[(i + 1) * ncol2 + np + j * nc + k] += tmp;
2744 dFdXsE[(i + 1) * ncol2 + np + j * nc + k] += tmp;
2749 USI diagptr = myLS.diagPtr[bId];
2752 if (bId != lastbId) {
2754 assert(myLS.val[bId].size() == diagptr * bsize);
2756 myLS.val[bId].insert(myLS.val[bId].end(), myLS.diagVal.data() +
id,
2757 myLS.diagVal.data() +
id + bsize);
2763 DaABpbC(ncol, ncol, ncol2, 1, dFdXsB.data(), &myBulk.dSec_dPri[FIMbIde * bsize2], 1,
2765 Dscalar(bsize, dt, bmat.data());
2768 if (FIMbId < FIMeId) {
2769 diagptr = myLS.diagPtr[FIMbId];
2770 for (
USI i = 0; i < bsize; i++) {
2771 myLS.val[FIMbId][diagptr * bsize + i] += bmat[i];
2775 for (
USI i = 0; i < bsize; i++) {
2776 myLS.diagVal[FIMbId * bsize + i] += bmat[i];
2784 Dscalar(bsize, -1, bmat.data());
2785 myLS.val[FIMeId].insert(myLS.val[FIMeId].end(), bmat.begin(), bmat.end());
2790 DaABpbC(ncol, ncol, ncol2, 1, dFdXsE.data(), &myBulk.dSec_dPri[FIMeIde * bsize2], 1,
2792 Dscalar(bsize, dt, bmat.data());
2797 for (
USI i = 0; i < ncol; i++) {
2798 for (
USI j = 1; j < ncol; j++) {
2799 bmat[i * ncol + j] = 0;
2803 myLS.val[FIMbId].insert(myLS.val[FIMbId].end(), bmat.begin(), bmat.end());
2806 Dscalar(bsize, -1, bmat.data());
2807 if (FIMbId < FIMeId) {
2808 for (
USI i = 0; i < bsize; i++) {
2809 myLS.diagVal[FIMeId * bsize + i] += bmat[i];
2813 diagptr = myLS.diagPtr[FIMeId];
2814 for (
USI i = 0; i < bsize; i++) {
2815 myLS.val[FIMeId][diagptr * bsize + i] += bmat[i];
2821 if (!bIdFIM || !eIdFIM) {
2837 dGamma =
GRAVITY_FACTOR* (myBulk.depth[IMPECbId] - myBulk.depth[IMPECeId]);
2838 for (
USI j = 0; j < np; j++) {
2839 uId = upblock[c * np + j];
2840 if (!myBulk.phaseExist[uId * np + j])
continue;
2845 for (
USI i = 0; i < nc; i++) {
2847 myBulk.vfi[IMPECbId * nc + i] * myBulk.xij[uId * np * nc + j * nc + i];
2849 myBulk.vfi[IMPECeId * nc + i] * myBulk.xij[uId * np * nc + j * nc + i];
2851 OCP_DBL dPc = myBulk.Pc[IMPECbId * np + j] - myBulk.Pc[IMPECeId * np + j];
2852 dP = myBulk.Pj[IMPECbId * np + j] - myBulk.Pj[IMPECeId * np + j] -
2853 upblock_Rho[c * np + j] * dGamma;
2854 OCP_DBL temp = myBulk.xi[uId * np + j] * upblock_Trans[c * np + j] * dt;
2856 valup += temp * valupi;
2857 valdown += temp * valdowni;
2862 rhsup += temp * valupi;
2863 rhsdown -= temp * valdowni;
2867 USI diagptr = myLS.diagPtr[bId];
2868 if (bId != lastbId) {
2870 assert(myLS.val[bId].size() == diagptr * bsize);
2872 myLS.val[bId].insert(myLS.val[bId].end(), myLS.diagVal.data() +
id,
2873 myLS.diagVal.data() +
id + bsize);
2878 if (IMPECbId < IMPECeId) {
2879 diagptr = myLS.diagPtr[IMPECbId];
2880 myLS.val[IMPECbId][diagptr * bsize] += valup;
2883 myLS.diagVal[IMPECbId * bsize] += valup;
2886 IMPECbmat[0] = (-valup);
2887 myLS.val[IMPECbId].insert(myLS.val[IMPECbId].end(), IMPECbmat.begin(), IMPECbmat.end());
2891 IMPECbmat[0] = (-valdown);
2892 myLS.val[IMPECeId].insert(myLS.val[IMPECeId].end(), IMPECbmat.begin(), IMPECbmat.end());
2894 if (IMPECbId < IMPECeId) {
2895 myLS.diagVal[IMPECeId * bsize] += valdown;
2898 diagptr = myLS.diagPtr[IMPECeId];
2899 myLS.val[IMPECeId][diagptr * bsize] += valup;
2905 res[IMPECbId * ncol] += rhsup;
2907 res[IMPECeId * ncol] += rhsdown;
2913 for (
OCP_USI n = 0; n < numBulk; n++) {
2914 if (myLS.val[n].size() == myLS.diagPtr[n] * bsize)
2915 myLS.val[n].insert(myLS.val[n].end(), myLS.diagVal.data() + n * bsize,
2916 myLS.diagVal.data() + n * bsize + bsize);
2925 upblock.resize(numConn * np);
2926 upblock_Rho.resize(numConn * np);
2927 upblock_Velocity.resize(numConn * np);
2933 const USI np = myBulk.numPhase;
2934 const USI nc = myBulk.numCom;
2935 const USI ncol = nc + 1;
2936 const USI ncol2 = np * nc + np;
2937 const USI bsize = ncol * ncol;
2938 const USI bsize2 = ncol * ncol2;
2940 vector<OCP_DBL> bmat(bsize, 0);
2943 for (
USI i = 1; i < nc + 1; i++) {
2944 bmat[i * ncol + i] = 1;
2946 for (
OCP_USI n = 0; n < numBulk; n++) {
2947 bmat[0] = myBulk.rockC1 * myBulk.rockVpInit[n] - myBulk.vfp[n];
2948 for (
USI i = 0; i < nc; i++) {
2949 bmat[i + 1] = -myBulk.vfi[n * nc + i];
2951 for (
USI i = 0; i < bsize; i++) {
2952 myLS.diagVal[n * bsize + i] = bmat[i];
2958 vector<OCP_DBL> dFdXpB(bsize, 0);
2959 vector<OCP_DBL> dFdXpE(bsize, 0);
2960 vector<OCP_DBL> dFdXsB(bsize2, 0);
2961 vector<OCP_DBL> dFdXsE(bsize2, 0);
2965 OCP_DBL kr, mu, xi, xij, rhoP, xiP, muP, rhox, xix, mux;
2968 bool bIdFIM, eIdFIM;
2971 OCP_USI lastbId = iteratorConn[0].EId;
2972 for (
OCP_USI c = 0; c < numConn; c++) {
2973 bId = iteratorConn[c].BId;
2974 eId = iteratorConn[c].EId;
2976 fill(dFdXpB.begin(), dFdXpB.end(), 0.0);
2977 fill(dFdXpE.begin(), dFdXpE.end(), 0.0);
2978 fill(dFdXsB.begin(), dFdXsB.end(), 0.0);
2979 fill(dFdXsE.begin(), dFdXsE.end(), 0.0);
2980 dGamma =
GRAVITY_FACTOR * (myBulk.depth[bId] - myBulk.depth[eId]);
2982 bIdFIM = eIdFIM =
false;
2983 if (myBulk.map_Bulk2FIM[bId] > -1) bIdFIM =
true;
2984 if (myBulk.map_Bulk2FIM[eId] > -1) eIdFIM =
true;
2986 for (
USI j = 0; j < np; j++) {
2987 uId = upblock[c * np + j];
2988 uId_np_j = uId * np + j;
2989 if (!myBulk.phaseExist[uId_np_j])
continue;
2990 dP = myBulk.Pj[bId * np + j] - myBulk.Pj[eId * np + j] -
2991 upblock_Rho[c * np + j] * dGamma;
2994 xi = myBulk.xi[uId_np_j];
2995 kr = myBulk.kr[uId_np_j];
2996 mu = myBulk.mu[uId_np_j];
2997 muP = myBulk.muP[uId_np_j];
2998 xiP = myBulk.xiP[uId_np_j];
2999 rhoP = myBulk.rhoP[uId_np_j];
3000 transJ = Akd * kr / mu;
3002 for (
USI i = 0; i < nc; i++) {
3003 xij = myBulk.xij[uId_np_j * nc + i];
3005 transIJ = xij * xi * transJ;
3008 dFdXpB[(i + 1) * ncol] += transIJ;
3009 dFdXpE[(i + 1) * ncol] -= transIJ;
3010 tmp = transIJ * (-rhoP * dGamma);
3011 tmp += xij * transJ * xiP * dP;
3012 tmp += -transIJ * muP / mu * dP;
3014 dFdXpB[(i + 1) * ncol] += tmp;
3017 dFdXpE[(i + 1) * ncol] += tmp;
3021 for (
USI k = 0; k < np; k++) {
3022 dFdXsB[(i + 1) * ncol2 + k] +=
3023 transIJ * myBulk.dPcj_dS[bId * np * np + j * np + k];
3024 dFdXsE[(i + 1) * ncol2 + k] -=
3025 transIJ * myBulk.dPcj_dS[eId * np * np + j * np + k];
3026 tmp = Akd * xij * xi / mu *
3027 myBulk.dKr_dS[uId * np * np + j * np + k] * dP;
3029 dFdXsB[(i + 1) * ncol2 + k] += tmp;
3032 dFdXsE[(i + 1) * ncol2 + k] += tmp;
3036 for (
USI k = 0; k < nc; k++) {
3037 rhox = myBulk.rhox[uId_np_j * nc + k];
3038 xix = myBulk.xix[uId_np_j * nc + k];
3039 mux = myBulk.mux[uId_np_j * nc + k];
3040 tmp = -transIJ * rhox * dGamma;
3041 tmp += xij * transJ * xix * dP;
3042 tmp += -transIJ * mux / mu * dP;
3044 tmp += xi * transJ * dP;
3047 dFdXsB[(i + 1) * ncol2 + np + j * nc + k] += tmp;
3050 dFdXsE[(i + 1) * ncol2 + np + j * nc + k] += tmp;
3056 USI diagptr = myLS.diagPtr[bId];
3058 if (bId != lastbId) {
3060 assert(myLS.val[bId].size() == diagptr * bsize);
3062 myLS.val[bId].insert(myLS.val[bId].end(), myLS.diagVal.data() +
id,
3063 myLS.diagVal.data() +
id + bsize);
3070 DaABpbC(ncol, ncol, ncol2, 1, dFdXsB.data(), &myBulk.dSec_dPri[bId * bsize2], 1,
3072 Dscalar(bsize, dt, bmat.data());
3077 for (
USI i = 1; i < ncol; i++) {
3078 for (
USI j = 1; j < ncol; j++) {
3079 bmat[i * ncol + j] = 0;
3083 for (
USI i = 0; i < bsize; i++) {
3084 myLS.val[bId][diagptr * bsize + i] += bmat[i];
3088 Dscalar(bsize, -1, bmat.data());
3089 myLS.val[eId].insert(myLS.val[eId].end(), bmat.begin(), bmat.end());
3093 DaABpbC(ncol, ncol, ncol2, 1, dFdXsE.data(), &myBulk.dSec_dPri[eId * bsize2], 1,
3095 Dscalar(bsize, dt, bmat.data());
3100 for (
USI i = 1; i < ncol; i++) {
3101 for (
USI j = 1; j < ncol; j++) {
3102 bmat[i * ncol + j] = 0;
3106 myLS.val[bId].insert(myLS.val[bId].end(), bmat.begin(), bmat.end());
3108 Dscalar(bsize, -1, bmat.data());
3109 for (
USI i = 0; i < bsize; i++) {
3110 myLS.diagVal[eId * bsize + i] += bmat[i];
3114 for (
OCP_USI n = 0; n < numBulk; n++) {
3115 if (myLS.val[n].size() == myLS.diagPtr[n] * bsize)
3116 myLS.val[n].insert(myLS.val[n].end(), myLS.diagVal.data() + n * bsize,
3117 myLS.diagVal.data() + n * bsize + bsize);
3123 const USI np = myBulk.numPhase;
3124 const USI nc = myBulk.numCom;
3125 const USI ncol = nc + 1;
3126 const USI ncol2 = np * nc + np;
3127 const USI bsize = ncol * ncol;
3128 const USI bsize2 = ncol * ncol2;
3130 vector<OCP_DBL> bmat(bsize, 0);
3133 for (
USI i = 1; i < nc + 1; i++) {
3134 bmat[i * ncol + i] = 1;
3136 for (
OCP_USI n = 0; n < numBulk; n++) {
3137 bmat[0] = myBulk.rockC1 * myBulk.rockVpInit[n] - myBulk.vfp[n];
3138 for (
USI i = 0; i < nc; i++) {
3139 bmat[i + 1] = -myBulk.vfi[n * nc + i];
3141 for (
USI i = 0; i < bsize; i++) {
3142 myLS.diagVal[n * bsize + i] = bmat[i];
3149 OCP_DBL kr, mu, xi, xij, rhoP, xiP, muP, rhox, xix, mux;
3152 bool bIdFIM, eIdFIM, uIdFIM;
3157 vector<OCP_DBL> dFdXpB(bsize, 0);
3158 vector<OCP_DBL> dFdXpE(bsize, 0);
3159 vector<OCP_DBL> dFdXsB(bsize2, 0);
3160 vector<OCP_DBL> dFdXsE(bsize2, 0);
3163 OCP_USI lastbId = iteratorConn[0].EId;
3164 for (
OCP_USI c = 0; c < numConn; c++) {
3165 bId = iteratorConn[c].BId;
3166 eId = iteratorConn[c].EId;
3169 bIdFIM = eIdFIM =
false;
3170 if (myBulk.map_Bulk2FIM[bId] > -1) bIdFIM =
true;
3171 if (myBulk.map_Bulk2FIM[eId] > -1) eIdFIM =
true;
3173 fill(dFdXpB.begin(), dFdXpB.end(), 0.0);
3174 fill(dFdXpE.begin(), dFdXpE.end(), 0.0);
3175 fill(dFdXsB.begin(), dFdXsB.end(), 0.0);
3176 fill(dFdXsE.begin(), dFdXsE.end(), 0.0);
3178 dGamma =
GRAVITY_FACTOR * (myBulk.depth[bId] - myBulk.depth[eId]);
3180 for (
USI j = 0; j < np; j++) {
3181 uId = upblock[c * np + j];
3182 uId_np_j = uId * np + j;
3184 if (!myBulk.phaseExist[uId_np_j])
continue;
3186 uIdFIM = (uId == bId ? bIdFIM : eIdFIM);
3188 dP = myBulk.Pj[bId * np + j] - myBulk.Pj[eId * np + j] -
3189 upblock_Rho[c * np + j] * dGamma;
3190 xi = myBulk.xi[uId_np_j];
3191 kr = myBulk.kr[uId_np_j];
3192 mu = myBulk.mu[uId_np_j];
3193 transJ = Akd * kr / mu;
3196 muP = myBulk.muP[uId_np_j];
3197 xiP = myBulk.xiP[uId_np_j];
3198 rhoP = myBulk.rhoP[uId_np_j];
3201 for (
USI i = 0; i < nc; i++) {
3202 xij = myBulk.xij[uId_np_j * nc + i];
3203 transIJ = xij * xi * transJ;
3206 dFdXpB[(i + 1) * ncol] += transIJ;
3207 dFdXpE[(i + 1) * ncol] -= transIJ;
3210 tmp = transIJ * (-rhoP * dGamma);
3211 tmp += xij * transJ * xiP * dP;
3212 tmp += -transIJ * muP / mu * dP;
3214 dFdXpB[(i + 1) * ncol] += tmp;
3217 dFdXpE[(i + 1) * ncol] += tmp;
3223 for (
USI k = 0; k < np; k++) {
3224 dFdXsB[(i + 1) * ncol2 + k] +=
3225 transIJ * myBulk.dPcj_dS[bId * np * np + j * np + k];
3228 for (
USI k = 0; k < np; k++) {
3229 tmp = Akd * xij * xi / mu *
3230 myBulk.dKr_dS[uId * np * np + j * np + k] * dP;
3231 dFdXsB[(i + 1) * ncol2 + k] += tmp;
3236 for (
USI k = 0; k < np; k++) {
3237 dFdXsE[(i + 1) * ncol2 + k] -=
3238 transIJ * myBulk.dPcj_dS[eId * np * np + j * np + k];
3241 for (
USI k = 0; k < np; k++) {
3242 tmp = Akd * xij * xi / mu *
3243 myBulk.dKr_dS[uId * np * np + j * np + k] * dP;
3244 dFdXsE[(i + 1) * ncol2 + k] += tmp;
3252 for (
USI k = 0; k < nc; k++) {
3253 rhox = myBulk.rhox[uId_np_j * nc + k];
3254 xix = myBulk.xix[uId_np_j * nc + k];
3255 mux = myBulk.mux[uId_np_j * nc + k];
3256 tmp = -transIJ * rhox * dGamma;
3257 tmp += xij * transJ * xix * dP;
3258 tmp += -transIJ * mux / mu * dP;
3259 dFdXsB[(i + 1) * ncol2 + np + j * nc + k] += tmp;
3261 dFdXsB[(i + 1) * ncol2 + np + j * nc + i] += xi * transJ * dP;
3264 for (
USI k = 0; k < nc; k++) {
3265 rhox = myBulk.rhox[uId_np_j * nc + k];
3266 xix = myBulk.xix[uId_np_j * nc + k];
3267 mux = myBulk.mux[uId_np_j * nc + k];
3268 tmp = -transIJ * rhox * dGamma;
3269 tmp += xij * transJ * xix * dP;
3270 tmp += -transIJ * mux / mu * dP;
3271 dFdXsE[(i + 1) * ncol2 + np + j * nc + k] += tmp;
3273 dFdXsE[(i + 1) * ncol2 + np + j * nc + i] += xi * transJ * dP;
3278 USI diagptr = myLS.diagPtr[bId];
3280 if (bId != lastbId) {
3282 assert(myLS.val[bId].size() == diagptr * bsize);
3284 myLS.val[bId].insert(myLS.val[bId].end(), myLS.diagVal.data() +
id,
3285 myLS.diagVal.data() +
id + bsize);
3293 DaABpbC(ncol, ncol, ncol2, 1, dFdXsB.data(), &myBulk.dSec_dPri[bId * bsize2], 1,
3296 Dscalar(bsize, dt, bmat.data());
3299 for (
USI i = 0; i < bsize; i++) {
3300 myLS.val[bId][diagptr * bsize + i] += bmat[i];
3304 Dscalar(bsize, -1, bmat.data());
3305 myLS.val[eId].insert(myLS.val[eId].end(), bmat.begin(), bmat.end());
3310 DaABpbC(ncol, ncol, ncol2, 1, dFdXsE.data(), &myBulk.dSec_dPri[eId * bsize2], 1,
3313 Dscalar(bsize, dt, bmat.data());
3316 myLS.val[bId].insert(myLS.val[bId].end(), bmat.begin(), bmat.end());
3318 Dscalar(bsize, -1, bmat.data());
3319 for (
USI i = 0; i < bsize; i++) {
3320 myLS.diagVal[eId * bsize + i] += bmat[i];
3324 for (
OCP_USI n = 0; n < numBulk; n++) {
3325 if (myLS.val[n].size() == myLS.diagPtr[n] * bsize)
3326 myLS.val[n].insert(myLS.val[n].end(), myLS.diagVal.data() + n * bsize,
3327 myLS.diagVal.data() + n * bsize + bsize);
3339 const USI np = myBulk.numPhase;
3340 const USI nc = myBulk.numCom;
3341 const USI len = nc + 1;
3345 for (
OCP_USI n = 0; n < numBulk; n++) {
3350 res[bId] = myBulk.rockVp[n] - myBulk.vf[n];
3351 for (
USI i = 0; i < nc; i++) {
3352 res[bId + 1 + i] = myBulk.Ni[bIdb + i] - myBulk.lNi[bIdb + i];
3356 OCP_USI bId_np_j, eId_np_j, uId_np_j;
3357 OCP_DBL Pbegin, Pend, rho, dP;
3362 for (
OCP_USI c = 0; c < numConn; c++) {
3363 bId = iteratorConn[c].BId;
3364 eId = iteratorConn[c].EId;
3367 for (
USI j = 0; j < np; j++) {
3368 bId_np_j = bId * np + j;
3369 eId_np_j = eId * np + j;
3371 bool exbegin = myBulk.phaseExist[bId_np_j];
3372 bool exend = myBulk.phaseExist[eId_np_j];
3374 if ((exbegin) && (exend)) {
3375 Pbegin = myBulk.Pj[bId_np_j];
3376 Pend = myBulk.Pj[eId_np_j];
3377 rho = (myBulk.rho[bId_np_j] + myBulk.rho[eId_np_j]) / 2;
3379 else if (exbegin && (!exend)) {
3380 Pbegin = myBulk.Pj[bId_np_j];
3381 Pend = myBulk.P[eId];
3382 rho = myBulk.rho[bId_np_j];
3384 else if ((!exbegin) && (exend)) {
3385 Pbegin = myBulk.P[bId];
3386 Pend = myBulk.Pj[eId_np_j];
3387 rho = myBulk.rho[eId_np_j];
3390 upblock[c * np + j] = bId;
3391 upblock_Velocity[c * np + j] = 0;
3392 upblock_Rho[c * np + j] = 0;
3397 bool exup = exbegin;
3404 uId_np_j = uId * np + j;
3406 upblock_Rho[c * np + j] = rho;
3407 upblock[c * np + j] = uId;
3410 upblock_Velocity[c * np + j] = Akd * myBulk.kr[uId_np_j] / myBulk.mu[uId_np_j] * dP;
3413 upblock_Velocity[c * np + j] = 0;
3420 tmp = dt * Akd * myBulk.xi[uId_np_j] *
3421 myBulk.kr[uId_np_j] / myBulk.mu[uId_np_j] * dP;
3423 for (
USI i = 0; i < nc; i++) {
3424 dNi = tmp * myBulk.xij[uId_np_j * nc + i];
3425 res[bId * len + 1 + i] += dNi;
3426 res[eId * len + 1 + i] -= dNi;
BulkConn class declaration.
void DaABpbC(const int &m, const int &n, const int &k, const double &alpha, const double *A, const double *B, const double &beta, double *C)
Computes C' = alpha B'A' + beta C', all matrices are column-major.
void Dscalar(const int &n, const double &alpha, double *x)
Scales a vector by a constant.
void DaAxpby(const int &m, const int &n, const double &a, const double *A, const double *x, const double &b, double *y)
Computes y = a A x + b y.
bool CheckNan(const int &N, const T *x)
check NaN
const OCP_DBL TINY
Small constant.
unsigned int USI
Generic unsigned integer.
double OCP_DBL
Double precision.
const OCP_DBL CONV1
1 bbl = CONV1 ft3
const OCP_DBL GRAVITY_FACTOR
0.00694444 ft2 psi / lb
unsigned int OCP_USI
Long unsigned integer.
const OCP_DBL CONV2
Darcy constant in field unit.
#define OCP_FUNCNAME
Print Function Name.
#define OCP_ABORT(msg)
Abort if critical error happens.
void Setup(const Grid &myGrid, const Bulk &myBulk)
Setup active connections and calculate necessary properties using Grid and Bulk.
void UpdateLastStep()
Update physcial values of the previous step.
void CheckDiff() const
Check differences between the current and previous steps.
void SetupWellBulk_K(Bulk &myBulk) const
Setup k-neighbor for bulks.
void AllocateAuxAIMc(const USI &np)
Allocate memory for auxiliary variables used by the AIMc method.
void AssembleMatIMPEC(LinearSystem &myLS, const Bulk &myBulk, const OCP_DBL &dt) const
Assmeble coefficient matrix for IMPEC, terms related to bulks only.
void SetupMatSparsity(LinearSystem &myLS) const
Setup sparsity pattern of the coefficient matrix.
void CalFluxFIMS(const Bulk &myBulk)
rho = (S1*rho1 + S2*rho2)/(S1+S2)
void AssembleMat_FIM(LinearSystem &myLS, const Bulk &myBulk, const OCP_DBL &dt) const
Assmeble coefficient matrix for FIM, terms related to bulks only.
void CalCFL(const Bulk &myBulk, const OCP_DBL &dt) const
Calculate the CFL number for flow between bulks???
void AllocateAuxIMPEC(const USI &np)
Allocate memory for auxiliary variables used by the IMPEC method.
void AllocateAuxAIMt()
Allocate memory for auxiliary variables used by the AIMt method.
void MassConserveIMPEC(Bulk &myBulk, const OCP_DBL &dt) const
Update mole composition of each bulk according to mass conservation for IMPEC.
void CalResFIM(vector< OCP_DBL > &res, const Bulk &myBulk, const OCP_DBL &dt)
Calculate resiual for the Newton iteration in FIM.
void PrintConnectionInfo(const Grid &myGrid) const
Print information of connections on screen.
void CalResAIMc(vector< OCP_DBL > &res, const Bulk &myBulk, const OCP_DBL &dt)
Calculate resiual for the Newton iteration in FIM.
void CalFluxFIM(const Bulk &myBulk)
Calculate flux for FIM, considering upwinding.
void AssembleMat_FIM_newS(LinearSystem &myLS, const Bulk &myBulk, const OCP_DBL &dt) const
OCP_NEW_FIM rho = (S1*rho1 + S2*rho2)/(S1+S2)
void CalFluxIMPEC(const Bulk &myBulk)
Calculate flux information about flow between bulks for IMPEC.
void AssembleMat_FIM_new(LinearSystem &myLS, const Bulk &myBulk, const OCP_DBL &dt) const
void AssembleMat_FIM_new_n(LinearSystem &myLS, const Bulk &myBulk, const OCP_DBL &dt) const
OCP_NEW_FIMn.
void AllocateMat(LinearSystem &myLS) const
Allocate memory for the coefficient matrix.
void SetupMatSparsityAIMt(LinearSystem &myLS, const Bulk &myBulk) const
Setup sparsity pattern of the coefficient matrix for AIMt.
void Reset()
Reset physcial values of the current step with the previous step.
void CalResAIMt(vector< OCP_DBL > &res, const Bulk &myBulk, const OCP_DBL &dt)
Calculate resiual for the Newton iteration in local FIM.
void AllocateAuxFIM(const USI &np)
Allocate memory for auxiliary variables used by the FIM method.
void CalResAIMs(vector< OCP_DBL > &res, const Bulk &myBulk, const OCP_DBL &dt)
void AssembleMat_AIMt(LinearSystem &myLS, const Bulk &myBulk, const OCP_DBL &dt) const
Assmeble coefficient matrix for FIM, terms related to bulks only.
void AssembleMat_AIMs(LinearSystem &myLS, vector< OCP_DBL > &res, const Bulk &myBulk, const OCP_DBL &dt) const
Connection between two bulks (BId, EId); usually, indices BId > EId.
Physical information of each active reservoir bulk.
Active cell indicator and its index among active cells.
bool IsAct() const
Return whether this cell is active or not.
OCP_USI GetId() const
Return the index of this cell among active cells.
Effective area of intersection surfaces with neighboring cells.
Basic information of computational grid, including the rock properties.
void GetIJKGrid(USI &i, USI &j, USI &k, const OCP_USI &n) const
Return the 3D coordinate for object grid with Grid index.
Linear solvers for discrete systems.