gnuplot.hh
Go to the documentation of this file.00001 #ifndef DUNE_IO_GNUPLOT_HH
00002 #define DUNE_IO_GNUPLOT_HH
00003
00009 #include <vector>
00010 #include <string>
00011 #include <iostream>
00012 #include <fstream>
00013 #include <dune/common/helpertemplates.hh>
00014
00015 namespace Dune {
00016
00022 template<class GridType, class IndexSet>
00023 class GnuplotWriter {
00024
00025 typedef typename GridType::ctype ctype;
00026
00027 enum {dimworld = GridType::dimensionworld};
00028
00029 public:
00030 GnuplotWriter (const GridType & g, const IndexSet & is) : _grid(g), _is(is)
00031 {
00032
00033 IsTrue<dimworld == 1 || dimworld == 2>::yes();
00034
00035 _data.resize(_is.size(0)*2);
00036 }
00037
00042 template <class DataContainer>
00043 void addCellData(const DataContainer& data, const std::string & name)
00044 {
00045 if (dimworld!=1)
00046 DUNE_THROW(IOError, "Gnuplot cell data writing is only supported for grids in a 1d world!");
00047 addData(cellData, data, name);
00048 }
00049
00054 template <class DataContainer>
00055 void addVertexData(const DataContainer& data, const std::string & name)
00056 {
00057 addData(vertexData, data, name);
00058 }
00059
00063 void write(const std::string& filename) const;
00064
00065 private:
00066 enum DataType { vertexData, cellData };
00067 const GridType & _grid;
00068 const IndexSet & _is;
00069 std::vector< std::vector< float > > _data;
00070 std::vector< std::string > _names;
00071
00072 template <class DataContainer>
00073 void addData(DataType t, const DataContainer& data, const std::string & name);
00074
00075 void writeRow(std::ostream & file,
00076 const FieldVector<ctype, dimworld>& position,
00077 const std::vector<float> & data) const;
00078 };
00079
00083 template<class G>
00084 class LeafGnuplotWriter : public GnuplotWriter<G,typename G::template Codim<0>::LeafIndexSet>
00085 {
00086 public:
00088 LeafGnuplotWriter (const G& grid)
00089 : GnuplotWriter<G,typename G::template Codim<0>::LeafIndexSet>(grid,grid.leafIndexSet())
00090 {}
00091 };
00092
00096 template<class G>
00097 class LevelGnuplotWriter : public GnuplotWriter<G, typename G::template Codim<0>::LevelIndexSet>
00098 {
00099 public:
00101 LevelGnuplotWriter (const G& grid, int level)
00102 : GnuplotWriter<G,typename G::template Codim<0>::LevelIndexSet>(grid,grid.levelIndexSet(level))
00103 {}
00104 };
00105
00106 }
00107
00108 #include "gnuplot/gnuplot.cc"
00109
00110 #endif // DUNE_IO_GNUPLOT_HH