dune-fem 2.12-git
Loading...
Searching...
No Matches
amgxsolver.hh
Go to the documentation of this file.
1#ifndef DUNE_FEM_AMGXSOLVER_HH
2#define DUNE_FEM_AMGXSOLVER_HH
3
4#include <limits>
5
9
15
16#if HAVE_AMGXSOLVER
17// AMGX solver wrapper based on Petsc data structures
18#include <AmgXSolver.hpp>
19#endif
20
21namespace Dune
22{
23
24 namespace Fem
25 {
26
27 //=====================================================================
28 // Implementation of AMGX solver wrapper using PETSc matrix
29 //=====================================================================
30
35 struct AMGXSolverParameter : public LocalParameter< SolverParameter, AMGXSolverParameter >
36 {
38
39 public:
42
46
47
51
55
56 virtual std::string solvermode () const
57 {
58 const std::string modes [] = { "dDDI" , "dDFI", "dFFI", "hDDI", "hDFI", "hFFI" };
59 int mode = parameter().getEnum(keyPrefix() + "amgx.mode", modes, 0 );
60 return modes[ mode ];
61 }
62
63 virtual std::string solverconfig () const
64 {
65 return parameter().template getValue< std::string >( keyPrefix() + "amgx.config", "amgxconfig.json");
66 }
67 };
68
69
70
71 // AMGXSolver
72 // --------------
73
74 template< class DiscreteFunction >
75 class AMGXInverseOperator;
76
77 template< class DiscreteFunction >
96
97
98
99
101 template< class DF >
103 : public InverseOperatorInterface< AMGXInverseOperatorTraits< DF > >
104 {
107 friend class InverseOperatorInterface< Traits >;
108 public:
110
112 static const bool preconditioningAvailable = false;
113
118
127
134
138 {
139 bind( op, preconditioner );
140 }
141
143 : AMGXInverseOperator( other.parameter() )
144 {
145 if( other.operator_ )
146 bind( *(other.operator_) );
147 }
148
149 void bind( const OperatorType& op )
150 {
151 BaseType::bind( op );
152 init( parameter() );
153 }
154
155 void unbind()
156 {
157#if HAVE_AMGXSOLVER
158 amgXSolver_->finalize();
159 amgXSolver_.reset();
160#endif
161 BaseType :: unbind();
162 }
163
164 protected:
166 {
168 {
169 std::string mode = parameter.solvermode();
170 std::string config = parameter.solverconfig();
171#if HAVE_AMGXSOLVER
172 amgXSolver_.reset( new AmgXSolver() );
173 amgXSolver_->initialize(PETSC_COMM_WORLD, mode, config );
174
175 // check that PetscMat was assembled not in block mode
176 if( assembledOperator_->blockedMode() )
177 DUNE_THROW(InvalidStateException, "AMGXInverseOperator only works with PetscLinearOperator in non-blocked mode!");
178
179 // attach Matrix to linear solver context
180 Mat& A = const_cast<Mat &> (assembledOperator_->exportMatrix());
181
182 // set matrix
183 amgXSolver_->setA( A );
184#else
185 DUNE_THROW(InvalidStateException,"AMGX solver or PETSc not found during cmake config. Please reconfigure!");
186#endif
187 }
188 }
189
191 {
192 if( !assembledOperator_ )
193 DUNE_THROW(NotImplemented,"AMGX solver with matrix free implementations is not supported!");
194
195
196 int iterations = -1;
197#if HAVE_AMGXSOLVER
198 assert( amgXSolver_ );
199
200 // need to have a 'distributed' destination vector for continuous spaces
201 if( dest.space().continuous() )
202 dest.dofVector().clearGhost();
203
204 // call PETSc solvers, dest = x, arg = rhs
205 Vec& x = *dest.petscVec();
206 Vec& rhs = *(const_cast< SolverDiscreteFunctionType& > (arg).petscVec());
207 amgXSolver_->solve( x, rhs );
208
209 // a continuous solution is 'distributed' so need a communication here
210 if( dest.space().continuous() )
211 {
212 dest.communicate();
213 }
214
215 // get number of iterations
216 amgXSolver_->getIters( iterations );
217#else
218 DUNE_THROW(InvalidStateException,"AMGX solver or PETSc not found during cmake config. Please reconfigure!");
219#endif
220 return iterations;
221 }
222
223 protected:
224#if HAVE_AMGXSOLVER
225 mutable std::unique_ptr< AmgXSolver > amgXSolver_;
226#endif
227 using BaseType :: assembledOperator_;
228 using BaseType :: parameter_;
229 };
230
232
233 } // namespace Fem
234
235} // namespace Dune
236
237#endif // #ifndef DUNE_FEM_PETSCSOLVER_HH
Y & rhs()
#define DUNE_THROW(E,...)
static ParameterContainer & container()
Definition io/parameter.hh:199
Definition io/parameter.hh:576
int getEnum(const std::string &key, const std::string(&values)[n]) const
Definition reader.hh:227
Definition amgxsolver.hh:36
AMGXSolverParameter(const SolverParameter &sp)
Definition amgxsolver.hh:52
virtual std::string solvermode() const
Definition amgxsolver.hh:56
virtual std::string solverconfig() const
Definition amgxsolver.hh:63
LocalParameter< SolverParameter, AMGXSolverParameter > BaseType
Definition amgxsolver.hh:37
AMGXSolverParameter(const std::string &keyPrefix, const ParameterReader &parameter=Parameter::container())
Definition amgxsolver.hh:48
AMGXSolverParameter(const ParameterReader &parameter=Parameter::container())
Definition amgxsolver.hh:43
AMGX solver context for PETSc Mat and PETSc Vec.
Definition amgxsolver.hh:104
BaseType::SolverDiscreteFunctionType SolverDiscreteFunctionType
Definition amgxsolver.hh:114
void init(const AMGXSolverParameter &parameter)
Definition amgxsolver.hh:165
AMGXInverseOperator(const AMGXSolverParameter &parameter=AMGXSolverParameter())
constructor
Definition amgxsolver.hh:123
int apply(const SolverDiscreteFunctionType &arg, SolverDiscreteFunctionType &dest) const
Definition amgxsolver.hh:190
void bind(const OperatorType &op)
Definition amgxsolver.hh:149
AMGXInverseOperator(const AMGXInverseOperator &other)
Definition amgxsolver.hh:142
void unbind()
Definition amgxsolver.hh:155
BaseType::PreconditionerType PreconditionerType
Definition amgxsolver.hh:116
AMGXInverseOperator(const OperatorType &op, const AMGXSolverParameter &parameter=AMGXSolverParameter())
Definition amgxsolver.hh:128
BaseType::AssembledOperatorType AssembledOperatorType
Definition amgxsolver.hh:117
AMGXInverseOperator(const OperatorType &op, PreconditionerType &preconditioner, const AMGXSolverParameter &parameter=AMGXSolverParameter())
Definition amgxsolver.hh:135
BaseType::OperatorType OperatorType
Definition amgxsolver.hh:115
static const bool preconditioningAvailable
this solver does not offer to set preconditioning option
Definition amgxsolver.hh:112
Definition amgxsolver.hh:79
OperatorType PreconditionerType
Definition amgxsolver.hh:84
DiscreteFunction DiscreteFunctionType
Definition amgxsolver.hh:80
PetscDiscreteFunction< typename DiscreteFunction::DiscreteFunctionSpaceType > SolverDiscreteFunctionType
Definition amgxsolver.hh:81
Dune::Fem::Operator< DiscreteFunction, DiscreteFunction > OperatorType
Definition amgxsolver.hh:83
OperatorType AssembledOperatorType
Definition amgxsolver.hh:89
AMGXSolverParameter SolverParameterType
Definition amgxsolver.hh:94
AMGXInverseOperator< DiscreteFunction > InverseOperatorType
Definition amgxsolver.hh:92
Definition inverseoperatorinterface.hh:37
SolverParameterType & parameter() const
Definition inverseoperatorinterface.hh:158
const OperatorType * operator_
Definition inverseoperatorinterface.hh:228
Traits::AssembledOperatorType AssembledOperatorType
Definition inverseoperatorinterface.hh:51
Traits::SolverDiscreteFunctionType SolverDiscreteFunctionType
Definition inverseoperatorinterface.hh:49
std::shared_ptr< SolverParameterType > parameter_
Definition inverseoperatorinterface.hh:226
Traits::PreconditionerType PreconditionerType
Definition inverseoperatorinterface.hh:52
const AssembledOperatorType * assembledOperator_
Definition inverseoperatorinterface.hh:229
void bind(const OperatorType &op)
store pointer to linear operator
Definition inverseoperatorinterface.hh:109
Traits::OperatorType OperatorType
Definition inverseoperatorinterface.hh:50
int iterations() const
return number of iterations used in previous call of application operator
Definition inverseoperatorinterface.hh:131
Definition solver/parameter.hh:25
const ParameterReader & parameter() const
Definition solver/parameter.hh:81
const std::string & keyPrefix() const
Definition solver/parameter.hh:79
Definition cachedcommmanager.hh:49