-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathdpd.cpp
More file actions
169 lines (144 loc) · 6.26 KB
/
Copy pathdpd.cpp
File metadata and controls
169 lines (144 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (C) 2010-2026 The ESPResSo project
* Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
* Max-Planck-Institute for Polymer Research, Theory Group
*
* This file is part of ESPResSo.
*
* ESPResSo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ESPResSo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** \file
* Implementation of dpd.hpp.
*/
#include <config/config.hpp>
#ifdef ESPRESSO_DPD
#include "dpd.hpp"
#include "BoxGeometry.hpp"
#include "cell_system/CellStructure.hpp"
#include "nonbonded_interactions/nonbonded_interaction_data.hpp"
#include "random.hpp"
#include "system/System.hpp"
#include "thermostat.hpp"
#include <utils/Vector.hpp>
#include <utils/math/tensor_product.hpp>
#include <utils/matrix.hpp>
#include <boost/mpi/collectives/reduce.hpp>
#include <cmath>
#include <functional>
#include <type_traits>
/** Return a random uniform 3D vector with the Philox thermostat.
* Random numbers depend on
* 1. dpd_rng_counter (initialized by seed) which is increased on integration
* 2. Salt (decorrelates different counters)
* 3. Two particle IDs (order-independent, decorrelates particles, gets rid of
* seed-per-node)
*/
Utils::Vector3d dpd_noise(DPDThermostat const &dpd, int pid1, int pid2) {
auto const pref = (pid1 < pid2) ? 1.0 : -1.0;
return pref * Random::noise_uniform<RNGSalt::SALT_DPD>(
dpd.rng_counter(), dpd.rng_seed(),
(pid1 < pid2) ? pid2 : pid1, (pid1 < pid2) ? pid1 : pid2);
}
void InteractionsNonBonded::dpd_init(double kT, double time_step) {
auto const max_type = get_max_seen_particle_type();
for (int type_a = 0; type_a <= max_type; type_a++) {
for (int type_b = type_a; type_b <= max_type; type_b++) {
auto &ia_params = get_ia_param(type_a, type_b);
ia_params.dpd.radial.pref =
sqrt(24.0 * kT * ia_params.dpd.radial.gamma / time_step);
ia_params.dpd.trans.pref =
sqrt(24.0 * kT * ia_params.dpd.trans.gamma / time_step);
}
}
}
Utils::Vector3d dpd_pair_force(
Utils::Vector3d const &p1_position, Utils::Vector3d const &p1_velocity,
int p1_id, Utils::Vector3d const &p2_position,
Utils::Vector3d const &p2_velocity, int p2_id, DPDThermostat const &dpd,
BoxGeometry const &box_geo, IA_parameters const &ia_params,
Utils::Vector3d const &d, double dist, double dist2) {
if (ia_params.dpd.radial.cutoff <= 0.0 && ia_params.dpd.trans.cutoff <= 0.0) {
return {};
}
auto const v21 = box_geo.velocity_difference(p1_position, p2_position,
p1_velocity, p2_velocity);
auto const noise_vec =
(ia_params.dpd.radial.pref > 0.0 || ia_params.dpd.trans.pref > 0.0)
? dpd_noise(dpd, p1_id, p2_id)
: Utils::Vector3d{};
auto const f_r = dpd_pair_force(ia_params.dpd.radial, v21, dist, noise_vec);
auto const f_t = dpd_pair_force(ia_params.dpd.trans, v21, dist, noise_vec);
/* Projection operator to radial direction */
auto const P = tensor_product(d / dist2, d);
/* This is equivalent to P * f_r + (1 - P) * f_t, but with
* doing only one matrix-vector multiplication */
auto const force = P * (f_r - f_t) + f_t;
return force;
}
static auto dpd_viscous_stress_local(System::System &system) {
auto const &box_geo = *system.box_geo;
auto const &nonbonded_ias = *system.nonbonded_ias;
auto &cell_structure = *system.cell_structure;
auto &dpd = *system.thermostat->dpd;
system.on_observable_calc();
Utils::Matrix<double, 3, 3> stress{};
cell_structure.non_bonded_loop([&stress, &box_geo, &nonbonded_ias,
&dpd](Particle const &p1, Particle const &p2,
Distance const &d) {
auto const v21 =
box_geo.velocity_difference(p1.pos(), p2.pos(), p1.v(), p2.v());
auto const &ia_params = nonbonded_ias.get_ia_param(p1.type(), p2.type());
auto const noise_vec =
(ia_params.dpd.radial.pref > 0.0 || ia_params.dpd.trans.pref > 0.0)
? dpd_noise(dpd, p1.id(), p2.id())
: Utils::Vector3d{};
auto const dist = std::sqrt(d.dist2);
auto const f_r = dpd_pair_force(ia_params.dpd.radial, v21, dist, noise_vec);
auto const f_t = dpd_pair_force(ia_params.dpd.trans, v21, dist, noise_vec);
/* Projection operator to radial direction */
auto const P = tensor_product(d.vec21 / d.dist2, d.vec21);
/* This is equivalent to P * f_r + (1 - P) * f_t, but with
* doing only one matrix-vector multiplication */
auto const f = P * (f_r - f_t) + f_t;
stress += tensor_product(d.vec21, f);
});
return stress;
}
Utils::Vector9d dpd_pressure_local(System::System &system) {
auto const local_stress = dpd_viscous_stress_local(system);
return -Utils::flatten(local_stress);
}
/**
* @brief Viscous stress tensor of the DPD interaction.
*
* This calculates the total viscous stress contribution of the
* DPD interaction. It contains only the dissipative contributions
* of the interaction without noise. It's calculated as the
* sum over all pair virials as
* \f[
* \sigma^{\nu\mu} = V^{-1}\sum_i \sum_{j < i} r_{i,j}^{\nu} (- \gamma_{i,j}
* v_{i,j})^{\mu} \f] where \f$\gamma_{i,j}\f$ is the (in general tensor valued)
* DPD friction coefficient for particles i and j, \f$v_{i,j}\f$, \f$r_{i,j}\f$
* are their relative velocity and distance and \f$V\f$ is the box volume.
*
* @return Stress tensor contribution.
*/
Utils::Vector9d dpd_stress(System::System &system,
boost::mpi::communicator const &comm) {
auto const local_stress = dpd_viscous_stress_local(system);
std::remove_const_t<decltype(local_stress)> global_stress{};
boost::mpi::reduce(comm, local_stress, global_stress, std::plus<>(), 0);
return Utils::flatten(global_stress) / system.box_geo->volume();
}
#endif // ESPRESSO_DPD