dune-fem 2.12-git
Loading...
Searching...
No Matches
linearized.hh
Go to the documentation of this file.
1#ifndef DUNE_FEM_SCHEMES_LINEARIZED_HH
2#define DUNE_FEM_SCHEMES_LINEARIZED_HH
3
4#include <cstddef>
5
6#include <tuple>
7#include <type_traits>
8#include <utility>
9#include <vector>
10
15
16
17namespace Dune
18{
19
20 namespace Fem
21 {
22 /*
23 Idea is to setup the first two terms in the Taylor expansion:
24 L[w] = S[u] + DS[u](w-u)
25 = S[u] - DS[u]u + DS[u]w
26 = DS[u]w + b
27
28 So b = S[u] - DS[u]u
29 and L[w] = DS[u]w + b
30
31 The solve method with rhs=f computs the solution w to
32 L[w] = f <=> DS[u]w = f-b
33
34 With Dirichlet BCs:
35 From construction of the underlying scheme we have
36 S[u] = u - g and DS[u]w = w on the boundary.
37
38 Therefore on the boundary we have
39 b = S[u] - DS[u]u = u - g - u = -g
40 and L[w] = DS[u]w + b = w - g
41 and solving L[w] = f is the same as DS[u]w = f-b or w = f-b = f+g on the boundary
42
43 Note: we store -b in the LinearizedScheme, the LinearScheme
44 represents the above but with b=0
45 */
46
48
49 // This class implements the Jacobian part of a scheme, i.e., L[w] = DS[u]w
50 // without the rhs 'b'.
51 template< class Scheme >
52 struct LinearScheme : public Scheme::LinearOperatorType,
53 public FemScheme< Scheme, typename Scheme::LinearInverseOperatorType, typename Scheme::LinearInverseOperatorType >
54 {
55 typedef Scheme SchemeType;
56 typedef typename SchemeType::LinearInverseOperatorType LinearInverseOperatorType;
57
58 // base types
59 typedef typename SchemeType::LinearOperatorType BaseType;
61
62 typedef typename SchemeType::DiscreteFunctionType DiscreteFunctionType;
63 typedef typename SchemeType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
64 typedef typename SchemeType::GridPartType GridPartType;
65 typedef typename LinearInverseOperatorType::SolverParameterType ParameterType;
66 typedef typename SchemeType::ModelType ModelType;
67
68 typedef typename SchemeType::JacobianOperatorType JacobianOperatorType;
69 typedef typename SchemeType::DomainFunctionType DomainFunctionType;
70 typedef typename SchemeType::RangeFunctionType RangeFunctionType;
71
72 typedef typename SchemeType::DirichletBlockVector DirichletBlockVector;
73
75 // std::function to represents the Python function passed as potential preconditioner
77
80 using FSBaseType :: space;
81
82 // from femscheme.hh
83 typedef typename LinearInverseOperatorType::SolverInfoType SolverInfoType ;
84
95
99 : BaseType( "linearized Op", scheme.space(), scheme.space(), parameter ),
101 isBound_(false),
102 parameter_( std::move( parameter ) ),
103 tmp_( "LS::tmp", scheme.space() )
104 {
105 fullOperator().jacobian(ubar,*this);
106 }
107
112 void setErrorMeasure() const {}
113
114 using BaseType::clear;
115 virtual void clear() override
116 {
117 BaseType::clear();
118 invOp_.unbind();
119 isBound_ = false;
120 }
121
122 void operator() ( const DiscreteFunctionType &u, DiscreteFunctionType &w ) const override
123 {
124 // use linear op (instead of fullOperator)
125 BaseType::operator()(u,w);
126 }
127
128 template <class GridFunction>
129 void operator() ( const GridFunction &arg, DiscreteFunctionType &dest ) const
130 {
132 // apply operator (i.e. matvec)
133 (*this)(tmp_, dest);
134 }
135
137 {
138 if (!isBound_)
139 {
140 invOp_.bind(*this);
141 isBound_ = true;
142 }
143
144 // copy Dirichlet constraints from rhs to solution
145 setConstraints(rhs, solution);
146 invOp_(rhs, solution );
147 return invOp_.info();
148 }
149
151 {
152 if (isBound_)
153 invOp_.unbind();
154
156 invOp_.bind(*this, pre);
157 isBound_ = true;
158 auto info = solve( rhs, solution );
159 invOp_.unbind();
160 isBound_ = false;
161 return info;
162 }
163
172 {
173 if (!isBound_)
174 {
175 invOp_.bind(*this);
176 isBound_ = true;
177 }
179 zero.clear();
180 setConstraints(typename DiscreteFunctionType::RangeType(0), solution);
181 invOp_( zero, solution );
182 return invOp_.info();
183 }
184
185 const SchemeType &scheme() const { return fullOperator(); }
186 const ParameterReader& parameter () const { return parameter_; }
187
189 protected:
190 using FSBaseType :: invOp_;
191 mutable bool isBound_;
194 };
195
196 // This class stores a 'LinearScheme' and defines 'setup' methods that
197 // compute the rhs 'b'. All methods are then
198 template< class Scheme >
200 : public Dune::Fem::Operator<typename Scheme::DomainFunctionType, typename Scheme::RangeFunctionType>
201 {
202 typedef Scheme SchemeType;
203 typedef typename SchemeType::DiscreteFunctionType DiscreteFunctionType;
204 typedef typename SchemeType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
205 typedef typename SchemeType::GridPartType GridPartType;
206 typedef typename SchemeType::LinearInverseOperatorType LinearInverseOperatorType;
207 typedef typename SchemeType::ModelType ModelType;
208
209 typedef typename SchemeType::JacobianOperatorType JacobianOperatorType;
210 typedef typename SchemeType::DomainFunctionType DomainFunctionType;
211 typedef typename SchemeType::RangeFunctionType RangeFunctionType;
212
213 typedef typename SchemeType::DirichletBlockVector DirichletBlockVector;
216
220 // std::function to represents the Python function passed as potential preconditioner
222
225 : linOp_( scheme, parameter ),
226 rhs_( "affine shift", scheme.space() ),
227 ubar_( "ubar", scheme.space() )
228 {
229 setup();
230 }
233 : linOp_( scheme, ubar, parameter ),
234 rhs_( "affine shift", scheme.space() ),
235 ubar_( "ubar", scheme.space() )
236 {
237 setup(ubar);
238 }
239
240 void setup(const DiscreteFunctionType &ubar)
241 {
242 ubar_.assign(ubar);
243 setup_();
244 }
245
246 template <class GridFunction>
247 void setup(const GridFunction &ubar)
248 {
249 Fem::interpolate(ubar, ubar_);
250 setup_();
251 }
252 void setup()
253 {
254 ubar_.clear();
255 setup_(true); // provide info that `ubar=0`
256 }
257
262 void setErrorMeasure() const {}
264 {
266 }
267 template <class LinOp>
268 void setConstraints( LinOp &lin ) const
269 {
271 }
272 void setConstraints( const typename DiscreteFunctionType::RangeType &value, DiscreteFunctionType &u ) const
273 {
274 linOp_.setConstraints(value, u);
275 }
277 {
279 }
280 template < class GridFunctionType,
282 void setConstraints( const GridFunctionType &u, DiscreteFunctionType &v ) const
283 {
285 }
287 {
289 }
291 {
293 }
295 {
297 }
299 {
301 }
302 const auto& dirichletBlocks() const
303 {
304 return linOp_.dirichletBlocks();
305 }
306
307 void operator() ( const DiscreteFunctionType &u, DiscreteFunctionType &w ) const override
308 {
309 linOp_(u,w);
310 w -= rhs_;
311 }
312 template <class GridFunction>
313 void operator() ( const GridFunction &arg, DiscreteFunctionType &w ) const
314 {
315 linOp_(arg, w);
316 w -= rhs_;
317 }
318
320 {
321 return _solve(rhs, solution, nullptr );
322 }
323
325 {
326 // TODO: Pass preconditioning to solver !
327 return _solve( rhs, solution, &p );
328 }
329
338 {
339 // rhs_ = DS[u]u-S[u] and = u-(u-g) = g on boundary so
340 // solution = u - DS[u]^{-1}S[u] and = g on the boundary
341 return linOp_.solve( rhs_, solution );
342 }
343
344 const GridPartType &gridPart () const { return linOp_.gridPart(); }
345 const DiscreteFunctionSpaceType &space() const { return linOp_.space(); }
346 const SchemeType &scheme() { return linOp_.scheme(); }
347 const ParameterReader& parameter () const { return linOp_.parameter(); }
348 const LinearOperatorType& linearScheme () const { return linOp_; }
349
350 protected:
352 {
354 sumRhs.assign(rhs);
355 sumRhs += rhs_;
356
357 // rhs_ = DS[u]u-S[u] and = u-(u-g) = g on boundary so
358 // solution = u - DS[u]^{-1}(rhs+S[u]) and = rhs+g on the boundary
359 if( p )
360 return linOp_.solve( sumRhs, solution, *p); // don't add constraints again
361 else
362 return linOp_.solve( sumRhs, solution); // don't add constraints again
363 }
364
365 void setup_(bool isZero=false)
366 {
367 scheme().jacobian(ubar_, linOp_);
368
369 // compute rhs
371
372 // compute tmp = S[u] (tmp = u-g on boundary)
373 tmp.clear();
374 scheme()( ubar_, tmp );
375
376 // compute rhs_ = DS[u]u (rhs_ = u on boundary)
377 rhs_.clear();
378 if (!isZero)
379 linOp_( ubar_, rhs_ );
380
381 // compute rhs_ - tmp = DS[u]u-S[u] and u-(u-g)=g on boundary
382 rhs_ -= tmp;
383 }
384
388 };
389
390 } // namespace Fem
391
392} // namespace Dune
393
394#endif // #ifndef DUNE_FEM_SCHEMES_LINEARIZED_HH
void pre(Domain &x, Range &b)
Y & rhs()
virtual void operator()()=0
static void interpolate(const GridFunction &u, DiscreteFunction &v)
perform native interpolation of a discrete function space
Definition common/interpolate.hh:55
STL namespace.
static ParameterContainer & container()
Definition io/parameter.hh:199
Definition grcommon.hh:31
abstract operator
Definition operator.hh:34
Definition femscheme.hh:36
const DiscreteFunctionSpaceType & space() const
Definition femscheme.hh:236
void subConstraints(const DiscreteFunctionType &u, DiscreteFunctionType &v) const
Definition femscheme.hh:154
const DifferentiableOperatorType & fullOperator() const
Definition femscheme.hh:116
const GridPartType & gridPart() const
Definition femscheme.hh:235
void addConstraints(const DiscreteFunctionType &u, DiscreteFunctionType &v) const
Definition femscheme.hh:164
const auto & dirichletBlocks() const
Definition femscheme.hh:174
Definition linearized.hh:54
SchemeType::ModelType ModelType
Definition linearized.hh:66
SchemeType::RangeFunctionType RangeFunctionType
Definition linearized.hh:70
const ParameterReader & parameter() const
Definition linearized.hh:186
Dune::Fem::PreconditionerFunctionWrapper< RangeFunctionType, DomainFunctionType > PreconditionerFunctionWrapperType
Definition linearized.hh:74
DiscreteFunctionType tmp_
Definition linearized.hh:193
const SchemeType & scheme() const
Definition linearized.hh:185
Dune::Fem::ParameterReader parameter_
Definition linearized.hh:192
LinearScheme(SchemeType &scheme, const DiscreteFunctionType &ubar, Dune::Fem::ParameterReader parameter=Dune::Fem::Parameter::container())
Definition linearized.hh:97
SolverInfoType solve(const DiscreteFunctionType &rhs, DiscreteFunctionType &solution) const
Definition linearized.hh:136
LinearInverseOperatorType::SolverParameterType ParameterType
Definition linearized.hh:65
SchemeType::LinearOperatorType BaseType
Definition linearized.hh:59
SchemeType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType
Definition linearized.hh:63
LinearInverseOperatorType::SolverInfoType SolverInfoType
Definition linearized.hh:83
SchemeType::DomainFunctionType DomainFunctionType
Definition linearized.hh:69
SchemeType::JacobianOperatorType JacobianOperatorType
Definition linearized.hh:68
SchemeType::GridPartType GridPartType
Definition linearized.hh:64
void setErrorMeasure() const
Definition linearized.hh:112
SchemeType::LinearInverseOperatorType LinearInverseOperatorType
Definition linearized.hh:56
virtual void clear() override
Definition linearized.hh:115
DiscreteFunctionType & temporaryData() const
Definition linearized.hh:188
bool isBound_
Definition linearized.hh:191
Scheme SchemeType
Definition linearized.hh:55
SchemeType::DiscreteFunctionType DiscreteFunctionType
Definition linearized.hh:62
PreconditionerFunctionWrapperType::PreconditionerFunctionType PreconditionerFunctionType
Definition linearized.hh:76
LinearScheme(SchemeType &scheme, Dune::Fem::ParameterReader parameter=Dune::Fem::Parameter::container())
Definition linearized.hh:86
FemScheme< SchemeType, LinearInverseOperatorType, LinearInverseOperatorType > FSBaseType
Definition linearized.hh:60
SchemeType::DirichletBlockVector DirichletBlockVector
Definition linearized.hh:72
SolverInfoType solve(DiscreteFunctionType &solution) const
Definition linearized.hh:171
SolverInfoType solve(const DiscreteFunctionType &rhs, DiscreteFunctionType &solution, const PreconditionerFunctionType &p) const
Definition linearized.hh:150
Definition linearized.hh:201
void setup()
Definition linearized.hh:252
DiscreteFunctionType rhs_
Definition linearized.hh:386
LinearOperatorType::SolverInfoType SolverInfoType
Definition linearized.hh:215
DiscreteFunctionType ubar_
Definition linearized.hh:387
PreconditionerFunctionWrapperType::PreconditionerFunctionType PreconditionerFunctionType
Definition linearized.hh:221
SchemeType::DiscreteFunctionType DiscreteFunctionType
Definition linearized.hh:203
Dune::Fem::PreconditionerFunctionWrapper< typename LinearOperatorType::RangeFunctionType, typename LinearOperatorType::DomainFunctionType > PreconditionerFunctionWrapperType
Definition linearized.hh:219
SolverInfoType solve(const DiscreteFunctionType &rhs, DiscreteFunctionType &solution, const PreconditionerFunctionType &p) const
Definition linearized.hh:324
SchemeType::ModelType ModelType
Definition linearized.hh:207
LinearizedScheme(SchemeType &scheme, const Dune::Fem::ParameterReader &parameter=Dune::Fem::Parameter::container())
Definition linearized.hh:223
void subConstraints(const DiscreteFunctionType &u, DiscreteFunctionType &v) const
Definition linearized.hh:286
void setup(const DiscreteFunctionType &ubar)
Definition linearized.hh:240
SchemeType::LinearInverseOperatorType LinearInverseOperatorType
Definition linearized.hh:206
void subConstraints(DiscreteFunctionType &v) const
Definition linearized.hh:290
SchemeType::DomainFunctionType DomainFunctionType
Definition linearized.hh:210
void setConstraints(const GridFunctionType &u, DiscreteFunctionType &v) const
Definition linearized.hh:282
LinearScheme< SchemeType > LinearOperatorType
Definition linearized.hh:214
const GridPartType & gridPart() const
Definition linearized.hh:344
const SchemeType & scheme()
Definition linearized.hh:346
SchemeType::GridPartType GridPartType
Definition linearized.hh:205
SolverInfoType solve(DiscreteFunctionType &solution) const
Definition linearized.hh:337
Scheme SchemeType
Definition linearized.hh:202
SchemeType::DirichletBlockVector DirichletBlockVector
Definition linearized.hh:213
const ParameterReader & parameter() const
Definition linearized.hh:347
void setConstraints(const DiscreteFunctionType &u, DiscreteFunctionType &v) const
Definition linearized.hh:276
void setConstraints(DomainFunctionType &u) const
Definition linearized.hh:263
void setConstraints(LinOp &lin) const
Definition linearized.hh:268
SchemeType::RangeFunctionType RangeFunctionType
Definition linearized.hh:211
void setConstraints(const typename DiscreteFunctionType::RangeType &value, DiscreteFunctionType &u) const
Definition linearized.hh:272
SolverInfoType _solve(const DiscreteFunctionType &rhs, DiscreteFunctionType &solution, const PreconditionerFunctionType *p) const
Definition linearized.hh:351
const auto & dirichletBlocks() const
Definition linearized.hh:302
LinearOperatorType linOp_
Definition linearized.hh:385
void setErrorMeasure() const
Definition linearized.hh:262
LinearizedScheme(SchemeType &scheme, const DiscreteFunctionType &ubar, const Dune::Fem::ParameterReader &parameter=Dune::Fem::Parameter::container())
Definition linearized.hh:231
const LinearOperatorType & linearScheme() const
Definition linearized.hh:348
void setup(const GridFunction &ubar)
Definition linearized.hh:247
SchemeType::JacobianOperatorType JacobianOperatorType
Definition linearized.hh:209
void setup_(bool isZero=false)
Definition linearized.hh:365
const DiscreteFunctionSpaceType & space() const
Definition linearized.hh:345
void addConstraints(DiscreteFunctionType &v) const
Definition linearized.hh:298
SchemeType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType
Definition linearized.hh:204
SolverInfoType solve(const DiscreteFunctionType &rhs, DiscreteFunctionType &solution) const
Definition linearized.hh:319
void addConstraints(const DiscreteFunctionType &u, DiscreteFunctionType &v) const
Definition linearized.hh:294
Wrapper for functions passed from Python side that implements a preconditioner.
Definition preconditionfunctionwrapper.hh:23