PeriDEM 0.2.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
mshWriter.cpp
Go to the documentation of this file.
1// Copyright (c) 2021 Prashant K. Jha
2//
3// Distributed under the GNU GENERAL PUBLIC LICENSE, Version 3.0.
4// (See accompanying file LICENSE.txt)
5
6#include "mshWriter.h"
8#include "util/io.h"
9#include <iostream>
10
11static int ntag = 0;
12static int etag = 0;
13
14//extern std::ofstream msh_out;
15
16rw::writer::MshWriter::MshWriter(const std::string &filename,
17 const std::string &compress_type)
18 : d_compressType(compress_type), d_file(nullptr) {
20}
21
22void rw::writer::MshWriter::writeMshDataHeader(const std::string &name, int field_type, size_t
23num_data, bool is_node_data) {
24
25 // Write metadata
26 if (is_node_data)
27 fprintf(d_file, "$NodeData\n");
28 else
29 fprintf(d_file, "$ElementData\n");
30
31 // number of string the data name has (int)
32 fprintf(d_file, "1\n");
33
34 // name of data (string)
35 fprintf(d_file, "\"%s\"\n", name.c_str());
36
37 // default (number of real number tags) (int and double)
38 fprintf(d_file, "1 \n");
39 fprintf(d_file, "1.0 \n");
40
41 // three tags in integer (ints)
42 fprintf(d_file, "3 \n");
43 if (is_node_data) {
44 fprintf(d_file, "%d\n", ntag);
45 ntag++;
46 } else {
47 fprintf(d_file, "%d\n", etag);
48 etag++;
49 }
50 fprintf(d_file, "%d\n", field_type);
51 fprintf(d_file, "%d\n", int(num_data));
52}
53
54void rw::writer::MshWriter::appendNodes(const std::vector<util::Point> *nodes,
55 const std::vector<util::Point> *u) {
56
57 // open file stream
58 if (!d_file)
59 d_file = fopen(d_filename.c_str(), "w");
60
61 if (!d_file) {
62 std::cerr << "Error: Can not open file = " << d_filename <<".\n";
63 exit(1);
64 }
65
66 // Write the file header.
67 fprintf(d_file, "$MeshFormat\n");
68 fprintf(d_file, "2.0 0 %zu\n", sizeof(double));
69 fprintf(d_file, "$EndMeshFormat\n");
70
71 // get mesh information
72 size_t num_nodes = nodes->size();
73
74 // write the nodes in (n x y z) format
75 fprintf(d_file, "$Nodes\n");
76 fprintf(d_file, "%zu\n", num_nodes);
77
78 for (size_t i=0; i < num_nodes; i++) {
79 auto p = (*nodes)[i];
80 if (u)
81 p = p + (*u)[i];
82 fprintf(d_file, "%zu %lf %lf %lf\n", i + 1, p.d_x, p.d_y, p.d_z);
83 }
84 fprintf(d_file, "$EndNodes\n");
85}
86
88 const std::vector<util::Point> *nodes, const size_t &element_type,
89 const std::vector<size_t> *en_con,
90 const std::vector<util::Point> *u) {
91
92 appendNodes(nodes, u);
93
94 // get mesh information
95 size_t num_vertex = util::vtk_map_element_to_num_nodes[element_type];
96 size_t num_elems = en_con->size() / num_vertex;
97 size_t msh_element_type = util::vtk_to_msh_element_type_map[element_type];
98
99 // write the connectivity
100 fprintf(d_file, "$Elements\n");
101 fprintf(d_file, "%zu\n", num_elems);
102
103 // loop over the elements
104 for (size_t e=0; e < num_elems; e++) {
105
106 // elements ids are 1 based in Gmsh
107 fprintf(d_file, "%zu %zu 2 0 6 ", e+1, msh_element_type);
108
109 // write ids of node (numbering starts with 1)
110 for (size_t v=0; v<num_vertex; v++)
111 fprintf(d_file, "%zu ", (*en_con)[e * num_vertex + v] + 1);
112
113 fprintf(d_file, "\n");
114 } // element loop
115 fprintf(d_file, "$EndElements\n");
116}
117
118void rw::writer::MshWriter::appendPointData(const std::string &name,
119 const std::vector<uint8_t> *data) {
120
121 // Write metadata
122 writeMshDataHeader(name, 1, data->size(), true);
123 for (size_t i=0; i < data->size(); i++) {
124 double d = (*data)[i];
125 fprintf(d_file, "%zu %lf\n", i + 1, d);
126 }
127 fprintf(d_file, "$EndNodeData\n");
128}
129
130void rw::writer::MshWriter::appendPointData(const std::string &name,
131 const std::vector<size_t> *data) {
132
133 // Write metadata
134 writeMshDataHeader(name, 1, data->size(), true);
135 for (size_t i=0; i < data->size(); i++) {
136 double d = (*data)[i];
137 fprintf(d_file, "%zu %lf\n", i + 1, d);
138 }
139 fprintf(d_file, "$EndNodeData\n");
140}
141
142void rw::writer::MshWriter::appendPointData(const std::string &name,
143 const std::vector<int> *data) {
144 // Write metadata
145 writeMshDataHeader(name, 1, data->size(), true);
146 for (size_t i=0; i < data->size(); i++) {
147 double d = (*data)[i];
148 fprintf(d_file, "%zu %lf\n", i + 1, d);
149 }
150 fprintf(d_file, "$EndNodeData\n");
151}
152
153void rw::writer::MshWriter::appendPointData(const std::string &name,
154 const std::vector<float> *data) {
155 // Write metadata
156 writeMshDataHeader(name, 1, data->size(), true);
157 for (size_t i=0; i < data->size(); i++) {
158 double d = (*data)[i];
159 fprintf(d_file, "%zu %lf\n", i + 1, d);
160 }
161 fprintf(d_file, "$EndNodeData\n");
162}
163
164void rw::writer::MshWriter::appendPointData(const std::string &name,
165 const std::vector<double> *data) {
166 // Write metadata
167 writeMshDataHeader(name, 1, data->size(), true);
168 for (size_t i=0; i < data->size(); i++) {
169 double d = (*data)[i];
170 fprintf(d_file, "%zu %lf\n", i + 1, d);
171 }
172 fprintf(d_file, "$EndNodeData\n");
173}
174
176 const std::string &name, const std::vector<util::Point> *data) {
177 // Write metadata
178 writeMshDataHeader(name, 3, data->size(), true);
179 for (size_t i=0; i < data->size(); i++) {
180 auto d = (*data)[i];
181 fprintf(d_file, "%zu %lf %lf %lf\n", i + 1, d.d_x, d.d_y, d.d_z);
182 }
183 fprintf(d_file, "$EndNodeData\n");
184}
185
187 const std::string &name, const std::vector<util::SymMatrix3> *data) {
188 // Write metadata
189 writeMshDataHeader(name, 6, data->size(), true);
190 for (size_t i=0; i < data->size(); i++) {
191 auto d = (*data)[i];
192 fprintf(d_file, "%zu %lf %lf %lf %lf %lf %lf\n", i + 1, d(0,0), d(1,1),
193 d(2,2), d(1,2), d(0,2), d(0,1));
194 }
195 fprintf(d_file, "$EndNodeData\n");
196}
197
198void rw::writer::MshWriter::appendCellData(const std::string &name,
199 const std::vector<float> *data) {
200 // Write metadata
201 writeMshDataHeader(name, 1, data->size(), false);
202 for (size_t i=0; i < data->size(); i++) {
203 double d = (*data)[i];
204 fprintf(d_file, "%zu %lf\n", i + 1, d);
205 }
206 fprintf(d_file, "$EndElementData\n");
207}
208
210 const std::string &name, const std::vector<util::SymMatrix3> *data) {
211 // Write metadata
212 writeMshDataHeader(name, 6, data->size(), false);
213 for (size_t i=0; i < data->size(); i++) {
214 auto d = (*data)[i];
215 fprintf(d_file, "%zu %lf %lf %lf %lf %lf %lf\n", i + 1, d(0,0), d(1,1),
216 d(2,2), d(1,2), d(0,2), d(0,1));
217 }
218 fprintf(d_file, "$EndElementData\n");
219}
220
221void rw::writer::MshWriter::addTimeStep(const double &timestep) {
222 // we add field data as simply node data
223
224 // Write metadata
225 writeMshDataHeader("time", 1, 1, true);
226 fprintf(d_file, "1 %lf\n", timestep);
227 fprintf(d_file, "$EndNodeData\n");
228}
229
231 ntag = 0;
232 etag = 0;
233 d_filename.clear();
234 fclose(d_file);
235}
236
237void rw::writer::MshWriter::appendFieldData(const std::string &name,
238 const double &data) {
239 // we add field data as simply node data
240
241 // Write metadata
242 writeMshDataHeader(name, 1, 1, true);
243 fprintf(d_file, "1 %lf\n", data);
244 fprintf(d_file, "$EndNodeData\n");
245}
246
247void rw::writer::MshWriter::appendFieldData(const std::string &name,
248 const float &data) {
249 // we add field data as simply node data
250
251 // Write metadata
252 writeMshDataHeader(name, 1, 1, true);
253 fprintf(d_file, "1 %lf\n", data);
254 fprintf(d_file, "$EndNodeData\n");
255}
void addTimeStep(const double &timestep)
Writes the time step to the file.
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 mshWriter.cpp:87
void writeMshDataHeader(const std::string &name, int field_type, size_t num_data, bool is_node_data=true)
utility function
Definition mshWriter.cpp:22
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.
MshWriter(const std::string &filename, const std::string &compress_type="")
Constructor.
Definition mshWriter.cpp:16
void appendNodes(const std::vector< util::Point > *nodes, const std::vector< util::Point > *u=nullptr)
Writes the nodes to the file.
Definition mshWriter.cpp:54
void appendPointData(const std::string &name, const std::vector< uint8_t > *data)
Writes the scalar point data to the file.
void appendCellData(const std::string &name, const std::vector< float > *data)
Writes the float data associated to cells to the file.
std::string d_filename
filename
Definition mshWriter.h:199
static int vtk_map_element_to_num_nodes[16]
Map from element type to number of nodes (for vtk)
static int vtk_to_msh_element_type_map[16]
Map from vtk element type to msh element type.
static int ntag
Definition mshWriter.cpp:11
static int etag
Definition mshWriter.cpp:12
std::string checkAndCreateNewFilename(std::string const &filename, std::string filename_ext)
Check for extension and if possible create new filename from a given filename and a given extension.
Definition io.h:443