13#include <deal.II/particles/data_out.h>
28 Triangulation<dim>::limit_level_difference_at_vertices)
37 TimerOutput::wall_times)
41 AssertThrow(!(
parameters.model_parameters.load_balance_method ==
43 dim>::LoadBalanceMethod::dynamic_with_sparse_contacts ||
44 parameters.model_parameters.load_balance_method ==
46 dim>::LoadBalanceMethod::dynamic),
48 "The \"dynamic\" and \"dynamic_with_sparse_contacts\""
49 "load balancing methods are not supported"));
63 "Simplex meshes are not supported by ray tracing simulations. Set 'simplex = false' in the mesh subsection."));
76 std::make_shared<SimulationControlRayTracing>(
parameters.simulation_control,
84 dummy_sparse_contacts_object;
88 dummy_sparse_contacts_object);
93std::shared_ptr<Insertion<dim, DEMProperties::PropertiesIndex>>
98 parameters.particle_insertion_info.insertion_method;
100 std::vector<std::shared_ptr<Distribution>>
101 dummy_size_distribution_object_container;
103 switch (insertion_method)
106 return std::make_shared<
108 dummy_size_distribution_object_container,
112 return std::make_shared<
114 dummy_size_distribution_object_container,
121 "For lethe-particles-ray-tracing simulation, only the list"
122 " and file insertion are supported."));
132 FE_Q<dim> background_fe(1);
151 pcout <<
"-->Repartitioning triangulation" << std::endl;
158 const auto average_minimum_maximum_cells =
162 const auto average_minimum_maximum_photons =
163 Utilities::MPI::min_max_avg(
photon_handler.n_locally_owned_particles(),
166 pcout <<
"Load balance finished " << std::endl;
168 <<
"Average, minimum and maximum number of photons on the processors are "
169 << average_minimum_maximum_photons.avg <<
" , "
170 << average_minimum_maximum_photons.min <<
" and "
171 << average_minimum_maximum_photons.max << std::endl;
172 pcout <<
"Minimum and maximum number of cells owned by the processors are "
173 << average_minimum_maximum_cells.min <<
" and "
174 << average_minimum_maximum_cells.max << std::endl;
198 const ConditionalOStream &
pcout)
200 std::stringstream ss;
202 ss << inserted_photon <<
" photons "
203 <<
" were inserted.";
219 std::vector<Point<3>> insertion_points_on_proc;
228 std::vector<std::vector<double>> photon_properties;
231 const std::vector<unsigned int> &n_photons_each_directions =
232 parameters.ray_tracing_info.n_photons_each_directions;
235 const Point<dim> starting_insertion_point =
239 const Tensor<1, dim> first_dir =
240 parameters.ray_tracing_info.insertion_directions_units_vector.at(0);
241 const Tensor<1, dim> second_dir =
242 parameters.ray_tracing_info.insertion_directions_units_vector.at(1);
243 const Tensor<1, dim> third_dir =
244 parameters.ray_tracing_info.insertion_directions_units_vector.at(2);
247 const double step_first_dir =
248 parameters.ray_tracing_info.step_between_photons_each_directions.at(0);
249 const double step_second_dir =
250 parameters.ray_tracing_info.step_between_photons_each_directions.at(1);
251 const double step_third_dir =
252 parameters.ray_tracing_info.step_between_photons_each_directions.at(2);
255 const Tensor<1, 3> ref_displacement_dir =
256 parameters.ray_tracing_info.ref_displacement_tensor_unit;
259 const unsigned int n_total_photons_to_insert =
260 n_photons_each_directions.at(0) * n_photons_each_directions.at(1) *
261 n_photons_each_directions.at(2);
264 const unsigned int base_photons_per_proc =
266 const unsigned int remainder = n_total_photons_to_insert %
n_mpi_processes;
270 const unsigned int n_photons_to_insert_this_proc =
275 const unsigned int first_id =
278 const unsigned int last_id = first_id + n_photons_to_insert_this_proc - 1;
287 std::vector<double> random_number_angular_1;
288 std::vector<double> random_number_angular_2;
289 std::vector<double> random_number_position;
292 random_number_angular_1.reserve(n_photons_to_insert_this_proc);
293 random_number_angular_2.reserve(n_photons_to_insert_this_proc);
294 random_number_position.reserve(3 * n_photons_to_insert_this_proc);
297 random_number_angular_1,
298 n_photons_to_insert_this_proc,
300 parameters.ray_tracing_info.prn_seed_photon_displacement);
303 random_number_angular_2,
304 n_photons_to_insert_this_proc,
305 parameters.ray_tracing_info.max_angular_offset,
306 parameters.ray_tracing_info.prn_seed_photon_displacement);
309 random_number_position,
310 3 * n_photons_to_insert_this_proc,
311 parameters.ray_tracing_info.max_insertion_offset,
312 parameters.ray_tracing_info.prn_seed_photon_insertion);
318 const Tensor<1, 3> temp_normal_vector =
319 (std::fabs(ref_displacement_dir[0]) < 0.9) ? Tensor<1, 3>({1, 0, 0}) :
320 Tensor<1, 3>({0, 1, 0});
322 Tensor<1, 3> u = temp_normal_vector -
324 scalar_product(temp_normal_vector, ref_displacement_dir);
328 const Tensor<1, 3> v = cross_product_3d(ref_displacement_dir, u);
333 Tensor<1, 3> temp_dir;
334 std::vector<double> properties_of_one_photon(6);
337 insertion_points_on_proc.reserve(n_photons_to_insert_this_proc);
340 photon_properties.resize(n_photons_to_insert_this_proc,
341 std::vector<double>(6));
345 for (
unsigned int id = first_id;
id <= last_id; ++id)
348 const unsigned int iz =
id / (n_photons_each_directions.at(0) *
349 n_photons_each_directions.at(1));
351 const unsigned int rem =
id % (n_photons_each_directions.at(0) *
352 n_photons_each_directions.at(1));
354 const unsigned int iy = rem / n_photons_each_directions.at(0);
357 const unsigned int ix = rem % n_photons_each_directions.at(0);
361 const unsigned int id_on_proc =
id - first_id;
363 starting_insertion_point +
364 (ix * step_first_dir + random_number_position.at(3 * id_on_proc)) *
366 (iy * step_second_dir + random_number_position.at(3 * id_on_proc + 1)) *
368 (iz * step_third_dir + random_number_position.at(3 * id_on_proc + 2)) *
370 insertion_points_on_proc.push_back(temp_point);
372 temp_dir = ref_displacement_dir +
373 std::sin(random_number_angular_2.at(id_on_proc)) *
374 (std::cos(random_number_angular_1.at(id_on_proc)) * u +
375 std::sin(random_number_angular_1.at(id_on_proc)) * v);
377 for (
unsigned int d = 0; d < 3; ++d)
379 properties_of_one_photon[d] = temp_point[d];
380 properties_of_one_photon[d + 3] = temp_dir[d];
382 photon_properties[id_on_proc] = properties_of_one_photon;
384 MPI_Comm communicator =
triangulation.get_mpi_communicator();
386 const auto my_bounding_box = GridTools::compute_mesh_predicate_bounding_box(
388 const auto global_bounding_boxes =
389 Utilities::MPI::all_gather(communicator, my_bounding_box);
393 global_bounding_boxes,
396 ConditionalOStream
pcout(std::cout,
397 Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) ==
405 const std::vector<Point<dim>> &points)
410 std::string output_dir_name =
parameters.simulation_control.output_folder;
416 if (stat(output_dir_name.c_str(), &buffer) != 0)
425 MPI_Comm communicator =
triangulation.get_mpi_communicator();
427 const auto my_bounding_box = GridTools::compute_mesh_predicate_bounding_box(
429 const auto global_bounding_boxes =
430 Utilities::MPI::all_gather(communicator, my_bounding_box);
432 std::vector<std::vector<double>> photon_properties(points.size());
435 temp_handler.insert_global_particles(points,
436 global_bounding_boxes,
440 Particles::DataOut<dim> data_out;
441 data_out.build_patches(temp_handler);
443 const std::string filename =
parameters.simulation_control.output_folder +
444 "/" +
parameters.simulation_control.output_name +
452 const std::vector<Point<3>> &intersection_points)
457 this->
pcout << std::defaultfloat;
459 this->
pcout << std::scientific;
466 std::vector<std::vector<Point<3>>> all_intersection_points =
469 pcout <<
"x, y, z" << std::endl;
471 for (
const std::vector<Point<3>> &sub_vector : all_intersection_points)
473 for (
const Point<3> &p : sub_vector)
474 pcout << p << std::endl;
480template <
typename NeighborListType>
483 NeighborListType &cell_list,
484 ankerl::unordered_dense::map<
485 types::particle_index,
486 std::tuple<
double, Point<dim>, Particles::ParticleIterator<dim>>>
487 &photon_intersection_points_map)
492 for (
auto cell_neighbor_list_iterator = cell_list.begin();
493 cell_neighbor_list_iterator != cell_list.end();
494 ++cell_neighbor_list_iterator)
497 const auto main_cell = cell_neighbor_list_iterator->begin();
500 typename Particles::ParticleHandler<dim>::particle_iterator_range
501 photons_in_main_cell =
photon_handler.particles_in_cell(*main_cell);
504 for (
auto current_photon = photons_in_main_cell.begin();
505 current_photon != photons_in_main_cell.end();
512 auto photon_properties = current_photon->get_properties();
515 const Point<3> photon_insertion_point(
516 {photon_properties[0], photon_properties[1], photon_properties[2]});
519 Tensor<1, 3> photon_displacement_vector(
520 {photon_properties[3], photon_properties[4], photon_properties[5]});
522 const Point<dim> current_photon_location =
523 current_photon->get_location();
527 auto &cell_neighbor_list = *cell_neighbor_list_iterator;
529 auto starting_iterator = cell_neighbor_list.begin();
534 if constexpr (std::is_same_v<NeighborListType, ghost_neighbor_list>)
536 for (
auto current_neighboring_cell = starting_iterator;
537 current_neighboring_cell != cell_neighbor_list.end();
538 ++current_neighboring_cell)
541 typename Particles::ParticleHandler<dim>::particle_iterator_range
542 particle_in_neighboring_cell =
548 std::vector<Point<dim>> current_intersection_points;
549 current_intersection_points.reserve(2);
552 for (
auto current_particle = particle_in_neighboring_cell.begin();
553 current_particle != particle_in_neighboring_cell.end();
558 const Point<dim> particle_position =
559 current_particle->get_location();
560 const double particle_diameter =
564 current_intersection_points =
566 photon_insertion_point,
567 photon_displacement_vector,
573 if (current_intersection_points.size() == 0)
579 Point<dim> new_closest_point;
583 if (current_intersection_points.size() == 1)
585 new_distance = (photon_insertion_point -
586 current_intersection_points[0])
588 new_closest_point = current_intersection_points[0];
595 const double distance_0 = (photon_insertion_point -
596 current_intersection_points[0])
598 const double distance_1 = (photon_insertion_point -
599 current_intersection_points[1])
602 if (distance_0 < distance_1)
604 new_distance = distance_0;
605 new_closest_point = current_intersection_points[0];
609 new_distance = distance_1;
610 new_closest_point = current_intersection_points[1];
617 if (photon_intersection_points_map.find(
618 current_photon->get_id()) ==
619 photon_intersection_points_map.end())
622 photon_intersection_points_map[current_photon->get_id()] =
623 {new_distance, new_closest_point, current_photon};
631 const double old_distance = std::get<0>(
632 photon_intersection_points_map[current_photon
635 if (new_distance < old_distance)
639 photon_intersection_points_map[current_photon
641 new_distance, new_closest_point, current_photon};
650 if constexpr (std::is_same_v<NeighborListType, local_neighbor_list>)
652 const Point<dim> new_photon_location =
653 current_photon_location +
655 current_photon->set_location(new_photon_location);
670 pcout <<
"Reading triangulation" << std::endl;
679 pcout << std::endl <<
"Finished reading triangulation" << std::endl;
695 std::vector<Point<dim>> total_intersection_points;
696 total_intersection_points.reserve(
photon_handler.n_global_particles());
700 ankerl::unordered_dense::map<
701 types::particle_index,
702 std::tuple<double, Point<dim>, Particles::ParticleIterator<dim>>>
703 photon_intersection_points_map;
705 photon_intersection_points_map.reserve(
734 photon_intersection_points_map);
737 photon_intersection_points_map);
742 std::vector<Particles::ParticleIterator<dim>> photon_iterators_to_remove;
743 photon_iterators_to_remove.reserve(photon_intersection_points_map.size());
745 for (
auto it = photon_intersection_points_map.begin();
746 it != photon_intersection_points_map.end();
749 auto photon_tuple = it->second;
750 total_intersection_points.push_back(std::get<1>(photon_tuple));
751 photon_iterators_to_remove.push_back(std::get<2>(photon_tuple));
756 photon_iterators_to_remove.clear();
757 photon_intersection_points_map.clear();
static DEMActionManager * get_action_manager()
Getter of the unique instance of the DEMActionManager.
Handles all the parameters declared in the parameter handler file.
Parameters controlling particle insertion in DEM simulations.
InsertionMethod
Method used to insert particles.
Numerical model parameters for DEM simulations.
Handles all the parameters declared in the parameter handler file.
Solver that uses ray tracing to find intersection points between photons and particles.
void load_balance()
Check if a load balancing is required according to the load balancing method and perform it if necess...
DEMActionManager * action_manager
The action manager that manages the actions triggered by events.
const DEMSolverParameters< dim > dem_parameters
DEM parameters needed for some functions related to the DEM particles.
const unsigned int this_mpi_process
The rank of the current MPI process.
ghost_neighbor_list cells_ghost_neighbor_list
Ghost neighbor list.
parallel::distributed::Triangulation< dim > triangulation
The distributed triangulation used for the DEM simulation.
void finish_simulation(const std::vector< Point< 3 > > &intersection_points)
Execute the last post-processing at the end of the simulation and output test results if necessary.
const unsigned int n_mpi_processes
The number of MPI processes used for the parallel simulation.
Particles::ParticleHandler< dim, dim > photon_handler
The photon handler that manages the photons.
TimerOutput computing_timer
The timer that keeps track of the time spent in some functions. Currently, the only function for whic...
LagrangianLoadBalancing< dim, DEMProperties::PropertiesIndex > load_balancing
The load balancing handler.
void setup_parameters()
Set the parameters for the ray tracing.
MPI_Comm mpi_communicator
The MPI communicator used for the parallel simulation.
void setup_background_dofs()
Set up the background degree of freedom used for parallel grid output or mapping of periodic dofs whe...
std::shared_ptr< Insertion< dim, DEMProperties::PropertiesIndex > > set_particle_insertion_type()
Set the insertion method. Supported insertion method are list and file.
ConditionalOStream pcout
The output stream used for the parallel simulation, only used by the process 0.
DoFHandler< dim > background_dh
The background degree of freedom handler uses for parallel grid processing.
MappingQGeneric< dim > mapping
The polynomial mapping. The mapping is first order in DEM simulations.
Particles::ParticleHandler< dim, dim > particle_handler
The particle handler that manages the particles.
void write_output_results(const std::vector< Point< dim > > &points)
Generate the output file of the particle ray tracing in parallel.
RayTracingSolver(RayTracingSolverParameters< dim > ¶meters, DEMSolverParameters< dim > &dem_parameters)
std::shared_ptr< Insertion< dim, DEMProperties::PropertiesIndex > > particle_insertion_object
The particle insertion object.
std::shared_ptr< SimulationControl > simulation_control
The simulation control (DEM Transient).
local_neighbor_list cells_local_neighbor_list
Local neighbor list.
void find_intersection(NeighborListType &cell_list, ankerl::unordered_dense::map< types::particle_index, std::tuple< double, Point< dim >, Particles::ParticleIterator< dim > > > &photon_intersection_points_map)
Execute the ray tracing algorithm to find intersection points between photons and particles.
double displacement_distance
The reference displacement distance used to move the photons during each pseudo time step.
void insert_particles_and_photons()
Insert particles and photons at the beginning of a ray tracing simulation.
void solve()
Calls all the necessary functions to set parameters, solve the intersection points between the photon...
const RayTracingSolverParameters< dim > parameters
The parameters of the Ray Tracing simulation.
static void print_insertion_info(const unsigned int &inserted_photon, const ConditionalOStream &pcout)
Print information about the photons that have been inserted during an insertion time step.
void create_random_number_container(std::vector< double > &random_container, const unsigned int number_of_elements, const double maximum_range, const unsigned int prn_seed)
Creates a vector of random doubles between 0 and a maximum value using a pseudo random seed.
void create_output_folder(const std::string &dirname)
Creates the simulation output folder.
void announce_string(const ConditionalOStream &pcout, const std::string &expression, const char delimiter='-')
Prints a string and then adds a line above and below made with dashes containing as many dashes as th...
void find_cell_neighbors(const parallel::distributed::Triangulation< dim > &triangulation, typename DEM::dem_data_structures< dim >::cells_neighbor_list &cells_local_neighbor_list, typename DEM::dem_data_structures< dim >::cells_neighbor_list &cells_ghost_neighbor_list)
Finds the neighbor list (without repetition) of all the active cells in the triangulation....
void read_mesh_and_manifolds(parallel::DistributedTriangulationBase< dim, spacedim > &triangulation, const Parameters::Mesh< dim, spacedim > &mesh_parameters, const Parameters::Manifolds &manifolds_parameters, bool restart, const Parameters::PeriodicBoundaries &periodic_boundaries)
Completely sets up a mesh and its manifolds.