dune-grid 2.9.0
gnuplot.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) DUNE Project contributors, see file LICENSE.md in module root
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4// vi: set et ts=4 sw=2 sts=2:
5#ifndef DUNE_IO_GNUPLOT_HH
6#define DUNE_IO_GNUPLOT_HH
7
13#include <vector>
14#include <string>
15#include <iostream>
16#include <fstream>
17
18#include <dune/common/fvector.hh>
19
21
22namespace Dune {
23
29 template<class GridView>
31
32 typedef typename GridView::Grid::ctype ctype;
33
34 constexpr static int dimworld = GridView::dimensionworld;
35
36 public:
37 GnuplotWriter (const GridView & gv) : _is(gv.indexSet()), _gv(gv)
38 {
39 static_assert(dimworld==1 || dimworld==2, "GnuPlot export only works for worlddim==1 and worlddim==2");
40 // allocate _data buffer
41 _data.resize(_is.size(0)*2);
42 }
43
48 template <class DataContainer>
49 void addCellData(const DataContainer& data, const std::string & name)
50 {
51 if (dimworld!=1)
52 DUNE_THROW(IOError, "Gnuplot cell data writing is only supported for grids in a 1d world!");
53 addData(cellData, data, name);
54 }
55
60 template <class DataContainer>
61 void addVertexData(const DataContainer& data, const std::string & name)
62 {
63 addData(vertexData, data, name);
64 }
65
69 void write(const std::string& filename) const;
70
71 private:
72 enum DataType { vertexData, cellData };
73 const typename GridView::IndexSet & _is;
74 const GridView _gv;
75 std::vector< std::vector< float > > _data;
76 std::vector< std::string > _names;
77
78 template <class DataContainer>
79 void addData(DataType t, const DataContainer& data, const std::string & name);
80
81 void writeRow(std::ostream & file,
82 const FieldVector<ctype, dimworld>& position,
83 const std::vector<float> & data) const;
84 };
85
89 template<class G>
90 class LeafGnuplotWriter : public GnuplotWriter<typename G::LeafGridView>
91 {
92 public:
94 LeafGnuplotWriter (const G& grid)
95 : GnuplotWriter<typename G::LeafGridView>(grid.leafGridView())
96 {}
97 };
98
102 template<class G>
103 class LevelGnuplotWriter : public GnuplotWriter<typename G::LevelGridView>
104 {
105 public:
107 LevelGnuplotWriter (const G& grid, int level)
108 : GnuplotWriter<typename G::LevelGridView>(grid.levelGridView(level))
109 {}
110 };
111
112}
113
114#include "gnuplot/gnuplot.cc"
115
116#endif // DUNE_IO_GNUPLOT_HH
Implementation of gnuplot output for 1D and 2D grids.
Traits::IndexSet IndexSet
type of the index set
Definition: common/gridview.hh:86
Grid< dim, dimworld, ct, GridFamily >::LeafGridView leafGridView(const Grid< dim, dimworld, ct, GridFamily > &grid)
leaf grid view for the given grid
Definition: common/grid.hh:819
Grid< dim, dimworld, ct, GridFamily >::LevelGridView levelGridView(const Grid< dim, dimworld, ct, GridFamily > &grid, int level)
level grid view for the given grid and level.
Definition: common/grid.hh:802
static constexpr int dimensionworld
The dimension of the world the grid lives in.
Definition: common/gridview.hh:151
Include standard header files.
Definition: agrid.hh:60
Grid view abstract base class.
Definition: common/gridview.hh:66
Writer for 1D grids in gnuplot format.
Definition: gnuplot.hh:30
void addVertexData(const DataContainer &data, const std::string &name)
Add vertex data.
Definition: gnuplot.hh:61
void addCellData(const DataContainer &data, const std::string &name)
Add cell data.
Definition: gnuplot.hh:49
void write(const std::string &filename) const
Write Gnuplot file to disk.
Definition: gnuplot.cc:19
GnuplotWriter(const GridView &gv)
Definition: gnuplot.hh:37
GnuplotWriter on the leaf grid.
Definition: gnuplot.hh:91
LeafGnuplotWriter(const G &grid)
Construct a Gnuplot writer for the leaf level of a given grid.
Definition: gnuplot.hh:94
GnuplotWriter on a given level grid.
Definition: gnuplot.hh:104
LevelGnuplotWriter(const G &grid, int level)
Construct a Gnuplot writer for a certain level of a given grid.
Definition: gnuplot.hh:107
Different resources needed by all grid implementations.