27#include <boost/fusion/include/zip.hpp>
28#include <boost/range/iterator_range.hpp>
30#include <dune/grid/common/grid.hh>
31#include <dune/geometry/quadraturerules.hh>
32#include <dune/geometry/referenceelements.hh>
58 std::string vtkEndianness();
74 vtkquadQuadrilateral = 23,
75 vtkquadTetrahedron = 24,
76 vtkquadHexahedron = 25,
77 vtkLagrangeTriangle = 69,
78 vtkLagrangeQuadrilateral = 70,
79 vtkLagrangeTetrahedron = 71,
80 vtkLagrangeHexahedron = 72
84 VTKGeometryType vtkType(Dune::GeometryType
const& t,
int order);
93 std::pair<int,int> vtkPointToDune(Dune::GeometryType
const& type,
int vtkPoint);
102 class VTKDataArrayWriter
114 VTKDataArrayWriter(std::ostream &s, IoOptions::OutputType outputType,
115 std::string
const& name,
int ncomps,
116 size_t entries,
int indentCount,
int precision);
122 void write (Dune::FieldVector<T,m>
const&);
130 ~VTKDataArrayWriter();
134 IoOptions::OutputType outputType;
140 std::unique_ptr<Base64Writer> base64;
146 void indent(std::ostream& s,
int indentCount);
154 template <
class ct,
int dim>
155 class CornerQuadratureRule:
public Dune::QuadratureRule<ct,dim>
158 CornerQuadratureRule(Dune::GeometryType gt,
int order)
159 : Dune::QuadratureRule<ct,dim>(gt,order)
161 assert(1<=order && order<=2);
163 auto refCell = Dune::referenceElement<ct,dim>(gt);
166 int const n = refCell.size(dim) + (order==2 ? refCell.size(dim-1) : 0);
172 for (
int i=0; i<n; ++i)
174 auto [k,codim] = vtkPointToDune(gt,i);
175 this->push_back(Dune::QuadraturePoint<ct,dim>(refCell.position(k,codim),w));
183 template <
class Gr
idView>
186 using Cell =
typename GridView::template Codim<0>::Entity;
187 using IndexSet =
typename GridView::IndexSet;
188 using CellIterator =
typename GridView::template Codim<0>::Iterator;
190 static int constexpr dim = GridView::dimension;
193 VTKGridInfo(GridView
const& gridView_, IoOptions
const& options_)
194 : gridView(gridView_),
195 cells(gridView.template begin<0>(),gridView.template end<0>()),
196 ncells(gridView.size(0)),
203 for (
auto const& cell: cells)
205 ncorners += cell.subEntities(dim);
206 if (options.order==2)
207 ncorners += cell.subEntities(dim-1);
212 if (options.dataMode==IoOptions::conforming)
214 npoints = gridView.size(dim);
215 if (options.order==2)
216 npoints += gridView.size(dim-1);
224 template <
class Cell>
225 auto center(Cell
const& cell,
int subindex,
int codim)
const
227 return Dune::ReferenceElements<typename GridView::ctype,dim>::general(cell.type()).position(subindex,codim);
232 template <
class Cell>
233 int conformingIndex(Cell
const& cell,
int subindex,
int codim)
const
237 return gridView.indexSet().subIndex(cell,subindex,codim);
239 return gridView.size(dim) + gridView.indexSet().subIndex(cell,subindex,codim);
243 template <
class Scalar=
typename Gr
idView::ctype>
244 void writeGridPoints (
int indentCount, std::ostream& s)
const
246 VTKDataArrayWriter<Scalar> p(s, options.outputType,
"Coordinates", 3,
247 npoints, indentCount, options.precision);
248 using Vector = Dune::FieldVector<Scalar,GridView::dimensionworld>;
250 if (options.dataMode==IoOptions::conforming)
256 std::vector<Vector> xs(npoints);
257 for (
auto const& cell: cells)
260 for (
int corner=0; corner<cell.subEntities(dim); ++corner)
261 xs[conformingIndex(cell,corner,dim)] = cell.geometry().corner(corner);
264 if (options.order == 2)
265 for (
int edge=0; edge<cell.subEntities(dim-1); ++edge)
266 xs[conformingIndex(cell,edge,dim-1)] = cell.geometry().global(center(cell,edge,dim-1));
271 if (options.order > 2)
273 assert(
"VTK output of order > 2 not yet implemented\n"==0);
277 for (
auto const& x: xs)
283 for (
auto const& cell: cells)
286 for (
int corner=0; corner<cell.subEntities(dim); ++corner)
287 p.write(
static_cast<Vector>(cell.geometry().corner(corner)));
290 if (options.order == 2)
291 for (
int edge=0; edge<cell.subEntities(dim-1); ++edge)
292 p.write(
static_cast<Vector>(cell.geometry().global(center(cell,edge,dim-1))));
297 if (options.order == 3)
299 assert(
"VTK output of order > 2 not yet implemented\n"==0);
305 void writeGridCells (
int indentCount, std::ostream& s)
const
308 auto p1 = std::make_unique<VTKDataArrayWriter<int>>(s, options.outputType,
"connectivity", 1,
309 ncorners, indentCount, options.precision);
312 std::vector<int> offsets; offsets.reserve(ncells);
313 for (
auto const& cell: cells)
315 int const numCorners = cell.subEntities(dim);
316 int const numEdges = cell.subEntities(dim-1);
317 int const numPoints = numCorners + (options.order==2? numEdges: 0);
319 for (
int vtkPoint=0; vtkPoint<numPoints; ++vtkPoint)
321 auto subentity = vtkPointToDune(cell.type(),vtkPoint);
322 p1->write( options.dataMode==IoOptions::conforming?
323 conformingIndex(cell,subentity.first,subentity.second):
324 offset + (subentity.second==dim? 0: numCorners) + subentity.first );
327 offsets.push_back(offset);
332 auto p2 = std::make_unique<VTKDataArrayWriter<int>>(s, options.outputType,
"offsets", 1,
333 ncells, indentCount, options.precision);
334 for (
int off: offsets)
341 VTKDataArrayWriter<unsigned char> p(s, options.outputType,
"types", 1,
342 ncells, indentCount, options.precision);
343 for (
auto const& cell: cells)
344 p.write(
static_cast<unsigned char>(vtkType(cell.type(),options.order)));
349 GridView
const& gridView;
350 boost::iterator_range<CellIterator> cells;
354 IoOptions
const& options;
377 template <
class Gr
idView,
class Function,
class Names>
378 void writeCellData (VTKGridInfo<GridView>
const& gridInfo, Function
const& pair,
379 Names
const& names,
int indentCount, std::ostream& s)
381 namespace bf = boost::fusion;
383 using Scalar =
typename std::remove_reference_t<typename bf::result_of::value_at_c<Function,0>::type>::Scalar;
385 auto const& f = bf::at_c<0>(pair);
388 if (f.space().mapper().maxOrder() > 0)
391 auto varDesc = bf::at_c<1>(pair);
392 VTKDataArrayWriter<Scalar> p(s, gridInfo.options.outputType, names[varDesc.id], varDesc.m, gridInfo.ncells, indentCount, gridInfo.options.precision);
393 for (
auto const& cell: gridInfo.cells)
394 p.write(f.value(cell,gridInfo.center(cell,0,0)));
399 template <
class Gr
idView,
class Function,
class Names>
400 void writeVertexData (VTKGridInfo<GridView>
const& gridInfo, Function
const& pair,
401 Names
const& names,
int indentCount, std::ostream& s, Timings& timer)
403 using ValueType =
typename std::remove_reference_t<typename boost::fusion::result_of::value_at_c<Function,0>::type>::ValueType;
404 using Grid =
typename GridView::Grid;
405 using Scalar =
typename ValueType::field_type;
406 constexpr int dim = GridView::dimension;
408 auto const& f = boost::fusion::at_c<0>(pair);
409 auto varDesc = boost::fusion::at_c<1>(pair);
413 if (f.space().mapper().maxOrder() == 0)
418 std::string
const& name = names[varDesc.id].empty() ?
"var"+
paddedString(varDesc.id,2)
420 VTKDataArrayWriter<Scalar> p(s, gridInfo.options.outputType, name,
421 varDesc.m, gridInfo.npoints, indentCount,
422 gridInfo.options.precision);
427 std::vector<ValueType> fs(gridInfo.npoints,ValueType(0.0));
428 std::vector<short>
count(gridInfo.npoints,0);
433 FunctionSpace_Detail::ToDomainRepresentation<std::remove_const_t<std::remove_reference_t<typename boost::fusion::result_of::value_at_c<Function,0>::type>>> tdr(f);
434 auto const& df = tdr.get();
439 ShapeFunctionCache<Grid,Scalar> sfCache;
440 auto evaluator = f.space().evaluator(&sfCache,0);
441 CornerQuadratureRule<typename Grid::ctype,dim>
qr(gridInfo.cells.begin()->type(),
442 gridInfo.options.order);
444 timer.start(
"eval at vertices");
445 for (
auto const& cell: gridInfo.cells)
447 evaluator.moveTo(cell);
448 assert(cell.type()==
qr.type());
449 for (
int point=0; point<
qr.size(); ++point)
451 auto [k,codim] = vtkPointToDune(cell.type(),point);
452 int idx = gridInfo.conformingIndex(cell,k,codim);
456 evaluator.evaluateAt(qp.position(),
qr,point,0);
457 fs[idx] += df.value(evaluator);
460 timer.stop(
"eval at vertices");
462 timer.start(
"write out");
463 for (
int i=0; i<gridInfo.npoints; ++i)
464 p.write(fs[i]/
static_cast<typename ValueType::field_type
>(
count[i]));
465 timer.stop(
"write out");
469 for (
auto const& cell: gridInfo.cells)
472 for (
int corner=0; corner<cell.subEntities(dim); ++corner)
473 p.write(f.value(cell,gridInfo.center(cell,corner,dim)));
476 if (gridInfo.options.order == 2)
477 for (
int edge=0; edge<cell.subEntities(dim-1); ++edge)
478 p.write(f.value(cell,gridInfo.center(cell,edge,dim-1)));
485 if (gridInfo.options.order > 2)
495 template <
class Functions,
class Names>
496 std::string guessActiveFields(Functions
const& functions, Names
const& names,
bool cellData)
498 std::string scalar =
"";
501 boost::fusion::for_each(functions,[&](
auto const& pair)
503 auto const& f = boost::fusion::at_c<0>(pair);
504 auto varDesc = boost::fusion::at_c<1>(pair);
507 if ( (f.space().mapper().maxOrder()==0 && cellData)
508 || (f.space().mapper().maxOrder()>0 && !cellData) )
510 if (f.components>1 &&
vector.empty())
511 vector =
" Vectors=\"" + names[varDesc.id] +
"\"";
512 if (f.components==1 && scalar.empty())
513 scalar =
" Scalars=\"" + names[varDesc.id] +
"\"" ;
533 template <
class VariableSet>
540 timer.start(
"VTK output");
544 options.
order = std::max(1,std::min(options.
order,2));
552 boost::fusion::for_each(vars.
data,[&](
auto const& f)
554 if (f.space().continuity<0 && f.space().mapper().maxOrder()>0)
555 options.dataMode = IoOptions::nonconforming;
561 constexpr int dim = VariableSet::Descriptions::Grid::dimension;
563 timer.start(
"grid info");
564 VTKGridInfo<typename VariableSet::Descriptions::GridView> gridInfo(vars.
descriptions->gridView,
566 timer.stop(
"grid info");
569 s <<
"<?xml version=\"1.0\"?>" <<
'\n';
572 std::string
const gridTag = dim>1?
"UnstructuredGrid":
"PolyData";
573 s <<
"<VTKFile type=\"" << gridTag <<
"\" version=\"0.1\" byte_order=\"" << vtkEndianness() <<
"\">\n";
577 indent(s,indentCount);
578 s <<
"<" << gridTag <<
">" <<
'\n';
582 indent(s,indentCount);
584 s <<
"<Piece NumberOfPoints=\"" << gridInfo.npoints <<
"\" NumberOfCells=\"" << gridInfo.ncells <<
"\">\n";
586 s <<
"<Piece NumberOfPoints=\"" << gridInfo.npoints <<
"\" NumberOfVerts=\"0\" NumberOfLines=\"" << gridInfo.ncells <<
"\" NumberOfPolys=\"0\">\n";
590 timer.start(
"write vertices");
591 indent(s,indentCount); s <<
"<Points>" <<
'\n';
593 gridInfo.template writeGridPoints<float>(indentCount+1,s);
595 gridInfo.template writeGridPoints<double>(indentCount+1,s);
596 indent(s,indentCount); s <<
"</Points>" <<
'\n';
597 timer.stop(
"write vertices");
600 timer.start(
"write cells");
601 std::string
const cellTagName = dim>1?
"Cells":
"Lines";
602 indent(s,indentCount); s <<
'<' << cellTagName <<
">\n";
603 gridInfo.writeGridCells(indentCount+1,s);
604 indent(s,indentCount); s <<
"</" << cellTagName <<
">\n";
605 timer.stop(
"write cells");
608 auto varDesc =
typename VariableSet::Descriptions::Variables();
609 auto functions = boost::fusion::zip(vars.
data,varDesc);
614 timer.start(
"write cell data");
615 indent(s,indentCount); s <<
"<CellData"
616 << guessActiveFields(functions,names,
true) <<
">\n";
617 boost::fusion::for_each(functions,[&](
auto const& f)
619 writeCellData(gridInfo,f,names,indentCount+1,s);
621 indent(s,indentCount); s <<
"</CellData>" <<
'\n';
622 timer.stop(
"write cell data");
626 timer.start(
"write point data");
627 indent(s,indentCount); s <<
"<PointData"
628 << guessActiveFields(functions,names,
false) <<
">\n";
629 boost::fusion::for_each(functions,[&](
auto const& f)
631 writeVertexData(gridInfo,f,names,indentCount+1,s,timer);
633 indent(s,indentCount); s <<
"</PointData>\n";
634 timer.stop(
"write point data");
638 indent(s,indentCount); s <<
"</Piece>\n";
642 indent(s,indentCount); s <<
"</" << gridTag <<
">\n";
645 s <<
"</VTKFile>" <<
'\n';
647 timer.stop(
"VTK output");
664 template <
class VariableSet>
668 fname += (VariableSet::Descriptions::GridView::dimension>1?
".vtu" :
".vtp");
671 std::ofstream file(fname);
An exception class for file IO errors.
static Timings & instance()
Returns a reference to a single default instance.
A class for storing a heterogeneous collection of FunctionSpaceElement s.
Descriptions const * descriptions
Descriptions of variable set, of type VariableSetDescription (lots of useful infos)
int count(Cell const &cell, int codim)
Computes the number of subentities of codimension c of a given entity.
std::string paddedString(int n, int places=3)
creates a zero-padded string representation of the given number
void writeVTK(Function const &f, std::string const &filename, IoOptions options, std::string fname)
Writes a single finite element function to a VTK file.
std::tuple< std::vector< typename EntryTraits< Entry >::field_type >, DynamicMatrix< typename EntryTraits< Entry >::field_type >, std::vector< int > > qr(DynamicMatrix< Entry > const &A)
Computes a rank-revealing QR decomposition of using column pivoting.
typename GridView::template Codim< 0 >::Entity Cell
The type of cells (entities of codimension 0) in the grid view.
Output of mesh and solution for visualization software.
void write(std::string const &directory, std::string const &phase, std::uint64_t exchange, int peer, int bits, std::size_t coarseCount, std::vector< U > const &quantized, std::vector< U > const &mapped, std::vector< U > const &lengths, bool encoded, bool floating, bool integer)
Observe the actual per-neighbor encoder inputs, without modifying them.
void vector(char const *phase, char const *direction, int id, int peer, char const *stage, std::vector< T > const &data)
Dune::FieldVector< Scalar, dim > Vector
int const LagrangeCubeShapeFunction< ctype, dimension, Scalar, O >::order
options for VTK/AMIRA output