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-2022 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
37 std::size_t get_non_bonded_offset(int type1, int type2) const;
38
39 /** Get contribution from a non-bonded interaction */
40 auto get_non_bonded_contribution(std::span<double> view, int type1,
41 int type2) const {
42 auto const offset = get_non_bonded_offset(type1, type2);
43 return view.subspan(offset * m_chunk_size, m_chunk_size);
44 }
45
46public:
47 Observable_stat(std::size_t chunk_size, std::size_t n_bonded, int max_type);
48
49 auto get_chunk_size() const { return m_chunk_size; }
50
51 /** Accumulate values.
52 * @param acc Initial value for the accumulator.
53 * @param column Which column to sum up (only relevant for multi-dimensional
54 * observables).
55 */
56 double accumulate(double acc = 0.0, std::size_t column = 0ul) const {
57 assert(column < m_chunk_size);
58 if (m_chunk_size == 1ul)
59 return std::accumulate(m_data.begin(), m_data.end(), acc);
60
61 for (auto it = m_data.begin() + static_cast<std::ptrdiff_t>(column);
62 it < m_data.end(); it += static_cast<std::ptrdiff_t>(m_chunk_size))
63 acc += *it;
64 return acc;
65 }
66
67 /** Rescale values */
68 void rescale(double volume) {
69 std::ranges::transform(m_data, m_data.begin(),
70 std::bind_front(std::multiplies{}, 1. / volume));
71 }
72
73 /** Contribution from linear and angular kinetic energy (accumulated). */
74 std::span<double> kinetic;
75 /** Contribution(s) from bonded interactions. */
76 std::span<double> bonded;
77 /** Contribution(s) from Coulomb interactions. */
78 std::span<double> coulomb;
79 /** Contribution(s) from dipolar interactions. */
80 std::span<double> dipolar;
81 /** Contribution from virtual sites (accumulated). */
82 std::span<double> virtual_sites;
83 /** Contribution from external fields (accumulated). */
84 std::span<double> external_fields;
85 /** Contribution(s) from non-bonded intramolecular interactions. */
86 std::span<double> non_bonded_intra;
87 /** Contribution(s) from non-bonded intermolecular interactions. */
88 std::span<double> non_bonded_inter;
89
90 /** Get contribution from a bonded interaction */
91 std::span<double> bonded_contribution(int bond_id) const {
92 auto const offset = m_chunk_size * static_cast<std::size_t>(bond_id);
93 return {bonded.data() + offset, m_chunk_size};
94 }
95
96 void add_non_bonded_contribution(int type1, int type2, int molid1, int molid2,
97 std::span<const double> data) {
98 assert(data.size() == m_chunk_size);
99 auto const view = (molid1 == molid2) ? non_bonded_intra : non_bonded_inter;
100 auto const dest = get_non_bonded_contribution(view, type1, type2);
101
102 std::ranges::transform(dest, data, dest.begin(), std::plus{});
103 }
104
105 void add_non_bonded_contribution(int type1, int type2, int molid1, int molid2,
106 double data) {
107 add_non_bonded_contribution(type1, type2, molid1, molid2, {&data, 1});
108 }
109
110 /** Get contribution from a non-bonded intramolecular interaction */
111 auto non_bonded_intra_contribution(int type1, int type2) const {
112 return get_non_bonded_contribution(non_bonded_intra, type1, type2);
113 }
114
115 /** Get contribution from a non-bonded intermolecular interaction */
116 auto non_bonded_inter_contribution(int type1, int type2) const {
117 return get_non_bonded_contribution(non_bonded_inter, type1, type2);
118 }
119
120 /** MPI reduction. */
121 void mpi_reduce();
122};
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
void add_non_bonded_contribution(int type1, int type2, int molid1, int molid2, std::span< const double > data)
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).
std::span< double > kinetic
Contribution from linear and angular kinetic energy (accumulated).
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).
std::span< double > bonded
Contribution(s) from bonded interactions.