ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
bond_pressure_kokkos.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <config/config.hpp>
23
24#include "aosoa_pack.hpp"
25#include "bond_error.hpp"
27#include "pressure_cabana.hpp"
28#include "pressure_inline.hpp"
29
30#include <utils/Vector.hpp>
32
33#include <Kokkos_Core.hpp>
34
35#include <omp.h>
36
37#include <array>
38#include <cstddef>
39
41 using execution_space = Kokkos::HostSpace;
44 Kokkos::View<double **, Kokkos::LayoutRight, execution_space> local_pressure;
47};
48
54
62
63 ESPRESSO_ATTR_ALWAYS_INLINE inline void operator()(std::size_t idx) const {
64 auto const &bonded_ias = data.bonded_ias;
65 auto const &box_geo = data.box_geo;
66 auto &local_pressure = data.local_pressure;
67 auto const &layout = data.layout;
68 auto const &aosoa = data.aosoa;
69 auto const bond_id = bond_ids(idx);
70
71 // TODO: omp_get_thread_num() is only available for the OpenMP backend.
72 // This should be updated when using other Kokkos backends.
73 auto const thread_id = omp_get_thread_num();
74
75 auto const i = bond_list(idx, 0);
76 auto const j = bond_list(idx, 1);
77 auto const &iaparams = *bonded_ias.at(bond_id);
78
79 auto const pos1 = aosoa.get_vector_at(aosoa.position, i);
80 auto const pos2 = aosoa.get_vector_at(aosoa.position, j);
81
82 std::optional<Utils::Matrix<double, 3, 3>> pressure =
86 aosoa.charge(i) * aosoa.charge(j)
87#else
88 0.0
89#endif
90 );
91
92 if (pressure) {
93 auto const flat = Utils::flatten(*pressure);
94 for (std::size_t k = 0; k < 9; ++k)
95 local_pressure(thread_id,
96 layout.tensor_offset(layout.bonded_idx(bond_id), k)) +=
97 flat[k];
98 } else {
99 auto partner_id = aosoa.id(j);
100 bond_broken_error(aosoa.id(i), {&partner_id, 1});
101 }
102 }
103};
104
109
115
116 ESPRESSO_ATTR_ALWAYS_INLINE inline void operator()(std::size_t idx) const {
117 auto const &bonded_ias = data.bonded_ias;
118 auto const &box_geo = data.box_geo;
119 auto &local_pressure = data.local_pressure;
120 auto const &layout = data.layout;
121 auto const &aosoa = data.aosoa;
122 auto const bond_id = bond_ids(idx);
123
124 // TODO: omp_get_thread_num() is only available for the OpenMP backend.
125 // This should be updated when using other Kokkos backends.
126 auto const thread_id = omp_get_thread_num();
127
128 auto const i = bond_list(idx, 0);
129 auto const j = bond_list(idx, 1);
130 auto const k = bond_list(idx, 2);
131 auto const &iaparams = *bonded_ias.at(bond_id);
132
133 auto const pos1 = aosoa.get_vector_at(aosoa.position, i);
134 auto const pos2 = aosoa.get_vector_at(aosoa.position, j);
135 auto const pos3 = aosoa.get_vector_at(aosoa.position, k);
136
137 std::optional<Utils::Matrix<double, 3, 3>> pressure =
139 box_geo);
140
141 if (pressure) {
142 auto const flat = Utils::flatten(*pressure);
143 for (std::size_t k2 = 0; k2 < 9; ++k2)
144 local_pressure(thread_id,
145 layout.tensor_offset(layout.bonded_idx(bond_id), k2)) +=
146 flat[k2];
147 } else {
148 std::array<int, 2> pids = {aosoa.id(j), aosoa.id(k)};
149 bond_broken_error(aosoa.id(i), {pids.data(), 2});
150 }
151 }
152};
153
158
164
165 ESPRESSO_ATTR_ALWAYS_INLINE inline void operator()(std::size_t idx) const {
166 auto const &bonded_ias = data.bonded_ias;
167 auto const &box_geo = data.box_geo;
168 auto &local_pressure = data.local_pressure;
169 auto const &layout = data.layout;
170 auto const &aosoa = data.aosoa;
171 auto const bond_id = bond_ids(idx);
172
173 // TODO: omp_get_thread_num() is only available for the OpenMP backend.
174 // This should be updated when using other Kokkos backends.
175 auto const thread_id = omp_get_thread_num();
176
177 auto const i = bond_list(idx, 0);
178 auto const j = bond_list(idx, 1);
179 auto const k = bond_list(idx, 2);
180 auto const m = bond_list(idx, 3);
181 auto const &iaparams = *bonded_ias.at(bond_id);
182
183 auto const pos1 = aosoa.get_vector_at(aosoa.position, i);
184 auto const pos2 = aosoa.get_vector_at(aosoa.position, j);
185 auto const pos3 = aosoa.get_vector_at(aosoa.position, k);
186 auto const pos4 = aosoa.get_vector_at(aosoa.position, m);
187
188 std::optional<Utils::Matrix<double, 3, 3>> pressure =
190 box_geo);
191
192 if (pressure) {
193 auto const flat = Utils::flatten(*pressure);
194 for (std::size_t k3 = 0; k3 < 9; ++k3)
195 local_pressure(thread_id,
196 layout.tensor_offset(layout.bonded_idx(bond_id), k3)) +=
197 flat[k3];
198 } else {
199 std::array<int, 3> pids = {aosoa.id(j), aosoa.id(k), aosoa.id(m)};
200 bond_broken_error(aosoa.id(i), {pids.data(), 3});
201 }
202 }
203};
Vector implementation and trait types for boost qvm interoperability.
#define ESPRESSO_ATTR_ALWAYS_INLINE
void bond_broken_error(int id, std::span< const int > partner_ids)
container for bonded interactions.
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
void flatten(Range const &v, OutputIterator out)
Flatten a range of ranges.
Definition flatten.hpp:56
STL namespace.
std::optional< Utils::Matrix< double, 3, 3 > > calc_bonded_four_body_pressure_tensor(Bonded_IA_Parameters const &iaparams, Utils::Vector3d const &pos1, Utils::Vector3d const &pos2, Utils::Vector3d const &pos3, Utils::Vector3d const &pos4, BoxGeometry const &box_geo)
std::optional< Utils::Matrix< double, 3, 3 > > calc_bonded_three_body_pressure_tensor(Bonded_IA_Parameters const &iaparams, Utils::Vector3d const &pos1, Utils::Vector3d const &pos2, Utils::Vector3d const &pos3, BoxGeometry const &box_geo)
std::optional< Utils::Matrix< double, 3, 3 > > calc_bonded_virial_pressure_tensor(Bonded_IA_Parameters const &iaparams, Utils::Vector3d const &pos1, Utils::Vector3d const &pos2, BoxGeometry const &box_geo, Coulomb::ShortRangeForceKernel::kernel_type const *kernel, double q1q2)
ESPRESSO_ATTR_ALWAYS_INLINE void operator()(std::size_t idx) const
BondsPressureKernelData data
LocalBondState::AngleBondIDType bond_ids
LocalBondState::AngleBondlistType bond_list
AngleBondsPressureKernel(BondsPressureKernelData data_, LocalBondState::AngleBondlistType bond_list_, LocalBondState::AngleBondIDType bond_ids_)
BondedInteractionsMap const & bonded_ias
CellStructure::AoSoA_pack const & aosoa
Kokkos::View< double **, Kokkos::LayoutRight, execution_space > local_pressure
Solver::ShortRangeForceKernel kernel_type
ESPRESSO_ATTR_ALWAYS_INLINE void operator()(std::size_t idx) const
LocalBondState::DihedralBondIDType bond_ids
DihedralBondsPressureKernel(BondsPressureKernelData data_, LocalBondState::DihedralBondlistType bond_list_, LocalBondState::DihedralBondIDType bond_ids_)
LocalBondState::DihedralBondlistType bond_list
Kokkos::View< int *, Kokkos::LayoutRight, execution_space > AngleBondIDType
Kokkos::View< int *[3], Kokkos::LayoutRight, execution_space > AngleBondlistType
Kokkos::View< int *[2], Kokkos::LayoutRight, execution_space > PairBondlistType
Kokkos::View< int *, Kokkos::LayoutRight, execution_space > DihedralBondIDType
Kokkos::View< int *[4], Kokkos::LayoutRight, execution_space > DihedralBondlistType
Kokkos::View< int *, Kokkos::LayoutRight, execution_space > PairBondIDType
BondsPressureKernelData data
LocalBondState::PairBondIDType bond_ids
ESPRESSO_ATTR_ALWAYS_INLINE void operator()(std::size_t idx) const
PairBondsPressureKernel(BondsPressureKernelData data_, LocalBondState::PairBondlistType bond_list_, LocalBondState::PairBondIDType bond_ids_, Coulomb::ShortRangeForceKernel::kernel_type const *coulomb_f_kernel_)
Coulomb::ShortRangeForceKernel::kernel_type const *const coulomb_f_kernel
LocalBondState::PairBondlistType bond_list