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 std::size_t gridSizeInterior () const { return fullOperator().gridSizeInterior(); }
120
121 template <typename O = DifferentiableOperatorType>
122 auto setQuadratureOrders(unsigned int interior, unsigned int surface)
123 -> Dune::void_t< decltype( std::declval< O >().setQuadratureOrders(0,0) ) >
124 {
125 fullOperator().setQuadratureOrders(interior,surface);
126 }
127
128 void setConstraints( DomainFunctionType &u ) const
129 {
130 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
131 fullOperator().setConstraints( u );
132 }
133 void setConstraints( const DiscreteFunctionType &u, DiscreteFunctionType &v ) const
134 {
135 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
136 fullOperator().setConstraints( u,v );
137 }
138 template <class GridFunctionType>
139 void setConstraints( const GridFunctionType &u, DiscreteFunctionType &v ) const
140 {
141 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
142 fullOperator().setConstraints( u, v );
143 }
144 void setConstraints( const RangeType &value, DiscreteFunctionType &u ) const
145 {
146 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
147 fullOperator().setConstraints( value, u );
148 }
149 void setConstraints( JacobianOperatorType &lin ) const
150 {
151 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
152 fullOperator().setConstraints( lin );
153 }
154 void subConstraints( const DiscreteFunctionType &u, DiscreteFunctionType &v ) const
155 {
156 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
157 fullOperator().subConstraints( u, v );
158 }
159 void subConstraints( DiscreteFunctionType &v ) const
160 {
161 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
162 fullOperator().subConstraints( v );
163 }
164 void addConstraints( const DiscreteFunctionType &u, DiscreteFunctionType &v ) const
165 {
166 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
167 fullOperator().addConstraints( u, v );
168 }
169 void addConstraints( DiscreteFunctionType &v ) const
170 {
171 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
172 fullOperator().addConstraints( v );
173 }
174 const auto &dirichletBlocks() const
175 {
176 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
177 return fullOperator().dirichletBlocks();
178 }
179
180 void operator() ( const DiscreteFunctionType &arg, DiscreteFunctionType &dest ) const
181 {
182 fullOperator()( arg, dest );
183 }
184 template <class GridFunction>
185 auto operator() ( const GridFunction &arg, DiscreteFunctionType &dest ) const
187 {
188 fullOperator()( arg, dest );
189 }
190 void setErrorMeasure(ErrorMeasureType &errorMeasure) const
191 {
192 invOp_.setErrorMeasure(errorMeasure);
193 }
194
195 SolverInfoType solve ( const DiscreteFunctionType &rhs, DiscreteFunctionType &solution) const
196 {
197 invOp_.bind(fullOperator());
198 _solve(rhs,solution);
199 invOp_.unbind();
200 return invOp_.info();
201 }
202 SolverInfoType solve ( const DiscreteFunctionType &rhs, DiscreteFunctionType &solution, const PreconditionerFunctionType& p) const
203 {
204 PreconditionerFunctionWrapperType pre( p );
205 invOp_.bind(fullOperator(), pre);
206 _solve(rhs,solution);
207 invOp_.unbind();
208 return invOp_.info();
209 }
210 SolverInfoType solve ( DiscreteFunctionType &solution ) const
211 {
212 DiscreteFunctionType zero( solution );
213 zero.clear();
214 return solve(zero,solution);
215 }
216 SolverInfoType solve ( DiscreteFunctionType &solution, const PreconditionerFunctionType& p ) const
217 {
218 DiscreteFunctionType zero( solution );
219 zero.clear();
220 return solve(zero,solution,p);
221 }
222
223 template< class GridFunction, std::enable_if_t<
224 std::is_same< decltype(
225 std::declval< const DifferentiableOperatorType >().jacobian(
226 std::declval< const GridFunction& >(), std::declval< JacobianOperatorType& >()
227 )
228 ), void >::value, int> i = 0
229 >
230 void jacobian( const GridFunction &ubar, JacobianOperatorType &linOp ) const
231 {
232 fullOperator().jacobian(ubar, linOp);
233 }
234
235 const GridPartType &gridPart () const { return space().gridPart(); }
236 const DiscreteFunctionSpaceType &space( ) const { return space_; }
237
238 const ModelType &model() const
239 {
240 return fullOperator().model();
241 }
242 ModelType &model()
243 {
244 return fullOperator().model();
245 }
246protected:
247 SolverInfoType _solve ( const DiscreteFunctionType &rhs, DiscreteFunctionType &solution) const
248 { // setup for Newton scheme
249 setConstraints(solution); // sol=g on bnd
250 addConstraints(rhs,solution); // sol=g+rhs on bnd
251 invOp_( rhs, solution );
252 return invOp_.info();
253 }
254
255 const DiscreteFunctionSpaceType &space_; // discrete function space
256 std::shared_ptr< DifferentiableOperatorType > fullOpPtr_; // full operator object storage
257 DifferentiableOperatorType& fullOperator_; // reference to fullOperator (could be provided by derived class)
258 mutable InverseOperatorType invOp_; // non linear solver
259};
260} // end namespace Fem
261
262} // end namespace Dune
263#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 & Uni Heidelberg  |  generated with Hugo v0.111.3 (Sep 5, 22:35, 2025)