diff --git a/dune/vtk/CMakeLists.txt b/dune/vtk/CMakeLists.txt index a270981487bfaa32698b952402c10de2b0ea03c6..99642d1db0ae59be6f988efc345e689284f5dc7b 100644 --- a/dune/vtk/CMakeLists.txt +++ b/dune/vtk/CMakeLists.txt @@ -3,6 +3,7 @@ dune_add_library("vtktypes" OBJECT #install headers install(FILES + datacollectorinterface.hh defaultvtkfunction.hh filereader.hh filewriter.hh @@ -15,6 +16,8 @@ install(FILES vtkreader.impl.hh vtktypes.hh vtkwriter.hh + vtkwriterinterface.hh + vtkwriterinterface.impl.hh DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dune/vtkwriter) add_subdirectory(datacollectors) diff --git a/dune/vtk/datacollectors/datacollectorinterface.hh b/dune/vtk/datacollectorinterface.hh similarity index 69% rename from dune/vtk/datacollectors/datacollectorinterface.hh rename to dune/vtk/datacollectorinterface.hh index 1dcb283fa25c48addcf657dc4b74951361ef9935..8d967f3afcd22d853a96e80c3ebbaa38ba2b7b83 100644 --- a/dune/vtk/datacollectors/datacollectorinterface.hh +++ b/dune/vtk/datacollectorinterface.hh @@ -1,6 +1,7 @@ #pragma once -#include <dune/vtk/vtktypes.hh> +#include <cstdint> +#include <vector> namespace Dune { @@ -24,13 +25,13 @@ public: return asDerived().ghostLevelImpl(); } - /// \brief Return the number of points in the grid + /// Return the number of points in the grid std::uint64_t numPoints () const { return asDerived().numPointsImpl(); } - /// \brief Return a flat vector of point coordinates + /// Return a flat vector of point coordinates /** * All coordinates are extended to 3 components and concatenated. * [p0_x, p0_y, p0_z, p1_x, p1_y, p1_z, ...] @@ -43,7 +44,7 @@ public: return asDerived().template pointsImpl<T>(); } - /// \brief Return a flat vector of function values evaluated at the grid points. + /// Return a flat vector of function values evaluated at the points. /** * In case of a vector valued function, flat the vector entries: * [fct(p0)_0, fct(p0)_1, fct(p0)_2, fct(p1)_0, ...] @@ -58,7 +59,12 @@ public: return asDerived().template pointDataImpl<T>(fct); } - /// \brief Return a flat vector of function values evaluated at the grid cells. \see pointData. + /// Return a flat vector of function values evaluated at the cells. \see pointData. + /** + * Note: Cells might be descibred explicitly by connectivity, offsets, and types, e.g. in + * an UnstructuredGrid, or might be described implicitly by the grid type, e.g. in + * StructuredGrid. + */ template <class T, class VtkFunction> std::vector<T> cellData (VtkFunction const& fct) const { @@ -91,24 +97,12 @@ public: // default implementations // Evaluate `fct` in center of cell template <class T, class VtkFunction> - std::vector<T> cellDataImpl (VtkFunction const& fct) const - { - 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)) { - localFct.bind(e); - auto refElem = referenceElement<T,GridView::dimension>(e.type()); - std::size_t idx = fct.ncomps() * indexSet.index(e); - for (int comp = 0; comp < fct.ncomps(); ++comp) - data[idx + comp] = T(localFct.evaluate(comp, refElem.position(0,0))); - localFct.unbind(); - } - return data; - } + std::vector<T> cellDataImpl (VtkFunction const& fct) const; protected: GridView gridView_; }; } // end namespace Dune + +#include "datacollectorinterface.impl.hh" diff --git a/dune/vtk/datacollectorinterface.impl.hh b/dune/vtk/datacollectorinterface.impl.hh new file mode 100644 index 0000000000000000000000000000000000000000..d146cb1fa0c888776b6d63643eb2149845fd688b --- /dev/null +++ b/dune/vtk/datacollectorinterface.impl.hh @@ -0,0 +1,27 @@ +#pragma once + +#include <dune/geometry/referenceelements.hh> +#include <dune/grid/common/gridenums.hh> + +namespace Dune { + +template <class GridView, class Derived> + template <class T, class VtkFunction> +std::vector<T> DataCollectorInterface<GridView, Derived> + ::cellDataImpl (VtkFunction const& fct) const +{ + 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)) { + localFct.bind(e); + auto refElem = referenceElement<T,GridView::dimension>(e.type()); + std::size_t idx = fct.ncomps() * indexSet.index(e); + for (int comp = 0; comp < fct.ncomps(); ++comp) + data[idx + comp] = T(localFct.evaluate(comp, refElem.position(0,0))); + localFct.unbind(); + } + return data; +} + +} // end namespace Dune diff --git a/dune/vtk/datacollectors/CMakeLists.txt b/dune/vtk/datacollectors/CMakeLists.txt index c7841ffbe015595b08f0e71d17c847be2048a243..3ee908c806377ad04b1a0e4fc712cd3e160d3522 100644 --- a/dune/vtk/datacollectors/CMakeLists.txt +++ b/dune/vtk/datacollectors/CMakeLists.txt @@ -1,7 +1,6 @@ #install headers install(FILES continuousdatacollector.hh - datacollectorinterface.hh discontinuousdatacollector.hh quadraticdatacollector.hh spdatacollector.hh diff --git a/dune/vtk/datacollectors/structureddatacollector.hh b/dune/vtk/datacollectors/structureddatacollector.hh index 5383666972d2b6fcfc9f515e567d665c0caf4f11..bcfd4c34fa39061d442d16d38eb2a64223bbf27e 100644 --- a/dune/vtk/datacollectors/structureddatacollector.hh +++ b/dune/vtk/datacollectors/structureddatacollector.hh @@ -4,6 +4,7 @@ #include <dune/common/filledarray.hh> #include <dune/common/fvector.hh> +#include <dune/vtk/datacollectorinterface.hh> #include "continuousdatacollector.hh" namespace Dune diff --git a/dune/vtk/datacollectors/unstructureddatacollector.hh b/dune/vtk/datacollectors/unstructureddatacollector.hh index a473ed30e20c49c330e9af78c3490b937d8914a6..e9a55fb1fcb378ddeeada6f3c9b6c927bbb42808 100644 --- a/dune/vtk/datacollectors/unstructureddatacollector.hh +++ b/dune/vtk/datacollectors/unstructureddatacollector.hh @@ -3,7 +3,7 @@ #include <cstdint> #include <vector> -#include "datacollectorinterface.hh" +#include <dune/vtk/datacollectorinterface.hh> namespace Dune { diff --git a/dune/vtk/vtkwriter.hh b/dune/vtk/vtkwriter.hh index d33339260c5481230379f11c1c50c5fc852533f5..91befcfed7d93ce92232634d0813638239c2eb67 100644 --- a/dune/vtk/vtkwriter.hh +++ b/dune/vtk/vtkwriter.hh @@ -24,6 +24,28 @@ namespace Dune { using type = VtkUnstructuredGridWriter<GridView>; }; + } + + /// \brief Default choice of VTK Writer for several grid types. + /** + * Choose a VTK writer depending on the grid type. Some specialization for standard dune-grid grids + * are provided, like YaspGrid and GeometrGrid. + * + * Note: Uses the default data-collector. If you want to choose a special data-collector, use + * the concrete write Implementation instead. + **/ + template <class GridView> + using VtkWriter = typename Impl::VtkWriterImpl<GridView, typename GridView::Grid>::type; + + + namespace Impl + { + // A structured grid with constant spacing in x, y, and z direction. + template <class GridView, int dim, class Coordinates> + struct VtkWriterImpl<GridView, YaspGrid<dim,Coordinates>> + { + using type = VtkImageDataWriter<GridView, YaspDataCollector<GridView>>; + }; #if HAVE_DUNE_SPGRID // A structured grid with constant spacing in x, y, and z direction. @@ -34,13 +56,6 @@ namespace Dune }; #endif - // A structured grid with constant spacing in x, y, and z direction. - template <class GridView, int dim, class Coordinates> - struct VtkWriterImpl<GridView, YaspGrid<dim,Coordinates>> - { - using type = VtkImageDataWriter<GridView, YaspDataCollector<GridView>>; - }; - // A structured grid with coordinates in x, y, and z direction with arbitrary spacing template <class GridView, int dim, class ct> struct VtkWriterImpl<GridView, YaspGrid<dim,TensorProductCoordinates<ct,dim>>> @@ -57,10 +72,4 @@ namespace Dune }; } // end namespace Impl - - - /// Default choice for several grid types, uses the default data-collector. - template <class GridView> - using VtkWriter = typename Impl::VtkWriterImpl<GridView, typename GridView::Grid>::type; - } // end namespace Dune diff --git a/dune/vtk/writers/vtkwriterinterface.hh b/dune/vtk/vtkwriterinterface.hh similarity index 88% rename from dune/vtk/writers/vtkwriterinterface.hh rename to dune/vtk/vtkwriterinterface.hh index 5f9d3d2d5cb343d0f03f89f65cb2321c38e05666..dab897bea478d54ee8b6344da729bb992c32cb23 100644 --- a/dune/vtk/writers/vtkwriterinterface.hh +++ b/dune/vtk/vtkwriterinterface.hh @@ -1,8 +1,9 @@ #pragma once -#include <array> #include <iosfwd> #include <map> +#include <string> +#include <vector> #include <dune/common/std/optional.hh> @@ -104,15 +105,7 @@ namespace Dune std::uint64_t writeAppended (std::ofstream& out, std::vector<T> const& values) const; /// Return PointData/CellData attributes for the name of the first scalar/vector/tensor DataArray - std::string getNames (std::vector<VtkFunction> const& data) const - { - auto scalar = std::find_if(data.begin(), data.end(), [](auto const& v) { return v.ncomps() == 1; }); - auto vector = std::find_if(data.begin(), data.end(), [](auto const& v) { return v.ncomps() == 3; }); - auto tensor = std::find_if(data.begin(), data.end(), [](auto const& v) { return v.ncomps() == 9; }); - return (scalar != data.end() ? " Scalars=\"" + scalar->name() + "\"" : "") - + (vector != data.end() ? " Vectors=\"" + vector->name() + "\"" : "") - + (tensor != data.end() ? " Tensors=\"" + tensor->name() + "\"" : ""); - } + std::string getNames (std::vector<VtkFunction> const& data) const; // Returns endianness std::string getEndian () const diff --git a/dune/vtk/writers/vtkwriterinterface.impl.hh b/dune/vtk/vtkwriterinterface.impl.hh similarity index 91% rename from dune/vtk/writers/vtkwriterinterface.impl.hh rename to dune/vtk/vtkwriterinterface.impl.hh index 5b17a3cac88bf71e25caddff352898feffd1c7ee..26883e356b091c2b8d8900381969a66b4284cffc 100644 --- a/dune/vtk/writers/vtkwriterinterface.impl.hh +++ b/dune/vtk/vtkwriterinterface.impl.hh @@ -1,5 +1,6 @@ #pragma once +#include <algorithm> #include <iomanip> #include <iostream> #include <iterator> @@ -236,4 +237,17 @@ std::uint64_t VtkWriterInterface<GV,DC> return std::uint64_t(end_pos - begin_pos); } + +template <class GV, class DC> +std::string VtkWriterInterface<GV,DC> + ::getNames (std::vector<VtkFunction> const& data) const +{ + auto scalar = std::find_if(data.begin(), data.end(), [](auto const& v) { return v.ncomps() == 1; }); + auto vector = std::find_if(data.begin(), data.end(), [](auto const& v) { return v.ncomps() == 3; }); + auto tensor = std::find_if(data.begin(), data.end(), [](auto const& v) { return v.ncomps() == 9; }); + return (scalar != data.end() ? " Scalars=\"" + scalar->name() + "\"" : "") + + (vector != data.end() ? " Vectors=\"" + vector->name() + "\"" : "") + + (tensor != data.end() ? " Tensors=\"" + tensor->name() + "\"" : ""); +} + } // end namespace Dune diff --git a/dune/vtk/writers/CMakeLists.txt b/dune/vtk/writers/CMakeLists.txt index 0a9e7e241d9e1d47def09960fa7da194dc3bffe9..779237467679ed664f32981dfa76537d2d2cb4fb 100644 --- a/dune/vtk/writers/CMakeLists.txt +++ b/dune/vtk/writers/CMakeLists.txt @@ -8,6 +8,4 @@ install(FILES vtkstructuredgridwriter.impl.hh vtkunstructuredgridwriter.hh vtkunstructuredgridwriter.impl.hh - vtkwriterinterface.hh - vtkwriterinterface.impl.hh DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dune/vtkwriter/writers) diff --git a/dune/vtk/writers/vtkimagedatawriter.hh b/dune/vtk/writers/vtkimagedatawriter.hh index c9a67836f7eec2fa1d24e834eab21f1c684d30f2..d3b2af6fec89b8e9ce56efd560073f3917aab817 100644 --- a/dune/vtk/writers/vtkimagedatawriter.hh +++ b/dune/vtk/writers/vtkimagedatawriter.hh @@ -9,7 +9,7 @@ #include <dune/vtk/vtktypes.hh> #include <dune/vtk/datacollectors/structureddatacollector.hh> -#include "vtkwriterinterface.hh" +#include <dune/vtk/vtkwriterinterface.hh> namespace Dune { diff --git a/dune/vtk/writers/vtkrectilineargridwriter.hh b/dune/vtk/writers/vtkrectilineargridwriter.hh index fdfa416329bc9be944b23d6828d47dd963d99c8e..b40fdabe21dfd13be82f992a8c31c42e0fd27408 100644 --- a/dune/vtk/writers/vtkrectilineargridwriter.hh +++ b/dune/vtk/writers/vtkrectilineargridwriter.hh @@ -9,7 +9,7 @@ #include <dune/vtk/vtktypes.hh> #include <dune/vtk/datacollectors/structureddatacollector.hh> -#include "vtkwriterinterface.hh" +#include <dune/vtk/vtkwriterinterface.hh> namespace Dune { diff --git a/dune/vtk/writers/vtkstructuredgridwriter.hh b/dune/vtk/writers/vtkstructuredgridwriter.hh index 6dff211c33f0fcf93665049c4385539e2ce9df7c..beb9d564e3347ab0bbf22b10379fae47ff530c3e 100644 --- a/dune/vtk/writers/vtkstructuredgridwriter.hh +++ b/dune/vtk/writers/vtkstructuredgridwriter.hh @@ -9,7 +9,7 @@ #include <dune/vtk/vtktypes.hh> #include <dune/vtk/datacollectors/structureddatacollector.hh> -#include "vtkwriterinterface.hh" +#include <dune/vtk/vtkwriterinterface.hh> namespace Dune { diff --git a/dune/vtk/writers/vtkunstructuredgridwriter.hh b/dune/vtk/writers/vtkunstructuredgridwriter.hh index 4368d0bb4f7a46c10a3fa52b982ece47d51211cb..221cb3931b624837b0d7635a5cf285976c49b948 100644 --- a/dune/vtk/writers/vtkunstructuredgridwriter.hh +++ b/dune/vtk/writers/vtkunstructuredgridwriter.hh @@ -9,7 +9,7 @@ #include <dune/vtk/vtktypes.hh> #include <dune/vtk/datacollectors/continuousdatacollector.hh> -#include "vtkwriterinterface.hh" +#include <dune/vtk/vtkwriterinterface.hh> namespace Dune { diff --git a/src/structuredgridwriter.cc b/src/structuredgridwriter.cc index a8db430d4183f6b719e8f47e056799e1e7599ed7..349244a50ef50b4fdbf8fddeaf02f771a808ef55 100644 --- a/src/structuredgridwriter.cc +++ b/src/structuredgridwriter.cc @@ -77,9 +77,9 @@ void write_yaspgrid(std::integral_constant<int,dim>) { using GridType = YaspGrid<dim>; FieldVector<double,dim> upperRight; upperRight = 1.0; - auto numElements = filledArray<dim,int>(16); + auto numElements = filledArray<dim,int>(12); GridType grid(upperRight,numElements,0,0); - grid.globalRefine(3); + grid.globalRefine(1); write("yasp_" + std::to_string(dim) + "d_", grid.leafGridView()); }