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
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 <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, ParticleReferenceRange const &,
50 ParticleObservables::traits<Particle> const &) const override {
51
52 if (comm.rank() != 0) {
53 return {};
54 }
55 // Get instances of the system and cell structures
56 auto const &system = System::get_system();
57 auto const &box_geo = *system.box_geo;
58 auto &cell_structure = *system.cell_structure;
59
60 std::vector<double> pairwise_distances;
61 for (auto const &[pid1, pid2] : m_pairs) {
62 auto const *p1 = cell_structure.get_local_particle(pid1);
63 auto const *p2 = cell_structure.get_local_particle(pid2);
64 auto const dist = box_geo.get_mi_vector(p1->pos(), p2->pos()).norm();
65 pairwise_distances.emplace_back(dist);
66 }
67 return pairwise_distances;
68 }
69
70 std::vector<std::size_t> shape() const override { return {m_pairs.size()}; }
71
72private:
73 std::vector<std::pair<int, int>> m_pairs;
74
75 std::vector<std::pair<int, int>>
76 get_unique_pairs(std::vector<int> const &ids1, std::vector<int> const &ids2) {
77 std::set<std::pair<int, int>> unique_pairs;
78 for (int id1 : ids1) {
79 for (int id2 : ids2) {
80 if (id1 != id2) {
81 unique_pairs.emplace(std::minmax(id1, id2));
82 }
83 }
84 }
85 return {unique_pairs.begin(), unique_pairs.end()};
86 }
87};
88
89} // namespace Observables
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 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)
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.