PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
vtkWriter.cpp
Go to the documentation of this file.
1/*
2 * -------------------------------------------
3 * Copyright (c) 2021 - 2026 Prashant K. Jha
4 * -------------------------------------------
5 * PeriDEM https://github.com/prashjha/PeriDEM
6 *
7 * Distributed under the Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE)
9 */
10
11#include "vtkWriter.h"
12#include <util/feElementDefs.h>
13#include <vtkCellArray.h>
14#include <vtkCellData.h>
15#include <vtkDoubleArray.h>
16#include <vtkIdList.h>
17#include <vtkIntArray.h>
18#include <vtkPointData.h>
19#include <vtkPoints.h>
20#include <vtkUnsignedCharArray.h>
21#include <vtkUnsignedIntArray.h>
22
23rw::writer::VtkWriter::VtkWriter(const std::string &filename,
24 const std::string &compress_type)
25 : d_compressType(compress_type) {
26
27 std::string f = filename + ".vtu";
28
29 d_writer_p = vtkSmartPointer<vtkXMLUnstructuredGridWriter>::New();
30 d_writer_p->SetFileName(const_cast<char *>(f.c_str()));
31}
32
33void rw::writer::VtkWriter::appendNodes(const std::vector<util::Point> *nodes,
34 const std::vector<util::Point> *u) {
35
36 auto points = vtkSmartPointer<vtkPoints>::New();
37
38 for (size_t i = 0; i < nodes->size(); i++) {
39
40 util::Point p = (*nodes)[i];
41 if (u)
42 p = p + (*u)[i];
43 points->InsertNextPoint(p.d_x, p.d_y, p.d_z);
44 }
45
46 d_grid_p = vtkSmartPointer<vtkUnstructuredGrid>::New();
47 d_grid_p->SetPoints(points);
48}
49
51 const std::vector<util::Point> *nodes, const size_t &element_type,
52 const std::vector<size_t> *en_con,
53 const std::vector<util::Point> *u) {
54
55 // we write following things to the file
56 //
57 // Node data
58 // 1. Coordinates of nodes (current)
59 //
60 // Element data
61 // 1. element node connectivity
62 // 2. element type (either triangle or square)
63
64 // add current position of nodes
65 this->appendNodes(nodes, u);
66
67 // get the total number of elements
68 size_t num_vertex = util::vtk_map_element_to_num_nodes[element_type];
69 size_t num_elems = en_con->size() / num_vertex;
70
71 //
72 // process elements data
73 //
74 // element node connectivity
75 auto cells = vtkSmartPointer<vtkCellArray>::New();
76 cells->AllocateEstimate(static_cast<vtkIdType>(num_elems), static_cast<vtkIdType>(num_vertex));
77
78 auto cellTypeArray = vtkSmartPointer<vtkUnsignedCharArray>::New();
79 cellTypeArray->SetNumberOfValues(static_cast<vtkIdType>(num_elems));
80
81 vtkIdType ids[8];
82 for (size_t i = 0; i < num_elems; i++) {
83
84 // get ids of vertex of this element
85 for (size_t k = 0; k < num_vertex; k++)
86 ids[k] = (*en_con)[num_vertex*i + k];
87
88 cells->InsertNextCell(static_cast<int>(num_vertex), ids);
89 cellTypeArray->SetValue(static_cast<vtkIdType>(i), static_cast<unsigned char>(element_type));
90 }
91
92 d_grid_p->SetCells(cellTypeArray, cells);
93}
94
95void rw::writer::VtkWriter::appendPointData(const std::string &name,
96 const std::vector<uint8_t> *data) {
97
98 auto array = vtkSmartPointer<vtkDoubleArray>::New();
99 array->SetNumberOfComponents(1);
100 array->SetName(name.c_str());
101
102 double value[1];
103 for (unsigned char i : *data) {
104 value[0] = i;
105 array->InsertNextTuple(value);
106 }
107
108 d_grid_p->GetPointData()->AddArray(array);
109}
110
111void rw::writer::VtkWriter::appendPointData(const std::string &name,
112 const std::vector<size_t> *data) {
113
114 auto array = vtkSmartPointer<vtkDoubleArray>::New();
115 array->SetNumberOfComponents(1);
116 array->SetName(name.c_str());
117
118 double value[1];
119 for (unsigned long i : *data) {
120 value[0] = i;
121 array->InsertNextTuple(value);
122 }
123
124 d_grid_p->GetPointData()->AddArray(array);
125}
126
127void rw::writer::VtkWriter::appendPointData(const std::string &name,
128 const std::vector<int> *data) {
129
130 auto array = vtkSmartPointer<vtkDoubleArray>::New();
131 array->SetNumberOfComponents(1);
132 array->SetName(name.c_str());
133
134 double value[1];
135 for (int i : *data) {
136 value[0] = i;
137 array->InsertNextTuple(value);
138 }
139
140 d_grid_p->GetPointData()->AddArray(array);
141}
142
143void rw::writer::VtkWriter::appendPointData(const std::string &name,
144 const std::vector<float> *data) {
145
146 auto array = vtkSmartPointer<vtkDoubleArray>::New();
147 array->SetNumberOfComponents(1);
148 array->SetName(name.c_str());
149
150 double value[1];
151 for (float i : *data) {
152 value[0] = i;
153 array->InsertNextTuple(value);
154 }
155
156 d_grid_p->GetPointData()->AddArray(array);
157}
158
159void rw::writer::VtkWriter::appendPointData(const std::string &name,
160 const std::vector<double> *data) {
161
162 auto array = vtkSmartPointer<vtkDoubleArray>::New();
163 array->SetNumberOfComponents(1);
164 array->SetName(name.c_str());
165
166 double value[1];
167 for (double i : *data) {
168 value[0] = i;
169 array->InsertNextTuple(value);
170 }
171
172 d_grid_p->GetPointData()->AddArray(array);
173}
174
176 const std::string &name, const std::vector<util::Point> *data) {
177
178 auto array = vtkSmartPointer<vtkDoubleArray>::New();
179 array->SetNumberOfComponents(3);
180 array->SetName(name.c_str());
181
182 array->SetComponentName(0, "x");
183 array->SetComponentName(1, "y");
184 array->SetComponentName(2, "z");
185
186 double value[3];
187 for (const auto &i : *data) {
188 value[0] = i.d_x;
189 value[1] = i.d_y;
190 value[2] = i.d_z;
191 array->InsertNextTuple(value);
192 }
193
194 d_grid_p->GetPointData()->AddArray(array);
195}
196
198 const std::string &name, const std::vector<util::SymMatrix3> *data) {
199
200 auto array = vtkSmartPointer<vtkDoubleArray>::New();
201 array->SetNumberOfComponents(6);
202 array->SetName(name.c_str());
203
204 array->SetComponentName(0, "xx");
205 array->SetComponentName(1, "yy");
206 array->SetComponentName(2, "zz");
207 array->SetComponentName(3, "yz");
208 array->SetComponentName(4, "xz");
209 array->SetComponentName(5, "xy");
210
211 double value[6];
212 for (const auto &i : *data) {
213 value[0] = i(0,0);
214 value[1] = i(1,1);
215 value[2] = i(2,2);
216 value[3] = i(1,2);
217 value[4] = i(0,2);
218 value[5] = i(0,1);
219 array->InsertNextTuple(value);
220 }
221
222 d_grid_p->GetPointData()->AddArray(array);
223}
224
225void rw::writer::VtkWriter::appendCellData(const std::string &name,
226 const std::vector<float> *data) {
227 auto array = vtkSmartPointer<vtkDoubleArray>::New();
228 array->SetNumberOfComponents(1);
229 array->SetName(name.c_str());
230
231 double value[1];
232 for (float i : *data) {
233 value[0] = i;
234 array->InsertNextTuple(value);
235 }
236
237 d_grid_p->GetCellData()->AddArray(array);
238}
239
241 const std::string &name, const std::vector<util::SymMatrix3> *data) {
242
243 auto array = vtkSmartPointer < vtkDoubleArray > ::New();
244 array->SetNumberOfComponents(6);
245 array->SetName(name.c_str());
246
247 array->SetComponentName(0, "xx");
248 array->SetComponentName(1, "yy");
249 array->SetComponentName(2, "zz");
250 array->SetComponentName(3, "yz");
251 array->SetComponentName(4, "xz");
252 array->SetComponentName(5, "xy");
253
254 double value[6];
255 for (const auto &i : *data) {
256 value[0] = i(0,0);
257 value[1] = i(1,1);
258 value[2] = i(2,2);
259 value[3] = i(1,2);
260 value[4] = i(0,2);
261 value[5] = i(0,1);
262 array->InsertNextTuple(value);
263 }
264
265 d_grid_p->GetCellData()->AddArray(array);
266}
267
268void rw::writer::VtkWriter::addTimeStep(const double &timestep) {
269
270 auto t = vtkDoubleArray::New();
271 t->SetName("TIME");
272 t->SetNumberOfTuples(1);
273 t->SetTuple1(0, timestep);
274 d_grid_p->GetFieldData()->AddArray(t);
275}
276
278 d_writer_p->SetInputData(d_grid_p);
279 d_writer_p->SetDataModeToAppended();
280 // Raw appended binary — VTK 9.1 rejects base64 EncodeAppendedDataOn().
281 d_writer_p->EncodeAppendedDataOff();
282 if (d_compressType == "zlib")
283 d_writer_p->SetCompressorTypeToZLib();
284 else
285 d_writer_p->SetCompressor(0);
286 d_writer_p->Write();
287}
288
289void rw::writer::VtkWriter::appendFieldData(const std::string &name,
290 const double &data) {
291
292 auto t = vtkDoubleArray::New();
293 t->SetName(name.c_str());
294 t->SetNumberOfTuples(1);
295 t->SetTuple1(0, data);
296 d_grid_p->GetFieldData()->AddArray(t);
297}
298
299void rw::writer::VtkWriter::appendFieldData(const std::string &name,
300 const float &data) {
301
302 auto t = vtkDoubleArray::New();
303 t->SetName(name.c_str());
304 t->SetNumberOfTuples(1);
305 t->SetTuple1(0, data);
306 d_grid_p->GetFieldData()->AddArray(t);
307}
void appendFieldData(const std::string &name, const double &data)
Writes the scalar field data to the file.
void close()
Closes the file and store it to the hard disk.
void appendCellData(const std::string &name, const std::vector< float > *data)
Writes the float data associated to cells to the file.
vtkSmartPointer< vtkXMLUnstructuredGridWriter > d_writer_p
XML unstructured grid writer.
Definition vtkWriter.h:188
void addTimeStep(const double &timestep)
Writes the time step to the file.
void appendNodes(const std::vector< util::Point > *nodes, const std::vector< util::Point > *u=nullptr)
Writes the nodes to the file.
Definition vtkWriter.cpp:33
VtkWriter(const std::string &filename, const std::string &compress_type="")
Constructor.
Definition vtkWriter.cpp:23
void appendMesh(const std::vector< util::Point > *nodes, const size_t &element_type, const std::vector< size_t > *en_con, const std::vector< util::Point > *u=nullptr)
Writes the mesh data to file.
Definition vtkWriter.cpp:50
void appendPointData(const std::string &name, const std::vector< uint8_t > *data)
Writes the scalar point data to the file.
Definition vtkWriter.cpp:95
static int vtk_map_element_to_num_nodes[16]
Map from element type to number of nodes (for vtk)
Definition contact.h:20
A structure to represent 3d vectors.
Definition point.h:30
double d_y
the y coordinate
Definition point.h:36
double d_z
the z coordinate
Definition point.h:39
double d_x
the x coordinate
Definition point.h:33