280 bool has_b_field = solver_config.has_field_b_on_rho();
281 bool has_psi_field = solver_config.has_field_psi_on_ch();
282 bool has_epsilon_field = solver_config.dust_config.has_epsilon_field();
283 bool has_deltav_field = solver_config.dust_config.has_deltav_field();
284 bool has_s_j_field = solver_config.dust_config.has_s_j_field();
286 using namespace shamrock::solvergraph;
293 auto ensure_sync_edge
294 = [&](
const std::string &name,
const std::string &tex_symbol,
auto init_value) {
295 using T =
decltype(init_value);
296 if (!sync_data.has_edge(name)) {
299 edge->data = std::move(init_value);
303 ensure_sync_edge(
"time",
"t", Tscal{0});
304 ensure_sync_edge(
"dt",
"dt", Tscal{0});
305 ensure_sync_edge(
"cfl_multiplier",
"C_{\\rm CFL}", Tscal{1e-2});
310 if (solver_config.show_cfl_detail) {
311 auto ensure_cfl_detail_edge = [&](
const std::string &type,
const std::string &tex_type) {
314 "\\Delta t_{\\rm cfl, " + tex_type +
"}",
317 ensure_cfl_detail_edge(
"courant",
"courant");
318 ensure_cfl_detail_edge(
"force",
"force");
319 if (solver_config.do_nimhd()) {
320 ensure_cfl_detail_edge(
"NIMHD",
"NIMHD");
323 ensure_cfl_detail_edge(
"divB_cleaning",
"\\nabla \\cdot B\\ cleaning");
326 ensure_cfl_detail_edge(
"dust1_fluid",
"dust\\ 1\\ fluid");
327 ensure_cfl_detail_edge(
"dust_drift",
"dust\\ drift");
330 ensure_cfl_detail_edge(
"sink_sink",
"sink-sink");
333 ensure_sync_edge(
"sink_pos",
"\\bf{r}_{\\mathrm{sink}}", std::vector<Tvec>{});
334 ensure_sync_edge(
"sink_vel",
"\\bf{v}_{\\mathrm{sink}}", std::vector<Tvec>{});
335 ensure_sync_edge(
"sink_acc_sph",
"\\bf{a}_{\\mathrm{sink, sph}}", std::vector<Tvec>{});
336 ensure_sync_edge(
"sink_acc_ext",
"\\bf{a}_{\\mathrm{sink, ext}}", std::vector<Tvec>{});
337 ensure_sync_edge(
"sink_mass",
"m_{\\mathrm{sink}}", std::vector<Tscal>{});
338 ensure_sync_edge(
"sink_angular_momentum",
"\\bf{L}_{\\mathrm{sink}}", std::vector<Tvec>{});
339 ensure_sync_edge(
"sink_accretion_radius",
"r_{\\mathrm{accretion}}", std::vector<Tscal>{});
343 "sink_accreted_dust_mass",
"m_{\\mathrm{sink, acc}, j}", std::vector<Tscal>{});
374 if (has_epsilon_field) {
378 if (has_deltav_field) {
386 u32 ndust = solver_config.dust_config.get_dust_nvar();
393 gpart_mass.data = solver_config.gpart_mass;
403 std::vector<std::shared_ptr<shamrock::solvergraph::INode>> attach_field_sequence;
407 "set_scheduler_patchdata",
410 scheduler().for_each_patchdata_nonempty(
418 attach_field_sequence.push_back(set_scheduler_patchdata);
422 auto attach_part_counts
428 attach_field_sequence.push_back(attach_part_counts);
438 attach_field_sequence.push_back(attach_xyz);
448 attach_field_sequence.push_back(attach_vxyz);
458 attach_field_sequence.push_back(attach_axyz);
468 attach_field_sequence.push_back(attach_uint);
478 attach_field_sequence.push_back(attach_duint);
488 attach_field_sequence.push_back(attach_hpart);
498 attach_field_sequence.push_back(attach_B_on_rho);
508 attach_field_sequence.push_back(attach_dB_on_rho);
518 attach_field_sequence.push_back(attach_psi_on_ch);
528 attach_field_sequence.push_back(attach_dpsi_on_ch);
531 if (has_epsilon_field) {
538 attach_field_sequence.push_back(attach_epsilon);
541 if (has_epsilon_field) {
548 attach_field_sequence.push_back(attach_dtepsilon);
551 if (has_deltav_field) {
558 attach_field_sequence.push_back(attach_deltav);
561 if (has_deltav_field) {
568 attach_field_sequence.push_back(attach_dtdeltav);
578 attach_field_sequence.push_back(attach_s_j);
588 attach_field_sequence.push_back(attach_ds_j_dt);
591 "attach fields to scheduler",
601 auto make_half_step_sequence = [&](std::string prefix) {
602 std::vector<std::shared_ptr<shamrock::solvergraph::INode>> half_step_sequence;
613 half_step_sequence.push_back(half_step_vxyz);
625 half_step_sequence.push_back(half_step_uint);
637 half_step_sequence.push_back(half_step_B_on_rho);
649 half_step_sequence.push_back(half_step_psi_on_ch);
652 if (has_epsilon_field) {
661 half_step_sequence.push_back(half_step_epsilon);
664 if (has_deltav_field) {
673 half_step_sequence.push_back(half_step_deltav);
677 u32 ndust = solver_config.dust_config.get_dust_nvar();
679 auto &cfg = solver_config.dust_config.get_monofluid_tva();
681 if (cfg.ensure_s_j_positivity) {
691 half_step_sequence.push_back(half_step_s_j);
701 half_step_sequence.push_back(half_step_s_j);
704 if (cfg.should_clamp_dust_density()) {
705 auto hfactd_edge = IDataEdge<Tscal>::make_shared(
"hfactd",
"hfactd");
706 hfactd_edge->data = Kernel::hfactd;
709 = IDataEdge<Tscal>::make_shared(
"clamp_frac",
"clamp_frac");
710 clamp_frac_edge->data = cfg.get_clamp_dust_frac();
712 auto half_step_s_j_density_clamp = solver_graph.
register_node(
713 prefix +
"_s_j_density_clamp",
723 half_step_sequence.push_back(half_step_s_j_density_clamp);
730 solver_graph.
register_node(
"half_step1", make_half_step_sequence(
"half_step1"));
731 solver_graph.
register_node(
"half_step2", make_half_step_sequence(
"half_step2"));
746 "leapfrog predictor",
748 "leapfrog predictor",
760 bool do_part_killing_step = solver_config.particle_killing.kill_list.size() > 0;
762 if (do_part_killing_step) {
770 std::vector<std::shared_ptr<shamrock::solvergraph::INode>> part_kill_sequence{};
774 auto empty_part_to_remove
777 part_kill_sequence.push_back(empty_part_to_remove);
780 using kill_t =
typename ParticleKillingConfig<Tvec>::kill_t;
784 for (kill_t &kill_obj : solver_config.particle_killing.kill_list) {
785 if (kill_sphere *kill_info = std::get_if<kill_sphere>(&kill_obj)) {
788 kill_info->center, kill_info->radius);
789 node_selector.set_edges(xyz_edge, part_to_remove);
791 part_kill_sequence.push_back(
792 std::make_shared<
decltype(node_selector)>(std::move(node_selector)));
798 node_killer.set_edges(part_to_remove, patchdatas);
800 part_kill_sequence.push_back(
801 std::make_shared<
decltype(node_killer)>(std::move(node_killer)));
806 part_kill_sequence.push_back(solver_graph.
get_node_ptr_base(
"attach fields to scheduler"));
818 half_dt.data = dt.data / 2;
827 = std::make_shared<shamrock::solvergraph::Indexes<u32>>(
"part_counts",
"N_{\\rm part}");
829 storage.part_counts_with_ghost = std::make_shared<shamrock::solvergraph::Indexes<u32>>(
830 "part_counts_with_ghost",
"N_{\\rm part, with ghost}");
832 storage.patch_rank_owner = std::make_shared<shamrock::solvergraph::RankGetter>(
833 [&](
u64 patch_id) ->
u32 {
834 return scheduler().get_patch_rank_owner(patch_id);
840 storage.positions_with_ghosts
841 = std::make_shared<shamrock::solvergraph::FieldRefs<Tvec>>(
"part_pos",
"\\mathbf{r}");
842 storage.hpart_with_ghosts
843 = std::make_shared<shamrock::solvergraph::FieldRefs<Tscal>>(
"h_part",
"h");
846 = std::make_shared<shammodels::sph::solvergraph::NeighCache>(
"neigh_cache",
"neigh");
848 storage.omega = std::make_shared<shamrock::solvergraph::Field<Tscal>>(1,
"omega",
"\\Omega");
851 = std::make_shared<shamrock::solvergraph::Field<Tvec>>(1,
"MagCurrentJ",
"\\mathbf{J}");
853 if (solver_config.has_field_alphaAV()) {
854 storage.alpha_av_updated = std::make_shared<shamrock::solvergraph::Field<Tscal>>(
855 1,
"alpha_av_updated",
"\\alpha_{\\rm AV}");
858 storage.pressure = std::make_shared<shamrock::solvergraph::Field<Tscal>>(1,
"pressure",
"P");
860 = std::make_shared<shamrock::solvergraph::Field<Tscal>>(1,
"soundspeed",
"c_s");
862 storage.exchange_gz_alpha
863 = std::make_shared<shamrock::solvergraph::ExchangeGhostField<Tscal>>();
864 storage.exchange_gz_node
865 = std::make_shared<shamrock::solvergraph::ExchangeGhostLayer>(storage.ghost_layout);
866 storage.exchange_gz_positions
867 = std::make_shared<shamrock::solvergraph::ExchangeGhostLayer>(storage.xyzh_ghost_layout);
869 storage.exchange_gz_J = std::make_shared<shamrock::solvergraph::ExchangeGhostField<Tvec>>();
897 u32 sink_acc_ndust = has_s_j_field ? solver_config.dust_config.get_dust_nvar() : 0;
899 std::optional<std::shared_ptr<FieldRefs<Tscal>>> sink_acc_hpart = std::nullopt;
900 std::optional<std::shared_ptr<FieldRefs<Tscal>>> sink_acc_s_j = std::nullopt;
902 std::vector<std::shared_ptr<shamrock::solvergraph::INode>> attach_sink_dust_fields{};
903 std::vector<std::shared_ptr<shamrock::solvergraph::INode>> free_sink_dust_fields{};
905 auto add_sink_dust_field = [&](
const std::string &name) {
907 attach_sink_dust_fields.push_back(solver_graph.
get_node_ptr_base(
"attach_" + name));
911 free_sink_dust_fields.push_back(free_node);
916 sink_acc_hpart = add_sink_dust_field(
"hpart");
917 sink_acc_s_j = add_sink_dust_field(
"s_j");
925 auto sink_accelerations
928 "sink_angular_momentum");
932 "sink_accretion_radius");
934 std::optional<std::shared_ptr<IDataEdgeSerializable<std::vector<Tscal>>>>
935 sink_accreted_dust_mass = std::nullopt;
936 if (sink_acc_ndust > 0) {
937 sink_accreted_dust_mass
939 "sink_accreted_dust_mass");
943 "sink_accretion_table",
Field<u32>(1,
"sink_accretion_table",
"\\mathrm{acc}"));
955 "accrete_quantities",
972 sink_accreted_dust_mass);
984 std::vector<std::shared_ptr<shamrock::solvergraph::INode>> accretion_sequence{
992 accretion_sequence.insert(
993 accretion_sequence.end(),
994 attach_sink_dust_fields.begin(),
995 attach_sink_dust_fields.end());
998 accretion_sequence.push_back(flag_node);
999 accretion_sequence.push_back(qty_node);
1000 accretion_sequence.push_back(evict_node);
1003 accretion_sequence.push_back(free_xyz);
1004 accretion_sequence.push_back(free_vxyz);
1005 accretion_sequence.push_back(free_axyz);
1006 accretion_sequence.insert(
1007 accretion_sequence.end(), free_sink_dust_fields.begin(), free_sink_dust_fields.end());
1010 "sink accretion body",
1015 "if_has_sinks",
OperationIf(
"if_has_sinks", accretion_body));
1036 "sink_ext_force_epsilon",
IDataEdge<Tscal>(
"epsilon_grav_sink",
"\\epsilon"));
1040 g_edge.data = solver_config.get_constant_G();
1046 "set_sink_ext_force_epsilon",
1048 epsilon_edge.data = 1e-9;
1054 "reset_sink_acc_ext",
1057 for (Tvec &a : acc_ext.data) {
1076 "sink_ext_force_body",
1078 "sink ext force body",
1089 "sink ext force",
OperationIf(
"sink ext force", ext_force_body));
1101 auto sink_predictor_dt_to_half_dt = solver_graph.
register_node(
1102 "sink_predictor_dt_to_half_dt",
1105 half_dt.data = dt.data / 2;
1112 auto sink_predictor_vel_update = solver_graph.
register_node(
1121 auto sink_predictor_pos_update = solver_graph.
register_node(
1130 "sink_predictor_body",
1132 "sink predictor body",
1137 sink_predictor_dt_to_half_dt,
1138 sink_predictor_vel_update,
1139 sink_predictor_pos_update,
1145 "sink predictor",
OperationIf(
"sink predictor", sink_predictor_body));
1154 auto sink_corrector_vel_update = solver_graph.
register_node(
1166 "sink corrector",
OperationIf(
"sink corrector", sink_corrector_vel_update));
1175 using EF_PointMass =
typename Config::ExtForceConfig::PointMass;
1176 using EF_PN_PW =
typename Config::ExtForceConfig::PN_PW;
1177 using EF_LenseThirring =
typename Config::ExtForceConfig::LenseThirring;
1181 auto collect_accretors = [&]() {
1182 std::vector<Tvec> positions{};
1183 std::vector<Tscal> radii{};
1184 for (
auto &var_force : solver_config.ext_force_config.ext_forces) {
1185 if (EF_PointMass *ext_force = std::get_if<EF_PointMass>(&var_force.val)) {
1186 positions.push_back(ext_force->central_pos);
1187 radii.push_back(ext_force->Racc);
1188 }
else if (EF_PN_PW *ext_force = std::get_if<EF_PN_PW>(&var_force.val)) {
1189 positions.push_back(ext_force->central_pos);
1190 radii.push_back(ext_force->Racc);
1192 EF_LenseThirring *ext_force = std::get_if<EF_LenseThirring>(&var_force.val)) {
1193 positions.push_back(ext_force->central_pos);
1194 radii.push_back(ext_force->Racc);
1197 return std::pair{std::move(positions), std::move(radii)};
1201 "ext_force_accretion_pos",
1202 IDataEdge<std::vector<Tvec>>(
"ext_force_accretion_pos",
"\\mathbf{r}_{\\rm acc, ext}"));
1204 "ext_force_accretion_racc",
1205 IDataEdge<std::vector<Tscal>>(
"ext_force_accretion_racc",
"R_{\\rm acc, ext}"));
1207 "ext_force_accretion_table",
1208 Field<u32>(1,
"ext_force_accretion_table",
"\\mathrm{acc}"));
1210 "has_ext_force_accretion",
1211 IDataEdge<bool>(
"has_ext_force_accretion",
"\\rm has\\_ext\\_force\\_accretion"));
1214 "set_ext_force_accretion_pos",
1216 [collect_accretors](
IDataEdge<std::vector<Tvec>> &accretion_pos) {
1217 accretion_pos.data = std::get<0>(collect_accretors());
1224 "set_ext_force_accretion_racc",
1226 [collect_accretors](
IDataEdge<std::vector<Tscal>> &accretion_racc) {
1227 accretion_racc.data = std::get<1>(collect_accretors());
1231 "ext_force_accretion_racc"));
1234 "set_has_ext_force_accretion",
1238 has_ext_force_accretion.data = !accretion_pos.data.empty();
1265 "ext_force_accretion_body",
1267 "ext force accretion",
1282 "if_has_ext_force_accretion",
1283 OperationIf(
"if_has_ext_force_accretion", accretion_body));
1289 "point mass accretion",
1291 "point mass accretion",
1301 std::vector<std::shared_ptr<shamrock::solvergraph::INode>> seq{};
1310 if (do_part_killing_step) {
1322 if (solver_config.self_grav_config.is_sg_on()) {
1326 auto constant_G = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
"",
"");
1330 constant_G.data = solver_config.get_constant_G();
1335 auto field_xyz = shamrock::solvergraph::FieldRefs<Tvec>::make_shared(
"",
"");
1340 scheduler().for_each_patchdata_nonempty(
1342 auto &field = pdat.get_field<Tvec>(ixyz);
1345 field_xyz_edge.set_refs(field_xyz_refs);
1351 auto field_axyz_ext = shamrock::solvergraph::FieldRefs<Tvec>::make_shared(
"",
"");
1357 scheduler().for_each_patchdata_nonempty(
1359 auto &field = pdat.get_field<Tvec>(iaxyz_ext);
1362 field_axyz_ext_edge.set_refs(field_axyz_ext_refs);
1364 set_field_axyz_ext.
set_edges(field_axyz_ext);
1366 auto sizes = shamrock::solvergraph::Indexes<u32>::make_shared(
"",
"");
1371 scheduler().for_each_patchdata_nonempty(
1378 auto gpart_mass = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
"",
"");
1382 gpart_mass.data = solver_config.gpart_mass;
1387 std::shared_ptr<shamrock::solvergraph::INode> sg_inode = build_self_gravity_node<Tvec>(
1388 solver_config.self_grav_config,
1396 "self gravity sequence",
1400 shambase::to_shared(std::move(set_gpart_mass)),
1401 shambase::to_shared(std::move(set_constant_G)),
1402 shambase::to_shared(std::move(set_field_xyz)),
1403 shambase::to_shared(std::move(set_field_axyz_ext)),
1404 shambase::to_shared(std::move(set_sizes)),
2305 timer_interf.
start();
2308 using namespace shamrock::patch;
2310 bool has_alphaAV_field = solver_config.has_field_alphaAV();
2311 bool has_soundspeed_field = solver_config.ghost_has_soundspeed();
2313 bool has_b_field = solver_config.has_field_b_on_rho();
2314 bool has_psi_field = solver_config.has_field_psi_on_ch();
2315 bool has_curl_b_field = solver_config.has_field_curl_b();
2316 bool has_epsilon_field = solver_config.dust_config.has_epsilon_field();
2317 bool has_deltav_field = solver_config.dust_config.has_deltav_field();
2318 bool has_s_j_field = solver_config.dust_config.has_s_j_field();
2328 const u32 ialpha_AV = (has_alphaAV_field) ? pdl.
get_field_idx<Tscal>(
"alpha_AV") : 0;
2329 const u32 isoundspeed = (has_soundspeed_field) ? pdl.
get_field_idx<Tscal>(
"soundspeed") : 0;
2331 const u32 iB_on_rho = (has_b_field) ? pdl.
get_field_idx<Tvec>(
"B/rho") : 0;
2332 const u32 idB_on_rho = (has_b_field) ? pdl.
get_field_idx<Tvec>(
"dB/rho") : 0;
2333 const u32 ipsi_on_ch = (has_psi_field) ? pdl.
get_field_idx<Tscal>(
"psi/ch") : 0;
2334 const u32 idpsi_on_ch = (has_psi_field) ? pdl.
get_field_idx<Tscal>(
"dpsi/ch") : 0;
2335 const u32 icurlB = (has_curl_b_field) ? pdl.
get_field_idx<Tvec>(
"curlB") : 0;
2337 bool do_MHD_debug = solver_config.do_MHD_debug();
2338 const u32 imag_pressure = (do_MHD_debug) ? pdl.
get_field_idx<Tvec>(
"mag_pressure") : -1;
2339 const u32 imag_tension = (do_MHD_debug) ? pdl.
get_field_idx<Tvec>(
"mag_tension") : -1;
2340 const u32 igas_pressure = (do_MHD_debug) ? pdl.
get_field_idx<Tvec>(
"gas_pressure") : -1;
2341 const u32 itensile_corr = (do_MHD_debug) ? pdl.
get_field_idx<Tvec>(
"tensile_corr") : -1;
2342 const u32 ipsi_propag = (do_MHD_debug) ? pdl.
get_field_idx<Tscal>(
"psi_propag") : -1;
2343 const u32 ipsi_diff = (do_MHD_debug) ? pdl.
get_field_idx<Tscal>(
"psi_diff") : -1;
2344 const u32 ipsi_cons = (do_MHD_debug) ? pdl.
get_field_idx<Tscal>(
"psi_cons") : -1;
2345 const u32 iu_mhd = (do_MHD_debug) ? pdl.
get_field_idx<Tscal>(
"u_mhd") : -1;
2347 const u32 iepsilon = (has_epsilon_field) ? pdl.
get_field_idx<Tscal>(
"epsilon") : 0;
2348 const u32 ideltav = (has_deltav_field) ? pdl.
get_field_idx<Tvec>(
"deltav") : 0;
2351 auto &ghost_layout_ptr = storage.ghost_layout;
2358 const u32 iaxyz_interf
2359 = (solver_config.has_axyz_in_ghost()) ? ghost_layout.
get_field_idx<Tvec>(
"axyz") : 0;
2361 const u32 isoundspeed_interf
2362 = (has_soundspeed_field) ? ghost_layout.
get_field_idx<Tscal>(
"soundspeed") : 0;
2364 const u32 iB_interf = (has_b_field) ? ghost_layout.
get_field_idx<Tvec>(
"B/rho") : 0;
2365 const u32 ipsi_interf = (has_psi_field) ? ghost_layout.
get_field_idx<Tscal>(
"psi/ch") : 0;
2366 const u32 icurlB_interf = (has_curl_b_field) ? ghost_layout.
get_field_idx<Tvec>(
"curlB") : 0;
2368 const u32 iepsilon_interf
2369 = (has_epsilon_field) ? ghost_layout.
get_field_idx<Tscal>(
"epsilon") : 0;
2370 const u32 ideltav_interf = (has_deltav_field) ? ghost_layout.
get_field_idx<Tvec>(
"deltav") : 0;
2371 const u32 is_j_interf = (has_s_j_field) ? ghost_layout.
get_field_idx<Tscal>(
"s_j") : 0;
2378 auto pdat_interf = ghost_handle.template build_interface_native<PatchDataLayer>(
2379 storage.ghost_patch_cache.get(),
2381 PatchDataLayer pdat(ghost_layout_ptr);
2388 ghost_handle.template modify_interface_native<PatchDataLayer>(
2389 storage.ghost_patch_cache.get(),
2393 InterfaceBuildInfos binfo,
2397 PatchDataLayer &sender_patch = scheduler().patch_data.get_pdat(sender);
2398 PatchDataField<Tscal> &sender_omega = omega.get(sender);
2400 sender_patch.get_field<Tscal>(ihpart).append_subset_to(
2401 buf_idx, cnt, pdat.get_field<Tscal>(ihpart_interf));
2402 sender_patch.get_field<Tscal>(iuint).append_subset_to(
2403 buf_idx, cnt, pdat.get_field<Tscal>(iuint_interf));
2405 if (solver_config.has_axyz_in_ghost()) {
2406 sender_patch.get_field<Tvec>(iaxyz).append_subset_to(
2407 buf_idx, cnt, pdat.get_field<Tvec>(iaxyz_interf));
2410 sender_patch.get_field<Tvec>(ivxyz).append_subset_to(
2411 buf_idx, cnt, pdat.get_field<Tvec>(ivxyz_interf));
2413 sender_omega.append_subset_to(buf_idx, cnt, pdat.get_field<Tscal>(iomega_interf));
2415 if (has_soundspeed_field) {
2416 sender_patch.get_field<Tscal>(isoundspeed)
2417 .append_subset_to(buf_idx, cnt, pdat.get_field<Tscal>(isoundspeed_interf));
2421 sender_patch.get_field<Tvec>(iB_on_rho).append_subset_to(
2422 buf_idx, cnt, pdat.get_field<Tvec>(iB_interf));
2425 if (has_psi_field) {
2426 sender_patch.get_field<Tscal>(ipsi_on_ch)
2427 .append_subset_to(buf_idx, cnt, pdat.get_field<Tscal>(ipsi_interf));
2430 if (has_curl_b_field) {
2431 sender_patch.get_field<Tvec>(icurlB).append_subset_to(
2432 buf_idx, cnt, pdat.get_field<Tvec>(icurlB_interf));
2435 if (has_epsilon_field) {
2436 sender_patch.get_field<Tscal>(iepsilon).append_subset_to(
2437 buf_idx, cnt, pdat.get_field<Tscal>(iepsilon_interf));
2440 if (has_deltav_field) {
2441 sender_patch.get_field<Tvec>(ideltav).append_subset_to(
2442 buf_idx, cnt, pdat.get_field<Tvec>(ideltav_interf));
2445 if (has_s_j_field) {
2446 sender_patch.get_field<Tscal>(is_j).append_subset_to(
2447 buf_idx, cnt, pdat.get_field<Tscal>(is_j_interf));
2451 ghost_handle.template modify_interface_native<PatchDataLayer>(
2452 storage.ghost_patch_cache.get(),
2456 InterfaceBuildInfos binfo,
2460 if (sycl::length(binfo.offset_speed) > 0) {
2461 pdat.get_field<Tvec>(ivxyz_interf).apply_offset(binfo.offset_speed);
2467 std::move(pdat_interf),
2468 storage.exchange_gz_node,
2469 solver_config.show_ghost_zone_graph);
2471 std::map<u64, u64> sz_interf_map;
2476 storage.merged_patchdata_ghost.set(
2477 ghost_handle.template merge_native<PatchDataLayer, PatchDataLayer>(
2478 std::move(interf_pdat),
2480 PatchDataLayer pdat_new(ghost_layout_ptr);
2482 u32 or_elem = pdat.get_obj_cnt();
2483 pdat_new.reserve(or_elem + sz_interf_map[p.id_patch]);
2484 u32 total_elements = or_elem;
2486 PatchDataField<Tscal> &cur_omega = omega.get(p.id_patch);
2488 pdat_new.get_field<Tscal>(ihpart_interf).insert(pdat.get_field<Tscal>(ihpart));
2489 pdat_new.get_field<Tscal>(iuint_interf).insert(pdat.get_field<Tscal>(iuint));
2490 pdat_new.get_field<Tvec>(ivxyz_interf).insert(pdat.get_field<Tvec>(ivxyz));
2492 if (solver_config.has_axyz_in_ghost()) {
2493 pdat_new.get_field<Tvec>(iaxyz_interf).insert(pdat.get_field<Tvec>(iaxyz));
2496 pdat_new.get_field<Tscal>(iomega_interf).insert(cur_omega);
2498 if (has_soundspeed_field) {
2499 pdat_new.get_field<Tscal>(isoundspeed_interf)
2500 .insert(pdat.get_field<Tscal>(isoundspeed));
2504 pdat_new.get_field<Tvec>(iB_interf).insert(pdat.get_field<Tvec>(iB_on_rho));
2507 if (has_psi_field) {
2508 pdat_new.get_field<Tscal>(ipsi_interf)
2509 .insert(pdat.get_field<Tscal>(ipsi_on_ch));
2512 if (has_curl_b_field) {
2513 pdat_new.get_field<Tvec>(icurlB_interf).insert(pdat.get_field<Tvec>(icurlB));
2516 if (has_epsilon_field) {
2517 pdat_new.get_field<Tscal>(iepsilon_interf)
2518 .insert(pdat.get_field<Tscal>(iepsilon));
2521 if (has_deltav_field) {
2522 pdat_new.get_field<Tvec>(ideltav_interf).insert(pdat.get_field<Tvec>(ideltav));
2525 if (has_s_j_field) {
2526 pdat_new.get_field<Tscal>(is_j_interf).insert(pdat.get_field<Tscal>(is_j));
2529 pdat_new.check_field_obj_cnt_match();
2534 pdat.insert_elements(pdat_interf);
2537 timer_interf.stop();
2538 storage.timings_details.interface += timer_interf.elapsed_sec();
2913 for (
auto &callbacks : timestep_callbacks) {
2914 if (callbacks.step_begin_callback) {
2919 Tscal t_current = get_time();
2920 Tscal dt = get_dt_sph();
2926 sham::format(
"---------------- t = {}, dt = {} ----------------", t_current, dt));
2934 .update_load_balancing();
2935 scheduler().scheduler_step(
true,
true);
2937 .update_load_balancing();
2939 scheduler().scheduler_step(
false,
false);
2945 using namespace shamrock::patch;
2947 bool has_b_field = solver_config.has_field_b_on_rho();
2948 bool has_psi_field = solver_config.has_field_psi_on_ch();
2949 bool has_epsilon_field = solver_config.dust_config.has_epsilon_field();
2950 bool has_deltav_field = solver_config.dust_config.has_deltav_field();
2951 bool has_s_j_field = solver_config.dust_config.has_s_j_field();
2953 bool do_nimhd = solver_config.do_nimhd();
2963 const u32 iB_on_rho = (has_b_field) ? pdl.
get_field_idx<Tvec>(
"B/rho") : 0;
2964 const u32 idB_on_rho = (has_b_field) ? pdl.
get_field_idx<Tvec>(
"dB/rho") : 0;
2965 const u32 ipsi_on_ch = (has_psi_field) ? pdl.
get_field_idx<Tscal>(
"psi/ch") : 0;
2966 const u32 idpsi_on_ch = (has_psi_field) ? pdl.
get_field_idx<Tscal>(
"dpsi/ch") : 0;
2967 const u32 iepsilon = (has_epsilon_field) ? pdl.
get_field_idx<Tscal>(
"epsilon") : 0;
2968 const u32 idtepsilon = (has_epsilon_field) ? pdl.
get_field_idx<Tscal>(
"dtepsilon") : 0;
2970 const u32 ids_j_dt = (has_s_j_field) ? pdl.
get_field_idx<Tscal>(
"ds_j_dt") : 0;
2971 const u32 ideltav = (has_deltav_field) ? pdl.
get_field_idx<Tvec>(
"deltav") : 0;
2972 const u32 idtdeltav = (has_deltav_field) ? pdl.
get_field_idx<Tvec>(
"dtdeltav") : 0;
2979 using namespace shamrock::solvergraph;
2993 gen_serial_patch_tree();
2997 u64 Npart_all = scheduler().get_total_obj_count();
2999 if (solver_config.enable_particle_reordering
3000 && solve_logs.step_count % solver_config.particle_reordering_step_freq == 0) {
3001 logger::info_ln(
"SPH",
"Reordering particles at step ", solve_logs.step_count);
3009 using namespace shamrock::solvergraph;
3020 if (solver_config.self_grav_config.is_sg_on()) {
3021 using namespace shamrock::solvergraph;
3027 auto &merged_xyzh = storage.merged_xyzh.get();
3037 u32 iB_on_rho_interf = (has_b_field) ? ghost_layout.
get_field_idx<Tvec>(
"B/rho") : 0;
3038 u32 ipsi_on_rho_interf = (has_psi_field) ? ghost_layout.
get_field_idx<Tscal>(
"psi/ch") : 0;
3045 u32 corrector_iter_cnt = 0;
3046 bool need_rerun_corrector =
false;
3052 if (corrector_iter_cnt == 50) {
3054 "the corrector has made over 50 loops, either their is a bug, either you are using "
3055 "a dt that is too large");
3061 if (solver_config.has_field_alphaAV()) {
3063 std::shared_ptr<shamrock::solvergraph::PatchDataLayerRefs> patchdatas
3064 = std::make_shared<shamrock::solvergraph::PatchDataLayerRefs>(
3065 "patchdata_layer_ref",
"patchdata_layer_ref");
3067 auto node_set_edge = scheduler().get_node_set_edge_patchdata_layer_refs();
3068 node_set_edge->set_edges(patchdatas);
3069 node_set_edge->evaluate();
3072 scheduler().get_layout_ptr_old(),
"alpha_AV");
3073 node_copy.set_edges(patchdatas, storage.alpha_av_updated);
3077 if (solver_config.has_field_dtdivv()) {
3079 if (solver_config.combined_dtdiv_divcurlv_compute) {
3080 if (solver_config.has_field_dtdivv()) {
3082 .update_dtdivv(
true);
3086 if (solver_config.has_field_divv()) {
3091 if (solver_config.has_field_curlv()) {
3096 if (solver_config.has_field_dtdivv()) {
3098 .update_dtdivv(
false);
3103 if (solver_config.has_field_divv()) {
3108 if (solver_config.has_field_curlv()) {
3125 if (solver_config.has_field_alphaAV()) {
3130 using InterfaceBuildInfos =
3134 time_interf.
start();
3136 auto field_interf = ghost_handle.template build_interface_native<PatchDataField<Tscal>>(
3137 storage.ghost_patch_cache.get(),
3140 InterfaceBuildInfos binfo,
3145 return sender_field.make_new_from_subset(buf_idx, cnt);
3149 = ghost_handle.communicate_pdatfield(
3150 std::move(field_interf), 1, storage.exchange_gz_alpha);
3154 std::move(interf_pdat),
3157 = comp_field_send.
get_field(p.id_patch);
3158 return receiver_field.duplicate();
3161 mpdat.insert(pdat_interf);
3165 storage.timings_details.interface += time_interf.
elapsed_sec();
3167 storage.alpha_av_ghost.set(std::move(merged_field));
3181 using InterfaceBuildInfos =
3185 time_interf.
start();
3187 auto field_interf = ghost_handle.template build_interface_native<PatchDataField<Tvec>>(
3188 storage.ghost_patch_cache.get(),
3191 InterfaceBuildInfos binfo,
3196 return sender_field.make_new_from_subset(buf_idx, cnt);
3200 = ghost_handle.communicate_pdatfield(
3201 std::move(field_interf), 1, storage.exchange_gz_J);
3205 std::move(interf_pdat),
3208 = comp_field_send.
get_field(p.id_patch);
3209 return receiver_field.duplicate();
3212 mpdat.insert(pdat_interf);
3216 storage.timings_details.interface += time_interf.
elapsed_sec();
3219 storage.MagCurrentJ_ghost.set(std::move(merged_field));
3225 constexpr bool debug_interfaces =
false;
3226 if constexpr (debug_interfaces) {
3228 if (solver_config.do_debug_dump) {
3231 = storage.merged_patchdata_ghost.get();
3238 merged_xyzh.get(cur_p.
id_patch).field_pos.get_buf());
3239 sycl::buffer<Tvec> &buf_vxyz = mpdat.get_field_buf_ref<Tvec>(ivxyz_interf);
3240 sycl::buffer<Tscal> &buf_hpart = mpdat.get_field_buf_ref<Tscal>(ihpart_interf);
3244 SHAM_ASSERT(merged_patch.total_elements == total_elements);
3248 solver_config.gpart_mass,
3254 make_interface_debug_phantom_dump(info).gen_file().write_to_file(
3255 solver_config.debug_dump_filename);
3262 shamlog_debug_ln(
"sph::BasicGas",
"compute force");
3272 bool compute_GW = solver_config.compute_gw;
3275 using namespace shamrock::solvergraph;
3278 auto central_pos = IDataEdge<Tvec>::make_shared(
"x_0",
"\\mathbf{x}_0");
3279 central_pos->data = Tvec{0, 0, 0};
3281 auto central_vel = IDataEdge<Tvec>::make_shared(
"v_0",
"\\mathbf{v}_0");
3282 central_vel->data = Tvec{0, 0, 0};
3284 auto central_acc = IDataEdge<Tvec>::make_shared(
"a_0",
"\\mathbf{a}_0");
3285 central_acc->data = Tvec{0, 0, 0};
3287 auto gw_prefactor = IDataEdge<Tscal>::make_shared(
"gw_prefactor",
"gw_prefactor");
3288 gw_prefactor->data = Tscal(1);
3290 auto theta_gw = IDataEdge<Tscal>::make_shared(
"theta_gw",
"\\theta_{\\rm gw}");
3291 theta_gw->data = Tscal(0);
3293 auto phi_gw = IDataEdge<Tscal>::make_shared(
"phi_gw",
"\\phi_{\\rm gw}");
3294 phi_gw->data = Tscal(0);
3299 auto spans_masses = std::make_shared<FieldRefs<Tscal>>(
"m",
"m");
3300 map_field_refs_ext(scheduler(), gw_mass_field, *spans_masses);
3303 auto spans_accel_ext
3304 = std::make_shared<FieldRefs<Tvec>>(
"axyz_ext",
"\\mathbf{a}_{\\rm ext}");
3305 map_field_refs(scheduler(), iaxyz_ext, *spans_accel_ext);
3307 auto ddq = IDataEdge<typename GW::Tddq>::make_shared(
"ddq",
"\\ddot{Q}");
3308 auto ddq_xy = IDataEdge<typename GW::Tddqxy>::make_shared(
"ddq_xy",
"\\ddot{Q}_{xy}");
3309 auto hx = IDataEdge<typename GW::Th>::make_shared(
"hx",
"h_x");
3310 auto hp = IDataEdge<typename GW::Th>::make_shared(
"hp",
"h_+");
3312 GW node_computeGW{};
3313 node_computeGW.set_edges(
3325 storage.part_counts,
3331 node_computeGW.evaluate();
3340 bool has_luminosity = solver_config.compute_luminosity;
3342 if (has_luminosity) {
3346 .set_refs(storage.merged_xyzh.get()
3349 return std::ref(mpdat.get_field<Tscal>(
3354 .set_refs(storage.merged_xyzh.get()
3357 return std::ref(mpdat.get_field<Tscal>(1));
3360 auto uint_with_ghost = shamrock::solvergraph::FieldRefs<Tscal>::make_shared(
"",
"");
3362 set_uint_with_ghost_refs(
3365 = storage.merged_patchdata_ghost.
get();
3370 scheduler().for_each_patchdata_nonempty(
3374 auto &field = mpdat.get_field<Tscal>(iuint_interf);
3378 field_uint_with_ghost_edge.set_refs(field_uint_with_ghost_refs);
3381 set_uint_with_ghost_refs.
set_edges(uint_with_ghost);
3383 auto omega_with_ghost = shamrock::solvergraph::FieldRefs<Tscal>::make_shared(
"",
"");
3386 &field_omega_with_ghost_edge) {
3388 = storage.merged_patchdata_ghost.
get();
3393 scheduler().for_each_patchdata_nonempty(
3397 auto &field = mpdat.get_field<Tscal>(iomega_interf);
3401 field_omega_with_ghost_edge.set_refs(field_omega_with_ghost_refs);
3404 set_omega_with_ghost_refs.
set_edges(omega_with_ghost);
3406 auto luminosity = shamrock::solvergraph::FieldRefs<Tscal>::make_shared(
"",
"");
3409 set_luminosity_refs(
3412 = storage.merged_patchdata_ghost.
get();
3417 scheduler().for_each_patchdata_nonempty(
3419 auto &field = pdat.get_field<Tscal>(iluminosity);
3422 field_luminosity_edge.set_refs(field_luminosity_refs);
3425 set_luminosity_refs.
set_edges(luminosity);
3427 set_uint_with_ghost_refs.evaluate();
3428 set_omega_with_ghost_refs.evaluate();
3429 set_luminosity_refs.evaluate();
3431 Tscal alpha_u = solver_config.artif_viscosity.get_alpha_u().value();
3434 solver_config.gpart_mass, alpha_u};
3436 compute_luminosity.set_edges(
3437 storage.part_counts,
3438 storage.part_counts_with_ghost,
3439 storage.neigh_cache,
3440 storage.positions_with_ghosts,
3441 storage.hpart_with_ghosts,
3459 shamlog_debug_ln(
"sph::BasicGas",
"leapfrog corrector");
3460 utility.fields_leapfrog_corrector<Tvec>(
3461 ivxyz, iaxyz, storage.old_axyz.get(), vepsilon_v_sq, dt / 2);
3462 utility.fields_leapfrog_corrector<Tscal>(
3463 iuint, iduint, storage.old_duint.get(), uepsilon_u_sq, dt / 2);
3465 if (solver_config.has_field_b_on_rho()) {
3468 utility.fields_leapfrog_corrector<Tvec>(
3469 iB_on_rho, idB_on_rho, storage.old_dB_on_rho.get(), BOR_epsilon_BOR_sq, dt / 2);
3471 if (solver_config.has_field_b_on_rho()) {
3474 utility.fields_leapfrog_corrector<Tscal>(
3475 ipsi_on_ch, idpsi_on_ch, storage.old_dpsi_on_ch.get(), POC_epsilon_POC_sq, dt / 2);
3478 if (solver_config.dust_config.has_epsilon_field()) {
3481 utility.fields_leapfrog_corrector<Tscal>(
3482 iepsilon, idtepsilon, storage.old_dtepsilon.get(), epsilon_epsilon_sq, dt / 2);
3485 if (solver_config.dust_config.has_deltav_field()) {
3488 utility.fields_leapfrog_corrector<Tvec>(
3489 ideltav, idtdeltav, storage.old_dtdeltav.get(), epsilon_deltav_sq, dt / 2);
3492 if (solver_config.dust_config.has_s_j_field()) {
3494 "s_j s_j^2", solver_config.dust_config.get_dust_nvar());
3495 bool ensure_positivity
3496 = solver_config.dust_config.get_monofluid_tva().ensure_s_j_positivity;
3497 if (ensure_positivity) {
3498 utility.fields_leapfrog_corrector_positive_only<Tscal>(
3499 is_j, ids_j_dt, storage.old_ds_j_dt.get(), s_j_s_j_sq, dt / 2);
3501 utility.fields_leapfrog_corrector<Tscal>(
3502 is_j, ids_j_dt, storage.old_ds_j_dt.get(), s_j_s_j_sq, dt / 2);
3505 auto &monofluid_tva_cfg = solver_config.dust_config.get_monofluid_tva();
3506 if (monofluid_tva_cfg.should_clamp_dust_density()) {
3508 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
"hfactd",
"hfactd");
3509 hfactd_edge->data = Kernel::hfactd;
3511 auto clamp_frac_edge = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
3512 "clamp_frac",
"clamp_frac");
3513 clamp_frac_edge->data = monofluid_tva_cfg.get_clamp_dust_frac();
3516 solver_config.dust_config.get_dust_nvar());
3517 density_clamp.set_edges(
3520 storage.solver_graph
3525 storage.solver_graph
3527 storage.solver_graph
3533 storage.old_axyz.reset();
3534 storage.old_duint.reset();
3535 if (solver_config.has_field_b_on_rho()) {
3536 storage.old_dB_on_rho.reset();
3538 if (solver_config.has_field_b_on_rho()) {
3539 storage.old_dpsi_on_ch.reset();
3542 if (solver_config.dust_config.has_epsilon_field()) {
3543 storage.old_dtepsilon.reset();
3546 if (solver_config.dust_config.has_deltav_field()) {
3547 storage.old_dtdeltav.reset();
3550 if (solver_config.dust_config.has_s_j_field()) {
3551 storage.old_ds_j_dt.reset();
3554 Tscal rank_veps_v = sycl::sqrt(vepsilon_v_sq.compute_rank_max());
3559 Tscal sum_vsq = utility.compute_rank_dot_sum<Tvec>(ivxyz);
3561 Tscal vmean_sq = shamalgs::collective::allreduce_sum(sum_vsq) / Tscal(Npart_all);
3563 Tscal vmean = sycl::sqrt(vmean_sq);
3565 Tscal rank_eps_v = rank_veps_v / vmean;
3571 Tscal eps_v = shamalgs::collective::allreduce_max(rank_eps_v);
3573 shamlog_debug_ln(
"BasicGas",
"epsilon v :", eps_v);
3580 "the corrector tolerance are broken the step will "
3581 "be re rerunned\n eps_v = {}",
3584 need_rerun_corrector =
true;
3585 set_cfl_multipler(get_cfl_multipler() / 2);
3589 need_rerun_corrector =
false;
3592 if (!need_rerun_corrector) {
3594 storage.solver_graph.get_node_ref_base(
"sink corrector").evaluate();
3597 if (solver_config.has_field_alphaAV()) {
3605 = pdat.get_field<Tscal>(ialpha_AV).get_buf();
3607 = alpha_av_updated.get_field(cur_p.
id_patch).get_buf();
3609 auto &q = shamsys::instance::get_compute_scheduler().
get_queue();
3617 const Tscal *__restrict alpha_av_updated,
3618 Tscal *__restrict alpha_av) {
3619 alpha_av[id_a] = alpha_av_updated[id_a];
3634 = MagCurrentJ.get_field(cur_p.
id_patch).get_buf();
3636 auto &q = shamsys::instance::get_compute_scheduler().
get_queue();
3643 [](
u32 id_a,
const Tvec *__restrict MagCurrentJ, Tvec *__restrict J) {
3644 J[id_a] = MagCurrentJ[id_a];
3649 shamlog_debug_ln(
"BasicGas",
"computing next CFL");
3653 = storage.merged_xyzh.get().template map<u32>(
3655 return scheduler().patch_data.get_pdat(
id).get_obj_cnt();
3658 std::shared_ptr<shamrock::solvergraph::Field<Tscal>> vsig_max_dt
3659 = std::make_shared<shamrock::solvergraph::Field<Tscal>>(
3660 1,
"vsig_a",
"v_{\\rm sig}");
3663 std::shared_ptr<shamrock::solvergraph::Field<Tscal>> vclean_dt;
3664 if (has_psi_field) {
3665 vclean_dt = std::make_shared<shamrock::solvergraph::Field<Tscal>>(
3666 1,
"vclean_a",
"v_{\\rm clean}");
3671 = storage.merged_patchdata_ghost.
get();
3677 = merged_xyzh.get(cur_p.
id_patch).template get_field_buf_ref<Tvec>(0);
3680 = mpdat.get_field_buf_ref<Tscal>(ihpart_interf);
3699 auto &q = shamsys::instance::get_compute_scheduler().get_queue();
3704 auto hpart = buf_hpart.get_read_access(depends_list);
3705 auto u = buf_uint.get_read_access(depends_list);
3706 auto pressure = buf_pressure.get_read_access(depends_list);
3709 auto particle_looper_ptrs = pcache.get_read_access(depends_list);
3712 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
3713 const Tscal pmass = solver_config.gpart_mass;
3714 const Tscal alpha_u = 1.0;
3715 const Tscal alpha_AV = 1.0;
3716 const Tscal beta_AV = 2.0;
3720 constexpr Tscal Rker2 = Kernel::Rkern * Kernel::Rkern;
3722 shambase::parallel_for(
3724 using namespace shamrock::sph;
3726 Tvec sum_axyz = {0, 0, 0};
3728 Tscal h_a = hpart[id_a];
3730 Tvec xyz_a = xyz[id_a];
3731 Tvec vxyz_a = vxyz[id_a];
3733 Tscal rho_a = rho_h(pmass, h_a, Kernel::hfactd);
3734 Tscal rho_a_sq = rho_a * rho_a;
3735 Tscal rho_a_inv = 1. / rho_a;
3737 Tscal P_a = pressure[id_a];
3739 const Tscal u_a = u[id_a];
3741 Tscal cs_a = cs[id_a];
3745 particle_looper.for_each_object(id_a, [&](
u32 id_b) {
3747 Tvec dr = xyz_a - xyz[id_b];
3748 Tscal rab2 = sycl::dot(dr, dr);
3749 Tscal h_b = hpart[id_b];
3751 if (rab2 > h_a * h_a * Rker2 && rab2 > h_b * h_b * Rker2) {
3755 Tscal rab = sycl::sqrt(rab2);
3756 Tvec vxyz_b = vxyz[id_b];
3757 Tvec v_ab = vxyz_a - vxyz_b;
3758 const Tscal u_b = u[id_b];
3760 Tvec r_ab_unit = dr / rab;
3763 r_ab_unit = {0, 0, 0};
3766 Tscal rho_b = rho_h(pmass, h_b, Kernel::hfactd);
3767 Tscal P_b = pressure[id_b];
3768 Tscal cs_b = cs[id_b];
3769 Tscal v_ab_r_ab = sycl::dot(v_ab, r_ab_unit);
3770 Tscal abs_v_ab_r_ab = sycl::fabs(v_ab_r_ab);
3775 const Tscal alpha_a = alpha_AV;
3776 const Tscal alpha_b = alpha_AV;
3778 Tscal vsig_a = alpha_a * cs_a + beta_AV * abs_v_ab_r_ab;
3780 vsig_max = sycl::fmax(vsig_max, vsig_a);
3783 vsig[id_a] = vsig_max;
3787 if (has_psi_field) {
3789 Tscal
const mu_0 = solver_config.get_constant_mu_0();
3792 Tvec *B_on_rho = mpdat.get_field_buf_ref<Tvec>(iB_on_rho_interf)
3793 .get_write_access(depends_list);
3797 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
3798 const Tscal pmass = solver_config.gpart_mass;
3802 constexpr Tscal Rker2 = Kernel::Rkern * Kernel::Rkern;
3804 shambase::parallel_for(
3806 using namespace shamrock::sph;
3808 Tscal h_a = hpart[id_a];
3809 Tscal rho_a = rho_h(pmass, h_a, Kernel::hfactd);
3810 const Tscal u_a = u[id_a];
3811 Tscal cs_a = cs[id_a];
3812 Tvec B_a = B_on_rho[id_a] * rho_a;
3814 Tscal vclean_a = shamphys::MHD_physics<Tvec, Tscal>::v_shock(
3815 cs_a, B_a, rho_a, mu_0);
3817 vclean[id_a] = vclean_a;
3820 mpdat.get_field_buf_ref<Tvec>(iB_on_rho_interf).complete_event_state(e);
3826 buf_hpart.complete_event_state(e);
3827 buf_uint.complete_event_state(e);
3828 buf_pressure.complete_event_state(e);
3834 pcache.complete_event_state(resulting_events);
3838 std::shared_ptr<shamrock::solvergraph::Field<Tscal>> cfl_dt
3839 = std::make_shared<shamrock::solvergraph::Field<Tscal>>(
3840 1,
"cfl_dt",
"\\Delta t_{cfl}");
3843 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tvec>> axyz_refs
3844 = std::make_shared<shamrock::solvergraph::FieldRefs<Tvec>>(
"axyz",
"\\mathbf{a}");
3845 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tscal>> hpart_refs
3846 = std::make_shared<shamrock::solvergraph::FieldRefs<Tscal>>(
"hpart",
"h");
3848 map_field_refs(scheduler(), iaxyz, *axyz_refs);
3849 map_field_refs_ext(scheduler(), mpdats, ihpart_interf, *hpart_refs);
3851 auto &q = shamsys::instance::get_compute_scheduler().get_queue();
3853 auto reset_dt_part_field = [&]() {
3854 if (solver_config.should_save_dt_to_fields()) {
3858 = pdat.get_field_buf_ref<Tscal>(idt_part);
3859 buf_dt_part.
fill(shambase::get_infty<Tscal>());
3864 auto save_dt_min_to_dt_part = [&]() {
3865 if (solver_config.should_save_dt_to_fields()) {
3869 = pdat.get_field_buf_ref<Tscal>(idt_part);
3877 [](
u32 id_a,
const Tscal *dt, Tscal *dt_part) {
3878 dt_part[id_a] = sycl::min(dt_part[id_a], dt[id_a]);
3885 auto reset_cfl_dt = [&]() {
3887 cfl_dt->get_buf(cur_p.
id_patch).fill(shambase::get_infty<Tscal>());
3891 Tscal C_cour = solver_config.cfl_config.cfl_cour * get_cfl_multipler();
3892 Tscal C_force = solver_config.cfl_config.cfl_force * get_cfl_multipler();
3893 Tscal eta_phi = solver_config.cfl_config.eta_sink;
3895 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> C_cour_edge
3896 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
"C_cour",
"C_{cour}");
3897 C_cour_edge->data = C_cour;
3898 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> C_force_edge
3899 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
"C_force",
"C_{force}");
3900 C_force_edge->data = C_force;
3901 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> eta_phi_edge
3902 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
"eta_phi",
"\\eta_{\\phi}");
3903 eta_phi_edge->data = eta_phi;
3905 std::shared_ptr<ComputeCFLCourant<Tscal>> compute_cfl_courant
3906 = std::make_shared<ComputeCFLCourant<Tscal>>();
3907 compute_cfl_courant->set_edges(
3908 storage.part_counts, C_cour_edge, hpart_refs, vsig_max_dt, cfl_dt);
3910 std::shared_ptr<ComputeCFLForce<Tvec>> compute_cfl_force
3911 = std::make_shared<ComputeCFLForce<Tvec>>();
3912 compute_cfl_force->set_edges(
3913 storage.part_counts, C_force_edge, hpart_refs, axyz_refs, cfl_dt);
3915 std::shared_ptr<ComputeCFLNIMHD<Tvec>> compute_cfl_NIMHD;
3917 compute_cfl_NIMHD = std::make_shared<ComputeCFLNIMHD<Tvec>>();
3919 Tscal C_NIMHD = solver_config.cfl_config.cfl_NIMHD * get_cfl_multipler();
3920 Cfg_MHD cfg_mhd = solver_config.mhd_config;
3921 auto *nimhd = std::get_if<typename Cfg_MHD::NonIdealMHD>(&cfg_mhd.configMHD);
3923 Tscal eta_AD = nimhd->etaAD;
3924 Tscal eta_O = nimhd->etaO;
3925 Tscal eta_H = nimhd->etaH;
3926 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> C_NIMHD_edge
3927 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
"C_NIMHD",
"C_{NIMHD}");
3928 C_NIMHD_edge->data = C_NIMHD;
3929 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> eta_O_edge
3930 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
"eta_O",
"eta_{O}");
3931 eta_O_edge->data = eta_O;
3932 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> eta_AD_edge
3933 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
"eta_AD",
"eta_{AD}");
3934 eta_AD_edge->data = eta_AD;
3935 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> eta_H_edge
3936 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
"eta_H",
"eta_{H}");
3937 eta_H_edge->data = eta_H;
3939 compute_cfl_NIMHD->set_edges(
3940 storage.part_counts,
3949 std::shared_ptr<ComputeCFLDivBCleaning<Tscal>> compute_cfl_divB_cleaning;
3950 if (has_psi_field) {
3951 compute_cfl_divB_cleaning = std::make_shared<ComputeCFLDivBCleaning<Tscal>>();
3952 compute_cfl_divB_cleaning->set_edges(
3953 storage.part_counts, C_cour_edge, hpart_refs, vclean_dt, cfl_dt);
3956 std::shared_ptr<ComputeCFLDust1Fluid<Tvec>> compute_cfl_dust1_fluid;
3957 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tscal>> s_j_refs;
3958 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> hfactd_edge;
3960 if (solver_config.dust_config.has_s_j_field()) {
3961 u32 ndust = solver_config.dust_config.get_dust_nvar();
3963 compute_cfl_dust1_fluid = std::make_shared<ComputeCFLDust1Fluid<Tvec>>(ndust);
3966 = storage.solver_graph
3967 .template get_edge_ptr<shamrock::solvergraph::Field<Tscal>>(
"Ts_j");
3970 = storage.solver_graph
3971 .template get_edge_ptr<shamrock::solvergraph::IDataEdge<Tscal>>(
3974 s_j_refs = std::make_shared<shamrock::solvergraph::FieldRefs<Tscal>>(
"s_j",
"s_j");
3977 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
"hfactd",
"hfactd");
3978 hfactd_edge->data = Kernel::hfactd;
3980 map_field_refs(scheduler(), is_j, *s_j_refs);
3982 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> C_1fluid_edge
3983 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
3984 "C_1fluid",
"C_{1fluid}");
3986 = solver_config.dust_config.get_monofluid_tva().C_1_fluid * get_cfl_multipler();
3988 compute_cfl_dust1_fluid->set_edges(
3989 storage.part_counts,
4000 std::shared_ptr<ComputeCFLDustDrift<Tvec>> compute_cfl_dust_drift;
4001 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> C_drift_edge;
4002 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> cfl_density_threshold_edge;
4003 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tvec>> delta_v_refs;
4005 if (solver_config.dust_config.has_s_j_field()) {
4006 u32 ndust = solver_config.dust_config.get_dust_nvar();
4008 compute_cfl_dust_drift = std::make_shared<ComputeCFLDustDrift<Tvec>>(ndust);
4010 delta_v_refs = std::make_shared<shamrock::solvergraph::FieldRefs<Tvec>>(
4011 "delta_v",
"delta_v");
4013 map_field_refs(scheduler(), idelta_v, *delta_v_refs);
4015 auto &cfg_monofluid_tva = solver_config.dust_config.get_monofluid_tva();
4018 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
"C_drift",
"C_{drift}");
4019 C_drift_edge->data = cfg_monofluid_tva.C_drift * get_cfl_multipler();
4021 cfl_density_threshold_edge = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
4022 "cfl_density_threshold",
"cfl_density_threshold");
4023 cfl_density_threshold_edge->data = cfg_monofluid_tva.cfl_density_threshold;
4026 = storage.solver_graph
4027 .template get_edge_ptr<shamrock::solvergraph::IDataEdge<Tscal>>(
4030 compute_cfl_dust_drift->set_edges(
4031 storage.part_counts,
4033 cfl_density_threshold_edge,
4042 bool show_cfl_detail = solver_config.show_cfl_detail;
4043 std::vector<std::pair<std::string, Tscal>> cfl_detail;
4045 auto save_cfl_detail = [&](
const char *key) {
4046 if (show_cfl_detail) {
4047 save_dt_min_to_dt_part();
4048 cfl_detail.push_back(
4049 {std::string(key), cfl_dt->get_native().compute_rank_min()});
4054 reset_dt_part_field();
4057 compute_cfl_courant->evaluate();
4058 save_cfl_detail(
"courant");
4060 compute_cfl_force->evaluate();
4061 save_cfl_detail(
"force");
4064 compute_cfl_NIMHD->evaluate();
4065 save_cfl_detail(
"NIMHD");
4068 if (has_psi_field) {
4069 compute_cfl_divB_cleaning->evaluate();
4070 save_cfl_detail(
"divB_cleaning");
4073 if (solver_config.dust_config.has_s_j_field()) {
4074 compute_cfl_dust1_fluid->evaluate();
4075 save_cfl_detail(
"dust1_fluid");
4077 compute_cfl_dust_drift->evaluate();
4078 save_cfl_detail(
"dust_drift");
4081 if (!show_cfl_detail) {
4082 save_dt_min_to_dt_part();
4083 cfl_detail.push_back({
"all SPH", cfl_dt->get_native().compute_rank_min()});
4086 auto &sync = scheduler().synchronized_data;
4091 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> G_edge
4092 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
"G",
"G");
4093 G_edge->data = solver_config.get_constant_G();
4095 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> sink_sink_cfl
4096 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
4097 "sink_sink_cfl",
"\\Delta t_{\\rm sink-sink}");
4104 compute_cfl_sink_sink.set_edges(
4108 sync.template get_edge_ptr<SinkVecEdge>(
"sink_pos"),
4109 sync.template get_edge_ptr<SinkScalEdge>(
"sink_mass"),
4110 sync.template get_edge_ptr<SinkVecEdge>(
"sink_acc_ext"),
4114 cfl_detail.push_back({
"sink_sink", sink_sink_cfl->data});
4117 Tscal rank_dt = shambase::get_infty<Tscal>();
4118 for (
auto &[key, value] : cfl_detail) {
4119 rank_dt = sham::min(rank_dt, value);
4122 if (show_cfl_detail) {
4123 for (
auto &[key, value] : cfl_detail) {
4124 value = shamalgs::collective::allreduce_min(value);
4129 table.add_double_rule();
4130 table.add_data({
"key",
"value"}, shambase::table::center);
4131 table.add_double_rule();
4132 for (
auto &[key, value] : cfl_detail) {
4134 {key, sham::format(
"{:.2e}", value)}, shambase::table::right);
4141 next_cfl = shamalgs::collective::allreduce_min(rank_dt);
4145 if (show_cfl_detail) {
4146 for (
auto &[key, value] : cfl_detail) {
4153 "sph::Model",
"cfl dt =", next_cfl,
"cfl multiplier :", get_cfl_multipler());
4159 if (solver_config.has_field_soundspeed()) {
4174 auto &q = shamsys::instance::get_compute_scheduler().get_queue();
4177 auto cs_in = buf_cs_in.get_read_access(depends_list);
4180 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
4181 const Tscal pmass = solver_config.gpart_mass;
4184 sycl::range<1>{pdat.
get_obj_cnt()}, [=](sycl::item<1> item) {
4185 cs[item] = cs_in[item];
4189 buf_cs_in.complete_event_state(e);
4196 corrector_iter_cnt++;
4198 if (solver_config.has_field_alphaAV()) {
4199 storage.alpha_av_ghost.reset();
4203 storage.MagCurrentJ_ghost.reset();
4206 }
while (need_rerun_corrector);
4208 reset_merge_ghosts_fields();
4215 for (
auto it = timestep_callbacks.rbegin(); it != timestep_callbacks.rend(); ++it) {
4216 if (it->step_end_callback) {
4229 = (mem_perf_infos_end.
time_alloc_device - mem_perf_infos_start.time_alloc_device)
4230 + (mem_perf_infos_end.
time_free_device - mem_perf_infos_start.time_free_device);
4231 f64 t_host_alloc = (mem_perf_infos_end.
time_alloc_host - mem_perf_infos_start.time_alloc_host)
4232 + (mem_perf_infos_end.
time_free_host - mem_perf_infos_start.time_free_host);
4234 u64 rank_count = scheduler().get_rank_count();
4235 f64 rate =
f64(rank_count) / tstep.elapsed_sec();
4237 u64 npatch = scheduler().patch_list.local.size();
4240 std::optional<u64> nobj_ghost = std::nullopt;
4241 if (solver_config.show_ghost_stats) {
4244 .indexes.for_each([&](
u64 id,
u32 cnt_with_ghost) {
4245 cnt_ghost += cnt_with_ghost;
4250 nobj_ghost = cnt_ghost;
4255 std::string log_step = report_perf_timestep(
4259 tstep.elapsed_sec(),
4265 system_metrics_delta,
4266 shamsys::has_reporter(),
4272 "sph::Model",
"estimated rate :", dt * (3600 / tstep.elapsed_sec()),
"(tsim/hr)");
4275 solve_logs.register_log(
4281 tstep.elapsed_sec(),
4283 system_metrics_delta});
4285 storage.timings_details.reset();
4287 reset_serial_patch_tree();
4288 reset_ghost_handler();
4294 storage.merged_xyzh.reset();
4296 clear_merged_pos_trees();
4297 clear_ghost_cache();
4298 reset_presteps_rint();
4299 reset_neighbors_cache();
4303 set_next_dt(next_cfl);
4304 set_time(t_current + dt);
4306 auto get_next_cfl_mult = [&]() {
4307 Tscal cfl_m = get_cfl_multipler();
4308 Tscal stiff = solver_config.cfl_config.cfl_multiplier_stiffness;
4310 return (cfl_m * stiff + 1.) / (stiff + 1.);
4313 set_cfl_multipler(get_next_cfl_mult());
4318 log.npart = rank_count;
4319 log.tcompute = tstep.elapsed_sec();