Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
visualization.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2020-2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#include <dem/visualization.h>
5
6#include <deal.II/numerics/data_out.h>
7
8using namespace dealii;
9
10template <int dim, typename PropertiesIndex>
12
13template <int dim, typename PropertiesIndex>
14void
16 dealii::Particles::ParticleHandler<dim> &particle_handler,
17 const std::vector<std::pair<std::string, int>> &properties)
18{
19 // We reserve 1 more name than the number of properties, since we will also be
20 // writing the ID of the particles
21 this->dataset_names.reserve(PropertiesIndex::n_properties + 1);
22
23 // Adding ID to properties vector for visualization
24 dataset_names.emplace_back("ID");
25
26 // Defining property field position by iterating over properties.
27 for (int field_position = 0; field_position < PropertiesIndex::n_properties;
28 ++field_position)
29 {
30 // Get the property field name
31 const std::string field_name = properties[field_position].first;
32
33 // Number of components of the corresponding property
34 const unsigned n_components = properties[field_position].second;
35
36 // Check to see if the property is a vector
37 // By default we assume that even 2D simulations have 3 components
38 // Since the velocity and the angular velocity are stored in 3D
39 // even for 2D simulations
40 if (n_components == 3)
41 {
42 // The property is a vector, thus we set that the components
43 // are part of a vector. Do not forget that since we added the ID
44 // of the particles, we need to shift the field_position by 1. Hence,
45 // we add 1 to field_position
46 vector_datasets.emplace_back(
47 field_position + 1,
48 field_position + n_components,
49 field_name,
50 DataComponentInterpretation::component_is_part_of_vector);
51 }
52 dataset_names.emplace_back(field_name);
53 }
54
55 // Building the patch data
56 patches.resize(particle_handler.n_locally_owned_particles());
57
58 typename dealii::Particles::ParticleHandler<dim>::particle_iterator particle =
59 particle_handler.begin();
60
61 // Looping over particle to get the properties from the particle_handler
62 for (unsigned int i = 0; particle != particle_handler.end(); ++particle, ++i)
63 {
64 // Particle location
65 patches[i].vertices[0] = particle->get_location();
66 patches[i].patch_index = i;
67 patches[i].data.reinit(PropertiesIndex::n_properties + 1, 1);
68
69 // ID and other properties
70 if (particle->has_properties())
71 {
72 auto particle_properties = particle->get_properties();
73
74 // Adding ID to patches
75 patches[i].data(0, 0) = particle->get_id();
76
77 // We need to offset the data we write by one since we have one more
78 // property due to the fact that we save the ID of the particles.
79 for (unsigned int property_index = 0;
80 property_index < PropertiesIndex::n_properties;
81 ++property_index)
82 patches[i].data(property_index + 1, 0) =
83 particle_properties[property_index];
84 }
85 }
86}
87
88template <int dim, typename PropertiesIndex>
89void
91 dealii::Particles::ParticleHandler<dim> &particle_handler,
92 const MPI_Comm &mpi_communicator,
93 const ConditionalOStream &pcout)
94{
95 pcout << "id, type, dp, x, y, z";
97 {
98 pcout << ", T";
99 }
100 pcout << " " << std::endl;
101
102 // Build this rank's particles as id to formatted line
103 std::map<unsigned int, std::string> local_lines;
104
105 for (auto particle = particle_handler.begin();
106 particle != particle_handler.end();
107 ++particle)
108 {
109 const unsigned int id = particle->get_id();
110 const auto &particle_properties = particle->get_properties();
111 const auto &particle_location = particle->get_location();
112
113 std::ostringstream oss;
114 oss << std::fixed << std::setprecision(0) << id << " "
115 << std::setprecision(0) << particle_properties[PropertiesIndex::type]
116 << " " << std::setprecision(5)
117 << particle_properties[PropertiesIndex::dp] << " "
118 << std::setprecision(4) << particle_location;
119
121 {
122 oss << " " << std::fixed << std::setprecision(4)
123 << particle_properties[PropertiesIndex::T];
124 }
125
126 local_lines[id] = oss.str();
127 }
128
129 // In order to ensure that the particle will be printed in order relative to
130 // their ID's, rank 0 gathers the information of every particles, then it
131 // insert those information inside a std::map, which stores the information is
132 // order.
133 const auto gathered =
134 Utilities::MPI::gather(mpi_communicator, local_lines, 0);
135
136 if (Utilities::MPI::this_mpi_process(mpi_communicator) == 0)
137 {
138 // Merge into one ordered map. An std::map keeps keys sorted by id,
139 // thus this will be printed in order.
140 std::map<unsigned int, std::string> all_lines;
141 for (const auto &proc_lines : gathered)
142 all_lines.insert(proc_lines.begin(), proc_lines.end());
143
144 for (const auto &[id, line] : all_lines)
145 pcout << line << std::endl;
146 }
147}
148
149template <int dim, typename PropertiesIndex>
150void
152 const Vector<float> &data_to_print,
153 const DoFHandler<dim> &background_dh,
154 const MPI_Comm &mpi_communicator,
155 const ConditionalOStream &pcout)
156{
157 unsigned int this_mpi_process(
158 Utilities::MPI::this_mpi_process(mpi_communicator));
159
160 // Attach the data to the background mesh
161 DataOut<dim> data_out;
162 data_out.attach_dof_handler(background_dh);
163 data_out.add_data_vector(data_to_print,
164 "print_from_processor_" +
165 Utilities::int_to_string(this_mpi_process),
166 DataOut<dim>::type_cell_data);
167 data_out.build_patches();
168
169 // Generate a string stream buffer to store this rank's output, dropping
170 // the header lines since they contain deal.II version information that
171 // may change
172 std::stringstream out;
173 data_out.write_deal_II_intermediate(out);
174
175 std::ostringstream local_block;
176 std::string line;
177 unsigned int counter = 0;
178 while (std::getline(out, line))
179 {
180 // Increment the counter and drop header lines
181 if (counter++ > 4)
182 local_block << line << std::endl;
183 }
184
185 // Rank 0 gathers every rank's block and prints them in rank order. This
186 // avoids relying on a usleep/MPI_Barrier to serialize raw
187 // std::cout writes across ranks, which does not guarantee that the
188 // process launcher forwards each rank's stdout in order and can produce
189 // nondeterministic output.
190 const auto gathered =
191 Utilities::MPI::gather(mpi_communicator, local_block.str(), 0);
192
193 if (this_mpi_process == 0)
194 {
195 for (const auto &block : gathered)
196 pcout << block;
197 }
198}
199
200template <int dim, typename PropertiesIndex>
201const std::vector<DataOutBase::Patch<0, dim>> &
206
207template <int dim, typename PropertiesIndex>
208std::vector<std::string>
213
214template <int dim, typename PropertiesIndex>
215std::vector<
216 std::tuple<unsigned int,
217 unsigned int,
218 std::string,
219 DataComponentInterpretation::DataComponentInterpretation>>
224
225template <int dim, typename PropertiesIndex>
227
Building patches of particle properties for visualization. This function is taken from Aspect and dea...
void print_intermediate_format(const Vector< float > &data_to_print, const DoFHandler< dim > &background_dh, const MPI_Comm &mpi_communicator, const ConditionalOStream &pcout)
Print the data of particles in the deal.II intermediate format.
virtual std::vector< std::tuple< unsigned int, unsigned int, std::string, DataComponentInterpretation::DataComponentInterpretation > > get_nonscalar_data_ranges() const override
std::vector< std::tuple< unsigned int, unsigned int, std::string, DataComponentInterpretation::DataComponentInterpretation > > vector_datasets
Store which of the data fields are vectors.
void build_patches(Particles::ParticleHandler< dim > &particle_handler, const std::vector< std::pair< std::string, int > > &properties)
Build the patches of properties of particles for visualization.
virtual std::vector< std::string > get_dataset_names() const override
Implementation of the corresponding function of the base class.
std::vector< DataOutBase::Patch< 0, dim > > patches
Output information that is filled by build_patches() and written by the write function of the base cl...
virtual const std::vector< DataOutBase::Patch< 0, dim > > & get_patches() const override
Implementation of the corresponding function of the base class.
std::vector< std::string > dataset_names
A list of field names for all data components stored in patches.
void print_xyz(dealii::Particles::ParticleHandler< dim > &particle_handler, const MPI_Comm &mpi_communicator, const ConditionalOStream &pcout)
Print the data of particles in the xyz format.
constexpr bool has_thermal_properties
Whether a properties index set carries thermal properties.