MRMD 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//
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{
30{
31private:
32 Kokkos::Random_XorShift1024_Pool<> randPool_ = Kokkos::Random_XorShift1024_Pool<>(1234);
33 real_t zeta_;
34 real_t temperature_;
35
36public:
37 void set(const real_t& zeta, const real_t& temperature)
38 {
39 zeta_ = zeta;
40 temperature_ = temperature;
41 }
42
43 VelocityVerletLangevinThermostat(const real_t& zeta, const real_t& temperature)
44 {
45 set(zeta, temperature);
46 }
47
49 {
51 atoms, dt, KOKKOS_LAMBDA(const real_t, const real_t, const real_t) { return true; });
52 }
53
58
59 template <OnePositionPredicate Pred>
60 real_t preForceIntegrate_apply_if(data::Atoms& atoms, const real_t dt, const Pred& pred);
61};
62
63template <OnePositionPredicate Pred>
65 const real_t dt,
66 const Pred& pred)
67{
68 auto RNG = randPool_;
69 auto dtHalf(0.5_r * dt);
70 auto dtFull(dt);
71 auto pos = atoms.getPos();
72 auto vel = atoms.getVel();
73 auto force = atoms.getForce();
74 auto mass = atoms.getMass();
75 auto zeta = zeta_;
76 auto temperature = temperature_;
77
78 auto policy = Kokkos::RangePolicy<>(0, atoms.numLocalAtoms);
79 auto kernel = KOKKOS_LAMBDA(const idx_t& idx, real_t& maxDistSqr)
80 {
81 real_t dx[3];
82 dx[0] = pos(idx, 0);
83 dx[1] = pos(idx, 1);
84 dx[2] = pos(idx, 2);
85
86 action::updateKick(vel(idx, 0),
87 vel(idx, 1),
88 vel(idx, 2),
89 force(idx, 0),
90 force(idx, 1),
91 force(idx, 2),
92 dtHalf,
93 mass(idx));
94
96 pos(idx, 0), pos(idx, 1), pos(idx, 2), vel(idx, 0), vel(idx, 1), vel(idx, 2), dtHalf);
97
98 if (pred(pos(idx, 0), pos(idx, 1), pos(idx, 2)))
99 {
100 // Get a random number state from the pool for the active thread
101 auto randGen = RNG.get_state();
102
103 // Apply the Ornstein-Uhlenbeck process to the velocity
105 vel(idx, 1),
106 vel(idx, 2),
107 dtFull,
108 mass(idx),
109 zeta,
110 temperature,
111 randGen.normal(),
112 randGen.normal(),
113 randGen.normal());
114
115 // Give the state back, which will allow another thread to acquire it
116 RNG.free_state(randGen);
117 }
118
120 pos(idx, 0), pos(idx, 1), pos(idx, 2), vel(idx, 0), vel(idx, 1), vel(idx, 2), dtHalf);
121
122 dx[0] -= pos(idx, 0);
123 dx[1] -= pos(idx, 1);
124 dx[2] -= pos(idx, 2);
125
126 auto distSqr = util::dot3(dx, dx);
127 maxDistSqr = Kokkos::max(distSqr, maxDistSqr);
128 };
129 real_t maxDistSqr = 0_r;
130 Kokkos::parallel_reduce("VelocityVerletLangevinThermostat::preForceIntegrate",
131 policy,
132 kernel,
133 Kokkos::Max<real_t>(maxDistSqr));
134 Kokkos::fence();
135 return std::sqrt(maxDistSqr);
136}
137
138} // namespace action
139} // namespace mrmd
Definition VelocityVerletLangevinThermostat.hpp:30
void set(const real_t &zeta, const real_t &temperature)
Definition VelocityVerletLangevinThermostat.hpp:37
real_t preForceIntegrate_apply_if(data::Atoms &atoms, const real_t dt, const Pred &pred)
Definition VelocityVerletLangevinThermostat.hpp:64
real_t preForceIntegrate(data::Atoms &atoms, const real_t dt)
Definition VelocityVerletLangevinThermostat.hpp:48
void postForceIntegrate(data::Atoms &atoms, const real_t dt)
Definition VelocityVerletLangevinThermostat.hpp:54
VelocityVerletLangevinThermostat(const real_t &zeta, const real_t &temperature)
Definition VelocityVerletLangevinThermostat.hpp:43
static void postForceIntegrate(data::Atoms &atoms, const real_t dt)
Definition VelocityVerlet.cpp:69
Data structure for storing information about atoms on the host and the device.
Definition Atoms.hpp:35
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
KOKKOS_INLINE_FUNCTION void updateDrift(real_t &pos_x, real_t &pos_y, real_t &pos_z, const real_t &vel_x, const real_t &vel_y, const real_t &vel_z, const real_t &dt)
Definition UpdateSteps.hpp:43
KOKKOS_INLINE_FUNCTION void updateKick(real_t &vel_x, real_t &vel_y, real_t &vel_z, const real_t &force_x, const real_t &force_y, const real_t &force_z, const real_t &dt, const real_t &mass)
Definition UpdateSteps.hpp:26
KOKKOS_INLINE_FUNCTION void updateOrnsteinUhlenbeck(real_t &vel_x, real_t &vel_y, real_t &vel_z, const real_t &dt, const real_t &mass, const real_t &zeta, const real_t &temperature, const real_t &rand_x, const real_t &rand_y, const real_t &rand_z)
Definition UpdateSteps.hpp:57
KOKKOS_INLINE_FUNCTION real_t dot3(real_t const *const a, real_t const *const b)
Definition math.hpp:24
Definition BerendsenBarostat.cpp:18
int64_t idx_t
Definition datatypes.hpp:91
double real_t
Definition datatypes.hpp:99