dune-grid  2.6-git
gnuplot.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_IO_GNUPLOT_HH
4 #define DUNE_IO_GNUPLOT_HH
5 
11 #include <vector>
12 #include <string>
13 #include <iostream>
14 #include <fstream>
15 
16 #include <dune/common/fvector.hh>
17 
18 #include <dune/grid/common/grid.hh>
19 
20 namespace Dune {
21 
27  template<class GridView>
28  class GnuplotWriter {
29 
30  typedef typename GridView::Grid::ctype ctype;
31 
32  enum {dimworld = GridView::dimensionworld};
33 
34  public:
35  GnuplotWriter (const GridView & gv) : _is(gv.indexSet()), _gv(gv)
36  {
37  static_assert(dimworld==1 || dimworld==2, "GnuPlot export only works for worlddim==1 and worlddim==2");
38  // allocate _data buffer
39  _data.resize(_is.size(0)*2);
40  }
41 
46  template <class DataContainer>
47  void addCellData(const DataContainer& data, const std::string & name)
48  {
49  if (dimworld!=1)
50  DUNE_THROW(IOError, "Gnuplot cell data writing is only supported for grids in a 1d world!");
51  addData(cellData, data, name);
52  }
53 
58  template <class DataContainer>
59  void addVertexData(const DataContainer& data, const std::string & name)
60  {
61  addData(vertexData, data, name);
62  }
63 
67  void write(const std::string& filename) const;
68 
69  private:
70  enum DataType { vertexData, cellData };
71  const typename GridView::IndexSet & _is;
72  const GridView _gv;
73  std::vector< std::vector< float > > _data;
74  std::vector< std::string > _names;
75 
76  template <class DataContainer>
77  void addData(DataType t, const DataContainer& data, const std::string & name);
78 
79  void writeRow(std::ostream & file,
80  const FieldVector<ctype, dimworld>& position,
81  const std::vector<float> & data) const;
82  };
83 
87  template<class G>
88  class LeafGnuplotWriter : public GnuplotWriter<typename G::LeafGridView>
89  {
90  public:
92  LeafGnuplotWriter (const G& grid)
93  : GnuplotWriter<typename G::LeafGridView>(grid.leafGridView())
94  {}
95  };
96 
100  template<class G>
101  class LevelGnuplotWriter : public GnuplotWriter<typename G::LevelGridView>
102  {
103  public:
105  LevelGnuplotWriter (const G& grid, int level)
106  : GnuplotWriter<typename G::LevelGridView>(grid.levelGridView(level))
107  {}
108  };
109 
110 }
111 
112 #include "gnuplot/gnuplot.cc"
113 
114 #endif // DUNE_IO_GNUPLOT_HH
void addVertexData(const DataContainer &data, const std::string &name)
Add vertex data.
Definition: gnuplot.hh:59
GnuplotWriter on a given level grid.
Definition: gnuplot.hh:101
The dimension of the world the grid lives in.
Definition: common/gridview.hh:131
Implementation of gnuplot output for 1D and 2D grids.
GnuplotWriter on the leaf grid.
Definition: gnuplot.hh:88
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:792
Writer for 1D grids in gnuplot format.
Definition: gnuplot.hh:28
GnuplotWriter(const GridView &gv)
Definition: gnuplot.hh:35
LeafGnuplotWriter(const G &grid)
Construct a Gnuplot writer for the leaf level of a given grid.
Definition: gnuplot.hh:92
Grid view abstract base class.
Definition: common/gridview.hh:59
Traits ::IndexSet IndexSet
type of the index set
Definition: common/gridview.hh:80
void write(const std::string &filename) const
Write Gnuplot file to disk.
Definition: gnuplot.cc:17
void addCellData(const DataContainer &data, const std::string &name)
Add cell data.
Definition: gnuplot.hh:47
Different resources needed by all grid implementations.
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:809
LevelGnuplotWriter(const G &grid, int level)
Construct a Gnuplot writer for a certain level of a given grid.
Definition: gnuplot.hh:105
Include standard header files.
Definition: agrid.hh:58