ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
core/collision_detection/utils.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2011-2024 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <config/config.hpp>
23
24#ifdef COLLISION_DETECTION
25
26#include "CollisionPair.hpp"
27
28#include "BoxGeometry.hpp"
29#include "Particle.hpp"
30#include "bonds.hpp"
32#include "communication.hpp"
33#include "virtual_sites.hpp"
34
35#include <utils/Vector.hpp>
37
38#include <boost/mpi/collectives.hpp>
39#include <boost/serialization/utility.hpp>
40
41#include <stdexcept>
42#include <string>
43#include <utility>
44#include <vector>
45
46namespace CollisionDetection {
47
48inline auto &get_part(CellStructure &cell_structure, int id) {
49 auto const p = cell_structure.get_local_particle(id);
50
51 if (not p) {
52 throw std::runtime_error("Could not handle collision because particle " +
53 std::to_string(id) + " was not found.");
54 }
55
56 return *p;
57}
58
59#ifdef VIRTUAL_SITES_RELATIVE
61 CellStructure &cell_structure, BoxGeometry const &box_geo,
62 int const part_type_vs, double const min_global_cut,
63 int const current_vs_pid, Utils::Vector3d const &pos, int const relate_to) {
64 Particle new_part;
65 new_part.id() = current_vs_pid;
66 new_part.pos() = pos;
67 auto p_vs = cell_structure.add_particle(std::move(new_part));
68 vs_relate_to(*p_vs, get_part(cell_structure, relate_to), box_geo,
69 min_global_cut);
70 p_vs->type() = part_type_vs;
71}
72#endif // VIRTUAL_SITES_RELATIVE
73
74inline auto gather_collision_queue(std::vector<CollisionPair> const &local) {
75 auto global = local;
76 if (::comm_cart.size() > 1) {
78 boost::mpi::broadcast(::comm_cart, global, 0);
79 }
80 return global;
81}
82
83inline void add_bind_centers(std::vector<CollisionPair> &collision_queue,
84 System::System &system, int bond_id) {
85 for (auto &c : collision_queue) {
86 // Ensure that the bond is associated with the non-ghost particle
87 if (system.cell_structure->get_local_particle(c.first)->is_ghost()) {
88 std::swap(c.first, c.second);
89 }
90
91 // Because MPI rank 1's queue containing (@c p1_on_rank_1, @c p2_on_rank_2)
92 // doesn't guarantee that the same pair (with or without swapped order) is
93 // also queued on the MPI rank 2.
94 // Once we change bond storage, some syncing has to be done.
96 ::add_bond(system, bond_id, {c.first, c.second});
97 }
98}
99
100} // namespace CollisionDetection
101
102#endif // COLLISION_DETECTION
Vector implementation and trait types for boost qvm interoperability.
bool add_bond(System::System &system, int bond_id, std::vector< int > const &particle_ids)
Add a bond to a particle.
Definition bonds.cpp:25
constexpr bool use_one_sided_bond_storage
Definition bonds.hpp:26
Main system class.
std::shared_ptr< CellStructure > cell_structure
boost::mpi::communicator comm_cart
The communicator.
This file contains the defaults for ESPResSo.
void place_vs_and_relate_to_particle(CellStructure &cell_structure, BoxGeometry const &box_geo, int const part_type_vs, double const min_global_cut, int const current_vs_pid, Utils::Vector3d const &pos, int const relate_to)
auto gather_collision_queue(std::vector< CollisionPair > const &local)
void add_bind_centers(std::vector< CollisionPair > &collision_queue, System::System &system, int bond_id)
auto & get_part(CellStructure &cell_structure, int id)
void gather_buffer(std::vector< T, Allocator > &buffer, boost::mpi::communicator const &comm, int root=0)
Gather buffer with different size on each node.
Describes a cell structure / cell system.
Particle * get_local_particle(int id)
Get a local particle by id.
Particle * add_particle(Particle &&p)
Add a particle.
Struct holding all information for one particle.
Definition Particle.hpp:395
void vs_relate_to(Particle &p_vs, Particle const &p_relate_to, BoxGeometry const &box_geo, double min_global_cut)
Setup a virtual site to track a real particle.