ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
core/observables/PairwiseDistances.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025-2026 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 "BoxGeometry.hpp"
25#include "cells.hpp"
26#include "particle_node.hpp"
27#include "system/System.hpp"
28
29#include <utils/Vector.hpp>
30
31#include <boost/mpi/collectives/gather.hpp>
32
33#include <algorithm>
34#include <cstddef>
35#include <set>
36#include <stdexcept>
37#include <unordered_map>
38#include <unordered_set>
39#include <utility>
40#include <vector>
41
42namespace Observables {
43
44/** @brief Track pairwise distances between two sets of particles. */
46public:
48 explicit PairwiseDistances(std::vector<int> const &ids,
49 std::vector<int> const &target_ids)
51 m_pairs{get_unique_pairs(ids, target_ids)} {}
52
53 /** @brief Evaluate pairwise distances, gathering positions from all ranks. */
54 std::vector<double>
55 evaluate(boost::mpi::communicator const &comm, ParticleReferenceRange const &,
56 ParticleObservables::traits<Particle> const &) const override {
57
58 auto const &system = System::get_system();
59 auto const &box_geo = *system.box_geo;
60 auto &cell_structure = *system.cell_structure;
61
62 // Collect local (pid, position) pairs for all particles in m_pairs
63 std::unordered_set<int> visited_pids;
64 std::vector<std::pair<int, Utils::Vector3d>> local_pid_pos;
65 for (auto const &[pid1, pid2] : m_pairs) {
66 for (auto const pid : {pid1, pid2}) {
67 if (not visited_pids.contains(pid)) {
68 auto const *p = cell_structure.get_local_particle(pid);
69 if (p and not p->is_ghost()) {
70 local_pid_pos.emplace_back(pid, p->pos());
71 }
72 visited_pids.emplace(pid);
73 }
74 }
75 }
76
77 // Gather from all ranks to rank 0
78 std::vector<std::vector<std::pair<int, Utils::Vector3d>>> all_pid_pos;
79 boost::mpi::gather(comm, local_pid_pos, all_pid_pos, 0);
80
81 if (comm.rank() != 0) {
82 return {};
83 }
84
85 // Build position map on rank 0
86 std::unordered_map<int, Utils::Vector3d> pos_map;
87 for (auto const &rank_data : all_pid_pos) {
88 for (auto const &[pid, pos] : rank_data) {
89 pos_map.emplace(pid, pos);
90 }
91 }
92
93 std::vector<double> pairwise_distances;
94 pairwise_distances.reserve(m_pairs.size());
95 for (auto const &[pid1, pid2] : m_pairs) {
96 auto const dist =
97 box_geo.get_mi_vector(pos_map.at(pid1), pos_map.at(pid2)).norm();
98 pairwise_distances.emplace_back(dist);
99 }
100 return pairwise_distances;
101 }
102
103 std::vector<std::size_t> shape() const override { return {m_pairs.size()}; }
104
105private:
106 std::vector<std::pair<int, int>> m_pairs;
107
108 std::vector<std::pair<int, int>>
109 get_unique_pairs(std::vector<int> const &ids1, std::vector<int> const &ids2) {
110 std::set<std::pair<int, int>> unique_pairs;
111 for (int id1 : ids1) {
112 for (int id2 : ids2) {
113 if (id1 != id2) {
114 unique_pairs.emplace(std::minmax(id1, id2));
115 }
116 }
117 }
118 return {unique_pairs.begin(), unique_pairs.end()};
119 }
120};
121
122} // namespace Observables
Vector implementation and trait types for boost qvm interoperability.
This file contains everything related to the global cell structure / cell system.
Track pairwise distances between two sets of particles.
std::vector< double > evaluate(boost::mpi::communicator const &comm, ParticleReferenceRange const &, ParticleObservables::traits< Particle > const &) const override
Evaluate pairwise distances, gathering positions from all ranks.
std::vector< std::size_t > shape() const override
PairwiseDistances(std::vector< int > const &ids, std::vector< int > const &target_ids)
std::vector< int > const & ids() const
Calculate pairwise distances between two sets of particles.
PidPairwiseDistancesObservable(std::vector< int > const &ids, std::vector< int > const &target_ids)
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
std::vector< std::reference_wrapper< Particle const > > ParticleReferenceRange
System & get_system()
Particles creation and deletion.