dune-grid  2.2.0
gnuplot.hh
Go to the documentation of this file.
1 #ifndef DUNE_IO_GNUPLOT_HH
2 #define DUNE_IO_GNUPLOT_HH
3 
9 #include <vector>
10 #include <string>
11 #include <iostream>
12 #include <fstream>
13 
14 #include <dune/common/fvector.hh>
15 
16 #include <dune/grid/common/grid.hh>
17 
18 namespace Dune {
19 
25  template<class GridView>
26  class GnuplotWriter {
27 
28  typedef typename GridView::Grid::ctype ctype;
29 
30  enum {dimworld = GridView::dimensionworld};
31 
32  public:
33  GnuplotWriter (const GridView & gv) : _is(gv.indexSet()), _gv(gv)
34  {
35  dune_static_assert(dimworld==1 || dimworld==2, "GnuPlot export only works for worlddim==1 and worlddim==2");
36  // allocate _data buffer
37  _data.resize(_is.size(0)*2);
38  }
39 
44  template <class DataContainer>
45  void addCellData(const DataContainer& data, const std::string & name)
46  {
47  if (dimworld!=1)
48  DUNE_THROW(IOError, "Gnuplot cell data writing is only supported for grids in a 1d world!");
49  addData(cellData, data, name);
50  }
51 
56  template <class DataContainer>
57  void addVertexData(const DataContainer& data, const std::string & name)
58  {
59  addData(vertexData, data, name);
60  }
61 
65  void write(const std::string& filename) const;
66 
67  private:
68  enum DataType { vertexData, cellData };
69  const typename GridView::IndexSet & _is;
70  const GridView _gv;
71  std::vector< std::vector< float > > _data;
72  std::vector< std::string > _names;
73 
74  template <class DataContainer>
75  void addData(DataType t, const DataContainer& data, const std::string & name);
76 
77  void writeRow(std::ostream & file,
78  const FieldVector<ctype, dimworld>& position,
79  const std::vector<float> & data) const;
80  };
81 
85  template<class G>
86  class LeafGnuplotWriter : public GnuplotWriter<typename G::LeafGridView>
87  {
88  public:
90  LeafGnuplotWriter (const G& grid)
91  : GnuplotWriter<typename G::LeafGridView>(grid.leafView())
92  {}
93  };
94 
98  template<class G>
99  class LevelGnuplotWriter : public GnuplotWriter<typename G::LevelGridView>
100  {
101  public:
103  LevelGnuplotWriter (const G& grid, int level)
104  : GnuplotWriter<typename G::LevelGridView>(grid.levelView(level))
105  {}
106  };
107 
108 }
109 
110 #include "gnuplot/gnuplot.cc"
111 
112 #endif // DUNE_IO_GNUPLOT_HH