20#include <boost/timer/timer.hpp>
22#include "dune/istl/operators.hh"
23#include <dune/istl/solvers.hh>
31 namespace DirectSolver_Detail
49 typedef decltype(hasScalar<T>(
nullptr))
type;
50 static constexpr bool value = !std::is_same<type,NoScalar>::value;
56 typedef decltype(hasFieldType<T>(
nullptr))
type;
57 static constexpr bool value = !std::is_same<type,NoFieldType>::value;
73 template <
class Scalar>
74 size_t checkNanInf(Scalar
const* x,
size_t n, std::string
const& what)
76 size_t nan = 0, inf = 0;
77 for (
size_t i=0; i<n; ++i)
79 if (std::isnan(x[i])) ++nan;
80 if (std::isinf(x[i])) ++inf;
82 if (nan>0) std::cerr << nan <<
" " << what <<
" entries are nan.\n";
83 if (inf>0) std::cerr << inf <<
" " << what <<
" entries are inf.\n";
97 template <
class Domain_,
class Range_>
99 public Dune::Preconditioner<Domain_,Range_>
126 template <
class GOP,
int firstRow,
int lastRow,
int firstCol,
int lastCol>
131 firstCol,lastCol>::
Scalar>>(),
140 template <
class AssembledGOP>
152 template <
class FieldType>
159 assert(solver.get());
167 template <
class FieldType>
180 template <
class FieldType>
187 assert(solver.get());
199 template <
class FieldType,
int n,
int m,
class Index>
216 template <
class FieldType,
int n,
int m,
class Index>
223 assert(solver.get());
246 apply(x,b,1e-10,res);
257 apply(x,b,1e-10,res);
267 virtual void apply(
Domain& x,
Range& b,
double reduction, Dune::InverseOperatorResult& res)
275 void apply(
Domain& x,
Range const& b,
double reduction, Dune::InverseOperatorResult& res)
const
277 if constexpr (
false && contiguousStorage<Domain> && contiguousStorage<Range>)
282 solver->solve(bp,xp);
287 std::vector<Scalar> rhs(b.dim()+16);
289 apply(rhs,reduction,res);
297 void apply(std::vector<Scalar>& v,
double , Dune::InverseOperatorResult& res)
const
299 boost::timer::cpu_timer timer;
306 res.reduction = 1e-10;
307 res.converged =
true;
308 res.conv_rate = 1e-10;
309 res.elapsed = (double)(timer.elapsed().user)/1e9;
319 virtual void apply(std::vector<Scalar>& v)
const
324 assert(v.size() >= n);
332 std::fill(v.begin(),v.end(),0.0);
344 Dune::InverseOperatorResult dummy_res;
345 apply(x,b,0,dummy_res);
353 Dune::InverseOperatorResult dummy_res;
354 apply(x,b,0,dummy_res);
378 virtual Dune::SolverCategory::Category
category()
const override
380 return Dune::SolverCategory::sequential;
384 std::shared_ptr<Factorization<Scalar>> solver;
391 template <
class S,
int n>
393 :
public Dune::InverseOperator<Dune::FieldVector<S,n>,Dune::FieldVector<S,n>>
394 ,
public Dune::Preconditioner<Dune::FieldVector<S,n>,Dune::FieldVector<S,n>>
411 template <
class FieldType>
439 virtual void apply(
Domain& x,
Range& b,
double reduction, Dune::InverseOperatorResult& res)
450 void apply(
Domain& x,
Range const& b,
double reduction, Dune::InverseOperatorResult& res)
const
457 res.reduction = 1e-10;
458 res.converged =
true;
459 res.conv_rate = 1e-10;
489 template <
class GOP,
int firstRow,
int lastRow,
int firstCol,
int lastCol>
492 directSolver(AssembledGalerkinOperator<GOP,firstRow,lastRow,firstCol,lastCol>
const& A,
497 return DirectSolver<Domain,Range>(A,directType,properties);
510 template <
class Scalar,
int n,
class Index>
511 DirectSolver<Dune::BlockVector<Dune::FieldVector<Scalar,n>>,
518 return DirectSolver<Domain,Range>(A,directType,properties);
531 template <
class Scalar,
int n,
class Index>
532 DirectSolver<Dune::BlockVector<Dune::FieldVector<Scalar,n>>,
536 FactorizationOptions options=FactorizationOptions())
540 return DirectSolver<Domain,Range>(A,directType,options);
551 template <
class InverseOperator>
552 class InverseLinearOperator:
public Dune::LinearOperator<typename InverseOperator::Range, typename InverseOperator::Domain>
555 typedef typename InverseOperator::Domain
Domain;
556 typedef typename InverseOperator::Range
Range;
557 typedef typename InverseOperator::Scalar
Scalar;
566 Dune::InverseOperatorResult result;
568 op.apply(y,rhs,result);
583 virtual Dune::SolverCategory::Category
category()
const override
585 return Dune::SolverCategory::sequential;
603 template <
class GOP,
int firstRow,
int lastRow,
int firstCol,
int lastCol>
608 FactorizationOptions options=FactorizationOptions())
612 return InverseLinearOperator<DirectSolver<Domain,Range>>(DirectSolver<Domain,Range>(A,directType,options));
615 template <
class GOP,
int firstRow,
int lastRow,
int firstCol,
int lastCol>
624 template <
class Matrix,
class Domain,
class Range>
625 InverseLinearOperator<DirectSolver<Domain,Range> >
An adaptor presenting a Dune::LinearOperator <domain_type,range_type> interface to a contiguous sub-b...
typename Assembler::AnsatzVariableSetDescription::template CoefficientVector< firstCol, lastCol > Domain
typename Assembler::TestVariableSetDescription::template CoefficientVector< firstRow, lastRow > Range
virtual void pre(Domain &x, Range &b)
Dune::FieldVector< S, n > Range
virtual void post(Domain &x)
void apply(Domain &x, Range &b, Dune::InverseOperatorResult &res) const
Solves the system for the given right hand side.
virtual void apply(Domain &x, Range &b, Dune::InverseOperatorResult &res)
Solves the system for the given right hand side.
Dune::FieldVector< S, n > Domain
virtual void apply(Domain &v, const Range &d)
void apply(Domain &x, Range const &b, double reduction, Dune::InverseOperatorResult &res) const
Solves the system for the given right hand side.
virtual void apply(Domain &x, Range &b, double reduction, Dune::InverseOperatorResult &res)
DirectSolver(Dune::FieldMatrix< FieldType, n, n > const &A)
Constructs a direct solver from a FieldMatrix matrix.
DirectSolver()
Default constructor.
virtual void apply(Domain &x, Range &b, Dune::InverseOperatorResult &res)
Solves the system for the given right hand side b.
DirectSolver(DirectSolver< Domain, Range > const &ds)=default
Copy constructor.
DirectSolver(AssembledGalerkinOperator< GOP, firstRow, lastRow, firstCol, lastCol > const &A, DirectType directType=DirectType::UMFPACK, FactorizationOptions options=FactorizationOptions())
Constructs a direct solver from an assembled linear operator.
virtual void apply(std::vector< Scalar > &v) const
Solves the system .
virtual void apply(Domain &x, Range const &b)
Solves the system for the given right hand side b.
virtual void apply(Domain &x, Range const &b) const
Solves the system for the given right hand side b.
DirectSolver_Detail::ScalarType< Domain_ >::type Scalar
DirectSolver(MatrixAsTriplet< FieldType > const &A, DirectType directType, MatrixProperties properties)
Constructs a direct solver from a triplet matrix.
virtual void pre(Domain &, Range &)
unused
DirectSolver(NumaBCRSMatrix< Dune::FieldMatrix< FieldType, n, m >, Index > const &A, DirectType directType=DirectType::UMFPACK, FactorizationOptions options=FactorizationOptions())
Constructs a direct solver from a NumaBCRS matrix. A copy of the linear operator is held internally,...
DirectSolver(NumaBCRSMatrix< Dune::FieldMatrix< FieldType, n, m >, Index > const &A, DirectType directType, MatrixProperties properties)
Constructs a direct solver from a NumaBCRS matrix. A copy of the linear operator is held internally,...
virtual Dune::SolverCategory::Category category() const override
returns the category of the operator
virtual void post(Domain &)
unused
void apply(Domain &x, Range const &b, double reduction, Dune::InverseOperatorResult &res) const
Solves the system for the given right hand side b.
DirectSolver()
Default constructor.
void apply(Domain &x, Range &b, Dune::InverseOperatorResult &res) const
Solves the system for the given right hand side b.
void apply(std::vector< Scalar > &v, double, Dune::InverseOperatorResult &res) const
The input vector v contains the rhs. It will be overwritten by the solution.
DirectSolver(MatrixAsTriplet< FieldType > const &A, DirectType directType=DirectType::UMFPACK, FactorizationOptions options=FactorizationOptions())
Constructs a direct solver from a triplet matrix.
virtual void apply(Domain &x, Range &b, double reduction, Dune::InverseOperatorResult &res)
Solves the system for the given right hand side b.
DirectSolver(Dune::BCRSMatrix< Dune::FieldMatrix< FieldType, 1, 1 > > const &A, DirectType directType=DirectType::UMFPACK, MatrixProperties properties=MatrixProperties::GENERAL)
Constructs a direct solver from a BCRS matrix. A copy of the linear operator is held internally,...
DirectSolver(AssembledGOP const &A, DirectType directType, MatrixProperties properties)
Constructs a direct solver from an assembled linear operator.
Dune::LinearOperator interface for inverse operators.
InverseLinearOperator(InverseOperator const &op_)
InverseLinearOperator()=default
virtual void apply(Domain const &x, Range &y) const
virtual void applyscaleadd(Scalar alpha, Domain const &x, Range &y) const
InverseOperator::Domain Domain
InverseOperator::Range Range
InverseOperator::Scalar Scalar
virtual Dune::SolverCategory::Category category() const override
returns the category of the operator
Operator with matrix-representation and coordinate mappings.
A NUMA-aware compressed row storage matrix adhering mostly to the Dune ISTL interface (to complete....
std::unique_ptr< Factorization< Scalar > > getFactorization(DirectType directType, MatrixAsTriplet< Scalar, Index > const &A, FactorizationOptions options)
Creates a factorization of the given triplet matrix.
DirectType
Available direct solvers for linear equation systems.
@ UMFPACK
UMFPack from SuiteSparse, using 32 bit integer indices.
MatrixProperties
Characterizations of sparse matrix properties.
NoFieldType hasFieldType(...)
size_t checkNanInf(Scalar const *x, size_t n, std::string const &what)
Checks a vector for nan/inf and reports their number to stderr.
InverseLinearOperator< DirectSolver< typename AssembledGalerkinOperator< GOP, firstRow, lastRow, firstCol, lastCol >::Domain, typename AssembledGalerkinOperator< GOP, firstRow, lastRow, firstCol, lastCol >::Range > > directInverseOperator(AssembledGalerkinOperator< GOP, firstRow, lastRow, firstCol, lastCol > const &A, DirectType directType, MatrixProperties properties)
OutIter vectorToSequence(double x, OutIter iter)
InIter vectorFromSequence(FunctionSpaceElement< Space, m > &v, InIter in)
decltype(hasFieldType< T >(nullptr)) type
static constexpr bool value
static constexpr bool value
decltype(hasScalar< T >(nullptr)) type
std::conditional< HasScalar< T >::value, typenameHasScalar< T >::type, typenamestd::conditional< HasFieldType< T >::value, typenameHasFieldType< T >::type, void >::type >::type type
The Options struct allows to specify several options of factorization.