Commit 6411139a authored by Praetorius, Simon's avatar Praetorius, Simon
Browse files

cleanup of datacollectors and some examples

parent f7590a3d
......@@ -3,14 +3,20 @@ dune_add_library("vtktypes" OBJECT
#install headers
install(FILES
defaultvtkfunction.hh
filereader.hh
filewriter.hh
gridcreator.hh
legacyvtkfunction.hh
vtkfunction.hh
vtklocalfunction.hh
vtklocalfunctioninterface.hh
vtkreader.hh
vtkreader.impl.hh
vtktypes.hh
vtkwriter.hh
vtkwriter.impl.hh
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dune/vtkwriter)
add_subdirectory(datacollectors)
add_subdirectory(utility)
add_subdirectory(writers)
#install headers
install(FILES
continuousdatacollector.hh
datacollectorinterface.hh
discontinuousdatacollector.hh
quadraticdatacollector.hh
spdatacollector.hh
structureddatacollector.hh
unstructureddatacollector.hh
yaspdatacollector.hh
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dune/vtkwriter/datacollectors)
#pragma once
#include <dune/vtk/datacollector.hh>
#include "unstructureddatacollector.hh"
namespace Dune { namespace experimental
{
/// Implementation of \ref DataCollector for linear cells, with continuous data.
template <class GridView>
class DefaultDataCollector
: public DataCollectorInterface<GridView, DefaultDataCollector<GridView>>
class ContinuousDataCollector
: public UnstructuredDataCollectorInterface<GridView, ContinuousDataCollector<GridView>>
{
enum { dim = GridView::dimension };
using Self = DefaultDataCollector;
using Super = DataCollectorInterface<GridView, Self>;
using Super::gridView_;
using Self = ContinuousDataCollector;
using Super = UnstructuredDataCollectorInterface<GridView, Self>;
public:
DefaultDataCollector (GridView const& gridView)
ContinuousDataCollector (GridView const& gridView)
: Super(gridView)
{}
......@@ -31,7 +30,7 @@ public:
template <class T>
std::vector<T> pointsImpl () const
{
std::vector<T> data(this->numPoints() * 3);
std::vector<T> data(gridView_.size(dim) * 3);
auto const& indexSet = gridView_.indexSet();
for (auto const& vertex : vertices(gridView_, Partitions::all)) {
std::size_t idx = 3 * indexSet.index(vertex);
......@@ -44,6 +43,12 @@ public:
return data;
}
/// Return number of grid cells
std::uint64_t numCellsImpl () const
{
return gridView_.size(0);
}
/// Return the types, offsets and connectivity of the cells, using the same connectivity as
/// given by the grid.
Cells cellsImpl () const
......@@ -56,9 +61,9 @@ public:
});
Cells cells;
cells.connectivity.reserve(this->numCells() * maxVertices);
cells.offsets.reserve(this->numCells());
cells.types.reserve(this->numCells());
cells.connectivity.reserve(gridView_.size(0) * maxVertices);
cells.offsets.reserve(gridView_.size(0));
cells.types.reserve(gridView_.size(0));
std::int64_t old_o = 0;
for (auto const& c : elements(gridView_, Partitions::all)) {
......@@ -68,7 +73,6 @@ public:
cells.offsets.push_back(old_o += c.subEntities(dim));
cells.types.push_back(cellType.type());
}
return cells;
}
......@@ -76,7 +80,7 @@ public:
template <class T, class GlobalFunction>
std::vector<T> pointDataImpl (GlobalFunction const& fct) const
{
std::vector<T> data(this->numPoints() * fct.ncomps());
std::vector<T> data(gridView_.size(dim) * fct.ncomps());
auto const& indexSet = gridView_.indexSet();
auto localFct = localFunction(fct);
for (auto const& e : elements(gridView_, Partitions::all)) {
......@@ -92,6 +96,9 @@ public:
}
return data;
}
protected:
using Super::gridView_;
};
}} // end namespace Dune::experimental
#pragma once
#include <algorithm>
#include <cstdint>
#include <vector>
#include "vtktypes.hh"
#include <dune/vtk/vtktypes.hh>
namespace Dune { namespace experimental {
struct Cells
{
std::vector<std::uint8_t> types;
std::vector<std::int64_t> offsets;
std::vector<std::int64_t> connectivity;
};
template <class GridView, class Derived>
class DataCollectorInterface
{
public:
DataCollectorInterface (GridView const& gridView)
: gridView_(gridView)
{}
/// \brief Return the number of points in the grid
std::uint64_t numPoints () const
{
return asDerived().numPointsImpl();
}
/// \brief Return the number of cells in the grid
std::uint64_t numCells () const
{
return asDerived().numCellsImpl();
}
/// Update the DataCollector on the current GridView
void update ()
{
......@@ -48,6 +24,12 @@ public:
return asDerived().ghostLevelImpl();
}
/// \brief Return the number of points in the grid
std::uint64_t numPoints () const
{
return asDerived().numPointsImpl();
}
/// \brief Return a flat vector of point coordinates
/**
* All coordinates are extended to 3 components and concatenated.
......@@ -61,12 +43,6 @@ public:
return asDerived().template pointsImpl<T>();
}
/// \brief Return cell types, offsets, and connectivity. \see Cells
Cells cells () const
{
return asDerived().cellsImpl();
}
/// \brief Return a flat vector of function values evaluated at the grid points.
/**
* In case of a vector valued function, flat the vector entries:
......@@ -76,20 +52,19 @@ public:
* [fct(p0)_00, fct(p0)_01, fct(p0)_02, fct(p0)_10, fct(p0)_11, fct(p0)_12, fct(p0)_20...]
* where the tensor dimension must be 3x3 (possible extended by 0s)
**/
template <class T, class GlobalFunction>
std::vector<T> pointData (GlobalFunction const& fct) const
template <class T, class VtkFunction>
std::vector<T> pointData (VtkFunction const& fct) const
{
return asDerived().template pointDataImpl<T>(fct);
}
/// \brief Return a flat vector of function values evaluated at the grid cells. \see pointData.
template <class T, class GlobalFunction>
std::vector<T> cellData (GlobalFunction const& fct) const
template <class T, class VtkFunction>
std::vector<T> cellData (VtkFunction const& fct) const
{
return asDerived().template cellDataImpl<T>(fct);
}
protected: // cast to derived type
Derived& asDerived ()
......@@ -102,13 +77,7 @@ protected: // cast to derived type
return static_cast<const Derived&>(*this);
}
protected: // default implementations
std::uint64_t numCellsImpl () const
{
return gridView_.size(0);
}
public: // default implementations
void updateImpl ()
{
......@@ -121,10 +90,10 @@ protected: // default implementations
}
// Evaluate `fct` in center of cell
template <class T, class GlobalFunction>
std::vector<T> cellDataImpl (GlobalFunction const& fct) const
template <class T, class VtkFunction>
std::vector<T> cellDataImpl (VtkFunction const& fct) const
{
std::vector<T> data(numCells() * fct.ncomps());
std::vector<T> data(gridView_.size(0) * fct.ncomps());
auto const& indexSet = gridView_.indexSet();
auto localFct = localFunction(fct);
for (auto const& e : elements(gridView_, Partitions::all)) {
......@@ -138,9 +107,7 @@ protected: // default implementations
return data;
}
protected:
GridView gridView_;
};
......
#pragma once
#include <dune/vtk/datacollector.hh>
#include "unstructureddatacollector.hh"
namespace Dune { namespace experimental
{
......@@ -8,20 +8,17 @@ namespace Dune { namespace experimental
/// Implementation of \ref DataCollector for linear cells, with discontinuous data.
template <class GridView>
class DiscontinuousDataCollector
: public DataCollectorInterface<GridView, DiscontinuousDataCollector<GridView>>
: public UnstructuredDataCollectorInterface<GridView, DiscontinuousDataCollector<GridView>>
{
enum { dim = GridView::dimension };
using Self = DiscontinuousDataCollector;
using Super = DataCollectorInterface<GridView, Self>;
using Super::gridView_;
using Super = UnstructuredDataCollectorInterface<GridView, Self>;
public:
DiscontinuousDataCollector (GridView const& gridView)
: Super(gridView)
{
this->update();
}
{}
/// Create an index map the uniquely assignes an index to each pair (element,corner)
void updateImpl ()
......@@ -47,7 +44,7 @@ public:
template <class T>
std::vector<T> pointsImpl () const
{
std::vector<T> data(this->numPoints() * 3);
std::vector<T> data(numPoints_ * 3);
auto const& indexSet = gridView_.indexSet();
for (auto const& element : elements(gridView_, Partitions::interior)) {
for (unsigned int i = 0; i < element.subEntities(dim); ++i) {
......@@ -62,13 +59,19 @@ public:
return data;
}
/// Return number of grid cells
std::uint64_t numCellsImpl () const
{
return gridView_.size(0);
}
/// Connect the corners of each cell. The leads to a global discontinuous grid
Cells cellsImpl () const
{
Cells cells;
cells.connectivity.reserve(this->numPoints());
cells.offsets.reserve(this->numCells());
cells.types.reserve(this->numCells());
cells.connectivity.reserve(numPoints_);
cells.offsets.reserve(gridView_.size(0));
cells.types.reserve(gridView_.size(0));
std::int64_t old_o = 0;
auto const& indexSet = gridView_.indexSet();
......@@ -89,7 +92,7 @@ public:
template <class T, class GlobalFunction>
std::vector<T> pointDataImpl (GlobalFunction const& fct) const
{
std::vector<T> data(this->numPoints() * fct.ncomps());
std::vector<T> data(numPoints_ * fct.ncomps());
auto const& indexSet = gridView_.indexSet();
auto localFct = localFunction(fct);
for (auto const& e : elements(gridView_, Partitions::interior)) {
......@@ -106,7 +109,8 @@ public:
return data;
}
private:
protected:
using Super::gridView_;
std::uint64_t numPoints_ = 0;
std::vector<std::int64_t> indexMap_;
};
......
#pragma once
#include <dune/vtk/datacollector.hh>
#include "unstructureddatacollector.hh"
namespace Dune { namespace experimental
{
......@@ -8,13 +8,12 @@ namespace Dune { namespace experimental
/// Implementation of \ref DataCollector for quadratic cells, with continuous data.
template <class GridView>
class QuadraticDataCollector
: public DataCollectorInterface<GridView, QuadraticDataCollector<GridView>>
: public UnstructuredDataCollectorInterface<GridView, QuadraticDataCollector<GridView>>
{
enum { dim = GridView::dimension };
using Self = QuadraticDataCollector;
using Super = DataCollectorInterface<GridView, Self>;
using Super::gridView_;
using Super = UnstructuredDataCollectorInterface<GridView, Self>;
public:
QuadraticDataCollector (GridView const& gridView)
......@@ -63,6 +62,12 @@ public:
return data;
}
/// Return number of grid cells
std::uint64_t numCellsImpl () const
{
return gridView_.size(0);
}
/// \brief Return cell types, offsets, and connectivity. \see Cells
/**
* The cell connectivity is composed of cell vertices first and second cell edges,
......@@ -92,7 +97,6 @@ public:
cells.offsets.push_back(old_o += c.subEntities(dim)+c.subEntities(dim-1));
cells.types.push_back(cellType.type());
}
return cells;
}
......@@ -123,6 +127,9 @@ public:
}
return data;
}
protected:
using Super::gridView_;
};
}} // end namespace Dune::extensions
......@@ -3,7 +3,8 @@
#if HAVE_DUNE_SPGRID
#include <dune/grid/spgrid.hh>
#endif
#include <dune/vtk/datacollectors/structureddatacollector.hh>
#include "structureddatacollector.hh"
namespace Dune { namespace experimental
{
......@@ -18,7 +19,6 @@ class SPDataCollector
using Self = SPDataCollector;
using Super = StructuredDataCollectorInterface<GridView, Self>;
using Super::gridView_;
using ctype = typename GridView::ctype;
public:
......@@ -69,7 +69,8 @@ public:
}
}
private:
protected:
using Super::gridView_;
std::array<int, 6> wholeExtent_;
std::array<int, 6> extent_;
FieldVector<ctype,3> spacing_;
......
#pragma once
#include <dune/vtk/datacollectors/continuousdatacollector.hh>
#include <array>
#include <dune/common/fvector.hh>
#include "continuousdatacollector.hh"
namespace Dune { namespace experimental
{
......@@ -23,14 +26,13 @@ class StructuredDataCollectorInterface
{
protected:
using Super = DataCollectorInterface<GridView, Derived>;
using Super::gridView_;
using SubDataCollector = ContinuousDataCollector<GridView>;
using ctype = typename GridView::ctype;
public:
StructuredDataCollectorInterface (GridView const& gridView)
: Super(gridView)
, defaultDataCollector_(gridView)
, ghostLevel_(gridView.overlapSize(0))
, subDataCollector_(gridView)
{}
/// Sequence of Index pairs [begin, end) for the cells in each direction
......@@ -45,18 +47,6 @@ public:
return this->asDerived().extentImpl();
}
/// Lower left corner of the grid
FieldVector<ctype, 3> origin () const
{
return this->asDerived().originImpl();
}
/// Constant grid spacing in each coordinate direction
FieldVector<ctype, 3> spacing () const
{
return this->asDerived().spacingImpl();
}
/// Call the `writer` with extent
template <class Writer>
void writeLocalPiece (Writer const& writer) const
......@@ -71,12 +61,26 @@ public:
this->asDerived().writePiecesImpl(writer);
}
/// Return the number of overlapping elements
int ghostLevel () const
/// Interface for ImageData:
/// @{
/// Lower left corner of the grid
FieldVector<ctype, 3> origin () const
{
return this->asDerived().ghostLevelImpl();
return this->asDerived().originImpl();
}
/// Constant grid spacing in each coordinate direction
FieldVector<ctype, 3> spacing () const
{
return this->asDerived().spacingImpl();
}
/// @}
/// Interface for RectilinearGrid
/// @{
/// The coordinates defines point coordinates for an extent by specifying the ordinate along each axis.
template <class T>
std::array<std::vector<T>, 3> coordinates () const
......@@ -84,17 +88,15 @@ public:
return this->asDerived().template coordinatesImpl<T>();
}
public:
/// Return number of grid vertices
std::uint64_t numPointsImpl () const
{
return gridView_.size(GridView::dimension);
}
/// @}
public: // default implementation:
/// \copyref DefaultDataCollector::update.
void updateImpl ()
{
defaultDataCollector_.update();
subDataCollector_.update();
#if HAVE_MPI
int rank = -1;
......@@ -113,23 +115,27 @@ public:
#endif
}
/// Return number of grid vertices
std::uint64_t numPointsImpl () const
{
return subDataCollector_.numPoints();
}
/// \copydoc DefaultDataCollector::points.
template <class T>
std::vector<T> pointsImpl () const
{
return defaultDataCollector_.template points<T>();
return subDataCollector_.template points<T>();
}
/// \copydoc DefaultDataCollector::pointData
template <class T, class GlobalFunction>
std::vector<T> pointDataImpl (GlobalFunction const& fct) const
{
return defaultDataCollector_.template pointData<T>(fct);
return subDataCollector_.template pointData<T>(fct);
}
/// Default implementation for \ref writeLocalPiece. Calculates the extent and communicates it to
/// rank 0.
// Calculates the extent and communicates it to rank 0.
template <class Writer>
void writeLocalPieceImpl (Writer const& writer) const
{
......@@ -154,8 +160,7 @@ public:
writer(extent);
}
/// Receive extent from all ranks and call the `writer` with the rank's extent vector
// Receive extent from all ranks and call the `writer` with the rank's extent vector
template <class Writer>
void writePiecesImpl (Writer const& writer) const
{
......@@ -176,15 +181,21 @@ public:
#endif
}
/// Return the \ref GridView::overlapSize
int ghostLevelImpl () const
// Origin (0,0,0)
FieldVector<ctype, 3> originImpl () const
{
return ghostLevel_;
FieldVector<ctype, 3> vec; vec = ctype(0);
return vec;
}
// Grid spacing (0,0,0)
FieldVector<ctype, 3> spacingImpl () const
{
FieldVector<ctype, 3> vec; vec = ctype(0);
return vec;
}
/// Ordinate along each axis with constant \ref spacing from the \ref origin
// Ordinate along each axis with constant \ref spacing from the \ref origin
template <class T>
std::array<std::vector<T>, 3> coordinatesImpl () const
{
......@@ -206,9 +217,9 @@ public:
return ordinates;
}
private:
DefaultDataCollector<GridView> defaultDataCollector_;
int ghostLevel_;
protected:
using Super::gridView_;
SubDataCollector subDataCollector_;
#if HAVE_MPI
mutable std::vector<std::array<int,6>> extents_;
......
#pragma once
#include <cstdint>
#include <vector>
#include "datacollectorinterface.hh"
namespace Dune { namespace experimental {
struct Cells
{
std::vector<std::uint8_t> types;
std::vector<std::int64_t> offsets;
std::vector<std::int64_t> connectivity;
};
template <class GridView, class Derived>
class UnstructuredDataCollectorInterface