17 const std::string &compress_type)
18 : d_compressType(compress_type), d_file(nullptr) {
23num_data,
bool is_node_data) {
27 fprintf(d_file,
"$NodeData\n");
29 fprintf(d_file,
"$ElementData\n");
32 fprintf(d_file,
"1\n");
35 fprintf(d_file,
"\"%s\"\n", name.c_str());
38 fprintf(d_file,
"1 \n");
39 fprintf(d_file,
"1.0 \n");
42 fprintf(d_file,
"3 \n");
44 fprintf(d_file,
"%d\n",
ntag);
47 fprintf(d_file,
"%d\n",
etag);
50 fprintf(d_file,
"%d\n", field_type);
51 fprintf(d_file,
"%d\n",
int(num_data));
55 const std::vector<util::Point> *u) {
59 d_file = fopen(d_filename.c_str(),
"w");
62 std::cerr <<
"Error: Can not open file = " << d_filename <<
".\n";
67 fprintf(d_file,
"$MeshFormat\n");
68 fprintf(d_file,
"2.0 0 %zu\n",
sizeof(
double));
69 fprintf(d_file,
"$EndMeshFormat\n");
72 size_t num_nodes = nodes->size();
75 fprintf(d_file,
"$Nodes\n");
76 fprintf(d_file,
"%zu\n", num_nodes);
78 for (
size_t i=0; i < num_nodes; i++) {
82 fprintf(d_file,
"%zu %lf %lf %lf\n", i + 1, p.d_x, p.d_y, p.d_z);
84 fprintf(d_file,
"$EndNodes\n");
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) {
92 appendNodes(nodes, u);
96 size_t num_elems = en_con->size() / num_vertex;
100 fprintf(d_file,
"$Elements\n");
101 fprintf(d_file,
"%zu\n", num_elems);
104 for (
size_t e=0; e < num_elems; e++) {
107 fprintf(d_file,
"%zu %zu 2 0 6 ", e+1, msh_element_type);
110 for (
size_t v=0; v<num_vertex; v++)
111 fprintf(d_file,
"%zu ", (*en_con)[e * num_vertex + v] + 1);
113 fprintf(d_file,
"\n");
115 fprintf(d_file,
"$EndElements\n");
119 const std::vector<uint8_t> *data) {
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);
127 fprintf(d_file,
"$EndNodeData\n");
131 const std::vector<size_t> *data) {
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);
139 fprintf(d_file,
"$EndNodeData\n");
143 const std::vector<int> *data) {
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);
150 fprintf(d_file,
"$EndNodeData\n");
154 const std::vector<float> *data) {
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);
161 fprintf(d_file,
"$EndNodeData\n");
165 const std::vector<double> *data) {
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);
172 fprintf(d_file,
"$EndNodeData\n");
176 const std::string &name,
const std::vector<util::Point> *data) {
178 writeMshDataHeader(name, 3, data->size(),
true);
179 for (
size_t i=0; i < data->size(); i++) {
181 fprintf(d_file,
"%zu %lf %lf %lf\n", i + 1, d.d_x, d.d_y, d.d_z);
183 fprintf(d_file,
"$EndNodeData\n");
187 const std::string &name,
const std::vector<util::SymMatrix3> *data) {
189 writeMshDataHeader(name, 6, data->size(),
true);
190 for (
size_t i=0; i < data->size(); 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));
195 fprintf(d_file,
"$EndNodeData\n");
199 const std::vector<float> *data) {
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);
206 fprintf(d_file,
"$EndElementData\n");
210 const std::string &name,
const std::vector<util::SymMatrix3> *data) {
212 writeMshDataHeader(name, 6, data->size(),
false);
213 for (
size_t i=0; i < data->size(); 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));
218 fprintf(d_file,
"$EndElementData\n");
225 writeMshDataHeader(
"time", 1, 1,
true);
226 fprintf(d_file,
"1 %lf\n", timestep);
227 fprintf(d_file,
"$EndNodeData\n");
238 const double &data) {
242 writeMshDataHeader(name, 1, 1,
true);
243 fprintf(d_file,
"1 %lf\n", data);
244 fprintf(d_file,
"$EndNodeData\n");
252 writeMshDataHeader(name, 1, 1,
true);
253 fprintf(d_file,
"1 %lf\n", data);
254 fprintf(d_file,
"$EndNodeData\n");
void addTimeStep(const double ×tep)
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.
void writeMshDataHeader(const std::string &name, int field_type, size_t num_data, bool is_node_data=true)
utility function
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.
void appendNodes(const std::vector< util::Point > *nodes, const std::vector< util::Point > *u=nullptr)
Writes the nodes to the file.
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
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.
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.