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-2022 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/Span.hpp>
29#include <utils/index.hpp>
30
31#include <boost/mpi/collectives/reduce.hpp>
32
33#include <cassert>
34#include <cstddef>
35#include <functional>
36#include <vector>
37
38Observable_stat::Observable_stat(std::size_t chunk_size, std::size_t n_bonded,
39 int max_type)
40 : m_data{}, m_chunk_size{chunk_size} {
41 // number of chunks for different interaction types
42 constexpr std::size_t n_coulomb = 2;
43 constexpr std::size_t n_dipolar = 2;
44#ifdef VIRTUAL_SITES
45 constexpr std::size_t n_vs = 1;
46#else
47 constexpr std::size_t n_vs = 0;
48#endif
49 auto const n_non_bonded =
50 static_cast<std::size_t>(Utils::lower_triangular(max_type, max_type)) +
51 1ul;
52 constexpr std::size_t n_ext_fields = 1; // reduction over all fields
53 constexpr std::size_t n_kinetic = 1; // linear+angular kinetic contributions
54
55 auto const n_elements = n_kinetic + n_bonded + 2ul * n_non_bonded +
56 n_coulomb + n_dipolar + n_vs + n_ext_fields;
57 m_data = std::vector<double>(m_chunk_size * n_elements);
58
59 // spans for the different contributions
60 kinetic = Utils::Span<double>(m_data.data(), m_chunk_size);
61 bonded = Utils::Span<double>(kinetic.end(), n_bonded * m_chunk_size);
62 coulomb = Utils::Span<double>(bonded.end(), n_coulomb * m_chunk_size);
63 dipolar = Utils::Span<double>(coulomb.end(), n_dipolar * m_chunk_size);
64 virtual_sites = Utils::Span<double>(dipolar.end(), n_vs * m_chunk_size);
66 Utils::Span<double>(virtual_sites.end(), n_ext_fields * m_chunk_size);
68 Utils::Span<double>(external_fields.end(), n_non_bonded * m_chunk_size);
70 Utils::Span<double>(non_bonded_intra.end(), n_non_bonded * m_chunk_size);
71 assert(non_bonded_inter.end() == (m_data.data() + m_data.size()));
72}
73
75Observable_stat::get_non_bonded_contribution(Utils::Span<double> base_pointer,
76 int type1, int type2) const {
77 auto const offset = static_cast<std::size_t>(
78 Utils::lower_triangular(std::max(type1, type2), std::min(type1, type2)));
79 return {base_pointer.begin() + offset * m_chunk_size, m_chunk_size};
80}
81
83 if (comm_cart.rank() == 0) {
84 std::vector<double> temp(m_data);
85 boost::mpi::reduce(comm_cart, temp, m_data, std::plus<>{}, 0);
86 } else {
87 boost::mpi::reduce(comm_cart, m_data, std::plus<>{}, 0);
88 }
89}
Utils::Span< double > kinetic
Contribution from linear and angular kinetic energy (accumulated).
Utils::Span< double > dipolar
Contribution(s) from dipolar interactions.
Observable_stat(std::size_t chunk_size, std::size_t n_bonded, int max_type)
Utils::Span< double > virtual_sites
Contribution from virtual sites (accumulated).
void mpi_reduce()
MPI reduction.
Utils::Span< double > non_bonded_intra
Contribution(s) from non-bonded intramolecular interactions.
Utils::Span< double > external_fields
Contribution from external fields (accumulated).
Utils::Span< double > non_bonded_inter
Contribution(s) from non-bonded intermolecular interactions.
Utils::Span< double > bonded
Contribution(s) from bonded interactions.
Utils::Span< double > coulomb
Contribution(s) from Coulomb interactions.
A stripped-down version of std::span from C++17.
Definition Span.hpp:38
DEVICE_QUALIFIER constexpr iterator end() const
Definition Span.hpp:91
DEVICE_QUALIFIER constexpr iterator begin() const
Definition Span.hpp:89
boost::mpi::communicator comm_cart
The communicator.
This file contains the defaults for ESPResSo.
T lower_triangular(T i, T j)
Linear index into a lower triangular matrix.
Definition index.hpp:71