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 <memory>
37#include <span>
38#include <vector>
39
40void Observable_stat::reset(std::size_t n_bonded, int max_type) {
41 if (n_bonded == m_n_bonded and max_type == m_max_type and
42 not m_data.empty()) {
43 std::ranges::fill(m_data, 0.);
44 return;
45 }
46
47 m_n_bonded = n_bonded;
48 m_max_type = max_type;
49
50 // number of chunks for different interaction types
51 constexpr std::size_t n_coulomb = 2;
52 constexpr std::size_t n_dipolar = 2;
53#ifdef ESPRESSO_VIRTUAL_SITES
54 constexpr std::size_t n_vs = 1;
55#else
56 constexpr std::size_t n_vs = 0;
57#endif
58#ifdef ESPRESSO_DPD
59 constexpr std::size_t n_dpd = 1;
60#else
61 constexpr std::size_t n_dpd = 0;
62#endif
63 constexpr std::size_t n_ext_fields = 1; // reduction over all fields
64 constexpr std::size_t n_kinetic_lin = 1; // linear kinetic contribution
65 constexpr std::size_t n_kinetic_rot = 1; // angular kinetic contribution
66
67 auto const n_non_bonded = get_non_bonded_offset(max_type, max_type) + 1ul;
68 auto const n_elements = n_kinetic_lin + n_kinetic_rot + n_bonded +
69 2ul * n_non_bonded + n_coulomb + n_dipolar + n_vs +
70 n_ext_fields + n_dpd;
71 m_data = std::vector<double>(m_chunk_size * n_elements);
72
73 // spans for the different contributions
74 kinetic_lin = std::span<double>(m_data.data(), m_chunk_size);
75 kinetic_rot = std::span<double>(kinetic_lin.end(), m_chunk_size);
76 bonded = std::span<double>(kinetic_rot.end(), n_bonded * m_chunk_size);
77 coulomb = std::span<double>(bonded.end(), n_coulomb * m_chunk_size);
78 dipolar = std::span<double>(coulomb.end(), n_dipolar * m_chunk_size);
79 virtual_sites = std::span<double>(dipolar.end(), n_vs * m_chunk_size);
80 dpd = std::span<double>(virtual_sites.end(), n_dpd * m_chunk_size);
81 external_fields = std::span<double>(dpd.end(), n_ext_fields * m_chunk_size);
83 std::span<double>(external_fields.end(), n_non_bonded * m_chunk_size);
85 std::span<double>(non_bonded_intra.end(), n_non_bonded * m_chunk_size);
86 assert(std::to_address(non_bonded_inter.end()) ==
87 (m_data.data() + m_data.size()));
88}
89
90std::size_t Observable_stat::get_non_bonded_offset(int type1, int type2) const {
91 return static_cast<std::size_t>(
92 Utils::lower_triangular(std::max(type1, type2), std::min(type1, type2)));
93}
94
96 if (comm_cart.rank() == 0) {
97 std::vector<double> temp(m_data);
98 boost::mpi::reduce(comm_cart, temp, m_data, std::plus<>{}, 0);
99 } else {
100 boost::mpi::reduce(comm_cart, m_data, std::plus<>{}, 0);
101 }
102}
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.
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