Loading [MathJax]/extensions/tex2jax.js

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 subConstraints( const DiscreteFunctionType &u, DiscreteFunctionType &v ) const
150 {
151 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
152 fullOperator().subConstraints( u, v );
153 }
154 void subConstraints( DiscreteFunctionType &v ) const
155 {
156 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
157 fullOperator().subConstraints( v );
158 }
159 void addConstraints( const DiscreteFunctionType &u, DiscreteFunctionType &v ) const
160 {
161 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
162 fullOperator().addConstraints( u, v );
163 }
164 void addConstraints( DiscreteFunctionType &v ) const
165 {
166 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
167 fullOperator().addConstraints( v );
168 }
169 const auto &dirichletBlocks() const
170 {
171 if constexpr (AddDirichletBC<Operator,DomainFunctionType>::value)
172 return fullOperator().dirichletBlocks();
173 }
174
175 void operator() ( const DiscreteFunctionType &arg, DiscreteFunctionType &dest ) const
176 {
177 fullOperator()( arg, dest );
178 }
179 template <class GridFunction>
180 auto operator() ( const GridFunction &arg, DiscreteFunctionType &dest ) const
182 {
183 fullOperator()( arg, dest );
184 }
185 void setErrorMeasure(ErrorMeasureType &errorMeasure) const
186 {
187 invOp_.setErrorMeasure(errorMeasure);
188 }
189
190 SolverInfoType solve ( const DiscreteFunctionType &rhs, DiscreteFunctionType &solution) const
191 {
192 invOp_.bind(fullOperator());
193 _solve(rhs,solution);
194 invOp_.unbind();
195 return invOp_.info();
196 }
197 SolverInfoType solve ( const DiscreteFunctionType &rhs, DiscreteFunctionType &solution, const PreconditionerFunctionType& p) const
198 {
199 PreconditionerFunctionWrapperType pre( p );
200 invOp_.bind(fullOperator(), pre);
201 _solve(rhs,solution);
202 invOp_.unbind();
203 return invOp_.info();
204 }
205 SolverInfoType solve ( DiscreteFunctionType &solution ) const
206 {
207 DiscreteFunctionType zero( solution );
208 zero.clear();
209 return solve(zero,solution);
210 }
211 SolverInfoType solve ( DiscreteFunctionType &solution, const PreconditionerFunctionType& p ) const
212 {
213 DiscreteFunctionType zero( solution );
214 zero.clear();
215 return solve(zero,solution,p);
216 }
217
218 template< class GridFunction, std::enable_if_t<
219 std::is_same< decltype(
220 std::declval< const DifferentiableOperatorType >().jacobian(
221 std::declval< const GridFunction& >(), std::declval< JacobianOperatorType& >()
222 )
223 ), void >::value, int> i = 0
224 >
225 void jacobian( const GridFunction &ubar, JacobianOperatorType &linOp ) const
226 {
227 fullOperator().jacobian(ubar, linOp);
228 }
229
230 const GridPartType &gridPart () const { return space().gridPart(); }
231 const DiscreteFunctionSpaceType &space( ) const { return space_; }
232
233 const ModelType &model() const
234 {
235 return fullOperator().model();
236 }
237 ModelType &model()
238 {
239 return fullOperator().model();
240 }
241protected:
242 SolverInfoType _solve ( const DiscreteFunctionType &rhs, DiscreteFunctionType &solution) const
243 { // setup for Newton scheme
244 setConstraints(solution); // sol=g on bnd
245 addConstraints(rhs,solution); // sol=g+rhs on bnd
246 invOp_( rhs, solution );
247 return invOp_.info();
248 }
249
250 const DiscreteFunctionSpaceType &space_; // discrete function space
251 std::shared_ptr< DifferentiableOperatorType > fullOpPtr_; // full operator object storage
252 DifferentiableOperatorType& fullOperator_; // reference to fullOperator (could be provided by derived class)
253 mutable InverseOperatorType invOp_; // non linear solver
254};
255} // end namespace Fem
256
257} // end namespace Dune
258#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 (Apr 21, 22:33, 2025)