ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
statistics_chain.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/** \file
22 * Implementation of \ref statistics_chain.hpp "statistics_chain.hpp".
23 */
24
26
27#include "BoxGeometry.hpp"
28#include "Particle.hpp"
30#include "communication.hpp"
31#include "system/System.hpp"
32
33#include <utils/Vector.hpp>
34#include <utils/math/sqr.hpp>
36
37#include <boost/mpi/collectives/all_reduce.hpp>
38
39#include <array>
40#include <cassert>
41#include <cmath>
42#include <functional>
43#include <stdexcept>
44#include <unordered_map>
45#include <vector>
46
47/**
48 * @brief Gather particle properties (or any derived quantities) on MPI rank 0.
49 * The data is collected into an unordered map.
50 */
51template <typename T> struct GatherParticleTraits {
52private:
53 std::vector<int> buffer_pid{};
54 std::vector<T> buffer_obs{};
55
56public:
57 virtual T kernel(Particle const &) const = 0;
58 virtual ~GatherParticleTraits() = default;
59
60 void fetch(CellStructure const &cell_structure, int pid) {
61 auto const ptr = cell_structure.get_local_particle(pid);
62 if (ptr != nullptr and not ptr->is_ghost()) {
63 buffer_pid.emplace_back(pid);
64 buffer_obs.emplace_back(kernel(*ptr));
65 }
66 }
67
68 auto join() {
69 std::unordered_map<int, T> map{};
72 if (::comm_cart.rank() == 0) {
73 for (std::size_t i = 0u; i < buffer_pid.size(); ++i) {
74 map[buffer_pid[i]] = buffer_obs[i];
75 }
76 }
77 buffer_pid.clear();
78 buffer_obs.clear();
79 return map;
80 }
81};
82
83struct GatherPos : public GatherParticleTraits<Utils::Vector3d> {
84 ~GatherPos() override = default;
86 GatherPos(BoxGeometry const &box_geo) : m_box_geo{box_geo} {}
87 Utils::Vector3d kernel(Particle const &p) const override {
89 }
90};
91
92struct GatherCom : public GatherParticleTraits<Utils::Vector3d> {
93 ~GatherCom() override = default;
95 GatherCom(BoxGeometry const &box_geo) : m_box_geo{box_geo} {}
96 Utils::Vector3d kernel(Particle const &p) const override {
97 return m_box_geo.unfolded_position(p.pos(), p.image_box()) * p.mass();
98 }
99};
100
101struct GatherMass : public GatherParticleTraits<double> {
102 ~GatherMass() override = default;
103 double kernel(Particle const &p) const override { return p.mass(); }
104};
105
106std::array<double, 4> calc_re(System::System const &system, int chain_start,
107 int chain_length, int n_chains) {
108 auto const &cell_structure = *system.cell_structure;
109 GatherPos prefetch{*system.box_geo};
110 double dist = 0.0, dist2 = 0.0, dist4 = 0.0;
111 std::array<double, 4> re{};
112
113 for (int i = 0; i < n_chains; i++) {
114 auto const pid2 = chain_start + i * chain_length;
115 auto const pid1 = pid2 + chain_length - 1;
116 prefetch.fetch(cell_structure, pid1);
117 prefetch.fetch(cell_structure, pid2);
118 }
119 auto const map = prefetch.join();
120 if (::comm_cart.rank() == 0) {
121 for (int i = 0; i < n_chains; i++) {
122 auto const pid2 = chain_start + i * chain_length;
123 auto const pid1 = pid2 + chain_length - 1;
124 auto const norm2 = (map.at(pid1) - map.at(pid2)).norm2();
125 dist += sqrt(norm2);
126 dist2 += norm2;
127 dist4 += norm2 * norm2;
128 }
129 auto const tmp = static_cast<double>(n_chains);
130 re[0] = dist / tmp;
131 re[2] = dist2 / tmp;
132 re[1] = (n_chains == 1) ? 0. : std::sqrt(re[2] - Utils::sqr(re[0]));
133 re[3] = (n_chains == 1) ? 0. : std::sqrt(dist4 / tmp - Utils::sqr(re[2]));
134 }
135 return re;
136}
137
138std::array<double, 4> calc_rg(System::System const &system, int chain_start,
139 int chain_length, int n_chains) {
140 auto const &cell_structure = *system.cell_structure;
141 GatherPos prefetch_pos{*system.box_geo};
142 GatherCom prefetch_com{*system.box_geo};
144 double r_G = 0.0, r_G2 = 0.0, r_G4 = 0.0;
145 std::array<double, 4> rg{};
146
147 auto has_virtual = false;
148 for (int i = 0; i < n_chains * chain_length; ++i) {
149 auto const pid = chain_start + i;
150 auto const ptr = cell_structure.get_local_particle(pid);
151 if (ptr != nullptr and not ptr->is_ghost() and ptr->is_virtual()) {
152 has_virtual = true;
153 break;
154 }
155 }
156 if (boost::mpi::all_reduce(::comm_cart, has_virtual, std::logical_or<>{})) {
157 throw std::runtime_error(
158 "Center of mass is not well-defined for chains including virtual "
159 "sites. Virtual sites do not have a meaningful mass.");
160 }
161
162 for (int i = 0; i < n_chains * chain_length; ++i) {
163 auto const pid = chain_start + i;
164 prefetch_com.fetch(cell_structure, pid);
165 prefetch_pos.fetch(cell_structure, pid);
166 prefetch_mass.fetch(cell_structure, pid);
167 }
168
169 auto const map_pos = prefetch_pos.join();
170 auto const map_com = prefetch_com.join();
171 auto const map_mass = prefetch_mass.join();
172 if (::comm_cart.rank() == 0) {
173 for (int i = 0; i < n_chains; i++) {
174 double M = 0.0;
176 for (int j = 0; j < chain_length; j++) {
177 auto const pid = chain_start + i * chain_length + j;
178 r_CM += map_com.at(pid);
179 M += map_mass.at(pid);
180 }
181 r_CM /= M;
182 double tmp = 0.0;
183 for (int j = 0; j < chain_length; ++j) {
184 auto const pid = chain_start + i * chain_length + j;
185 auto const d = map_pos.at(pid) - r_CM;
186 tmp += d.norm2();
187 }
188 tmp /= static_cast<double>(chain_length);
189 r_G += sqrt(tmp);
190 r_G2 += tmp;
191 r_G4 += tmp * tmp;
192 }
193 auto const tmp = static_cast<double>(n_chains);
194 rg[0] = r_G / tmp;
195 rg[2] = r_G2 / tmp;
196 rg[1] = (n_chains == 1) ? 0. : std::sqrt(rg[2] - Utils::sqr(rg[0]));
197 rg[3] = (n_chains == 1) ? 0. : std::sqrt(r_G4 / tmp - Utils::sqr(rg[2]));
198 }
199 return rg;
200}
201
202std::array<double, 2> calc_rh(System::System const &system, int chain_start,
203 int chain_length, int n_chains) {
204 assert(chain_length >= 2);
205 auto const &cell_structure = *system.cell_structure;
206 GatherPos prefetch{*system.box_geo};
207 double r_H = 0.0, r_H2 = 0.0;
208 std::array<double, 2> rh{};
209
210 auto const chain_l = static_cast<double>(chain_length);
211 auto const prefac = 0.5 * chain_l * (chain_l - 1.);
212 for (int p = 0; p < n_chains; p++) {
213 for (int i = chain_start + chain_length * p;
214 i < chain_start + chain_length * (p + 1); i++) {
215 prefetch.fetch(cell_structure, i);
216 for (int j = i + 1; j < chain_start + chain_length * (p + 1); j++) {
217 prefetch.fetch(cell_structure, j);
218 }
219 }
220 }
221 auto const map = prefetch.join();
222 if (::comm_cart.rank() == 0) {
223 for (int p = 0; p < n_chains; p++) {
224 double ri = 0.0;
225 for (int i = chain_start + chain_length * p;
226 i < chain_start + chain_length * (p + 1); i++) {
227 for (int j = i + 1; j < chain_start + chain_length * (p + 1); j++) {
228 ri += 1.0 / (map.at(i) - map.at(j)).norm();
229 }
230 }
231 auto const tmp = prefac / ri;
232 r_H += tmp;
233 r_H2 += tmp * tmp;
234 }
235 auto const tmp = static_cast<double>(n_chains);
236 rh[0] = r_H / tmp;
237 rh[1] = (n_chains == 1) ? 0. : std::sqrt(r_H2 / tmp - Utils::sqr(rh[0]));
238 }
239 return rh;
240}
Vector implementation and trait types for boost qvm interoperability.
auto unfolded_position(Utils::Vector3d const &pos, Utils::Vector3i const &image_box) const
Unfold particle coordinates to image box.
Describes a cell structure / cell system.
Particle * get_local_particle(int id)
Get a local particle by id.
Main system class.
DEVICE_QUALIFIER constexpr reference at(size_type i)
Definition Array.hpp:97
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
boost::mpi::communicator comm_cart
The communicator.
void gather_buffer(std::vector< T, Allocator > &buffer, boost::mpi::communicator const &comm, int root=0)
Gather buffer with different size on each node.
DEVICE_QUALIFIER constexpr T sqr(T x)
Calculates the SQuaRe of x.
Definition sqr.hpp:28
std::array< double, 4 > calc_rg(System::System const &system, int chain_start, int chain_length, int n_chains)
Calculate the radius of gyration.
std::array< double, 2 > calc_rh(System::System const &system, int chain_start, int chain_length, int n_chains)
Calculate the hydrodynamic radius (ref.
std::array< double, 4 > calc_re(System::System const &system, int chain_start, int chain_length, int n_chains)
Calculate the end-to-end-distance.
This file contains the code for statistics on chains.
GatherCom(BoxGeometry const &box_geo)
BoxGeometry const & m_box_geo
Utils::Vector3d kernel(Particle const &p) const override
~GatherCom() override=default
~GatherMass() override=default
double kernel(Particle const &p) const override
Gather particle properties (or any derived quantities) on MPI rank 0.
virtual T kernel(Particle const &) const =0
void fetch(CellStructure const &cell_structure, int pid)
virtual ~GatherParticleTraits()=default
BoxGeometry const & m_box_geo
Utils::Vector3d kernel(Particle const &p) const override
~GatherPos() override=default
GatherPos(BoxGeometry const &box_geo)
Struct holding all information for one particle.
Definition Particle.hpp:436
constexpr auto const & pos() const
Definition Particle.hpp:476
constexpr auto const & mass() const
Definition Particle.hpp:500
constexpr auto const & image_box() const
Definition Particle.hpp:489