23 const std::string &compress_type)
24 : d_compressType(compress_type), d_file(nullptr) {
29num_data,
bool is_node_data) {
33 fprintf(d_file,
"$NodeData\n");
35 fprintf(d_file,
"$ElementData\n");
38 fprintf(d_file,
"1\n");
41 fprintf(d_file,
"\"%s\"\n", name.c_str());
44 fprintf(d_file,
"1 \n");
45 fprintf(d_file,
"1.0 \n");
48 fprintf(d_file,
"3 \n");
50 fprintf(d_file,
"%d\n",
ntag);
53 fprintf(d_file,
"%d\n",
etag);
56 fprintf(d_file,
"%d\n", field_type);
57 fprintf(d_file,
"%d\n",
int(num_data));
61 const std::vector<util::Point> *u) {
65 d_file = fopen(d_filename.c_str(),
"w");
68 throw std::runtime_error(
70 <<
"Error: Can not open file = " << d_filename <<
".\n");
74 fprintf(d_file,
"$MeshFormat\n");
75 fprintf(d_file,
"2.0 0 %zu\n",
sizeof(
double));
76 fprintf(d_file,
"$EndMeshFormat\n");
79 size_t num_nodes = nodes->size();
82 fprintf(d_file,
"$Nodes\n");
83 fprintf(d_file,
"%zu\n", num_nodes);
85 for (
size_t i=0; i < num_nodes; i++) {
89 fprintf(d_file,
"%zu %lf %lf %lf\n", i + 1, p.d_x, p.d_y, p.d_z);
91 fprintf(d_file,
"$EndNodes\n");
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) {
99 appendNodes(nodes, u);
103 size_t num_elems = en_con->size() / num_vertex;
107 fprintf(d_file,
"$Elements\n");
108 fprintf(d_file,
"%zu\n", num_elems);
111 for (
size_t e=0; e < num_elems; e++) {
114 fprintf(d_file,
"%zu %zu 2 0 6 ", e+1, msh_element_type);
117 for (
size_t v=0; v<num_vertex; v++)
118 fprintf(d_file,
"%zu ", (*en_con)[e * num_vertex + v] + 1);
120 fprintf(d_file,
"\n");
122 fprintf(d_file,
"$EndElements\n");
126 const std::vector<uint8_t> *
data) {
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);
134 fprintf(d_file,
"$EndNodeData\n");
138 const std::vector<size_t> *
data) {
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);
146 fprintf(d_file,
"$EndNodeData\n");
150 const std::vector<int> *
data) {
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);
157 fprintf(d_file,
"$EndNodeData\n");
161 const std::vector<float> *
data) {
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);
168 fprintf(d_file,
"$EndNodeData\n");
172 const std::vector<double> *
data) {
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);
179 fprintf(d_file,
"$EndNodeData\n");
183 const std::string &name,
const std::vector<util::Point> *
data) {
185 writeMshDataHeader(name, 3,
data->size(),
true);
186 for (
size_t i=0; i <
data->size(); i++) {
188 fprintf(d_file,
"%zu %lf %lf %lf\n", i + 1, d.d_x, d.d_y, d.d_z);
190 fprintf(d_file,
"$EndNodeData\n");
194 const std::string &name,
const std::vector<util::SymMatrix3> *
data) {
196 writeMshDataHeader(name, 6,
data->size(),
true);
197 for (
size_t i=0; i <
data->size(); 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));
202 fprintf(d_file,
"$EndNodeData\n");
206 const std::vector<float> *
data) {
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);
213 fprintf(d_file,
"$EndElementData\n");
217 const std::string &name,
const std::vector<util::SymMatrix3> *
data) {
219 writeMshDataHeader(name, 6,
data->size(),
false);
220 for (
size_t i=0; i <
data->size(); 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));
225 fprintf(d_file,
"$EndElementData\n");
232 writeMshDataHeader(
"time", 1, 1,
true);
233 fprintf(d_file,
"1 %lf\n", timestep);
234 fprintf(d_file,
"$EndNodeData\n");
245 const double &
data) {
249 writeMshDataHeader(name, 1, 1,
true);
250 fprintf(d_file,
"1 %lf\n",
data);
251 fprintf(d_file,
"$EndNodeData\n");
259 writeMshDataHeader(name, 1, 1,
true);
260 fprintf(d_file,
"1 %lf\n",
data);
261 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
Collects a message with stream syntax for use in an exception.
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.