Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
NodeCGLoop.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
19
21#include "shambase/string.hpp"
22#include "shamcomm/logs.hpp"
25#include <cmath>
26
28
29 template<class Tvec, class TgridVec>
31 StackEntry stack_loc{};
32 auto edges = get_edges();
33
34 edges.spans_block_cell_sizes.check_sizes(edges.sizes.indexes);
35 edges.spans_phi.check_sizes(edges.sizes.indexes);
36 edges.spans_rho.check_sizes(edges.sizes.indexes);
37 edges.spans_phi_res.ensure_sizes(edges.sizes.indexes);
38 edges.spans_phi_p.ensure_sizes(edges.sizes.indexes);
39 edges.spans_phi_Ap.ensure_sizes(edges.sizes.indexes);
40 edges.spans_phi_hadamard_prod.ensure_sizes(edges.sizes.indexes);
41 edges.spans_phi_rhs.ensure_sizes(edges.sizes.indexes);
42 edges.spans_phi_z.ensure_sizes(edges.sizes.indexes);
43 edges.spans_rz_hadamard_prod.ensure_sizes(edges.sizes.indexes);
44
45 // r0 = b - A phi0, with b = -4 pi G (rho - mean_rho) dV, and z0 = p0 = r0 / A_ii
46 cg_init_node.evaluate();
47
48 // <r0, r0>
49 res_ddot_node.evaluate();
50
51 // <b, b>
52 rhs_node.evaluate();
53
54 // <r0, z0>
55 rz_hadamard_prod_node.evaluate();
56 rz_reduction_node.evaluate();
57
58 shamlog_debug_ln(
59 "Ramses::CG",
60 sham::format(
61 "k = 0, <r,r> = {}, <r,z> = {}, <b,b> = {}",
62 edges.old_values.data,
63 edges.rz_old_values.data,
64 edges.rhs_norm_values.data));
65
66 // TODO: (kept from #1942) if <p, A p> == 0 (e.g. r0 == 0 when phi0 is already the
67 // solution, or a uniform density with a uniform phi0), alpha = 0 / 0 is NaN and phi becomes
68 // NaN. Likewise, <b, b> == 0 (uniform density) makes the convergence test always false.
69
70 u32 k = 0;
71 bool converged = false;
72 while (k < Niter_max) {
73 k = k + 1;
74
75 // exchange the ghost blocks of p_k (on them the stencil is incomplete, so the update of
76 // p_k at the previous iteration did not give the right values)
77 node_gz_p.evaluate();
78 node_exch_gz_p.evaluate();
79 node_replace_gz_p.evaluate();
80
81 // A p_k
82 spmv_node.evaluate();
83
84 // <p_k, A p_k>
85 hadamard_prod_node.evaluate();
86 a_norm_node.evaluate();
87
88 // alpha_k = <r_k, z_k> / <p_k, A p_k>
89 edges.alpha.data = edges.rz_old_values.data / edges.e_norm.data;
90
91 // phi_{k+1} = phi_k + alpha_k p_k
92 new_potential_node.evaluate();
93
94 // r_{k+1} = r_k - alpha_k A p_k
95 edges.alpha.data = -edges.alpha.data;
96 new_residual_node.evaluate();
97
98 // z_{k+1} = M^-1 r_{k+1}
99 res_precond_node.evaluate();
100
101 // <r_{k+1}, z_{k+1}>
102 rz_hadamard_prod_node.evaluate();
103 rz_new_reduction_node.evaluate();
104
105 // <r_{k+1}, r_{k+1}>
106 res_ddot_new_node.evaluate();
107
108 // beta_k = <r_{k+1}, z_{k+1}> / <r_k, z_k>
109 edges.beta.data = edges.rz_new_values.data / edges.rz_old_values.data;
110 edges.old_values.data = edges.new_values.data;
111 edges.rz_old_values.data = edges.rz_new_values.data;
112
113 shamlog_debug_ln(
114 "Ramses::CG",
115 sham::format(
116 "k = {}, <r,r> = {}, <r,z> = {}",
117 k,
118 edges.old_values.data,
119 edges.rz_old_values.data));
120
121 // p_{k+1} = z_{k+1} + beta_k p_k
122 new_p_node.evaluate();
123
124 if ((edges.old_values.data / edges.rhs_norm_values.data) < tol * tol) {
125 converged = true;
126 break;
127 }
128 }
129
130 // exchange the ghost blocks of phi, needed to compute the gravitational acceleration
131 node_gz_phi.evaluate();
132 node_exch_gz_phi.evaluate();
133 node_replace_gz_phi.evaluate();
134
135 last_iteration_count = k;
136
137 if (shamcomm::world_rank() == 0) {
138 Tscal rel_res = std::sqrt(edges.old_values.data / edges.rhs_norm_values.data);
139 if (converged) {
141 "Ramses::CG",
142 sham::format("converged after {} iterations, |r|/|b| = {}", k, rel_res));
143 } else {
145 "Ramses::CG",
146 sham::format(
147 "not converged after {} iterations, |r|/|b| = {} (tol = {})",
148 k,
149 rel_res,
150 tol));
151 }
152 }
153 }
154
155 template<class Tvec, class TgridVec>
157 std::string tex = R"tex(
158 Jacobi preconditioned conjugate gradient solve of the Poisson equation
159 \begin{equation}
160 -\Delta x^3 \Delta {spans_phi} = -4 \pi G ({spans_rho} - {mean_rho}) \Delta x^3
161 \end{equation}
162 )tex";
163
164 replace_edges_tex_symbols(tex);
165
166 return tex;
167 }
168
169} // namespace shammodels::basegodunov::modules
170
Jacobi preconditioned conjugate gradient solver of the Poisson equation for the self-gravity.
std::uint32_t u32
32 bit unsigned integer
Solve with the Jacobi preconditioned conjugate gradient method, starting from the current content of...
virtual std::string _impl_get_tex() const
get the tex of the node
void _impl_evaluate_internal()
evaluate the node
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
Definition worldInfo.cpp:41
namespace for the basegodunov model modules
void info_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:132
void warn_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:132
This file contains the definition for the stacktrace related functionality.
shambase::details::BasicStackEntry StackEntry
Alias for shambase::details::BasicStackEntry.
Functions related to the MPI communicator.