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 "system/System.hpp"
27
28#include <utils/Vector.hpp>
29
30#include <boost/mpi/collectives/gather.hpp>
31
32#include <algorithm>
33#include <cstddef>
34#include <set>
35#include <stdexcept>
36#include <unordered_map>
37#include <unordered_set>
38#include <utility>
39#include <vector>
40
41namespace Observables {
42
43/** @brief Track pairwise distances between two sets of particles. */
45public:
47 explicit PairwiseDistances(std::vector<int> const &ids,
48 std::vector<int> const &target_ids)
50 m_pairs{get_unique_pairs(ids, target_ids)} {}
51
52 /** @brief Evaluate pairwise distances, gathering positions from all ranks. */
53 std::vector<double>
54 evaluate(boost::mpi::communicator const &comm, ParticleReferenceRange const &,
55 ParticleObservables::traits<Particle> const &) const override {
56
57 auto const &system = System::get_system();
58 auto const &box_geo = *system.box_geo;
59 auto &cell_structure = *system.cell_structure;
60
61 // Collect local (pid, position) pairs for all particles in m_pairs
62 std::unordered_set<int> visited_pids;
63 std::vector<std::pair<int, Utils::Vector3d>> local_pid_pos;
64 for (auto const &[pid1, pid2] : m_pairs) {
65 for (auto const pid : {pid1, pid2}) {
66 if (not visited_pids.contains(pid)) {
67 auto const *p = cell_structure.get_local_particle(pid);
68 if (p and not p->is_ghost()) {
69 local_pid_pos.emplace_back(pid, p->pos());
70 }
71 visited_pids.emplace(pid);
72 }
73 }
74 }
75
76 // Gather from all ranks to rank 0
77 std::vector<std::vector<std::pair<int, Utils::Vector3d>>> all_pid_pos;
78 boost::mpi::gather(comm, local_pid_pos, all_pid_pos, 0);
79
80 if (comm.rank() != 0) {
81 return {};
82 }
83
84 // Build position map on rank 0
85 std::unordered_map<int, Utils::Vector3d> pos_map;
86 for (auto const &rank_data : all_pid_pos) {
87 for (auto const &[pid, pos] : rank_data) {
88 pos_map.emplace(pid, pos);
89 }
90 }
91
92 std::vector<double> pairwise_distances;
93 pairwise_distances.reserve(m_pairs.size());
94 for (auto const &[pid1, pid2] : m_pairs) {
95 auto const dist =
96 box_geo.get_mi_vector(pos_map.at(pid1), pos_map.at(pid2)).norm();
97 pairwise_distances.emplace_back(dist);
98 }
99 return pairwise_distances;
100 }
101
102 std::vector<std::size_t> shape() const override { return {m_pairs.size()}; }
103
104private:
105 std::vector<std::pair<int, int>> m_pairs;
106
107 std::vector<std::pair<int, int>>
108 get_unique_pairs(std::vector<int> const &ids1, std::vector<int> const &ids2) {
109 std::set<std::pair<int, int>> unique_pairs;
110 for (int id1 : ids1) {
111 for (int id2 : ids2) {
112 if (id1 != id2) {
113 unique_pairs.emplace(std::minmax(id1, id2));
114 }
115 }
116 }
117 return {unique_pairs.begin(), unique_pairs.end()};
118 }
119};
120
121} // 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)
std::vector< std::reference_wrapper< Particle const > > ParticleReferenceRange
System & get_system()