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
47
53
58 Coulomb::ShortRangeForceKernel::kernel_type const *coulomb_f_kernel_)
59 : data(std::move(data_)), bond_list(std::move(bond_list_)),
60 bond_ids(std::move(bond_ids_)), coulomb_f_kernel(coulomb_f_kernel_) {}
61
62 ESPRESSO_ATTR_ALWAYS_INLINE KOKKOS_INLINE_FUNCTION void
63 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 =
83 calc_bonded_virial_pressure_tensor(iaparams, pos1, pos2, box_geo,
85#ifdef ESPRESSO_ELECTROSTATICS
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
113 : data(std::move(data_)), bond_list(std::move(bond_list_)),
114 bond_ids(std::move(bond_ids_)) {}
115
116 ESPRESSO_ATTR_ALWAYS_INLINE KOKKOS_INLINE_FUNCTION void
117 operator()(std::size_t idx) const {
118 auto const &bonded_ias = data.bonded_ias;
119 auto const &box_geo = data.box_geo;
120 auto &local_pressure = data.local_pressure;
121 auto const &layout = data.layout;
122 auto const &aosoa = data.aosoa;
123 auto const bond_id = bond_ids(idx);
124
125 // TODO: omp_get_thread_num() is only available for the OpenMP backend.
126 // This should be updated when using other Kokkos backends.
127 auto const thread_id = omp_get_thread_num();
128
129 auto const i = bond_list(idx, 0);
130 auto const j = bond_list(idx, 1);
131 auto const k = bond_list(idx, 2);
132 auto const &iaparams = *bonded_ias.at(bond_id);
133
134 auto const pos1 = aosoa.get_vector_at(aosoa.position, i);
135 auto const pos2 = aosoa.get_vector_at(aosoa.position, j);
136 auto const pos3 = aosoa.get_vector_at(aosoa.position, k);
137
138 std::optional<Utils::Matrix<double, 3, 3>> pressure =
139 calc_bonded_three_body_pressure_tensor(iaparams, pos1, pos2, pos3,
140 box_geo);
141
142 if (pressure) {
143 auto const flat = Utils::flatten(*pressure);
144 for (std::size_t k2 = 0; k2 < 9; ++k2)
145 local_pressure(thread_id,
146 layout.tensor_offset(layout.bonded_idx(bond_id), k2)) +=
147 flat[k2];
148 } else {
149 std::array<int, 2> pids = {aosoa.id(j), aosoa.id(k)};
150 bond_broken_error(aosoa.id(i), {pids.data(), 2});
151 }
152 }
153};
154
156 KOKKOS_INLINE_FUNCTION void operator()(std::size_t) const {}
157};
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.
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_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)
BondsPressureKernelData data
LocalBondState::AngleBondIDType bond_ids
ESPRESSO_ATTR_ALWAYS_INLINE KOKKOS_INLINE_FUNCTION void operator()(std::size_t idx) const
LocalBondState::AngleBondlistType bond_list
AngleBondsPressureKernel(BondsPressureKernelData data_, LocalBondState::AngleBondlistType bond_list_, LocalBondState::AngleBondIDType bond_ids_)
Kokkos::View< double **, Kokkos::LayoutRight > local_pressure
BondedInteractionsMap const & bonded_ias
CellStructure::AoSoA_pack const & aosoa
Solver::ShortRangeForceKernel kernel_type
Kokkos::View< int *[2], Kokkos::LayoutRight > PairBondlistType
Kokkos::View< int *[3], Kokkos::LayoutRight > AngleBondlistType
Kokkos::View< int *, Kokkos::LayoutRight > PairBondIDType
Kokkos::View< int *, Kokkos::LayoutRight > AngleBondIDType
KOKKOS_INLINE_FUNCTION void operator()(std::size_t) const
ESPRESSO_ATTR_ALWAYS_INLINE KOKKOS_INLINE_FUNCTION void operator()(std::size_t idx) const
BondsPressureKernelData data
LocalBondState::PairBondIDType bond_ids
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