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
16
18#include "shammath/riemann.hpp"
21#include "shamphys/eos.hpp"
24
25template<class Tvec, class TgridVec>
26auto shammodels::basegodunov::modules::AnalysisSodTube<Tvec, TgridVec>::compute_L2_dist()
27 -> field_val {
28
29 auto get_gamma = [&]() -> Tscal {
30 return solver_config.get_eos_gamma();
31 return {};
32 };
33
34 Tscal gamma = get_gamma();
35
36 auto &sched = scheduler();
37
38 using namespace shamrock::patch;
39 using namespace shamrock;
40 using namespace shammath;
41
42 // load layout info
43 PatchDataLayerLayout &pdl = scheduler().pdl_old();
44
45 const u32 icell_min = pdl.get_field_idx<TgridVec>("cell_min");
46 const u32 icell_max = pdl.get_field_idx<TgridVec>("cell_max");
47 const u32 irho = pdl.get_field_idx<Tscal>("rho");
48 const u32 irhoetot = pdl.get_field_idx<Tscal>("rhoetot");
49 const u32 irhovel = pdl.get_field_idx<Tvec>("rhovel");
50
51 Tscal sum_L2_rho = 0;
52 Tvec sum_L2_v = {0, 0, 0};
53 Tscal sum_L2_P = 0;
54 Tscal N = 0;
55
56 Tscal one_over_Nside = 1. / AMRBlock::Nside;
57
58 Tscal dxfact = solver_config.grid_coord_to_pos_fact;
59
60 scheduler().for_each_patchdata_nonempty(
62 u32 cell_count = pdat.get_obj_cnt() * AMRBlock::block_size;
63
64 sham::DeviceBuffer<TgridVec> &buf_block_min = pdat.get_field_buf_ref<TgridVec>(0);
65 sham::DeviceBuffer<TgridVec> &buf_block_max = pdat.get_field_buf_ref<TgridVec>(1);
66 sham::DeviceBuffer<Tscal> &buf_rho = pdat.get_field_buf_ref<Tscal>(irho);
67 sham::DeviceBuffer<Tvec> &buf_rhov = pdat.get_field_buf_ref<Tvec>(irhovel);
68 sham::DeviceBuffer<Tscal> &buf_rhoe = pdat.get_field_buf_ref<Tscal>(irhoetot);
69
70 {
71 auto acc_block_min = buf_block_min.copy_to_stdvec();
72 auto acc_block_max = buf_block_max.copy_to_stdvec();
73 auto acc_rho = buf_rho.copy_to_stdvec();
74 auto acc_rhov = buf_rhov.copy_to_stdvec();
75 auto acc_rhoe = buf_rhoe.copy_to_stdvec();
76
77 for (u32 i = 0; i < cell_count; i++) {
78 const u32 block_id = i / AMRBlock::block_size;
79 const u32 cell_loc_id = i % AMRBlock::block_size;
80
81 TgridVec lower = acc_block_min[block_id];
82 TgridVec upper = acc_block_max[block_id];
83
84 Tvec lower_flt = lower.template convert<Tscal>() * dxfact;
85
86 // size from the int coords difference, so that it does not depend on the block
87 // position
88 Tvec block_cell_size
89 = (upper - lower).template convert<Tscal>() * (dxfact * one_over_Nside);
90
91 Tvec xyz = lower_flt + (block_cell_size / 2);
92 Tscal rho = acc_rho[i];
93 Tvec rhov = acc_rhov[i];
94 Tscal rhoe = acc_rhoe[i];
95
96 auto conststate = shammath::ConsState<Tvec>{rho, rhoe, rhov};
97 shammath::FluidStateAdiabatic<Tvec> adiab_fluid{.m_gamma = gamma};
98 auto prim_state = adiab_fluid.cons_to_prim(conststate);
99
100 Tscal P = prim_state.press;
101 Tvec v = prim_state.vel;
102
103 Tscal x = sham::dot(xyz, direction) - x_ref;
104
105 if ((x + x_ref) > x_min && (x + x_ref) < x_max) {
106
107 auto result_sod = solution.get_value(time_val, x);
108
109 Tscal d_rho = rho - result_sod.rho;
110 Tscal d_P = P - result_sod.P;
111 Tvec d_vxyz = v - result_sod.vx * direction;
112
113 sum_L2_rho += d_rho * d_rho;
114 sum_L2_P += d_P * d_P;
115 sum_L2_v += d_vxyz * d_vxyz;
116 N += 1;
117 }
118 }
119 }
120 });
121
122 Tscal tot_N = shamalgs::collective::allreduce_sum(N);
123
124 if (tot_N == 0) {
125 shambase::throw_with_loc<std::runtime_error>("no particle in wanted region");
126 }
127
128 Tscal mpi_sum_L2_P = shamalgs::collective::allreduce_sum(sum_L2_P) / tot_N;
129 Tscal mpi_sum_L2_rho = shamalgs::collective::allreduce_sum(sum_L2_rho) / tot_N;
130 Tvec mpi_sum_L2_v = shamalgs::collective::allreduce_sum(sum_L2_v) / tot_N;
131
132 return field_val{mpi_sum_L2_rho, mpi_sum_L2_v, mpi_sum_L2_P};
133}
134
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.
u32 get_obj_cnt() const
get the number of objects (particles) stored in this layer
This header file contains utility functions related to exception handling in the code.
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
Umbrella header pulling in the gas states and all gas Riemann solvers (Rusanov, HLL,...
sph kernels
FluidStateSpec implementation for an ideal (adiabatic) gas equation of state.
Patch object that contain generic patch information.
Definition Patch.hpp:33