DUNE-ACFEM (2.5.1)

linearfunctional.hh
1 #ifndef _DUNE_ACFEM_DISCRETELINEARFUNCTIONAL_HH_
2 #define _DUNE_ACFEM_DISCRETELINEARFUNCTIONAL_HH_
3 
4 #include <dune/fem/misc/bartonnackmaninterface.hh>
5 #include <dune/fem/function/common/discretefunction.hh>
6 #include <dune/fem/function/localfunction/temporary.hh>
7 
8 #include "../../common/localobjectstorage.hh"
9 
10 namespace Dune {
11 
12  namespace ACFem {
13 
36  template<class GlobalFunctional, class LocalFunctional>
38  {
39  typedef GlobalFunctional GlobalFunctionalType;
40  typedef LocalFunctional LocalFunctionalType;
41  };
42 
48  template<class DiscreteFunctionSpace, class Traits>
50  : public Fem::BartonNackmanInterface<DiscreteLinearFunctional<DiscreteFunctionSpace, Traits>,
51  typename Traits::GlobalFunctionalType>
52  {
54  typedef
55  Fem::BartonNackmanInterface<DiscreteLinearFunctional<DiscreteFunctionSpace, Traits>,
56  typename Traits::GlobalFunctionalType>
57  BaseType;
58  public:
59  typedef Traits TraitsType;
60  typedef typename TraitsType::LocalFunctionalType LocalFunctionalType;
61  typedef DiscreteFunctionSpace DiscreteFunctionSpaceType;
62  typedef typename DiscreteFunctionSpaceType::GridPartType GridPartType;
63  typedef typename DiscreteFunctionSpaceType::EntityType EntityType;
64  typedef typename DiscreteFunctionSpace::FunctionSpaceType FunctionSpaceType;
65  typedef typename FunctionSpaceType::RangeFieldType FieldType;
66  typedef FieldType RangeType;
67  protected:
68  using BaseType::asImp;
69 
71  {}
72 
74  {}
75 
76  private:
77  ThisType& operator=(const ThisType& other);
78 
79  public:
81  template<class DFImpl>
82  RangeType operator()(const Fem::DiscreteFunctionInterface<DFImpl>& arg) const
83  {
84  static_assert(std::is_same<DiscreteFunctionSpaceType, typename DFImpl::DiscreteFunctionSpaceType>::value,
85  "Invalid Argument");
86  CHECK_INTERFACE_IMPLEMENTATION(asImp()(arg));
87  return asImp()(arg);
88  }
89 
91  template<class DFImpl>
92  void coefficients(Fem::DiscreteFunctionInterface<DFImpl>& coeffs)
93  {
94  static_assert(std::is_same<DiscreteFunctionSpaceType, typename DFImpl::DiscreteFunctionSpaceType>::value,
95  "Invalid Argument");
96  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION(asImp().coefficients(coeffs));
97  }
98 
99  const DiscreteFunctionSpaceType& space() const
100  {
101  CHECK_INTERFACE_IMPLEMENTATION(asImp().space());
102  return asImp().space();
103  }
104 
105  LocalFunctionalType localFunctional(const EntityType& entity)
106  {
107  CHECK_INTERFACE_IMPLEMENTATION(asImp().localFunctional(entity));
108  return asImp().localFunctional(entity);
109  }
110 
111  const LocalFunctionalType localFunctional(const EntityType& entity) const
112  {
113  CHECK_INTERFACE_IMPLEMENTATION(asImp().localFunctional(entity));
114  return asImp().localFunctional(entity);
115  }
116 
117  std::string name() const
118  {
119  CHECK_INTERFACE_IMPLEMENTATION(asImp().name());
120  return asImp().name();
121  }
122  };
123 
129  template<class DiscreteFunctionSpace, class Traits>
131  : public DiscreteLinearFunctional<DiscreteFunctionSpace, Traits>
132  {
135  public:
136  typedef Traits TraitsType;
137  typedef typename TraitsType::LocalFunctionalType LocalFunctionalType;
138  typedef typename TraitsType::GlobalFunctionalType GlobalFunctionalType;
139  typedef DiscreteFunctionSpace DiscreteFunctionSpaceType;
140  typedef typename DiscreteFunctionSpaceType::GridPartType GridPartType;
141  typedef typename DiscreteFunctionSpaceType::EntityType EntityType;
142  typedef typename DiscreteFunctionSpace::FunctionSpaceType FunctionSpaceType;
143  typedef typename FunctionSpaceType::RangeFieldType FieldType;
144  typedef FieldType RangeType;
145 
146  protected:
147  using BaseType::asImp;
148 
149  DiscreteLinearFunctionalDefault(const DiscreteFunctionSpaceType& space)
150  : space_(space)
151  {}
152 
154  : space_(other.space_)
155  {}
156 
157  ThisType& operator=(const ThisType& other) = delete;
158 
159  public:
161  template<class DFTraits>
162  RangeType operator()(const Fem::DiscreteFunctionInterface<DFTraits>& arg) const
163  {
164  static_assert(std::is_same<DiscreteFunctionSpaceType, typename DFTraits::DiscreteFunctionSpaceType>::value,
165  "Invalid Argument");
166 
167  typedef typename Fem::DiscreteFunctionInterface<DFTraits> DiscreteFunctionType;
168  typedef typename DiscreteFunctionType::LocalFunctionType LocalFunctionType;
169 
170  // get discrete function space
171  const auto& dfSpace = arg.space();
172 
173  auto localPhi = asImp().localFunctional();
174  LocalFunctionType localArg(ACFem::asImp(arg));
175 
176  RangeType value = 0;
177  auto end = dfSpace.end();
178  for (auto it = dfSpace.begin(); it != end; ++it) {
179  // get entity (here element)
180  const auto &entity = *it;
181  localPhi.init(entity);
182  localArg.init(entity);
183 
184  value += localPhi(localArg);
185  }
186  return value;
187  }
188 
190  template<class DFTraits>
191  void coefficients(Fem::DiscreteFunctionInterface<DFTraits>& coeffs)
192  {
193  static_assert(std::is_same<DiscreteFunctionSpaceType, typename DFTraits::DiscreteFunctionSpaceType>::value,
194  "Invalid Argument");
195 
196  typedef typename Fem::DiscreteFunctionInterface<DFTraits> DiscreteFunctionType;
197  typedef typename DiscreteFunctionType::LocalFunctionType LocalFunctionType;
198 
199  // get discrete function space
200  const auto& dfSpace = coeffs.space();
201 
202  auto localPhi = asImp().localFunctional();
203  LocalFunctionType localCoeffs(ACFem::asImp(coeffs));
204 
205  coeffs.clear();
206  auto end = dfSpace.end();
207  for (auto it = dfSpace.begin(); it != end; ++it) {
208  // get entity (here element)
209  const auto &entity = *it;
210 
211  localPhi.init(entity);
212  localCoeffs.init(entity);
213 
214  localPhi.coefficients(localCoeffs);
215  }
216  coeffs.communicate();
217  }
218 
219  const DiscreteFunctionSpaceType& space() const
220  {
221  return space_;
222  }
223 
224  LocalFunctionalType localFunctional()
225  {
226  return LocalFunctionalType(asImp());
227  }
228 
229  const LocalFunctionalType localFunctional() const
230  {
231  return LocalFunctionalType(asImp());
232  }
233 
234  LocalFunctionalType localFunctional(const EntityType& entity)
235  {
236  return LocalFunctionalType(entity, asImp());
237  }
238 
239  const LocalFunctionalType localFunctional(const EntityType& entity) const
240  {
241  return LocalFunctionalType(entity, asImp());
242  }
243 
244  std::string name() const
245  {
246  return "Functional";
247  }
248 
249  protected:
250  const DiscreteFunctionSpaceType& space_;
251  };
252 
254  template<class DiscreteFunctionSpace, class Traits>
256  : public Fem::BartonNackmanInterface<LocalLinearFunctional<DiscreteFunctionSpace, Traits>,
257  typename Traits::LocalFunctionalType>
258  {
260  typedef
261  Fem::BartonNackmanInterface<ThisType, typename Traits::LocalFunctionalType>
262  BaseType;
263  public:
264  typedef Traits TraitsType;
265  typedef typename TraitsType::GlobalFunctionalType FunctionalType;
267 
268  typedef DiscreteFunctionSpace DiscreteFunctionSpaceType;
269  typedef typename DiscreteFunctionSpaceType::EntityType EntityType;
270  typedef typename DiscreteFunctionSpace::FunctionSpaceType FunctionSpaceType;
271  typedef typename FunctionSpaceType::RangeFieldType FieldType;
272  typedef FieldType RangeType;
273  protected:
274  using BaseType::asImp;
275 
277  {}
278 
279  LocalLinearFunctional(const ThisType& other)
280  {}
281 
282  private:
283  ThisType& operator=(const ThisType& other) = delete;
284  ThisType& operator=(ThisType&& other) = delete;
285 
286  public:
288  void init(const EntityType& entity)
289  {
290  asImp().init(entity);
291  }
292 
293  // TODO: rather use
294  //
295  // template<Traits>
296  // void stuff(const LocalFunction<Traits>)
297  //
298  // in order to enforce the
299  // LocalFunctionInterface her.
300 
302  template<class LocalFunction>
303  RangeType operator()(const LocalFunction& arg) const
304  {
305  return asImp()(arg);
306  }
307 
315  template<class LocalFunction>
316  void coefficients(LocalFunction& coeffs) const
317  {
318  asImp().coefficients(coeffs);
319  }
320 
329  template<class LocalFunction>
330  void coefficients(const RangeType& c, LocalFunction& coeffs) const
331  {
332  asImp().coefficients(c, coeffs);
333  }
334 
337  const FunctionalType& functional() const
338  {
339  return asImp().functional();
340  }
341 
344  const EntityType& entity() const
345  {
346  return asImp().entity();
347  }
348  };
349 
359  template<class DiscreteFunctionSpace, class Traits>
361  : public LocalLinearFunctional<DiscreteFunctionSpace, Traits>
362  {
365  public:
366  typedef Traits TraitsType;
367  typedef typename TraitsType::GlobalFunctionalType FunctionalType;
368  typedef DiscreteFunctionSpace DiscreteFunctionSpaceType;
369  typedef typename DiscreteFunctionSpaceType::EntityType EntityType;
370  typedef typename DiscreteFunctionSpace::FunctionSpaceType FunctionSpaceType;
371  typedef typename FunctionSpaceType::RangeFieldType FieldType;
372  typedef FieldType RangeType;
373  protected:
374  typedef Fem::TemporaryLocalFunction<DiscreteFunctionSpaceType, FieldType> TemporaryCoefficientsType;
376  using BaseType::asImp;
377 
378  LocalLinearFunctionalDefault(const FunctionalType& functional)
379  : entity_(0),
380  functional_(functional),
381  localCoeffs_(functional_.space())
382  {}
383 
384  LocalLinearFunctionalDefault(const EntityType& entity,
385  const FunctionalType& functional)
386  : entity_(&entity),
387  functional_(functional),
388  localCoeffs_(functional_.space())
389  {}
390 
392  : entity_(other.entity_),
393  functional_(other.functional_),
394  localCoeffs_(functional_.space())
395  {}
396 
397  private:
398  ThisType& operator=(const ThisType& other) = delete;
399  ThisType& operator=(ThisType&& other) = delete;
400 
401  public:
403  void init(const EntityType& entity)
404  {
405  entity_ = &entity;
406  }
407 
409  template<class LocalFunction>
410  RangeType operator()(const LocalFunction& arg) const
411  {
412  typedef LocalFunction LocalDomainFunctionType;
413 
414  localCoeffs_.init(entity());
415  localCoeffs_.clear();
416  asImp().coefficients(FieldType(1.0), localCoeffs_);
417  RangeType result = 0;
418  for (unsigned i = 0; i < arg.size(); ++i) {
419  result += arg[i] * localCoeffs_[i];
420  }
421  return result;
422  }
423 
424  const FunctionalType& functional() const
425  {
426  return functional_;
427  }
428 
429  const EntityType& entity() const
430  {
431  return *entity_;
432  }
433 
435  template<class LocalFunction>
436  void coefficients(LocalFunction& coeffs) const
437  {
438  asImp().coefficients(FieldType(1.0), coeffs);
439  }
440 
442  template<class LocalFunction>
443  void coefficients(const RangeType& c, LocalFunction& coeffs) const
444  {
445  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION(asImp().coefficients(c, coeffs));
446  }
447  protected:
448  const EntityType* entity_;
449  const FunctionalType& functional_;
450  mutable TemporaryCoefficientsType localCoeffs_;
451  };
452 
454 
456 
457  } // namespace ACFem
458 
459 } // namespace Dune
460 
461 
462 
463 #endif // _DUNE_ACFEM_DISCRETELINEARFUNCTIONAL_HH_
Default implementation for linear functionals.
Definition: linearfunctional.hh:132
RangeType operator()(const Fem::DiscreteFunctionInterface< DFTraits > &arg) const
Definition: linearfunctional.hh:162
void coefficients(Fem::DiscreteFunctionInterface< DFTraits > &coeffs)
Compute the basis representation, which means: do the assembling stuff.
Definition: linearfunctional.hh:191
Interface class for a discrete linear functional.
Definition: linearfunctional.hh:52
RangeType operator()(const Fem::DiscreteFunctionInterface< DFImpl > &arg) const
Compute the value.
Definition: linearfunctional.hh:82
void coefficients(Fem::DiscreteFunctionInterface< DFImpl > &coeffs)
Compute the basis representation, which means: do the assembling stuff.
Definition: linearfunctional.hh:92
Default implementation for LocalLinearFunctional.
Definition: linearfunctional.hh:362
void coefficients(const RangeType &c, LocalFunction &coeffs) const
Compute the basis representation, which means: do the assembling stuff.
Definition: linearfunctional.hh:443
void coefficients(LocalFunction &coeffs) const
Compute the basis representation, which means: do the assembling stuff.
Definition: linearfunctional.hh:436
void init(const EntityType &entity)
Bind to an entity.
Definition: linearfunctional.hh:403
RangeType operator()(const LocalFunction &arg) const
Compute the value.
Definition: linearfunctional.hh:410
Interface or better: factory for local functionals.
Definition: linearfunctional.hh:258
void coefficients(LocalFunction &coeffs) const
Compute the basis representation, which means: do the assembling stuff.
Definition: linearfunctional.hh:316
RangeType operator()(const LocalFunction &arg) const
Compute the value of the functional.
Definition: linearfunctional.hh:303
void coefficients(const RangeType &c, LocalFunction &coeffs) const
Compute the basis representation, which means: do the assembling stuff.
Definition: linearfunctional.hh:330
const EntityType & entity() const
Return the entity the LocalLinearFunctional instance is tied to.
Definition: linearfunctional.hh:344
void init(const EntityType &entity)
Bind to an entity.
Definition: linearfunctional.hh:288
const FunctionalType & functional() const
Return the underlying global functional this LocalLinearFunctional instance belongs to.
Definition: linearfunctional.hh:337
const Implementation & asImp(const Fem::BartonNackmanInterface< Interface, Implementation > &arg)
Up-cast to the implementation for any Fem::BartonNackmanInterface.
Definition: expressionoperations.hh:71
Default traits were the local objects are not cached, but created on the fly.
Definition: linearfunctional.hh:38
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 10, 22:30, 2024)