HArD::Core3D
Hybrid Arbitrary Degree::Core 3D - Library to implement 3D schemes with vertex, edge, face and cell polynomials as unknowns
vtu_writer.hpp
Go to the documentation of this file.
1 // Classes and methods to ouput various file formats
2 //
3 // Currently provides:
4 // - Method to plot .vtu file from a mesh and node values.
5 //
6 // Author: Jerome Droniou (jerome.droniou@monash.edu)
7 //
8 
9 /*
10 *
11 * This library was developed around HHO methods, although some parts of it have a more
12 * general purpose. If you use this code or part of it in a scientific publication,
13 * please mention the following book as a reference for the underlying principles
14 * of HHO schemes:
15 *
16 * The Hybrid High-Order Method for Polytopal Meshes: Design, Analysis, and Applications.
17 * D. A. Di Pietro and J. Droniou. Modeling, Simulation and Applications, vol. 19.
18 * Springer International Publishing, 2020, xxxi + 525p. doi: 10.1007/978-3-030-37203-3.
19 * url: https://hal.archives-ouvertes.fr/hal-02151813.
20 *
21 */
22 
23 #ifndef _VTU_WRITER_HPP
24 #define _VTU_WRITER_HPP
25 
26 #include <memory>
27 #include <string>
28 #include <vector>
29 #include "mesh.hpp"
30 
31 #include <Eigen/Dense>
32 
38 namespace HArDCore3D {
39 
45 // ----------------------------------------------------------------------------
46 // Class definition
47 // ----------------------------------------------------------------------------
48 
50 class VtuWriter {
51 
52 public:
58  VtuWriter(const Mesh* mesh);
59 
61  template<typename T>
63  const std::string filename,
64  const std::vector<T> &sol_vrtx,
65  const std::vector<std::string> &sol_names = {}
66  )
67  {
68  FILE* pFile=fopen(filename.c_str(),"w");
69  write_header(pFile);
70  write_vertices(pFile);
71 
72  // If sol_names is too short, we re-create it
73  std::vector<std::string> names = sol_names;
74  if (names.size() < sol_vrtx.size()){
75  names.resize(0);
76  for (size_t i=0; i < sol_vrtx.size(); i++){
77  names.push_back("solution" + std::to_string(i));
78  }
79  }
80 
81  // Functions
82  fprintf (pFile,"%s\n"," <PointData>");
83  for (size_t i=0; i < sol_vrtx.size(); i++){
84  write_solution(pFile, sol_vrtx[i], names[i]);
85  }
86  fprintf (pFile,"%s\n"," </PointData>");
87 
88  write_cells(pFile);
89  write_footer(pFile);
90  fclose(pFile);
91  return true;
92  }
93 
94 
96  template<typename T>
97  inline bool write_to_vtu(
98  const std::string file_name,
99  const T &sol_vertex,
100  const std::string &sol_name = "solution"
101  )
102  {
103  return write_to_vtu(file_name, std::vector<T> {sol_vertex}, std::vector<std::string> {sol_name});
104  }
105 
106  bool write_header(FILE* pFile);
107  bool write_vertices(FILE* pFile);
108  bool write_solution(FILE* pFile, const std::vector<double> &sol_vertex, const std::string &name); // scalar solution
109  bool write_solution(FILE* pFile, const Eigen::VectorXd &sol_vertex, const std::string &name); // scalar solution (for legacy, values at the vertices should really be std::vector)
110  bool write_solution(FILE* pFile, const std::vector<VectorRd> &sol_vertex, const std::string &name); // vector-valued solution
111  bool write_cells(FILE* pFile);
112  bool write_footer(FILE* pFile);
113 
114 
115 private:
116  const Mesh* _mesh;
117  size_t ncells;
118  std::vector<int> vtk_type; // "vtk types" of each cell
119  std::vector<std::vector<Vertex *>> c_vertices; // vertices in each cell
120  std::vector<size_t> c_offset; // cell offsets
121  std::vector<std::vector<std::vector<Vertex *>>> c_f_vertices; // list of vertices in each face of each cell
122  std::vector<size_t> c_f_offset; // cell-face offsets
123 };
124 
125 }// end of namespace HArDCore3D
126 
127 #endif //_VTU_WRITER
The VtuWriter class provides methods to plot a 3D mesh and a solution on this mesh.
Definition: vtu_writer.hpp:50
Class to describe a mesh.
Definition: MeshND.hpp:17
VtuWriter(const Mesh *mesh)
Constructor for mesh writer.
Definition: vtu_writer.cpp:17
bool write_footer(FILE *pFile)
Definition: vtu_writer.cpp:192
bool write_cells(FILE *pFile)
Definition: vtu_writer.cpp:126
bool write_to_vtu(const std::string filename, const std::vector< T > &sol_vrtx, const std::vector< std::string > &sol_names={})
Writes file with a series of solutions (scalar if T=std::vector<double> (or Eigen::VectorXd),...
Definition: vtu_writer.hpp:62
bool write_to_vtu(const std::string file_name, const T &sol_vertex, const std::string &sol_name="solution")
Overload to simplify the call when only one solution is involved.
Definition: vtu_writer.hpp:97
bool write_solution(FILE *pFile, const std::vector< double > &sol_vertex, const std::string &name)
Definition: vtu_writer.cpp:97
bool write_vertices(FILE *pFile)
Definition: vtu_writer.cpp:81
bool write_header(FILE *pFile)
Definition: vtu_writer.cpp:69
Definition: ddr-magnetostatics.hpp:40