DUNE-FEM (unstable)

amgxsolver.hh
1#ifndef DUNE_FEM_AMGXSOLVER_HH
2#define DUNE_FEM_AMGXSOLVER_HH
3
4#include <limits>
5
6#include <dune/fem/function/common/scalarproducts.hh>
7#include <dune/fem/operator/common/operator.hh>
8#include <dune/fem/io/parameter.hh>
9
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>
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 {
37 typedef LocalParameter< SolverParameter, AMGXSolverParameter > BaseType;
38
39 public:
40 using BaseType :: parameter;
41 using BaseType :: keyPrefix;
42
43 AMGXSolverParameter( const ParameterReader& parameter = Parameter::container() )
44 : BaseType( parameter )
45 {}
46
47
48 AMGXSolverParameter( const std::string &keyPrefix, const ParameterReader &parameter = Parameter::container() )
49 : BaseType( keyPrefix, parameter )
50 {}
51
52 AMGXSolverParameter( const SolverParameter& sp )
53 : AMGXSolverParameter( sp.keyPrefix(), sp.parameter() )
54 {}
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 >
78 struct AMGXInverseOperatorTraits
79 {
80 typedef DiscreteFunction DiscreteFunctionType;
81 typedef PetscDiscreteFunction< typename DiscreteFunction::DiscreteFunctionSpaceType > SolverDiscreteFunctionType;
82
84 typedef OperatorType PreconditionerType;
85
86#if HAVE_AMGXSOLVER
87 typedef Fem::PetscLinearOperator< DiscreteFunction, DiscreteFunction > AssembledOperatorType;
88#else
89 typedef OperatorType AssembledOperatorType;
90#endif
91
92 typedef AMGXInverseOperator< DiscreteFunction > InverseOperatorType;
93
94 typedef AMGXSolverParameter SolverParameterType;
95 };
96
97
98
99
101 template< class DF >
103 : public InverseOperatorInterface< AMGXInverseOperatorTraits< DF > >
104 {
105 typedef AMGXInverseOperatorTraits< DF > Traits;
106 typedef InverseOperatorInterface< Traits > BaseType;
107 friend class InverseOperatorInterface< Traits >;
108 public:
109 using BaseType :: parameter;
110
112 static const bool preconditioningAvailable = false;
113
114 typedef typename BaseType::SolverDiscreteFunctionType SolverDiscreteFunctionType;
115 typedef typename BaseType::OperatorType OperatorType;
116 typedef typename BaseType::PreconditionerType PreconditionerType;
117 typedef typename BaseType::AssembledOperatorType AssembledOperatorType;
118
123 AMGXInverseOperator ( const AMGXSolverParameter& parameter = AMGXSolverParameter() )
124 : BaseType( parameter )
125 {
126 }
127
128 AMGXInverseOperator ( const OperatorType &op,
129 const AMGXSolverParameter & parameter = AMGXSolverParameter() )
130 : AMGXInverseOperator ( parameter )
131 {
132 bind( op );
133 }
134
135 AMGXInverseOperator ( const OperatorType &op, PreconditionerType &preconditioner,
136 const AMGXSolverParameter & parameter = AMGXSolverParameter() )
137 : AMGXInverseOperator( parameter )
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:
165 void init( const AMGXSolverParameter& parameter )
166 {
167 if( assembledOperator_ )
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
190 int apply( const SolverDiscreteFunctionType& arg, SolverDiscreteFunctionType& dest ) const
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
AMGX solver context for PETSc Mat and PETSc Vec.
Definition: amgxsolver.hh:104
AMGXInverseOperator(const AMGXSolverParameter &parameter=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
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)