Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
BiCGSTABInit.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
18
19#include "shambase/memory.hpp"
27
29
30 template<class Tvec, class TgridVec>
32 StackEntry stack_loc{};
33 auto edges = get_edges();
34
35 edges.spans_block_cell_sizes.check_sizes(edges.sizes.indexes);
36 edges.spans_phi.check_sizes(edges.sizes.indexes);
37 edges.spans_rho.check_sizes(edges.sizes.indexes);
38 edges.spans_phi_res.check_sizes(edges.sizes.indexes);
39 edges.spans_phi_res_bis.check_sizes(edges.sizes.indexes);
40 edges.spans_phi_p.check_sizes(edges.sizes.indexes);
41 edges.spans_rhs.check_sizes(edges.sizes.indexes);
42
43 auto &q = shamsys::instance::get_compute_scheduler().get_queue();
44 Tscal mean_rho = edges.mean_rho.value;
45
46 edges.cell_neigh_graph.graph.for_each(
47 [&](u64 id, const OrientedAMRGraph<Tvec, TgridVec> &oriented_cell_graph) {
48 auto &cell_sizes_span = edges.spans_block_cell_sizes.get_spans().get(id);
49 auto &phi_span = edges.spans_phi.get_spans().get(id);
50 auto &rho_span = edges.spans_rho.get_spans().get(id);
51 auto &phi_res_span = edges.spans_phi_res.get_spans().get(id);
52 auto &phi_res_bis_span = edges.spans_phi_res_bis.get_spans().get(id);
53 auto &phi_p_span = edges.spans_phi_p.get_spans().get(id);
54 auto &rhs_span = edges.spans_rhs.get_spans().get(id);
55
56 auto get_graph = [&](Direction dir) -> const AMRGraph & {
57 return shambase::get_check_ref(oriented_cell_graph.graph_links[dir]);
58 };
59
60 const AMRGraph &graph_neigh_xp = get_graph(Direction::xp);
61 const AMRGraph &graph_neigh_xm = get_graph(Direction::xm);
62 const AMRGraph &graph_neigh_yp = get_graph(Direction::yp);
63 const AMRGraph &graph_neigh_ym = get_graph(Direction::ym);
64 const AMRGraph &graph_neigh_zp = get_graph(Direction::zp);
65 const AMRGraph &graph_neigh_zm = get_graph(Direction::zm);
66
67 u32 cell_count = edges.sizes.indexes.get(id) * block_size;
68
70 q,
72 cell_sizes_span,
73 phi_span,
74 rho_span,
75 graph_neigh_xp,
76 graph_neigh_xm,
77 graph_neigh_yp,
78 graph_neigh_ym,
79 graph_neigh_zp,
80 graph_neigh_zm},
81 sham::MultiRef{phi_res_span, phi_res_bis_span, phi_p_span, rhs_span},
82 cell_count,
83 [block_size = block_size, fourPiG = fourPiG, mean_rho](
84 u32 cell_global_id,
85 const Tscal *__restrict cell_sizes,
86 const Tscal *__restrict phi,
87 const Tscal *__restrict rho,
88 const AMRGraph::ro_access graph_iter_xp,
89 const AMRGraph::ro_access graph_iter_xm,
90 const AMRGraph::ro_access graph_iter_yp,
91 const AMRGraph::ro_access graph_iter_ym,
92 const AMRGraph::ro_access graph_iter_zp,
93 const AMRGraph::ro_access graph_iter_zm,
94 Tscal *__restrict phi_res,
95 Tscal *__restrict phi_res_bis,
96 Tscal *__restrict phi_p,
97 Tscal *__restrict rhs) {
98 Tscal Aphi = laplacian_7pt<Tscal, Tvec>(
99 cell_sizes,
100 block_size,
101 cell_global_id,
102 graph_iter_xp,
103 graph_iter_xm,
104 graph_iter_yp,
105 graph_iter_ym,
106 graph_iter_zp,
107 graph_iter_zm,
108 [=](u32 id) {
109 return phi[id];
110 });
111
112 // laplacian_7pt is volume integrated, hence the dV factor
113 Tscal delta_cell = cell_sizes[cell_global_id / block_size];
114 Tscal dV = delta_cell * delta_cell * delta_cell;
115 Tscal b_rhs = -fourPiG * (rho[cell_global_id] - mean_rho) * dV;
116
117 phi_res[cell_global_id] = b_rhs - Aphi;
118 phi_res_bis[cell_global_id] = 0.5 * (b_rhs - Aphi);
119 phi_p[cell_global_id] = b_rhs - Aphi;
120 rhs[cell_global_id] = b_rhs;
121 });
122 });
123 }
124
125 template<class Tvec, class TgridVec>
127 std::string tex = R"tex(
128 Initialisation step of BiCGSTAB
129 \begin{align}
130 {spans_phi_res}_i &= -4 \pi G ({spans_rho}_i - {mean_rho}) \Delta x_i^3 - (A {spans_phi})_i \\
131 {spans_phi_res_bis}_i &= {spans_phi_res}_i / 2 \\
132 {spans_phi_p}_i &= {spans_phi_res}_i \\
133 {spans_rhs}_i &= -4 \pi G ({spans_rho}_i - {mean_rho}) \Delta x_i^3
134 \end{align}
135 )tex";
136
137 replace_edges_tex_symbols(tex);
138
139 return tex;
140 }
141
142} // namespace shammodels::basegodunov::modules
143
Initialisation step of the BiCGSTAB Poisson solver.
7-point stencil for the discrete Laplacian operator
Header file describing a Node Instance.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
Initialisation step of BiCGSTAB: compute the right-hand side , the residual , the shadow residual an...
virtual std::string _impl_get_tex() const
get the tex of the node
void kernel_call(sham::DeviceQueue &q, RefIn in, RefOut in_out, u32 n, Functor &&func, SourceLocation &&callsite=SourceLocation{})
Submit a kernel to a SYCL queue.
T & get_check_ref(const std::unique_ptr< T > &ptr, SourceLocation loc=SourceLocation())
Takes a std::unique_ptr and returns a reference to the object it holds. It throws a std::runtime_erro...
Definition memory.hpp:112
namespace for the basegodunov model modules
T laplacian_7pt(const shambase::VecComponent< Tvec > *cell_sizes, const u32 block_size, const u32 cell_global_id, const AMRGraphLinkiterator &graph_iter_xp, const AMRGraphLinkiterator &graph_iter_xm, const AMRGraphLinkiterator &graph_iter_yp, const AMRGraphLinkiterator &graph_iter_ym, const AMRGraphLinkiterator &graph_iter_zp, const AMRGraphLinkiterator &graph_iter_zm, ACCField &&field_access)
Get the discretized (finite volume) Laplacian.
This file contains the definition for the stacktrace related functionality.
shambase::details::BasicStackEntry StackEntry
Alias for shambase::details::BasicStackEntry.
A class that references multiple buffers or similar objects.
Definition MultiRef.hpp:33