1#ifndef DUNE_FEM_AMGXSOLVER_HH
2#define DUNE_FEM_AMGXSOLVER_HH
6#include <dune/fem/function/common/scalarproducts.hh>
7#include <dune/fem/operator/common/operator.hh>
8#include <dune/fem/io/parameter.hh>
10#include <dune/fem/solver/inverseoperatorinterface.hh>
11#include <dune/fem/solver/parameter.hh>
12#include <dune/fem/operator/linear/petscoperator.hh>
13#include <dune/fem/misc/petsc/petsccommon.hh>
14#include <dune/fem/function/petscdiscretefunction.hh>
18#include <AmgXSolver.hpp>
35 struct AMGXSolverParameter :
public LocalParameter< SolverParameter, AMGXSolverParameter >
37 typedef LocalParameter< SolverParameter, AMGXSolverParameter > BaseType;
40 using BaseType :: parameter;
41 using BaseType :: keyPrefix;
43 AMGXSolverParameter(
const ParameterReader& parameter = Parameter::container() )
44 : BaseType( parameter )
48 AMGXSolverParameter(
const std::string &keyPrefix,
const ParameterReader ¶meter = Parameter::container() )
49 : BaseType( keyPrefix, parameter )
52 AMGXSolverParameter(
const SolverParameter& sp )
53 : AMGXSolverParameter( sp.keyPrefix(), sp.parameter() )
56 virtual std::string solvermode ()
const
58 const std::string modes [] = {
"dDDI" ,
"dDFI",
"dFFI",
"hDDI",
"hDFI",
"hFFI" };
59 int mode = parameter().getEnum(keyPrefix() +
"amgx.mode", modes, 0 );
63 virtual std::string solverconfig ()
const
65 return parameter().template getValue< std::string >( keyPrefix() +
"amgx.config",
"amgxconfig.json");
74 template<
class DiscreteFunction >
75 class AMGXInverseOperator;
77 template<
class DiscreteFunction >
78 struct AMGXInverseOperatorTraits
81 typedef PetscDiscreteFunction< typename DiscreteFunction::DiscreteFunctionSpaceType > SolverDiscreteFunctionType;
84 typedef OperatorType PreconditionerType;
87 typedef Fem::PetscLinearOperator< DiscreteFunction, DiscreteFunction > AssembledOperatorType;
89 typedef OperatorType AssembledOperatorType;
94 typedef AMGXSolverParameter SolverParameterType;
103 :
public InverseOperatorInterface< AMGXInverseOperatorTraits< DF > >
105 typedef AMGXInverseOperatorTraits< DF > Traits;
106 typedef InverseOperatorInterface< Traits > BaseType;
107 friend class InverseOperatorInterface< Traits >;
109 using BaseType :: parameter;
114 typedef typename BaseType::SolverDiscreteFunctionType SolverDiscreteFunctionType;
115 typedef typename BaseType::OperatorType OperatorType;
116 typedef typename BaseType::PreconditionerType PreconditionerType;
117 typedef typename BaseType::AssembledOperatorType AssembledOperatorType;
124 : BaseType( parameter )
129 const AMGXSolverParameter & parameter = AMGXSolverParameter() )
136 const AMGXSolverParameter & parameter = AMGXSolverParameter() )
139 bind( op, preconditioner );
145 if( other.operator_ )
146 bind( *(other.operator_) );
149 void bind(
const OperatorType& op )
151 BaseType::bind( op );
158 amgXSolver_->finalize();
161 BaseType :: unbind();
165 void init(
const AMGXSolverParameter& parameter )
167 if( assembledOperator_ )
169 std::string mode = parameter.solvermode();
170 std::string config = parameter.solverconfig();
172 amgXSolver_.reset(
new AmgXSolver() );
173 amgXSolver_->initialize(PETSC_COMM_WORLD, mode, config );
176 if( assembledOperator_->blockedMode() )
177 DUNE_THROW(InvalidStateException,
"AMGXInverseOperator only works with PetscLinearOperator in non-blocked mode!");
180 Mat& A =
const_cast<Mat &
> (assembledOperator_->exportMatrix());
183 amgXSolver_->setA( A );
185 DUNE_THROW(InvalidStateException,
"AMGX solver or PETSc not found during cmake config. Please reconfigure!");
190 int apply(
const SolverDiscreteFunctionType& arg, SolverDiscreteFunctionType& dest )
const
192 if( !assembledOperator_ )
193 DUNE_THROW(NotImplemented,
"AMGX solver with matrix free implementations is not supported!");
198 assert( amgXSolver_ );
201 if( dest.space().continuous() )
202 dest.dofVector().clearGhost();
205 Vec& x = *dest.petscVec();
206 Vec& rhs = *(
const_cast< SolverDiscreteFunctionType&
> (arg).petscVec());
207 amgXSolver_->solve( x, rhs );
210 if( dest.space().continuous() )
216 amgXSolver_->getIters( iterations );
218 DUNE_THROW(InvalidStateException,
"AMGX solver or PETSc not found during cmake config. Please reconfigure!");
225 mutable std::unique_ptr< AmgXSolver > amgXSolver_;
227 using BaseType :: assembledOperator_;
228 using BaseType :: parameter_;
AMGX solver context for PETSc Mat and PETSc Vec.
Definition: amgxsolver.hh:104
AMGXInverseOperator(const AMGXSolverParameter ¶meter=AMGXSolverParameter())
constructor
Definition: amgxsolver.hh:123
static const bool preconditioningAvailable
this solver does not offer to set preconditioning option
Definition: amgxsolver.hh:112
Inverse operator base on CG method. Uses a runtime parameter fem.preconditioning which enables diagon...
Definition: cginverseoperator.hh:408
forward declaration
Definition: discretefunction.hh:51
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13