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