40 auto edges = get_edges();
42 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
45 Tscal gpart_mass = edges.gpart_mass.data;
46 Tscal dt = edges.dt.data;
50 bool had_accretion =
false;
51 std::string log =
"sink accretion :";
53 auto &sink_positions = edges.sink_positions.data;
54 auto &sink_velocities = edges.sink_velocities.data;
55 auto &sink_accelerations = edges.sink_accelerations.data;
56 auto &sink_angmom = edges.sink_angmom.data;
57 auto &sink_mass = edges.sink_mass.data;
59 u32 sink_count = shambase::narrow_or_throw<u32>(sink_positions.size());
63 bool track_dust = ndust > 0 && edges.sink_accreted_dust_mass.has_value();
64 if (track_dust && !(edges.s_j.has_value() && edges.hpart.has_value())) {
66 "accreted dust mass tracking requires s_j and hpart");
71 edges.sink_accreted_dust_mass.value().get().data, sink_count, ndust);
74 for (
u32 i_sink = 0; i_sink < sink_count; i_sink++) {
76 Tvec r_sink = sink_positions[i_sink];
77 Tvec v_sink = sink_velocities[i_sink];
81 Tvec s_acc_mxyz = {0, 0, 0};
82 Tvec s_acc_pxyz = {0, 0, 0};
83 Tvec s_acc_maxyz = {0, 0, 0};
84 Tvec s_acc_lxyz = {0, 0, 0};
85 std::vector<Tscal> s_acc_dust_mass(track_dust ? ndust : 0, 0);
87 edges.part_counts.indexes.for_each([&](
u64 id_patch,
u32 Nobj) {
90 auto &acc_table = edges.sink_accretion_table.get_spans().get(id_patch);
97 [i_sink](
u32 id_a,
const u32 *__restrict acc_table,
u32 *__restrict acc_flag) {
98 acc_flag[id_a] = (acc_table[id_a] == i_sink) ? 1 : 0;
103 auto &pos_data = edges.positions.get_spans().get(id_patch);
104 auto &vel_data = edges.velocities.get_spans().get(id_patch);
105 auto &acc_data = edges.accelerations.get_spans().get(id_patch);
108 if (id_list_accrete.get_size() > 0) {
109 u32 Naccrete = shambase::narrow_or_throw<u32>(id_list_accrete.get_size());
111 Tscal acc_mass = gpart_mass * Naccrete;
123 [r_sink, v_sink, gpart_mass, dt](
125 const Tvec *__restrict xyz,
126 const Tvec *__restrict vxyz,
127 const Tvec *__restrict axyz,
128 const u32 *__restrict id_acc,
129 Tvec *__restrict accretion_p,
130 Tvec *__restrict accretion_mr,
131 Tvec *__restrict accretion_ma,
132 Tvec *__restrict accretion_l) {
133 u32 i_a = id_acc[id_a];
137 accretion_p[id_a] = gpart_mass * v;
138 accretion_mr[id_a] = gpart_mass * r;
139 accretion_ma[id_a] = gpart_mass * a;
145 accretion_l[id_a] = gpart_mass * sycl::cross(r - r_sink, v - v_sink);
153 s_acc_mass += acc_mass;
154 s_acc_pxyz += acc_pxyz;
155 s_acc_mxyz += acc_mxyz;
156 s_acc_maxyz += acc_maxyz;
157 s_acc_lxyz += acc_lxyz;
164 auto &hpart_data = edges.hpart.value().get().get_spans().get(id_patch);
165 auto &s_j_data = edges.s_j.value().get().get_spans().get(id_patch);
172 [gpart_mass, hfactd = this->hfactd, ndust = this->ndust, Naccrete](
174 const Tscal *__restrict hpart,
175 const Tscal *__restrict s_j,
176 const u32 *__restrict id_acc,
177 Tscal *__restrict dust_mass) {
178 u32 i_a = id_acc[id_a];
179 Tscal rho_a = shamrock::sph::rho_h(gpart_mass, hpart[i_a], hfactd);
180 for (
u32 j = 0; j < ndust; j++) {
181 Tscal sj = s_j[i_a * ndust + j];
182 dust_mass[j * Naccrete + id_a] = gpart_mass * sj * sj / rho_a;
186 for (
u32 j = 0; j < ndust; j++) {
188 dev_sched, dust_mass_acc, j * Naccrete, (j + 1) * Naccrete);
194 Tscal sum_acc_mass = shamalgs::collective::allreduce_sum(s_acc_mass);
197 if (sum_acc_mass <= 0) {
201 Tvec sum_acc_pxyz = shamalgs::collective::allreduce_sum(s_acc_pxyz);
202 Tvec sum_acc_mxyz = shamalgs::collective::allreduce_sum(s_acc_mxyz);
203 Tvec sum_acc_maxyz = shamalgs::collective::allreduce_sum(s_acc_maxyz);
204 Tvec sum_acc_lxyz = shamalgs::collective::allreduce_sum(s_acc_lxyz);
206 std::vector<Tscal> sum_acc_dust_mass(s_acc_dust_mass.size());
207 for (
u32 j = 0; j < s_acc_dust_mass.size(); j++) {
208 sum_acc_dust_mass[j] = shamalgs::collective::allreduce_sum(s_acc_dust_mass[j]);
211 Tscal old_mass = sink_mass[i_sink];
212 Tvec old_pos = sink_positions[i_sink];
213 Tvec old_vel = sink_velocities[i_sink];
214 Tvec old_acc = sink_accelerations[i_sink];
215 Tvec old_ang = sink_angmom[i_sink];
218 Tscal new_mass = old_mass + sum_acc_mass;
219 Tvec new_pos = (sum_acc_mxyz + old_pos * old_mass) / (old_mass + sum_acc_mass);
220 Tvec new_vel = (sum_acc_pxyz + old_vel * old_mass) / (old_mass + sum_acc_mass);
221 Tvec new_acc = (sum_acc_maxyz + old_acc * old_mass) / (old_mass + sum_acc_mass);
222 Tvec new_ang_mom = old_ang + sum_acc_lxyz
223 - new_mass * sycl::cross(new_pos - old_pos, new_vel - old_vel);
226 sink_mass[i_sink] = new_mass;
227 sink_positions[i_sink] = new_pos;
228 sink_velocities[i_sink] = new_vel;
229 sink_angmom[i_sink] = new_ang_mom;
230 sink_accelerations[i_sink] = new_acc;
233 auto &sink_dust_mass = edges.sink_accreted_dust_mass.value().get().data;
234 for (
u32 j = 0; j < ndust; j++) {
235 sink_dust_mass[i_sink * ndust + j] += sum_acc_dust_mass[j];
239 had_accretion =
true;
241 "\n id {} deltas : mass={} r={} v={} l={}",
246 new_ang_mom - old_ang);
248 log += sham::format(
" dust={}", sum_acc_dust_mass);