23#include <dune/common/version.hh>
25#include <dune/istl/ilu.hh>
26#include <dune/istl/owneroverlapcopy.hh>
28#include <opm/common/ErrorMacros.hpp>
29#include <opm/common/TimingMacros.hpp>
45 OPM_TIMEBLOCK(GhostLastBlockILU0Decomp);
47 assert(interiorSize <= A.N());
48 using rowiterator =
typename M::RowIterator;
49 using coliterator =
typename M::ColIterator;
50 using block =
typename M::block_type;
53 for (rowiterator i = A.begin(); i.index() < interiorSize; ++i)
56 coliterator endij=(*i).end();
60 for (ij=(*i).begin(); ij.index()<i.index(); ++ij)
63 coliterator jj = A[ij.index()].find(ij.index());
66 (*ij).rightmultiply(*jj);
69 coliterator endjk=A[ij.index()].end();
70 coliterator jk=jj; ++jk;
71 coliterator ik=ij; ++ik;
72 while (ik!=endij && jk!=endjk)
73 if (ik.index()==jk.index())
82 if (ik.index()<jk.index())
90 if (ij.index()!=i.index())
91 DUNE_THROW(Dune::ISTLError,
"diagonal entry missing");
95 catch (Dune::FMatrixError & e) {
96 DUNE_THROW(Dune::ISTLError,
"ILU failed to invert matrix block");
102template<
class M,
class CRS,
class InvVector>
113 using size_type =
typename M :: size_type;
118 lower.resize( A.N() );
119 upper.resize( A.N() );
123 size_type numLower = 0;
124 size_type numUpper = 0;
125 const auto endi = A.end();
126 for (
auto i = A.begin(); i != endi; ++i) {
127 const size_type iIndex = i.index();
128 size_type numLowerRow = 0;
129 for (
auto j = (*i).begin(); j.index() < iIndex; ++j) {
132 numLower += numLowerRow;
133 numUpper += (*i).size() - numLowerRow - 1;
135 assert(numLower + numUpper + A.N() == A.nonzeroes());
137 lower.reserveAdditional( numLower );
141 size_type colcount = 0;
142 lower.rows_[ 0 ] = colcount;
143 for (
auto i=A.begin(); i!=endi; ++i, ++row)
145 const size_type iIndex = i.index();
148 for (
auto j=(*i).begin(); j.index() < iIndex; ++j )
150 lower.push_back( (*j), j.index() );
153 lower.rows_[ iIndex+1 ] = colcount;
156 assert(colcount == numLower);
158 const auto rbegini = std::make_reverse_iterator(A.begin());
161 upper.rows_[ 0 ] = colcount ;
163 upper.reserveAdditional( numUpper );
167 auto rindex = [](
auto it) {
return std::prev(it.base()).index(); };
168 for (
auto i=std::make_reverse_iterator(A.end()); i!=rbegini; ++i, ++ row )
170 const size_type iIndex = rindex(i);
174 for (
auto j=std::make_reverse_iterator(i->end()); rindex(j)>=iIndex; ++j )
176 const size_type jIndex = rindex(j);
177 if( rindex(j) == iIndex )
182 else if ( rindex(j) >= rindex(i) )
184 upper.push_back( (*j), jIndex );
188 upper.rows_[ row+1 ] = colcount;
190 assert(colcount == numUpper);
194size_t set_interiorSize( [[maybe_unused]]
size_t N,
size_t interiorSize, [[maybe_unused]]
const PI& comm)
201size_t set_interiorSize(
size_t N,
size_t interiorSize,
const Dune::OwnerOverlapCopyCommunication<int,int>& comm)
205 auto indexSet = comm.indexSet();
208 for (
auto idx = indexSet.begin(); idx!=indexSet.end(); ++idx)
210 if (idx->local().attribute()==1)
212 auto loc = idx->local().local();
225template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
226Dune::SolverCategory::Category
229 return std::is_same_v<ParallelInfoT, Dune::Amg::SequentialInformation> ?
230 Dune::SolverCategory::sequential : Dune::SolverCategory::overlapping;
233template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
242 comm_(nullptr), w_(w),
243 relaxation_( std::abs( w - 1.0 ) > 1e-15 ),
244 A_(&reinterpret_cast<const Matrix&>(A)), iluIteration_(n),
245 milu_(milu), redBlack_(redblack), reorderSphere_(reorder_sphere)
253template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
256 const ParallelInfo& comm,
const int n,
const field_type w,
263 relaxation_( std::abs( w - 1.0 ) > 1e-15 ),
264 A_(&reinterpret_cast<const Matrix&>(A)), iluIteration_(n),
265 milu_(milu), redBlack_(redblack), reorderSphere_(reorder_sphere)
273template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
281template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
291 relaxation_( std::abs( w - 1.0 ) > 1e-15 ),
292 A_(&reinterpret_cast<const Matrix&>(A)), iluIteration_(0),
293 milu_(milu), redBlack_(redblack), reorderSphere_(reorder_sphere)
301template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
304 const ParallelInfo& comm,
312 relaxation_( std::abs( w - 1.0 ) > 1e-15 ),
313 interiorSize_(interiorSize),
314 A_(&reinterpret_cast<const Matrix&>(A)), iluIteration_(0),
315 milu_(milu), redBlack_(redblack), reorderSphere_(reorder_sphere)
319 assert(interiorSize <= A_->N());
323template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
325apply (Domain& v,
const Range& d)
327 OPM_TIMEBLOCK(apply);
328 Range& md = reorderD(d);
329 Domain& mv = reorderV(v);
332 using dblock =
typename Range ::block_type;
333 using vblock =
typename Domain::block_type;
337 size_type upperLoopStart = iEnd - interiorSize_;
339 if (iEnd != upper_.rows())
341 OPM_THROW(std::logic_error,
"ILU: number of lower and upper rows must be the same");
345 for (
size_type i = 0; i < lowerLoopEnd; ++i)
347 dblock rhs( md[ i ] );
348 const size_type rowI = lower_.rows_[ i ];
349 const size_type rowINext = lower_.rows_[ i+1 ];
351 for (
size_type col = rowI; col < rowINext; ++col)
353 lower_.values_[ col ].mmv( mv[ lower_.cols_[ col ] ], rhs );
359 for (
size_type i = upperLoopStart; i < iEnd; ++i)
361 vblock& vBlock = mv[ lastRow - i ];
362 vblock rhs ( vBlock );
363 const size_type rowI = upper_.rows_[ i ];
364 const size_type rowINext = upper_.rows_[ i+1 ];
366 for (
size_type col = rowI; col < rowINext; ++col)
368 upper_.values_[ col ].mmv( mv[ upper_.cols_[ col ] ], rhs );
372 inv_[ i ].mv( rhs, vBlock);
375 copyOwnerToAll( mv );
383template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
389 comm_->copyOwnerToAll(v, v);
393template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
397 OPM_TIMEBLOCK(update);
401 if (comm_ && comm_->communicator().size() <= 0)
405 OPM_THROW(std::logic_error,
"Expected a matrix with zero rows for an invalid communicator.");
414 int ilu_setup_successful = 1;
416 const int rank = comm_ ? comm_->communicator().rank() : 0;
420 using Graph = Dune::Amg::MatrixGraph<const Matrix>;
423 const auto& colors = std::get<0>(colorsTuple);
424 const auto& verticesPerColor = std::get<2>(colorsTuple);
425 auto noColors = std::get<1>(colorsTuple);
426 if ( reorderSphere_ )
438 std::vector<std::size_t> inverseOrdering(ordering_.size());
440 OPM_TIMEBLOCK(createInverseOrdering);
441 std::size_t index = 0;
442 for (
const auto newIndex : ordering_)
444 inverseOrdering[newIndex] = index++;
450 OPM_TIMEBLOCK(iluDecomposition);
451 if (iluIteration_ == 0) {
455 assert(interiorSize_ <= A_->N());
459 if (ordering_.empty())
461 OPM_TIMEBLOCK(iluDecompositionUpdateMatrix);
463 OPM_TIMEBLOCK(iluDecompositionCopyEntries);
467 for (std::size_t row = 0; row < A_->N(); ++row) {
468 const auto& Arow = (*A_)[row];
469 auto Ait = Arow.begin();
470 auto Iit = (*ILU_)[row].begin();
471 for (; Ait != Arow.end(); ++Ait, ++Iit) {
476 OPM_TIMEBLOCK(iluDecompositionDuplicateMatrix);
478 ILU_ = std::make_unique<Matrix>(*A_);
483 ILU_ = std::make_unique<Matrix>(A_->N(), A_->M(),
484 A_->nonzeroes(), Matrix::row_wise);
487 auto endcreateA = newA.createend();
488 for (
auto iter = newA.createbegin(); iter != endcreateA; ++iter)
490 const auto& row = (*A_)[inverseOrdering[iter.index()]];
491 for (
auto col = row.begin(), cend = row.end(); col != cend; ++col)
493 iter.insert(ordering_[col.index()]);
497 for (
auto iter = A_->begin(); iter != A_->end(); ++iter)
499 auto newRow = newA.begin() + ordering_[iter.index()];
500 for (
auto&& [A_ij, j] : sparseRange(*newRow))
502 (*newRow)[ordering_[j]] = A_ij;
514 detail::signFunctor<typename Matrix::field_type> );
518 detail::signFunctor<typename Matrix::field_type> );
522 detail::isPositiveFunctor<typename Matrix::field_type> );
525 if (interiorSize_ == A_->N())
526 Dune::ILU::blockILU0Decomposition( *ILU_ );
534 ILU_ = std::make_unique<Matrix>(A_->N(), A_->M(), Matrix::row_wise);
535 std::unique_ptr<detail::Reorderer> reorderer, inverseReorderer;
536 if (ordering_.empty())
550 catch (
const Dune::MatrixBlockError& error)
552 message = error.what();
553 std::cerr <<
"Exception occurred on process " << rank <<
" during " <<
554 "setup of ILU0 preconditioner with message: "
555 << message<<std::endl;
556 ilu_setup_successful = 0;
561 OPM_TIMEBLOCK(checkParallelFailure);
562 const bool parallel_failure = comm_ && comm_->communicator().min(ilu_setup_successful) == 0;
563 const bool local_failure = ilu_setup_successful == 0;
564 if (local_failure || parallel_failure)
566 throw Dune::MatrixBlockError();
574template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
578 if (ordering_.empty())
584 return const_cast<Range&
>(d);
588 reorderedD_.resize(d.size());
590 for (
const auto index : ordering_)
592 reorderedD_[index] = d[i++];
598template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
602 if (ordering_.empty())
608 reorderedV_.resize(v.size());
610 for (
const auto index : ordering_)
612 reorderedV_[index] = v[i++];
618template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
622 if (!ordering_.empty())
625 for (
const auto index : ordering_)
627 v[i++] = reorderedV[index];
A two-step version of an overlapping Schwarz preconditioner using one step ILU0 as.
Definition: ParallelOverlappingILU0.hpp:131
ParallelOverlappingILU0(const Matrix &A, const int n, const field_type w, MILU_VARIANT milu, bool redblack=false, bool reorder_sphere=true)
Constructor.
Definition: ParallelOverlappingILU0_impl.hpp:235
Domain & reorderV(Domain &v)
Reorder V if needed and return a reference to it.
Definition: ParallelOverlappingILU0_impl.hpp:600
size_type interiorSize_
Definition: ParallelOverlappingILU0.hpp:350
void reorderBack(const Range &reorderedV, Range &v)
Definition: ParallelOverlappingILU0_impl.hpp:620
Range & reorderD(const Range &d)
Reorder D if needed and return a reference to it.
Definition: ParallelOverlappingILU0_impl.hpp:576
void copyOwnerToAll(V &v) const
Definition: ParallelOverlappingILU0_impl.hpp:386
Dune::SolverCategory::Category category() const override
Definition: ParallelOverlappingILU0_impl.hpp:227
void update() override
Definition: ParallelOverlappingILU0_impl.hpp:395
void apply(Domain &v, const Range &d) override
Apply the preconditoner.
Definition: ParallelOverlappingILU0_impl.hpp:325
typename matrix_type::size_type size_type
Definition: ParallelOverlappingILU0.hpp:145
typename Domain::field_type field_type
The field type of the preconditioner.
Definition: ParallelOverlappingILU0.hpp:142
void milu0_decomposition(M &A, FieldFunct< M > absFunctor=signFunctor< typename M::field_type >, FieldFunct< M > signFunctor=oneFunctor< typename M::field_type >, std::vector< typename M::block_type > *diagonal=nullptr)
void convertToCRS(const M &A, CRS &lower, CRS &upper, InvVector &inv)
compute ILU decomposition of A. A is overwritten by its decomposition
Definition: ParallelOverlappingILU0_impl.hpp:103
void milun_decomposition(const M &A, int n, MILU_VARIANT milu, M &ILU, Reorderer &ordering, Reorderer &inverseOrdering)
size_t set_interiorSize(size_t N, size_t interiorSize, const PI &comm)
Definition: ParallelOverlappingILU0_impl.hpp:194
void ghost_last_bilu0_decomposition(M &A, std::size_t interiorSize)
Compute Blocked ILU0 decomposition, when we know junk ghost rows are located at the end of A.
Definition: ParallelOverlappingILU0_impl.hpp:43
Definition: blackoilbioeffectsmodules.hh:45
MILU_VARIANT
Definition: MILU.hpp:34
@ MILU_1
sum(dropped entries)
@ MILU_2
sum(dropped entries)
@ MILU_3
sum(|dropped entries|)
@ MILU_4
sum(dropped entries)
std::vector< std::size_t > reorderVerticesPreserving(const std::vector< int > &colors, int noColors, const std::vector< std::size_t > &verticesPerColor, const Graph &graph)
! Reorder colored graph preserving order of vertices with the same color.
Definition: GraphColoring.hpp:169
std::vector< std::size_t > reorderVerticesSpheres(const std::vector< int > &colors, int noColors, const std::vector< std::size_t > &verticesPerColor, const Graph &graph, typename Graph::VertexDescriptor root)
! Reorder Vetrices in spheres
Definition: GraphColoring.hpp:189
std::tuple< std::vector< int >, int, std::vector< std::size_t > > colorVerticesWelshPowell(const Graph &graph)
Color the vertices of graph.
Definition: GraphColoring.hpp:113