38#ifndef OPENRS_INCOMPFLOWSOLVERHYBRID_HEADER
39#define OPENRS_INCOMPFLOWSOLVERHYBRID_HEADER
41#include <opm/common/ErrorMacros.hpp>
42#include <opm/grid/utility/SparseTable.hpp>
46#include <opm/common/utility/platform_dependent/disable_warnings.h>
48#include <unordered_map>
50#include <dune/common/fvector.hh>
51#include <dune/common/fmatrix.hh>
53#include <dune/istl/bvector.hh>
54#include <dune/istl/bcrsmatrix.hh>
55#include <dune/istl/operators.hh>
56#include <dune/istl/io.hh>
57#include <dune/istl/overlappingschwarz.hh>
58#include <dune/istl/schwarz.hh>
59#include <dune/istl/preconditioners.hh>
60#include <dune/istl/solvers.hh>
61#include <dune/istl/owneroverlapcopy.hh>
62#include <dune/istl/paamg/amg.hh>
63#include <dune/common/version.hh>
64#include <dune/istl/paamg/fastamg.hh>
65#include <dune/istl/paamg/kamg.hh>
66#include <dune/istl/paamg/pinfo.hh>
68#include <opm/common/utility/platform_dependent/reenable_warnings.h>
108 bool topologyIsSane(
const GI& g)
110 typedef typename GI::CellIterator CI;
111 typedef typename CI::FaceIterator FI;
113 bool sane = g.numberOfCells() >= 0;
115 for (CI c = g.cellbegin(); sane && c != g.cellend(); ++c) {
118 for (FI f = c->facebegin(); f != c->faceend(); ++f) {
119 if (!f->boundary()) {
120 n.push_back(f->neighbourCellIndex());
123 std::ranges::sort(n);
125 sane = std::unique(n.begin(), n.end()) == n.end();
165 axpby(
const T& a,
const T& b) : a_(a), b_(b) {}
179 T operator()(
const T& x,
const T& y)
361 template <
class GridInterface,
364 template <
class Gr
idIF,
class RockIF>
class InnerProduct>
371 typedef typename GridInterface::Scalar Scalar;
377 enum FaceType { Internal, Dirichlet, Neumann, Periodic };
388 typedef typename GridInterface::Scalar Scalar;
393 typedef typename GridInterface::CellIterator CI;
397 typedef typename CI ::FaceIterator FI;
410 Scalar pressure(
const CI& c)
const
412 return pressure_[cellno_[c->index()]];
425 Scalar outflux (
const FI& f)
const
427 return outflux_[cellno_[f->cellIndex()]][f->localIndex()];
429 Scalar outflux (
int hf)
const
431 return outflux_.data(hf);
434 std::vector< int > cellno_;
435 Opm::SparseTable< int > cellFaces_;
436 std::vector<Scalar> pressure_;
437 Opm::SparseTable<Scalar> outflux_;
440 std::vector<int>().swap(cellno_);
443 std::vector<Scalar>().swap(pressure_);
475 template<
class Po
int>
476 void init(
const GridInterface& g,
477 const RockInterface& r,
479 const BCInterface& bc)
483 if (g.numberOfCells() > 0) {
501 num_internal_faces_ = 0;
502 total_num_faces_ = 0;
503 matrix_structure_valid_ =
false;
504 do_regularization_ =
true;
506 bdry_id_map_.clear();
508 std::vector<Scalar>().swap(L_);
509 std::vector<Scalar>().swap(g_);
512 flowSolution_.clear();
514 cleared_state_ =
true;
534 const BCInterface& bc)
537 assert (cleared_state_);
539 assert (topologyIsSane(g));
542 allocateConnections(bc);
564 template<
class Po
int>
569 assert (matrix_structure_valid_);
571 typedef typename GridInterface::CellIterator CI;
572 const Opm::SparseTable<int>& cf = flowSolution_.cellFaces_;
575 for (CI c = pgrid_->cellbegin(); c != pgrid_->cellend(); ++c, ++i) {
576 ip_.buildStaticContrib(c, r, grav, cf.rowSize(i));
657 template<
class Flu
idInterface>
658 void solve(
const FluidInterface& r ,
659 const std::vector<double>& sat,
660 const BCInterface& bc ,
661 const std::vector<double>& src,
662 double residual_tolerance = 1e-8,
663 int linsolver_verbosity = 1,
664 int linsolver_type = 1,
665 bool same_matrix =
false,
666 int linsolver_maxit = 0,
667 double prolongate_factor = 1.6,
668 int smooth_steps = 1)
670 assembleDynamic(r, sat, bc, src);
674 switch (linsolver_type) {
676 solveLinearSystem(residual_tolerance, linsolver_verbosity, linsolver_maxit);
679 solveLinearSystemAMG(residual_tolerance, linsolver_verbosity,
680 linsolver_maxit, prolongate_factor, same_matrix, smooth_steps);
684 solveLinearSystemKAMG(residual_tolerance, linsolver_verbosity,
685 linsolver_maxit, prolongate_factor, same_matrix,smooth_steps);
688 solveLinearSystemFastAMG(residual_tolerance, linsolver_verbosity,
689 linsolver_maxit, prolongate_factor, same_matrix,smooth_steps);
692 std::cerr <<
"Unknown linsolver_type: " << linsolver_type <<
'\n';
693 throw std::runtime_error(
"Unknown linsolver_type");
695 computePressureAndFluxes(r, sat);
703 explicit FaceFluxes(
int sz)
704 : fluxes_(sz, 0.0), visited_(sz, 0), max_modification_(0.0)
707 void put(
double flux,
int f_ix) {
708 assert(visited_[f_ix] == 0 || visited_[f_ix] == 1);
709 double sign = visited_[f_ix] ? -1.0 : 1.0;
710 fluxes_[f_ix] += sign*flux;
713 void get(
double& flux,
int f_ix) {
714 assert(visited_[f_ix] == 0 || visited_[f_ix] == 1);
715 double sign = visited_[f_ix] ? -1.0 : 1.0;
716 double new_flux = 0.5*sign*fluxes_[f_ix];
717 double diff = std::fabs(flux - new_flux);
718 max_modification_ = std::max(max_modification_, diff);
724 std::ranges::fill(visited_, 0);
727 double maxMod()
const
729 return max_modification_;
732 std::vector<double> fluxes_;
733 std::vector<int> visited_;
734 double max_modification_;
749 typedef typename GridInterface::CellIterator CI;
750 typedef typename CI ::FaceIterator FI;
751 const std::vector<int>& cell = flowSolution_.cellno_;
752 const Opm::SparseTable<int>& cf = flowSolution_.cellFaces_;
753 Opm::SparseTable<double>& cflux = flowSolution_.outflux_;
755 FaceFluxes face_fluxes(pgrid_->numberOfFaces());
757 for (CI c = pgrid_->cellbegin(); c != pgrid_->cellend(); ++c) {
758 const int cell_index = cell[c->index()];
759 for (FI f = c->facebegin(); f != c->faceend(); ++f) {
760 int f_ix = cf[cell_index][f->localIndex()];
761 double flux = cflux[cell_index][f->localIndex()];
763 if (ppartner_dof_.empty()) {
766 int partner_f_ix = ppartner_dof_[f_ix];
767 if (partner_f_ix != -1) {
768 face_fluxes.put(flux, f_ix);
769 face_fluxes.put(flux, partner_f_ix);
772 face_fluxes.put(flux, f_ix);
776 face_fluxes.resetVisited();
778 for (CI c = pgrid_->cellbegin(); c != pgrid_->cellend(); ++c) {
779 const int cell_index = cell[c->index()];
780 for (FI f = c->facebegin(); f != c->faceend(); ++f) {
781 int f_ix = cf[cell_index][f->localIndex()];
782 double& flux = cflux[cell_index][f->localIndex()];
784 if (ppartner_dof_.empty()) {
787 int partner_f_ix = ppartner_dof_[f_ix];
788 if (partner_f_ix != -1) {
789 face_fluxes.get(flux, f_ix);
791 face_fluxes.get(dummy, partner_f_ix);
792 assert(dummy == flux);
795 face_fluxes.get(flux, f_ix);
799 return face_fluxes.maxMod();
823 return flowSolution_;
841 template<
typename charT,
class traits>
844 os <<
"IncompFlowSolverHybrid<>:\n"
845 <<
"\tMaximum number of cell faces = " << max_ncf_ <<
'\n'
846 <<
"\tNumber of internal faces = " << num_internal_faces_ <<
'\n'
847 <<
"\tTotal number of faces = " << total_num_faces_ <<
'\n';
849 const std::vector<int>& cell = flowSolution_.cellno_;
850 os <<
"cell index map = [";
851 std::ranges::copy(cell, std::ostream_iterator<int>(os,
" "));
854 const Opm::SparseTable<int>& cf = flowSolution_.cellFaces_;
855 os <<
"cell faces =\n";
856 for (
int i = 0; i < cf.size(); ++i)
858 os <<
"\t[" << i <<
"] -> [";
859 std::ranges::copy(cf[i], std::ostream_iterator<int>(os,
","));
888 writeMatrixToMatlab(S_, prefix +
"-mat.dat");
890 std::string rhsfile(prefix +
"-rhs.dat");
891 std::ofstream rhs(rhsfile.c_str());
893 rhs.setf(std::ios::scientific | std::ios::showpos);
894 std::ranges::copy(rhs_, std::ostream_iterator<VectorBlockType>(rhs,
"\n"));
898 typedef std::pair<int,int> DofID;
899 typedef std::unordered_map<int,DofID> BdryIdMapType;
900 typedef BdryIdMapType::const_iterator BdryIdMapIterator;
902 const GridInterface* pgrid_;
903 BdryIdMapType bdry_id_map_;
904 std::vector<int> ppartner_dof_;
906 InnerProduct<GridInterface, RockInterface> ip_;
911 int num_internal_faces_;
912 int total_num_faces_;
915 std::vector<Scalar> L_, g_;
916 Opm::SparseTable<Scalar> F_ ;
920 typedef Dune::FieldVector<Scalar, 1 > VectorBlockType;
921 typedef Dune::FieldMatrix<Scalar, 1, 1> MatrixBlockType;
923 Dune::BCRSMatrix <MatrixBlockType> S_;
924 Dune::BlockVector<VectorBlockType> rhs_;
925 Dune::BlockVector<VectorBlockType> soln_;
926 bool matrix_structure_valid_;
927 bool do_regularization_;
931 FlowSolution flowSolution_;
935 void enumerateDof(
const GridInterface& g,
const BCInterface& bc)
939 enumerateBCDof(g, bc);
942 cleared_state_ =
false;
946 void enumerateGridDof(
const GridInterface& g)
949 typedef typename GridInterface::CellIterator CI;
950 typedef typename CI ::FaceIterator FI;
952 const int nc = g.numberOfCells();
953 std::vector<int> fpos ; fpos.reserve(nc + 1);
954 std::vector<int> num_cf(nc) ;
955 std::vector<int> faces ;
958 std::vector<int>(nc, -1).swap(flowSolution_.cellno_);
960 std::vector<int>& cell = flowSolution_.cellno_;
963 int cellno = 0; fpos.push_back(0);
965 for (CI c = g.cellbegin(); c != g.cellend(); ++c, ++cellno) {
966 const int c0 = c->index();
967 assert((0 <= c0) && (c0 < nc) && (cell[c0] == -1));
971 int& ncf = num_cf[c0];
973 for (FI f = c->facebegin(); f != c-> faceend(); ++f) {
974 if (!f->boundary()) {
975 const int c1 = f->neighbourCellIndex();
976 assert((0 <= c1) && (c1 < nc) && (c1 != c0));
978 if (cell[c1] == -1) {
986 fpos.push_back(
int(faces.size()));
987 max_ncf_ = std::max(max_ncf_, ncf);
990 assert (cellno == nc);
992 total_num_faces_ = num_internal_faces_ = int(faces.size());
994 ip_.init(max_ncf_); ip_.reserveMatrices(num_cf);
995 F_ .reserve(nc, tot_ncf);
997 flowSolution_.cellFaces_.reserve(nc, tot_ncf);
998 flowSolution_.outflux_ .reserve(nc, tot_ncf);
1000 Opm::SparseTable<int>& cf = flowSolution_.cellFaces_;
1003 std::vector<int> l2g; l2g .reserve(max_ncf_);
1004 std::vector<Scalar> F_alloc; F_alloc .reserve(max_ncf_);
1007 typedef std::vector<int>::iterator VII;
1008 for (CI c = g.cellbegin(); c != g.cellend(); ++c) {
1009 const int c0 = c->index();
1011 assert ((0 <= c0 ) && ( c0 < nc) &&
1012 (0 <= cell[c0]) && (cell[c0] < nc));
1014 const int ncf = num_cf[cell[c0]];
1015 l2g .resize(ncf , 0 );
1016 F_alloc .resize(ncf , Scalar(0.0));
1018 for (FI f = c->facebegin(); f != c->faceend(); ++f) {
1019 if (f->boundary()) {
1021 l2g[f->localIndex()] = total_num_faces_++;
1033 const int c1 = f->neighbourCellIndex();
1034 assert ((0 <= c1 ) && ( c1 < nc) &&
1035 (0 <= cell[c1]) && (cell[c1] < nc));
1037 int t = c0, seek = c1;
1038 if (cell[seek] < cell[t])
1041 int s = fpos[cell[t]], e = fpos[cell[t] + 1];
1043 VII p = std::find(faces.begin() + s, faces.begin() + e, seek);
1044 assert(p != faces.begin() + e);
1046 l2g[f->localIndex()] = s + (p - (faces.begin() + s));
1050 cf.appendRow (l2g .begin(), l2g .end());
1051 F_.appendRow (F_alloc.begin(), F_alloc.end());
1053 flowSolution_.outflux_
1054 .appendRow(F_alloc.begin(), F_alloc.end());
1060 void enumerateBCDof(
const GridInterface& g,
const BCInterface& bc)
1063 typedef typename GridInterface::CellIterator CI;
1064 typedef typename CI ::FaceIterator FI;
1066 const std::vector<int>& cell = flowSolution_.cellno_;
1067 const Opm::SparseTable<int>& cf = flowSolution_.cellFaces_;
1069 bdry_id_map_.clear();
1070 for (CI c = g.cellbegin(); c != g.cellend(); ++c) {
1071 for (FI f = c->facebegin(); f != c->faceend(); ++f) {
1072 if (f->boundary() && bc.flowCond(*f).isPeriodic()) {
1073 const int bid = f->boundaryId();
1074 DofID dof(cell[c->index()], f->localIndex());
1075 bdry_id_map_.insert(std::make_pair(bid, dof));
1080 ppartner_dof_.clear();
1081 if (!bdry_id_map_.empty()) {
1082 ppartner_dof_.assign(total_num_faces_, -1);
1083 for (CI c = g.cellbegin(); c != g.cellend(); ++c) {
1084 for (FI f = c->facebegin(); f != c->faceend(); ++f) {
1085 if (f->boundary() && bc.flowCond(*f).isPeriodic()) {
1086 const int dof1 = cf[cell[c->index()]][f->localIndex()];
1088 BdryIdMapIterator
j =
1089 bdry_id_map_.find(bc.getPeriodicPartner(f->boundaryId()));
1090 assert (
j != bdry_id_map_.end());
1091 const int dof2 = cf[
j->second.first][
j->second.second];
1093 ppartner_dof_[dof1] = dof2;
1094 ppartner_dof_[dof2] = dof1;
1104 void allocateConnections(
const BCInterface& bc)
1108 assert(!cleared_state_);
1110 assert (!matrix_structure_valid_);
1113 S_.setSize(total_num_faces_, total_num_faces_);
1114 S_.setBuildMode(Dune::BCRSMatrix<MatrixBlockType>::random);
1117 for (
int f = 0; f < total_num_faces_; ++f) {
1118 S_.setrowsize(f, 1);
1121 allocateGridConnections();
1122 allocateBCConnections(bc);
1126 rhs_ .resize(total_num_faces_);
1127 soln_.resize(total_num_faces_);
1132 void allocateGridConnections()
1135 const Opm::SparseTable<int>& cf = flowSolution_.cellFaces_;
1137 for (
int c = 0; c < cf.size(); ++c) {
1138 const int nf = cf[c].size();
1139 for (
auto& f : cf[c]) {
1140 S_.incrementrowsize(f, nf - 1);
1147 void allocateBCConnections(
const BCInterface& bc)
1163 typedef typename GridInterface::CellIterator CI;
1164 typedef typename CI ::FaceIterator FI;
1166 const std::vector<int>& cell = flowSolution_.cellno_;
1167 const Opm::SparseTable<int>& cf = flowSolution_.cellFaces_;
1169 if (!bdry_id_map_.empty()) {
1172 for (CI c = pgrid_->cellbegin(); c != pgrid_->cellend(); ++c) {
1173 for (FI f = c->facebegin(); f != c->faceend(); ++f) {
1174 if (f->boundary() && bc.flowCond(*f).isPeriodic()) {
1176 const int dof1 = cf[cell[c->index()]][f->localIndex()];
1179 BdryIdMapIterator
j =
1180 bdry_id_map_.find(bc.getPeriodicPartner(f->boundaryId()));
1181 assert (
j != bdry_id_map_.end());
1182 const int c2 =
j->second.first;
1183 const int dof2 = cf[c2][
j->second.second];
1189 const int ndof = cf.rowSize(c2);
1190 S_.incrementrowsize(dof1, ndof);
1191 for (
int dof = 0; dof < ndof; ++dof) {
1192 int ii = cf[c2][dof];
1193 int pp = ppartner_dof_[ii];
1194 if ((pp != -1) && (pp != dof1) && (pp < ii)) {
1195 S_.incrementrowsize(pp, 1);
1197 S_.incrementrowsize(ii, 1);
1209 void setConnections(
const BCInterface& bc)
1212 setGridConnections();
1213 setBCConnections(bc);
1217 const int nc = pgrid_->numberOfCells();
1218 std::vector<Scalar>(nc).swap(flowSolution_.pressure_);
1219 std::vector<Scalar>(nc).swap(g_);
1220 std::vector<Scalar>(nc).swap(L_);
1222 matrix_structure_valid_ =
true;
1227 void setGridConnections()
1230 const Opm::SparseTable<int>& cf = flowSolution_.cellFaces_;
1233 for (
int c = 0; c < cf.size(); ++c) {
1234 auto fb = cf[c].begin(), fe = cf[c].end();
1236 for (
auto i = fb; i != fe; ++i) {
1237 for (
auto j = fb;
j != fe; ++
j) {
1238 S_.addindex(*i, *
j);
1246 void setBCConnections(
const BCInterface& bc)
1262 typedef typename GridInterface::CellIterator CI;
1263 typedef typename CI ::FaceIterator FI;
1265 const std::vector<int>& cell = flowSolution_.cellno_;
1266 const Opm::SparseTable<int>& cf = flowSolution_.cellFaces_;
1268 if (!bdry_id_map_.empty()) {
1271 for (CI c = pgrid_->cellbegin(); c != pgrid_->cellend(); ++c) {
1272 for (FI f = c->facebegin(); f != c->faceend(); ++f) {
1273 if (f->boundary() && bc.flowCond(*f).isPeriodic()) {
1275 const int dof1 = cf[cell[c->index()]][f->localIndex()];
1278 BdryIdMapIterator
j =
1279 bdry_id_map_.find(bc.getPeriodicPartner(f->boundaryId()));
1280 assert (
j != bdry_id_map_.end());
1281 const int c2 =
j->second.first;
1282 const int dof2 = cf[c2][
j->second.second];
1287 const int ndof = cf.rowSize(c2);
1288 for (
int dof = 0; dof < ndof; ++dof) {
1289 int ii = cf[c2][dof];
1290 int pp = ppartner_dof_[ii];
1291 if ((pp != -1) && (pp != dof1) && (pp < ii)) {
1294 S_.addindex(dof1, ii);
1295 S_.addindex(ii, dof1);
1296 S_.addindex(dof2, ii);
1297 S_.addindex(ii, dof2);
1309 template<
class Flu
idInterface>
1310 void assembleDynamic(
const FluidInterface& fl ,
1311 const std::vector<double>& sat,
1312 const BCInterface& bc ,
1313 const std::vector<double>& src)
1316 typedef typename GridInterface::CellIterator CI;
1318 const std::vector<int>& cell = flowSolution_.cellno_;
1319 const Opm::SparseTable<int>& cf = flowSolution_.cellFaces_;
1321 std::vector<Scalar> data_store(max_ncf_ * max_ncf_);
1322 std::vector<Scalar> e (max_ncf_);
1323 std::vector<Scalar> rhs (max_ncf_);
1324 std::vector<Scalar> gflux (max_ncf_);
1326 std::vector<FaceType> facetype (max_ncf_);
1327 std::vector<Scalar> condval (max_ncf_);
1328 std::vector<int> ppartner (max_ncf_);
1334 std::ranges::fill(g_, Scalar(0.0));
1335 std::ranges::fill(L_, Scalar(0.0));
1336 std::ranges::fill(e, Scalar(1.0));
1340 do_regularization_ =
true;
1343 for (CI c = pgrid_->cellbegin(); c != pgrid_->cellend(); ++c) {
1344 const int ci = c->index();
1345 const int c0 = cell[ci]; assert (c0 < cf.size());
1346 const int nf = cf[c0].size();
1351 setExternalContrib(c, c0, bc, src[ci], rhs,
1352 facetype, condval, ppartner);
1354 ip_.computeDynamicParams(c, fl, sat);
1357 ip_.getInverseMatrix(c, S);
1359 std::ranges::fill(gflux, Scalar(0.0));
1360 ip_.gravityFlux(c, gflux);
1363 buildCellContrib(c0, one, gflux, S, rhs);
1365 addCellContrib(S, rhs, facetype, condval, ppartner, cf[c0]);
1372 void solveLinearSystem(
double residual_tolerance,
int verbosity_level,
int maxit)
1376 Scalar residTol = residual_tolerance;
1378 typedef Dune::BCRSMatrix <MatrixBlockType> MatrixT;
1379 typedef Dune::BlockVector<VectorBlockType> VectorT;
1380 typedef Dune::MatrixAdapter<Matrix,Vector,Vector> Adapter;
1383 if (do_regularization_) {
1389 Dune::SeqILU<MatrixT,VectorT,VectorT> precond(S_, 1.0);
1392 Dune::CGSolver<VectorT> linsolve(opS, precond, residTol,
1393 (maxit>0)?maxit:S_.N(), verbosity_level);
1395 Dune::InverseOperatorResult result;
1400 linsolve.apply(soln_, rhs_, result);
1401 if (!result.converged) {
1402 OPM_THROW(std::runtime_error,
1403 "Linear solver failed to converge in " +
1404 std::to_string(result.iterations) +
" iterations.\n"
1405 "Residual reduction achieved is " +
1406 std::to_string(result.reduction) +
'\n');
1415 typedef Dune::BCRSMatrix <MatrixBlockType> Matrix;
1416 typedef Dune::BlockVector<VectorBlockType> Vector;
1417 typedef Dune::MatrixAdapter<Matrix,Vector,Vector> Operator;
1422#ifndef FIRST_DIAGONAL
1423#define FIRST_DIAGONAL 1
1429#define SMOOTHER_ILU 1
1432#define SMOOTHER_BGS 0
1434#ifndef ANISOTROPIC_3D
1435#define ANISOTROPIC_3D 0
1439 typedef Dune::Amg::FirstDiagonal CouplingMetric;
1441 typedef Dune::Amg::RowSum CouplingMetric;
1445 typedef Dune::Amg::SymmetricCriterion<Matrix,CouplingMetric> CriterionBase;
1447 typedef Dune::Amg::UnSymmetricCriterion<Matrix,CouplingMetric> CriterionBase;
1451 typedef Dune::SeqOverlappingSchwarz<Matrix,Vector,Dune::MultiplicativeSchwarzMode> Smoother;
1454 typedef Dune::SeqILU<Matrix,Vector,Vector> Smoother;
1456 typedef Dune::SeqSSOR<Matrix,Vector,Vector> Smoother;
1459 typedef Dune::Amg::CoarsenCriterion<CriterionBase> Criterion;
1463 std::unique_ptr<Operator> opS_;
1464 typedef Dune::Preconditioner<Vector,Vector> PrecondBase;
1465 std::unique_ptr<PrecondBase> precond_;
1469 void solveLinearSystemAMG(
double residual_tolerance,
int verbosity_level,
1470 int maxit,
double prolong_factor,
bool same_matrix,
int smooth_steps)
1473 typedef Dune::Amg::AMG<Operator,Vector,Smoother,Dune::Amg::SequentialInformation>
1477 Scalar residTol = residual_tolerance;
1481 if (do_regularization_) {
1484 opS_.reset(
new Operator(S_));
1488 typename Precond::SmootherArgs smootherArgs;
1489 smootherArgs.relaxationFactor = relax;
1491 smootherArgs.overlap = Precond::SmootherArgs::none;
1492 smootherArgs.onthefly =
false;
1494 Criterion criterion;
1495 criterion.setDebugLevel(verbosity_level);
1497 criterion.setDefaultValuesAnisotropic(3, 2);
1499 criterion.setProlongationDampingFactor(prolong_factor);
1500 criterion.setBeta(1e-10);
1501 criterion.setNoPreSmoothSteps(smooth_steps);
1502 criterion.setNoPostSmoothSteps(smooth_steps);
1503 criterion.setGamma(1);
1504 precond_.reset(
new Precond(*opS_, criterion, smootherArgs));
1507 Dune::CGSolver<Vector> linsolve(*opS_,
dynamic_cast<Precond&
>(*precond_), residTol, (maxit>0)?maxit:S_.N(), verbosity_level);
1509 Dune::InverseOperatorResult result;
1513 typedef typename Dune::BCRSMatrix <MatrixBlockType>::ConstRowIterator RowIter;
1514 typedef typename Dune::BCRSMatrix <MatrixBlockType>::ConstColIterator ColIter;
1515 for(RowIter ri=S_.begin(); ri!=S_.end(); ++ri){
1516 bool isDirichlet=
true;
1517 for(ColIter ci=ri->begin(); ci!=ri->end(); ++ci)
1518 if(ci.index()!=ri.index() && *ci!=0.0)
1521 soln_[ri.index()]=rhs_[ri.index()]/S_[ri.index()][ri.index()];
1525 linsolve.apply(soln_, rhs_, result);
1526 if (!result.converged) {
1527 OPM_THROW(std::runtime_error,
1528 "Linear solver failed to converge in " +
1529 std::to_string(result.iterations) +
" iterations.\n"
1530 "Residual reduction achieved is " +
1531 std::to_string(result.reduction) +
'\n');
1538 void solveLinearSystemFastAMG(
double residual_tolerance,
int verbosity_level,
1539 int maxit,
double prolong_factor,
bool same_matrix,
int smooth_steps)
1542 typedef Dune::Amg::FastAMG<Operator,Vector> Precond;
1545 Scalar residTol = residual_tolerance;
1549 if (do_regularization_) {
1555 typedef Dune::Amg::AggregationCriterion<Dune::Amg::SymmetricMatrixDependency<Matrix,CouplingMetric> > CritBase;
1557 typedef Dune::Amg::CoarsenCriterion<CritBase> Crit;
1559 criterion.setDebugLevel(verbosity_level);
1561 criterion.setDefaultValuesAnisotropic(3, 2);
1563 criterion.setProlongationDampingFactor(prolong_factor);
1564 criterion.setBeta(1e-10);
1565 Dune::Amg::Parameters parms;
1566 parms.setDebugLevel(verbosity_level);
1567 parms.setNoPreSmoothSteps(smooth_steps);
1568 parms.setNoPostSmoothSteps(smooth_steps);
1569 precond_.reset(
new Precond(*opS_, criterion, parms));
1572 Dune::GeneralizedPCGSolver<Vector> linsolve(*opS_,
dynamic_cast<Precond&
>(*precond_), residTol, (maxit>0)?maxit:S_.N(), verbosity_level);
1574 Dune::InverseOperatorResult result;
1579 typedef typename Dune::BCRSMatrix <MatrixBlockType>::ConstRowIterator RowIter;
1580 typedef typename Dune::BCRSMatrix <MatrixBlockType>::ConstColIterator ColIter;
1581 for(RowIter ri=S_.begin(); ri!=S_.end(); ++ri){
1582 bool isDirichlet=
true;
1583 for(ColIter ci=ri->begin(); ci!=ri->end(); ++ci)
1584 if(ci.index()!=ri.index() && *ci!=0.0)
1587 soln_[ri.index()]=rhs_[ri.index()]/S_[ri.index()][ri.index()];
1591 linsolve.apply(soln_, rhs_, result);
1592 if (!result.converged) {
1593 OPM_THROW(std::runtime_error,
1594 "Linear solver failed to converge in " +
1595 std::to_string(result.iterations) +
" iterations.\n"
1596 "Residual reduction achieved is " +
1597 std::to_string(result.reduction) +
'\n');
1603 void solveLinearSystemKAMG(
double residual_tolerance,
int verbosity_level,
1604 int maxit,
double prolong_factor,
bool same_matrix,
int smooth_steps)
1608 typedef Dune::Amg::KAMG<Operator,Vector,Smoother,Dune::Amg::SequentialInformation> Precond;
1610 Scalar residTol = residual_tolerance;
1613 if (do_regularization_) {
1620 typename Precond::SmootherArgs smootherArgs;
1621 smootherArgs.relaxationFactor = relax;
1623 smootherArgs.overlap = Precond::SmootherArgs::none;
1624 smootherArgs.onthefly =
false;
1626 Criterion criterion;
1627 criterion.setDebugLevel(verbosity_level);
1629 criterion.setDefaultValuesAnisotropic(3, 2);
1631 criterion.setProlongationDampingFactor(prolong_factor);
1632 criterion.setBeta(1e-10);
1633 criterion.setNoPreSmoothSteps(smooth_steps);
1634 criterion.setNoPostSmoothSteps(smooth_steps);
1635 criterion.setGamma(2);
1636 precond_.reset(
new Precond(*opS_, criterion, smootherArgs));
1639 Dune::CGSolver<Vector> linsolve(*opS_,
dynamic_cast<Precond&
>(*precond_), residTol, (maxit>0)?maxit:S_.N(), verbosity_level);
1641 Dune::InverseOperatorResult result;
1645 typedef typename Dune::BCRSMatrix <MatrixBlockType>::ConstRowIterator RowIter;
1646 typedef typename Dune::BCRSMatrix <MatrixBlockType>::ConstColIterator ColIter;
1647 for(RowIter ri=S_.begin(); ri!=S_.end(); ++ri){
1648 bool isDirichlet=
true;
1649 for(ColIter ci=ri->begin(); ci!=ri->end(); ++ci)
1650 if(ci.index()!=ri.index() && *ci!=0.0)
1653 soln_[ri.index()]=rhs_[ri.index()]/S_[ri.index()][ri.index()];
1657 linsolve.apply(soln_, rhs_, result);
1658 if (!result.converged) {
1659 OPM_THROW(std::runtime_error,
1660 "Linear solver failed to converge in " +
1661 std::to_string(result.iterations) +
" iterations.\n"
1662 "Residual reduction achieved is " +
1663 std::to_string(result.reduction) +
'\n');
1671 template<
class Flu
idInterface>
1672 void computePressureAndFluxes(
const FluidInterface& r ,
1673 const std::vector<double>& sat)
1676 typedef typename GridInterface::CellIterator CI;
1678 const std::vector<int>& cell = flowSolution_.cellno_;
1679 const Opm::SparseTable<int>& cf = flowSolution_.cellFaces_;
1681 std::vector<Scalar>& p = flowSolution_.pressure_;
1682 Opm::SparseTable<Scalar>& v = flowSolution_.outflux_;
1685 std::vector<double> pi (max_ncf_);
1686 std::vector<double> gflux(max_ncf_);
1687 std::vector<double> Binv_storage(max_ncf_ * max_ncf_);
1690 for (CI c = pgrid_->cellbegin(); c != pgrid_->cellend(); ++c) {
1691 const int c0 = cell[c->index()];
1692 const int nf = cf.rowSize(c0);
1698 for (
int i = 0; i < nf; ++i) {
1699 pi[i] = soln_[cf[c0][i]];
1704 std::inner_product(F_[c0].begin(), F_[c0].end(),
1705 pi.begin(), 0.0)) / L_[c0];
1707 std::ranges::transform(pi, pi.begin(),
1708 [&p, c0](
const double& input) { return p[c0] - input; });
1715 ip_.computeDynamicParams(c, r, sat);
1718 ip_.getInverseMatrix(c, Binv);
1719 vecMulAdd_N(Scalar(1.0), Binv, &pi[0], Scalar(0.0), &v[c0][0]);
1723 ip_.gravityFlux(c, gflux);
1724 std::ranges::transform(gflux, v[c0], v[c0].begin(), std::plus<Scalar>());
1732 void setExternalContrib(
const typename GridInterface::CellIterator c,
1733 const int c0,
const BCInterface& bc,
1735 std::vector<Scalar>& rhs,
1736 std::vector<FaceType>& facetype,
1737 std::vector<double>& condval,
1738 std::vector<int>& ppartner)
1741 typedef typename GridInterface::CellIterator::FaceIterator FI;
1743 const Opm::SparseTable<int>& cf = flowSolution_.cellFaces_;
1745 std::ranges::fill(rhs, Scalar(0.0));
1746 std::ranges::fill(facetype, Internal);
1747 std::ranges::fill(condval, Scalar(0.0));
1748 std::ranges::fill(ppartner, -1);
1753 for (FI f = c->facebegin(); f != c->faceend(); ++f, ++k) {
1754 if (f->boundary()) {
1755 const FlowBC& bcond = bc.flowCond(*f);
1756 if (bcond.isDirichlet()) {
1757 facetype[k] = Dirichlet;
1758 condval[k] = bcond.pressure();
1759 do_regularization_ =
false;
1760 }
else if (bcond.isPeriodic()) {
1761 BdryIdMapIterator
j =
1762 bdry_id_map_.find(bc.getPeriodicPartner(f->boundaryId()));
1763 assert (
j != bdry_id_map_.end());
1765 facetype[k] = Periodic;
1766 condval[k] = bcond.pressureDifference();
1767 ppartner[k] = cf[
j->second.first][
j->second.second];
1769 assert (bcond.isNeumann());
1770 facetype[k] = Neumann;
1771 rhs[k] = bcond.outflux();
1781 void buildCellContrib(
const int c ,
1783 const std::vector<Scalar>& gflux,
1785 std::vector<Scalar>& rhs)
1792 L_[c] = std::accumulate(Ft.data(), Ft.data() + Ft.numRows(), 0.0);
1793 g_[c] -= std::accumulate(gflux.begin(), gflux.end(), Scalar(0.0));
1796 std::ranges::transform(gflux, rhs, rhs.begin(), std::minus<Scalar>());
1799 std::transform(rhs.begin(), rhs.end(), Ft.data(),
1801 axpby<Scalar>(Scalar(1.0), Scalar(g_[c] / L_[c])));
1813 const std::vector<Scalar>& rhs ,
1814 const std::vector<FaceType>& facetype,
1815 const std::vector<Scalar>& condval ,
1816 const std::vector<int>& ppartner,
1821 for (
auto i = l2g.begin(); i != l2g.end(); ++i, ++r) {
1825 switch (facetype[r]) {
1831 S_ [ii][ii] = S(r,r);
1832 rhs_[ii] = S(r,r) * condval[r];
1847 assert ((0 <= ppartner[r]) && (ppartner[r] <
int(rhs_.size())));
1848 assert (ii != ppartner[r]);
1851 const double a = S(r,r), b = a * condval[r];
1855 S_ [ ii][ppartner[r]] -= a;
1859 S_ [ppartner[r]][ ii] -= a;
1860 S_ [ppartner[r]][ppartner[r]] += a;
1861 rhs_[ppartner[r]] -= b;
1871 for (
auto j = l2g.begin();
j != l2g.end(); ++
j, ++c) {
1875 if (facetype[c] == Dirichlet) {
1876 rhs_[ii] -= S(r,c) * condval[c];
1879 if (facetype[c] == Periodic) {
1880 assert ((0 <= ppartner[c]) && (ppartner[c] <
int(rhs_.size())));
1881 assert (jj != ppartner[c]);
1882 if (ppartner[c] < jj) {
1883 rhs_[ii] -= S(r,c) * condval[c];
1887 S_[ii][jj] += S(r,c);
Solve mixed formulation of incompressible flow modelled by Darcy's law.
Definition: IncompFlowSolverHybrid.hpp:365
double postProcessFluxes()
Postprocess the solution fluxes. This method modifies the solution object so that out-fluxes of twin ...
Definition: IncompFlowSolverHybrid.hpp:747
SolutionType getSolution()
Recover the solution to the problem defined by the parameters to method.
Definition: IncompFlowSolverHybrid.hpp:821
void solve(const FluidInterface &r, const std::vector< double > &sat, const BCInterface &bc, const std::vector< double > &src, double residual_tolerance=1e-8, int linsolver_verbosity=1, int linsolver_type=1, bool same_matrix=false, int linsolver_maxit=0, double prolongate_factor=1.6, int smooth_steps=1)
Construct and solve system of linear equations for the pressure values on each interface/contact betw...
Definition: IncompFlowSolverHybrid.hpp:658
const FlowSolution & SolutionType
Type representing the solution to the problem defined by the parameters to.
Definition: IncompFlowSolverHybrid.hpp:811
void printSystem(const std::string &prefix)
Output current system of linear equations to permanent storage in files. One file for the coefficient...
Definition: IncompFlowSolverHybrid.hpp:886
void computeInnerProducts(const RockInterface &r, const Point &grav)
Compute static (i.e., independent of saturation) parts of the spatially varying inner products for e...
Definition: IncompFlowSolverHybrid.hpp:565
void clear()
Clear all topologic, geometric and rock-dependent information currently held in internal data structu...
Definition: IncompFlowSolverHybrid.hpp:497
void printStats(std::basic_ostream< charT, traits > &os)
Print statistics about the connections in the current model. This is mostly for debugging purposes an...
Definition: IncompFlowSolverHybrid.hpp:842
void init(const GridInterface &g, const RockInterface &r, const Point &grav, const BCInterface &bc)
All-in-one initialization routine. Enumerates all grid connections, allocates sufficient space,...
Definition: IncompFlowSolverHybrid.hpp:476
void initSystemStructure(const GridInterface &g, const BCInterface &bc)
Compute structure of coefficient matrix in final system of linear equations for this flow problem....
Definition: IncompFlowSolverHybrid.hpp:533
Dune::MatrixAdapter< Matrix, Vector, Vector > Operator
A linear operator.
Definition: elasticity_preconditioners.hpp:49
min[0]
Definition: elasticity_upscale_impl.hpp:146
int j
Definition: elasticity_upscale_impl.hpp:658
Definition: ImplicitAssembly.hpp:43
void symmetricUpdate(const T &a1, const FullMatrix< T, StoragePolicy, FortranOrdering > &A, const T &a2, FullMatrix< T, StoragePolicy, FortranOrdering > &C)
Symmetric, rank update of symmetric matrix. Specifically, .
Definition: Matrix.hpp:829
const FullMatrix< double, ImmutableSharedData, FortranOrdering > ImmutableFortranMatrix
Definition: Matrix.hpp:590
void vecMulAdd_N(const T &a1, const FullMatrix< T, SP, FortranOrdering > &A, const std::vector< T > &x, const T &a2, std::vector< T > &y)
GEneral Matrix-Vector product (GAXPY operation). Specifically, .
Definition: Matrix.hpp:913
FullMatrix< double, SharedData, FortranOrdering > SharedFortranMatrix
Definition: Matrix.hpp:589
void matMulAdd_TN(const T &a1, const FullMatrix< T, SP1, FortranOrdering > &A, const FullMatrix< T, SP2, FortranOrdering > &B, const T &a2, FullMatrix< T, SP3, FortranOrdering > &C)
GEneral Matrix-Matrix product update of other matrix. Specificlly, .
Definition: Matrix.hpp:1252