dune-vtk  0.2
structureddatacollector.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
4 #include <vector>
5 
6 #include <dune/common/fvector.hh>
8 
10 
11 namespace Dune
12 {
13  namespace Vtk
14  {
16  template <class GridView, class Derived>
18  : public DataCollectorInterface<GridView, Derived>
19  {
20  protected:
23  using ctype = typename GridView::ctype;
24 
25  public:
26  using Super::dim;
27  using Super::partition;
28 
29  public:
31  : Super(gridView)
32  , subDataCollector_(gridView)
33  {}
34 
36  std::array<int, 6> wholeExtent () const
37  {
38  return this->asDerived().wholeExtentImpl();
39  }
40 
42  std::array<int, 6> extent () const
43  {
44  return this->asDerived().extentImpl();
45  }
46 
48  template <class Writer>
49  void writeLocalPiece (Writer const& writer) const
50  {
51  this->asDerived().writeLocalPieceImpl(writer);
52  }
53 
55  template <class Writer>
56  void writePieces (Writer const& writer) const
57  {
58  this->asDerived().writePiecesImpl(writer);
59  }
60 
63 
66  {
67  return this->asDerived().originImpl();
68  }
69 
72  {
73  return this->asDerived().spacingImpl();
74  }
75 
77 
80 
82  template <class T>
83  std::array<std::vector<T>, 3> coordinates () const
84  {
85  return this->asDerived().template coordinatesImpl<T>();
86  }
87 
89 
90 
91  public: // default implementation:
92 
94  void updateImpl ()
95  {
97 
98  #if HAVE_MPI
99  int rank = gridView_.comm().rank();
100  int numRanks = gridView_.comm().size();
101 
102  if (rank == 0) {
103  extents_.resize(numRanks);
104  requests_.resize(numRanks, MPI_REQUEST_NULL);
105  for (int i = 1; i < numRanks; ++i)
106  MPI_Irecv(extents_[i].data(), extents_[i].size(), MPI_INT, i, /*tag=*/6, gridView_.comm(), &requests_[i]);
107  }
108 
109  sendRequest_ = MPI_REQUEST_NULL;
110  #endif
111  }
112 
114  std::uint64_t numCellsImpl () const
115  {
116  auto extent = this->extent();
117  std::uint64_t num = 1;
118  for (int d = 0; d < dim; ++d)
119  num *= extent[2*d+1] - extent[2*d];
120  return num;
121  }
122 
124  std::uint64_t numPointsImpl () const
125  {
126  return subDataCollector_.numPoints();
127  }
128 
130  template <class T>
131  std::vector<T> pointsImpl () const
132  {
133  return subDataCollector_.template points<T>();
134  }
135 
137  template <class T, class GlobalFunction>
138  std::vector<T> pointDataImpl (GlobalFunction const& fct) const
139  {
140  return subDataCollector_.template pointData<T>(fct);
141  }
142 
143  // Calculates the extent and communicates it to rank 0.
144  template <class Writer>
145  void writeLocalPieceImpl (Writer const& writer) const
146  {
147  auto&& extent = this->extent();
148 
149  #if HAVE_MPI
150  int sendFlag = 0;
151  MPI_Status sendStatus;
152  MPI_Test(&sendRequest_, &sendFlag, &sendStatus);
153 
154  if (sendFlag) {
155  int rank = gridView_.comm().rank();
156  if (rank != 0) {
157  MPI_Isend(extent.data(), extent.size(), MPI_INT, 0, /*tag=*/6, gridView_.comm(), &sendRequest_);
158  } else {
159  extents_[0] = extent;
160  }
161  }
162  #endif
163 
164  writer(extent);
165  }
166 
167  // Receive extent from all ranks and call the `writer` with the rank's extent vector
168  template <class Writer>
169  void writePiecesImpl (Writer const& writer) const
170  {
171  #if HAVE_MPI
172  writer(0, extents_[0], true);
173 
174  int numRanks = gridView_.comm().size();
175  for (int p = 1; p < numRanks; ++p) {
176  int idx = -1;
177  MPI_Status status;
178  MPI_Waitany(numRanks, requests_.data(), &idx, &status);
179  if (idx != MPI_UNDEFINED) {
180  assert(idx == status.MPI_SOURCE && status.MPI_TAG == 6);
181  writer(idx, extents_[idx], true);
182  }
183  }
184  #else
185  writer(0, this->extent(), true);
186  #endif
187  }
188 
189  // Origin (0,0,0)
191  {
192  FieldVector<ctype, 3> vec; vec = ctype(0);
193  return vec;
194  }
195 
196  // Grid spacing (0,0,0)
198  {
199  FieldVector<ctype, 3> vec; vec = ctype(0);
200  return vec;
201  }
202 
203  // Ordinate along each axis with constant \ref spacing from the \ref origin
204  template <class T>
205  std::array<std::vector<T>, 3> coordinatesImpl () const
206  {
207  auto origin = this->origin();
208  auto spacing = this->spacing();
209  auto extent = this->extent();
210 
211  std::array<std::vector<T>, 3> ordinates{};
212  for (int d = 0; d < dim; ++d) {
213  auto s = extent[2*d+1] - extent[2*d] + 1;
214  ordinates[d].resize(s);
215  for (int i = 0; i < s; ++i)
216  ordinates[d][i] = origin[d] + (extent[2*d] + i)*spacing[d];
217  }
218 
219  for (int d = dim; d < 3; ++d)
220  ordinates[d].resize(1, T(0));
221 
222  return ordinates;
223  }
224 
225  protected:
226  using Super::gridView_;
228 
229  #if HAVE_MPI
230  mutable std::vector<std::array<int,6>> extents_;
231  mutable std::vector<MPI_Request> requests_;
232  mutable MPI_Request sendRequest_ = MPI_REQUEST_NULL;
233  #endif
234  };
235 
236  namespace Impl
237  {
238  // Should be specialized for concrete structured grid
239  template <class GridView, class Grid>
240  struct StructuredDataCollectorImpl;
241  }
242 
243  template <class GridView>
245  = typename Impl::StructuredDataCollectorImpl<GridView, typename GridView::Grid>::type;
246 
247  } // end namespace Vtk
248 } // end namespace Dune
Definition: writer.hh:13
typename Impl::StructuredDataCollectorImpl< GridView, typename GridView::Grid >::type StructuredDataCollector
Definition: structureddatacollector.hh:245
Base class for data collectors in a CRTP style.
Definition: datacollectorinterface.hh:20
std::uint64_t numPoints() const
Return the number of points in (this partition of the) grid.
Definition: datacollectorinterface.hh:58
static constexpr auto partition
The partitionset to collect data from.
Definition: datacollectorinterface.hh:23
GridView GridView
Definition: datacollectorinterface.hh:25
@ dim
Definition: datacollectorinterface.hh:28
Derived & asDerived()
Definition: datacollectorinterface.hh:106
GridView gridView_
Definition: datacollectorinterface.hh:133
void update()
Update the DataCollector on the current GridView.
Definition: datacollectorinterface.hh:40
Implementation of DataCollector for linear cells, with continuous data.
Definition: continuousdatacollector.hh:21
The Interface for structured data-collectors.
Definition: structureddatacollector.hh:19
std::uint64_t numCellsImpl() const
Return number of grid cells.
Definition: structureddatacollector.hh:114
FieldVector< ctype, 3 > origin() const
Lower left corner of the grid.
Definition: structureddatacollector.hh:65
void writePieces(Writer const &writer) const
Call the writer with piece number and piece extent.
Definition: structureddatacollector.hh:56
MPI_Request sendRequest_
Definition: structureddatacollector.hh:232
std::array< std::vector< T >, 3 > coordinatesImpl() const
Definition: structureddatacollector.hh:205
typename GridView::ctype ctype
Definition: structureddatacollector.hh:23
std::vector< std::array< int, 6 > > extents_
Definition: structureddatacollector.hh:230
void writeLocalPiece(Writer const &writer) const
Call the writer with extent.
Definition: structureddatacollector.hh:49
void writePiecesImpl(Writer const &writer) const
Definition: structureddatacollector.hh:169
std::array< int, 6 > extent() const
Sequence of Index pairs [begin, end) for the cells in each direction of the local partition.
Definition: structureddatacollector.hh:42
std::vector< T > pointsImpl() const
Definition: structureddatacollector.hh:131
FieldVector< ctype, 3 > originImpl() const
Definition: structureddatacollector.hh:190
SubDataCollector subDataCollector_
Definition: structureddatacollector.hh:227
void updateImpl()
\copyref DefaultDataCollector::update.
Definition: structureddatacollector.hh:94
StructuredDataCollectorInterface(GridView const &gridView)
Definition: structureddatacollector.hh:30
std::uint64_t numPointsImpl() const
Return number of grid vertices.
Definition: structureddatacollector.hh:124
std::array< int, 6 > wholeExtent() const
Sequence of Index pairs [begin, end) for the cells in each direction.
Definition: structureddatacollector.hh:36
std::array< std::vector< T >, 3 > coordinates() const
The coordinates defines point coordinates for an extent by specifying the ordinate along each axis.
Definition: structureddatacollector.hh:83
std::vector< T > pointDataImpl(GlobalFunction const &fct) const
Definition: structureddatacollector.hh:138
FieldVector< ctype, 3 > spacing() const
Constant grid spacing in each coordinate direction.
Definition: structureddatacollector.hh:71
void writeLocalPieceImpl(Writer const &writer) const
Definition: structureddatacollector.hh:145
FieldVector< ctype, 3 > spacingImpl() const
Definition: structureddatacollector.hh:197
std::vector< MPI_Request > requests_
Definition: structureddatacollector.hh:231