Multi Resolution Molecular Dynamics 0.4.0
Multi Resolution Molecular Dynamics
Loading...
Searching...
No Matches
Shake.hpp
Go to the documentation of this file.
1// Copyright 2024 Sebastian Eibl
2// Copyright 2026 Julian Friedrich Hille
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// https://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#pragma once
17
18#include "assert/assert.hpp"
19#include "data/Atoms.hpp"
20#include "data/Bond.hpp"
21#include "data/Molecules.hpp"
22#include "datatypes.hpp"
23#include "util/Kokkos_grow.hpp"
24#include "util/math.hpp"
25
26namespace mrmd
27{
28namespace action
29{
30namespace impl
31{
35class Shake
36{
37private:
40 data::Atoms::force_t::atomic_access_slice force_;
42 VectorView updatedPos_;
43
44 real_t dtv_;
45 real_t dtf_;
46
47public:
48 struct UnconstraintUpdate
49 {
50 };
51 struct ApplyConstraint
52 {
53 };
54 struct RemoveBondVelocity
55 {
56 };
57
58 KOKKOS_INLINE_FUNCTION
59 void enforceVelocityConstraint(const idx_t idx, const idx_t jdx, const real_t eqDistance) const
60 {
62 real_t dist[3];
63 dist[0] = pos_(idx, 0) - pos_(jdx, 0);
64 dist[1] = pos_(idx, 1) - pos_(jdx, 1);
65 dist[2] = pos_(idx, 2) - pos_(jdx, 2);
67 auto distSq = util::dot3(dist, dist);
68
69 auto invMassI = 1_r / mass_(idx);
70 auto invMassJ = 1_r / mass_(jdx);
71 auto reducedMass = 1_r / (invMassI + invMassJ);
72
73 real_t relVel[3];
74 relVel[0] = vel_(idx, 0) - vel_(jdx, 0);
75 relVel[1] = vel_(idx, 1) - vel_(jdx, 1);
76 relVel[2] = vel_(idx, 2) - vel_(jdx, 2);
77
78 auto factor = util::dot3(relVel, dist) / distSq * reducedMass;
79
80 vel_(idx, 0) -= factor * dist[0] * invMassI;
81 vel_(idx, 1) -= factor * dist[1] * invMassI;
82 vel_(idx, 2) -= factor * dist[2] * invMassI;
83
84 vel_(jdx, 0) += factor * dist[0] * invMassJ;
85 vel_(jdx, 1) += factor * dist[1] * invMassJ;
86 vel_(jdx, 2) += factor * dist[2] * invMassJ;
87 }
88
89 KOKKOS_INLINE_FUNCTION
90 void enforcePositionalConstraint(const idx_t idx,
91 const idx_t jdx,
92 const real_t eqDistance) const
93 {
95 real_t dist[3];
96 dist[0] = pos_(idx, 0) - pos_(jdx, 0);
97 dist[1] = pos_(idx, 1) - pos_(jdx, 1);
98 dist[2] = pos_(idx, 2) - pos_(jdx, 2);
100 real_t distSq = util::dot3(dist, dist);
101
103 real_t updatedDist[3];
104 updatedDist[0] = updatedPos_(idx, 0) - updatedPos_(jdx, 0);
105 updatedDist[1] = updatedPos_(idx, 1) - updatedPos_(jdx, 1);
106 updatedDist[2] = updatedPos_(idx, 2) - updatedPos_(jdx, 2);
108 real_t updatedDistSq = util::dot3(updatedDist, updatedDist);
109
110 auto invMassI = 1_r / mass_(idx);
111 auto invMassJ = 1_r / mass_(jdx);
112
114 real_t a = util::sqr(invMassI + invMassJ) * distSq;
115 real_t b = 2_r * (invMassI + invMassJ) * util::dot3(updatedDist, dist);
116 real_t c = updatedDistSq - util::sqr(eqDistance);
117
118 real_t determinant = b * b - 4_r * a * c;
119 MRMD_DEVICE_ASSERT_GREATER(determinant, 1e-8);
120 determinant = Kokkos::max(0_r, determinant); // ensure positive determinant
121
122 // solve for lambda
123 auto lambda1 = (-b + std::sqrt(determinant)) / (2_r * a);
124 auto lambda2 = (-b - std::sqrt(determinant)) / (2_r * a);
125 auto lambda = std::abs(lambda1) < std::abs(lambda2) ? lambda1 : lambda2;
126
127 lambda /= dtf_;
128
129 force_(idx, 0) += lambda * dist[0];
130 force_(idx, 1) += lambda * dist[1];
131 force_(idx, 2) += lambda * dist[2];
132
133 force_(jdx, 0) -= lambda * dist[0];
134 force_(jdx, 1) -= lambda * dist[1];
135 force_(jdx, 2) -= lambda * dist[2];
136 }
137
138 KOKKOS_INLINE_FUNCTION
139 void operator()(UnconstraintUpdate, const idx_t idx) const
140 {
141 auto dtfm = dtf_ / mass_(idx);
142 updatedPos_(idx, 0) = pos_(idx, 0) + dtv_ * vel_(idx, 0) + dtfm * force_(idx, 0);
143 updatedPos_(idx, 1) = pos_(idx, 1) + dtv_ * vel_(idx, 1) + dtfm * force_(idx, 1);
144 updatedPos_(idx, 2) = pos_(idx, 2) + dtv_ * vel_(idx, 2) + dtfm * force_(idx, 2);
145 }
146
147 Shake(data::Atoms& atoms, const real_t& dt)
148 {
149 pos_ = atoms.getPos();
150 vel_ = atoms.getVel();
151 force_ = atoms.getForce();
152 mass_ = atoms.getMass();
153
154 util::grow(updatedPos_, idx_c(atoms.numLocalAtoms + atoms.numGhostAtoms));
155
156 dtv_ = dt;
157 dtf_ = 0.5_r * dt * dt;
158 }
159};
160} // namespace impl
161
170{
171private:
172 idx_t atomsPerMolecule_;
173 data::BondView bonds_;
174
175 idx_t numConstraintIterations_;
176
177public:
179 data::Atoms& atoms,
180 const real_t dt)
181 {
182 impl::Shake shake(atoms, dt);
183
184 for (int iteration = 0; iteration < numConstraintIterations_; ++iteration)
185 {
186 auto policy = Kokkos::RangePolicy<impl::Shake::UnconstraintUpdate>(
187 0, atoms.numLocalAtoms + atoms.numGhostAtoms);
188 Kokkos::parallel_for("Shake::UnconstraintUpdate", policy, shake);
189 Kokkos::fence();
190
191 auto moleculesAtomsOffset = molecules.getAtomsOffset();
192 auto moleculesNumAtoms = molecules.getNumAtoms();
193 auto bonds = bonds_;
194 auto applyBondsPolicy = Kokkos::RangePolicy<>(0, molecules.numLocalMolecules);
195 auto kernel = KOKKOS_LAMBDA(idx_t moleculeIdx)
196 {
197 auto atomsStart = moleculesAtomsOffset(moleculeIdx);
198 auto numAtoms = moleculesNumAtoms(moleculeIdx);
199 for (idx_t bondIdx = 0; bondIdx < bonds.extent(0); ++bondIdx)
200 {
201 MRMD_DEVICE_ASSERT_LESS(bonds(bondIdx).idx,
202 numAtoms,
203 "not enough atoms in molecule to satisfy bond");
204 MRMD_DEVICE_ASSERT_LESS(bonds(bondIdx).jdx,
205 numAtoms,
206 "not enough atoms in molecule to satisfy bond");
207 shake.enforcePositionalConstraint(atomsStart + bonds(bondIdx).idx,
208 atomsStart + bonds(bondIdx).jdx,
209 bonds(bondIdx).eqDistance);
210 }
211 };
212
213 Kokkos::parallel_for(
214 "MoleculeConstraints::enforcePositionalConstraints", applyBondsPolicy, kernel);
215 Kokkos::fence();
216 }
217 }
218
220 {
221 impl::Shake shake(atoms, dt);
222
223 auto moleculesAtomsOffset = molecules.getAtomsOffset();
224 auto moleculesNumAtoms = molecules.getNumAtoms();
225 auto bonds = bonds_;
226 auto applyBondsPolicy = Kokkos::RangePolicy<>(0, molecules.numLocalMolecules);
227 auto kernel = KOKKOS_LAMBDA(idx_t moleculeIdx)
228 {
229 auto atomsStart = moleculesAtomsOffset(moleculeIdx);
230 auto numAtoms = moleculesNumAtoms(moleculeIdx);
231 for (idx_t bondIdx = 0; bondIdx < bonds.extent(0); ++bondIdx)
232 {
234 bonds(bondIdx).idx, numAtoms, "not enough atoms in molecule to satisfy bond");
236 bonds(bondIdx).jdx, numAtoms, "not enough atoms in molecule to satisfy bond");
237 shake.enforceVelocityConstraint(atomsStart + bonds(bondIdx).idx,
238 atomsStart + bonds(bondIdx).jdx,
239 bonds(bondIdx).eqDistance);
240 }
241 };
242 Kokkos::parallel_for(
243 "MoleculeConstraints::enforceVelocityConstraints", applyBondsPolicy, kernel);
244 Kokkos::fence();
245 }
246
247 void setConstraints(const data::BondView& bonds) { bonds_ = bonds; }
248 void setConstraints(const data::BondView::host_mirror_type& bonds)
249 {
250 Kokkos::resize(bonds_, bonds.extent(0));
251 Kokkos::deep_copy(bonds_, bonds);
252 }
253
254 MoleculeConstraints(idx_t atomsPerMolecule, idx_t numConstraintIterations)
255 : atomsPerMolecule_(atomsPerMolecule), numConstraintIterations_(numConstraintIterations)
256 {
257 }
258};
259
260} // namespace action
261} // namespace mrmd
void enforceVelocityConstraints(data::Molecules &molecules, data::Atoms &atoms, const real_t dt)
Definition Shake.hpp:219
void enforcePositionalConstraints(data::Molecules &molecules, data::Atoms &atoms, const real_t dt)
Definition Shake.hpp:178
MoleculeConstraints(idx_t atomsPerMolecule, idx_t numConstraintIterations)
Definition Shake.hpp:254
void setConstraints(const data::BondView::host_mirror_type &bonds)
Definition Shake.hpp:248
void setConstraints(const data::BondView &bonds)
Definition Shake.hpp:247
idx_t numLocalAtoms
Definition Atoms.hpp:120
idx_t numGhostAtoms
Definition Atoms.hpp:121
typename AtomsT::template member_slice_type< MASS > mass_t
Definition Atoms.hpp:60
typename AtomsT::template member_slice_type< POS > pos_t
Definition Atoms.hpp:56
typename AtomsT::template member_slice_type< VEL > vel_t
Definition Atoms.hpp:57
KOKKOS_FORCEINLINE_FUNCTION atoms_offset_t getAtomsOffset() const
Definition Molecules.hpp:67
KOKKOS_FORCEINLINE_FUNCTION num_atoms_t getNumAtoms() const
Definition Molecules.hpp:68
idx_t numLocalMolecules
Definition Molecules.hpp:120
#define MRMD_DEVICE_ASSERT_GREATER(...)
Definition device.hpp:471
#define MRMD_DEVICE_ASSERT_LESS(...)
Definition device.hpp:472
Everything to evolve the simulation physically.
Kokkos::View< Bond * > BondView
Definition Bond.hpp:30
DeviceMolecules Molecules
Definition Molecules.hpp:176
DeviceAtoms Atoms
Definition Atoms.hpp:177
KOKKOS_INLINE_FUNCTION real_t sqr(const real_t &sqr)
Definition math.hpp:29
void grow(Kokkos::View< T, P... > &view, idx_t size, real_t safetyMargin=1.1_r)
Definition Kokkos_grow.hpp:25
KOKKOS_INLINE_FUNCTION real_t dot3(real_t const *const a, real_t const *const b)
Definition math.hpp:24
Top-level namespace of MRMD.
KOKKOS_INLINE_FUNCTION constexpr idx_t idx_c(const T &val)
Definition datatypes.hpp:93
int64_t idx_t
Definition datatypes.hpp:91
Kokkos::View< real_t *[3]> VectorView
Definition datatypes.hpp:163
double real_t
Definition datatypes.hpp:99