40#include <boost/mpi/collectives/all_reduce.hpp>
49#include <unordered_map>
57 int chain_length,
int n_chains) {
60 throw std::domain_error(
"Chain analysis needs at least 1 chain");
62 if (chain_length <= 0) {
63 throw std::domain_error(
"Chain analysis needs at least 1 bead per chain");
66 auto n_particles_local = 0;
67 for (
int i = 0; i < n_chains; ++i) {
68 for (
int j = 0; j < chain_length; ++j) {
69 auto const pid = chain_start + i * chain_length + j;
71 if (ptr !=
nullptr and not ptr->is_ghost()) {
76 auto const n_particles_total = boost::mpi::all_reduce(
79 if (n_particles_total != n_chains * chain_length) {
80 for (
int i = 0; i < chain_length * n_chains; ++i) {
81 auto const pid = chain_start + i;
84 if (ptr !=
nullptr and not ptr->is_ghost()) {
87 auto const total_count =
88 boost::mpi::all_reduce(
::comm_cart, local_count, std::plus<int>{});
89 if (total_count == 0) {
90 std::stringstream error_msg;
91 error_msg <<
"Particle with id " << pid <<
" does not exist; "
92 <<
"cannot perform analysis on the range chain_start="
93 << chain_start <<
", number_of_chains=" << n_chains
94 <<
", chain_length=" << chain_length <<
". "
95 <<
"Please provide a contiguous range of particle ids.";
96 throw std::runtime_error(error_msg.str());
108void Analysis::check_particle_type(
int p_type)
const {
109 auto const &nonbonded_ias =
get_system().nonbonded_ias;
110 if (p_type < 0 or p_type > nonbonded_ias->get_max_seen_particle_type()) {
111 std::stringstream error_msg;
112 error_msg <<
"Particle type " << p_type <<
" does not exist";
113 throw std::invalid_argument(error_msg.str());
119 if (
name ==
"linear_momentum") {
125 if (
name ==
"particle_energy") {
128 auto const local = system.particle_short_range_energy_contribution(pid);
131 if (
name ==
"particle_bond_energy") {
136 auto const local = system.particle_bond_energy(pid, bond_id, partners);
139 if (
name ==
"potential_energy") {
140 auto const &obs =
get_system().calculate_energy();
141 return obs.accumulate(-(obs.kinetic_lin[0] + obs.kinetic_rot[0]));
143 if (
name ==
"particle_neighbor_pids") {
145 system.on_observable_calc();
146 std::unordered_map<int, std::vector<int>> dict;
150 std::ranges::for_each(neighbor_pids, [&dict](
auto const &
nbhood) {
157 if (
name ==
"dpd_pressure") {
159 return result.as_vector();
162 if (
name ==
"min_dist") {
165 for (
auto const p_type : p_types1) {
168 for (
auto const p_type : p_types2) {
173 if (
name ==
"center_of_mass") {
179 check_particle_type(p_type);
186 if (
name ==
"angular_momentum") {
195 if (
name ==
"nbhood") {
201 if (
name ==
"calc_re") {
203 auto const chain_start =
get_value<int>(parameters,
"chain_start");
204 auto const chain_length =
get_value<int>(parameters,
"chain_length");
205 auto const n_chains =
get_value<int>(parameters,
"number_of_chains");
206 check_topology(*system.cell_structure, chain_start, chain_length, n_chains);
207 auto const result =
calc_re(system, chain_start, chain_length, n_chains);
208 return std::vector<double>(result.begin(), result.end());
210 if (
name ==
"calc_rg") {
212 auto const chain_start =
get_value<int>(parameters,
"chain_start");
213 auto const chain_length =
get_value<int>(parameters,
"chain_length");
214 auto const n_chains =
get_value<int>(parameters,
"number_of_chains");
215 check_topology(*system.cell_structure, chain_start, chain_length, n_chains);
218 auto const result =
calc_rg(system, chain_start, chain_length, n_chains);
219 output =
Variant{std::vector<double>(result.begin(), result.end())};
223 if (
name ==
"calc_rh") {
225 auto const chain_start =
get_value<int>(parameters,
"chain_start");
226 auto const chain_length =
get_value<int>(parameters,
"chain_length");
227 auto const n_chains =
get_value<int>(parameters,
"number_of_chains");
228 check_topology(*system.cell_structure, chain_start, chain_length, n_chains);
230 if (chain_length < 2) {
231 throw std::domain_error(
232 "Hydrodynamic radius is undefined for chains shorter than 2 beads");
235 auto const result =
calc_rh(system, chain_start, chain_length, n_chains);
236 return std::vector<double>(result.begin(), result.end());
238 if (
name ==
"gyration_tensor") {
242 for (
auto const p_type : p_types) {
243 check_particle_type(p_type);
246 result = std::vector<double>(mat.begin(), mat.end());
250 if (
name ==
"moment_of_inertia_matrix") {
254 check_particle_type(p_type);
260 if (
name ==
"structure_factor") {
265 throw std::domain_error(
"order has to be a strictly positive number");
267 for (
auto const p_type : p_types) {
273 if (
name ==
"distribution") {
274 auto const &box_l =
get_system().box_geo->length();
275 auto const r_max_limit =
276 0.5 * std::min(std::min(box_l[0], box_l[1]), box_l[2]);
283 if (log_flag and r_min <= 0.) {
284 throw std::domain_error(
"Parameter 'r_min' must be > 0");
287 throw std::domain_error(
"Parameter 'r_min' must be >= 0");
289 if (r_min >= r_max) {
290 throw std::domain_error(
"Parameter 'r_max' must be > 'r_min'");
292 if (r_max > r_max_limit) {
293 throw std::domain_error(
"Parameter 'r_max' must be <= box_l / 2");
296 throw std::domain_error(
"Parameter 'r_bins' must be >= 1");
299 auto const p_types1 =
301 auto const p_types2 =
303 for (
auto const p_type : p_types1) {
306 for (
auto const p_type : p_types2) {
311 r_bins, log_flag, int_flag));
313 if (
name ==
"calculate_energy") {
314 return m_obs_stat->do_call_method(
"calculate_energy", {});
316 if (
name ==
"calculate_scalar_pressure") {
317 return m_obs_stat->do_call_method(
"calculate_scalar_pressure", {});
319 if (
name ==
"calculate_pressure_tensor") {
320 return m_obs_stat->do_call_method(
"calculate_pressure_tensor", {});
322 if (
name ==
"_observable_stat_test_fallthrough") {
324 return m_obs_stat->do_call_method(
"unknown", {});
327 if (
name ==
"get_instantaneous_pressure") {
328 return get_system().npt_inst_pressure->p_inst[0];
330 if (
name ==
"get_instantaneous_pressure_virial") {
331 return get_system().npt_inst_pressure->p_inst[1];
Vector implementation and trait types for boost qvm interoperability.
std::vector< NeighborPIDs > get_neighbor_pids(System::System const &system)
Returns pairs of particle ids and neighbor particle id lists.
This file contains everything related to the global cell structure / cell system.
Describes a cell structure / cell system.
Particle * get_local_particle(int id)
Get a local particle by id.
Variant do_call_method(std::string const &name, VariantMap const ¶meters) override
virtual void parallel_try_catch(std::function< void()> const &cb) const =0
Context * context() const
Responsible context.
std::string_view name() const
auto const & get_system() const
boost::mpi::communicator comm_cart
The communicator.
This file contains the asynchronous MPI communication.
Utils::Vector9d dpd_pressure(System::System &system, boost::mpi::communicator const &comm)
Pressure tensor contribution of the DPD interaction.
Routines to use DPD as thermostat or pair force soddemann03a.
static void check_topology(CellStructure const &cell_structure, int chain_start, int chain_length, int n_chains)
Check if a contiguous range of particle ids exists.
make_recursive_variant< ObjectRef > Variant
Possible types for parameters.
T get_value(Variant const &v)
Extract value of specific type T from a Variant.
auto make_unordered_map_of_variants(std::unordered_map< K, V > const &v)
T get_value_or(VariantMap const &vals, std::string const &name, T const &default_)
Get a value from a VariantMap by name, or return a default value if it does not exist.
T mpi_reduce_sum(boost::mpi::communicator const &comm, T const &result)
Reduce object by sum on the head node.
auto make_vector_of_variants(std::vector< T > const &v)
std::unordered_map< std::string, Variant > VariantMap
void gather_buffer(std::vector< T, Allocator > &buffer, boost::mpi::communicator const &comm, int root=0)
Gather buffer with different size on each node.
T reduce_optional(boost::mpi::communicator const &comm, std::optional< T > const &result)
Reduce an optional on the head node.
Various procedures concerning interactions between particles.
Exports for the NpT code.
Utils::Vector3d center_of_mass(System::System const &system, int p_type)
Calculate the center of mass of particles of a certain type.
Utils::Vector3d angular_momentum(System::System const &system, int p_type)
Calculate the angular momentum of particles of a certain type.
std::vector< int > nbhood(System::System const &system, Utils::Vector3d const &pos, double dist)
Find all particles within a given radius dist around a position pos.
Utils::Vector9d moment_of_inertia_matrix(System::System const &system, int p_type)
Calculate the moment of inertia of particles of a certain type.
Utils::Vector9d gyration_tensor(System::System const &system, std::vector< int > const &p_types)
Calculate the gyration tensor of particles of certain types.
Utils::Vector3d calc_linear_momentum(System::System const &system, bool include_particles, bool include_lbfluid)
Calculate total momentum of the system (particles & LB fluid).
std::vector< std::vector< double > > structure_factor(System::System const &system, std::vector< int > const &p_types, int order)
Calculate the spherically averaged structure factor.
double mindist(System::System const &system, std::vector< int > const &set1, std::vector< int > const &set2)
Calculate the minimal distance of two particles with types in set1 and set2, respectively.
std::vector< std::vector< double > > calc_part_distribution(System::System const &system, std::vector< int > const &p1_types, std::vector< int > const &p2_types, double r_min, double r_max, int r_bins, bool log_flag, bool int_flag)
Calculate the distribution of particles around others.
Statistical tools to analyze simulations.
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.