Multi Resolution Molecular Dynamics 0.4.0
Multi Resolution Molecular Dynamics
Loading...
Searching...
No Matches
VelocityVerletLangevinThermostat.hpp
Go to the documentation of this file.
1// Copyright 2024 Sebastian Eibl
2// Copyright 2026 Julian Friedrich Hille
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <Kokkos_Random.hpp>
18
19#include "VelocityVerlet.hpp"
21#include "data/Atoms.hpp"
22#include "datatypes.hpp"
23#include "util/math.hpp"
24
25namespace mrmd
26{
27namespace action
28{
36{
37private:
38 Kokkos::Random_XorShift1024_Pool<> randPool_ = Kokkos::Random_XorShift1024_Pool<>(1234);
39 real_t zeta_;
40 real_t temperature_;
41
42public:
43 void set(const real_t& zeta, const real_t& temperature)
44 {
45 zeta_ = zeta;
46 temperature_ = temperature;
47 }
48
49 VelocityVerletLangevinThermostat(const real_t& zeta, const real_t& temperature)
50 {
51 set(zeta, temperature);
52 }
53
55 {
57 atoms, dt, KOKKOS_LAMBDA(const real_t, const real_t, const real_t) { return true; });
58 }
59
64
65 template <OnePositionPredicate Pred>
66 real_t preForceIntegrate_apply_if(data::Atoms& atoms, const real_t dt, const Pred& pred);
67};
68
69template <OnePositionPredicate Pred>
71 const real_t dt,
72 const Pred& pred)
73{
74 auto RNG = randPool_;
75 auto dtHalf(0.5_r * dt);
76 auto dtFull(dt);
77 auto pos = atoms.getPos();
78 auto vel = atoms.getVel();
79 auto force = atoms.getForce();
80 auto mass = atoms.getMass();
81 auto zeta = zeta_;
82 auto temperature = temperature_;
83
84 auto policy = Kokkos::RangePolicy<>(0, atoms.numLocalAtoms);
85 auto kernel = KOKKOS_LAMBDA(const idx_t& idx, real_t& maxDistSqr)
86 {
87 real_t dx[3];
88 dx[0] = pos(idx, 0);
89 dx[1] = pos(idx, 1);
90 dx[2] = pos(idx, 2);
91
92 impl::updateKick(vel(idx, 0),
93 vel(idx, 1),
94 vel(idx, 2),
95 force(idx, 0),
96 force(idx, 1),
97 force(idx, 2),
98 dtHalf,
99 mass(idx));
100
101 impl::updateDrift(
102 pos(idx, 0), pos(idx, 1), pos(idx, 2), vel(idx, 0), vel(idx, 1), vel(idx, 2), dtHalf);
103
104 if (pred(pos(idx, 0), pos(idx, 1), pos(idx, 2)))
105 {
106 // Get a random number state from the pool for the active thread
107 auto randGen = RNG.get_state();
108
109 // Apply the Ornstein-Uhlenbeck process to the velocity
110 impl::updateOrnsteinUhlenbeck(vel(idx, 0),
111 vel(idx, 1),
112 vel(idx, 2),
113 dtFull,
114 mass(idx),
115 zeta,
116 temperature,
117 randGen.normal(),
118 randGen.normal(),
119 randGen.normal());
120
121 // Give the state back, which will allow another thread to acquire it
122 RNG.free_state(randGen);
123 }
124
125 impl::updateDrift(
126 pos(idx, 0), pos(idx, 1), pos(idx, 2), vel(idx, 0), vel(idx, 1), vel(idx, 2), dtHalf);
127
128 dx[0] -= pos(idx, 0);
129 dx[1] -= pos(idx, 1);
130 dx[2] -= pos(idx, 2);
131
132 auto distSqr = util::dot3(dx, dx);
133 maxDistSqr = Kokkos::max(distSqr, maxDistSqr);
134 };
135 real_t maxDistSqr = 0_r;
136 Kokkos::parallel_reduce("VelocityVerletLangevinThermostat::preForceIntegrate",
137 policy,
138 kernel,
139 Kokkos::Max<real_t>(maxDistSqr));
140 Kokkos::fence();
141 return std::sqrt(maxDistSqr);
142}
143
144} // namespace action
145} // namespace mrmd
void set(const real_t &zeta, const real_t &temperature)
Definition VelocityVerletLangevinThermostat.hpp:43
real_t preForceIntegrate_apply_if(data::Atoms &atoms, const real_t dt, const Pred &pred)
Definition VelocityVerletLangevinThermostat.hpp:70
real_t preForceIntegrate(data::Atoms &atoms, const real_t dt)
Definition VelocityVerletLangevinThermostat.hpp:54
void postForceIntegrate(data::Atoms &atoms, const real_t dt)
Definition VelocityVerletLangevinThermostat.hpp:60
VelocityVerletLangevinThermostat(const real_t &zeta, const real_t &temperature)
Definition VelocityVerletLangevinThermostat.hpp:49
static void postForceIntegrate(data::Atoms &atoms, const real_t dt)
Definition VelocityVerlet.cpp:69
KOKKOS_FORCEINLINE_FUNCTION vel_t getVel() const
Definition Atoms.hpp:65
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
KOKKOS_FORCEINLINE_FUNCTION charge_t getMass() const
Definition Atoms.hpp:68
Everything to evolve the simulation physically.
DeviceAtoms Atoms
Definition Atoms.hpp:177
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.
int64_t idx_t
Definition datatypes.hpp:91
double real_t
Definition datatypes.hpp:99