Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
modifiers.hpp
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
10#pragma once
11
22#include <stdexcept>
23#include <tuple>
24#include <vector>
25
26//%Impl status : Good
27
28namespace generic::setup::modifiers {
29
30 template<class T, class vec>
31 inline void set_value_in_box(
32 PatchScheduler &sched, T val, std::string name, std::tuple<vec, vec> box) {
33 StackEntry stack_loc{};
34 sched.patch_data.for_each_patchdata(
35 [&](u64 patch_id, shamrock::patch::PatchDataLayer &pdat) {
37 = pdat.template get_field<vec>(sched.pdl_old().get_field_idx<vec>("xyz"));
38
40 = pdat.template get_field<T>(sched.pdl_old().get_field_idx<T>(name));
41
42 {
43 auto &buf = f.get_buf();
44 sycl::host_accessor acc{*buf};
45
46 auto &buf_xyz = xyz.get_buf();
47 sycl::host_accessor acc_xyz{*buf_xyz};
48
49 for (u32 i = 0; i < f.size(); i++) {
50 vec r = acc_xyz[i];
51
52 if (BBAA::is_coord_in_range(r, std::get<0>(box), std::get<1>(box))) {
53 acc[i] = val;
54 }
55 }
56 }
57 });
58 }
59
60 template<class T, class vec>
61 inline void set_value_in_sphere(
62 PatchScheduler &sched,
63 T val,
64 std::string name,
65 vec center,
66 shambase::VecComponent<vec> radius) {
67
68 using flt = shambase::VecComponent<vec>;
69
70 StackEntry stack_loc{};
71 sched.patch_data.for_each_patchdata(
72 [&](u64 patch_id, shamrock::patch::PatchDataLayer &pdat) {
74 = pdat.template get_field<vec>(sched.pdl_old().get_field_idx<vec>("xyz"));
75
77 = pdat.template get_field<T>(sched.pdl_old().get_field_idx<T>(name));
78
79 flt r2 = radius * radius;
80 {
81 auto &buf = f.get_buf();
82 sycl::host_accessor acc{*buf};
83
84 auto &buf_xyz = xyz.get_buf();
85 sycl::host_accessor acc_xyz{*buf_xyz};
86
87 for (u32 i = 0; i < f.size(); i++) {
88 vec dr = acc_xyz[i] - center;
89
90 if (sycl::dot(dr, dr) < r2) {
91 acc[i] = val;
92 }
93 }
94 }
95 });
96 }
97
98 template<class flt>
99 inline void pertub_eigenmode_wave(
100 PatchScheduler &sched, std::tuple<flt, flt> ampls, sycl::vec<flt, 3> k, flt phase) {
101
102 using vec = sycl::vec<flt, 3>;
103
104 if (std::get<0>(ampls) != 0) {
106 "density perturbation not implemented");
107 }
108
109 sched.patch_data.for_each_patchdata(
110 [&](u64 patch_id, shamrock::patch::PatchDataLayer &pdat) {
112 = pdat.template get_field<vec>(sched.pdl_old().get_field_idx<vec>("xyz"));
114 = pdat.template get_field<vec>(sched.pdl_old().get_field_idx<vec>("vxyz"));
115
116 flt ampl = std::get<1>(ampls);
117
118 {
119
120 u32 cnt = pdat.get_obj_cnt();
121
122 auto &buf_xyz = xyz.get_buf();
123 auto acc_xyz = buf_xyz.copy_to_stdvec();
124
125 auto &buf_vxyz = vxyz.get_buf();
126 auto acc_vxyz = buf_vxyz.copy_to_stdvec();
127
128 for (u32 i = 0; i < cnt; i++) {
129 vec r = acc_xyz[i];
130 flt rkphi = sycl::dot(r, k) + phase;
131 acc_vxyz[i] = ampl * sycl::sin(rkphi);
132 }
133
134 buf_xyz.copy_from_stdvec(acc_xyz);
135 buf_vxyz.copy_from_stdvec(acc_vxyz);
136 }
137 });
138 }
139
140 template<class T>
141 inline T get_sum(PatchScheduler &sched, std::string name) {
142
144
145 StackEntry stack_loc{};
146 sched.patch_data.for_each_patchdata(
147 [&](u64 patch_id, shamrock::patch::PatchDataLayer &pdat) {
149 = pdat.template get_field<T>(sched.pdl_old().get_field_idx<T>(name));
150
151 sum += xyz.compute_sum();
152 });
153
154 return shamalgs::collective::allreduce_sum(sum);
155 }
156
157} // namespace generic::setup::modifiers
constexpr const char * vxyz
3-velocity field
constexpr const char * xyz
Position field (3D coordinates)
MPI scheduler.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
The MPI scheduler.
SchedulerPatchData patch_data
handle the data of the patches of the scheduler
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.
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.