MRMD 0.4.0
Multi Resolution Molecular Dynamics
Loading...
Searching...
No Matches
Molecules.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 <Cabana_Core.hpp>
18
19#include "cmake.hpp"
20#include "constants.hpp"
21#include "datatypes.hpp"
22
23namespace mrmd::data
24{
25namespace impl
26{
27template <class DEVICE_TYPE = DeviceType, bool DEVICE = true>
29{
30public:
41 using DataTypes = Cabana::MemberTypes<real_t[DIMENSIONS],
43 real_t,
44 real_t,
46 idx_t,
47 idx_t>;
48 using MoleculesT = Cabana::AoSoA<DataTypes, typename DEVICE_TYPE::memory_space, VECTOR_LENGTH>;
49
50 using pos_t = typename MoleculesT::template member_slice_type<POS>;
51 using force_t = typename MoleculesT::template member_slice_type<FORCE>;
52 using lambda_t = typename MoleculesT::template member_slice_type<LAMBDA>;
53 using modulated_lambda_t = typename MoleculesT::template member_slice_type<MODULATED_LAMBDA>;
54 using grad_lambda_t = typename MoleculesT::template member_slice_type<GRAD_LAMBDA>;
55 using atoms_offset_t = typename MoleculesT::template member_slice_type<ATOMS_OFFSET>;
56 using num_atoms_t = typename MoleculesT::template member_slice_type<NUM_ATOMS>;
57
60 void setForce(const real_t& val) const { Cabana::deep_copy(force, val); }
63 {
64 return modulatedLambda;
65 }
69
70 void sliceAll()
71 {
72 pos = Cabana::slice<POS>(molecules_);
73 force = Cabana::slice<FORCE>(molecules_);
74 lambda = Cabana::slice<LAMBDA>(molecules_);
75 modulatedLambda = Cabana::slice<MODULATED_LAMBDA>(molecules_);
76 gradLambda = Cabana::slice<GRAD_LAMBDA>(molecules_);
77 atomsOffset = Cabana::slice<ATOMS_OFFSET>(molecules_);
78 numAtoms = Cabana::slice<NUM_ATOMS>(molecules_);
79 }
80
81 KOKKOS_INLINE_FUNCTION idx_t size() const { return idx_c(molecules_.size()); }
82 KOKKOS_INLINE_FUNCTION auto numSoA() const { return molecules_.numSoA(); }
83 KOKKOS_INLINE_FUNCTION auto arraySize(size_t s) const { return molecules_.arraySize(s); }
84
85 void resize(size_t size)
86 {
87 molecules_.resize(size);
88 sliceAll();
89 }
90
93 {
94 Cabana::permute(linkedCellList, molecules_);
95 }
96
98 void copy(const idx_t dst, const idx_t src) const
99 {
100 for (auto dim = 0; dim < DIMENSIONS; ++dim)
101 {
102 pos(dst, dim) = pos(src, dim);
103 force(dst, dim) = force(src, dim);
104 gradLambda(dst, dim) = gradLambda(src, dim);
105 }
106 lambda(dst) = lambda(src);
107 modulatedLambda(dst) = modulatedLambda(src);
108 atomsOffset(dst) = atomsOffset(src);
109 numAtoms(dst) = numAtoms(src);
110 }
111
117
118 auto& getAoSoA() const { return molecules_; }
119
122
123 explicit GeneralMolecules(const idx_t numMolecules) : molecules_("molecules", numMolecules)
124 {
125 sliceAll();
126 Cabana::deep_copy(pos, 0_r);
127 Cabana::deep_copy(force, 0_r);
128 Cabana::deep_copy(lambda, 0_r);
129 Cabana::deep_copy(modulatedLambda, 0_r);
130 Cabana::deep_copy(gradLambda, 0_r);
131 Cabana::deep_copy(atomsOffset, 0);
132 Cabana::deep_copy(numAtoms, 0);
133 }
134
135 template <class DEVICE_TYPE_SRC, bool DEVICE_SRC>
137
138private:
139 MoleculesT molecules_;
140
141 pos_t pos;
142 force_t force;
143 lambda_t lambda;
144 modulated_lambda_t modulatedLambda;
145 grad_lambda_t gradLambda;
146 atoms_offset_t atomsOffset;
147 num_atoms_t numAtoms;
148};
149} // namespace impl
150
151template <class DEVICE_TYPE_A, bool DEVICE_A, class DEVICE_TYPE_B, bool DEVICE_B>
154{
155 dst.numLocalMolecules = src.numLocalMolecules;
156 dst.numGhostMolecules = src.numGhostMolecules;
157 dst.resize(src.size());
158 Cabana::deep_copy(dst.getAoSoA(), src.getAoSoA());
159 dst.sliceAll();
160}
161
162namespace impl
163{
164template <class DEVICE_TYPE, bool DEVICE>
165template <class DEVICE_TYPE_SRC, bool DEVICE_SRC>
168 : molecules_("molecules", molecules.size())
169{
170 deep_copy(*this, molecules);
171}
172} // namespace impl
173
177
178} // namespace mrmd::data
Definition Molecules.hpp:29
typename MoleculesT::template member_slice_type< GRAD_LAMBDA > grad_lambda_t
Definition Molecules.hpp:54
KOKKOS_INLINE_FUNCTION auto numSoA() const
Definition Molecules.hpp:82
void setForce(const real_t &val) const
Definition Molecules.hpp:60
KOKKOS_FORCEINLINE_FUNCTION atoms_offset_t getAtomsOffset() const
Definition Molecules.hpp:67
KOKKOS_FORCEINLINE_FUNCTION num_atoms_t getNumAtoms() const
Definition Molecules.hpp:68
Cabana::AoSoA< DataTypes, typename DEVICE_TYPE::memory_space, VECTOR_LENGTH > MoleculesT
Definition Molecules.hpp:48
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
void sliceAll()
Definition Molecules.hpp:70
KOKKOS_INLINE_FUNCTION void copy(const idx_t dst, const idx_t src) const
Definition Molecules.hpp:98
void resize(size_t size)
Definition Molecules.hpp:85
KOKKOS_INLINE_FUNCTION auto arraySize(size_t s) const
Definition Molecules.hpp:83
KOKKOS_FORCEINLINE_FUNCTION pos_t getPos() const
Definition Molecules.hpp:58
typename MoleculesT::template member_slice_type< FORCE > force_t
Definition Molecules.hpp:51
typename MoleculesT::template member_slice_type< LAMBDA > lambda_t
Definition Molecules.hpp:52
KOKKOS_INLINE_FUNCTION idx_t size() const
Definition Molecules.hpp:81
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
Cabana::MemberTypes< real_t[DIMENSIONS], real_t[DIMENSIONS], real_t, real_t, real_t[DIMENSIONS], idx_t, idx_t > DataTypes
Definition Molecules.hpp:47
GeneralMolecules(const idx_t numMolecules)
Definition Molecules.hpp:123
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_INLINE_FUNCTION void permute(LinkedCellList &linkedCellList) const
Definition Molecules.hpp:92
auto & getAoSoA() const
Definition Molecules.hpp:118
KOKKOS_FORCEINLINE_FUNCTION grad_lambda_t getGradLambda() const
Definition Molecules.hpp:66
Props
Definition Molecules.hpp:32
@ FORCE
Definition Molecules.hpp:34
@ NUM_ATOMS
number of atoms for this molecule
Definition Molecules.hpp:39
@ GRAD_LAMBDA
Definition Molecules.hpp:37
@ ATOMS_OFFSET
offset into atoms array
Definition Molecules.hpp:38
@ MODULATED_LAMBDA
Definition Molecules.hpp:36
@ LAMBDA
Definition Molecules.hpp:35
@ POS
Definition Molecules.hpp:33
void removeGhostMolecules()
Definition Molecules.hpp:112
typename MoleculesT::template member_slice_type< POS > pos_t
Definition Molecules.hpp:50
Definition Array.hpp:18
impl::GeneralMolecules< DeviceType, true > DeviceMolecules
Definition Molecules.hpp:175
void deep_copy(impl::GeneralAtoms< DEVICE_TYPE_A, DEVICE_A > &dst, const impl::GeneralAtoms< DEVICE_TYPE_B, DEVICE_B > &src)
Definition Atoms.hpp:151
KOKKOS_INLINE_FUNCTION constexpr idx_t idx_c(const T &val)
Definition datatypes.hpp:93
static constexpr int DIMENSIONS
number of spatial dimensions
Definition constants.hpp:24
Cabana::LinkedCellList< typename DeviceType::memory_space > LinkedCellList
Definition datatypes.hpp:179
int64_t idx_t
Definition datatypes.hpp:91
double real_t
Definition datatypes.hpp:99
Definition Array.hpp:21