VtuPlot.hpp 6.76 KB
Newer Older
1
2
3
#ifndef VTUPLOT_H
#define VTUPLOT_H

4
#include "Algebra.hpp"
5

6
enum PlotStatus {
7
  CLEAR,
8
9
10
11
  FLUSH,
  SAVE
};

12
13

class PointData {
14
  vector<double> data;
15
public:
16
  explicit PointData(int dim = 1) : data(dim) {}
17

18
19
20
21
  PointData(const vector<double> &d) : data(d.size()) {
    for (int i = 0; i < d.size(); i++)
      data[i] = d[i];
  }
22

23
  void AddData(int index, double d) { data[index] = d; }
24

25
  double GetData(int i) { return data.at(i); }
26

27
28
  // TODO: Research vector.back()
  double PlotData() { return data.back(); }
29
30
};

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
 * Helper Class to allow lazy initialization of Mesh references.
 * PlotMesh is never responsible for cleaning up the underlying pointer!
 */
class PlotMesh {
  const Mesh *mesh = nullptr;
public:
  PlotMesh() = default;

  explicit PlotMesh(const Mesh &other) {
    (*this).mesh = &other;
  }

  void operator()(const Mesh &other) {
    (*this).mesh = &other;
  }

  const Mesh &operator()() const {
    return *mesh;
  }

  bool IsInstatiated() const {
    return mesh != nullptr;
  }
55
56
57
58

  void Reset(){
    mesh = nullptr;
  }
59
60
};

61
62
63
64
65
/**
 * A VtuPlotter Object is responsible for creating .vtu files using vtks XML-format.
 */
class VtuPlot {
protected:
66
  string dataPath = Config::getDataPath() + "vtu/";
67
  string plotName{"default"};
68

69
  PlotMesh plotMesh;
70

71
72
  int pointN = 0;
  int cellN = 0;
73

74
75
76
  string cellConnectivity{""};
  string cellOffset{""};
  string cellTypes{""};
77

78
  std::pair<string, string> activeArrays = {"", ""};
79

80
81
  std::unordered_map<int, Point> points{};
  std::unordered_map<int, Point> displacement{};
82

83
84
  std::unordered_map<string, std::unordered_map<Point, PointData> *> pointData{};
  std::unordered_map<string, int> pointDataSize{};
85

86
87
88
  std::unordered_map<int, Point> cells{};
  std::unordered_map<string, std::unordered_map<Point, PointData> *> cellData{};
  std::unordered_map<string, int> cellDataSize{};
89

90
91
92
93
94
  // == Default linebreak and indentation arguments ==
  int lineBreakP = 5;
  int lineBreakC = 8;
  int lineBreakZ = 16;
  string standardIndent = "  ";
95

96
  string plotIndent(int i);
97

98
  string plotLineBreak(int i, int limit, int indent = 0);
99

100
101
  void plotPoint(std::ostream &os, const Point &p);
  // =================================================
102

103
  void createVtu(const string &filename, const string& filecontent);
104
105


106
107
108
  int pointID(const Point &p) {
    return (int) pointData["ID"]->at(p).GetData(0);
  }
109
110

public:
111
112
113
  VtuPlot() : plotMesh() {}

  explicit VtuPlot(const Mesh &init) : plotMesh(init){};
114
115
116
117

  /// Creates a Plotter where the Vector init defines the Grid on which the VtuPlotter operates.
  explicit VtuPlot(const Vector &init) : VtuPlot(init.GetMesh()) {};

118
119
120
121
  int DataCount() {
    return pointData.size() + cellData.size();
  }

122
123
  void FileName(const string &name) { plotName = name; }
  const std::string& FileName() const { return plotName; }
124

125
126
127
128
129
130
131
132
133
134
135
136
137
  void ChangeDataPath(const string &dataPath) {
    this->dataPath = dataPath;
  }

  /**
   * Adds a deformation on all Points of the grid. The initial Grid is not distorted, the deformation
   * is applied at the time of plotting.
   *
   * @param deformation Vector containing the displacement of all points (not the new location, just the
   *                      difference from their stating position).
   */
  virtual void DeformGrid(const Vector &diplacement) = 0;

138
  virtual void AddData(const string &name, const Vector& data) = 0;
139
140
141
142
143
144
145
  /**
   * Adds a PointData Container to the plotted file.
   *
   * @param name Name of the DataContainer. This name is shown in Paraview when selecting the specified vector.
   * @param data Contains the data to be plotted.
   * @param dim
   */
146
 //virtual void AddPointData(const string &name, const Vector &data) = 0;
147
148
149
150
151
152
153
154
155

