PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
rw::writer::MshWriter Class Reference

A .msh writer for simple point data and complex fem mesh data. More...

#include <mshWriter.h>

Collaboration diagram for rw::writer::MshWriter:

Public Member Functions

 MshWriter (const std::string &filename, const std::string &compress_type="")
 Constructor.
 
void close ()
 Closes the file and store it to the hard disk.
 
Mesh data
void appendNodes (const std::vector< util::Point > *nodes, const std::vector< util::Point > *u=nullptr)
 Writes the nodes 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.
 
Point data
void appendPointData (const std::string &name, const std::vector< uint8_t > *data)
 Writes the scalar point data to the file.
 
void appendPointData (const std::string &name, const std::vector< size_t > *data)
 Writes the scalar point data to the file.
 
void appendPointData (const std::string &name, const std::vector< int > *data)
 Writes the scalar point data to the file.
 
void appendPointData (const std::string &name, const std::vector< float > *data)
 Writes the scalar point data to the file.
 
void appendPointData (const std::string &name, const std::vector< double > *data)
 Writes the scalar point data to the file.
 
void appendPointData (const std::string &name, const std::vector< util::Point > *data)
 Writes the vector point data to the file.
 
void appendPointData (const std::string &name, const std::vector< util::SymMatrix3 > *data)
 Writes the symmetric matrix data associated to nodes to the file.
 
Cell data
void appendCellData (const std::string &name, const std::vector< float > *data)
 Writes the float data associated to cells to the file.
 
void appendCellData (const std::string &name, const std::vector< util::SymMatrix3 > *data)
 Writes the symmetric matrix data associated to cells to the file.
 
Field data
void appendFieldData (const std::string &name, const double &data)
 Writes the scalar field data to the file.
 
void appendFieldData (const std::string &name, const float &data)
 Writes the scalar field data to the file.
 
void addTimeStep (const double &timestep)
 Writes the time step to the file.
 

Private Member Functions

void writeMshDataHeader (const std::string &name, int field_type, size_t num_data, bool is_node_data=true)
 utility function
 

Private Attributes

std::string d_filename
 filename
 
std::string d_compressType
 compression_type Specify the compressor (if any)
 
FILE * d_file
 msh file
 

Detailed Description

A .msh writer for simple point data and complex fem mesh data.

We are using Gmsh 2.0 format.

Definition at line 29 of file mshWriter.h.

Constructor & Destructor Documentation

◆ MshWriter()

rw::writer::MshWriter::MshWriter ( const std::string &  filename,
const std::string &  compress_type = "" 
)
explicit

Constructor.

Writes mesh data in .msh format

Parameters
filenameName of file which will be created
compress_typeCompression method (optional)

Definition at line 22 of file mshWriter.cpp.

24 : d_compressType(compress_type), d_file(nullptr) {
26}
std::string d_compressType
compression_type Specify the compressor (if any)
Definition mshWriter.h:207
FILE * d_file
msh file
Definition mshWriter.h:210
std::string d_filename
filename
Definition mshWriter.h:204
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

References util::io::checkAndCreateNewFilename(), and d_filename.

Here is the call graph for this function:

Member Function Documentation

◆ addTimeStep()

void rw::writer::MshWriter::addTimeStep ( const double &  timestep)

Writes the time step to the file.

Parameters
timestepCurrent time step of the simulation

Definition at line 228 of file mshWriter.cpp.

228 {
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}
void writeMshDataHeader(const std::string &name, int field_type, size_t num_data, bool is_node_data=true)
utility function
Definition mshWriter.cpp:28

◆ appendCellData() [1/2]

void rw::writer::MshWriter::appendCellData ( const std::string &  name,
const std::vector< float > *  data 
)

Writes the float data associated to cells to the file.

Parameters
nameName of the data
dataVector containing the data

Definition at line 205 of file mshWriter.cpp.

206 {
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}
Definition contact.h:20

◆ appendCellData() [2/2]

void rw::writer::MshWriter::appendCellData ( const std::string &  name,
const std::vector< util::SymMatrix3 > *  data 
)

Writes the symmetric matrix data associated to cells to the file.

Parameters
nameName of the data
dataVector containing the data

Definition at line 216 of file mshWriter.cpp.

217 {
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}

◆ appendFieldData() [1/2]

void rw::writer::MshWriter::appendFieldData ( const std::string &  name,
const double &  data 
)

Writes the scalar field data to the file.

Parameters
nameName of the data
dataValue

Definition at line 244 of file mshWriter.cpp.

245 {
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}

◆ appendFieldData() [2/2]

void rw::writer::MshWriter::appendFieldData ( const std::string &  name,
const float &  data 
)

