PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
writer.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 "writer.h"
12#include "util/io.h"
13#include <stdexcept>
14#include "vtkWriter.h"
15#include "legacyVtkWriter.h"
16#include "mshWriter.h"
17
19 : d_vtkWriter_p(nullptr), d_legacyVtkWriter_p(nullptr),
20 d_mshWriter_p(nullptr), d_format("vtu") {}
21
22rw::writer::Writer::Writer(const std::string &filename,
23 const std::string &format,
24 const std::string &compress_type)
25 : d_vtkWriter_p(nullptr), d_legacyVtkWriter_p(nullptr),
26 d_mshWriter_p(nullptr), d_format("vtu") {
27 open(filename, format, compress_type);
28}
29
30void rw::writer::Writer::open(const std::string &filename,
31 const std::string &format,
32 const std::string &compress_type) {
33 d_format = format;
34 if (d_format == "vtu")
35 d_vtkWriter_p = new rw::writer::VtkWriter(filename, compress_type);
36 else if (d_format == "msh")
37 d_mshWriter_p = new rw::writer::MshWriter(filename, compress_type);
38 else if (d_format == "legacy_vtk")
39 d_legacyVtkWriter_p =
40 new rw::writer::LegacyVtkWriter(filename, compress_type);
41 else {
42 throw std::runtime_error(
44 << "File format " << d_format << " is not supported.\n");
45 }
46}
47
49 delete (d_vtkWriter_p);
50}
51
53 const std::vector<util::Point> *nodes,
54 const std::vector<util::Point> *u) {
55
56 if (d_format == "vtu")
57 d_vtkWriter_p->appendNodes(nodes, u);
58 else if (d_format == "msh")
59 d_mshWriter_p->appendNodes(nodes, u);
60 else if (d_format == "legacy_vtk")
61 d_legacyVtkWriter_p->appendNodes(nodes, u);
62}
63
65 const std::vector<util::Point> *nodes, const size_t &element_type,
66 const std::vector<size_t> *en_con, const std::vector<util::Point> *u) {
67
68 if (d_format == "vtu")
69 d_vtkWriter_p->appendMesh(nodes, element_type, en_con, u);
70 else if (d_format == "msh")
71 d_mshWriter_p->appendMesh(nodes, element_type, en_con, u);
72 else if (d_format == "legacy_vtk")
73 d_legacyVtkWriter_p->appendMesh(nodes, element_type, en_con, u);
74}
75
77 const std::string &name, const std::vector<uint8_t> *data) {
78
79 if (d_format == "vtu")
80 d_vtkWriter_p->appendPointData(name, data);
81 else if (d_format == "msh")
82 d_mshWriter_p->appendPointData(name, data);
83 else if (d_format == "legacy_vtk")
84 d_legacyVtkWriter_p->appendPointData(name, data);
85}
86
88 const std::string &name, const std::vector<size_t> *data) {
89
90 if (d_format == "vtu")
91 d_vtkWriter_p->appendPointData(name, data);
92 else if (d_format == "msh")
93 d_mshWriter_p->appendPointData(name, data);
94 else if (d_format == "legacy_vtk")
95 d_legacyVtkWriter_p->appendPointData(name, data);
96}
97
99 const std::string &name, const std::vector<int> *data) {
100
101 if (d_format == "vtu")
102 d_vtkWriter_p->appendPointData(name, data);
103 else if (d_format == "msh")
104 d_mshWriter_p->appendPointData(name, data);
105 else if (d_format == "legacy_vtk")
106 d_legacyVtkWriter_p->appendPointData(name, data);
107}
108
110 const std::string &name, const std::vector<float> *data) {
111
112 if (d_format == "vtu")
113 d_vtkWriter_p->appendPointData(name, data);
114 else if (d_format == "msh")
115 d_mshWriter_p->appendPointData(name, data);
116 else if (d_format == "legacy_vtk")
117 d_legacyVtkWriter_p->appendPointData(name, data);
118}
119
121 const std::string &name, const std::vector<double> *data) {
122
123 if (d_format == "vtu")
124 d_vtkWriter_p->appendPointData(name, data);
125 else if (d_format == "msh")
126 d_mshWriter_p->appendPointData(name, data);
127 else if (d_format == "legacy_vtk")
128 d_legacyVtkWriter_p->appendPointData(name, data);
129}
130
132 const std::string &name, const std::vector<util::Point> *data) {
133
134 if (d_format == "vtu")
135 d_vtkWriter_p->appendPointData(name, data);
136 else if (d_format == "msh")
137 d_mshWriter_p->appendPointData(name, data);
138 else if (d_format == "legacy_vtk")
139 d_legacyVtkWriter_p->appendPointData(name, data);
140}
141
143 const std::string &name, const std::vector<util::SymMatrix3> *data) {
144
145 if (d_format == "vtu")
146 d_vtkWriter_p->appendPointData(name, data);
147 else if (d_format == "msh")
148 d_mshWriter_p->appendPointData(name, data);
149 else if (d_format == "legacy_vtk")
150 d_legacyVtkWriter_p->appendPointData(name, data);
151}
152
154 const std::string &name, const std::vector<float> *data) {
155
156 if (d_format == "vtu")
157 d_vtkWriter_p->appendCellData(name, data);
158 else if (d_format == "msh")
159 d_mshWriter_p->appendCellData(name, data);
160 else if (d_format == "legacy_vtk")
161 d_legacyVtkWriter_p->appendCellData(name, data);
162}
163
165 const std::string &name, const std::vector<util::SymMatrix3> *data) {
166
167 if (d_format == "vtu")
168 d_vtkWriter_p->appendCellData(name, data);
169 else if (d_format == "msh")
170 d_mshWriter_p->appendCellData(name, data);
171 else if (d_format == "legacy_vtk")
172 d_legacyVtkWriter_p->appendCellData(name, data);
173}
174
175void rw::writer::Writer::addTimeStep(const double &timestep) {
176
177 if (d_format == "vtu")
178 d_vtkWriter_p->addTimeStep(timestep);
179 else if (d_format == "msh")
180 d_mshWriter_p->addTimeStep(timestep);
181 else if (d_format == "legacy_vtk")
182 d_legacyVtkWriter_p->addTimeStep(timestep);
183}
184
185void rw::writer::Writer::appendFieldData(const std::string &name,
186 const double &data) {
187
188 if (d_format == "vtu")
189 d_vtkWriter_p->appendFieldData(name, data);
190 else if (d_format == "msh")
191 d_mshWriter_p->appendFieldData(name, data);
192 else if (d_format == "legacy_vtk")
193 d_legacyVtkWriter_p->appendFieldData(name, data);
194}
195
196void rw::writer::Writer::appendFieldData(const std::string &name,
197 const float &data) {
198
199 if (d_format == "vtu")
200 d_vtkWriter_p->appendFieldData(name, data);
201 else if (d_format == "msh")
202 d_mshWriter_p->appendFieldData(name, data);
203 else if (d_format == "legacy_vtk")
204 d_legacyVtkWriter_p->appendFieldData(name, data);
205}
206
208
209 if (d_format == "vtu")
210 d_vtkWriter_p->close();
211 else if (d_format == "msh")
212 d_mshWriter_p->close();
213 else if (d_format == "legacy_vtk")
214 d_legacyVtkWriter_p->close();
215}
A vtk writer for simple point data and complex fem mesh data.
A .msh writer for simple point data and complex fem mesh data.
Definition mshWriter.h:29
A vtk writer for simple point data and complex fem mesh data.
Definition vtkWriter.h:28
void appendCellData(const std::string &name, const std::vector< float > *data)
Writes the float data associated to cells to the file.
Definition writer.cpp:153
void appendFieldData(const std::string &name, const double &data)
Writes the scalar field data to the file.
Definition writer.cpp:185
void appendNodes(const std::vector< util::Point > *nodes, const std::vector< util::Point > *u=nullptr)
Writes the nodes to the file.
Definition writer.cpp:52
void addTimeStep(const double &timestep)
Writes the time step to the file.
Definition writer.cpp:175
void appendPointData(const std::string &name, const std::vector< uint8_t > *data)
Writes the scalar point data to the file.
Definition writer.cpp:76
~Writer()
Destructor.
Definition writer.cpp:48
void open(const std::string &filename, const std::string &format="vtu", const std::string &compress_type="")
Open a .vtu file.
Definition writer.cpp:30
Writer()
Constructor.
Definition writer.cpp:18
void close()
Closes the file and store it to the hard disk.
Definition writer.cpp:207
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 writer.cpp:64
Collects a message with stream syntax for use in an exception.
Definition io.h:52
Definition contact.h:20