12#ifndef DUMUX_VTK_GRID_DATA_HANDLE_HH
13#define DUMUX_VTK_GRID_DATA_HANDLE_HH
20#include <dune/common/parallel/communication.hh>
21#include <dune/geometry/dimension.hh>
22#include <dune/grid/common/partitionset.hh>
23#include <dune/grid/common/datahandleif.hh>
31template<
class Gr
id,
class Gr
idFactory,
class Data>
33:
public Dune::CommDataHandleIF<VtkGridDataHandle<Grid, GridFactory, Data>, typename Data::value_type>
38 : gridView_(grid.levelGridView(0))
39 , idSet_(grid.localIdSet())
40 , userCellData_(cellData)
41 , userPointData_(pointData)
45 for (
const auto& [key, data] : userCellData_)
46 cellData_[key] = std::move(userCellData_[key]);
47 for (
const auto& [key, data] : userPointData_)
48 pointData_[key] = std::move(userPointData_[key]);
52 std::array<std::size_t, 2> numKeys{{ cellData_.size(), pointData_.size() }};
53 Dune::MPIHelper::getCommunication().broadcast(numKeys.data(), 2, 0);
57 std::vector<std::size_t> keyLengthAndComponents(2*(numKeys[0] + numKeys[1]), 0);
62 for (
const auto& [key, data] : cellData_)
63 keyLengthAndComponents[n++] = key.size();
64 for (
const auto& [key, data] : pointData_)
65 keyLengthAndComponents[n++] = key.size();
68 for (
const auto& [key, data] : cellData_)
69 keyLengthAndComponents[n++] = gridView_.size(0) > 0 ? data.size()/gridView_.size(0) : 0;
70 for (
const auto& [key, data] : pointData_)
71 keyLengthAndComponents[n++] = gridView_.size(Grid::dimension) > 0 ? data.size()/gridView_.size(Grid::dimension) : 0;
74 assert((Dune::MPIHelper::instance().rank() == 0) == (n == keyLengthAndComponents.size()));
76 Dune::MPIHelper::getCommunication().broadcast(keyLengthAndComponents.data(), keyLengthAndComponents.size(), 0);
80 const auto begin = keyLengthAndComponents.begin() + numKeys[0] + numKeys[1];
81 cellDataComponents_.assign(begin, begin + numKeys[0]);
82 pointDataComponents_.assign(begin + numKeys[0], keyLengthAndComponents.end());
83 numCellDataPerElement_ = std::accumulate(cellDataComponents_.begin(), cellDataComponents_.end(), 0UL);
84 numPointDataPerVertex_ = std::accumulate(pointDataComponents_.begin(), pointDataComponents_.end(), 0UL);
87 std::string keys; keys.resize(std::accumulate(keyLengthAndComponents.begin(), begin, 0UL));
90 for (
const auto& [key, data] : cellData_)
91 for (
const auto& c : key)
93 for (
const auto& [key, data] : pointData_)
94 for (
const auto& c : key)
98 assert((Dune::MPIHelper::instance().rank() == 0) == (n == keys.size()));
100 Dune::MPIHelper::getCommunication().broadcast(keys.data(), keys.size(), 0);
104 std::size_t offset = 0;
105 for (
int keyIdx = 0; keyIdx < numKeys[0]; ++keyIdx)
107 if (std::string key{ keys, offset, keyLengthAndComponents[keyIdx] }; cellData_.count(key) == 0)
108 cellData_[key] = Data{};
110 offset += keyLengthAndComponents[keyIdx];
112 for (
int keyIdx = numKeys[0]; keyIdx < numKeys[0] + numKeys[1]; ++keyIdx)
114 if (std::string key{ keys, offset, keyLengthAndComponents[keyIdx] }; pointData_.count(key) == 0)
115 pointData_[key] = Data{};
117 offset += keyLengthAndComponents[keyIdx];
121 for (
const auto& element : elements(gridView_, Dune::Partitions::interior))
123 data_[idSet_.id(element)].resize(numCellDataPerElement_);
126 for (
const auto& [key, data] : cellData_)
128 const auto nComp = cellDataComponents_[l++];
129 for (
int k = 0; k < nComp; ++k)
130 std::swap(cellData_[key][k + nComp*gridFactory.insertionIndex(element)], data_[idSet_.id(element)][n++]);
133 assert(n == numCellDataPerElement_);
136 for (
const auto& vertex : vertices(gridView_))
138 data_[idSet_.id(vertex)].resize(numPointDataPerVertex_);
141 for (
const auto& [key, data] : pointData_)
143 const auto nComp = pointDataComponents_[l++];
144 for (
int k = 0; k < nComp; ++k)
145 std::swap(pointData_[key][k + nComp*gridFactory.insertionIndex(vertex)], data_[idSet_.id(vertex)][n++]);
148 assert(n == numPointDataPerVertex_);
155 const auto& indexSet = gridView_.indexSet();
159 for (
const auto& [key, data] : cellData_)
160 cellData_[key].resize(indexSet.size(0)*cellDataComponents_[n++]);
164 for (
const auto& [key, data] : pointData_)
165 pointData_[key].resize(indexSet.size(GridView::dimension)*pointDataComponents_[n++]);
168 for (
const auto& element : elements(gridView_))
171 for (
const auto& [key, data] : cellData_)
173 const auto nComp = cellDataComponents_[l++];
174 for (
int k = 0; k < nComp; ++k)
175 std::swap(cellData_[key][k + nComp*indexSet.index(element)], data_[idSet_.id(element)][n++]);
179 for (
const auto& vertex : vertices(gridView_))
182 for (
const auto& [key, data] : pointData_)
184 const auto nComp = pointDataComponents_[l++];
185 for (
int k = 0; k < nComp; ++k)
186 std::swap(pointData_[key][k + nComp*indexSet.index(vertex)], data_[idSet_.id(vertex)][n++]);
191 for (
const auto& [key, data] : cellData_)
192 userCellData_[key] = std::move(cellData_[key]);
193 for (
const auto& [key, data] : pointData_)
194 userPointData_[key] = std::move(pointData_[key]);
197 Dune::CommDataHandleIF<VtkGridDataHandle<Grid, GridFactory, Data>,
typename Data::value_type>&
interface()
201 {
return codim == 0 || codim == dim; }
207 template<
class Entity>
208 std::size_t
size (
const Entity&)
const
210 if constexpr (Entity::codimension == 0)
211 return numCellDataPerElement_;
212 else if constexpr (Entity::codimension == GridView::dimension)
213 return numPointDataPerVertex_;
218 template<
class MessageBufferImp,
class Entity>
219 void gather (MessageBufferImp& buff,
const Entity& e)
const
221 if constexpr (Entity::codimension == 0)
223 const auto& data = data_[idSet_.id(e)];
224 for (
int n = 0; n < numCellDataPerElement_; ++n)
228 if constexpr (Entity::codimension == GridView::dimension)
230 const auto& data = data_[idSet_.id(e)];
231 for (
int n = 0; n < numPointDataPerVertex_; ++n)
236 template<
class MessageBufferImp,
class Entity>
237 void scatter (MessageBufferImp& buff,
const Entity& e, std::size_t n)
239 auto& data = data_[idSet_.id(e)];
242 if constexpr (Entity::codimension == 0)
243 for (
int k = 0; k < numCellDataPerElement_; ++k)
246 if constexpr (Entity::codimension == GridView::dimension)
247 for (
int k = 0; k < numPointDataPerVertex_; ++k)
252 using IdSet =
typename Grid::LocalIdSet;
260 std::map<std::string, VTKReader::Data::mapped_type> cellData_;
261 std::map<std::string, VTKReader::Data::mapped_type> pointData_;
263 std::vector<std::size_t> cellDataComponents_;
264 std::vector<std::size_t> pointDataComponents_;
266 std::size_t numCellDataPerElement_;
267 std::size_t numPointDataPerVertex_;
269 mutable std::map< typename IdSet::IdType, std::vector<typename Data::value_type> > data_;
std::unordered_map< std::string, std::vector< double > > Data
the cell / point data type for point data read from a grid file
Definition: vtkreader.hh:48
A data handle for communicating grid data for VTK grids.
Definition: vtkgriddatahandle.hh:34
void scatter(MessageBufferImp &buff, const Entity &e, std::size_t n)
Definition: vtkgriddatahandle.hh:237
void gather(MessageBufferImp &buff, const Entity &e) const
Definition: vtkgriddatahandle.hh:219
bool contains(int dim, int codim) const
Definition: vtkgriddatahandle.hh:200
bool fixedSize(int dim, int codim) const
returns true if size per entity of given dim and codim is a constant
Definition: vtkgriddatahandle.hh:204
~VtkGridDataHandle()
Definition: vtkgriddatahandle.hh:152
Dune::CommDataHandleIF< VtkGridDataHandle< Grid, GridFactory, Data >, typename Data::value_type > & interface()
Definition: vtkgriddatahandle.hh:197
std::size_t size(const Entity &) const
Definition: vtkgriddatahandle.hh:208
VtkGridDataHandle(const Grid &grid, const GridFactory &gridFactory, VTKReader::Data &cellData, VTKReader::Data &pointData)
Definition: vtkgriddatahandle.hh:37
typename Grid::LevelGridView GridView
Definition: vtkgriddatahandle.hh:35