  /**
   * Adds a CellData Container to the plotted file. As a Vector typically stores values in the vertices of the mesh,
   * a cell average is calculated.
   *
   * @param name Name of the DataContainer. This name is shown in Paraview when selecting the specified vector.
   * @param data Contains the data to be plotted.
   * @param dim
   */
156
  //virtual void AddCellData(const string &name, const Vector &data) = 0;
157
158
159
160
161
162
163
164

  /**
   * Adds a CellData Container containing the subdomain of each cell.
   *
   * @param grid Vector containing the current mesh, including possibly changed subdomains
   */
  virtual void AddSubdomain(const Mesh &grid) = 0;

165
166
  void AddSubdomain() { AddSubdomain(plotMesh()); }

167
168
  void AddSubdomain(const Vector &gridVector) { AddSubdomain(gridVector.GetMesh()); }

169
170
171
172
173
174
  virtual void AddProcLoad(const Mesh &grid) = 0;

  void AddProcLoad() { AddSubdomain(plotMesh()); }

  void AddProcLoad(const Vector &gridVector) { AddSubdomain(gridVector.GetMesh()); }

175
  virtual string FileString();
176
177
  virtual void PlotFile(const string &filename) = 0;
  virtual void PlotFile() { PlotFile(plotName); };
178
179

  void ClearData();
180
181
182
  void ClearMesh();
  void Erase();
  void Flush(PlotStatus s);
183
184

  ~VtuPlot() {
185
    ClearData();
186
  }
187

188
  void plotData(std::ostream &os, const Point &p);
189
190
191
};

class SerialVtuPlot : public VtuPlot {
192
  void initialize(const Mesh& init);
193
194
  void initPoints(const Mesh &init);
  void initCells(const Mesh &init);
195

196
  void checkMesh(const Mesh& init);
197
198
199

  void addPointData(const string& name, const Vector& data);
  void addCellData(const string& name, const Vector& data);
200
public:
201
202

  SerialVtuPlot();
203
  explicit SerialVtuPlot(const Mesh &init);
204

205
206
  /// Creates a Plotter where the Vector init defines the Grid on which the VtuPlotter operates.
  explicit SerialVtuPlot(const Vector &init) : SerialVtuPlot(init.GetMesh()) {}
207

208
209
  void AddData(const string& name, const Vector& data) override;

210
  void PlotFile(const string &filename) override;
211

212
  void DeformGrid(const Vector &diplacement) override;
213

214
  void AddSubdomain(const Mesh &grid) override;
215
216

  void AddProcLoad(const Mesh &grid) override;
217
218

  int dataDim(int dim) const;
219
220
};

221
222
// TODO: Reimplement
/*
223
224
class ParallelVtuPlot : public VtuPlot {

225
  void createMasterFile(const string &filename);
226
227

public:
228
  explicit ParallelVtuPlot(const Mesh &init);
229

230
231
  /// Creates a Plotter where the Vector init defines the Grid on which the VtuPlotter operates.
  explicit ParallelVtuPlot(const Vector &init) : ParallelVtuPlot(init.GetMesh()) {}
232

233
  void PlotFile(const string &filename, bool flush) override;
234

235
  void DeformGrid(const Vector &diplacement) override;
236

237
  void AddPointData(const string &name, const Vector &data, int dim) override;
238

239
  void AddCellData(const string &name, const Vector &data, int dim) override;
240

241
  void AddSubdomain(const Mesh &grid) override;
242
243

  void AddProcLoad(const Mesh &grid) override;
244
};
245
*/
246

247
248
int vtkCellType(CELLTYPE cType);

249
250
251
252
253
VtuPlot &operator<<(VtuPlot &plot, const Vector &v);
VtuPlot &operator<<(VtuPlot &plot, PlotStatus status);
VtuPlot &operator<<(VtuPlot &plot, const string &name);

// Use as global macro to quickly save plot
254
VtuPlot &operator<<(VtuPlot &plot, const std::pair<std::string, PlotStatus>& saveoptions);
255

256
#endif //VTUPLOT_H