Molecular Dynamics Simulation  1.0
VTUWriter.cpp
Go to the documentation of this file.
1 #include "VTUWriter.h"
2 
3 #include <spdlog/fmt/bundled/core.h>
4 
5 #include <fstream>
6 
7 VTKFile_t VTUWriter::initializeOutput(int numParticles) {
8  VTKFile_t vtu_file("UnstructuredGrid");
9 
10  // per point, we add type, position, velocity and force
11  PointData point_data;
12  DataArray_t mass(type::Float32, "mass", 1);
13  DataArray_t velocity(type::Float32, "velocity", 3);
14  DataArray_t forces(type::Float32, "force", 3);
15  DataArray_t type(type::Int32, "type", 1);
16 
17  point_data.DataArray().push_back(mass);
18  point_data.DataArray().push_back(velocity);
19  point_data.DataArray().push_back(forces);
20  point_data.DataArray().push_back(type);
21 
22  CellData cell_data; // we don't have cell data => leave it empty
23 
24  // 3 coordinates
25  Points points;
26  DataArray_t point_coordinates(type::Float32, "points", 3);
27  points.DataArray().push_back(point_coordinates);
28 
29  Cells cells; // we don't have cells, => leave it empty
30  // for some reasons, we have to add a dummy entry for paraview
31  DataArray_t cells_data(type::Float32, "types", 0);
32  cells.DataArray().push_back(cells_data);
33 
34  PieceUnstructuredGrid_t piece(point_data, cell_data, points, cells, numParticles, 0);
35  UnstructuredGrid_t unstructured_grid(piece);
36  vtu_file.UnstructuredGrid(unstructured_grid);
37 
38  return vtu_file;
39 }
40 
41 void VTUWriter::plotParticle(VTKFile_t& vtuFile, const Particle& p) {
42  PointData::DataArray_sequence& point_data_sequence = vtuFile.UnstructuredGrid()->Piece().PointData().DataArray();
43  PointData::DataArray_iterator data_iterator = point_data_sequence.begin();
44 
45  data_iterator->push_back(p.getM());
46 
47  data_iterator++;
48  data_iterator->push_back(p.getV()[0]);
49  data_iterator->push_back(p.getV()[1]);
50  data_iterator->push_back(p.getV()[2]);
51 
52  data_iterator++;
53  data_iterator->push_back(p.getF()[0]);
54  data_iterator->push_back(p.getF()[1]);
55  data_iterator->push_back(p.getF()[2]);
56 
57  data_iterator++;
58  data_iterator->push_back(p.getType());
59 
60  Points::DataArray_sequence& points_sequence = vtuFile.UnstructuredGrid()->Piece().Points().DataArray();
61  Points::DataArray_iterator points_iterator = points_sequence.begin();
62  points_iterator->push_back(p.getX()[0]);
63  points_iterator->push_back(p.getX()[1]);
64  points_iterator->push_back(p.getX()[2]);
65 }
66 
67 const std::filesystem::path VTUWriter::writeFile(const SimulationParams& params, size_t iteration,
68  const std::vector<Particle>& particles) const {
69  auto file_name = params.output_dir_path / fmt::format("MD_VTU_{:08d}.vtu", iteration);
70 
71  auto vtu_file = initializeOutput(static_cast<int>(particles.size()));
72 
73  for (const Particle& particle : particles) {
74  plotParticle(vtu_file, particle);
75  }
76 
77  std::ofstream file(file_name);
78  VTKFile(file, vtu_file);
79  file.close();
80 
81  return file_name;
82 }
Class to represent a particle.
Definition: Particle.h:26
const std::array< double, 3 > & getV() const
Gets the velocity of the particle.
Definition: Particle.h:162
int getType() const
Gets the type of the particle.
Definition: Particle.h:182
const std::array< double, 3 > & getX() const
Gets the position of the particle.
Definition: Particle.h:157
double getM() const
Gets the mass of the particle.
Definition: Particle.h:177
const std::array< double, 3 > & getF() const
Gets the total force of the particle.
Definition: Particle.h:167
Contains all parameters needed to run a simulation.
std::filesystem::path output_dir_path
Path to the directory in which to save the simulation output.
static void plotParticle(VTKFile_t &file, const Particle &p)
Writes a given particle to the given VTKFile_t object.
Definition: VTUWriter.cpp:41
static VTKFile_t initializeOutput(int numParticles)
Creates a VTKFile_t object with the given number of particles.
Definition: VTUWriter.cpp:7
const std::filesystem::path writeFile(const SimulationParams &params, size_t iteration, const std::vector< Particle > &particles) const override
Writes the data of the given ParticleContainer to a .vtu file.
Definition: VTUWriter.cpp:67