ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
LocalBondState.cpp
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#include <config/config.hpp>
21
22#include "LocalBondState.hpp"
23
25 if (pair_list.is_allocated()) {
26 Kokkos::realloc(Kokkos::view_alloc(Kokkos::WithoutInitializing), pair_list,
28 Kokkos::realloc(Kokkos::view_alloc(Kokkos::WithoutInitializing), pair_ids,
30 Kokkos::realloc(Kokkos::view_alloc(Kokkos::WithoutInitializing), angle_list,
32 Kokkos::realloc(Kokkos::view_alloc(Kokkos::WithoutInitializing), angle_ids,
34 Kokkos::realloc(Kokkos::view_alloc(Kokkos::WithoutInitializing),
36 Kokkos::realloc(Kokkos::view_alloc(Kokkos::WithoutInitializing),
38 } else {
40 Kokkos::ViewAllocateWithoutInitializing("pair_bond_list"), pair_count);
42 Kokkos::ViewAllocateWithoutInitializing("pair_bond_id"), pair_count);
44 Kokkos::ViewAllocateWithoutInitializing("angle_bond_list"),
47 Kokkos::ViewAllocateWithoutInitializing("angle_bond_id"), angle_count);
49 Kokkos::ViewAllocateWithoutInitializing("dihedral_bond_list"),
52 Kokkos::ViewAllocateWithoutInitializing("dihedral_bond_id"),
54 }
55}
56
59 // Reset Kokkos Views to default (unallocated) state
66#ifdef ESPRESSO_COLLISION_DETECTION
68#endif
69}
70
73#ifdef ESPRESSO_COLLISION_DETECTION
75#endif
76}
77
78#ifdef ESPRESSO_COLLISION_DETECTION
80 new_pair_list.clear();
81 new_pair_ids.clear();
82 new_angle_list.clear();
83 new_angle_ids.clear();
84 new_dihedral_list.clear();
85 new_dihedral_ids.clear();
86}
87
89 std::vector<int> const &particle_ids,
90 Kokkos::View<int *> const &id_to_index) {
91 if (particle_ids.size() == 2u) {
92 new_pair_list.reserve(new_pair_list.size() + 2u);
93 for (auto pid : particle_ids)
94 new_pair_list.emplace_back(id_to_index(pid));
95 new_pair_ids.emplace_back(bond_id);
96 pair_count++;
97 } else if (particle_ids.size() == 3u) {
98 new_angle_list.reserve(new_angle_list.size() + 3u);
99 for (auto pid : particle_ids)
100 new_angle_list.emplace_back(id_to_index(pid));
101 new_angle_ids.emplace_back(bond_id);
102 angle_count++;
103 } else if (particle_ids.size() == 4u) {
104 new_dihedral_list.reserve(new_dihedral_list.size() + 4u);
105 for (auto pid : particle_ids)
106 new_dihedral_list.emplace_back(id_to_index(pid));
107 new_dihedral_ids.emplace_back(bond_id);
109 }
110}
111
112namespace {
113template <typename BondListT, typename BondIDT>
114void rebuild_bond_list_impl(std::vector<int> const &new_bond_list,
115 std::vector<int> const &new_bond_ids,
116 BondListT &bond_list, BondIDT &bond_ids,
117 int total_bond_count) {
118 if (new_bond_list.empty())
119 return;
120
121 auto new_data_view = Kokkos::View<const int *, Kokkos::HostSpace,
123 new_bond_list.data(), new_bond_list.size());
124 auto new_id_view = Kokkos::View<const int *, Kokkos::HostSpace,
126 new_bond_ids.data(), new_bond_ids.size());
127
128 auto old_count = total_bond_count - static_cast<int>(new_bond_ids.size());
129
130 BondListT rebuilt_list(
131 Kokkos::ViewAllocateWithoutInitializing("bond_list_rebuild"),
132 total_bond_count);
133 BondIDT rebuilt_ids(
134 Kokkos::ViewAllocateWithoutInitializing("bond_id_rebuild"),
135 total_bond_count);
136
137 Kokkos::deep_copy(
138 Kokkos::subview(rebuilt_list, std::make_pair(0, old_count),
139 Kokkos::ALL()),
140 Kokkos::subview(bond_list, std::make_pair(0, old_count), Kokkos::ALL()));
141 Kokkos::deep_copy(Kokkos::subview(rebuilt_ids, std::make_pair(0, old_count)),
142 Kokkos::subview(bond_ids, std::make_pair(0, old_count)));
143
144 Kokkos::parallel_for(
145 "copy_bondlist", new_bond_list.size(),
146 [&bond_view = rebuilt_list, old_count, &new_data_view](auto flat_idx) {
147 constexpr int NCols =
148 BondListT::rank == 2 ? static_cast<int>(BondListT::static_extent(1))
149 : 1;
150 auto bond_idx = old_count + static_cast<int>(flat_idx / NCols);
151 auto col_idx = static_cast<int>(flat_idx % NCols);
152 bond_view(bond_idx, col_idx) = new_data_view(flat_idx);
153 });
154
155 Kokkos::parallel_for(
156 "copy_bond_ids", new_bond_ids.size(),
157 [&id_view = rebuilt_ids, old_count, &new_id_view](auto idx) {
158 id_view(old_count + static_cast<int>(idx)) = new_id_view(idx);
159 });
160
161 bond_list = rebuilt_list;
162 bond_ids = rebuilt_ids;
163}
164} // anonymous namespace
165
167 rebuild_bond_list_impl(new_pair_list, new_pair_ids, pair_list, pair_ids,
168 pair_count);
169 rebuild_bond_list_impl(new_angle_list, new_angle_ids, angle_list, angle_ids,
171 rebuild_bond_list_impl(new_dihedral_list, new_dihedral_ids, dihedral_list,
174}
175#endif // ESPRESSO_COLLISION_DETECTION
void rebuild_bond_list_impl(std::vector< int > const &new_bond_list, std::vector< int > const &new_bond_ids, BondListT &bond_list, BondIDT &bond_ids, int total_bond_count)
Kokkos::View< int *[4], Kokkos::LayoutRight > DihedralBondlistType
std::vector< int > new_angle_ids
void allocate()
Allocate or reallocate all Kokkos Views to current counts.
Kokkos::View< int *[2], Kokkos::LayoutRight > PairBondlistType
AngleBondIDType angle_ids
std::vector< int > new_dihedral_list
Kokkos::View< int *, Kokkos::LayoutRight > DihedralBondIDType
DihedralBondlistType dihedral_list
AngleBondlistType angle_list
std::vector< int > new_pair_list
PairBondIDType pair_ids
void reset()
Reset counts + collision vectors.
Kokkos::View< int *[3], Kokkos::LayoutRight > AngleBondlistType
Kokkos::View< int *, Kokkos::LayoutRight > PairBondIDType
std::vector< int > new_angle_list
void clear()
Deallocates Views.
Kokkos::View< int *, Kokkos::LayoutRight > AngleBondIDType
void add_new_bond(int bond_id, std::vector< int > const &particle_ids, Kokkos::View< int * > const &id_to_index)
std::vector< int > new_dihedral_ids
std::vector< int > new_pair_ids
DihedralBondIDType dihedral_ids
PairBondlistType pair_list