dune-vtk 2.12
Loading...
Searching...
No Matches
structureddatacollector.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
3#pragma once
4
5#include <array>
6#include <vector>
7
10
12
13namespace Dune::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 using Super::gridView;
29
30 public:
33 , subDataCollector_(gridView)
34 {}
35
38 {
39 return this->asDerived().wholeExtentImpl();
40 }
41
44 {
45 return this->asDerived().extentImpl();
46 }
47
49 template <class Writer>
50 void writeLocalPiece (Writer const& writer) const
51 {
52 this->asDerived().writeLocalPieceImpl(writer);
53 }
54
56 template <class Writer>
57 void writePieces (Writer const& writer) const
58 {
59 this->asDerived().writePiecesImpl(writer);
60 }
61
64
67 {
68 return this->asDerived().originImpl();
69 }
70
73 {
74 return this->asDerived().spacingImpl();
75 }
76
78
81
83 template <class T>
85 {
86 return this->asDerived().template coordinatesImpl<T>();
87 }
88
90
91
92 public: // default implementation:
93
95 void updateImpl ()
96 {
97 subDataCollector_.update();
98
99 #if HAVE_MPI
100 int rank = gridView().comm().rank();
101 int numRanks = gridView().comm().size();
102
103 if (rank == 0) {
104 extents_.resize(numRanks);
105 requests_.resize(numRanks, MPI_REQUEST_NULL);
106 for (int i = 1; i < numRanks; ++i)
107 MPI_Irecv(extents_[i].data(), extents_[i].size(), MPI_INT, i, /*tag=*/6, gridView().comm(), &requests_[i]);
108 }
109
110 sendRequest_ = MPI_REQUEST_NULL;
111 #endif
112 }
113
116 {
117 auto extent = this->extent();
118 std::uint64_t num = 1;
119 for (int d = 0; d < dim; ++d)
120 num *= extent[2*d+1] - extent[2*d];
121 return num;
122 }
123
126 {
127 return subDataCollector_.numPoints();
128 }
129
131 template <class T>
133 {
134 return subDataCollector_.template points<T>();
135 }
136
138 template <class T, class GlobalFunction>
139 std::vector<T> pointDataImpl (GlobalFunction const& fct) const
140 {
141 return subDataCollector_.template pointData<T>(fct);
142 }
143
144 // Calculates the extent and communicates it to rank 0.
145 template <class Writer>
146 void writeLocalPieceImpl (Writer const& writer) const
147 {
148 auto&& extent = this->extent();
149
150 #if HAVE_MPI
151 int sendFlag = 0;
153 MPI_Test(&sendRequest_, &sendFlag, &sendStatus);
154
155 if (sendFlag) {
156 int rank = gridView().comm().rank();
157 if (rank != 0) {
158 MPI_Isend(extent.data(), extent.size(), MPI_INT, 0, /*tag=*/6, gridView().comm(), &sendRequest_);
159 } else {
160 extents_[0] = extent;
161 }
162 }
163 #endif
164
165 writer(extent);
166 }
167
168 // Receive extent from all ranks and call the `writer` with the rank's extent vector
169 template <class Writer>
170 void writePiecesImpl (Writer const& writer) const
171 {
172 #if HAVE_MPI
173 writer(0, extents_[0], true);
174
175 int numRanks = gridView().comm().size();
176 for (int p = 1; p < numRanks; ++p) {
177 int idx = -1;
178 MPI_Status status;
179 MPI_Waitany(numRanks, requests_.data(), &idx, &status);
180 if (idx != MPI_UNDEFINED) {
181 assert(idx == status.MPI_SOURCE && status.MPI_TAG == 6);
182 writer(idx, extents_[idx], true);
183 }
184 }
185 #else
186 writer(0, this->extent(), true);
187 #endif
188 }
189
190 // Origin (0,0,0)
192 {
194 return vec;
195 }
196
197 // Grid spacing (0,0,0)
199 {
201 return vec;
202 }
203
204 // Ordinate along each axis with constant \ref spacing from the \ref origin
205 template <class T>
207 {
208 auto origin = this->origin();
209 auto spacing = this->spacing();
210 auto extent = this->extent();
211
213 for (int d = 0; d < dim; ++d) {
214 auto s = extent[2*d+1] - extent[2*d] + 1;
215 ordinates[d].resize(s);
216 for (int i = 0; i < s; ++i)
217 ordinates[d][i] = origin[d] + (extent[2*d] + i)*spacing[d];
218 }
219
220 for (int d = dim; d < 3; ++d)
221 ordinates[d].resize(1, T(0));
222
223 return ordinates;
224 }
225
226 private:
227 SubDataCollector subDataCollector_;
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 Dune::Vtk
Definition datacollector.hh:19
typename Impl::StructuredDataCollectorImpl< GridView, typename GridView::Grid >::type StructuredDataCollector
Definition structureddatacollector.hh:245
int size() const
Base class for data collectors in a CRTP style.
Definition datacollectorinterface.hh:20
void update()
Update the DataCollector on the current GridView.
Definition datacollectorinterface.hh:47
auto comm() const
Definition datacollectorinterface.hh:112
std::uint64_t numPoints() const
Return the number of points in (this partition of the) grid.
Definition datacollectorinterface.hh:65
@ dim
Definition datacollectorinterface.hh:29
static constexpr auto partition
The partitionset to collect data from.
Definition datacollectorinterface.hh:26
GridView GridView
Type of the bound grid view.
Definition datacollectorinterface.hh:23
Derived & asDerived()
Definition datacollectorinterface.hh:119
GridView const & gridView() const
Return the bound grid view.
Definition datacollectorinterface.hh:41
Implementation of DataCollector for cells, with continuous data.
Definition celltypedatacollector.hh:20
The Interface for structured data-collectors.
Definition structureddatacollector.hh:19
std::uint64_t numCellsImpl() const
Return number of grid cells.
Definition structureddatacollector.hh:115
std::array< std::vector< T >, 3 > coordinatesImpl() const
Definition structureddatacollector.hh:206
void writePieces(Writer const &writer) const
Call the writer with piece number and piece extent.
Definition structureddatacollector.hh:57
FieldVector< ctype, 3 > originImpl() const
Definition structureddatacollector.hh:191
typename GridView::ctype ctype
Definition structureddatacollector.hh:23
ContinuousDataCollector< GridView > SubDataCollector
Definition structureddatacollector.hh:22
void writeLocalPiece(Writer const &writer) const
Call the writer with extent.
Definition structureddatacollector.hh:50
void writePiecesImpl(Writer const &writer) const
Definition structureddatacollector.hh:170
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:43
std::array< int, 6 > wholeExtent() const
Sequence of Index pairs [begin, end) for the cells in each direction.
Definition structureddatacollector.hh:37
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:84
std::vector< T > pointDataImpl(GlobalFunction const &fct) const
Definition structureddatacollector.hh:139
FieldVector< ctype, 3 > spacingImpl() const
Definition structureddatacollector.hh:198
void updateImpl()
\copyref DefaultDataCollector::update.
Definition structureddatacollector.hh:95
std::vector< T > pointsImpl() const
Definition structureddatacollector.hh:132
FieldVector< ctype, 3 > origin() const
Lower left corner of the grid.
Definition structureddatacollector.hh:66
StructuredDataCollectorInterface(GridView const &gridView)
Definition structureddatacollector.hh:31
std::uint64_t numPointsImpl() const
Return number of grid vertices.
Definition structureddatacollector.hh:125
void writeLocalPieceImpl(Writer const &writer) const
Definition structureddatacollector.hh:146
FieldVector< ctype, 3 > spacing() const
Constant grid spacing in each coordinate direction.
Definition structureddatacollector.hh:72
T data(T... args)
T resize(T... args)
T size(T... args)