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#ifndef NDEBUG
102 for (auto pid : particle_ids) {
103 assert(pid >= 0 and static_cast<std::size_t>(pid) < id_to_index.extent(0));
104 assert(id_to_index(pid) != -1);
105 }
106#endif // not NDEBUG
107 if (particle_ids.size() == 2u) {
108 new_pair_list.reserve(new_pair_list.size() + 2u);
109 for (auto pid : particle_ids)
110 new_pair_list.emplace_back(id_to_index(pid));
111 new_pair_ids.emplace_back(bond_id);
112 pair_count++;
113 } else if (particle_ids.size() == 3u) {
114 new_angle_list.reserve(new_angle_list.size() + 3u);
115 for (auto pid : particle_ids)
116 new_angle_list.emplace_back(id_to_index(pid));
117 new_angle_ids.emplace_back(bond_id);
118 angle_count++;
119 } else if (particle_ids.size() == 4u) {
120 new_dihedral_list.reserve(new_dihedral_list.size() + 4u);
121 for (auto pid : particle_ids)
122 new_dihedral_list.emplace_back(id_to_index(pid));
123 new_dihedral_ids.emplace_back(bond_id);
125 }
126}
127
128namespace {
129template <typename BondListT, typename BondIDT>
130void rebuild_bond_list_impl(std::vector<int> const &new_bond_list,
131 std::vector<int> const &new_bond_ids,
132 BondListT &bond_list, BondIDT &bond_ids,
133 int total_bond_count) {
134 if (new_bond_list.empty())
135 return;
136
137 auto new_data_view = Kokkos::View<const int *, Kokkos::HostSpace,
138 Kokkos::MemoryTraits<Kokkos::Unmanaged>>(
139 new_bond_list.data(), new_bond_list.size());
140 auto new_id_view = Kokkos::View<const int *, Kokkos::HostSpace,
141 Kokkos::MemoryTraits<Kokkos::Unmanaged>>(
142 new_bond_ids.data(), new_bond_ids.size());
143
144 auto old_count = total_bond_count - static_cast<int>(new_bond_ids.size());
145
146 using execution_space = Kokkos::DefaultHostExecutionSpace;
147 BondListT rebuilt_list(Kokkos::view_alloc(execution_space{},
148 Kokkos::WithoutInitializing,
149 "bond_list_rebuild"),
151 BondIDT rebuilt_ids(Kokkos::view_alloc(execution_space{},
152 Kokkos::WithoutInitializing,
153 "bond_id_rebuild"),
155
156 Kokkos::deep_copy(
157 Kokkos::subview(rebuilt_list, std::make_pair(0, old_count),
158 Kokkos::ALL()),
159 Kokkos::subview(bond_list, std::make_pair(0, old_count), Kokkos::ALL()));
160 Kokkos::deep_copy(Kokkos::subview(rebuilt_ids, std::make_pair(0, old_count)),
161 Kokkos::subview(bond_ids, std::make_pair(0, old_count)));
162
163 Kokkos::parallel_for(
164 "copy_bondlist",
165 Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>(
166 std::size_t{0}, new_bond_list.size()),
168 constexpr int NCols =
169 BondListT::rank == 2 ? static_cast<int>(BondListT::static_extent(1))
170 : 1;
171 auto bond_idx = old_count + static_cast<int>(flat_idx / NCols);
172 auto col_idx = static_cast<int>(flat_idx % NCols);
174 });
175
176 Kokkos::parallel_for(
177 "copy_bond_ids",
178 Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>(
179 std::size_t{0}, new_bond_ids.size()),
181 id_view(old_count + static_cast<int>(idx)) = new_id_view(idx);
182 });
183
184 bond_list = rebuilt_list;
185 bond_ids = rebuilt_ids;
186}
187} // anonymous namespace
188
190 rebuild_bond_list_impl(new_pair_list, new_pair_ids, pair_list, pair_ids,
191 pair_count);
192 rebuild_bond_list_impl(new_angle_list, new_angle_ids, angle_list, angle_ids,
194 rebuild_bond_list_impl(new_dihedral_list, new_dihedral_ids, dihedral_list,
197}
198#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