dune-grid 3.0-git
gnuplot.cc
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:
8#include "../gnuplot.hh"
9
10namespace Dune {
11
15 template<class GridView>
16 void
17 GnuplotWriter<GridView>::write(const std::string& filename) const
18 {
19 // open file
20 std::ofstream file(filename.c_str());
21 // write all column names
22 file << "# coord\t";
23 for (size_t i=0; i<_names.size(); i++)
24 file << _names[i] << "\t";
25 file << "\n";
26
27 if (dimworld==1) {
28#if !NDEBUG
29 int counter = 0;
30#endif
31 typedef typename GridView::template Codim<0>::Iterator CellIterator;
32 CellIterator it = _gv.template begin<0>();
33 CellIterator end = _gv.template end<0>();
34 for (; it != end; ++it)
35 {
36 int i = _is.index(*it);
37 // check that the elements are numbered consecutively
38 assert (i == counter++);
39 // calc positions
40 assert(it->geometry().corners() == 2);
41 const FieldVector<ctype,dimworld>& left = it->geometry().corner(0);
42 const FieldVector<ctype,dimworld>& right = it->geometry().corner(1);
43 assert(left[0] < right[0]);
44 // write gnuplot rows for left & right vertex
45 writeRow(file, left, _data[2*i]);
46 writeRow(file, right, _data[2*i+1]);
47 }
48
49 } else {
50
51 typedef typename GridView::template Codim<dimworld>::Iterator VertexIterator;
52 VertexIterator it = _gv.template begin<dimworld>();
53 VertexIterator end = _gv.template end<dimworld>();
54 for (; it != end; ++it) {
55
56 // write gnuplot rows for vertex
57 writeRow(file, it->geometry().corner(0), _data[_is.index(*it)]);
58
59 }
60
61 }
62
63 }
64
65 template<class GridView>
66 void
68 const FieldVector<ctype,dimworld>& position,
69 const std::vector<float> & data) const
70 {
71 assert (data.size() == _names.size());
72 // write position
73 file << position << "\t";
74 // write all data columns
75 for (size_t j=0; j<data.size(); j++)
76 file << data[j] << "\t";
77 file << "\n";
78 }
79
85 template<class GridView>
86 template<class DataContainer>
87 void
88 GnuplotWriter<GridView>::addData(DataType t, const DataContainer& data, const std::string & name)
89 {
90 assert((t == cellData && _is.size(0) == data.size())
91 || (t == vertexData && _is.size(GridView::dimension) == data.size()) );
92 _names.push_back(name);
93
94 // copy data to new container
95
96 if (dimworld==1) {
97
98 // data is transformed to nonconforming vertex data
99 int c = 0;
100 int shift = (t==vertexData ? 1 : 0);
101 for (size_t i=0; i<_is.size(0); i++)
102 {
103 _data[c++].push_back(data[i]);
104 _data[c++].push_back(data[i+shift]);
105 };
106
107 } else {
108
109 // 2d: only vertex data is allowed
110 for (size_t i=0; i<_is.size(dimworld); i++)
111 _data[i].push_back(data[i]);
112
113 }
114
115 }
116
117}
@ dimension
The dimension of the grid.
Definition common/gridview.hh:128
Include standard header files.
Definition agrid.hh:60
Writer for 1D grids in gnuplot format.
Definition gnuplot.hh:28
void write(const std::string &filename) const
Write Gnuplot file to disk.
Definition gnuplot.cc:17