Multi Resolution Molecular Dynamics 0.4.0
Multi Resolution Molecular Dynamics
Loading...
Searching...
No Matches
SPC.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 "Coulomb.hpp"
19#include "LennardJones.hpp"
20#include "Shake.hpp"
21#include "data/Atoms.hpp"
22#include "data/Histogram.hpp"
23#include "datatypes.hpp"
24#include "util/angle.hpp"
25
26namespace mrmd::action::impl
27{
28struct Energy
29{
30 real_t LJ = 0_r;
31 real_t coulomb = 0_r;
32
33 Energy() = default;
34 Energy(const Energy& rhs) = default;
35
36 KOKKOS_INLINE_FUNCTION
37 Energy& operator+=(const Energy& src)
38 {
39 LJ += src.LJ;
40 coulomb += src.coulomb;
41 return *this;
42 }
43};
44} // namespace mrmd::action::impl
45
46namespace Kokkos
47{ // reduction identity must be defined in Kokkos namespace
48template <>
49struct reduction_identity<mrmd::action::impl::Energy>
50{
51 KOKKOS_FORCEINLINE_FUNCTION static mrmd::action::impl::Energy sum()
52 {
53 return mrmd::action::impl::Energy();
54 }
55};
56} // namespace Kokkos
57
58namespace mrmd
59{
60namespace action
61{
69class SPC
70{
71private:
72 impl::CappedLennardJonesPotential LJ_;
73 impl::Coulomb coulomb_;
74 real_t rcSqr_ = 0_r;
75
76 data::Molecules::pos_t moleculesPos_;
77 data::Molecules::force_t::atomic_access_slice moleculesForce_;
78 data::Molecules::lambda_t moleculesLambda_;
79 data::Molecules::modulated_lambda_t moleculesModulatedLambda_;
80 data::Molecules::grad_lambda_t moleculesGradLambda_;
81 data::Molecules::atoms_offset_t moleculesAtomsOffset_;
82 data::Molecules::num_atoms_t moleculesNumAtoms_;
83
84 data::Atoms::pos_t atomsPos_;
85 data::Atoms::force_t::atomic_access_slice atomsForce_;
86 data::Atoms::charge_t atomsCharge_;
87
88 data::Histogram compensationEnergy_ = data::Histogram("compensationEnergy", 0_r, 1_r, 200);
89 ScalarScatterView compensationEnergyScatter_;
90
91 data::Histogram compensationEnergyCounter_ =
92 data::Histogram("compensationEnergyCounter", 0_r, 1_r, 200);
93
94 data::Histogram meanCompensationEnergy_ =
95 data::Histogram("meanCompensationEnergy", 0_r, 1_r, 200);
96
97 bool isDriftCompensationSamplingRun_ = false;
98
99 HalfVerletList verletList_;
100
101 idx_t runCounter_ = 0;
102
103 data::BondView::host_mirror_type bonds_;
104
105public:
108
109 auto getEnergyLJ() const { return sumEnergyLJ_; }
110 auto getEnergyCoulomb() const { return sumEnergyCoulomb_; }
111
112 static constexpr real_t massO = 15.999_r;
113 static constexpr real_t chargeO = -0.82_r;
114
115 static constexpr real_t massH = 1.008_r;
116 static constexpr real_t chargeH = +0.41_r;
117
118 // LJ parameters for O-O interaction
119 // @reference{SPC water model,10.1021/j100308a038}
120 static constexpr real_t sigma = 0.31655578901998815_r;
121 static constexpr real_t epsilon = 0.6501695808187486_r;
122 static constexpr real_t rc = 1.2_r;
123
124 // Coulomb DSF parameters
125 static constexpr real_t alpha = 2.0_r;
126
128 static constexpr real_t eqDistanceHO = 0.1_r;
129 static constexpr real_t angleHOH = util::degToRad(109.47_r);
130 const real_t eqDistanceHH = eqDistanceHO * std::sqrt(2_r - 2_r * std::cos(angleHOH));
131
134
135 const auto& getMeanCompensationEnergy() const { return meanCompensationEnergy_; }
136
138 {
139 };
141 {
142 };
143
150 KOKKOS_INLINE_FUNCTION void operator()(CalcInteractions,
151 const idx_t& alpha,
152 impl::Energy& sumEnergy) const
153 {
154 const auto numNeighbors = idx_c(HalfNeighborList::numNeighbor(verletList_, alpha));
155 for (idx_t n = 0; n < numNeighbors; ++n)
156 {
158 const idx_t beta = idx_c(HalfNeighborList::getNeighbor(verletList_, alpha, n));
159 assert(0 <= beta);
160
162 const auto startAtomsAlpha = moleculesAtomsOffset_(alpha);
164 const auto endAtomsAlpha = startAtomsAlpha + moleculesNumAtoms_(alpha);
165 assert(0 <= startAtomsAlpha);
166 assert(startAtomsAlpha < endAtomsAlpha);
167
169 const auto startAtomsBeta = moleculesAtomsOffset_(beta);
171 const auto endAtomsBeta = startAtomsBeta + moleculesNumAtoms_(beta);
172 assert(0 <= startAtomsBeta);
173 assert(startAtomsBeta < endAtomsBeta);
174
175 // LJ interaction between oxygen atoms
176 real_t posTmp[3];
177 posTmp[0] = atomsPos_(startAtomsAlpha, 0);
178 posTmp[1] = atomsPos_(startAtomsAlpha, 1);
179 posTmp[2] = atomsPos_(startAtomsAlpha, 2);
180
181 real_t dx[3];
182 dx[0] = posTmp[0] - atomsPos_(startAtomsBeta, 0);
183 dx[1] = posTmp[1] - atomsPos_(startAtomsBeta, 1);
184 dx[2] = posTmp[2] - atomsPos_(startAtomsBeta, 2);
185
186 const auto distSqr = util::dot3(dx, dx);
187
188 if (distSqr < rcSqr_)
189 {
190 auto forceAndVirial = LJ_.computeForceAndEnergy(distSqr, 0);
191 sumEnergy.LJ += forceAndVirial.energy;
192
193 atomsForce_(startAtomsBeta, 0) -= dx[0] * forceAndVirial.forceFactor;
194 atomsForce_(startAtomsBeta, 1) -= dx[1] * forceAndVirial.forceFactor;
195 atomsForce_(startAtomsBeta, 2) -= dx[2] * forceAndVirial.forceFactor;
196
197 atomsForce_(startAtomsAlpha, 0) += dx[0] * forceAndVirial.forceFactor;
198 atomsForce_(startAtomsAlpha, 1) += dx[1] * forceAndVirial.forceFactor;
199 atomsForce_(startAtomsAlpha, 2) += dx[2] * forceAndVirial.forceFactor;
200 }
201
203 for (idx_t idx = startAtomsAlpha; idx < endAtomsAlpha; ++idx)
204 {
205 real_t posTmp[3];
206 posTmp[0] = atomsPos_(idx, 0);
207 posTmp[1] = atomsPos_(idx, 1);
208 posTmp[2] = atomsPos_(idx, 2);
209
210 auto q1 = atomsCharge_(idx);
211
212 // avoid atomic force contributions to idx in innermost loop
213 real_t forceTmpIdx[3] = {0_r, 0_r, 0_r};
214
215 for (idx_t jdx = startAtomsBeta; jdx < endAtomsBeta; ++jdx)
216 {
217 auto q2 = atomsCharge_(jdx);
218
219 real_t dx[3];
220 dx[0] = posTmp[0] - atomsPos_(jdx, 0);
221 dx[1] = posTmp[1] - atomsPos_(jdx, 1);
222 dx[2] = posTmp[2] - atomsPos_(jdx, 2);
223
224 const auto distSqr = util::dot3(dx, dx);
225
226 if (distSqr > rcSqr_) continue;
227
228 auto ffactor = coulomb_.computeForce(distSqr, q1, q2);
229 sumEnergy.coulomb += coulomb_.computeEnergy(distSqr, q1, q2);
230
231 forceTmpIdx[0] += dx[0] * ffactor;
232 forceTmpIdx[1] += dx[1] * ffactor;
233 forceTmpIdx[2] += dx[2] * ffactor;
234
235 atomsForce_(jdx, 0) -= dx[0] * ffactor;
236 atomsForce_(jdx, 1) -= dx[1] * ffactor;
237 atomsForce_(jdx, 2) -= dx[2] * ffactor;
238 }
239
240 atomsForce_(idx, 0) += forceTmpIdx[0];
241 atomsForce_(idx, 1) += forceTmpIdx[1];
242 atomsForce_(idx, 2) += forceTmpIdx[2];
243 }
244 }
245 }
246
248 {
249 MoleculeConstraints moleculeConstraints(3, 20);
250 moleculeConstraints.setConstraints(bonds_);
251 moleculeConstraints.enforcePositionalConstraints(molecules, atoms, dt);
252 }
253
255 {
256 MoleculeConstraints moleculeConstraints(3, 20);
257 moleculeConstraints.setConstraints(bonds_);
258 moleculeConstraints.enforceVelocityConstraints(molecules, atoms, dt);
259 }
260
261 void applyForces(data::Molecules& molecules, HalfVerletList& verletList, data::Atoms& atoms)
262 {
263 moleculesPos_ = molecules.getPos();
264 moleculesForce_ = molecules.getForce();
265 moleculesLambda_ = molecules.getLambda();
266 moleculesModulatedLambda_ = molecules.getModulatedLambda();
267 moleculesGradLambda_ = molecules.getGradLambda();
268 moleculesAtomsOffset_ = molecules.getAtomsOffset();
269 moleculesNumAtoms_ = molecules.getNumAtoms();
270 atomsPos_ = atoms.getPos();
271 atomsForce_ = atoms.getForce();
272 atomsCharge_ = atoms.getCharge();
273 verletList_ = verletList;
274
275 isDriftCompensationSamplingRun_ = runCounter_ % COMPENSATION_ENERGY_SAMPLING_INTERVAL == 0;
276
277 // compensationEnergyScatter_ = ScalarScatterView(compensationEnergy_.data);
278
279 auto energy = impl::Energy();
280 auto policy = Kokkos::RangePolicy<CalcInteractions>(0, molecules.numLocalMolecules);
281 Kokkos::parallel_reduce(
282 "SPC::applyForces", policy, *this, Kokkos::Sum<impl::Energy>(energy));
283
284 // Kokkos::Experimental::contribute(compensationEnergy_.data,
285 // compensationEnergyScatter_);
286
287 Kokkos::fence();
288
289 // if (runCounter_ % compensationEnergyUpdateInveral == 0)
290 // updateMeanCompensationEnergy(
291 // compensationEnergy_, compensationEnergyCounter_, meanCompensationEnergy_,
292 // 10_r);
293 //
294 // ++runCounter_;
295
296 sumEnergyLJ_ = energy.LJ;
297 sumEnergyCoulomb_ = energy.coulomb;
298 }
299
300 KOKKOS_INLINE_FUNCTION void operator()(BondEnergy, const idx_t& alpha, real_t& sumEnergy) const
301 {
303 const auto startAtomsAlpha = moleculesAtomsOffset_(alpha);
304 assert(0 <= startAtomsAlpha);
305
306 auto idxO = startAtomsAlpha;
307 auto idxH0 = startAtomsAlpha + 1;
308 auto idxH1 = startAtomsAlpha + 2;
309
310 real_t dx[3];
311 real_t dist = 0_r;
312
313 dx[0] = atomsPos_(idxO, 0) - atomsPos_(idxH0, 0);
314 dx[1] = atomsPos_(idxO, 1) - atomsPos_(idxH0, 1);
315 dx[2] = atomsPos_(idxO, 2) - atomsPos_(idxH0, 2);
316 dist = std::sqrt(util::dot3(dx, dx));
317 sumEnergy += util::sqr(dist - eqDistanceHO);
318
319 dx[0] = atomsPos_(idxO, 0) - atomsPos_(idxH1, 0);
320 dx[1] = atomsPos_(idxO, 1) - atomsPos_(idxH1, 1);
321 dx[2] = atomsPos_(idxO, 2) - atomsPos_(idxH1, 2);
322 dist = std::sqrt(util::dot3(dx, dx));
323 sumEnergy += util::sqr(dist - eqDistanceHO);
324
325 dx[0] = atomsPos_(idxH0, 0) - atomsPos_(idxH1, 0);
326 dx[1] = atomsPos_(idxH0, 1) - atomsPos_(idxH1, 1);
327 dx[2] = atomsPos_(idxH0, 2) - atomsPos_(idxH1, 2);
328 dist = std::sqrt(util::dot3(dx, dx));
329 sumEnergy += util::sqr(dist - eqDistanceHH);
330 }
331
333 data::Atoms& atoms,
334 const real_t& harmonicPreFactor)
335 {
336 moleculesPos_ = molecules.getPos();
337 moleculesForce_ = molecules.getForce();
338 moleculesLambda_ = molecules.getLambda();
339 moleculesModulatedLambda_ = molecules.getModulatedLambda();
340 moleculesGradLambda_ = molecules.getGradLambda();
341 moleculesAtomsOffset_ = molecules.getAtomsOffset();
342 moleculesNumAtoms_ = molecules.getNumAtoms();
343 atomsPos_ = atoms.getPos();
344 atomsForce_ = atoms.getForce();
345 atomsCharge_ = atoms.getCharge();
346
347 real_t energy = 0_r;
348 auto policy = Kokkos::RangePolicy<BondEnergy>(
349 0, molecules.numLocalMolecules + molecules.numGhostMolecules);
350 Kokkos::parallel_reduce("SPC::calcHarmonicPotential", policy, *this, energy);
351
352 Kokkos::fence();
353
354 return harmonicPreFactor * energy / real_c(atoms.numLocalAtoms + atoms.numGhostAtoms);
355 }
356
358 : LJ_({0.7_r * sigma}, {rc}, {sigma}, {epsilon}, 1, true),
359 coulomb_(),
360 rcSqr_(rc * rc),
361 bonds_("bonds", 3)
362 {
363 bonds_(0).idx = 0;
364 bonds_(0).jdx = 1;
365 bonds_(0).eqDistance = eqDistanceHO;
366 bonds_(1).idx = 0;
367 bonds_(1).jdx = 2;
368 bonds_(1).eqDistance = eqDistanceHO;
369 bonds_(2).idx = 1;
370 bonds_(2).jdx = 2;
371 bonds_(2).eqDistance = eqDistanceHH;
372 }
373};
374} // namespace action
375} // namespace mrmd
High-level interface for the application of SHAKE/RATTLE position and velocity constraints to given m...
Definition Shake.hpp:170
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
void setConstraints(const data::BondView &bonds)
Definition Shake.hpp:247
static constexpr idx_t COMPENSATION_ENERGY_SAMPLING_INTERVAL
Definition SPC.hpp:132
SPC()
Definition SPC.hpp:357
static constexpr real_t massH
unit: g/mol
Definition SPC.hpp:115
static constexpr real_t rc
unit: nm
Definition SPC.hpp:122
void applyForces(data::Molecules &molecules, HalfVerletList &verletList, data::Atoms &atoms)
Definition SPC.hpp:261
void enforceVelocityConstraints(data::Molecules &molecules, data::Atoms &atoms, real_t dt)
Definition SPC.hpp:254
auto getEnergyLJ() const
Definition SPC.hpp:109
auto getEnergyCoulomb() const
Definition SPC.hpp:110
static constexpr real_t chargeH
Definition SPC.hpp:116
const real_t eqDistanceHH
Definition SPC.hpp:130
real_t sumEnergyLJ_
Definition SPC.hpp:106
static constexpr idx_t COMPENSATION_ENERGY_UPDATE_INTERVAL
Definition SPC.hpp:133
KOKKOS_INLINE_FUNCTION void operator()(CalcInteractions, const idx_t &alpha, impl::Energy &sumEnergy) const
Loop over molecules.
Definition SPC.hpp:150
void enforcePositionalConstraints(data::Molecules &molecules, data::Atoms &atoms, real_t dt)
Definition SPC.hpp:247
real_t calcBondEnergy(data::Molecules &molecules, data::Atoms &atoms, const real_t &harmonicPreFactor)
Definition SPC.hpp:332
static constexpr real_t sigma
unit: nm
Definition SPC.hpp:120
static constexpr real_t angleHOH
unit: radians
Definition SPC.hpp:129
static constexpr real_t chargeO
Definition SPC.hpp:113
static constexpr real_t epsilon
unit: kJ / mol
Definition SPC.hpp:121
static constexpr real_t massO
unit: g/mol
Definition SPC.hpp:112
const auto & getMeanCompensationEnergy() const
Definition SPC.hpp:135
static constexpr real_t alpha
unit: 1/nm
Definition SPC.hpp:125
static constexpr real_t eqDistanceHO
unit: nm, equilibirum distance between hydrogen and oxygen
Definition SPC.hpp:128
real_t sumEnergyCoulomb_
Definition SPC.hpp:107
KOKKOS_INLINE_FUNCTION void operator()(BondEnergy, const idx_t &alpha, real_t &sumEnergy) const
Definition SPC.hpp:300
idx_t numLocalAtoms
Definition Atoms.hpp:120
idx_t numGhostAtoms
Definition Atoms.hpp:121
KOKKOS_FORCEINLINE_FUNCTION charge_t getCharge() const
Definition Atoms.hpp:69
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< CHARGE > charge_t
Definition Atoms.hpp:61
typename AtomsT::template member_slice_type< POS > pos_t
Definition Atoms.hpp:56
typename MoleculesT::template member_slice_type< GRAD_LAMBDA > grad_lambda_t
Definition Molecules.hpp:54
KOKKOS_FORCEINLINE_FUNCTION atoms_offset_t getAtomsOffset() const
Definition Molecules.hpp:67
KOKKOS_FORCEINLINE_FUNCTION num_atoms_t getNumAtoms() const
Definition Molecules.hpp:68
typename MoleculesT::template member_slice_type< MODULATED_LAMBDA > modulated_lambda_t
Definition Molecules.hpp:53
KOKKOS_FORCEINLINE_FUNCTION force_t getForce() const
Definition Molecules.hpp:59
KOKKOS_FORCEINLINE_FUNCTION pos_t getPos() const
Definition Molecules.hpp:58
typename MoleculesT::template member_slice_type< LAMBDA > lambda_t
Definition Molecules.hpp:52
KOKKOS_FORCEINLINE_FUNCTION modulated_lambda_t getModulatedLambda() const
Definition Molecules.hpp:62
typename MoleculesT::template member_slice_type< NUM_ATOMS > num_atoms_t
Definition Molecules.hpp:56
idx_t numGhostMolecules
Definition Molecules.hpp:121
KOKKOS_FORCEINLINE_FUNCTION lambda_t getLambda() const
Definition Molecules.hpp:61
typename MoleculesT::template member_slice_type< ATOMS_OFFSET > atoms_offset_t
Definition Molecules.hpp:55
idx_t numLocalMolecules
Definition Molecules.hpp:120
KOKKOS_FORCEINLINE_FUNCTION grad_lambda_t getGradLambda() const
Definition Molecules.hpp:66
typename MoleculesT::template member_slice_type< POS > pos_t
Definition Molecules.hpp:50
Definition SPC.hpp:47
DeviceMolecules Molecules
Definition Molecules.hpp:176
DeviceAtoms Atoms
Definition Atoms.hpp:177
KOKKOS_INLINE_FUNCTION constexpr real_t degToRad(const real_t &grd)
converts degree to radians
Definition angle.hpp:28
KOKKOS_INLINE_FUNCTION real_t sqr(const real_t &sqr)
Definition math.hpp:29
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.
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
Kokkos::Experimental::ScatterView< real_t * > ScalarScatterView
Definition datatypes.hpp:160
double real_t
Definition datatypes.hpp:99
KOKKOS_INLINE_FUNCTION constexpr real_t real_c(T t)
Definition datatypes.hpp:114
Definition SPC.hpp:138
Definition Histogram.hpp:26