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 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#ifndef OBSERVABLES_PAIRWISEDISTANCES_HPP
20#define OBSERVABLES_PAIRWISEDISTANCES_HPP
21
22#include "BoxGeometry.hpp"
25#include "cells.hpp"
26#include "particle_node.hpp"
27#include "system/System.hpp"
28
29#include <algorithm>
30#include <cstddef>
31#include <set>
32#include <stdexcept>
33#include <utility>
34#include <vector>
35
36namespace Observables {
37
38/** @brief Track pairwise distances between two sets of particles. */
40public:
42 explicit PairwiseDistances(std::vector<int> const &ids,
43 std::vector<int> const &target_ids)
45 m_pairs{get_unique_pairs(ids, target_ids)} {}
46
47 /** @brief Evaluate the current contact times */
48 std::vector<double>
49 evaluate(boost::mpi::communicator const &comm,
50 ParticleReferenceRange const &local_particles,
51 const ParticleObservables::traits<Particle> &traits) const override {
52
53 if (comm.rank() != 0) {
54 return {};
55 }
56 // Get instances of the system and cell structures
57 auto const &system = System::get_system();
58 auto const &box_geo = *system.box_geo;
59 auto &cell_structure = *system.cell_structure;
60
61 std::vector<double> pairwise_distances;
62 for (const auto &p : m_pairs) {
63 auto const *p1 = cell_structure.get_local_particle(p.first);
64 auto const *p2 = cell_structure.get_local_particle(p.second);
65 auto const dist = box_geo.get_mi_vector(p1->pos(), p2->pos()).norm();
66 pairwise_distances.emplace_back(dist);
67 }
68 return pairwise_distances;
69 }
70
71 std::vector<std::size_t> shape() const override { return {m_pairs.size()}; }
72
73private:
74 std::vector<std::pair<int, int>> m_pairs;
75
76 std::vector<std::pair<int, int>>
77 get_unique_pairs(std::vector<int> const &ids1, std::vector<int> const &ids2) {
78 std::set<std::pair<int, int>> unique_pairs;
79 for (int id1 : ids1) {
80 for (int id2 : ids2) {
81 if (id1 != id2) {
82 unique_pairs.emplace(std::minmax(id1, id2));
83 }
84 }
85 }
86 return {unique_pairs.begin(), unique_pairs.end()};
87 }
88};
89
90} // namespace Observables
91
92#endif
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 &local_particles, const ParticleObservables::traits< Particle > &traits) const override
Evaluate the current contact times.
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()
Particles creation and deletion.