Molecular Dynamics Simulation  1.0
Public Member Functions | Static Private Member Functions | List of all members
VTUWriter Class Reference

Class to write particle data to a .vtu file. More...

#include <VTUWriter.h>

Inheritance diagram for VTUWriter:
Inheritance graph
Collaboration diagram for VTUWriter:
Collaboration graph

Public Member Functions

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. More...
 
- Public Member Functions inherited from FileWriter
virtual ~FileWriter ()=default
 virtual destructor for correct cleanup of derived classes More...
 

Static Private Member Functions

static VTKFile_t initializeOutput (int numParticles)
 Creates a VTKFile_t object with the given number of particles. More...
 
static void plotParticle (VTKFile_t &file, const Particle &p)
 Writes a given particle to the given VTKFile_t object. More...
 

Detailed Description

Class to write particle data to a .vtu file.

Definition at line 10 of file VTUWriter.h.

Member Function Documentation

◆ initializeOutput()

VTKFile_t VTUWriter::initializeOutput ( int  numParticles)
staticprivate

Creates a VTKFile_t object with the given number of particles.

Parameters
numParticlesNumber of particles to be plotted
Returns
VTUFile_t object with the given number of particles

Definition at line 7 of file VTUWriter.cpp.

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

◆ plotParticle()

void VTUWriter::plotParticle ( VTKFile_t &  file,
const Particle p 
)
staticprivate

Writes a given particle to the given VTKFile_t object.

Parameters
fileVTUFile_t object to write to
pParticle to be written

Definition at line 41 of file VTUWriter.cpp.

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

◆ writeFile()

const std::filesystem::path VTUWriter::writeFile ( const SimulationParams params,
size_t  iteration,
const std::vector< Particle > &  particles 
) const
overridevirtual

Writes the data of the given ParticleContainer to a .vtu file.

Parameters
paramsSimulationParams object which provides the output directory path
iterationThe current iteration number
particlesA vector of particles to write to the file
Returns
The path to the written file

Implements FileWriter.

Definition at line 67 of file VTUWriter.cpp.

68  {
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
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

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