ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
core/galilei/ComFixed.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017-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 "ParticleRange.hpp"
23#include "communication.hpp"
24
25#include <utils/Vector.hpp>
26
27#include <boost/mpi/collectives/all_reduce.hpp>
28#include <boost/mpi/communicator.hpp>
29
30#include <algorithm>
31#include <cstddef>
32#include <functional>
33#include <ranges>
34#include <unordered_map>
35#include <vector>
36
37class ComFixed {
38private:
39 std::unordered_map<int, std::size_t> m_type_index;
40
41 std::vector<Utils::Vector3d>
42 local_type_forces(ParticleRange const &particles) const {
43 std::vector<Utils::Vector3d> ret(m_type_index.size(), Utils::Vector3d{});
44
45 for (auto const &p : particles) {
46 /* Check if type is of interest */
47 auto it = m_type_index.find(p.type());
48 if (it != m_type_index.end()) {
49 ret[it->second] += p.force();
50 }
51 }
52
53 return ret;
54 }
55
56 std::vector<double> local_type_masses(ParticleRange const &particles) const {
57 std::vector<double> ret(m_type_index.size(), 0.);
58
59 for (auto const &p : particles) {
60 /* Check if type is of interest */
61 auto it = m_type_index.find(p.type());
62 if (it != m_type_index.end()) {
63 ret[it->second] += p.mass();
64 }
65 }
66
67 return ret;
68 }
69
70public:
71 ComFixed() = default;
72
73 void set_fixed_types(std::vector<int> const &p_types) {
74 m_type_index.clear();
75
76 for (auto const &p_type : p_types) {
77 m_type_index.try_emplace(p_type, m_type_index.size());
78 }
79 }
80
81 std::vector<int> get_fixed_types() const {
82 std::vector<int> res{};
83 std::ranges::copy(std::views::keys(m_type_index), std::back_inserter(res));
84 std::ranges::sort(res);
85 return res;
86 }
87
88 void apply(ParticleRange const &particles) const {
89 /* Bail out early if there is nothing to do. */
90 if (m_type_index.empty())
91 return;
92
93 auto const local_forces = local_type_forces(particles);
94 auto const local_masses = local_type_masses(particles);
95
96 /* Total forces and masses of the types. */
97 std::vector<Utils::Vector3d> forces(m_type_index.size(), Utils::Vector3d{});
98 std::vector<double> masses(m_type_index.size(), 0.0);
99
100 /* Add contributions from all nodes and redistribute them to all. */
101 boost::mpi::all_reduce(::comm_cart, local_forces.data(),
102 static_cast<int>(local_forces.size()), forces.data(),
103 std::plus<Utils::Vector3d>{});
104 boost::mpi::all_reduce(::comm_cart, local_masses.data(),
105 static_cast<int>(local_masses.size()), masses.data(),
106 std::plus<double>{});
107
108 for (auto &p : particles) {
109 /* Check if type is of interest */
110 auto it = m_type_index.find(p.type());
111 if (it != m_type_index.end()) {
112 auto const mass_frac = p.mass() / masses[it->second];
113 p.force() -= mass_frac * forces[it->second];
114 }
115 }
116 }
117};
Vector implementation and trait types for boost qvm interoperability.
ComFixed()=default
std::vector< int > get_fixed_types() const
void apply(ParticleRange const &particles) const
void set_fixed_types(std::vector< int > const &p_types)
A range of particles.
boost::mpi::communicator comm_cart
The communicator.
VectorXd< 3 > Vector3d
Definition Vector.hpp:207