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