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 {
39 using execution_space = Kokkos::DefaultHostExecutionSpace;
40 pair_list = PairBondlistType(Kokkos::view_alloc(execution_space{},
41 Kokkos::WithoutInitializing,
42 "pair_bond_list"),
44 pair_ids = PairBondIDType(Kokkos::view_alloc(execution_space{},
45 Kokkos::WithoutInitializing,
46 "pair_bond_id"),
49 Kokkos::view_alloc(execution_space{}, Kokkos::WithoutInitializing,
50 "angle_bond_list"),
52 angle_ids = AngleBondIDType(Kokkos::view_alloc(execution_space{},
53 Kokkos::WithoutInitializing,
54 "angle_bond_id"),
57 Kokkos::view_alloc(execution_space{}, Kokkos::WithoutInitializing,
58 "dihedral_bond_list"),
61 Kokkos::view_alloc(execution_space{}, Kokkos::WithoutInitializing,
62 "dihedral_bond_id"),
64 }
65}
66
69 // Reset Kokkos Views to default (unallocated) state
76#ifdef ESPRESSO_COLLISION_DETECTION
78#endif
79}
80
83#ifdef ESPRESSO_COLLISION_DETECTION
85#endif
86}
87
88#ifdef ESPRESSO_COLLISION_DETECTION
90 new_pair_list.clear();
91 new_pair_ids.clear();
92 new_angle_list.clear();
93 new_angle_ids.clear();
94 new_dihedral_list.clear();
95 new_dihedral_ids.clear();
96}
97
99 int bond_id, std::vector<int> const &particle_ids,
100 Kokkos::View<int *, execution_space> const &id_to_index) {
101 if (particle_ids.size() == 2u) {
102 new_pair_list.reserve(new_pair_list.size() + 2u);
103 for (auto pid : particle_ids)
104 new_pair_list.emplace_back(id_to_index(pid));
105 new_pair_ids.emplace_back(bond_id);
106 pair_count++;
107 } else if (particle_ids.size() == 3u) {
108 new_angle_list.reserve(new_angle_list.size() + 3u);
109 for (auto pid : particle_ids)
110 new_angle_list.emplace_back(id_to_index(pid));
111 new_angle_ids.emplace_back(bond_id);
112 angle_count++;
113 } else if (particle_ids.size() == 4u) {
114 new_dihedral_list.reserve(new_dihedral_list.size() + 4u);
115 for (auto pid : particle_ids)
116 new_dihedral_list.emplace_back(id_to_index(pid));
117 new_dihedral_ids.emplace_back(bond_id);
119 }
120}
121
122namespace {
123template <typename BondListT, typename BondIDT>
124void rebuild_bond_list_impl(std::vector<int> const &new_bond_list,
125 std::vector<int> const &new_bond_ids,
126 BondListT &bond_list, BondIDT &bond_ids,
127 int total_bond_count) {
128 if (new_bond_list.empty())
129 return;
130
131 auto new_data_view = Kokkos::View<const int *, Kokkos::HostSpace,
132 Kokkos::MemoryTraits<Kokkos::Unmanaged>>(
133 new_bond_list.data(), new_bond_list.size());
134 auto new_id_view = Kokkos::View<const int *, Kokkos::HostSpace,
135 Kokkos::MemoryTraits<Kokkos::Unmanaged>>(
136 new_bond_ids.data(), new_bond_ids.size());
137
138 auto old_count = total_bond_count - static_cast<int>(new_bond_ids.size());
139
140 using execution_space = Kokkos::DefaultHostExecutionSpace;
141 BondListT rebuilt_list(Kokkos::view_alloc(execution_space{},
142 Kokkos::WithoutInitializing,
143 "bond_list_rebuild"),
145 BondIDT rebuilt_ids(Kokkos::view_alloc(execution_space{},
146 Kokkos::WithoutInitializing,
147 "bond_id_rebuild"),
149
150 Kokkos::deep_copy(
151 Kokkos::subview(rebuilt_list, std::make_pair(0, old_count),
152 Kokkos::ALL()),
153 Kokkos::subview(bond_list, std::make_pair(0, old_count), Kokkos::ALL()));
154 Kokkos::deep_copy(Kokkos::subview(rebuilt_ids, std::make_pair(0, old_count)),
155 Kokkos::subview(bond_ids, std::make_pair(0, old_count)));
156
157 Kokkos::parallel_for(
158 "copy_bondlist",
159 Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>(
160 std::size_t{0}, new_bond_list.size()),
162 constexpr int NCols =
163 BondListT::rank == 2 ? static_cast<int>(BondListT::static_extent(1))
164 : 1;
165 auto bond_idx = old_count + static_cast<int>(flat_idx / NCols);
166 auto col_idx = static_cast<int>(flat_idx % NCols);
168 });
169
170 Kokkos::parallel_for(
171 "copy_bond_ids",
172 Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>(
173 std::size_t{0}, new_bond_ids.size()),
175 id_view(old_count + static_cast<int>(idx)) = new_id_view(idx);
176 });
177
178 bond_list = rebuilt_list;
179 bond_ids = rebuilt_ids;
180}
181} // anonymous namespace
182
184 rebuild_bond_list_impl(new_pair_list, new_pair_ids, pair_list, pair_ids,
185 pair_count);
186 rebuild_bond_list_impl(new_angle_list, new_angle_ids, angle_list, angle_ids,
188 rebuild_bond_list_impl(new_dihedral_list, new_dihedral_ids, dihedral_list,
191}
192#endif // ESPRESSO_COLLISION_DETECTION
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
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 *, Kokkos::LayoutRight, execution_space > AngleBondIDType
std::vector< int > new_angle_ids
Kokkos::View< int *[3], Kokkos::LayoutRight, execution_space > AngleBondlistType
void allocate()
Allocate or reallocate all Kokkos Views to current counts.
AngleBondIDType angle_ids
Kokkos::View< int *[2], Kokkos::LayoutRight, execution_space > PairBondlistType
std::vector< int > new_dihedral_list
DihedralBondlistType dihedral_list
AngleBondlistType angle_list
void add_new_bond(int bond_id, std::vector< int > const &particle_ids, Kokkos::View< int *, execution_space > const &id_to_index)
std::vector< int > new_pair_list
PairBondIDType pair_ids
void reset()
Reset counts + collision vectors.
std::vector< int > new_angle_list
void clear()
Deallocates Views.
std::vector< int > new_dihedral_ids
Kokkos::DefaultHostExecutionSpace execution_space
Kokkos::View< int *, Kokkos::LayoutRight, execution_space > DihedralBondIDType
std::vector< int > new_pair_ids
Kokkos::View< int *[4], Kokkos::LayoutRight, execution_space > DihedralBondlistType
Kokkos::View< int *, Kokkos::LayoutRight, execution_space > PairBondIDType
DihedralBondIDType dihedral_ids
PairBondlistType pair_list