ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
Observable_stat.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2026 The ESPResSo project
3 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4 * Max-Planck-Institute for Polymer Research, Theory Group
5 *
6 * This file is part of ESPResSo.
7 *
8 * ESPResSo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ESPResSo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "Observable_stat.hpp"
23
24#include "config/config.hpp"
25
26#include "communication.hpp"
27
28#include <utils/index.hpp>
29
30#include <boost/mpi/collectives/reduce.hpp>
31
32#include <algorithm>
33#include <cassert>
34#include <cstddef>
35#include <functional>
36#include <span>
37#include <vector>
38
39void Observable_stat::reset(std::size_t n_bonded, int max_type) {
40 if (n_bonded == m_n_bonded and max_type == m_max_type and
41 not m_data.empty()) {
42 std::ranges::fill(m_data, 0.);
43 return;
44 }
45
46 m_n_bonded = n_bonded;
47 m_max_type = max_type;
48
49 // number of chunks for different interaction types
50 constexpr std::size_t n_coulomb = 2;
51 constexpr std::size_t n_dipolar = 2;
52#ifdef ESPRESSO_VIRTUAL_SITES
53 constexpr std::size_t n_vs = 1;
54#else
55 constexpr std::size_t n_vs = 0;
56#endif
57#ifdef ESPRESSO_DPD
58 constexpr std::size_t n_dpd = 1;
59#else
60 constexpr std::size_t n_dpd = 0;
61#endif
62 constexpr std::size_t n_ext_fields = 1; // reduction over all fields
63 constexpr std::size_t n_kinetic_lin = 1; // linear kinetic contribution
64 constexpr std::size_t n_kinetic_rot = 1; // angular kinetic contribution
65
66 auto const n_non_bonded = get_non_bonded_offset(max_type, max_type) + 1ul;
67 auto const n_elements = n_kinetic_lin + n_kinetic_rot + n_bonded +
70 m_data = std::vector<double>(m_chunk_size * n_elements);
71
72 // spans for the different contributions
73 kinetic_lin = std::span<double>(m_data.data(), m_chunk_size);
74 kinetic_rot = std::span<double>(kinetic_lin.end(), m_chunk_size);
75 bonded = std::span<double>(kinetic_rot.end(), n_bonded * m_chunk_size);
76 coulomb = std::span<double>(bonded.end(), n_coulomb * m_chunk_size);
77 dipolar = std::span<double>(coulomb.end(), n_dipolar * m_chunk_size);
78 virtual_sites = std::span<double>(dipolar.end(), n_vs * m_chunk_size);
79 dpd = std::span<double>(virtual_sites.end(), n_dpd * m_chunk_size);
80 external_fields = std::span<double>(dpd.end(), n_ext_fields * m_chunk_size);
82 std::span<double>(external_fields.end(), n_non_bonded * m_chunk_size);
84 std::span<double>(non_bonded_intra.end(), n_non_bonded * m_chunk_size);
85 assert(&*non_bonded_inter.end() == (m_data.data() + m_data.size()));
86}
87
88std::size_t Observable_stat::get_non_bonded_offset(int type1, int type2) const {
89 return static_cast<std::size_t>(
90 Utils::lower_triangular(std::max(type1, type2), std::min(type1, type2)));
91}
92
94 if (comm_cart.rank() == 0) {
95 std::vector<double> temp(m_data);
96 boost::mpi::reduce(comm_cart, temp, m_data, std::plus<>{}, 0);
97 } else {
98 boost::mpi::reduce(comm_cart, m_data, std::plus<>{}, 0);
99 }
100}
std::span< double > coulomb
Contribution(s) from Coulomb interactions.
std::span< double > non_bonded_inter
Contribution(s) from non-bonded intermolecular interactions.
std::span< double > dpd
Contribution from DPD.
std::span< double > kinetic_rot
Contribution from angular kinetic energy.
std::span< double > kinetic_lin
Contribution from linear kinetic energy.
std::span< double > virtual_sites
Contribution from virtual sites (accumulated).
std::span< double > non_bonded_intra
Contribution(s) from non-bonded intramolecular interactions.
void mpi_reduce()
MPI reduction.
std::span< double > dipolar
Contribution(s) from dipolar interactions.
std::span< double > external_fields
Contribution from external fields (accumulated).
void reset(std::size_t n_bonded, int max_type)
Reinitialize the underlying storage.
std::span< double > bonded
Contribution(s) from bonded interactions.
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
boost::mpi::communicator comm_cart
The communicator.
DEVICE_QUALIFIER T lower_triangular(T i, T j)
Linear index into a lower triangular matrix.
Definition index.hpp:86