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  PointDataLocalFunction () = default;
58 
59  void bind (LocalContext const& element)
60  {
61  lfe_ = &cache_.get(element.type());
62 
63  // collect values on vertices
64  // NOTE: assumes, that Lagrange nodes are ordered like element vertices
65  localValues_.resize(element.subEntities(Grid::dimension));
66  for (unsigned int i = 0; i < element.subEntities(Grid::dimension); ++i) {
67  unsigned int idx = factory_->insertionIndex(element.template subEntity<Grid::dimension>(i));
68  DynamicVector<Field>& v = localValues_[i];
69  v.resize(comp_);
70  for (unsigned int j = 0; j < comp_; ++j)
71  v[j] = (*values_)[comp_*idx + j];
72  }
73  }
74 
75  void unbind ()
76  {
77  lfe_ = nullptr;
78  }
79 
80  Range operator() (Domain const& local) const
81  {
82  assert(!!lfe_);
83  auto const& lb = lfe_->localBasis();
84  lb.evaluateFunction(local, shapeValues_);
85  assert(shapeValues_.size() == localValues_.size());
86 
87  Range y(comp_, Field(0));
88  for (std::size_t i = 0; i < shapeValues_.size(); ++i)
89  y.axpy(shapeValues_[i], localValues_[i]);
90 
91  return y;
92  }
93 
94  private:
95  Factory const* factory_ = nullptr;
96  std::vector<Field> const* values_ = nullptr;
97  unsigned int comp_;
98 
99  // Local Finite-Element
100  LFECache cache_;
101  LFE const* lfe_ = nullptr;
102 
103  // cache of local values
104  std::vector<DynamicVector<Field>> localValues_;
105  mutable std::vector<typename LB::Traits::RangeType> shapeValues_;
106  };
107 
108  template <class LC>
109  class CellDataLocalFunction
110  {
111  public:
112  using LocalContext = LC;
113  using Domain = typename LC::Geometry::LocalCoordinate;
114  using Range = DynamicVector<Field>;
115  using Signature = Range(Domain);
116 
117  public:
118  CellDataLocalFunction (Factory const* factory, std::vector<Field> const* values, unsigned int comp)
119  : factory_(factory)
120  , values_(values)
121  , comp_(comp)
122  {}
123 
124  CellDataLocalFunction () = default;
125 
126  void bind (LocalContext const& element)
127  {
128  unsigned int idx = factory_->insertionIndex(element);
129 
130  // collect values on cells
131  DynamicVector<Field>& v = localValue_;
132  v.resize(comp_);
133 
134  for (unsigned int j = 0; j < comp_; ++j)
135  v[j] = (*values_)[comp_*idx + j];
136  }
137 
138  void unbind ()
139  {}
140 
141  Range operator() (Domain const& local) const
142  {
143  return localValue_;
144  }
145 
146  private:
147  Factory const* factory_ = nullptr;
148  std::vector<Field> const* values_ = nullptr;
149  unsigned int comp_;
150 
151  // cache of local values
152  DynamicVector<Field> localValue_;
153  };
154 
155  template <class LC>
156  using LocalFunction = std::conditional_t< std::is_same_v<Context,PointContext>,
157  PointDataLocalFunction<LC>,
158  CellDataLocalFunction<LC>>;
159 
160  public:
161  template <class GridCreator>
162  ContinuousGridFunction (GridCreator const& creator, std::vector<Field> const& values,
163  std::string name, unsigned int ncomps, Vtk::DataTypes dataType,
164  std::vector<std::uint8_t> const& /*types*/,
165  std::vector<std::int64_t> const& /*offsets*/,
166  std::vector<std::int64_t> const& /*connectivity*/)
167  : factory_(&creator.factory())
168  , values_(&values)
169  , name_(std::move(name))
170  , ncomps_(ncomps)
171  , dataType_(dataType)
172  {}
173 
175 
176  Range operator() (Domain const& global) const
177  {
178  DUNE_THROW(Dune::NotImplemented, "Evaluation in global coordinates not implemented.");
179  return Range(ncomps_, 0);
180  }
181 
182  EntitySet const& entitySet () const
183  {
184  return entitySet_;
185  }
186 
187  std::string const& name () const
188  {
189  return name_;
190  }
191 
192  int numComponents () const
193  {
194  return ncomps_;
195  }
196 
198  {
199  return dataType_;
200  }
201 
202  friend LocalFunction<typename EntitySet::Element> localFunction (ContinuousGridFunction const& gf)
203  {
204  return {gf.factory_, gf.values_, gf.ncomps_};
205  }
206 
207  private:
208  Factory const* factory_;
209  std::vector<Field> const* values_ = nullptr;
210  std::string name_ = "GridFunction";
211  unsigned int ncomps_ = 0;
213 
214  EntitySet entitySet_;
215  };
216 
217  } // end namespace Vtk
218 } // end namespace Dune
Definition: writer.hh:13
DataTypes
Definition: types.hh:52
A GridFunction representing data stored on the grid vertices in a continuous manner.
Definition: continuousgridfunction.hh:16
int numComponents() const
Definition: continuousgridfunction.hh:192
Vtk::DataTypes dataType() const
Definition: continuousgridfunction.hh:197
Range(Domain) Signature
Definition: continuousgridfunction.hh:33
DynamicVector< Field > Range
Definition: continuousgridfunction.hh:32
ContinuousGridFunction(GridCreator const &creator, std::vector< Field > const &values, std::string name, unsigned int ncomps, Vtk::DataTypes dataType, std::vector< std::uint8_t > const &, std::vector< std::int64_t > const &, std::vector< std::int64_t > const &)
Definition: continuousgridfunction.hh:162
Range operator()(Domain const &global) const
Definition: continuousgridfunction.hh:176
friend LocalFunction< typename EntitySet::Element > localFunction(ContinuousGridFunction const &gf)
Definition: continuousgridfunction.hh:202
EntitySet const & entitySet() const
Definition: continuousgridfunction.hh:182
typename EntitySet::GlobalCoordinate Domain
Definition: continuousgridfunction.hh:31
std::string const & name() const
Definition: continuousgridfunction.hh:187
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