Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
AnalysisSodTube.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
18template<class Tvec, class TgridVec>
20
21 auto get_gamma = [&]() -> Tscal {
22 return solver_config.eos_gamma;
23 return {};
24 };
25
26 Tscal gamma = get_gamma();
27
28 auto &sched = scheduler();
29
30 using namespace shamrock::patch;
31 using namespace shamrock;
32 using namespace shammath;
33
34 // load layout info
35 PatchDataLayerLayout &pdl = scheduler().pdl_old();
36
37 const u32 icell_min = pdl.get_field_idx<TgridVec>("cell_min");
38 const u32 icell_max = pdl.get_field_idx<TgridVec>("cell_max");
39 const u32 irho = pdl.get_field_idx<Tscal>("rho");
40 const u32 ieint = pdl.get_field_idx<Tscal>("eint");
41 const u32 ivel = pdl.get_field_idx<Tvec>("vel");
42 Tscal sum_L2_rho = 0;
43 Tvec sum_L2_v = {0, 0, 0};
44 Tscal sum_L2_P = 0;
45 Tscal N = 0;
46
47 Tscal one_over_Nside = 1. / AMRBlock::Nside;
48
49 Tscal dxfact = solver_config.grid_coord_to_pos_fact;
50
51 scheduler().for_each_patchdata_nonempty(
53 u32 cell_count = pdat.get_obj_cnt() * AMRBlock::block_size;
54
55 sham::DeviceBuffer<TgridVec> &buf_block_min = pdat.get_field_buf_ref<TgridVec>(0);
56 sham::DeviceBuffer<TgridVec> &buf_block_max = pdat.get_field_buf_ref<TgridVec>(1);
57 sham::DeviceBuffer<Tscal> &buf_rho = pdat.get_field_buf_ref<Tscal>(irho);
58 sham::DeviceBuffer<Tvec> &buf_vel = pdat.get_field_buf_ref<Tvec>(ivel);
59 sham::DeviceBuffer<Tscal> &buf_eint = pdat.get_field_buf_ref<Tscal>(ieint);
60
61 {
62 auto acc_block_min = buf_block_min.copy_to_stdvec();
63 auto acc_block_max = buf_block_max.copy_to_stdvec();
64 auto acc_rho = buf_rho.copy_to_stdvec();
65 auto acc_vel = buf_vel.copy_to_stdvec();
66 auto acc_eint = buf_eint.copy_to_stdvec();
67
68 for (u32 i = 0; i < cell_count; i++) {
69 const u32 block_id = i / AMRBlock::block_size;
70 const u32 cell_loc_id = i % AMRBlock::block_size;
71
72 TgridVec lower = acc_block_min[block_id];
73 TgridVec upper = acc_block_max[block_id];
74
75 Tvec lower_flt = lower.template convert<Tscal>() * dxfact;
76 Tvec upper_flt = upper.template convert<Tscal>() * dxfact;
77
78 Tvec block_cell_size = (upper_flt - lower_flt) * one_over_Nside;
79
80 Tvec xyz = lower_flt + (block_cell_size / 2);
81 Tscal rho = acc_rho[i];
82 Tvec v = acc_vel[i];
83 Tscal eint = acc_eint[i];
84
85 Tscal P = eint * (gamma - 1);
86
87 Tscal x = sham::dot(xyz, direction) - x_ref;
88
89 if ((x + x_ref) > x_min && (x + x_ref) < x_max) {
90
91 auto result_sod = solution.get_value(time_val, x);
92
93 Tscal d_rho = rho - result_sod.rho;
94 Tscal d_P = P - result_sod.P;
95 Tvec d_vxyz = v - result_sod.vx * direction;
96
97 sum_L2_rho += d_rho * d_rho;
98 sum_L2_P += d_P * d_P;
99 sum_L2_v += d_vxyz * d_vxyz;
100 N += 1;
101 }
102 }
103 }
104 });
105
106 Tscal tot_N = shamalgs::collective::allreduce_sum(N);
107
108 if (tot_N == 0) {
109 shambase::throw_with_loc<std::runtime_error>("no particle in wanted region");
110 }
111
112 Tscal mpi_sum_L2_P = shamalgs::collective::allreduce_sum(sum_L2_P) / tot_N;
113 Tscal mpi_sum_L2_rho = shamalgs::collective::allreduce_sum(sum_L2_rho) / tot_N;
114 Tvec mpi_sum_L2_v = shamalgs::collective::allreduce_sum(sum_L2_v) / tot_N;
115
116 return field_val{mpi_sum_L2_rho, mpi_sum_L2_v, mpi_sum_L2_P};
117}
118
constexpr const char * xyz
Position field (3D coordinates)
std::uint32_t u32
32 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory)
std::vector< T > copy_to_stdvec() const
Copy the content of the buffer to a std::vector.
u32 get_field_idx(const std::string &field_name) const
Get the field id if matching name & type.
PatchDataLayer container class, the layout is described in patchdata_layout.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
namespace for math utility
Definition AABB.hpp:26
namespace for the main framework
Definition __init__.py:1
Patch object that contain generic patch information.
Definition Patch.hpp:33