VtuPlot.cpp 6.11 KB
Newer Older
1
#include "VtuPlot.hpp"
2
#include "Config.hpp"
jonathan.froehlich's avatar
jonathan.froehlich committed
3

4
5
#include <fstream>

6

7
using namespace std;
jonathan.froehlich's avatar
jonathan.froehlich committed
8

9
int vtkCellType(CELLTYPE cType) {
10
  switch (cType) {
11
12
    case INTERVAL:
      return 3;
13
14
15
16
17
18
19
20
21
22
23
    case TRIANGLE:
      return 5;
    case QUADRILATERAL:
      return 9;
    case TETRAHEDRON:
      return 10;
    case HEXAHEDRON:
      return 12;
    default:
      return -1;
  }
jonathan.froehlich's avatar
jonathan.froehlich committed
24
25
26
}

string VtuPlot::plotIndent(int i) {
27
28
29
30
  string indent;
  for (int j = 0; j < i; j++)
    indent += standardIndent;
  return indent;
jonathan.froehlich's avatar
jonathan.froehlich committed
31
32
33
}

string VtuPlot::plotLineBreak(int i, int limit, int indent) {
34
  return (i % limit != 0 ? " " : ("\n" + plotIndent(indent)));
jonathan.froehlich's avatar
jonathan.froehlich committed
35
36
}

37
void VtuPlot::createVtu(const string &filename, const string& filecontent) {
38
39
  string filepath = dataPath + filename + string(".vtu");
  std::ofstream out(filepath.c_str());
40
  out << FileString() << std::endl;
41
42
43
44
}

string VtuPlot::FileString() {
  std::stringstream out;
45
46

  // == Header ==
47
48
  out << R"(<VTKFile type ="UnstructuredGrid" version="0.1" byte_order="LittleEndian">)" << "\n"
      << plotIndent(1) << "<UnstructuredGrid>" << "\n"
49
      << plotIndent(2) << "<Piece NumberOfPoints=\"" << pointN
50
      << "\" NumberOfCells=\"" << cellN << "\">" << "\n";
51
52

  // == Points ==
53
  out << plotIndent(3) << "<Points>" << "\n"
54
55
56
57
58
      << plotIndent(4) << R"(<DataArray type="Float32" NumberOfComponents="3" format="ascii">)";
  for (int i = 0; i < pointN; i++) {
    out << plotLineBreak(i, lineBreakP, 5);
    plotPoint(out, points.at(i) + displacement.at(i));
  }
59
60
61
  out << "\n"
      << plotIndent(4) << "</DataArray>" << "\n"
      << plotIndent(3) << "</Points>" << "\n";
62
63
64
65
66
67
68
69
70
71

  // == PointData ==
  out << plotIndent(3) << "<PointData";
  if (!activeArrays.first.empty()) {
    string aArray = " ";
    aArray += (pointDataSize.at(activeArrays.first) > 1 ? R"(Vectors=")" : R"(Scalars=")");
    aArray += activeArrays.first;
    aArray += R"(")";
    out << aArray;
  }
72
  out << ">" << "\n";
73
74
75
76
77
78
79
80
  for (auto pData : pointData) {
    if (pData.first == "ID") continue;

    int dataSize = pointDataSize.at(pData.first);
    out << plotIndent(4)
        << R"(<DataArray type="Float32" Name=")" << pData.first
        << R"(" NumberOfComponents=")" << dataSize
        << R"(" format="ascii">)";
jonathan.froehlich's avatar
jonathan.froehlich committed
81
    for (int i = 0; i < pointN; i++) {
82
83
84
      for (int j = 0; j < dataSize; j++)
        out << plotLineBreak(i + j, lineBreakZ, 5)
            << to_string(pData.second->at(points[i]).GetData(j));
jonathan.froehlich's avatar
jonathan.froehlich committed
85
    }
86
    out << "\n" << plotIndent(4) << "</DataArray>" << "\n";
87
  }
88
  out << plotIndent(3) << "</PointData>" << "\n";
89
90

  // == Cells ==
91
  out << plotIndent(3) << "<Cells>" << "\n"
92
      << plotIndent(4) << R"(<DataArray type="Int64" Name="connectivity" format="ascii">)"
93
94
      << cellConnectivity << "\n"
      << plotIndent(4) << "</DataArray>" << "\n";
95
  out << plotIndent(4) << R"(<DataArray type="Int64" Name="offsets" format="ascii">)"
96
97
      << cellOffset << "\n"
      << plotIndent(4) << "</DataArray>" << "\n";
98
  out << plotIndent(4) << R"(<DataArray type="Int32" Name="types" format="ascii">)"
99
100
101
      << cellTypes << "\n"
      << plotIndent(4) << "</DataArray>" << "\n"
      << plotIndent(3) << "</Cells>" << "\n";
