Commit 3612f5f1 authored by Praetorius, Simon's avatar Praetorius, Simon
Browse files

Merge branch 'feature/export_types' into 'master'

Export some types and data

See merge request extensions/dune-vtk!14
parents bab2cc12 4ac2e5bc
......@@ -26,14 +26,14 @@ namespace Dune
*
* NOTE: Assumption on the file structure: Each XML tag must be on a separate line.
*
* \tparam Grid The type of the grid to construct.
* \tparam GridCreator Policy type to control what to pass to a grid factory with
* data given from the file. [ContinuousGridCreator]
* \tparam FieldType Type of the components of the data to extract from the file [default: double]
* \tparam Grid The type of the grid to construct.
* \tparam GC GridCreator policy type to control what to pass to a grid factory with
* data given from the file. [ContinuousGridCreator]
* \tparam FieldType Type of the components of the data to extract from the file [default: double]
**/
template <class Grid, class GridCreator = Vtk::ContinuousGridCreator<Grid>, class FieldType = double>
template <class Grid, class GC = Vtk::ContinuousGridCreator<Grid>, class FieldType = double>
class VtkReader
: public Vtk::FileReader<Grid, VtkReader<Grid, GridCreator>>
: public Vtk::FileReader<Grid, VtkReader<Grid, GC>>
{
// Sections visited during the xml parsing
enum Sections {
......@@ -52,17 +52,32 @@ namespace Dune
};
// Type of global world coordinates
using GlobalCoordinate = typename GridCreator::GlobalCoordinate;
using GlobalCoordinate = typename GC::GlobalCoordinate;
// Template representing a grid-function that is created in getPointData() and getCellData()
// with Context either Vtk::PointContext or Vek::CellContext, respectively.
// To each GridCreate a GridFunction is associated, see, e.g. Vtk::ContinuousGridFunction
// To each GridCreator a GridFunction is associated, see, e.g. Vtk::ContinuousGridFunction
// or Vtk::LagrangeGridFunction.
template <class Context>
using GridFunction = typename Vtk::AssociatedGridFunction<GridCreator, FieldType, Context>::type;
using GridFunction = typename Vtk::AssociatedGridFunction<GC, FieldType, Context>::type;
public:
using GridCreator = GC;
/// GridFunction representing the data stored on the points in the file
using PointGridFunction = GridFunction<Vtk::PointContext>;
/// GridFunction representing the data stored on the cells in the file
using CellGridFunction = GridFunction<Vtk::CellContext>;
public:
/// Constructor. Creates a new GridCreator with the passed factory
/**
* \param args... Either pass a GridFactory by reference or shared_ptr, or a list of arguments
* passed to the constructor of a Dune::GridFactory (typically and empty parameter
* list). See the constructor of \ref GridCreatorInterface and the GridCreator
* passed to this reader.
**/
template <class... Args,
std::enable_if_t<std::is_constructible<GridCreator, Args...>::value,int> = 0>
explicit VtkReader (Args&&... args)
......@@ -97,36 +112,66 @@ namespace Dune
}
/// Construct the actual grid using the GridCreator
/// NOTE: requires an aforegoing call to \ref read()
/// [[expects: read_ == true]]
std::unique_ptr<Grid> createGrid () const
{
return creator_->createGrid();
}
/// Construct a grid-function representing the point-data with the given name
/// NOTE: requires an aforegoing call to \ref read()
/// [[expects: read_ == true]]
GridFunction<Vtk::PointContext> getPointData (std::string const& name) const
{
auto const& data = dataArray_.at("PointData." + name);
VTK_ASSERT_MSG(data.section == POINT_DATA,
"The data to extract is not point-data. Use `getCellData()` instead!");
auto data_it = dataArray_.find("PointData." + name);
auto point_it = pointData_.find("PointData." + name);
VTK_ASSERT_MSG(data_it != dataArray_.end() && point_it != pointData_.end(),
"The data to extract is not found in point-data. Try `getCellData()` instead!");
VTK_ASSERT(data_it->second.section == POINT_DATA);
return {*creator_, pointData_.at("PointData." + name), data.components,
return {*creator_, point_it->second, data_it->second.components,
vec_types, vec_offsets, vec_connectivity};
}
/// Return a vector of DataArrayAttributes for all POINT_DATA blocks
/// [[expects: read_ == true]]
std::vector<DataArrayAttributes> getPointDataAttributes () const
{
std::vector<DataArrayAttributes> attributes;
attributes.reserve(pointData_.size());
for (auto const& da : dataArray_) {
if (da.second.section == POINT_DATA);
attributes.push_back(da.second);
}
return attributes;
}
/// Construct a grid-function representing the cell-data with the given name
/// NOTE: requires an aforegoing call to \ref read()
/// [[expects: read_ == true]]
GridFunction<Vtk::CellContext> getCellData (std::string const& name) const
{
auto const& data = dataArray_.at("CellData." + name);
VTK_ASSERT_MSG(data.section == CELL_DATA,
"The data to extract is not cell-data. Use `getPointData()` instead!");
auto data_it = dataArray_.find("CellData." + name);
auto cell_it = cellData_.find("CellData." + name);
VTK_ASSERT_MSG(data_it != dataArray_.end() && cell_it != cellData_.end(),
"The data to extract is not found in cell-data. Try `getPointData()` instead!");
VTK_ASSERT(data_it->second.section == CELL_DATA);
return {*creator_, cellData_.at("CellData." + name), data.components,
return {*creator_, cell_it->second, data_it->second.components,
vec_types, vec_offsets, vec_connectivity};
}
/// Return a vector of DataArrayAttributes for all CELL_DATA blocks
/// [[expects: read_ == true]]
std::vector<DataArrayAttributes> getCellDataAttributes () const
{
std::vector<DataArrayAttributes> attributes;
attributes.reserve(cellData_.size());
for (auto const& da : dataArray_) {
if (da.second.section == CELL_DATA);
attributes.push_back(da.second);
}
return attributes;
}
/// Advanced read methods
/// @{
......@@ -258,6 +303,8 @@ namespace Dune
/// Offset of beginning of appended data
std::uint64_t offset0_ = 0;
bool read_ = false;
};
// deduction guides
......
......@@ -35,6 +35,7 @@ void VtkReader<Grid,Creator,Field>::read (std::string const& filename, bool fill
} else {
DUNE_THROW(Dune::VtkError, "File has unknown file-extension '" << ext << "'. Allowed are only '.vtu' and '.pvtu'.");
}
read_ = true;
}
......@@ -807,6 +808,7 @@ void VtkReader<Grid,Creator,Field>::clear ()
numberOfCells_ = 0;
numberOfPoints_ = 0;
offset0_ = 0;
read_ = false;
}
} // end namespace Dune
......@@ -16,21 +16,22 @@ namespace Dune
{
/// Interface for file writers for the Vtk XML file formats
/**
* \tparam GridView Model of Dune::GridView
* \tparam DataCollector Model of \ref DataCollectorInterface
* \tparam GV Model of Dune::GridView
* \tparam DC Model of \ref DataCollectorInterface
**/
template <class GridView, class DataCollector>
template <class GV, class DC>
class VtkWriterInterface
: public Vtk::FileWriter
{
template <class> friend class TimeseriesWriter;
template <class> friend class PvdWriter;
protected:
static constexpr int dimension = GridView::dimension;
public:
using GridView = GV;
using DataCollector = DC;
protected:
using VtkFunction = Dune::Vtk::Function<GridView>;
using Communicator = CollectiveCommunication<typename MPIHelper::MPICommunicator>;
using pos_type = typename std::ostream::pos_type;
enum PositionTypes {
......
......@@ -22,8 +22,6 @@ namespace Dune
class VtkImageDataWriter
: public VtkWriterInterface<GridView, DataCollector>
{
static constexpr int dimension = GridView::dimension;
using Super = VtkWriterInterface<GridView, DataCollector>;
using pos_type = typename Super::pos_type;
......
......@@ -22,8 +22,6 @@ namespace Dune
class VtkRectilinearGridWriter
: public VtkWriterInterface<GridView, DataCollector>
{
static constexpr int dimension = GridView::dimension;
using Super = VtkWriterInterface<GridView, DataCollector>;
using pos_type = typename Super::pos_type;
......
......@@ -22,8 +22,6 @@ namespace Dune
class VtkStructuredGridWriter
: public VtkWriterInterface<GridView, DataCollector>
{
static constexpr int dimension = GridView::dimension;
using Super = VtkWriterInterface<GridView, DataCollector>;
using pos_type = typename Super::pos_type;
......
......@@ -24,8 +24,6 @@ namespace Dune
{
template <class> friend class VtkTimeseriesWriter;
static constexpr int dimension = GridView::dimension;
using Super = VtkWriterInterface<GridView, DataCollector>;
using pos_type = typename Super::pos_type;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment