dune-vtk  0.2
continuousgridfunction.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 
5 #include <dune/common/dynvector.hh>
6 #include <dune/localfunctions/lagrange/lagrangelfecache.hh>
8 
9 namespace Dune
10 {
11  namespace Vtk
12  {
14  template <class GridType, class FieldType, class Context>
16  {
17  using Grid = GridType;
18  using Field = FieldType;
19 
20  using Factory = GridFactory<Grid>;
21 
22  public:
23  struct EntitySet
24  {
25  using Grid = GridType;
26  using Element = typename GridType::template Codim<0>::Entity;
27  using LocalCoordinate = typename Element::Geometry::LocalCoordinate;
28  using GlobalCoordinate = typename Element::Geometry::GlobalCoordinate;
29  };
30 
32  using Range = DynamicVector<Field>;
34 
35  private:
36  template <class LC>
37  class PointDataLocalFunction
38  {
39  // NOTE: local finite-element fixed to Lagrange P1/Q1
40  using LFECache = LagrangeLocalFiniteElementCache<Field,Field,LC::mydimension,1>;
41  using LFE = typename LFECache::FiniteElementType;
42  using LB = typename LFE::Traits::LocalBasisType;
43 
44  public:
45  using LocalContext = LC;
46  using Domain = typename LC::Geometry::LocalCoordinate;
47  using Range = DynamicVector<Field>;
48  using Signature = Range(Domain);
49 
50  public:
51  PointDataLocalFunction (Factory const& factory, std::vector<Field> const& values, unsigned int comp)
52  : factory_(factory)
53  , values_(values)
54  , comp_(comp)
55  {}
56 
57  void bind (LocalContext const& element)
58  {
59  lfe_ = &cache_.get(element.type());
60 
61  // collect values on vertices
62  // NOTE: assumes, that Lagrange nodes are ordered like element vertices
63  localValues_.resize(element.subEntities(Grid::dimension));
64  for (unsigned int i = 0; i < element.subEntities(Grid::dimension); ++i) {
65  unsigned int idx = factory_.insertionIndex(element.template subEntity<Grid::dimension>(i));
66  DynamicVector<Field>& v = localValues_[i];
67  v.resize(comp_);
68  for (unsigned int j = 0; j < comp_; ++j)
69  v[j] = values_[comp_*idx + j];
70  }
71  }
72 
73  void unbind ()
74  {
75  lfe_ = nullptr;
76  }
77 
78  Range operator() (Domain const& local) const
79  {
80  assert(!!lfe_);
81  auto const& lb = lfe_->localBasis();
82  lb.evaluateFunction(local, shapeValues_);
83  assert(shapeValues_.size() == localValues_.size());
84 
85  Range y(comp_, Field(0));
86  for (std::size_t i = 0; i < shapeValues_.size(); ++i)
87  y.axpy(shapeValues_[i], localValues_[i]);
88 
89  return y;
90  }
91 
92  private:
93  Factory const& factory_;
94  std::vector<Field> const& values_;
95  unsigned int comp_;
96 
97  // Local Finite-Element
98  LFECache cache_;
99  LFE const* lfe_ = nullptr;
100 
101  // cache of local values
102  std::vector<DynamicVector<Field>> localValues_;
103  mutable std::vector<typename LB::Traits::RangeType> shapeValues_;
104  };
105 
106  template <class LC>
107  class CellDataLocalFunction
108  {
109  public:
110  using LocalContext = LC;
111  using Domain = typename LC::Geometry::LocalCoordinate;
112  using Range = DynamicVector<Field>;
113  using Signature = Range(Domain);
114 
115  public:
116  CellDataLocalFunction (Factory const& factory, std::vector<Field> const& values, unsigned int comp)
117  : factory_(factory)
118  , values_(values)
119  , comp_(comp)
120  {}
121 
122  void bind (LocalContext const& element)
123  {
124  unsigned int idx = factory_.insertionIndex(element);
125 
126  // collect values on cells
127  DynamicVector<Field>& v = localValue_;
128  v.resize(comp_);
129 
130  for (unsigned int j = 0; j < comp_; ++j)
131  v[j] = values_[comp_*idx + j];
132  }
133 
134  void unbind ()
135  {}
136 
137  Range operator() (Domain const& local) const
138  {
139  return localValue_;
140  }
141 
142  private:
143  Factory const& factory_;
144  std::vector<Field> const& values_;
145  unsigned int comp_;
146 
147  // cache of local values
148  DynamicVector<Field> localValue_;
149  };
150 
151  template <class LC>
152  using LocalFunction = std::conditional_t< std::is_same_v<Context,PointContext>,
153  PointDataLocalFunction<LC>,
154  CellDataLocalFunction<LC>>;
155 
156  public:
157  template <class GridCreator>
158  ContinuousGridFunction (GridCreator const& creator, std::vector<Field> const& values, unsigned int comp,
159  std::vector<std::uint8_t> const& /*types*/,
160  std::vector<std::int64_t> const& /*offsets*/,
161  std::vector<std::int64_t> const& /*connectivity*/)
162  : factory_(creator.factory())
163  , values_(values)
164  , comp_(comp)
165  {}
166 
167  Range operator() (Domain const& global) const
168  {
169  DUNE_THROW(Dune::NotImplemented, "Evaluation in global coordinates not implemented.");
170  return Range(comp_, 0);
171  }
172 
173  EntitySet const& entitySet () const
174  {
175  return entitySet_;
176  }
177 
178  friend LocalFunction<typename EntitySet::Element> localFunction (ContinuousGridFunction const& gf)
179  {
180  return {gf.factory_, gf.values_, gf.comp_};
181  }
182 
183  private:
184  Factory const& factory_;
185  std::vector<Field> const& values_;
186  unsigned int comp_;
187 
188  EntitySet entitySet_;
189  };
190 
191  } // end namespace Vtk
192 } // end namespace Dune
Definition: datacollectorinterface.hh:9
A GridFunction representing data stored on the grid vertices in a continuous manner.
Definition: continuousgridfunction.hh:16
ContinuousGridFunction(GridCreator const &creator, std::vector< Field > const &values, unsigned int comp, std::vector< std::uint8_t > const &, std::vector< std::int64_t > const &, std::vector< std::int64_t > const &)
Definition: continuousgridfunction.hh:158
Range(Domain) Signature
Definition: continuousgridfunction.hh:33
DynamicVector< Field > Range
Definition: continuousgridfunction.hh:32
Range operator()(Domain const &global) const
Definition: continuousgridfunction.hh:167
friend LocalFunction< typename EntitySet::Element > localFunction(ContinuousGridFunction const &gf)
Definition: continuousgridfunction.hh:178
EntitySet const & entitySet() const
Definition: continuousgridfunction.hh:173
typename EntitySet::GlobalCoordinate Domain
Definition: continuousgridfunction.hh:31
Definition: continuousgridfunction.hh:24
typename Element::Geometry::GlobalCoordinate GlobalCoordinate
Definition: continuousgridfunction.hh:28
GridType Grid
Definition: continuousgridfunction.hh:25
typename Element::Geometry::LocalCoordinate LocalCoordinate
Definition: continuousgridfunction.hh:27
typename GridType::template Codim< 0 >::Entity Element
Definition: continuousgridfunction.hh:26