102
103
104
105
106

  // == CellData ==
  out << plotIndent(3) << "<CellData";
  if (!activeArrays.second.empty()) {
    string aArray = " ";
107
    aArray += (cellDataSize.at(activeArrays.second) > 1 ? R"(Vectors=")" : R"(Scalars=")");
108
109
110
111
    aArray += activeArrays.second;
    aArray += R"(")";
    out << aArray;
  }
112
  out << ">" << "\n";
113
114
115
116
117
118
119
  for (const auto &cData : cellData) {
    if (cData.first == "ID") continue;

    out << plotIndent(4)
        << R"(<DataArray type="Float32" Name=")" << cData.first
        << R"(" NumberOfComponents=")" << cellDataSize.at(cData.first)
        << R"(" format="ascii">)";
120
121

    int dataSize = cellDataSize.at(cData.first);
122
    for (int i = 0; i < cellN; i++) {
123
124
125
      for (int j = 0; j < dataSize; j++)
        out << plotLineBreak(i + j, lineBreakZ, 5)
            << to_string(cData.second->at(cells[i]).GetData(j));
jonathan.froehlich's avatar
jonathan.froehlich committed
126
    }
127
    out << "\n" << plotIndent(4) << "</DataArray>" << "\n";
128
  }
129
  out << plotIndent(3) << "</CellData>" << "\n";
130
  // == End of File ==
131
132
  out << plotIndent(2) << "</Piece>" << "\n"
      << plotIndent(1) << "</UnstructuredGrid>" << "\n"
133
134
135
      << "</VTKFile>";

  return out.str();
jonathan.froehlich's avatar
jonathan.froehlich committed
136
137
}

138
139
140
141
142
void VtuPlot::Erase() {
  ClearData();
  ClearMesh();
}

143
void VtuPlot::ClearData() {
144
145
146
147
148
149
150
151
152
153
154
155
  vector<string> dataToDrop;
  for (const auto &pData : pointData) {
    if (pData.first == "ID") continue;
    dataToDrop.push_back(pData.first);
  }
  for (const auto &dataName : dataToDrop) {
    pointData.erase(dataName);
    pointDataSize.erase(dataName);
  }

  dataToDrop.clear();
  for (const auto &cData : cellData) {
156
    if (std::string{"IDSubdomainProcLoad"}.find(cData.first) != std::string::npos) continue;
157
158
159
160
161
162
163
164
    dataToDrop.push_back(cData.first);
  }
  for (const auto &dataName : dataToDrop) {
    cellData.erase(dataName);
    cellDataSize.erase(dataName);
  }

  activeArrays = {"", ""};
165
166
  for (auto & i : displacement) {
    i.second = Origin;
167
  }
jonathan.froehlich's avatar
jonathan.froehlich committed
168
}
169

170
171
172
173
174
175
176
177
178
179
void VtuPlot::ClearMesh(){
  displacement.clear();
  pointData.erase("ID");
  cellData.erase("ID");
  cellData.erase("Subdomain");
  cellData.erase("ProcLoad");

  plotMesh.Reset();
}

180
void VtuPlot::plotPoint(std::ostream &os, const Point &p) {
181
182
183
184
185
  os << p[0];
  for (int i = 1; i < SpaceDimension; ++i)
    os << " " << p[i];
  for (int i = SpaceDimension; i < 3; ++i)
    os << " 0.0";
186
}
187

188
189
190
191
192
193
194
195
void VtuPlot::plotData(std::ostream &os, const Point &p) {
  os << p[0];
  for (int i = 1; i < SpaceDimension; ++i)
    os << " " << p[i];
  for (int i = SpaceDimension; i < 3; ++i)
    os << " 0.0";
}

196
197
198
199
200
201
202
203
204
205
void VtuPlot::Flush(PlotStatus status) {
  if(status==PlotStatus::FLUSH){
    ClearData();
  }
  if(status==PlotStatus::CLEAR){
    Erase();
  }
}


206
VtuPlot &operator<<(VtuPlot &plot, const Vector &v) {
207
  plot.AddData("value" + std::to_string(plot.DataCount()), v);
208
209
210
211
  return plot;
}

VtuPlot &operator<<(VtuPlot &plot, PlotStatus status) {
212
213
  plot.PlotFile();
  plot.Flush(status);
214
215
216
217
  return plot;
}

VtuPlot &operator<<(VtuPlot &plot, const string &name) {
218
  plot.FileName(name);
219
220
221
  return plot;
}

222
223
VtuPlot &operator<<(VtuPlot &plot, const std::pair<std::string, PlotStatus>& saveoptions) {
  return plot << saveoptions.first << saveoptions.second;
224
}