59 int const n_bonds_max) {
61 std::unordered_map<int, std::vector<std::pair<int, int>>> partners;
62 std::vector<int> bonded_pairs;
65 auto &cell_structure = *system.cell_structure;
68 for (
auto const &p : cell_structure.local_particles()) {
69 auto const pid1 = p.id();
70 for (
auto const bond : p.bonds()) {
71 if (bond.partner_ids().size() == 1u) {
72 auto const pid2 = bond.partner_ids()[0];
74 bonded_pairs.emplace_back(pid1);
75 bonded_pairs.emplace_back(pid2);
83 if (comm.rank() == 0) {
84 auto const add_partner = [&partners](
int pid1,
int pid2,
int n_bonds) {
87 for (
auto const &partner_pid : std::views::elements<0>(partners[pid1]))
88 if (partner_pid == pid2)
90 partners[pid1].emplace_back(pid2, n_bonds);
93 for (
auto it = bonded_pairs.begin(); it != bonded_pairs.end(); it += 2) {
94 add_partner(it[0], it[1], 1);
95 add_partner(it[1], it[0], 1);
99 for (
int iteration = 1; iteration < n_bonds_max; iteration++) {
100 for (
auto const pid1 : std::views::elements<0>(partners)) {
103 for (std::size_t i = 0u; i < partners[pid1].size(); ++i) {
104 auto const [pid2, dist21] = partners[pid1][i];
105 assert(dist21 <= n_bonds_max);
108 for (std::size_t j = 0u; j < partners[pid2].size(); ++j) {
109 auto const [pid3, dist32] = partners[pid2][j];
110 auto const dist31 = dist32 + dist21;
111 if (dist31 <= n_bonds_max) {
112 add_partner(pid1, pid3, dist31);
113 add_partner(pid3, pid1, dist31);
121 boost::mpi::broadcast(comm, partners, 0);
122 for (
auto const &[pid1, partner_list] : partners) {
123 for (
auto const &pid2 : std::views::elements<0>(partner_list)) {
124 if (
auto p1 = cell_structure.get_local_particle(pid1)) {
127 if (
auto p2 = cell_structure.get_local_particle(pid2)) {
132 system.on_particle_change();
138#ifdef ESPRESSO_EXCLUSIONS
139 if (
name ==
"auto_exclusions") {
145 if (
name ==
"get_highest_particle_id") {
148 if (
name ==
"clear") {
149 get_system().cell_structure->remove_all_particles();
152 if (not
context()->is_head_node()) {
155 if (
name ==
"by_id") {
157 {
"__cell_structure", m_cell_structure.lock()},
158 {
"__bonded_ias", m_bonded_ias.lock()}};
159 return std::dynamic_pointer_cast<ParticleHandle>(
160 context()->make_shared(
"Particles::ParticleHandle", obj_params));
162 if (
name ==
"by_ids") {
165 {
"__cell_structure", m_cell_structure.lock()},
166 {
"__bonded_ias", m_bonded_ias.lock()}};
169 if (
name ==
"get_n_part") {
172 if (
name ==
"get_particle_ids") {
175 if (
name ==
"particle_exists") {
178 if (
name ==
"add_particle") {
179 assert(not params.contains(
"bonds"));
181 local_params[
"__cell_structure"] = m_cell_structure.lock();
182 local_params[
"__bonded_ias"] = m_bonded_ias.lock();
183 return std::dynamic_pointer_cast<ParticleHandle>(
184 context()->make_shared(
"Particles::ParticleHandle", local_params));
virtual std::shared_ptr< ObjectHandle > make_shared(std::string const &name, const VariantMap ¶meters)=0
Get a new reference counted instance of a script interface by name.
static void auto_exclusions(boost::mpi::communicator const &comm, int const n_bonds_max)
Use the bond topology to automatically add exclusions between particles that are up to n_bonds_max bo...
void gather_buffer(std::vector< T, Allocator > &buffer, boost::mpi::communicator const &comm, int root=0)
Gather buffer with different size on each node.
int get_maximal_particle_id()
Get maximal particle id.
std::vector< int > get_particle_ids()
Get all particle ids.
bool particle_exists(int p_id)
Check if particle exists.
Particles creation and deletion.