Multi Resolution Molecular Dynamics 0.4.0
Multi Resolution Molecular Dynamics
Loading...
Searching...
No Matches
ThermodynamicForce.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 <concepts>
19
20#include "assert/assert.hpp"
21#include "data/Atoms.hpp"
23#include "data/Subdomain.hpp"
24#include "datatypes.hpp"
27
28namespace mrmd
29{
30namespace action
31{
39{
40private:
42 data::MultiHistogram densityProfile_;
43 idx_t densityProfileSamples_ = 0;
44 real_t binVolume_;
45 const std::vector<real_t> targetDensity_;
46 const std::vector<real_t> thermodynamicForceModulation_;
47 idx_t numTypes_;
48
49 ScalarView forceFactor_;
50 bool enforceSymmetry_ = false;
51 bool usePeriodicity_ = false;
52
53public:
54 inline auto getForce() const { return force_; }
55 inline auto getForce(const idx_t& typeId) const
56 {
57 assert(typeId < numTypes_);
58 assert(typeId >= 0);
59 return Kokkos::subview(force_.data, Kokkos::ALL(), typeId);
60 }
61 inline void setForce(const MultiView& forces) const { Kokkos::deep_copy(force_.data, forces); }
62 inline auto getDensityProfile() const { return densityProfile_; }
63 inline auto getDensityProfile(const idx_t& typeId) const
64 {
65 assert(typeId < numTypes_);
66 assert(typeId >= 0);
67 return Kokkos::subview(densityProfile_.data, Kokkos::ALL(), typeId);
68 }
69 inline const auto& getNumberOfDensityProfileSamples() const { return densityProfileSamples_; }
70
71 void sample(data::Atoms& atoms);
72 void update(const real_t& smoothingSigma, const real_t& smoothingIntensity);
73 void apply(const data::Atoms& atoms) const;
74
75 template <OnePositionPredicate Pred>
76 void apply_if(const data::Atoms& atoms, const Pred& pred) const;
77
78 template <OneCoordinatePredicate Pred>
79 void update_if(const real_t& smoothingSigma,
80 const real_t& smoothingIntensity,
81 const Pred& pred);
82
83 template <OnePositionPredicate Pred>
84 void applyInterpolated_if(const data::Atoms& atoms, const Pred& pred) const;
85
86 std::vector<real_t> getMuLeft() const;
87 std::vector<real_t> getMuRight() const;
88
89 ThermodynamicForce(const std::vector<real_t>& targetDensity,
90 const data::Subdomain& subdomain,
91 const real_t& requestedDensityBinWidth,
92 const std::vector<real_t>& thermodynamicForceModulation,
93 const bool enforceSymmetry = false,
94 const bool usePeriodicity = false);
95
96 ThermodynamicForce(const real_t targetDensity,
97 const data::Subdomain& subdomain,
98 const real_t& requestedDensityBinWidth,
99 const real_t thermodynamicForceModulation,
100 const bool enforceSymmetry = false,
101 const bool usePeriodicity = false);
102};
103
104template <OnePositionPredicate Pred>
105void ThermodynamicForce::apply_if(const data::Atoms& atoms, const Pred& pred) const
106{
107 auto atomsPos = atoms.getPos();
108 auto atomsForce = atoms.getForce();
109 auto atomsType = atoms.getType();
110
111 auto forceHistogram = force_; // avoid capturing this pointer
112
113 auto policy = Kokkos::RangePolicy<>(0, atoms.numLocalAtoms);
114 auto kernel = KOKKOS_LAMBDA(const idx_t idx)
115 {
116 auto xPos = atomsPos(idx, 0);
117 if (!pred(atomsPos(idx, 0), atomsPos(idx, 1), atomsPos(idx, 2))) return;
118 auto bin = forceHistogram.getBin(xPos);
119 if (bin != -1)
120 {
121 MRMD_DEVICE_ASSERT_LESS(atomsType(idx), forceHistogram.numHistograms);
122 MRMD_DEVICE_ASSERT(!std::isnan(forceHistogram.data(bin, atomsType(idx))));
123 atomsForce(idx, 0) += forceHistogram.data(bin, atomsType(idx));
124 }
125 };
126 Kokkos::parallel_for("ThermodynamicForce::apply_if", policy, kernel);
127 Kokkos::fence();
128}
129
130template <OneCoordinatePredicate Pred>
131void ThermodynamicForce::update_if(const real_t& smoothingSigma,
132 const real_t& smoothingIntensity,
133 const Pred& pred)
134{
135 MRMD_HOST_CHECK_GREATER(densityProfileSamples_, 0);
136
137 if (enforceSymmetry_)
138 {
139 densityProfile_.makeSymmetric();
140 }
141
142 auto normalizationFactor = 1_r / (binVolume_ * real_c(densityProfileSamples_));
143 densityProfile_.scale(normalizationFactor);
144
145 auto smoothedDensityProfile =
146 data::smoothen(densityProfile_, smoothingSigma, smoothingIntensity, usePeriodicity_);
147 auto smoothedDensityGradient = data::gradient(smoothedDensityProfile, usePeriodicity_);
148 smoothedDensityGradient.scale(forceFactor_);
149
151 smoothedDensityGradient, KOKKOS_LAMBDA(const real_t x) { return !pred(x); }, 0_r);
152
153 force_ -= smoothedDensityGradient;
154
155 // reset sampling data
156 Kokkos::deep_copy(densityProfile_.data, 0_r);
157 densityProfileSamples_ = 0;
158}
159
160template <OnePositionPredicate Pred>
161void ThermodynamicForce::applyInterpolated_if(const data::Atoms& atoms, const Pred& pred) const
162{
163 auto atomsPos = atoms.getPos();
164 auto atomsForce = atoms.getForce();
165 auto atomsType = atoms.getType();
166
167 auto forceHistogram = force_; // avoid capturing this pointer
168
169 auto policy = Kokkos::RangePolicy<>(0, atoms.numLocalAtoms);
170 auto kernel = KOKKOS_LAMBDA(const idx_t idx)
171 {
172 auto xPos = atomsPos(idx, 0);
173 if (!pred(atomsPos(idx, 0), atomsPos(idx, 1), atomsPos(idx, 2))) return;
174 auto bin = forceHistogram.getBin(xPos);
175
176 if (bin != -1)
177 {
178 auto atomType = atomsType(idx);
179 MRMD_DEVICE_ASSERT_LESS(atomType, forceHistogram.numHistograms);
180
181 // centered interpolation: switch neighboring bins at the bin center
182 auto binStart = forceHistogram.min + real_c(bin) * forceHistogram.binSize;
183 auto fracInBin = (xPos - binStart) * forceHistogram.inverseBinSize;
184
185 idx_t leftBinIdx;
186 idx_t rightBinIdx;
187 real_t factor;
188
189 if (fracInBin < 0.5_r)
190 {
191 leftBinIdx = bin - 1;
192 rightBinIdx = bin;
193 factor = fracInBin + 0.5_r;
194 }
195 else
196 {
197 leftBinIdx = bin;
198 rightBinIdx = bin + 1;
199 factor = fracInBin - 0.5_r;
200 }
201
202 // interpolate if both neighbors are valid, otherwise clamp to the current bin
203 if (leftBinIdx >= 0 && rightBinIdx < forceHistogram.numBins)
204 {
205 auto inputLeft = forceHistogram.data(leftBinIdx, atomType);
206 auto inputRight = forceHistogram.data(rightBinIdx, atomType);
207 MRMD_DEVICE_ASSERT(!std::isnan(inputLeft));
208 MRMD_DEVICE_ASSERT(!std::isnan(inputRight));
209
210 atomsForce(idx, 0) += util::lerp(inputLeft, inputRight, factor);
211 }
212 else
213 {
214 auto input = forceHistogram.data(bin, atomType);
215 MRMD_DEVICE_ASSERT(!std::isnan(input));
216 atomsForce(idx, 0) += input;
217 }
218 }
219 };
220 Kokkos::parallel_for("ThermodynamicForce::applyInterpolated_if", policy, kernel);
221 Kokkos::fence();
222}
223
224} // namespace action
225} // namespace mrmd
auto getDensityProfile(const idx_t &typeId) const
Definition ThermodynamicForce.hpp:63
void apply(const data::Atoms &atoms) const
Definition ThermodynamicForce.cpp:93
void update_if(const real_t &smoothingSigma, const real_t &smoothingIntensity, const Pred &pred)
Definition ThermodynamicForce.hpp:131
std::vector< real_t > getMuLeft() const
Definition ThermodynamicForce.cpp:98
void applyInterpolated_if(const data::Atoms &atoms, const Pred &pred) const
Definition ThermodynamicForce.hpp:161
auto getForce(const idx_t &typeId) const
Definition ThermodynamicForce.hpp:55
void sample(data::Atoms &atoms)
Definition ThermodynamicForce.cpp:74
auto getForce() const
Definition ThermodynamicForce.hpp:54
void setForce(const MultiView &forces) const
Definition ThermodynamicForce.hpp:61
std::vector< real_t > getMuRight() const
Definition ThermodynamicForce.cpp:115
const auto & getNumberOfDensityProfileSamples() const
Definition ThermodynamicForce.hpp:69
void update(const real_t &smoothingSigma, const real_t &smoothingIntensity)
Definition ThermodynamicForce.cpp:88
ThermodynamicForce(const std::vector< real_t > &targetDensity, const data::Subdomain &subdomain, const real_t &requestedDensityBinWidth, const std::vector< real_t > &thermodynamicForceModulation, const bool enforceSymmetry=false, const bool usePeriodicity=false)
Definition ThermodynamicForce.cpp:25
auto getDensityProfile() const
Definition ThermodynamicForce.hpp:62
void apply_if(const data::Atoms &atoms, const Pred &pred) const
Definition ThermodynamicForce.hpp:105
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 type_t getType() const
Definition Atoms.hpp:67
#define MRMD_DEVICE_ASSERT_LESS(...)
Definition device.hpp:472
#define MRMD_DEVICE_ASSERT(...)
Definition device.hpp:465
#define MRMD_HOST_CHECK_GREATER(...)
Definition host.hpp:116
Everything to evolve the simulation physically.
data::MultiHistogram smoothen(data::MultiHistogram &input, const real_t &sigma, const real_t &range, const bool periodic)
Smoothen a histogram using gaussian convolution.
Definition MultiHistogram.cpp:163
data::Histogram gradient(const data::Histogram &input)
Definition Histogram.cpp:37
DeviceAtoms Atoms
Definition Atoms.hpp:177
void replace_if_bin_position(MultiHistogram &hist, const Pred &pred, real_t newValue)
Replaces histogram values with a new value if the bin position satisfies a predicate.
Definition MultiHistogram.hpp:181
KOKKOS_INLINE_FUNCTION real_t lerp(const real_t &left, const real_t &right, const real_t &factor)
Linear interpolation between two values.
Definition interpolation.hpp:35
Top-level namespace of MRMD.
int64_t idx_t
Definition datatypes.hpp:91
Kokkos::View< real_t ** > MultiView
Definition datatypes.hpp:161
double real_t
Definition datatypes.hpp:99
KOKKOS_INLINE_FUNCTION constexpr real_t real_c(T t)
Definition datatypes.hpp:114
Kokkos::View< real_t * > ScalarView
Definition datatypes.hpp:159
Definition MultiHistogram.hpp:30
Axis-aligned simulation box with a surrounding ghost layer.
Definition Subdomain.hpp:46