PeriDEM 0.2.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 24 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 16 of file mshWriter.cpp.

18 : d_compressType(compress_type), d_file(nullptr) {
20}
std::string d_compressType
compression_type Specify the compressor (if any)
Definition mshWriter.h:202
FILE * d_file
msh file
Definition mshWriter.h:205
std::string d_filename
filename
Definition mshWriter.h:199
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

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 221 of file mshWriter.cpp.

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

◆ 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 198 of file mshWriter.cpp.

199 {
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}
float d
initial distance between particle and wall

◆ 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 209 of file mshWriter.cpp.

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

◆ 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 237 of file mshWriter.cpp.

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

◆ 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 247 of file mshWriter.cpp.

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

◆ 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 87 of file mshWriter.cpp.

90 {
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}
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
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 54 of file mshWriter.cpp.

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

◆ 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 164 of file mshWriter.cpp.

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

◆ 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 153 of file mshWriter.cpp.

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

◆ 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 142 of file mshWriter.cpp.

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

◆ 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 130 of file mshWriter.cpp.

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

◆ 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 118 of file mshWriter.cpp.

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

◆ 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 175 of file mshWriter.cpp.

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

◆ 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 186 of file mshWriter.cpp.

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

◆ close()

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

Closes the file and store it to the hard disk.

Definition at line 230 of file mshWriter.cpp.

230 {
231 ntag = 0;
232 etag = 0;
233 d_filename.clear();
234 fclose(d_file);
235}
static int ntag
Definition mshWriter.cpp:11
static int etag
Definition mshWriter.cpp:12

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 22 of file mshWriter.cpp.

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

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 202 of file mshWriter.h.

◆ d_file

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

msh file

Definition at line 205 of file mshWriter.h.

◆ d_filename

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

filename

Definition at line 199 of file mshWriter.h.

Referenced by MshWriter().


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