Version: SMASH-1.7
vtkoutput.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2019
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #include <fstream>
11 #include <memory>
12 
13 #include "smash/clock.h"
14 #include "smash/config.h"
15 #include "smash/file.h"
17 #include "smash/particles.h"
18 #include "smash/vtkoutput.h"
19 
20 namespace smash {
21 
22 VtkOutput::VtkOutput(const bf::path &path, const std::string &name,
23  const OutputParameters &out_par)
24  : OutputInterface(name),
25  base_path_(std::move(path)),
26  is_thermodynamics_output_(name == "Thermodynamics") {
27  const auto &log = logger<LogArea::Output>();
28  if (out_par.part_extended) {
29  log.warn() << "Creating VTK output: There is no extended VTK format.";
30  }
31 }
32 
34 
59 void VtkOutput::at_eventstart(const Particles &particles,
60  const int event_number) {
67 
68  current_event_ = event_number;
70  write(particles);
72  }
73 }
74 
75 void VtkOutput::at_eventend(const Particles & /*particles*/,
76  const int /*event_number*/,
77  double /*impact_parameter*/, bool /*empty_event*/) {
78 }
79 
81  const std::unique_ptr<Clock> &,
82  const DensityParameters &) {
84  write(particles);
86  }
87 }
88 
89 void VtkOutput::write(const Particles &particles) {
90  char filename[32];
91  snprintf(filename, sizeof(filename), "pos_ev%05i_tstep%05i.vtk",
93  FilePtr file_{std::fopen((base_path_ / filename).native().c_str(), "w")};
94 
95  /* Legacy VTK file format */
96  std::fprintf(file_.get(), "# vtk DataFile Version 2.0\n");
97  std::fprintf(file_.get(), "Generated from molecular-offset data %s\n",
98  VERSION_MAJOR);
99  std::fprintf(file_.get(), "ASCII\n");
100 
101  /* Unstructured data sets are composed of points, lines, polygons, .. */
102  std::fprintf(file_.get(), "DATASET UNSTRUCTURED_GRID\n");
103  std::fprintf(file_.get(), "POINTS %zu double\n", particles.size());
104  for (const auto &p : particles) {
105  std::fprintf(file_.get(), "%g %g %g\n", p.position().x1(),
106  p.position().x2(), p.position().x3());
107  }
108  std::fprintf(file_.get(), "CELLS %zu %zu\n", particles.size(),
109  particles.size() * 2);
110  for (size_t point_index = 0; point_index < particles.size(); point_index++) {
111  std::fprintf(file_.get(), "1 %zu\n", point_index);
112  }
113  std::fprintf(file_.get(), "CELL_TYPES %zu\n", particles.size());
114  for (size_t point_index = 0; point_index < particles.size(); point_index++) {
115  std::fprintf(file_.get(), "1\n");
116  }
117  std::fprintf(file_.get(), "POINT_DATA %zu\n", particles.size());
118  std::fprintf(file_.get(), "SCALARS pdg_codes int 1\n");
119  std::fprintf(file_.get(), "LOOKUP_TABLE default\n");
120  for (const auto &p : particles) {
121  std::fprintf(file_.get(), "%s\n", p.pdgcode().string().c_str());
122  }
123  std::fprintf(file_.get(), "SCALARS is_formed int 1\n");
124  std::fprintf(file_.get(), "LOOKUP_TABLE default\n");
125  double current_time = particles.time();
126  for (const auto &p : particles) {
127  std::fprintf(file_.get(), "%s\n",
128  (p.formation_time() > current_time) ? "0" : "1");
129  }
130  std::fprintf(file_.get(), "SCALARS cross_section_scaling_factor double 1\n");
131  std::fprintf(file_.get(), "LOOKUP_TABLE default\n");
132  for (const auto &p : particles) {
133  std::fprintf(file_.get(), "%g\n", p.xsec_scaling_factor());
134  }
135  std::fprintf(file_.get(), "SCALARS mass double 1\n");
136  std::fprintf(file_.get(), "LOOKUP_TABLE default\n");
137  for (const auto &p : particles) {
138  std::fprintf(file_.get(), "%g\n", p.effective_mass());
139  }
140  std::fprintf(file_.get(), "SCALARS N_coll int 1\n");
141  std::fprintf(file_.get(), "LOOKUP_TABLE default\n");
142  for (const auto &p : particles) {
143  std::fprintf(file_.get(), "%i\n", p.get_history().collisions_per_particle);
144  }
145  std::fprintf(file_.get(), "SCALARS particle_ID int 1\n");
146  std::fprintf(file_.get(), "LOOKUP_TABLE default\n");
147  for (const auto &p : particles) {
148  std::fprintf(file_.get(), "%i\n", p.id());
149  }
150 
151  std::fprintf(file_.get(), "VECTORS momentum double\n");
152  for (const auto &p : particles) {
153  std::fprintf(file_.get(), "%g %g %g\n", p.momentum().x1(),
154  p.momentum().x2(), p.momentum().x3());
155  }
156 }
157 
167 template <typename T>
168 void VtkOutput::write_vtk_header(std::ofstream &file,
169  RectangularLattice<T> &lattice,
170  const std::string &description) {
171  const auto dim = lattice.dimensions();
172  const auto cs = lattice.cell_sizes();
173  const auto orig = lattice.origin();
174  file << "# vtk DataFile Version 2.0\n"
175  << description << "\n"
176  << "ASCII\n"
177  << "DATASET STRUCTURED_POINTS\n"
178  << "DIMENSIONS " << dim[0] << " " << dim[1] << " " << dim[2] << "\n"
179  << "SPACING " << cs[0] << " " << cs[1] << " " << cs[2] << "\n"
180  << "ORIGIN " << orig[0] << " " << orig[1] << " " << orig[2] << "\n"
181  << "POINT_DATA " << lattice.size() << "\n";
182 }
183 
184 template <typename T, typename F>
185 void VtkOutput::write_vtk_scalar(std::ofstream &file,
186  RectangularLattice<T> &lattice,
187  const std::string &varname, F &&get_quantity) {
188  file << "SCALARS " << varname << " double 1\n"
189  << "LOOKUP_TABLE default\n";
190  file << std::setprecision(3);
191  file << std::fixed;
192  const auto dim = lattice.dimensions();
193  lattice.iterate_sublattice({0, 0, 0}, dim, [&](T &node, int ix, int, int) {
194  const double f_from_node = get_quantity(node);
195  file << f_from_node << " ";
196  if (ix == dim[0] - 1) {
197  file << "\n";
198  }
199  });
200 }
201 
202 template <typename T, typename F>
203 void VtkOutput::write_vtk_vector(std::ofstream &file,
204  RectangularLattice<T> &lattice,
205  const std::string &varname, F &&get_quantity) {
206  file << "VECTORS " << varname << " double\n";
207  file << std::setprecision(3);
208  file << std::fixed;
209  const auto dim = lattice.dimensions();
210  lattice.iterate_sublattice({0, 0, 0}, dim, [&](T &node, int, int, int) {
211  const ThreeVector v = get_quantity(node);
212  file << v.x1() << " " << v.x2() << " " << v.x3() << "\n";
213  });
214 }
215 
216 std::string VtkOutput::make_filename(const std::string &descr, int counter) {
217  char suffix[22];
218  snprintf(suffix, sizeof(suffix), "_%05i_tstep%05i.vtk", current_event_,
219  counter);
220  return base_path_.string() + std::string("/") + descr + std::string(suffix);
221 }
222 
224  const DensityType dens_type) {
225  return std::string(to_string(dens_type)) + std::string("_") +
226  std::string(to_string(tq));
227 }
228 
230  const ThermodynamicQuantity tq, const DensityType dens_type,
233  return;
234  }
235  std::ofstream file;
236  const std::string varname = make_varname(tq, dens_type);
237  file.open(make_filename(varname, vtk_density_output_counter_), std::ios::out);
238  write_vtk_header(file, lattice, varname);
239  write_vtk_scalar(file, lattice, varname,
240  [&](DensityOnLattice &node) { return node.density(); });
242 }
243 
261  const ThermodynamicQuantity tq, const DensityType dens_type,
264  return;
265  }
266  std::ofstream file;
267  const std::string varname = make_varname(tq, dens_type);
268 
269  if (tq == ThermodynamicQuantity::Tmn) {
270  file.open(make_filename(varname, vtk_tmn_output_counter_++), std::ios::out);
271  write_vtk_header(file, Tmn_lattice, varname);
272  for (int i = 0; i < 4; i++) {
273  for (int j = i; j < 4; j++) {
274  write_vtk_scalar(file, Tmn_lattice,
275  varname + std::to_string(i) + std::to_string(j),
276  [&](EnergyMomentumTensor &node) {
277  return node[EnergyMomentumTensor::tmn_index(i, j)];
278  });
279  }
280  }
281  } else if (tq == ThermodynamicQuantity::TmnLandau) {
282  file.open(make_filename(varname, vtk_tmn_landau_output_counter_++),
283  std::ios::out);
284  write_vtk_header(file, Tmn_lattice, varname);
285  for (int i = 0; i < 4; i++) {
286  for (int j = i; j < 4; j++) {
287  write_vtk_scalar(file, Tmn_lattice,
288  varname + std::to_string(i) + std::to_string(j),
289  [&](EnergyMomentumTensor &node) {
290  const FourVector u = node.landau_frame_4velocity();
291  const EnergyMomentumTensor Tmn_L = node.boosted(u);
292  return Tmn_L[EnergyMomentumTensor::tmn_index(i, j)];
293  });
294  }
295  }
296  } else {
297  file.open(make_filename(varname, vtk_v_landau_output_counter_++),
298  std::ios::out);
299  write_vtk_header(file, Tmn_lattice, varname);
300  write_vtk_vector(file, Tmn_lattice, varname,
301  [&](EnergyMomentumTensor &node) {
302  const FourVector u = node.landau_frame_4velocity();
303  return -u.velocity();
304  });
305  }
306 }
307 
310  return;
311  }
312  std::ofstream file;
313  file.open(make_filename("fluidization_td", vtk_fluidization_counter_++),
314  std::ios::out);
315  write_vtk_header(file, gct.lattice(), "fluidization_td");
316  write_vtk_scalar(file, gct.lattice(), "e",
317  [&](ThermLatticeNode &node) { return node.e(); });
318  write_vtk_scalar(file, gct.lattice(), "p",
319  [&](ThermLatticeNode &node) { return node.p(); });
320  write_vtk_vector(file, gct.lattice(), "v",
321  [&](ThermLatticeNode &node) { return node.v(); });
322  write_vtk_scalar(file, gct.lattice(), "T",
323  [&](ThermLatticeNode &node) { return node.T(); });
324  write_vtk_scalar(file, gct.lattice(), "mub",
325  [&](ThermLatticeNode &node) { return node.mub(); });
326  write_vtk_scalar(file, gct.lattice(), "mus",
327  [&](ThermLatticeNode &node) { return node.mus(); });
328 }
329 
330 } // namespace smash
FilePtr fopen(const bf::path &filename, const std::string &mode)
Open a file with given mode.
Definition: file.cc:14
A class to pre-calculate and store parameters relevant for density calculation.
Definition: density.h:106
A class for time-efficient (time-memory trade-off) calculation of density on the lattice.
Definition: density.h:256
The ThreeVector class represents a physical three-vector with the components .
Definition: threevector.h:31
void at_eventstart(const Particles &particles, const int event_number) override
Writes the initial particle information list of an event to the VTK output.
Definition: vtkoutput.cc:59
void write_vtk_vector(std::ofstream &file, RectangularLattice< T > &lat, const std::string &varname, F &&function)
Write a VTK vector.
Definition: vtkoutput.cc:203
void write_vtk_header(std::ofstream &file, RectangularLattice< T > &lat, const std::string &description)
Write the VTK header.
Definition: vtkoutput.cc:168
static std::int8_t tmn_index(std::int8_t mu, std::int8_t nu)
Access the index of component .
void thermodynamics_output(const ThermodynamicQuantity tq, const DensityType dt, RectangularLattice< DensityOnLattice > &lattice) override
Prints the density lattice in VTK format on a grid.
Definition: vtkoutput.cc:229
double x3() const
Definition: threevector.h:173
bool part_extended
Extended format for particles output.
int vtk_tmn_output_counter_
Number of energy-momentum tensor lattice vtk output in current event.
Definition: vtkoutput.h:179
STL namespace.
const std::array< int, 3 > & dimensions() const
Definition: lattice.h:145
int vtk_v_landau_output_counter_
Number of Landau rest frame velocity vtk output in current event.
Definition: vtkoutput.h:183
EnergyMomentumTensor boosted(const FourVector &u) const
Boost to a given 4-velocity.
RectangularLattice< ThermLatticeNode > & lattice() const
Getter function for the lattice.
const bf::path base_path_
filesystem path for output
Definition: vtkoutput.h:169
A container class to hold all the arrays on the lattice and access them.
Definition: lattice.h:46
int current_event_
Event number.
Definition: vtkoutput.h:172
std::string make_filename(const std::string &description, int counter)
Make a file name given a description and a counter.
Definition: vtkoutput.cc:216
ThermodynamicQuantity
Represents thermodynamic quantities that can be printed out.
ThreeVector velocity() const
Get the velocity (3-vector divided by zero component).
Definition: fourvector.h:323
int vtk_tmn_landau_output_counter_
Number of Landau frame energy-momentum tensor vtk output in current event.
Definition: vtkoutput.h:181
FourVector landau_frame_4velocity() const
Find the Landau frame 4-velocity from energy-momentum tensor.
size_t size() const
Definition: particles.h:87
std::unique_ptr< std::FILE, FileDeleter > FilePtr
A RAII type to replace std::FILE *.
Definition: file.h:63
int vtk_output_counter_
Number of vtk output in current event.
Definition: vtkoutput.h:174
void write_vtk_scalar(std::ofstream &file, RectangularLattice< T > &lat, const std::string &varname, F &&function)
Write a VTK scalar.
Definition: vtkoutput.cc:185
Helper structure for Experiment to hold output options and parameters.
The GrandCanThermalizer class implements the following functionality:
The EnergyMomentumTensor class represents a symmetric positive semi-definite energy-momentum tensor ...
void at_intermediate_time(const Particles &particles, const std::unique_ptr< Clock > &clock, const DensityParameters &dens_param) override
Writes out all current particles.
Definition: vtkoutput.cc:80
int vtk_fluidization_counter_
Number of fluidization output.
Definition: vtkoutput.h:185
double x1() const
Definition: threevector.h:165
bool is_thermodynamics_output_
Is the VTK output a thermodynamics output.
Definition: vtkoutput.h:187
const std::array< double, 3 > & origin() const
Definition: lattice.h:151
int vtk_density_output_counter_
Number of density lattice vtk output in current event.
Definition: vtkoutput.h:177
constexpr int p
Proton.
The ThermLatticeNode class is intended to compute thermodynamical quantities in a cell given a set of...
double x2() const
Definition: threevector.h:169
void at_eventend(const Particles &particles, const int event_number, double impact_parameter, bool empty_event) override
Writes the final particle information list of an event to the VTK output.
Definition: vtkoutput.cc:75
const char * to_string(const ThermodynamicQuantity tq)
Convert thermodynamic quantities to strings.
void write(const Particles &particles)
Write the given particles to the output.
Definition: vtkoutput.cc:89
The Particles class abstracts the storage and manipulation of particles.
Definition: particles.h:33
DensityType
Allows to choose which kind of density to calculate.
Definition: density.h:34
const std::array< double, 3 > & cell_sizes() const
Definition: lattice.h:148
The FourVector class holds relevant values in Minkowski spacetime with (+, −, −, −) metric signature.
Definition: fourvector.h:33
VtkOutput(const bf::path &path, const std::string &name, const OutputParameters &out_par)
Create a new VTK output.
Definition: vtkoutput.cc:22
std::size_t size() const
Definition: lattice.h:176
std::string make_varname(const ThermodynamicQuantity tq, const DensityType dens_type)
Make a variable name given quantity and density type.
Definition: vtkoutput.cc:223
double density(const double norm_factor=1.0)
Compute the net Eckart density on the local lattice.
Definition: density.h:324
void iterate_sublattice(const std::array< int, 3 > &lower_bounds, const std::array< int, 3 > &upper_bounds, F &&func)
A sub-lattice iterator, which iterates in a 3D-structured manner and calls a function on every cell...
Definition: lattice.h:233
Abstraction of generic output.
Definition: action.h:24