Writes the scalar field data to the file.

Parameters
nameName of the data
dataValue

Definition at line 254 of file mshWriter.cpp.

255 {
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}

◆ appendMesh()

void rw::writer::MshWriter::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.

Parameters
nodesVector of nodal coordinates
element_typeType of element
en_conVector of element-node connectivity
uVector of nodal displacement

Definition at line 94 of file mshWriter.cpp.

97 {
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}
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
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.

References util::vtk_map_element_to_num_nodes, and util::vtk_to_msh_element_type_map.

◆ appendNodes()

void rw::writer::MshWriter::appendNodes ( const std::vector< util::Point > *  nodes,
const std::vector< util::Point > *  u = nullptr 
)

Writes the nodes to the file.

Parameters
nodesReference positions of the nodes
uNodal displacements

Definition at line 60 of file mshWriter.cpp.

61 {
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}
Collects a message with stream syntax for use in an exception.
Definition io.h:52

◆ appendPointData() [1/7]

void rw::writer::MshWriter::appendPointData ( const std::string &  name,
const std::vector< double > *  data 
)

Writes the scalar point data to the file.

Parameters
nameName of the data
dataVector containing the data

Definition at line 171 of file mshWriter.cpp.

172 {
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}

◆ appendPointData() [2/7]

void rw::writer::MshWriter::appendPointData ( const std::string &  name,
const std::vector< float > *  data 
)

Writes the scalar point data to the file.

Parameters
nameName of the data
dataVector containing the data

Definition at line 160 of file mshWriter.cpp.

161 {
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}

◆ appendPointData() [3/7]

void rw::writer::MshWriter::appendPointData ( const std::string &  name,
const std::vector< int > *  data 
)

Writes the scalar point data to the file.

Parameters
nameName of the data
dataVector containing the data

Definition at line 149 of file mshWriter.cpp.

150 {
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}

◆ appendPointData() [4/7]

void rw::writer::MshWriter::appendPointData ( const std::string &  name,
const std::vector< size_t > *  data 
)

Writes the scalar point data to the file.

Parameters
nameName of the data
dataVector containing the data

Definition at line 137 of file mshWriter.cpp.

138 {
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}

◆ appendPointData() [5/7]

void rw::writer::MshWriter::appendPointData ( const std::string &  name,
const std::vector< uint8_t > *  data 
)

Writes the scalar point data to the file.

Parameters
nameName of the data
dataVector containing the data

Definition at line 125 of file mshWriter.cpp.

126 {
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}

◆ appendPointData() [6/7]

void rw::writer::MshWriter::appendPointData ( const std::string &  name,
const std::vector< util::Point > *  data 
)

Writes the vector point data to the file.

Parameters
nameName of the data
dataVector containing the data

Definition at line 182 of file mshWriter.cpp.

183 {
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}

◆ appendPointData() [7/7]

void rw::writer::MshWriter::appendPointData ( const std::string &  name,
const std::vector< util::SymMatrix3 > *  data 
)

Writes the symmetric matrix data associated to nodes to the file.

Parameters
nameName of the data
dataVector containing the data

Definition at line 193 of file mshWriter.cpp.

194 {
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}

◆ close()

void rw::writer::MshWriter::close ( )

Closes the file and store it to the hard disk.

Definition at line 237 of file mshWriter.cpp.

237 {
238 ntag = 0;
239 etag = 0;
240 d_filename.clear();
241 fclose(d_file);
242}
static int ntag
Definition mshWriter.cpp:17
static int etag
Definition mshWriter.cpp:18

References etag, and ntag.

◆ writeMshDataHeader()

void rw::writer::MshWriter::writeMshDataHeader ( const std::string &  name,
int  field_type,
size_t  num_data,
bool  is_node_data = true 
)
private

utility function

field_type:

  • 1 - scalar with 1 component
  • 2 - vector with 2 component
  • 3 - vector with 3 component
  • 6 - symmetric tensor with 6 component
Parameters
nameName of data
field_typeField type (see above)
num_dataNumber of data
is_node_dataIndicate if this is Node or Element data

Definition at line 28 of file mshWriter.cpp.

29 {
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}

References etag, and ntag.

Field Documentation

◆ d_compressType

std::string rw::writer::MshWriter::d_compressType
private

compression_type Specify the compressor (if any)

Definition at line 207 of file mshWriter.h.

◆ d_file

FILE* rw::writer::MshWriter::d_file
private

msh file

Definition at line 210 of file mshWriter.h.

◆ d_filename

std::string rw::writer::MshWriter::d_filename
private

filename

Definition at line 204 of file mshWriter.h.

Referenced by MshWriter().


The documentation for this class was generated from the following files: