28#ifndef TPFA_LINEARIZER_HH
29#define TPFA_LINEARIZER_HH
31#include <dune/common/version.hh>
32#include <dune/common/fvector.hh>
33#include <dune/common/fmatrix.hh>
35#include <opm/common/Exceptions.hpp>
36#include <opm/common/TimingMacros.hpp>
38#include <opm/common/utility/SparseTable.hpp>
40#include <opm/material/common/ConditionalStorage.hpp>
42#include <opm/input/eclipse/EclipseState/Grid/FaceDir.hpp>
43#include <opm/input/eclipse/Schedule/BCState.hpp>
54#include <opm/material/fluidsystems/BlackOilFluidSystem.hpp>
55#include <opm/material/fluidsystems/BlackOilFluidSystemNonStatic.hpp>
66#include <unordered_map>
74#include <fmt/format.h>
76#include <opm/common/utility/gpuDecorators.hpp>
77#include <opm/common/utility/pointerArithmetic.hpp>
89template<
class TypeTag>
101template<
class TypeTag>
122 using Element =
typename GridView::template Codim<0>::Entity;
123 using ElementIterator =
typename GridView::template Codim<0>::Iterator;
125 using Vector = GlobalEqVector;
129 enum { numEq = getPropValue<TypeTag, Properties::NumEq>() };
130 enum { historySize = getPropValue<TypeTag, Properties::TimeDiscHistorySize>() };
131 enum { dimWorld = GridView::dimensionworld };
133 constexpr static bool runAssemblyOnGpu = getPropValue<TypeTag, Properties::RunAssemblyOnGpu>();
135 using MatrixBlockCPU =
typename SparseMatrixAdapter::MatrixBlock;
136 using VectorBlockCPU = Dune::FieldVector<Scalar, numEq>;
140 friend GpuLinearizer;
141 using MatrixBlock =
typename GpuLinearizer::MatrixBlock;
142 using VectorBlock =
typename GpuLinearizer::VectorBlock;
143 using ADVectorBlock =
typename GpuLinearizer::ADVectorBlock;
145 static constexpr bool linearizeNonLocalElements =
146 getPropValue<TypeTag, Properties::LinearizeNonLocalElements>();
147 static constexpr bool enableFullyImplicitThermal = (getPropValue<TypeTag, Properties::EnergyModuleType>() == EnergyModules::FullyImplicitThermal);
148 static constexpr bool enableDiffusion = getPropValue<TypeTag, Properties::EnableDiffusion>();
149 static constexpr bool enableDispersion = getPropValue<TypeTag, Properties::EnableDispersion>();
150 static constexpr bool enablePolymer = getPropValue<TypeTag, Properties::EnablePolymer>();
151 static const bool enableBioeffects = getPropValue<TypeTag, Properties::EnableBioeffects>();
160 simulatorPtr_ =
nullptr;
161 separateSparseSourceTerms_ = Parameters::Get<Parameters::SeparateSparseSourceTerms>();
171 Parameters::Register<Parameters::EnableGpuAssemblySolverBridge>
172 (
"Pass source-only serial GPU assembly directly to gpuISTL.");
173 Parameters::Register<Parameters::SeparateSparseSourceTerms>
174 (
"Treat well source terms all in one go, instead of on a cell by cell basis.");
188 simulatorPtr_ = &simulator;
202 gpuLinearizer_.eraseMatrix();
238 catch (
const std::exception& e) {
239 std::cout <<
"rank " << simulator_().
gridView().comm().rank()
240 <<
" caught an exception while linearizing:" << e.what()
241 <<
"\n" << std::flush;
245 std::cout <<
"rank " << simulator_().
gridView().comm().rank()
246 <<
" caught an exception while linearizing"
247 <<
"\n" << std::flush;
250 OPM_TIMEBLOCK(linearizationSynch);
251 succeeded = simulator_().
gridView().comm().min(succeeded);
254 throw NumericalProblem(
"A process did not succeed in linearizing the system");
270 template <
class SubDomainType>
278 initFirstIteration_();
282 if (problem_().iterationContext().inLocalSolve()) {
293 { jacobian_->finalize(); }
297 decltype(
auto)
gpuJacobian() {
return gpuLinearizer_.gpuJacobian(); }
298 decltype(
auto)
gpuResidual() {
return gpuLinearizer_.gpuResidual(); }
307 OPM_TIMEBLOCK(linearizeAuxilaryEquations);
311 auto& model = model_();
312 const auto& comm = simulator_().
gridView().comm();
313 for (
unsigned auxModIdx = 0; auxModIdx < model.numAuxiliaryModules(); ++auxModIdx) {
314 bool succeeded =
true;
316 model.auxiliaryModule(auxModIdx)->linearize(*jacobian_, residual_);
318 catch (
const std::exception& e) {
321 std::cout <<
"rank " << simulator_().
gridView().comm().rank()
322 <<
" caught an exception while linearizing:" << e.what()
323 <<
"\n" << std::flush;
326 succeeded = comm.min(succeeded);
329 throw NumericalProblem(
"linearization of an auxiliary equation failed");
338 {
return *jacobian_; }
341 {
return *jacobian_; }
347 {
return residual_; }
350 {
return residual_; }
355 void exportSystem(
const int idx, std::string& tag,
const char *path=
"export")
357 const bool export_sparsity = exportIndex_ == -1;
360 exportCount_ = exportIndex_ == idx ? ++exportCount_ : 0;
362 tag = fmt::format(fmt::runtime(
"_{:03d}_{:02d}"), exportIndex_, exportCount_);
364 fmt::print(fmt::runtime(
"index = {:d}\n"), exportIndex_);
365 fmt::print(fmt::runtime(
"count = {:d}\n"), exportCount_);
369 gpuLinearizer_.copyJacobianToHost(*jacobian_);
370 Opm::exportSystem(jacobian_->istlMatrix(), residual_, export_sparsity, tag.c_str(), path);
374 { linearizationType_ = linearizationType; }
377 {
return linearizationType_; }
385 {
return flowsInfo_; }
393 {
return floresInfo_; }
402 {
return velocityInfo_; }
405 return neighborInfo_;
411 updateStoredTransmissibilities();
416 for (
auto& bdyInfo : boundaryInfo_) {
417 const auto [type, massrateAD] = problem_().boundaryCondition(bdyInfo.cell, bdyInfo.dir);
420 VectorBlockCPU massrate(0.0);
421 for (std::size_t ii = 0; ii < massrate.size(); ++ii) {
422 massrate[ii] = massrateAD[ii].value();
425 const auto& exFluidState = problem_().boundaryFluidState(bdyInfo.cell, bdyInfo.dir);
426 bdyInfo.bcdata.type = type;
427 bdyInfo.bcdata.massRate = massrate;
428 bdyInfo.bcdata.exFluidState = exFluidState;
441 template <
class SubDomainType>
445 initFirstIteration_();
447 for (
int globI : domain.cells) {
448 residual_[globI] = 0.0;
449 jacobian_->clearRow(globI, 0.0);
455 {
return *simulatorPtr_; }
457 const Simulator& simulator_()
const
458 {
return *simulatorPtr_; }
461 {
return simulator_().
problem(); }
463 const Problem& problem_()
const
464 {
return simulator_().
problem(); }
467 {
return simulator_().
model(); }
469 const Model& model_()
const
470 {
return simulator_().
model(); }
472 const GridView& gridView_()
const
473 {
return problem_().gridView(); }
475 void initFirstIteration_()
481 residual_.resize(model_().numTotalDof());
492 if (!neighborInfo_.empty()) {
497 const auto& model = model_();
498 Stencil stencil(gridView_(), model_().dofMapper());
502 using NeighborSet = std::set<unsigned>;
503 std::vector<NeighborSet> sparsityPattern(model.numTotalDof());
504 const Scalar gravity = problem_().gravity()[dimWorld - 1];
505 unsigned numCells = model.numTotalDof();
506 neighborInfo_.reserve(numCells, 6 * numCells);
507 std::vector<NeighborInfoCPU> loc_nbinfo;
508 for (
const auto& elem : elements(gridView_())) {
509 stencil.update(elem);
511 for (
unsigned primaryDofIdx = 0; primaryDofIdx < stencil.numPrimaryDof(); ++primaryDofIdx) {
512 const unsigned myIdx = stencil.globalSpaceIndex(primaryDofIdx);
513 loc_nbinfo.resize(stencil.numDof() - 1);
515 for (
unsigned dofIdx = 0; dofIdx < stencil.numDof(); ++dofIdx) {
516 const unsigned neighborIdx = stencil.globalSpaceIndex(dofIdx);
517 sparsityPattern[myIdx].insert(neighborIdx);
519 const Scalar trans = problem_().transmissibility(myIdx, neighborIdx);
520 const auto scvfIdx = dofIdx - 1;
521 const auto& scvf = stencil.interiorFace(scvfIdx);
522 const Scalar area = scvf.area();
523 const Scalar Vin = problem_().model().dofTotalVolume(myIdx);
524 const Scalar Vex = problem_().model().dofTotalVolume(neighborIdx);
525 const Scalar zIn = problem_().dofCenterDepth(myIdx);
526 const Scalar zEx = problem_().dofCenterDepth(neighborIdx);
527 const Scalar dZg = (zIn - zEx)*gravity;
528 const Scalar thpresInToEx = problem_().thresholdPressure(myIdx, neighborIdx);
529 const Scalar thpresExToIn = problem_().thresholdPressure(neighborIdx, myIdx);
530 const auto dirId = scvf.dirId();
531 auto faceDir = dirId < 0 ? FaceDir::DirEnum::Unknown
532 : FaceDir::FromIntersectionIndex(dirId);
533 ResidualNBInfo nbinfo{trans,
546 if constexpr (enableFullyImplicitThermal) {
547 nbinfo.inAlpha = problem_().thermalHalfTransmissibility(myIdx, neighborIdx);
548 nbinfo.outAlpha = problem_().thermalHalfTransmissibility(neighborIdx, myIdx);
550 if constexpr (enableDiffusion) {
551 nbinfo.diffusivity = problem_().diffusivity(myIdx, neighborIdx);
553 if constexpr (enableDispersion) {
554 nbinfo.dispersivity = problem_().dispersivity(myIdx, neighborIdx);
556 if constexpr (enablePolymer) {
557 nbinfo.dist = (stencil.subControlVolume(primaryDofIdx).globalPos()
558 - stencil.subControlVolume(dofIdx).globalPos())
561 loc_nbinfo[dofIdx - 1] = NeighborInfoCPU{neighborIdx, nbinfo,
nullptr};
564 neighborInfo_.appendRow(loc_nbinfo.begin(), loc_nbinfo.end());
565 if (problem_().nonTrivialBoundaryConditions()) {
566 for (
unsigned bfIndex = 0; bfIndex < stencil.numBoundaryFaces(); ++bfIndex) {
567 const auto& bf = stencil.boundaryFace(bfIndex);
568 const int dir_id = bf.dirId();
573 const auto [type, massrateAD] = problem_().boundaryCondition(myIdx, dir_id);
575 VectorBlockCPU massrate(0.0);
576 for (std::size_t ii = 0; ii < massrate.size(); ++ii) {
577 massrate[ii] = massrateAD[ii].value();
579 const auto& exFluidState = problem_().boundaryFluidState(myIdx, dir_id);
580 BoundaryConditionDataCPU bcdata {type,
582 exFluidState.pvtRegionIndex(),
585 bf.integrationPos()[dimWorld - 1],
587 boundaryInfo_.push_back({myIdx, dir_id, bfIndex, bcdata});
595 const std::size_t numAuxMod = model.numAuxiliaryModules();
596 for (
unsigned auxModIdx = 0; auxModIdx < numAuxMod; ++auxModIdx) {
597 model.auxiliaryModule(auxModIdx)->addNeighbors(sparsityPattern);
601 jacobian_ = std::make_unique<SparseMatrixAdapter>(simulator_());
602 diagMatAddress_.resize(numCells);
604 jacobian_->reserve(sparsityPattern);
605 for (
unsigned globI = 0; globI < numCells; globI++) {
606 const auto& nbInfos = neighborInfo_[globI];
607 diagMatAddress_[globI] = jacobian_->blockAddress(globI, globI);
608 for (
auto& nbInfo : nbInfos) {
609 nbInfo.matBlockAddress = jacobian_->blockAddress(nbInfo.neighbor, globI);
615 gpuLinearizer_.createMatrix(*jacobian_);
618 fullDomain_.
cells.resize(numCells);
619 std::iota(fullDomain_.
cells.begin(), fullDomain_.
cells.end(), 0);
630 gpuLinearizer_.resetSystem();
636 OPM_TIMEBLOCK(createFlows);
639 const bool anyFlows = simulator_().
problem().eclWriter().outputModule().getFlows().anyFlows();
640 const auto& blockFlows = simulator_().
problem().eclWriter().outputModule().getFlows().blockFlows();
641 const auto& blockVelocity = simulator_().
problem().eclWriter().outputModule().getFlows().blockVelocity();
642 const bool isTemp = simulator_().
vanguard().eclState().getSimulationConfig().isTemp();
643 const bool anyFlores = simulator_().
problem().eclWriter().outputModule().getFlows().anyFlores() || isTemp;
644 const bool dispersionActive = simulator_().
vanguard().eclState().getSimulationConfig().rock_config().dispersion();
645 if (!dispersionActive && !enableBioeffects && blockVelocity.empty()
646 && !((anyFlows || !blockFlows.empty()) && flowsInfo_.empty())
647 && !(anyFlores && floresInfo_.empty())) {
650 const auto& model = model_();
651 const auto& nncOutput = simulator_().
problem().eclWriter().getOutputNnc().front();
652 Stencil stencil(gridView_(), model_().dofMapper());
653 const unsigned numCells = model.numTotalDof();
654 std::unordered_multimap<int, std::pair<int, int>> nncIndices;
655 std::vector<FlowInfo> loc_flinfo;
656 std::vector<VelocityInfo> loc_vlinfo;
657 unsigned int nncId = 0;
658 VectorBlock flow(0.0);
661 for (
unsigned nncIdx = 0; nncIdx < nncOutput.size(); ++nncIdx) {
662 const int ci1 = nncOutput[nncIdx].cell1;
663 const int ci2 = nncOutput[nncIdx].cell2;
664 nncIndices.emplace(ci1, std::make_pair(ci2, nncIdx));
668 flowsInfo_.reserve(numCells, 6 * numCells);
670 else if (!blockFlows.empty()) {
671 flowsInfo_.reserve(numCells, 6 * blockFlows.size());
674 floresInfo_.reserve(numCells, 6 * numCells);
676 if (dispersionActive || enableBioeffects) {
677 velocityInfo_.reserve(numCells, 6 * numCells);
679 else if (!blockVelocity.empty()) {
680 velocityInfo_.reserve(numCells, 6 * blockVelocity.size());
683 for (
const auto& elem : elements(gridView_())) {
684 stencil.update(elem);
685 for (
unsigned primaryDofIdx = 0; primaryDofIdx < stencil.numPrimaryDof(); ++primaryDofIdx) {
686 const unsigned myIdx = stencil.globalSpaceIndex(primaryDofIdx);
687 bool blockFlowFound =
false;
688 bool blockVelocityFound =
false;
689 if (!blockFlows.empty()) {
690 if (std::ranges::binary_search(blockFlows,
691 simulator_().vanguard().cartesianIndex(myIdx))) {
692 blockFlowFound =
true;
695 flowsInfo_.appendRow(loc_flinfo.begin(), loc_flinfo.begin());
696 if (!dispersionActive && !enableBioeffects && !anyFlores && blockVelocity.empty()) {
701 if (!blockVelocity.empty() && !(dispersionActive || enableBioeffects)) {
702 if (std::ranges::binary_search(blockVelocity,
703 simulator_().vanguard().cartesianIndex(myIdx))) {
704 blockVelocityFound =
true;
707 velocityInfo_.appendRow(loc_vlinfo.begin(), loc_vlinfo.begin());
708 if (!anyFlows && blockFlows.empty() && !anyFlores) {
713 const int numFaces = stencil.numBoundaryFaces() + stencil.numInteriorFaces();
714 loc_flinfo.resize(numFaces);
715 loc_vlinfo.resize(stencil.numDof() - 1);
717 for (
unsigned dofIdx = 0; dofIdx < stencil.numDof(); ++dofIdx) {
718 const unsigned neighborIdx = stencil.globalSpaceIndex(dofIdx);
720 const auto scvfIdx = dofIdx - 1;
721 const auto& scvf = stencil.interiorFace(scvfIdx);
722 int faceId = scvf.dirId();
723 const int cartMyIdx = simulator_().
vanguard().cartesianIndex(myIdx);
724 const int cartNeighborIdx = simulator_().
vanguard().cartesianIndex(neighborIdx);
725 const auto& range = nncIndices.equal_range(cartMyIdx);
726 for (
auto it = range.first; it != range.second; ++it) {
727 if (it->second.first == cartNeighborIdx){
731 nncId = it->second.second;
734 loc_flinfo[dofIdx - 1] = FlowInfo{faceId, flow, nncId};
735 loc_vlinfo[dofIdx - 1] = VelocityInfo{faceId, flow};
739 for (
unsigned bdfIdx = 0; bdfIdx < stencil.numBoundaryFaces(); ++bdfIdx) {
740 const auto& scvf = stencil.boundaryFace(bdfIdx);
741 const int faceId = scvf.dirId();
742 loc_flinfo[stencil.numInteriorFaces() + bdfIdx] = FlowInfo{faceId, flow, nncId};
745 if (anyFlows || blockFlowFound) {
746 flowsInfo_.appendRow(loc_flinfo.begin(), loc_flinfo.end());
749 floresInfo_.appendRow(loc_flinfo.begin(), loc_flinfo.end());
751 if (dispersionActive || enableBioeffects || blockVelocityFound) {
752 velocityInfo_.appendRow(loc_vlinfo.begin(), loc_vlinfo.end());
759 template <
class VectorBlockType,
class MatrixBlockType,
class ADVectorBlockType>
760 OPM_HOST_DEVICE
static void
761 setResAndJacobi(VectorBlockType& res, MatrixBlockType& bMat,
const ADVectorBlockType& resid)
763 for (
unsigned eqIdx = 0; eqIdx < numEq; ++eqIdx) {
764 res[eqIdx] = resid[eqIdx].value();
767 for (
unsigned eqIdx = 0; eqIdx < numEq; ++eqIdx) {
768 for (
unsigned pvIdx = 0; pvIdx < numEq; ++pvIdx) {
773 bMat[eqIdx][pvIdx] = resid[eqIdx].derivative(pvIdx);
780 OPM_TIMEBLOCK(updateFlows);
781 const bool enableFlows = simulator_().
problem().eclWriter().outputModule().getFlows().hasFlows();
782 const auto& blockFlows = simulator_().
problem().eclWriter().outputModule().getFlows().blockFlows();
784 const bool isTemp = simulator_().
vanguard().eclState().getSimulationConfig().isTemp();
785 const bool enableFlores = simulator_().
problem().eclWriter().outputModule().getFlows().hasFlores() || isTemp;
786 if (!enableFlows && !enableFlores && blockFlows.empty()) {
789 const unsigned int numCells = model_().numTotalDof();
791#pragma omp parallel for
793 for (
unsigned globI = 0; globI < numCells; ++globI) {
794 OPM_TIMEBLOCK_LOCAL(linearizationForEachCell, Subsystem::Assembly);
795 const auto& nbInfos = neighborInfo_[globI];
796 ADVectorBlock adres(0.0);
797 ADVectorBlock darcyFlux(0.0);
798 const IntensiveQuantities& intQuantsIn = model_().intensiveQuantities(globI, 0);
801 OPM_TIMEBLOCK_LOCAL(fluxCalculationForEachCell, Subsystem::Assembly);
803 for (
const auto& nbInfo : nbInfos) {
804 OPM_TIMEBLOCK_LOCAL(fluxCalculationForEachFace, Subsystem::Assembly);
805 const unsigned globJ = nbInfo.neighbor;
806 assert(globJ != globI);
809 const IntensiveQuantities& intQuantsEx = model_().intensiveQuantities(globJ, 0);
810 LocalResidual::computeFlux(adres, darcyFlux, globI, globJ, intQuantsIn,
811 intQuantsEx, nbInfo.res_nbinfo, problem_().moduleParams());
812 adres *= nbInfo.res_nbinfo.faceArea;
813 if (!blockFlows.empty()) {
814 if (std::ranges::binary_search(blockFlows,
815 simulator_().vanguard().cartesianIndex(globI))) {
816 for (
unsigned eqIdx = 0; eqIdx < numEq; ++eqIdx) {
817 flowsInfo_[globI][loc].flow[eqIdx] = adres[eqIdx].value();
821 else if (enableFlows) {
822 for (
unsigned eqIdx = 0; eqIdx < numEq; ++eqIdx) {
823 flowsInfo_[globI][loc].flow[eqIdx] = adres[eqIdx].value();
827 for (
unsigned eqIdx = 0; eqIdx < numEq; ++eqIdx) {
828 floresInfo_[globI][loc].flow[eqIdx] = darcyFlux[eqIdx].value();
837 for (
const auto& bdyInfo : boundaryInfo_) {
842 ADVectorBlockCPU adres(0.0);
843 const unsigned globI = bdyInfo.cell;
844 const auto& nbInfos = neighborInfo_[globI];
845 const IntensiveQuantities& insideIntQuants = model_().intensiveQuantities(globI, 0);
846 LocalResidual::computeBoundaryFlux(adres, problem_(), bdyInfo.bcdata, insideIntQuants, globI);
847 adres *= bdyInfo.bcdata.faceArea;
848 const unsigned bfIndex = bdyInfo.bfIndex;
850 for (
unsigned eqIdx = 0; eqIdx < numEq; ++eqIdx) {
851 flowsInfo_[globI][nbInfos.size() + bfIndex].flow[eqIdx] = adres[eqIdx].value();
859 template <
class SubDomainType>
860 void linearize_(
const SubDomainType& domain)
862 constexpr bool run_assembly_on_gpu = getPropValue<TypeTag, Properties::RunAssemblyOnGpu>();
864 const bool useAssemblyLinearsolveBridge = gpuLinearizer_.updateSolverBridge(*
this);
869 if (!problem_().recycleFirstIterationStorage()) {
870 if (!model_().storeIntensiveQuantities() && !model_().enableStorageCache()) {
871 OPM_THROW(std::runtime_error,
"Must have cached either IQs or storage when we cannot recycle.");
882 const bool dispersionActive = simulator_().
vanguard().eclState().getSimulationConfig().rock_config().dispersion();
883 const unsigned int numCells = domain.cells.size();
885 if constexpr (!run_assembly_on_gpu) {
886 linearize_parallelization_wrapper(
896 linearize_bc<IntensiveQuantities, Model, LocalResidual>(
897 diagMatAddress_, residual_, boundaryInfo_);
901 gpuLinearizer_.linearize(*
this, domain, numCells, dt, dispersionActive);
906 if (!useAssemblyLinearsolveBridge) {
907 linearize_source_terms(numCells, domain);
912 template <
class ModelClass,
915 class NeighborSparseTable,
918 void linearize_parallelization_wrapper(
const unsigned int numCells,
919 const DomainType& domain,
920 const NeighborSparseTable& neighborInfo,
921 DiagPtrType& diagMatAddress,
923 const ModelClass& model,
925 const ProblemT& problem)
928#pragma omp parallel for
930 for (
unsigned ii = 0; ii < numCells; ++ii) {
931 linearize_cell<false, LocalResidual>(ii,
943 template <
class SubDomainType>
944 void linearize_source_terms(
unsigned int numCells,
const SubDomainType& domain)
947#pragma omp parallel for
949 for (
unsigned ii = 0; ii < numCells; ++ii) {
950 OPM_TIMEBLOCK_LOCAL(linearizationForEachCell, Subsystem::Assembly);
951 const unsigned globI = domain.cells[ii];
952 VectorBlockCPU res(0.0);
953 MatrixBlockCPU bMat(0.0);
954 ADVectorBlockCPU adres(0.0);
955 const IntensiveQuantities& intQuantsIn = model_().intensiveQuantities(globI, 0);
956 const double volume = model_().dofTotalVolume(globI);
963 if (separateSparseSourceTerms_) {
964 LocalResidual::computeSourceDense(adres, problem_(), intQuantsIn, globI, 0);
966 LocalResidual::computeSource(adres, problem_(), intQuantsIn, globI, 0);
970 residual_[globI] += res;
972 *diagMatAddress_[globI] += bMat;
976 if (separateSparseSourceTerms_) {
977 problem_().wellModel().addReservoirSourceTerms(residual_, diagMatAddress_);
982 template <
bool useGPU,
983 class LocalResidualT,
985 class VelocityInfoType,
989 class NeighborSparseTable,
992 const DomainType& domain,
993 const NeighborSparseTable& neighborInfo,
994 const DiagPtrType& diagMatAddress,
996 const ModelClass& model,
997 VelocityInfoType& velocityInfo,
999 const ProblemType& problem)
1001#if OPM_IS_INSIDE_HOST_FUNCTION
1002 OPM_TIMEBLOCK_LOCAL(linearizationForEachCell, Subsystem::Assembly);
1004 const unsigned globI = domain.cells[ii];
1005 const auto& nbInfos = neighborInfo[globI];
1006 VectorBlock res(0.0);
1008 ADVectorBlock adres(0.0);
1009 ADVectorBlock darcyFlux(0.0);
1010 const auto& intQuantsIn = model.intensiveQuantities(globI, 0);
1014#if OPM_IS_INSIDE_HOST_FUNCTION
1015 OPM_TIMEBLOCK_LOCAL(fluxCalculationForEachCell, Subsystem::Assembly);
1018 for (
const auto& nbInfo : nbInfos) {
1019 const unsigned globJ = nbInfo.neighbor;
1025 const auto& intQuantsEx = model.intensiveQuantities(globJ, 0);
1027 LocalResidualT::computeFlux(adres,
1034 problem.moduleParams());
1036 adres *= nbInfo.res_nbinfo.faceArea;
1039 if constexpr (!useGPU) {
1041 if (std::cmp_less(globI, velocityInfo.size())) {
1042 if (velocityInfo.rowSize(globI) > 0) {
1043 for (
unsigned phaseIdx = 0; phaseIdx < numEq; ++phaseIdx) {
1044 velocityInfo[globI][loc].velocity[phaseIdx]
1045 = darcyFlux[phaseIdx].value() / nbInfo.res_nbinfo.faceArea;
1054 *diagMatAddress[globI] += bMat;
1057 *nbInfo.matBlockAddress += bMat;
1065 LocalResidualT::template computeStorage<Evaluation>(adres, intQuantsIn);
1069 if constexpr (!useGPU) {
1071 if (model.enableStorageCache()) {
1075 model.updateCachedStorage(globI, 0, res);
1082 if (problem.iterationContext().isFirstGlobalIteration()) {
1084 if (problem.recycleFirstIterationStorage()) {
1087 model.updateCachedStorage(globI, 1, res);
1090 const auto& intQuantOld = model.intensiveQuantities(globI, 1);
1091 LocalResidualT::template computeStorage<Scalar>(tmp, intQuantOld);
1092 model.updateCachedStorage(globI, 1, tmp);
1095 res -= model.cachedStorage(globI, 1);
1097#if OPM_IS_INSIDE_HOST_FUNCTION
1098 OPM_TIMEBLOCK_LOCAL(computeStorage0, Subsystem::Assembly);
1101 const auto& intQuantOld = model.intensiveQuantities(globI, 1);
1102 LocalResidualT::template computeStorage<Scalar>(tmp, intQuantOld);
1108 const auto& intQuantOld = model.intensiveQuantities(globI, 1);
1109 LocalResidualT::template computeStorage<Scalar>(tmp, intQuantOld);
1114 const Scalar volume = model.dofTotalVolume(globI);
1115 const Scalar storefac = volume / dt;
1120 *diagMatAddress[globI] += bMat;
1125 template <
class LocalIntensiveQuantities,
1127 class LocalResidualT,
1130 class BoundaryInfoT>
1131 void linearize_bc(DiagPtrType& diagMatAdress,
1133 const BoundaryInfoT& boundaryInfo)
1136 for (
const auto& bdyInfo : boundaryInfo) {
1140 VectorBlock res(0.0);
1141 MatrixBlock bMat(0.0);
1142 ADVectorBlock adres(0.0);
1143 const unsigned globI = bdyInfo.cell;
1144 const LocalIntensiveQuantities& insideIntQuants
1145 = model_().intensiveQuantities(globI, 0);
1146 LocalResidual::computeBoundaryFlux(
1147 adres, problem_(), bdyInfo.bcdata, insideIntQuants, globI);
1148 adres *= bdyInfo.bcdata.faceArea;
1152 *diagMatAdress[globI] += bMat;
1156 void updateStoredTransmissibilities()
1158 if (neighborInfo_.empty()) {
1162 initFirstIteration_();
1165 const unsigned numCells = model_().numTotalDof();
1167#pragma omp parallel for
1169 for (
unsigned globI = 0; globI < numCells; globI++) {
1170 auto nbInfos = neighborInfo_[globI];
1171 for (
auto& nbInfo : nbInfos) {
1172 const unsigned globJ = nbInfo.neighbor;
1173 nbInfo.res_nbinfo.trans = problem_().transmissibility(globI, globJ);
1178 Simulator* simulatorPtr_{};
1181 std::unique_ptr<SparseMatrixAdapter> jacobian_{};
1182 GpuLinearizer gpuLinearizer_;
1185 GlobalEqVector residual_;
1187 LinearizationType linearizationType_{};
1189 using ResidualNBInfo =
typename LocalResidual::ResidualNBInfo;
1190 using NeighborInfoCPU = NeighborInfoStruct<ResidualNBInfo, MatrixBlockCPU>;
1192 SparseTable<NeighborInfoCPU> neighborInfo_{};
1193 std::vector<MatrixBlockCPU*> diagMatAddress_ {};
1201 SparseTable<FlowInfo> flowsInfo_;
1202 SparseTable<FlowInfo> floresInfo_;
1207 VectorBlock velocity;
1209 SparseTable<VelocityInfo> velocityInfo_;
1211 using ScalarFluidState =
typename IntensiveQuantities::ScalarFluidState;
1213 using BoundaryConditionDataCPU = BoundaryConditionData<VectorBlockCPU, ScalarFluidState>;
1215 using BoundaryInfoCPU = BoundaryInfo<BoundaryConditionDataCPU>;
1217 std::vector<BoundaryInfoCPU> boundaryInfo_;
1219 bool separateSparseSourceTerms_ =
false;
1221 FullDomain<> fullDomain_;
Declares the properties required by the black oil model.
The base class for the element-centered finite-volume discretization scheme.
Definition: ecfvdiscretization.hh:160
Definition: matrixblock.hh:256
Manages the initializing and running of time dependent problems.
Definition: simulator.hh:87
Scalar timeStepSize() const
Returns the time step length so that we don't miss the beginning of the next episode or cross the en...
Definition: simulator.hh:421
Vanguard & vanguard()
Return a reference to the grid manager of simulation.
Definition: simulator.hh:242
Problem & problem()
Return the object which specifies the pysical setup of the simulation.
Definition: simulator.hh:273
const GridView & gridView() const
Return the grid view for which the simulation is done.
Definition: simulator.hh:254
Model & model()
Return the physical model used in the simulation.
Definition: simulator.hh:260
Definition: tpfalinearizergpu.hh:64
The common code for the linearizers of non-linear systems of equations.
Definition: tpfalinearizer.hh:103
const auto & getFloresInfo() const
Return constant reference to the floresInfo.
Definition: tpfalinearizer.hh:392
const LinearizationType & getLinearizationType() const
Definition: tpfalinearizer.hh:376
const auto & getNeighborInfo() const
Definition: tpfalinearizer.hh:404
decltype(auto) gpuResidual()
Definition: tpfalinearizer.hh:298
void updateBoundaryConditionData()
Definition: tpfalinearizer.hh:414
void linearize()
Linearize the full system of non-linear equations.
Definition: tpfalinearizer.hh:215
SparseMatrixAdapter & jacobian()
Definition: tpfalinearizer.hh:340
const auto & getFlowsInfo() const
Return constant reference to the flowsInfo.
Definition: tpfalinearizer.hh:384
std::map< unsigned, Constraints > constraintsMap() const
Returns the map of constraint degrees of freedom.
Definition: tpfalinearizer.hh:438
TpfaLinearizer()
Definition: tpfalinearizer.hh:158
void linearizeDomain(const SubDomainType &domain)
Linearize the part of the non-linear system of equations that is associated with a part of the spatia...
Definition: tpfalinearizer.hh:271
void finalize()
Definition: tpfalinearizer.hh:292
void init(Simulator &simulator)
Initialize the linearizer.
Definition: tpfalinearizer.hh:186
void updateFlowsInfo()
Definition: tpfalinearizer.hh:778
static void registerParameters()
Register all run-time parameters for the Jacobian linearizer.
Definition: tpfalinearizer.hh:169
void exportSystem(const int idx, std::string &tag, const char *path="export")
Export block sparse linear system.
Definition: tpfalinearizer.hh:355
void linearizeDomain()
Linearize the part of the non-linear system of equations that is associated with the spatial domain.
Definition: tpfalinearizer.hh:231
const SparseMatrixAdapter & jacobian() const
Return constant reference to global Jacobian matrix backend.
Definition: tpfalinearizer.hh:337
void setLinearizationType(LinearizationType linearizationType)
Definition: tpfalinearizer.hh:373
GlobalEqVector & residual()
Definition: tpfalinearizer.hh:349
void eraseMatrix()
Causes the Jacobian matrix to be recreated from scratch before the next iteration.
Definition: tpfalinearizer.hh:199
const auto & getVelocityInfo() const
Return constant reference to the velocityInfo.
Definition: tpfalinearizer.hh:401
static OPM_HOST_DEVICE void linearize_cell(const unsigned int ii, const DomainType &domain, const NeighborSparseTable &neighborInfo, const DiagPtrType &diagMatAddress, ResidualType &residual, const ModelClass &model, VelocityInfoType &velocityInfo, const Scalar dt, const ProblemType &problem)
Definition: tpfalinearizer.hh:991
void updateDiscretizationParameters()
Definition: tpfalinearizer.hh:409
void resetSystem_(const SubDomainType &domain)
Definition: tpfalinearizer.hh:442
static OPM_HOST_DEVICE void setResAndJacobi(VectorBlockType &res, MatrixBlockType &bMat, const ADVectorBlockType &resid)
Definition: tpfalinearizer.hh:761
decltype(auto) flattenedGpuResidual()
Definition: tpfalinearizer.hh:299
void linearizeAuxiliaryEquations()
Linearize the part of the non-linear system of equations that is associated with the spatial domain.
Definition: tpfalinearizer.hh:305
bool gpuSystemActive() const
Definition: tpfalinearizer.hh:295
decltype(auto) gpuJacobian()
Definition: tpfalinearizer.hh:297
const GlobalEqVector & residual() const
Return constant reference to global residual vector.
Definition: tpfalinearizer.hh:346
Declare the properties used by the infrastructure code of the finite volume discretizations.
Defines the common properties required by the porous medium multi-phase models.
@ NONE
Definition: DeferredLogger.hpp:46
Definition: blackoilnewtonmethodparams.hpp:31
HYPRE_IJMatrix createMatrix(HYPRE_Int N, HYPRE_Int dof_offset, const CommType &comm)
Create Hypre matrix.
Definition: hypreinterface/HypreSetup.hpp:165
Definition: blackoilbioeffectsmodules.hh:45
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property (equivalent to old macro GET_PROP_TYPE(....
Definition: propertysystem.hh:233
void exportSystem(const IstlMatrix &jacobian, const GlobalEqVector &residual, const bool export_sparsity, const char *tag, const char *path="export")
Export blocks-sparse linear system.
Definition: exportSystem.hpp:42
Storage cells
Definition: tpfalinearizerstructs.hh:39
Definition: linearizationtype.hh:34
Definition: tpfalinearizer.hh:82
static constexpr bool value
Definition: tpfalinearizer.hh:82