ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
mark_boundary_cells.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-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 "cell_system/Cell.hpp"
23#include "ghosts/HaloPlan.hpp"
24
25#include <functional>
26#include <span>
27#include <unordered_map>
28#include <unordered_set>
29
30namespace GhostComm {
31
32/**
33 * @brief Classify each local cell as interior or boundary.
34 *
35 * A local cell is *boundary* for two independent reasons:
36 *
37 * Source 1 — geometric wrap rules: the cell has a ghost neighbor (rule a)
38 * or participates in a periodic wrap-around neighborship on a single MPI
39 * rank (rule b, captured by the optional @p wrap_predicate). These rules
40 * serve future Euclidean-distance uses: wrap-neighborship must mark a cell
41 * boundary even when it is not exported by the current plan.
42 *
43 * Source 2 — plan membership: the cell appears as a send source in any
44 * NeighborComm or as a LocalComm src. Plans for geometries such as
45 * Lees-Edwards fully-connected boundaries and ELC periodicity-change paths
46 * can export cells that the geometric rules never see. Those cells must
47 * still be boundary so that the integrator step-2 / force-reduce overlap
48 * does not update their velocities before remote force contributions
49 * arrive.
50 *
51 * Both sources are complementary: keep both to satisfy present and future
52 * callers. @ref mark_plan_cells_boundary applies source 2.
53 *
54 * All other local cells are *interior*.
55 *
56 * The function first resets every local cell to interior (idempotent on
57 * repeated calls / plan rebuild), then marks boundary cells.
58 *
59 * Call this right after `m_halo_plan = make_halo_plan()` in each
60 * decomposition, where `local_cells()` and `ghost_cells()` are already
61 * populated.
62 *
63 * @param local_cells Local cell pointer span.
64 * @param ghost_cells Ghost cell pointer span.
65 * @param wrap_predicate Optional predicate <tt>(Cell const *a, Cell const *b)
66 * -> bool</tt> that returns @c true when the neighbor relation a->b crosses
67 * a periodic box boundary (both cells are local).
68 * When omitted, no additional wrapping boundary is detected.
69 */
71 std::span<Cell *const> local_cells, std::span<Cell *const> ghost_cells,
72 std::function<bool(Cell const *, Cell const *)> wrap_predicate = nullptr) {
73 // Build a set of ghost ParticleList pointers for O(1) lookup.
74 std::unordered_set<ParticleList const *> ghost_set;
75 ghost_set.reserve(ghost_cells.size());
76 for (Cell *c : ghost_cells) {
77 ghost_set.insert(&c->particles());
78 }
79
80 // Reset all local cells to interior first (idempotent on rebuild).
81 for (Cell *c : local_cells) {
82 c->m_is_boundary = false;
83 }
84
85 // Mark a cell as boundary iff any of its neighbors is a ghost (rule a) or
86 // the wrap predicate fires for that neighbor pair (rule b).
87 for (Cell *c : local_cells) {
88 for (Cell *n : c->neighbors().all()) {
89 if (ghost_set.count(&n->particles()) ||
90 (wrap_predicate && wrap_predicate(c, n))) {
91 c->m_is_boundary = true;
92 break;
93 }
94 }
95 }
96}
97
98/**
99 * @brief Mark plan-exported local cells as boundary (source 2, see
100 * @ref mark_boundary_cells).
101 *
102 * Iterates the halo plan and marks every local cell that appears as a send
103 * source. Specifically: every `NeighborComm::send[k].cell` and every
104 * `LocalComm::src`. Recv/dst targets are ghost cells by construction and
105 * are not touched.
106 *
107 * Call this immediately after @ref mark_boundary_cells so that the combined
108 * invariant "interior ⇒ not exported by the plan" holds for any plan shape.
109 *
110 * @param plan The halo plan produced by `make_halo_plan()`.
111 * @param local_cells Local cell pointer span (used to build the ParticleList ->
112 * Cell reverse map in O(n)).
113 */
114inline void mark_plan_cells_boundary(HaloPlan const &plan,
115 std::span<Cell *const> local_cells) {
116 // Build a reverse map: ParticleList* -> Cell* for O(1) lookup.
117 std::unordered_map<ParticleList const *, Cell *> pl_to_cell;
118 pl_to_cell.reserve(local_cells.size());
119 for (Cell *c : local_cells) {
120 pl_to_cell[&c->particles()] = c;
121 }
122
123 // Mark every NeighborComm send source boundary.
124 for (auto const &nc : plan.neighbors) {
125 for (auto const &sr : nc.send) {
126 auto it = pl_to_cell.find(sr.cell);
127 if (it != pl_to_cell.end()) {
128 it->second->m_is_boundary = true;
129 }
130 }
131 }
132
133 // Mark every LocalComm src boundary.
134 for (auto const &lc : plan.local) {
135 auto it = pl_to_cell.find(lc.src);
136 if (it != pl_to_cell.end()) {
137 it->second->m_is_boundary = true;
138 }
139 }
140}
141
142} // namespace GhostComm
Definition Cell.hpp:96
void mark_boundary_cells(std::span< Cell *const > local_cells, std::span< Cell *const > ghost_cells, std::function< bool(Cell const *, Cell const *)> wrap_predicate=nullptr)
Classify each local cell as interior or boundary.
void mark_plan_cells_boundary(HaloPlan const &plan, std::span< Cell *const > local_cells)
Mark plan-exported local cells as boundary (source 2, see mark_boundary_cells).
std::vector< NeighborComm > neighbors
Definition HaloPlan.hpp:61
std::vector< LocalComm > local
Definition HaloPlan.hpp:62