DUNE-FEM (unstable)

femscheme.hh
1#ifndef DUNE_FEM_SCHEMES_FEMSCHEME_HH
2#define DUNE_FEM_SCHEMES_FEMSCHEME_HH
3
5#include <dune/fem/operator/common/differentiableoperator.hh>
6#include <dune/fem/io/parameter/reader.hh>
7#include <dune/fem/solver/newtoninverseoperator.hh>
8#include <dune/fem/solver/preconditionfunctionwrapper.hh>
9
10namespace Dune
11{
12 namespace Fem
13 {
14
15// FemScheme
16//----------
17
18template < class Op, class DF, typename = void >
19struct AddDirichletBC
20{
21 static const bool value = false;
22 using DirichletBlockVector = void;
23};
24template < class Op, class DF>
25struct AddDirichletBC<Op,DF,std::enable_if_t<std::is_void< decltype( std::declval<const Op>().
26 setConstraints( std::declval<DF&>() ) )>::value > >
27{
28 static const bool value = true;
29 using DirichletBlockVector = typename Op::DirichletBlockVector;
30};
31
32template< class Operator, class LinearInverseOperator,
33 class InverseOperator = Dune::Fem::NewtonInverseOperator< typename Operator::JacobianOperatorType,
34 LinearInverseOperator > >
35class FemScheme
36{
37public:
39 typedef typename Operator::ModelType ModelType;
40 typedef typename Operator::DomainFunctionType DomainFunctionType;
41 typedef typename Operator::RangeFunctionType RangeFunctionType;
43 typedef Operator DifferentiableOperatorType;
44 typedef typename DiscreteFunctionType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
45 typedef LinearInverseOperator LinearInverseOperatorType;
46
48 typedef typename ModelType::GridPartType GridPartType;
49 static_assert( std::is_same< typename DiscreteFunctionSpaceType::GridPartType, GridPartType >::value,
50 "GridPart of Space has to be identical to GridPart of Model class" );
51
53 typedef typename GridPartType::GridType GridType;
54
57
58 typedef typename Operator::JacobianOperatorType JacobianOperatorType;
59 typedef typename Operator::JacobianOperatorType LinearOperatorType;
60
61 // type of inverse operator (could be nonlinear or linear depending on the derived class)
62 typedef InverseOperator InverseOperatorType;
63
64 typedef typename InverseOperatorType::ErrorMeasureType ErrorMeasureType;
65
68 typename LinearOperatorType::DomainFunctionType > PreconditionerFunctionWrapperType;
69 // std::function to represents the Python function passed as potential preconditioner
70 typedef typename PreconditionerFunctionWrapperType::PreconditionerFunctionType PreconditionerFunctionType ;
71
72 typedef typename FunctionSpaceType::RangeType RangeType;
73 static const int dimRange = FunctionSpaceType::dimRange;
74 static constexpr bool addDirichletBC = AddDirichletBC<Operator,DomainFunctionType>::value;
75 using DirichletBlockVector = typename AddDirichletBC<Operator,DomainFunctionType>::DirichletBlockVector;
76 /*********************************************************/
77
79 typedef typename InverseOperatorType::SolverInfoType SolverInfoType;
80
82 FemScheme ( const DiscreteFunctionSpaceType &space, ModelType &model,
83 const Dune::Fem::ParameterReader &parameter = Dune::Fem::Parameter::container() )
84 : space_( space ),
85 // the full discretized operator
86 fullOpPtr_( new DifferentiableOperatorType(space, space, model, parameter) ),
87 fullOperator_( *fullOpPtr_ ),
88 // create inverse operator to invert the operator
89 invOp_( parameter )
90 {}
91
93 template < class... Models >
94 FemScheme ( const DiscreteFunctionSpaceType &space, // discrete function space
95 const Dune::Fem::ParameterReader &parameter, // parameters
96 Models &&... models ) // list of models, could be more than one
97 : space_( space ),
98 // the full discretized operator
99 fullOpPtr_( new DifferentiableOperatorType( space, space, std::forward< Models >( models )... )),
100 fullOperator_( *fullOpPtr_ ),
101 // create inverse operator to invert the operator
102 invOp_( parameter )
103 {}
104
106 FemScheme ( DifferentiableOperatorType& fullOp,
107 const Dune::Fem::ParameterReader &parameter) // parameters
108 : space_( fullOp.space() ),
109 fullOpPtr_(), // empty here since fullOp reference is provided
110 // the full discretized operator
111 fullOperator_( fullOp ),
112 // create inverse operator to invert the operator
113 invOp_( parameter )
114 {}
115
116 const DifferentiableOperatorType &fullOperator() const { return fullOperator_; }
117 DifferentiableOperatorType &fullOperator() { return fullOperator_; }
118
119 template <typename O = DifferentiableOperatorType>
120 auto setQuadratureOrders(unsigned int interior, unsigned int surface)
121 -> Dune::void_t< decltype( std::declval< O >().setQuadratureOrders(0,0) ) >
122 {
123 fullOperator().setQuadratureOrders(interior,surface);
124 }
125
126 void setConstraints( DomainFunctionType &u ) const
127 {
128 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
129 fullOperator().setConstraints( u );
130 }
131 void setConstraints( const DiscreteFunctionType &u, DiscreteFunctionType &v ) const
132 {
133 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
134 fullOperator().setConstraints( u,v );
135 }
136 template <class GridFunctionType>
137 void setConstraints( const GridFunctionType &u, DiscreteFunctionType &v ) const
138 {
139 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
140 fullOperator().setConstraints( u, v );
141 }
142 void setConstraints( const RangeType &value, DiscreteFunctionType &u ) const
143 {
144 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
145 fullOperator().setConstraints( value, u );
146 }
147 void subConstraints( const DiscreteFunctionType &u, DiscreteFunctionType &v ) const
148 {
149 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
150 fullOperator().subConstraints( u, v );
151 }
152 void subConstraints( DiscreteFunctionType &v ) const
153 {
154 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
155 fullOperator().subConstraints( v );
156 }
157 void addConstraints( const DiscreteFunctionType &u, DiscreteFunctionType &v ) const
158 {
159 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
160 fullOperator().addConstraints( u, v );
161 }
162 void addConstraints( DiscreteFunctionType &v ) const
163 {
164 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
165 fullOperator().addConstraints( v );
166 }
167 const auto &dirichletBlocks() const
168 {
169 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
170 return fullOperator().dirichletBlocks();
171 }
172
173 void operator() ( const DiscreteFunctionType &arg, DiscreteFunctionType &dest ) const
174 {
175 fullOperator()( arg, dest );
176 }
177 template <class GridFunction>
178 auto operator() ( const GridFunction &arg, DiscreteFunctionType &dest ) const
180 {
181 fullOperator()( arg, dest );
182 }
183 void setErrorMeasure(ErrorMeasureType &errorMeasure) const
184 {
185 invOp_.setErrorMeasure(errorMeasure);
186 }
187
188 SolverInfoType solve ( const DiscreteFunctionType &rhs, DiscreteFunctionType &solution) const
189 {
190 invOp_.bind(fullOperator());
191 _solve(rhs,solution);
192 invOp_.unbind();
193 return invOp_.info();
194 }
195 SolverInfoType solve ( const DiscreteFunctionType &rhs, DiscreteFunctionType &solution, const PreconditionerFunctionType& p) const
196 {
197 PreconditionerFunctionWrapperType pre( p );
198 invOp_.bind(fullOperator(), pre);
199 _solve(rhs,solution);
200 invOp_.unbind();
201 return invOp_.info();
202 }
203 SolverInfoType solve ( DiscreteFunctionType &solution ) const
204 {
205 DiscreteFunctionType zero( solution );
206 zero.clear();
207 return solve(zero,solution);
208 }
209 SolverInfoType solve ( DiscreteFunctionType &solution, const PreconditionerFunctionType& p ) const
210 {
211 DiscreteFunctionType zero( solution );
212 zero.clear();
213 return solve(zero,solution,p);
214 }
215
216 template< class GridFunction, std::enable_if_t<
217 std::is_same< decltype(
218 std::declval< const DifferentiableOperatorType >().jacobian(
219 std::declval< const GridFunction& >(), std::declval< JacobianOperatorType& >()
220 )
221 ), void >::value, int> i = 0
222 >
223 void jacobian( const GridFunction &ubar, JacobianOperatorType &linOp ) const
224 {
225 fullOperator().jacobian(ubar, linOp);
226 }
227
228 const GridPartType &gridPart () const { return space().gridPart(); }
229 const DiscreteFunctionSpaceType &space( ) const { return space_; }
230
231 const ModelType &model() const
232 {
233 return fullOperator().model();
234 }
235 ModelType &model()
236 {
237 return fullOperator().model();
238 }
239protected:
240 SolverInfoType _solve ( const DiscreteFunctionType &rhs, DiscreteFunctionType &solution) const
241 { // setup for Newton scheme
242 setConstraints(solution); // sol=g on bnd
243 addConstraints(rhs,solution); // sol=g+rhs on bnd
244 invOp_( rhs, solution );
245 return invOp_.info();
246 }
247
248 const DiscreteFunctionSpaceType &space_; // discrete function space
249 std::shared_ptr< DifferentiableOperatorType > fullOpPtr_; // full operator object storage
250 DifferentiableOperatorType& fullOperator_; // reference to fullOperator (could be provided by derived class)
251 mutable InverseOperatorType invOp_; // non linear solver
252};
253} // end namespace Fem
254
255} // end namespace Dune
256#endif // #ifndef DUNE_FEM_SCHEMES_FEMSCHEME_HH
Inverse operator base on CG method. Uses a runtime parameter fem.preconditioning which enables diagon...
Definition: cginverseoperator.hh:408
Traits::FunctionSpaceType FunctionSpaceType
type of function space
Definition: discretefunctionspace.hh:194
FunctionSpaceTraits::RangeType RangeType
Type of range vector (using type of range field) has a Dune::FieldVector type interface.
Definition: functionspaceinterface.hh:71
@ dimRange
dimension of range vector space
Definition: functionspaceinterface.hh:48
A vector valued function space.
Definition: functionspace.hh:60
inverse operator based on a newton scheme
Definition: newtoninverseoperator.hh:357
Wrapper for functions passed from Python side that implements a preconditioner.
Definition: preconditionfunctionwrapper.hh:23
forward declaration
Definition: discretefunction.hh:51
TupleDiscreteFunctionSpace< typename DiscreteFunctions::DiscreteFunctionSpaceType ... > DiscreteFunctionSpaceType
type for the discrete function space this function lives in
Definition: discretefunction.hh:69
constexpr auto models()
Check if concept is modeled by given types.
Definition: concept.hh:184
typename Impl::voider< Types... >::type void_t
Is void for all valid input types. The workhorse for C++11 SFINAE-techniques.
Definition: typetraits.hh:40
Dune namespace.
Definition: alignedallocator.hh:13
STL namespace.
DomainFunction DomainFunctionType
type of discrete function in the operator's domain
Definition: operator.hh:36
RangeFunction RangeFunctionType
type of discrete function in the operator's range
Definition: operator.hh:38
SparseRowLinearOperator.
Definition: spoperator.hh:25
Utilities for type computations, constraining overloads, ...
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)