42#ifndef OPM_SINGLEPOINTUPWINDTWOPHASE_HPP_HEADER
43#define OPM_SINGLEPOINTUPWINDTWOPHASE_HPP_HEADER
92 : drho_(0.0), mob_(0), dmob_(0),
93 porevol_(0), dg_(0), ds_(0), pc_(0), dpc_(0), trans_(0),
101 alloc_sz += 1 * totconn;
105 alloc_sz += 1 * totconn;
106 data_.resize(alloc_sz);
109 dmob_ = mob_ + (2 * nc );
110 porevol_ = dmob_ + (2 * nc );
111 dg_ = porevol_ + (1 * nc );
112 ds_ = dg_ + (1 * totconn);
113 pc_ = ds_ + (1 * nc );
114 dpc_ = pc_ + (1 * nc );
115 trans_ = dpc_ + (1 * nc );
123 double&
drho () {
return drho_ ; }
128 double drho ()
const {
return drho_ ; }
136 double*
mob (
int c) {
return mob_ + (2*c + 0); }
143 const double*
mob (
int c)
const {
return mob_ + (2*c + 0); }
152 double*
dmob (
int c) {
return dmob_ + (2*c + 0); }
160 const double*
dmob (
int c)
const {
return dmob_ + (2*c + 0); }
172 double porevol(
int c)
const {
return porevol_[c] ; }
182 double&
dg(
int i) {
return dg_[i] ; }
190 double dg(
int i)
const {
return dg_[i] ; }
199 double&
ds(
int c) {
return ds_[c] ; }
207 double ds(
int c)
const {
return ds_[c] ; }
215 double&
pc(
int c) {
return pc_[c] ; }
222 double pc(
int c)
const {
return pc_[c] ; }
231 double&
dpc(
int c) {
return dpc_[c] ; }
239 double dpc(
int c)
const {
return dpc_[c] ; }
248 double&
trans(
int f) {
return trans_[f] ; }
256 double trans(
int f)
const {
return trans_[f] ; }
272 std::vector<double> data_;
277 template <
class TwophaseFlu
id>
280 template <
class Gr
id>
283 const std::vector<double>& porevol ,
284 const double* grav = 0,
285 const bool guess_previous =
true)
288 f2hf_ (2 * g.number_of_faces, -1) ,
289 store_ (g.number_of_cells,
290 g.cell_facepos[ g.number_of_cells ]),
291 init_step_use_previous_sol_(guess_previous),
296 store_.
drho() = fluid_.density(0) - fluid_.density(1);
299 auto i = grid_size_t(0);
300 for (
int c = 0; c < g.number_of_cells; ++c) {
301 for (; i < g.cell_facepos[c + 1]; ++i) {
302 const int f = g.cell_faces[i];
303 const int p = 1 - (g.face_cells[2*f + 0] == c);
308 std::copy(porevol.begin(), porevol.end(), store_.
porevol());
313 const std::vector<int>& hf_faces,
314 const std::vector<int>& nb_faces)
316 if (p_faces.empty()) {
319 assert (p_faces.size() == hf_faces.size());
320 assert (hf_faces.size() == nb_faces.size());
322 std::vector<int> nbhf(hf_faces.size());
324 for (std::vector<int>::size_type i = 0; i < p_faces.size(); ++i) {
325 const int nbf = nb_faces[i];
327 assert (2*std::vector<int>::size_type(nbf) + 1 < f2hf_.size());
328 assert ((f2hf_[2*nbf + 0] < 0) ^ (f2hf_[2*nbf + 1] < 0));
330 const int p = (f2hf_[2*nbf + 0] < 0) ? 1 : 0;
331 nbhf[ i ] = f2hf_[2*nbf + p];
334 for (std::vector<int>::size_type i = 0; i < p_faces.size(); ++i) {
335 const int f = p_faces [i];
336 const int hf = hf_faces[i];
340 assert (2*std::vector<int>::size_type(f) + 1 < f2hf_.size());
342 assert ((f2hf_[2*f + 0] < 0 ) ^ (f2hf_[2*f + 1] < 0 ));
343 assert ((f2hf_[2*f + 0] == hf) ^ (f2hf_[2*f + 1] == hf));
345 const int p = (f2hf_[2*f + 0] == hf) ? 1 : 0;
347 f2hf_[2*f + p] = nbhf[ i ];
375 const int *n = g.face_cells + (2 * f);
377 double gflux = gravityFlux(f);
378 double pcflux,dpcflux[2];
379 capFlux(f,n, pcflux, dpcflux);
384 upwindMobility(dflux, gflux, n, pix, m, dm);
386 assert (! ((m[0] < 0) || (m[1] < 0)));
388 double mt = m[0] + m[1];
391 double sgn = 2.0*(n[0] == c) - 1.0;
396 double f1 = m[0] / mt;
397 const double v1 = dflux + m[1]*gflux;
405 J[0] = J1; J[1] = J2;
407 J1[0*2 + 0] += sgn*dt * f1 * dpcflux[0] * m[1];
408 J2[0*2 + 0] += sgn*dt * f1 * dpcflux[1] * m[1];
410 J[0] = J2; J[1] = J1;
412 J1[0*2 + 0] += sgn*dt * f1 * dpcflux[1] * m[1];
413 J2[0*2 + 0] += sgn*dt * f1 * dpcflux[0] * m[1];
417 *J[ pix[0] ] += dt * (1 - f1) / mt * v1 * dm[0];
420 *J[ pix[1] ] -= dt * f1 / mt * v1 * dm[1];
421 *J[ pix[1] ] += dt * f1 * gflux * dm[1];
424 template <
class Gr
id>
432 const double pv = store_.
porevol(c);
435 *F += pv * store_.
ds(c);
438 template <
class Grid ,
442 const SourceTerms* src,
450 double dflux = -src->flux[i];
454 *F += dt * dflux * src->saturation[2*i + 0];
457 const int c = src->cell[i];
458 const double* m = store_.
mob (c);
459 const double* dm = store_.
dmob(c);
461 const double mt = m[0] + m[1];
463 assert (! ((m[0] < 0) || (m[1] < 0)));
466 const double f = m[0] / mt;
467 const double df = ((1 - f)*dm[0] - f*dm[1]) / mt;
469 *F += dt * dflux * f;
470 *J += dt * dflux * df;
473 template <
class Gr
id>
476 const std::vector<double> & htrans) {
478 assert (htrans.size() ==
479 static_cast<std::vector<double>::size_type
>(g.cell_facepos[ g.number_of_cells ]));
481 for (
int f = 0; f < g.number_of_faces; ++f) {
482 store_.
trans(f) = 0.0;
485 auto i = grid_size_t(0);
486 for (
int c = 0; c < g.number_of_cells; ++c) {
487 for (; i < g.cell_facepos[c + 1]; ++i) {
488 int f = g.cell_faces[i];
490 assert (htrans[i] > 0.0);
492 store_.
trans(f) += 1.0 / htrans[i];
496 for (
int f = 0; f < g.number_of_faces; ++f) {
501 this->computeStaticGravity(g);
511 class JacobianSystem>
515 JacobianSystem& sys ) {
519 typename JacobianSystem::vector_type& x =
520 sys.vector().writableSolution();
522 assert (x.size() == (::std::size_t) (g.number_of_cells));
524 if (init_step_use_previous_sol_) {
525 std::fill(x.begin(), x.end(), 0.0);
527 const std::vector<double>& s = state.
saturation();
528 for (
int c = 0, nc = g.number_of_cells; c < nc; ++c) {
530 x[c] = 0.5 - s[2*c + 0];
537 class JacobianSystem>
541 JacobianSystem& sys) {
543 double s[2], mob[2], dmob[2 * 2], pc, dpc;
545 const typename JacobianSystem::vector_type& x =
546 sys.vector().solution();
547 const ::std::vector<double>& sat = state.
saturation();
549 bool in_range =
true;
550 for (
int c = 0; c < g.number_of_cells; ++c) {
553 s[0] = sat[c*2 + 0] + x[c];
555 double s_min = fluid_.s_min(c);
556 double s_max = fluid_.s_max(c);
558 if ( s[0] < (s_min - sat_tol_) || s[0] > (s_max + sat_tol_) ) {
567 s[0] = std::max(s_min, s[0]);
571 fluid_.mobility(c, s, mob, dmob);
572 fluid_.pc(c, s, pc, dpc);
574 store_.
mob (c)[0] = mob [0];
575 store_.
mob (c)[1] = mob [1];
576 store_.
dmob(c)[0] = dmob[0*2 + 0];
577 store_.
dmob(c)[1] = -dmob[1*2 + 1];
583 std::cout <<
"Warning: initIteration() - s was clamped in some cells.\n";
591 class NewtonIterate >
595 NewtonIterate& it ) {
597 (void) state; (void) g; (void) it;
600 template <
class Grid ,
601 class SolutionVector,
605 const SolutionVector& x ,
610 for (
int c = 0; c < g.number_of_cells; ++c, s += 2) {
612 double s_min = fluid_.s_min(c);
613 double s_max = fluid_.s_max(c);
616 assert(s[0] >= s_min - sat_tol_);
617 assert(s[0] <= s_max + sat_tol_);
620 s[0] = std::max(s_min, s[0]);
628 upwindMobility(
const double dflux,
634 bool equal_sign = ( (! (dflux < 0)) && (! (gflux < 0)) ) ||
635 ( (! (dflux > 0)) && (! (gflux > 0)) );
639 if (! (dflux < 0) && ! (gflux < 0)) { pix[0] = 0; }
642 m[0] = store_.
mob(n[ pix[0] ]) [ 0 ];
644 if (! (dflux - m[0]*gflux < 0)) { pix[1] = 0; }
647 m[1] = store_.
mob(n[ pix[1] ]) [ 1 ];
651 if (! (dflux < 0) && ! (gflux > 0)) { pix[1] = 0; }
654 m[1] = store_.
mob(n[ pix[1] ]) [ 1 ];
656 if (dflux + m[1]*gflux > 0) { pix[0] = 0; }
659 m[0] = store_.
mob(n[ pix[0] ]) [ 0 ];
662 dm[0] = store_.
dmob(n[ pix[0] ]) [ 0 ];
663 dm[1] = store_.
dmob(n[ pix[1] ]) [ 1 ];
666 template <
class Gr
id>
668 computeStaticGravity(
const Grid& g) {
670 const int d = g.dimensions;
671 auto i = grid_size_t(0);
673 for (
int c = 0; c < g.number_of_cells; ++c) {
674 const double* cc = g.cell_centroids + (c * d);
676 for (; i < g.cell_facepos[c + 1]; ++i) {
677 const int f = g.cell_faces[i];
678 const double* fc = g.face_centroids + (f * d);
681 for (
int j = 0;
j < d; ++
j) {
682 dg += gravity_[
j] * (fc[
j] - cc[
j]);
685 store_.
dg(i) = store_.
trans(f) * dg;
691 gravityFlux(
const int f)
const {
695 int i1 = f2hf_[2*f + 0];
696 int i2 = f2hf_[2*f + 1];
698 assert ((i1 >= 0) && (i2 >= 0));
700 gflux = store_.
dg(i1) - store_.
dg(i2);
701 gflux *= store_.
drho();
709 capFlux(
const int f,
const int* n,
double& pcflux,
double* dpcflux)
const {
713 assert ((i1 >= 0) && (i2 >= 0));
715 pcflux = store_.
trans(f)*(store_.
pc(i2) - store_.
pc(i1));
716 dpcflux[0] = -store_.
trans(f)*store_.
dpc(i1);
717 dpcflux[1] = store_.
trans(f)*store_.
dpc(i2);
720 TwophaseFluid fluid_ ;
721 const double* gravity_;
722 std::vector<int> f2hf_ ;
723 spu_2p::ModelParameterStorage store_ ;
724 bool init_step_use_previous_sol_;
Definition: ImplicitTransportDefs.hpp:52
::std::vector< double > & faceflux()
Definition: ImplicitTransportDefs.hpp:63
::std::vector< double > & saturation()
Definition: ImplicitTransportDefs.hpp:64
Definition: SinglePointUpwindTwoPhase.hpp:278
void initStep(const ReservoirState &state, const Grid &g, JacobianSystem &sys)
Definition: SinglePointUpwindTwoPhase.hpp:513
void accumulation(const Grid &g, const int c, double *J, double *F) const
Definition: SinglePointUpwindTwoPhase.hpp:426
void initGravityTrans(const Grid &g, const std::vector< double > &htrans)
Definition: SinglePointUpwindTwoPhase.hpp:475
void initResidual(const int c, double *F) const
Definition: SinglePointUpwindTwoPhase.hpp:358
void sourceTerms(const Grid &g, const SourceTerms *src, const int i, const double dt, double *J, double *F) const
Definition: SinglePointUpwindTwoPhase.hpp:441
SinglePointUpwindTwoPhase(const TwophaseFluid &fluid, const Grid &g, const std::vector< double > &porevol, const double *grav=0, const bool guess_previous=true)
Definition: SinglePointUpwindTwoPhase.hpp:281
void makefhfQPeriodic(const std::vector< int > &p_faces, const std::vector< int > &hf_faces, const std::vector< int > &nb_faces)
Definition: SinglePointUpwindTwoPhase.hpp:312
void fluxConnection(const ReservoirState &state, const Grid &g, const double dt, const int c, const int f, double *J1, double *J2, double *F) const
Definition: SinglePointUpwindTwoPhase.hpp:366
void finishStep(const Grid &g, const SolutionVector &x, ReservoirState &state)
Definition: SinglePointUpwindTwoPhase.hpp:604
void finishIteration(const ReservoirState &state, const Grid &g, NewtonIterate &it)
Definition: SinglePointUpwindTwoPhase.hpp:593
@ DofPerCell
Definition: SinglePointUpwindTwoPhase.hpp:355
bool initIteration(const ReservoirState &state, const Grid &g, JacobianSystem &sys)
Definition: SinglePointUpwindTwoPhase.hpp:539
Definition: SinglePointUpwindTwoPhase.hpp:82
double * porevol()
Definition: SinglePointUpwindTwoPhase.hpp:166
ModelParameterStorage(int nc, int totconn)
Definition: SinglePointUpwindTwoPhase.hpp:91
double & drho()
Definition: SinglePointUpwindTwoPhase.hpp:123
double drho() const
Definition: SinglePointUpwindTwoPhase.hpp:128
double trans(int f) const
Definition: SinglePointUpwindTwoPhase.hpp:256
double dg(int i) const
Definition: SinglePointUpwindTwoPhase.hpp:190
double * mob(int c)
Definition: SinglePointUpwindTwoPhase.hpp:136
double & pc(int c)
Definition: SinglePointUpwindTwoPhase.hpp:215
double & dpc(int c)
Definition: SinglePointUpwindTwoPhase.hpp:231
double & trans(int f)
Definition: SinglePointUpwindTwoPhase.hpp:248
double & ds(int c)
Definition: SinglePointUpwindTwoPhase.hpp:199
double pc(int c) const
Definition: SinglePointUpwindTwoPhase.hpp:222
double dpc(int c) const
Definition: SinglePointUpwindTwoPhase.hpp:239
double ds(int c) const
Definition: SinglePointUpwindTwoPhase.hpp:207
const double * mob(int c) const
Definition: SinglePointUpwindTwoPhase.hpp:143
double porevol(int c) const
Definition: SinglePointUpwindTwoPhase.hpp:172
double & dg(int i)
Definition: SinglePointUpwindTwoPhase.hpp:182
const double * dmob(int c) const
Definition: SinglePointUpwindTwoPhase.hpp:160
double * dmob(int c)
Definition: SinglePointUpwindTwoPhase.hpp:152
min[0]
Definition: elasticity_upscale_impl.hpp:146
int j
Definition: elasticity_upscale_impl.hpp:658
Definition: ImplicitAssembly.hpp:43