ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
Observable_stat.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2026 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 <algorithm>
23#include <cassert>
24#include <cstddef>
25#include <functional>
26#include <numeric>
27#include <span>
28#include <vector>
29
30/** Observable for the pressure and energy. */
32 /** Array for observables on each node. */
33 std::vector<double> m_data;
34 /** Number of doubles per data item */
35 std::size_t m_chunk_size;
36 /** Number of non-bonded interactions */
37 std::size_t m_n_bonded;
38 /** Number of particle types */
39 int m_max_type;
40
41 std::size_t get_non_bonded_offset(int type1, int type2) const;
42
43 /** Get contribution from a non-bonded interaction */
44 auto get_non_bonded_contribution(std::span<double> view, int type1,
45 int type2) const {
46 auto const offset = get_non_bonded_offset(type1, type2);
47 return view.subspan(offset * m_chunk_size, m_chunk_size);
48 }
49
50public:
51 Observable_stat(std::size_t chunk_size, std::size_t n_bonded, int max_type)
52 : m_data{}, m_chunk_size{chunk_size}, m_n_bonded{n_bonded},
53 m_max_type{max_type} {
54 reset(n_bonded, max_type);
55 }
56
57 /**
58 * @brief Reinitialize the underlying storage.
59 * If the new data layout is unchanged, no reallocation takes place.
60 */
61 void reset(std::size_t n_bonded, int max_type);
62
63 auto get_chunk_size() const { return m_chunk_size; }
64
65 /** Accumulate values.
66 * @param acc Initial value for the accumulator.
67 * @param column Which column to sum up (only relevant for multi-dimensional
68 * observables).
69 */
70 double accumulate(double acc = 0.0, std::size_t column = 0ul) const {
71 assert(column < m_chunk_size);
72 if (m_chunk_size == 1ul)
73 return std::accumulate(m_data.begin(), m_data.end(), acc);
74
75 for (auto it = m_data.begin() + static_cast<std::ptrdiff_t>(column);
76 it < m_data.end(); it += static_cast<std::ptrdiff_t>(m_chunk_size))
77 acc += *it;
78 return acc;
79 }
80
81 /** Rescale values */
82 void rescale(double volume) {
83 std::ranges::transform(m_data, m_data.begin(),
84 std::bind_front(std::multiplies{}, 1. / volume));
85 }
86
87 /** Contribution from linear kinetic energy. */
88 std::span<double> kinetic_lin;
89 /** Contribution from angular kinetic energy. */
90 std::span<double> kinetic_rot;
91 /** Contribution(s) from bonded interactions. */
92 std::span<double> bonded;
93 /** Contribution(s) from Coulomb interactions. */
94 std::span<double> coulomb;
95 /** Contribution(s) from dipolar interactions. */
96 std::span<double> dipolar;
97 /** Contribution from virtual sites (accumulated). */
98 std::span<double> virtual_sites;
99 /** Contribution from DPD. */
100 std::span<double> dpd;
101 /** Contribution from external fields (accumulated). */
102 std::span<double> external_fields;
103 /** Contribution(s) from non-bonded intramolecular interactions. */
104 std::span<double> non_bonded_intra;
105 /** Contribution(s) from non-bonded intermolecular interactions. */
106 std::span<double> non_bonded_inter;
107
108 /** Get contribution from a bonded interaction */
109 std::span<double> bonded_contribution(int bond_id) const {
110 auto const offset = m_chunk_size * static_cast<std::size_t>(bond_id);
111 return {bonded.data() + offset, m_chunk_size};
112 }
113
115 std::span<const double> data) {
116 assert(data.size() == m_chunk_size);
117 auto const view = (molid1 == molid2) ? non_bonded_intra : non_bonded_inter;
118 auto const dest = get_non_bonded_contribution(view, type1, type2);
119
120 std::ranges::transform(dest, data, dest.begin(), std::plus{});
121 }
122
124 double data) {
126 }
127
128 /** Get contribution from a non-bonded intramolecular interaction */
130 return get_non_bonded_contribution(non_bonded_intra, type1, type2);
131 }
132
133 /** Get contribution from a non-bonded intermolecular interaction */
135 return get_non_bonded_contribution(non_bonded_inter, type1, type2);
136 }
137
138 /** MPI reduction. */
139 void mpi_reduce();
140};
Observable for the pressure and energy.
auto non_bonded_intra_contribution(int type1, int type2) const
Get contribution from a non-bonded intramolecular interaction.
void add_non_bonded_contribution(int type1, int type2, int molid1, int molid2, double data)
std::span< double > coulomb
Contribution(s) from Coulomb interactions.
std::span< double > non_bonded_inter
Contribution(s) from non-bonded intermolecular interactions.
auto get_chunk_size() const
std::span< double > dpd
Contribution from DPD.
void add_non_bonded_contribution(int type1, int type2, int molid1, int molid2, std::span< const double > data)
std::span< double > kinetic_rot
Contribution from angular kinetic energy.
std::span< double > kinetic_lin
Contribution from linear kinetic energy.
void rescale(double volume)
Rescale values.
auto non_bonded_inter_contribution(int type1, int type2) const
Get contribution from a non-bonded intermolecular interaction.
std::span< double > virtual_sites
Contribution from virtual sites (accumulated).
Observable_stat(std::size_t chunk_size, std::size_t n_bonded, int max_type)
std::span< double > bonded_contribution(int bond_id) const
Get contribution from a bonded interaction.
double accumulate(double acc=0.0, std::size_t column=0ul) const
Accumulate values.
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.