Multi Resolution Molecular Dynamics 0.4.0
Multi Resolution Molecular Dynamics
Loading...
Searching...
No Matches
LennardJones.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 "data/Atoms.hpp"
20#include "datatypes.hpp"
21
22namespace mrmd::action::impl
23{
32class CappedLennardJonesPotential
33{
34public:
35 struct PrecomputedValues
36 {
37 real_t ff1;
38 real_t ff2;
39 real_t ef1;
40 real_t ef2;
41 real_t rcSqr;
42 real_t cappingDistance;
43 real_t cappingDistanceSqr;
44 real_t cappingCoeff;
45 real_t shift;
46 real_t energyAtCappingPoint;
47 };
48
49 struct ForceAndEnergy
50 {
51 real_t forceFactor;
52 real_t energy;
53 };
54
55private:
56 Kokkos::View<PrecomputedValues*> precomputedValues_;
57 bool isShifted_;
58
59public:
60 KOKKOS_INLINE_FUNCTION
61 ForceAndEnergy computeForceAndEnergy(const real_t& distSqr, const idx_t& typeIdx) const
62 {
63 ForceAndEnergy ret;
64 if (distSqr >= precomputedValues_(typeIdx).cappingDistanceSqr)
65 {
66 // normal LJ calculation
67 auto frac2 = 1_r / distSqr;
68 auto frac6 = frac2 * frac2 * frac2;
69 ret.forceFactor =
70 frac6 *
71 (precomputedValues_(typeIdx).ff1 * frac6 - precomputedValues_(typeIdx).ff2) * frac2;
72 ret.energy = frac6 * (precomputedValues_(typeIdx).ef1 * frac6 -
73 precomputedValues_(typeIdx).ef2) -
74 precomputedValues_(typeIdx).shift;
75 return ret;
76 }
77
78 // force capping
79 auto dist = std::sqrt(distSqr);
80 ret.forceFactor = precomputedValues_(typeIdx).cappingCoeff / dist;
81 ret.energy = precomputedValues_(typeIdx).energyAtCappingPoint -
82 (dist - precomputedValues_(typeIdx).cappingDistance) *
83 precomputedValues_(typeIdx).cappingCoeff -
84 precomputedValues_(typeIdx).shift;
85 return ret;
86 }
87
92 KOKKOS_FUNCTION
93 void operator()(const idx_t& typeIdx) const;
94
95 CappedLennardJonesPotential(const std::vector<real_t>& cappingDistance,
96 const std::vector<real_t>& rc,
97 const std::vector<real_t>& sigma,
98 const std::vector<real_t>& epsilon,
99 const idx_t& numTypes,
100 const bool isShifted);
101};
102} // namespace mrmd::action::impl
103
104namespace mrmd::action
105{
118{
119private:
120 impl::CappedLennardJonesPotential LJ_;
121 real_t rcSqr_;
123 data::Atoms::force_t::atomic_access_slice force_;
125
126 HalfVerletList verletList_;
127
128 const idx_t numTypes_;
129
130 data::EnergyAndVirialReducer energyAndVirial_;
131
132public:
133 real_t getEnergy() const;
134 real_t getVirial() const;
135
136 void apply(data::Atoms& atoms, HalfVerletList& verletList);
137
138 template <TwoPositionsPredicate Pred>
139 void apply_if(const data::Atoms& atoms, const HalfVerletList& verletList, const Pred& pred);
140
141 LennardJones(const real_t rc,
142 const real_t& sigma,
143 const real_t& epsilon,
144 const real_t& cappingDistance = 0_r);
145
146 LennardJones(const std::vector<real_t>& cappingDistance,
147 const std::vector<real_t>& rc,
148 const std::vector<real_t>& sigma,
149 const std::vector<real_t>& epsilon,
150 const idx_t& numTypes,
151 const bool isShifted);
152};
153
154template <TwoPositionsPredicate Pred>
156 const HalfVerletList& verletList,
157 const Pred& pred)
158{
159 energyAndVirial_ = data::EnergyAndVirialReducer();
160 pos_ = atoms.getPos();
161 force_ = atoms.getForce();
162 type_ = atoms.getType();
163 verletList_ = verletList;
164
165 auto policy = Kokkos::RangePolicy<>(0, atoms.numLocalAtoms);
166
167 // avoid capturing this pointer
168 auto pos = pos_;
169 auto force = force_;
170 auto type = type_;
171 auto verletListLocal = verletList_;
172 auto rcSqr = rcSqr_;
173 auto LJ = LJ_;
174 auto numTypes = numTypes_;
175 auto predLocal = pred;
176
177 auto kernel = KOKKOS_LAMBDA(const idx_t idx, data::EnergyAndVirialReducer& energyAndVirial)
178 {
179 real_t posTmp[3];
180 posTmp[0] = pos(idx, 0);
181 posTmp[1] = pos(idx, 1);
182 posTmp[2] = pos(idx, 2);
183
184 real_t forceTmp[3] = {0_r, 0_r, 0_r};
185
186 const auto numNeighbors = idx_c(HalfNeighborList::numNeighbor(verletListLocal, idx));
187 for (idx_t n = 0; n < numNeighbors; ++n)
188 {
189 idx_t jdx = idx_c(HalfNeighborList::getNeighbor(verletListLocal, idx, n));
190 assert(0 <= jdx);
191
192 if (!predLocal(posTmp[0], posTmp[1], posTmp[2], pos(jdx, 0), pos(jdx, 1), pos(jdx, 2)))
193 continue;
194
195 auto dx = posTmp[0] - pos(jdx, 0);
196 auto dy = posTmp[1] - pos(jdx, 1);
197 auto dz = posTmp[2] - pos(jdx, 2);
198
199 auto distSqr = dx * dx + dy * dy + dz * dz;
200
201 if (distSqr > rcSqr) continue;
202
203 auto typeIdx = type(idx) * numTypes + type(jdx);
204 auto forceAndEnergy = LJ.computeForceAndEnergy(distSqr, typeIdx);
205 assert(!std::isnan(forceAndEnergy.forceFactor));
206 energyAndVirial.energy += forceAndEnergy.energy;
207 energyAndVirial.virial -= 0.5_r * forceAndEnergy.forceFactor * distSqr;
208
209 forceTmp[0] += dx * forceAndEnergy.forceFactor;
210 forceTmp[1] += dy * forceAndEnergy.forceFactor;
211 forceTmp[2] += dz * forceAndEnergy.forceFactor;
212
213 force(jdx, 0) -= dx * forceAndEnergy.forceFactor;
214 force(jdx, 1) -= dy * forceAndEnergy.forceFactor;
215 force(jdx, 2) -= dz * forceAndEnergy.forceFactor;
216 }
217
218 force(idx, 0) += forceTmp[0];
219 force(idx, 1) += forceTmp[1];
220 force(idx, 2) += forceTmp[2];
221 };
222
223 Kokkos::parallel_reduce("LennardJones::apply_if", policy, kernel, energyAndVirial_);
224 Kokkos::fence();
225}
226
227} // namespace mrmd::action
real_t getEnergy() const
Definition LennardJones.cpp:35
LennardJones(const real_t rc, const real_t &sigma, const real_t &epsilon, const real_t &cappingDistance=0_r)
Definition LennardJones.cpp:38
real_t getVirial() const
Definition LennardJones.cpp:36
void apply(data::Atoms &atoms, HalfVerletList &verletList)
Definition LennardJones.cpp:24
void apply_if(const data::Atoms &atoms, const HalfVerletList &verletList, const Pred &pred)
Definition LennardJones.hpp:155
idx_t numLocalAtoms
Definition Atoms.hpp:120
KOKKOS_FORCEINLINE_FUNCTION pos_t getPos() const
Definition Atoms.hpp:64
KOKKOS_FORCEINLINE_FUNCTION force_t getForce() const
Definition Atoms.hpp:66
typename AtomsT::template member_slice_type< TYPE > type_t
Definition Atoms.hpp:59
typename AtomsT::template member_slice_type< POS > pos_t
Definition Atoms.hpp:56
KOKKOS_FORCEINLINE_FUNCTION type_t getType() const
Definition Atoms.hpp:67
Everything to evolve the simulation physically.
DeviceAtoms Atoms
Definition Atoms.hpp:177
Cabana::VerletList< Kokkos::DefaultExecutionSpace::memory_space, Cabana::HalfNeighborTag, Cabana::VerletLayout2D, Cabana::TeamOpTag > HalfVerletList
Definition datatypes.hpp:184
KOKKOS_INLINE_FUNCTION constexpr idx_t idx_c(const T &val)
Definition datatypes.hpp:93
int64_t idx_t
Definition datatypes.hpp:91
double real_t
Definition datatypes.hpp:99
Definition EnergyAndVirialReducer.hpp:20
real_t virial
Definition EnergyAndVirialReducer.hpp:22
real_t energy
Definition EnergyAndVirialReducer.hpp:21