ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
ObservableStat.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2013-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#include "config/config.hpp"
21
22#include "ObservableStat.hpp"
23
26
28
29#include <utils/Span.hpp>
30
31#include <cstddef>
32#include <string>
33#include <vector>
34
35namespace ScriptInterface {
36namespace Analysis {
37
38/**
39 * @brief Generate an observable summary.
40 * @param[in] system The system to analyze.
41 * @param[in] obs The observable handle.
42 * @param[in] calc_sp Whether to compute a scalar pressure.
43 */
44static auto get_summary(::System::System const &system,
45 Observable_stat const &obs, bool const calc_sp) {
46 auto const obs_dim = obs.get_chunk_size();
47
48 auto const get_obs_contribs = [obs_dim,
49 calc_sp](Utils::Span<double> const views) {
50 if (obs_dim == 1) {
51 return std::vector<Variant>(views.begin(), views.end());
52 }
53 assert(obs_dim == 9ul);
54 assert(views.size() % 9ul == 0ul);
55 std::vector<Variant> out;
56 for (std::size_t i = 0ul; i < views.size() / 9ul; ++i) {
57 auto const view = Utils::Span<double>{views.data() + i * 9ul, 9ul};
58 if (calc_sp) {
59 auto const trace = view[0] + view[4] + view[8];
60 out.emplace_back(trace / 3.);
61 } else {
62 auto const flat_matrix = std::vector<double>(view.begin(), view.end());
63 out.emplace_back(flat_matrix);
64 }
65 }
66 return out;
67 };
68
69 auto const get_obs_contrib =
70 [&get_obs_contribs](Utils::Span<double> const views) -> Variant {
71 return get_obs_contribs(views)[0];
72 };
73
74 std::unordered_map<std::string, Variant> dict;
75 dict["kinetic"] = get_obs_contrib(obs.kinetic);
76 dict["external_fields"] = get_obs_contrib(obs.external_fields);
77
78 {
79 auto values = std::vector<double>(obs_dim);
80 for (std::size_t i = 0; i < obs_dim; ++i) {
81 values[i] = obs.accumulate(0., i);
82 }
83 dict["total"] = get_obs_contrib({values.data(), obs_dim});
84 }
85
86 auto const n_bonds = static_cast<int>(::bonded_ia_params.get_next_key());
87 for (int bond_id = 0; bond_id < n_bonds; ++bond_id) {
88 if (::bonded_ia_params.get_zero_based_type(bond_id) != 0) {
89 dict["bonded," + std::to_string(bond_id)] =
90 get_obs_contrib(obs.bonded_contribution(bond_id));
91 }
92 }
93
94 auto const n_nonbonded =
95 system.nonbonded_ias->get_max_seen_particle_type() + 1;
96 for (int i = 0; i < n_nonbonded; ++i) {
97 for (int j = i; j < n_nonbonded; ++j) {
98 auto const indices = std::to_string(i) + "," + std::to_string(j);
99 dict["non_bonded_intra," + indices] =
100 get_obs_contrib(obs.non_bonded_intra_contribution(i, j));
101 dict["non_bonded_inter," + indices] =
102 get_obs_contrib(obs.non_bonded_inter_contribution(i, j));
103 }
104 }
105
106#ifdef ELECTROSTATICS
107 {
108 auto const values = get_obs_contribs(obs.coulomb);
109 for (std::size_t i = 0; i < values.size(); ++i) {
110 dict["coulomb," + std::to_string(i)] = values[i];
111 }
112 }
113#endif // ELECTROSTATICS
114
115#ifdef DIPOLES
116 {
117 auto const values = get_obs_contribs(obs.dipolar);
118 for (std::size_t i = 0; i < values.size(); ++i) {
119 dict["dipolar," + std::to_string(i)] = values[i];
120 }
121 }
122#endif // DIPOLES
123
124#ifdef VIRTUAL_SITES
125 {
126 auto const values = get_obs_contribs(obs.virtual_sites);
127 for (std::size_t i = 0; i < values.size(); ++i) {
128 dict["virtual_sites," + std::to_string(i)] = values[i];
129 }
130 }
131#endif // VIRTUAL_SITES
132
133 return dict;
134}
135
137 VariantMap const &parameters) {
138 auto &system = get_system();
139 if (name == "calculate_energy") {
140 auto const obs = system.calculate_energy();
141 return get_summary(system, *obs, false);
142 }
143 if (name == "calculate_scalar_pressure") {
144 auto const obs = system.calculate_pressure();
145 return get_summary(system, *obs, true);
146 }
147 if (name == "calculate_pressure_tensor") {
148 auto const obs = system.calculate_pressure();
149 return get_summary(system, *obs, false);
150 }
151 return {};
152}
153
154} // namespace Analysis
155} // namespace ScriptInterface
BondedInteractionsMap bonded_ia_params
Field containing the parameters of the bonded ia types.
Data structures for bonded interactions.
Observable for the pressure and energy.
Utils::Span< double > kinetic
Contribution from linear and angular kinetic energy (accumulated).
auto get_chunk_size() const
Utils::Span< double > dipolar
Contribution(s) from dipolar interactions.
Utils::Span< double > bonded_contribution(int bond_id) const
Get contribution from a bonded interaction.
Utils::Span< double > non_bonded_intra_contribution(int type1, int type2) const
Get contribution from a non-bonded intramolecular interaction.
double accumulate(double acc=0.0, std::size_t column=0) const
Accumulate values.
Utils::Span< double > virtual_sites
Contribution from virtual sites (accumulated).
Utils::Span< double > external_fields
Contribution from external fields (accumulated).
Utils::Span< double > non_bonded_inter_contribution(int type1, int type2) const
Get contribution from a non-bonded intermolecular interaction.
Utils::Span< double > coulomb
Contribution(s) from Coulomb interactions.
Variant do_call_method(std::string const &name, VariantMap const &parameters) override
boost::string_ref name() const
Main system class.
std::shared_ptr< InteractionsNonBonded > nonbonded_ias
A stripped-down version of std::span from C++17.
Definition Span.hpp:38
DEVICE_QUALIFIER constexpr pointer data() const
Definition Span.hpp:108
This file contains the defaults for ESPResSo.
static auto get_summary(::System::System const &system, Observable_stat const &obs, bool const calc_sp)
Generate an observable summary.
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:82
boost::make_recursive_variant< None, bool, int, std::size_t, double, std::string, ObjectRef, Utils::Vector3b, Utils::Vector3i, Utils::Vector2d, Utils::Vector3d, Utils::Vector4d, std::vector< int >, std::vector< double >, std::vector< boost::recursive_variant_ >, std::unordered_map< int, boost::recursive_variant_ >, std::unordered_map< std::string, boost::recursive_variant_ > >::type Variant
Possible types for parameters.
Definition Variant.hpp:80
Various procedures concerning interactions between particles.