40 data::Atoms::force_t::atomic_access_slice force_;
48 struct UnconstraintUpdate
51 struct ApplyConstraint
54 struct RemoveBondVelocity
58 KOKKOS_INLINE_FUNCTION
59 void enforceVelocityConstraint(
const idx_t idx,
const idx_t jdx,
const real_t eqDistance)
const
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);
69 auto invMassI = 1_r / mass_(idx);
70 auto invMassJ = 1_r / mass_(jdx);
71 auto reducedMass = 1_r / (invMassI + invMassJ);
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);
78 auto factor =
util::dot3(relVel, dist) / distSq * reducedMass;
80 vel_(idx, 0) -= factor * dist[0] * invMassI;
81 vel_(idx, 1) -= factor * dist[1] * invMassI;
82 vel_(idx, 2) -= factor * dist[2] * invMassI;
84 vel_(jdx, 0) += factor * dist[0] * invMassJ;
85 vel_(jdx, 1) += factor * dist[1] * invMassJ;
86 vel_(jdx, 2) += factor * dist[2] * invMassJ;
89 KOKKOS_INLINE_FUNCTION
90 void enforcePositionalConstraint(
const idx_t idx,
92 const real_t eqDistance)
const
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);
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);
110 auto invMassI = 1_r / mass_(idx);
111 auto invMassJ = 1_r / mass_(jdx);
118 real_t determinant = b * b - 4_r * a * c;
120 determinant = Kokkos::max(0_r, determinant);
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;
129 force_(idx, 0) += lambda * dist[0];
130 force_(idx, 1) += lambda * dist[1];
131 force_(idx, 2) += lambda * dist[2];
133 force_(jdx, 0) -= lambda * dist[0];
134 force_(jdx, 1) -= lambda * dist[1];
135 force_(jdx, 2) -= lambda * dist[2];
138 KOKKOS_INLINE_FUNCTION
139 void operator()(UnconstraintUpdate,
const idx_t idx)
const
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);
149 pos_ = atoms.getPos();
150 vel_ = atoms.getVel();
151 force_ = atoms.getForce();
152 mass_ = atoms.getMass();
154 util::grow(updatedPos_,
idx_c(atoms.numLocalAtoms + atoms.numGhostAtoms));
157 dtf_ = 0.5_r * dt * dt;
172 idx_t atomsPerMolecule_;
175 idx_t numConstraintIterations_;
182 impl::Shake shake(atoms, dt);
184 for (
int iteration = 0; iteration < numConstraintIterations_; ++iteration)
186 auto policy = Kokkos::RangePolicy<impl::Shake::UnconstraintUpdate>(
188 Kokkos::parallel_for(
"Shake::UnconstraintUpdate", policy, shake);
195 auto kernel = KOKKOS_LAMBDA(
idx_t moleculeIdx)
197 auto atomsStart = moleculesAtomsOffset(moleculeIdx);
198 auto numAtoms = moleculesNumAtoms(moleculeIdx);
199 for (
idx_t bondIdx = 0; bondIdx < bonds.extent(0); ++bondIdx)
203 "not enough atoms in molecule to satisfy bond");
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);
213 Kokkos::parallel_for(
214 "MoleculeConstraints::enforcePositionalConstraints", applyBondsPolicy, kernel);
221 impl::Shake shake(atoms, dt);
227 auto kernel = KOKKOS_LAMBDA(
idx_t moleculeIdx)
229 auto atomsStart = moleculesAtomsOffset(moleculeIdx);
230 auto numAtoms = moleculesNumAtoms(moleculeIdx);
231 for (
idx_t bondIdx = 0; bondIdx < bonds.extent(0); ++bondIdx)
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);
242 Kokkos::parallel_for(
243 "MoleculeConstraints::enforceVelocityConstraints", applyBondsPolicy, kernel);
250 Kokkos::resize(bonds_, bonds.extent(0));
251 Kokkos::deep_copy(bonds_, bonds);
255 : atomsPerMolecule_(atomsPerMolecule), numConstraintIterations_(numConstraintIterations)
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