ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
thermalized_bond_kernel.hpp
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
22#pragma once
23
24#include "thermalized_bond.hpp"
25
26#include "Particle.hpp"
27#include "random.hpp"
28#include "thermostat.hpp"
29
30#include <utils/Vector.hpp>
31
32#include <cmath>
33#include <optional>
34#include <tuple>
35
36/** Separately thermalizes the com and distance of a particle pair.
37 * @param[in] p1 First particle.
38 * @param[in] p2 Second particle.
39 * @param[in] dx Distance between the particles.
40 * @return the forces on @p p1 and @p p2
41 */
42inline std::optional<std::tuple<Utils::Vector3d, Utils::Vector3d>>
44 Utils::Vector3d const &dx) const {
45 return forces(p1.mass(), p2.mass(), p1.v(), p2.v(), p1.id(), p2.id(), dx);
46}
47
48inline std::optional<std::tuple<Utils::Vector3d, Utils::Vector3d>>
49ThermalizedBond::forces(double const mass1, double const mass2,
50 Utils::Vector<double, 3> const vel1,
51 Utils::Vector<double, 3> const vel2, int const id1,
52 int const id2, Utils::Vector3d const &dx) const {
53 // Bond broke?
54 if (r_cut > 0.0 && dx.norm() > r_cut) {
55 return {};
56 }
57
58 auto const mass_tot = mass1 + mass2;
59 auto const mass_tot_inv = 1.0 / mass_tot;
60 auto const sqrt_mass_tot = sqrt(mass_tot);
61 auto const sqrt_mass_red = sqrt(mass1 * mass2 / mass_tot);
62 auto const com_vel = mass_tot_inv * (mass1 * vel1 + mass2 * vel2);
63 auto const dist_vel = vel2 - vel1;
64 auto const thermostat_view = m_thermostat.lock();
65 assert(thermostat_view);
66 auto const &thermalized_bond = *thermostat_view->thermalized_bond;
67
68 Utils::Vector3d force1{};
69 Utils::Vector3d force2{};
70 auto const noise = Random::noise_uniform<RNGSalt::THERMALIZED_BOND>(
71 thermalized_bond.rng_counter(), thermalized_bond.rng_seed(), id1, id2);
72
73 for (unsigned int i = 0u; i < 3u; ++i) {
74 double force_lv_com, force_lv_dist;
75
76 // Langevin thermostat for center of mass
77 if (pref2_com > 0.0) {
78 force_lv_com =
79 -pref1_com * com_vel[i] + sqrt_mass_tot * pref2_com * noise[i];
80 } else {
81 force_lv_com = -pref1_com * com_vel[i];
82 }
83
84 // Langevin thermostat for distance p1->p2
85 if (pref2_dist > 0.0) {
86 force_lv_dist =
87 -pref1_dist * dist_vel[i] + sqrt_mass_red * pref2_dist * noise[i];
88 } else {
89 force_lv_dist = -pref1_dist * dist_vel[i];
90 }
91 // Add forces
92 force1[i] = mass1 * mass_tot_inv * force_lv_com - force_lv_dist;
93 force2[i] = mass2 * mass_tot_inv * force_lv_com + force_lv_dist;
94 }
95
96 return std::make_tuple(force1, force2);
97}
Vector implementation and trait types for boost qvm interoperability.
T norm() const
Definition Vector.hpp:159
Random number generation using Philox.
Struct holding all information for one particle.
Definition Particle.hpp:435
auto const & mass() const
Definition Particle.hpp:492
auto const & v() const
Definition Particle.hpp:473
auto const & id() const
Definition Particle.hpp:454
std::optional< std::tuple< Utils::Vector3d, Utils::Vector3d > > forces(Particle const &p1, Particle const &p2, Utils::Vector3d const &dx) const
Separately thermalizes the com and distance of a particle pair.
Routines to thermalize the center of mass and distance of a particle pair.