45#include <experimental/mdspan>
46#include <pybind11/cast.h>
47#include <pybind11/numpy.h>
48#include <pybind11/pytypes.h>
54template<
class Tvec,
template<
class>
class SPHKernel>
55void add_instance(py::module &m, std::string name_config, std::string name_model) {
58 using Tscal = shambase::VecComponent<Tvec>;
65 using TConfig =
typename T::Solver::Config;
70 shamlog_debug_ln(
"[Py]",
"registering class :", name_config,
typeid(T).name());
71 shamlog_debug_ln(
"[Py]",
"registering class :", name_model,
typeid(T).name());
73 py::class_<TConfig> config_cls(m, name_config.c_str());
75 shammodels::common::add_json_defs<TConfig>(config_cls);
77 config_cls.def(
"print_status", &TConfig::print_status)
78 .def(
"set_particle_tracking", &TConfig::set_particle_tracking)
80 "set_scheduler_config",
81 [](TConfig &self,
u64 split_crit,
u64 merge_crit) {
82 self.scheduler_conf.split_load_value = split_crit;
83 self.scheduler_conf.merge_load_value = merge_crit;
86 py::arg(
"split_load_value"),
87 py::arg(
"merge_load_value"))
88 .def(
"set_tree_reduction_level", &TConfig::set_tree_reduction_level)
90 "set_neigh_cache_strategy",
91 &TConfig::set_neigh_cache_strategy,
93 Set the strategy used to build the neighbours cache.
97 strategy : NeighCacheStrategy
98 Either ``NeighCacheStrategy.SingleStage`` or ``NeighCacheStrategy.TwoStage``
99 (the default), as obtained from ``from shamrock import NeighCacheStrategy``.
102 "set_two_stage_search",
103 &TConfig::set_two_stage_search,
105 Set the neighbours cache strategy from a boolean.
108 Use :py:meth:`set_neigh_cache_strategy` instead.
110 .def("set_show_neigh_stats", &TConfig::set_show_neigh_stats)
112 "set_max_neigh_cache_size",
113 [](TConfig &self,
const py::object &max_neigh_cache_size) {
116 ".set_max_neigh_cache_size() is deprecated,\n"
117 " -> calling this is a no-op,\n"
118 " -> you can remove the call to that function"););
120 .def(
"set_filter_empty_patch_gz", &TConfig::set_filter_empty_patch_gz)
121 .def(
"set_smoothing_length_density_based", &TConfig::set_smoothing_length_density_based)
123 "set_smoothing_length_density_based_neigh_lim",
124 &TConfig::set_smoothing_length_density_based_neigh_lim)
126 "set_smoothing_length_density_based_bisecting_nr",
127 &TConfig::set_smoothing_length_density_based_bisecting_nr)
128 .def(
"set_enable_particle_reordering", &TConfig::set_enable_particle_reordering)
129 .def(
"set_particle_reordering_step_freq", &TConfig::set_particle_reordering_step_freq)
130 .def(
"set_show_ghost_zone_graph", &TConfig::set_show_ghost_zone_graph)
131 .def(
"set_show_ghost_stats", &TConfig::set_show_ghost_stats)
132 .def(
"use_luminosity", &TConfig::use_luminosity)
133 .def(
"compute_GW", &TConfig::use_GW)
134 .def(
"set_save_dt_to_fields", &TConfig::set_save_dt_to_fields)
135 .def(
"should_save_dt_to_fields", &TConfig::should_save_dt_to_fields)
136 .def(
"set_eos_isothermal", &TConfig::set_eos_isothermal)
137 .def(
"set_eos_adiabatic", &TConfig::set_eos_adiabatic)
138 .def(
"set_eos_polytropic", &TConfig::set_eos_polytropic)
139 .def(
"set_eos_locally_isothermal", &TConfig::set_eos_locally_isothermal)
141 "set_eos_locally_isothermalLP07",
142 [](TConfig &self, Tscal cs0, Tscal q, Tscal r0) {
143 self.set_eos_locally_isothermalLP07(cs0, q, r0);
150 "set_eos_locally_isothermalFA2014",
151 [](TConfig &self, Tscal h_over_r) {
152 self.set_eos_locally_isothermalFA2014(h_over_r);
157 "set_eos_locally_isothermalFA2014_extended",
158 [](TConfig &self, Tscal cs0, Tscal q, Tscal r0,
u32 n_sinks) {
159 self.set_eos_locally_isothermalFA2014_extended(cs0, q, r0, n_sinks);
168 [](TConfig &self, Tscal mu_e) {
169 self.set_eos_fermi(mu_e);
174 "set_eos_barotropic",
175 [](TConfig &self, Tscal cs0, Tscal rho0, Tscal gamma0) {
176 self.set_eos_barotropic(cs0, rho0, gamma0);
181 py::arg(
"gamma0") = Tscal{5.0 / 3.0})
182 .def(
"set_artif_viscosity_None", &TConfig::set_artif_viscosity_None)
184 "set_artif_viscosity_Constant",
185 [](TConfig &self, Tscal alpha_u, Tscal alpha_AV, Tscal beta_AV) {
186 self.set_artif_viscosity_Constant({alpha_u, alpha_AV, beta_AV});
193 "set_artif_viscosity_VaryingMM97",
200 self.set_artif_viscosity_VaryingMM97(
201 {alpha_min, alpha_max, sigma_decay, alpha_u, beta_AV});
204 py::arg(
"alpha_min"),
205 py::arg(
"alpha_max"),
206 py::arg(
"sigma_decay"),
210 "set_artif_viscosity_VaryingCD10",
217 self.set_artif_viscosity_VaryingCD10(
218 {alpha_min, alpha_max, sigma_decay, alpha_u, beta_AV});
221 py::arg(
"alpha_min"),
222 py::arg(
"alpha_max"),
223 py::arg(
"sigma_decay"),
227 "set_artif_viscosity_ConstantDisc",
228 [](TConfig &self, Tscal alpha_AV, Tscal alpha_u, Tscal beta_AV) {
229 self.set_artif_viscosity_ConstantDisc({alpha_AV, alpha_u, beta_AV});
237 [](TConfig &self, Tscal sigma_mhd, Tscal alpha_B) {
238 self.set_ideal_mhd({sigma_mhd, alpha_B});
241 py::arg(
"sigma_mhd"),
242 py::arg(
"alpha_B") = 1.0)
245 [](TConfig &self, Tscal sigma_mhd, Tscal etaO, Tscal etaH, Tscal etaAD, Tscal alpha_B) {
246 self.set_non_ideal_mhd({sigma_mhd, alpha_B, etaO, etaH, etaAD});
249 py::arg(
"sigma_mhd"),
253 py::arg(
"alpha_B") = 1.0)
255 "set_self_gravity_none",
257 self.self_grav_config.set_none();
260 "set_self_gravity_direct",
261 [](TConfig &self,
bool reference_mode =
false) {
262 self.self_grav_config.set_direct(reference_mode);
265 py::arg(
"reference_mode") =
false)
267 "set_self_gravity_mm",
268 [](TConfig &self,
u32 mm_order,
f64 opening_angle,
u32 reduction_level) {
269 self.self_grav_config.set_mm(mm_order, opening_angle, reduction_level);
273 py::arg(
"opening_angle"),
274 py::arg(
"reduction_level") = 3)
276 "set_self_gravity_fmm",
277 [](TConfig &self,
u32 order,
f64 opening_angle,
u32 reduction_level) {
278 self.self_grav_config.set_fmm(order, opening_angle, reduction_level);
282 py::arg(
"opening_angle"),
283 py::arg(
"reduction_level") = 3)
285 "set_self_gravity_sfmm",
290 u32 reduction_level) {
291 self.self_grav_config.set_sfmm(
292 sfmm_order, opening_angle, leaf_lowering, reduction_level);
296 py::arg(
"opening_angle"),
297 py::arg(
"leaf_lowering") =
true,
298 py::arg(
"reduction_level") = 3)
300 "set_softening_plummer",
301 [](TConfig &self,
f64 epsilon) {
302 self.self_grav_config.set_softening_plummer(epsilon);
307 "set_softening_none",
309 self.self_grav_config.set_softening_none();
311 .def(
"set_boundary_free", &TConfig::set_boundary_free)
312 .def(
"set_boundary_periodic", &TConfig::set_boundary_periodic)
313 .def(
"set_boundary_shearing_periodic", &TConfig::set_boundary_shearing_periodic)
315 "set_dust_mode_none",
317 self.dust_config.set_none();
320 "set_dust_mode_monofluid_tva",
323 bool pure_diffusion_mode,
326 Tscal cfl_density_threshold,
327 bool ensure_s_j_positivity,
328 bool smooth_s_positivity_limiter,
329 bool dust_corrected_av,
330 std::optional<Tscal> clamp_dust_frac,
331 bool pair_gate_s_positivity_limiter) {
332 self.dust_config.set_monofluid_tva(
337 cfl_density_threshold,
338 ensure_s_j_positivity,
339 smooth_s_positivity_limiter,
342 pair_gate_s_positivity_limiter);
346 py::arg(
"pure_diffusion_mode") =
false,
347 py::arg(
"C_1_fluid") = 0.1,
348 py::arg(
"C_drift") = 1.0,
349 py::arg(
"cfl_density_threshold") = shambase::get_epsilon<Tscal>(),
350 py::arg(
"ensure_s_j_positivity") =
true,
351 py::arg(
"smooth_s_positivity_limiter") =
false,
352 py::arg(
"dust_corrected_av") =
false,
353 py::arg(
"clamp_dust_frac") = std::nullopt,
354 py::arg(
"pair_gate_s_positivity_limiter") =
false)
356 "set_dust_mode_monofluid_complete",
357 [](TConfig &self,
u32 ndust) {
358 self.dust_config.set_monofluid_complete(ndust);
363 "set_dust_drag_constant",
364 [](TConfig &self, std::vector<Tscal> ts) {
365 self.dust_config.set_drag_constant({.stopping_times = std::move(ts)});
368 "set_dust_drag_epstein",
371 std::vector<Tscal> grain_sizes,
372 std::vector<Tscal> grain_densities) {
373 self.dust_config.set_drag_epstein(
375 .grains_sizes = std::move(grain_sizes),
376 .grains_densities = std::move(grain_densities)});
379 py::arg(
"grain_sizes"),
380 py::arg(
"grain_densities"))
382 "set_dust_evol_coala_coag",
385 Tscal vfrag_threshold,
386 std::vector<Tscal> massgrid,
387 py::array_t<Tscal> tabflux_coag,
389 if (massgrid.size() == 0) {
391 "massgrid must not be empty");
394 u32 nbins = massgrid.size() - 1;
399 if (tabflux_coag.ndim() != 3) {
401 "tabflux_coag must be a 3D array, got ndim="
402 + std::to_string(tabflux_coag.ndim()));
406 if (tabflux_coag.shape(0) != nbins || tabflux_coag.shape(1) != nbins
407 || tabflux_coag.shape(2) != nbins) {
409 "tabflux_coag must be a 3D array of shape (nbins, nbins, nbins) with "
411 + std::to_string(nbins) +
" (massgrid.size() - 1), got shape ("
412 + std::to_string(tabflux_coag.shape(0)) +
", "
413 + std::to_string(tabflux_coag.shape(1)) +
", "
414 + std::to_string(tabflux_coag.shape(2)) +
")");
417 std::vector<Tscal> tabflux_coag_vec(nbins * nbins * nbins);
419 using mdspan_rank_3 = std::mdspan<Tscal, std::dextents<u32, 3>>;
420 mdspan_rank_3 tabflux_coag_mdspan(tabflux_coag_vec.data(), nbins, nbins, nbins);
422 for (
u32 i = 0; i < nbins; i++) {
423 for (
u32 j = 0; j < nbins; j++) {
424 for (
u32 k = 0;
k < nbins;
k++) {
425 tabflux_coag_mdspan(i, j, k) = tabflux_coag.mutable_at(i, j, k);
430 self.dust_config.set_dust_evol_coala(
431 {.rhodust_eps = rhodust_eps,
432 .tau_frac = tau_frac,
433 .vfrag_threshold = vfrag_threshold,
434 .massgrid = massgrid,
435 .tabflux_coag = tabflux_coag_vec});
437 py::arg(
"rhodust_eps"),
438 py::arg(
"vfrag_threshold"),
440 py::arg(
"tabflux_coag"),
441 py::arg(
"tau_frac") = Tscal(10),
443 Enable the COALA dust coagulation source term.
446 rhodust_eps: Dust density floor.
447 vfrag_threshold: Fragmentation velocity threshold, must be positive.
448 In this coagulation-only mode, a pair of dust bins whose
449 differential velocity ``|dv_ij|`` exceeds this value gets
450 ``dv_ij = 0``. This is a crude stand-in for fragmentation
451 ("poor man" fragmentation), not a fragmentation model. Pass
452 ``math.inf`` to disable it.
453 massgrid: Dust mass grid bin edges, of size ``nbins + 1``.
454 tabflux_coag: Tabulated coagulation flux, of shape
455 ``(nbins, nbins, nbins)``.
456 tau_frac: The source term on ``s_j`` is integrated implicitly over
457 ``dt_eff = max(dt, tau_frac * rhodust_eps / |S|)``, that is over at
458 least the time needed to move ``tau_frac * rhodust_eps``. This bounds
459 the rate of an empty bin when ``dt`` is null or tiny (e.g. right after
460 a dust injection). Must be positive.
463 "set_dust_ballabio_ts_limiter",
464 [](TConfig &self,
bool enabled) {
465 self.dust_config.ballabio_ts_limiter = enabled;
469 "add_ext_force_point_mass",
470 [](TConfig &self, Tscal central_mass, Tscal Racc, Tvec central_pos) {
471 self.add_ext_force_point_mass(central_mass, Racc, central_pos);
473 py::arg(
"central_mass"),
476 py::arg(
"central_pos") = Tvec{0, 0, 0})
477 .def(
"add_ext_force_paczynski_wiita", &TConfig::add_ext_force_paczynski_wiita)
479 "add_ext_force_lense_thirring",
486 self.add_ext_force_lense_thirring(
487 central_mass, Racc, a_spin, dir_spin, central_pos);
490 py::arg(
"central_mass"),
494 py::arg(
"central_pos") = Tvec{0, 0, 0})
496 "add_ext_force_shearing_box",
497 [](TConfig &self, Tscal Omega_0, Tscal eta, Tscal q) {
498 self.add_ext_force_shearing_box(Omega_0, eta, q);
505 "add_ext_force_velocity_dissipation",
506 [](TConfig &self, Tscal eta) {
507 self.ext_force_config.add_velocity_dissipation(eta);
512 "add_ext_force_vertical_disc_potential",
513 [](TConfig &self, Tscal central_mass, Tscal R0) {
514 self.ext_force_config.add_vertical_disc_potential(central_mass, R0);
517 py::arg(
"central_mass"),
519 .def(
"set_units", &TConfig::set_units)
523 return self.unit_sys;
527 [](TConfig &self, Tscal cfl_cour) {
528 self.cfl_config.cfl_cour = cfl_cour;
532 [](TConfig &self, Tscal cfl_force) {
533 self.cfl_config.cfl_force = cfl_force;
537 [](TConfig &self, Tscal eta_sink) {
538 self.cfl_config.eta_sink = eta_sink;
540 .def(
"set_cfl_mult_stiffness", &TConfig::set_cfl_mult_stiffness)
542 "set_show_cfl_detail",
543 [](TConfig &self,
bool show_cfl_detail) {
544 self.show_cfl_detail = show_cfl_detail;
546 py::arg(
"show_cfl_detail"))
549 [](TConfig &self, Tscal gpart_mass) {
550 self.gpart_mass = gpart_mass;
554 [](TConfig &self,
const Tvec ¢er, Tscal radius) {
555 self.particle_killing.add_kill_sphere(center, radius);
561 std::string sod_tube_analysis_name = name_model +
"_AnalysisSodTube";
562 py::class_<TAnalysisSodTube>(m, sod_tube_analysis_name.c_str())
563 .def(
"compute_L2_dist", [](TAnalysisSodTube &self) -> std::tuple<Tscal, Tvec, Tscal> {
564 auto ret = self.compute_L2_dist();
565 return {ret.rho, ret.v, ret.P};
568 std::string disc_analysis_name = name_model +
"_AnalysisDisc";
569 py::class_<TAnalysisDisc>(m, disc_analysis_name.c_str())
572 [](TAnalysisDisc &self, Tscal Rmin, Tscal Rmax,
u32 Nbin,
ShamrockCtx &ctx) {
573 auto anal = self.compute_analysis(Rmin, Rmax, Nbin, ctx);
576 auto radius = anal.radius.copy_to_stdvec();
577 auto counter = anal.counter.copy_to_stdvec();
578 auto Sigma = anal.Sigma.copy_to_stdvec();
579 auto lx = anal.lx.copy_to_stdvec();
580 auto ly = anal.ly.copy_to_stdvec();
581 auto lz = anal.lz.copy_to_stdvec();
582 auto tilt = anal.tilt.copy_to_stdvec();
583 auto twist = anal.twist.copy_to_stdvec();
584 auto psi = anal.psi.copy_to_stdvec();
585 auto Hsq = anal.Hsq.copy_to_stdvec();
587 dic_out[
"radius"] = radius;
588 dic_out[
"counter"] = counter;
589 dic_out[
"Sigma"] = Sigma;
593 dic_out[
"tilt"] = tilt;
594 dic_out[
"twist"] = twist;
595 dic_out[
"psi"] = psi;
596 dic_out[
"Hsq"] = Hsq;
601 std::string setup_name = name_model +
"_SPHSetup";
602 py::class_<TSPHSetup>(m, setup_name.c_str())
604 "make_generator_lattice_hcp",
610 Tscal init_h_factor) {
611 return self.make_generator_lattice_hcp(
612 dr, {box_min, box_max}, discontinuous, init_h_factor);
617 py::arg(
"discontinuous") =
true,
620 Generate particles on a HCP lattice of parameter dr (neighbours are 2 dr apart)
622 The initial smoothing length is set to init_h_factor * dr. The default 2^(5/6)
623 is the equilibrium smoothing length for hfact = 1 (the smallest hfact of all
624 the SPH kernels), so the initial guess never exceeds the equilibrium value.
627 "make_generator_lattice_fcc",
633 Tscal init_h_factor) {
634 return self.make_generator_lattice_fcc(
635 dr, {box_min, box_max}, discontinuous, init_h_factor);
640 py::arg(
"discontinuous") =
true,
643 Generate particles on a true FCC lattice (ABC stacking of close-packed layers along z)
644 of parameter dr (neighbours are 2 dr apart)
646 The number of layers along z must be a multiple of 3, the number of rows along y even
648 The initial smoothing length is set to init_h_factor * dr. The default 2^(5/6)
649 is the equilibrium smoothing length for hfact = 1 (the smallest hfact of all
650 the SPH kernels), so the initial guess never exceeds the equilibrium value.
653 "make_generator_lattice_cubic",
654 [](TSPHSetup &self, Tscal dr, Tvec box_min, Tvec box_max) {
655 return self.make_generator_lattice_cubic(dr, {box_min, box_max});
658 "make_generator_disc_mc",
664 std::function<Tscal(Tscal)> sigma_profile,
665 std::function<Tscal(Tscal)> H_profile,
666 std::function<Tscal(Tscal)> rot_profile,
667 std::function<Tscal(Tscal)> cs_profile,
668 std::function<Tvec(Tvec)> velocity_field,
669 std::function<Tscal(Tvec)> cs_field,
671 Tscal init_h_factor) {
672 auto build_vel_lambda = [&]() -> std::function<Tvec(Tvec)> {
673 if (!velocity_field && !rot_profile) {
675 "make_generator_disc_mc: either velocity_field or rot_profile must be "
676 "provided, you must provide one of them");
679 if (velocity_field && rot_profile) {
681 "make_generator_disc_mc: either velocity_field or rot_profile must be "
682 "provided, you cannot provide both");
685 if (velocity_field) {
686 return std::move(velocity_field);
688 return [vth_r = std::move(rot_profile)](Tvec pos) {
690 Tscal r = sycl::length(pos);
692 auto etheta = sycl::vec<Tscal, 3>{-pos.y(), pos.x(), 0};
693 etheta /= sycl::length(etheta);
695 return vth_r(r) * etheta;
699 auto build_cs_lambda = [&]() -> std::function<Tscal(Tvec)> {
700 bool need_cs = self.solver_config.is_eos_locally_isothermal();
707 "make_generator_disc_mc: with the current EOS, cs_field is "
715 "make_generator_disc_mc: with the current EOS, cs_profile is "
719 return std::function<Tscal(Tvec)>{};
722 if (!cs_field && !cs_profile) {
724 "make_generator_disc_mc: either cs_field or cs_profile must be "
725 "provided, you must provide one of them");
728 if (cs_field && cs_profile) {
730 "make_generator_disc_mc: either cs_field or cs_profile must be "
731 "provided, you cannot provide both");
735 return std::move(cs_field);
738 return [cs_r = std::move(cs_profile)](Tvec pos) {
740 Tscal r = sycl::length(pos);
745 return self.make_generator_disc_mc(
750 std::move(sigma_profile),
751 std::move(H_profile),
754 std::mt19937_64(random_seed),
758 py::arg(
"part_mass"),
759 py::arg(
"disc_mass"),
762 py::arg(
"sigma_profile"),
763 py::arg(
"H_profile"),
764 py::arg(
"rot_profile") = std::function<Tscal(Tscal)>{},
765 py::arg(
"cs_profile") = std::function<Tscal(Tscal)>{},
766 py::arg(
"velocity_field") = std::function<Tvec(Tvec)>{},
767 py::arg(
"cs_field") = std::function<Tscal(Tvec)>{},
768 py::arg(
"random_seed"),
769 py::arg(
"init_h_factor") = 0.8,
771 Create a Monte Carlo disc particle generator.
773 Particles are sampled in cylindrical coordinates: the radius is drawn
774 with rejection sampling from ``sigma_profile``, the azimuth is uniform,
775 and the vertical coordinate follows a Gaussian with scale ``H_profile(r)``.
776 The initial density is extrapolated from the surface density profile, and
777 smoothing lengths are set from that density.
780 part_mass: Mass of each SPH particle.
781 disc_mass: Total disc mass. The particle count is ``disc_mass / part_mass``.
782 r_in: Inner disc radius.
783 r_out: Outer disc radius.
784 sigma_profile: Surface density profile ``sigma(r)``.
785 H_profile: Disc scale height profile ``H(r)``.
786 rot_profile: Azimuthal speed profile ``v_theta(r)``. The velocity is
787 projected along the cylindrical azimuthal direction at each
788 particle position. Mutually exclusive with ``velocity_field``.
789 cs_profile: Sound speed profile ``c_s(r)``. Evaluated at the cylindrical
790 radius of each particle. Required when the solver uses a locally
791 isothermal EOS. Mutually exclusive with ``cs_field``.
792 velocity_field: Velocity profile ``v(x, y, z)``. Mutually exclusive
793 with ``rot_profile``.
794 cs_field: Sound speed profile ``c_s(x, y, z)``. Required when the solver
795 uses a locally isothermal EOS. Mutually exclusive with ``cs_profile``.
796 random_seed: Seed for the Monte Carlo sampler.
797 init_h_factor: Multiplier applied to the smoothing length inferred from
798 the generated density. Defaults to ``0.8``.
801 Exactly one of ``velocity_field`` or ``rot_profile`` must be provided.
803 If the solver uses a locally isothermal EOS, exactly one of ``cs_field``
804 or ``cs_profile`` must be provided. Otherwise both sound-speed profiles
805 are ignored and a warning is emitted if either is supplied.
808 A setup node to pass to :py:meth:`apply_setup`.
811 "make_generator_from_context",
813 return self.make_generator_from_context(context_other);
820 return self.make_combiner_add(parent1, parent2);
823 "make_modifier_warp_disc",
830 return self.make_modifier_warp_disc(parent, Rwarp, Hwarp, inclination, posangle);
836 py::arg(
"inclination"),
837 py::arg(
"posangle") = 0.)
839 "make_modifier_custom_warp",
842 std::function<Tscal(Tscal)> inc_profile,
843 std::function<Tscal(Tscal)> psi_profile,
844 std::function<Tvec(Tscal)> k_profile) {
845 return self.make_modifier_custom_warp(parent, inc_profile, psi_profile, k_profile);
849 py::arg(
"inc_profile"),
850 py::arg(
"psi_profile"),
851 py::arg(
"k_profile"))
853 "make_modifier_offset",
857 Tvec offset_velocity) {
858 return self.make_modifier_add_offset(parent, offset_postion, offset_velocity);
862 py::arg(
"offset_position"),
863 py::arg(
"offset_velocity"))
865 "make_modifier_filter",
868 std::function<
bool(Tvec)> filter) {
869 return self.make_modifier_filter(parent, filter);
875 "make_modifier_split_part",
881 return self.make_modifier_split_part(parent, n_split, seed, h_scaling);
887 py::arg(
"h_scaling") = 0.6)
892 bool part_reordering,
893 std::optional<u32> gen_step,
894 std::optional<u32> insert_step,
895 std::optional<u64> msg_count_limit,
896 std::optional<u64> msg_size_limit,
897 std::optional<u64> max_msg_size,
900 bool speculative_balancing) {
902 return self.apply_setup_new(
911 speculative_balancing);
913 if (
bool(gen_step)) {
916 "SPHSetup",
"gen_step is ignored when using old setup"));
918 if (
bool(msg_count_limit)) {
921 "SPHSetup",
"msg_count_limit is ignored when using old setup"));
923 if (
bool(msg_size_limit)) {
926 "SPHSetup",
"msg_size_limit is ignored when using old setup"));
928 if (
bool(max_msg_size)) {
931 "SPHSetup",
"max_msg_size is ignored when using old setup"));
933 if (
bool(do_setup_log)) {
936 "SPHSetup",
"do_setup_log is ignored when using old setup"));
938 return self.apply_setup(setup, part_reordering, insert_step);
943 py::arg(
"part_reordering") =
true,
944 py::arg(
"gen_step") = std::nullopt,
945 py::arg(
"insert_step") = std::nullopt,
946 py::arg(
"msg_count_limit") = std::nullopt,
947 py::arg(
"rank_comm_size_limit") = std::nullopt,
948 py::arg(
"max_msg_size") = std::nullopt,
949 py::arg(
"do_setup_log") =
false,
950 py::arg(
"use_new_setup") =
true,
951 py::arg(
"speculative_balancing") =
false);
953 py::class_<T>(m, name_model.c_str())
955 return std::make_unique<T>(ctx);
957 .def(
"init", &T::init)
958 .def(
"init_scheduler", &T::init_scheduler)
961 "evolve_once_override_time",
962 &T::evolve_once_time_expl,
965 .def(
"evolve_once", &T::evolve_once)
968 [](T &self,
f64 target_time,
i32 niter_max,
f64 max_walltime) {
969 return self.evolve_until(target_time, niter_max, max_walltime);
971 py::arg(
"target_time"),
973 py::arg(
"niter_max") = -1,
974 py::arg(
"max_walltime") = -1)
975 .def(
"timestep", &T::timestep)
976 .def(
"set_cfl_cour", &T::set_cfl_cour, py::arg(
"cfl_cour"))
977 .def(
"set_cfl_force", &T::set_cfl_force, py::arg(
"cfl_force"))
978 .def(
"set_eta_sink", &T::set_eta_sink, py::arg(
"eta_sink"))
979 .def(
"set_particle_mass", &T::set_particle_mass, py::arg(
"gpart_mass"))
980 .def(
"get_particle_mass", &T::get_particle_mass)
981 .def(
"rho_h", &T::rho_h)
982 .def(
"get_hfact", &T::get_hfact)
989 "get_solver_dot_graph",
994 "get_box_dim_fcc_3d",
996 return self.get_box_dim_fcc_3d(dr, xcnt, ycnt, zcnt);
999 "get_box_dim_true_fcc_3d",
1001 return self.get_box_dim_true_fcc_3d(dr, xcnt, ycnt, zcnt);
1008 Get the dimensions of a periodic box holding a true FCC lattice of xcnt * ycnt * zcnt
1009 (see setup.make_generator_lattice_fcc). Unlike get_box_dim_fcc_3d (which actually
1010 describes a HCP lattice and is kept for backward compatibility), this throws if the box
1011 cannot be periodic: xcnt must be >= 2, ycnt even and zcnt a multiple of 3.
1014 "get_ideal_fcc_box",
1015 [](T &self,
f64 dr, f64_3 box_min, f64_3 box_max) {
1019 "The python function get_ideal_fcc_box is deprecated in the SPH model and "
1020 "will be removed at some point, replace it by "
1021 "shamrock.math.get_ideal_hcp_box"));
1022 return shammath::LatticeHCP<f64_3>::get_ideal_hcp_box(dr, {box_min, box_max});
1025 "get_ideal_hcp_box",
1026 [](T &self,
f64 dr, f64_3 box_min, f64_3 box_max) {
1030 "The python function get_ideal_hcp_box is deprecated in the SPH model and "
1031 "will be removed at some point, replace it by "
1032 "shamrock.math.get_ideal_hcp_box"));
1033 return shammath::LatticeHCP<f64_3>::get_ideal_hcp_box(dr, {box_min, box_max});
1036 "resize_simulation_box",
1037 [](T &self, f64_3 box_min, f64_3 box_max) {
1038 return self.resize_simulation_box({box_min, box_max});
1042 [](T &self, std::vector<f64_3> pos, std::vector<f64> hpart, std::vector<f64> upart) {
1043 return self.push_particle(pos, hpart, upart);
1046 "push_particle_mhd",
1048 std::vector<f64_3> pos,
1049 std::vector<f64> hpart,
1050 std::vector<f64> upart,
1051 std::vector<f64_3> B_on_rho,
1052 std::vector<f64> psi_on_ch) {
1053 return self.push_particle_mhd(pos, hpart, upart, B_on_rho, psi_on_ch);
1057 [](T &self,
f64 dr, f64_3 box_min, f64_3 box_max) {
1058 return self.add_cube_fcc_3d(dr, {box_min, box_max});
1062 [](T &self,
f64 dr, f64_3 box_min, f64_3 box_max) {
1063 return self.add_cube_hcp_3d(dr, {box_min, box_max});
1066 "add_cube_hcp_3d_v2",
1067 [](T &self,
f64 dr, f64_3 box_min, f64_3 box_max) {
1068 return self.add_cube_hcp_3d_v2(dr, {box_min, box_max});
1071 "add_disc_3d_keplerian",
1082 return self.add_cube_disc_3d(center, Npart, p, rho_0, m, r_in, r_out, q, cmass);
1096 return self.add_disc_3d(
1097 center, central_mass, Npart, r_in, r_out, disc_mass, p, H_r_in, q);
1112 self.add_big_disc_3d(
1122 std::mt19937{seed});
1123 return disc_mass / Npart;
1125 .def(
"get_total_part_count", &T::get_total_part_count)
1126 .def(
"total_mass_to_part_mass", &T::total_mass_to_part_mass)
1128 "set_value_in_a_box",
1130 const std::string &field_name,
1131 const std::string &field_type,
1132 const pybind11::object &value,
1136 if (field_type ==
"f64") {
1137 f64 val = value.cast<
f64>();
1138 self.set_value_in_a_box(field_name, val, {box_min, box_max}, ivar);
1139 }
else if (field_type ==
"f64_3") {
1140 f64_3 val = value.cast<f64_3>();
1141 self.set_value_in_a_box(field_name, val, {box_min, box_max}, ivar);
1144 "unknown field type");
1147 py::arg(
"field_name"),
1148 py::arg(
"field_type"),
1153 py::arg(
"ivar") = 0)
1155 "set_value_in_sphere",
1157 const std::string &field_name,
1158 const std::string &field_type,
1159 const pybind11::object &value,
1162 if (field_type ==
"f64") {
1163 f64 val = value.cast<
f64>();
1164 self.set_value_in_sphere(field_name, val, center, radius);
1165 }
else if (field_type ==
"f64_3") {
1166 f64_3 val = value.cast<f64_3>();
1167 self.set_value_in_sphere(field_name, val, center, radius);
1170 "unknown field type");
1174 "set_field_value_lambda_f64",
1176 std::string field_name,
1177 const std::function<
f64(Tvec)> pos_to_val,
1179 return self.template set_field_value_lambda<f64>(
1180 std::move(field_name), pos_to_val, offset);
1182 py::arg(
"field_name"),
1183 py::arg(
"pos_to_val"),
1184 py::arg(
"offset") = 0)
1186 "set_field_value_lambda_f64_3",
1188 std::string field_name,
1189 const std::function<f64_3(Tvec)> pos_to_val,
1191 return self.template set_field_value_lambda<f64_3>(
1192 std::move(field_name), pos_to_val, offset);
1194 py::arg(
"field_name"),
1195 py::arg(
"pos_to_val"),
1196 py::arg(
"offset") = 0)
1197 .def(
"overwrite_field_value_f64", &T::template overwrite_field_value<f64>)
1198 .def(
"overwrite_field_value_f64_3", &T::template overwrite_field_value<f64_3>)
1199 .def(
"remap_positions", &T::remap_positions)
1209 const std::string &field_name,
1210 const std::string &field_type,
1211 const pybind11::object &value,
1214 if (field_type ==
"f64") {
1215 f64 val = value.cast<
f64>();
1216 self.add_kernel_value(field_name, val, center, h_ker);
1217 }
else if (field_type ==
"f64_3") {
1218 f64_3 val = value.cast<f64_3>();
1219 self.add_kernel_value(field_name, val, center, h_ker);
1222 "unknown field type");
1227 [](T &self,
const std::string &field_name,
const std::string &field_type) {
1228 if (field_type ==
"f64") {
1229 return py::cast(self.template get_sum<f64>(field_name));
1230 }
else if (field_type ==
"f64_3") {
1231 return py::cast(self.template get_sum<f64_3>(field_name));
1234 "unknown field type");
1238 "get_closest_part_to",
1239 [](T &self, f64_3 pos) -> f64_3 {
1240 return self.get_closest_part_to(pos);
1243 "gen_default_config",
1245 return typename T::Solver::Config{};
1248 "get_current_config",
1250 return self.solver.solver_config;
1252 .def(
"set_solver_config", &T::set_solver_config)
1253 .def(
"add_sink", &T::add_sink)
1263 sink_dic[
"pos"] = sink.pos;
1264 sink_dic[
"velocity"] = sink.velocity;
1265 sink_dic[
"sph_acceleration"] = sink.sph_acceleration;
1266 sink_dic[
"ext_acceleration"] = sink.ext_acceleration;
1267 sink_dic[
"mass"] = sink.mass;
1268 sink_dic[
"angular_momentum"] = sink.angular_momentum;
1269 sink_dic[
"accretion_radius"] = sink.accretion_radius;
1270 list_out.append(sink_dic);
1275 auto &dust_mass = get_sink_accreted_dust_mass<Tvec>(sync);
1276 size_t nsink = list_out.size();
1277 size_t ndust = (nsink == 0) ? 0 : dust_mass.size() / nsink;
1278 for (
size_t i = 0; i < nsink; i++) {
1279 auto begin = dust_mass.begin() + i * ndust;
1280 list_out[i][
"accreted_dust_mass"]
1281 = std::vector<Tscal>(begin, begin + ndust);
1290 return self.solver.solver_config.unit_sys;
1295 const std::string &name,
1296 const std::string &field_type,
1297 const std::vector<Tvec> &positions,
1298 const std::optional<custom_getter_t> &custom_getter)
1299 -> std::variant<std::vector<f64>, std::vector<f64_3>> {
1300 if (custom_getter.has_value()) {
1301 if (!(name ==
"custom" && field_type ==
"f64")) {
1303 "custom_getter only available for name=custom and field_type=f64");
1307 if (field_type ==
"f64") {
1309 self.ctx, self.solver.solver_config, self.solver.storage);
1310 return render.compute_slice(name, positions, custom_getter).copy_to_stdvec();
1313 if (field_type ==
"f64_3") {
1315 self.ctx, self.solver.solver_config, self.solver.storage);
1316 return render.compute_slice(name, positions, std::nullopt).copy_to_stdvec();
1322 py::arg(
"field_type"),
1323 py::arg(
"positions"),
1324 py::arg(
"custom_getter") = std::nullopt)
1329 const std::vector<Tvec> &positions) -> std::vector<f64> {
1331 self.ctx, self.solver.solver_config, self.solver.storage);
1332 return render.compute_slice(field, positions).copy_to_stdvec();
1335 py::arg(
"positions"))
1340 const std::vector<Tvec> &positions) -> std::vector<f64_3> {
1342 self.ctx, self.solver.solver_config, self.solver.storage);
1343 return render.compute_slice(field, positions).copy_to_stdvec();
1346 py::arg(
"positions"))
1348 "render_column_integ",
1350 const std::string &name,
1351 const std::string &field_type,
1353 const std::optional<custom_getter_t> &custom_getter)
1354 -> std::variant<std::vector<f64>, std::vector<f64_3>> {
1355 if (custom_getter.has_value()) {
1356 if (!(name ==
"custom" && field_type ==
"f64")) {
1358 "custom_getter only available for name=custom and field_type=f64");
1362 if (field_type ==
"f64") {
1364 self.ctx, self.solver.solver_config, self.solver.storage);
1365 return render.compute_column_integ(name, rays, custom_getter).copy_to_stdvec();
1368 if (field_type ==
"f64_3") {
1370 self.ctx, self.solver.solver_config, self.solver.storage);
1371 return render.compute_column_integ(name, rays, std::nullopt).copy_to_stdvec();
1377 py::arg(
"field_type"),
1379 py::arg(
"custom_getter") = std::nullopt)
1381 "render_column_integ",
1386 self.ctx, self.solver.solver_config, self.solver.storage);
1387 return render.compute_column_integ(field, rays).copy_to_stdvec();
1392 "render_column_integ",
1397 self.ctx, self.solver.solver_config, self.solver.storage);
1398 return render.compute_column_integ(field, rays).copy_to_stdvec();
1405 const std::string &name,
1406 const std::string &field_type,
1407 const std::optional<custom_getter_t> &custom_getter)
1411 if (custom_getter.has_value()) {
1412 if (!(name ==
"custom" && field_type ==
"f64")) {
1414 "custom_getter only available for name=custom and field_type=f64");
1418 if (field_type ==
"f64") {
1420 self.ctx, self.solver.solver_config, self.solver.storage);
1421 return render_field_getter.build_field(name, custom_getter);
1424 if (field_type ==
"f64_3") {
1426 self.ctx, self.solver.solver_config, self.solver.storage);
1427 return render_field_getter.build_field(name, custom_getter);
1433 py::arg(
"field_type"),
1434 py::arg(
"custom_getter") = std::nullopt)
1436 "render_azymuthal_integ",
1438 const std::string &name,
1439 const std::string &field_type,
1441 const std::optional<custom_getter_t> &custom_getter)
1442 -> std::variant<std::vector<f64>, std::vector<f64_3>> {
1443 if (custom_getter.has_value()) {
1444 if (!(name ==
"custom" && field_type ==
"f64")) {
1446 "custom_getter only available for name=custom and field_type=f64");
1450 if (field_type ==
"f64") {
1452 self.ctx, self.solver.solver_config, self.solver.storage);
1453 return render.compute_azymuthal_integ(name, ring_rays, custom_getter)
1457 if (field_type ==
"f64_3") {
1459 self.ctx, self.solver.solver_config, self.solver.storage);
1460 return render.compute_azymuthal_integ(name, ring_rays, std::nullopt)
1467 py::arg(
"field_type"),
1468 py::arg(
"ring_rays"),
1469 py::arg(
"custom_getter") = std::nullopt)
1471 "render_azymuthal_integ",
1476 self.ctx, self.solver.solver_config, self.solver.storage);
1477 return render.compute_azymuthal_integ(field, ring_rays).copy_to_stdvec();
1480 py::arg(
"ring_rays"))
1482 "render_azymuthal_integ",
1487 self.ctx, self.solver.solver_config, self.solver.storage);
1488 return render.compute_azymuthal_integ(field, ring_rays).copy_to_stdvec();
1491 py::arg(
"ring_rays"))
1493 "render_cartesian_slice",
1495 const std::string &name,
1496 const std::string &field_type,
1502 const std::optional<custom_getter_t> &custom_getter)
1503 -> std::variant<py::array_t<Tscal>> {
1504 if (custom_getter.has_value()) {
1505 if (!(name ==
"custom" && field_type ==
"f64")) {
1507 "custom_getter only available for name=custom and field_type=f64");
1511 if (field_type ==
"f64") {
1512 py::array_t<Tscal> ret({ny, nx});
1515 self.ctx, self.solver.solver_config, self.solver.storage);
1517 std::vector<f64> slice
1519 .compute_slice(name, center, delta_x, delta_y, nx, ny, custom_getter)
1522 for (
u32 iy = 0; iy < ny; iy++) {
1523 for (
u32 ix = 0; ix < nx; ix++) {
1524 ret.mutable_at(iy, ix) = slice[ix + nx * iy];
1531 if (field_type ==
"f64_3") {
1532 py::array_t<Tscal> ret({ny, nx, 3_u32});
1535 self.ctx, self.solver.solver_config, self.solver.storage);
1537 std::vector<f64_3> slice
1538 = render.compute_slice(name, center, delta_x, delta_y, nx, ny, std::nullopt)
1541 for (
u32 iy = 0; iy < ny; iy++) {
1542 for (
u32 ix = 0; ix < nx; ix++) {
1543 ret.mutable_at(iy, ix, 0) = slice[ix + nx * iy][0];
1544 ret.mutable_at(iy, ix, 1) = slice[ix + nx * iy][1];
1545 ret.mutable_at(iy, ix, 2) = slice[ix + nx * iy][2];
1553 return py::array_t<Tscal>({nx, ny});
1556 py::arg(
"field_type"),
1562 py::arg(
"custom_getter") = std::nullopt)
1564 "render_cartesian_slice",
1571 u32 ny) -> py::array_t<Tscal> {
1572 py::array_t<Tscal> ret({ny, nx});
1575 self.ctx, self.solver.solver_config, self.solver.storage);
1577 std::vector<f64> slice
1578 = render.compute_slice(field, center, delta_x, delta_y, nx, ny)
1581 for (
u32 iy = 0; iy < ny; iy++) {
1582 for (
u32 ix = 0; ix < nx; ix++) {
1583 ret.mutable_at(iy, ix) = slice[ix + nx * iy];
1596 "render_cartesian_slice",
1603 u32 ny) -> py::array_t<Tscal> {
1604 py::array_t<Tscal> ret({ny, nx, 3_u32});
1607 self.ctx, self.solver.solver_config, self.solver.storage);
1609 std::vector<f64_3> slice
1610 = render.compute_slice(field, center, delta_x, delta_y, nx, ny)
1613 for (
u32 iy = 0; iy < ny; iy++) {
1614 for (
u32 ix = 0; ix < nx; ix++) {
1615 ret.mutable_at(iy, ix, 0) = slice[ix + nx * iy][0];
1616 ret.mutable_at(iy, ix, 1) = slice[ix + nx * iy][1];
1617 ret.mutable_at(iy, ix, 2) = slice[ix + nx * iy][2];
1630 "render_cartesian_column_integ",
1632 const std::string &name,
1633 const std::string &field_type,
1639 const std::optional<custom_getter_t> &custom_getter)
1640 -> std::variant<py::array_t<Tscal>> {
1641 if (custom_getter.has_value()) {
1642 if (!(name ==
"custom" && field_type ==
"f64")) {
1644 "custom_getter only available for name=custom and field_type=f64");
1648 if (field_type ==
"f64") {
1649 py::array_t<Tscal> ret({ny, nx});
1652 self.ctx, self.solver.solver_config, self.solver.storage);
1654 std::vector<f64> slice
1656 .compute_column_integ(
1657 name, center, delta_x, delta_y, nx, ny, custom_getter)
1660 for (
u32 iy = 0; iy < ny; iy++) {
1661 for (
u32 ix = 0; ix < nx; ix++) {
1662 ret.mutable_at(iy, ix) = slice[ix + nx * iy];
1669 if (field_type ==
"f64_3") {
1670 py::array_t<Tscal> ret({ny, nx, 3_u32});
1673 self.ctx, self.solver.solver_config, self.solver.storage);
1675 std::vector<f64_3> slice
1677 .compute_column_integ(
1678 name, center, delta_x, delta_y, nx, ny, std::nullopt)
1681 for (
u32 iy = 0; iy < ny; iy++) {
1682 for (
u32 ix = 0; ix < nx; ix++) {
1683 ret.mutable_at(iy, ix, 0) = slice[ix + nx * iy][0];
1684 ret.mutable_at(iy, ix, 1) = slice[ix + nx * iy][1];
1685 ret.mutable_at(iy, ix, 2) = slice[ix + nx * iy][2];
1693 return py::array_t<Tscal>({nx, ny});
1696 py::arg(
"field_type"),
1702 py::arg(
"custom_getter") = std::nullopt)
1704 "render_cartesian_column_integ",
1711 u32 ny) -> py::array_t<Tscal> {
1712 py::array_t<Tscal> ret({ny, nx});
1715 self.ctx, self.solver.solver_config, self.solver.storage);
1717 std::vector<f64> slice
1718 = render.compute_column_integ(field, center, delta_x, delta_y, nx, ny)
1721 for (
u32 iy = 0; iy < ny; iy++) {
1722 for (
u32 ix = 0; ix < nx; ix++) {
1723 ret.mutable_at(iy, ix) = slice[ix + nx * iy];
1736 "render_cartesian_column_integ",
1743 u32 ny) -> py::array_t<Tscal> {
1744 py::array_t<Tscal> ret({ny, nx, 3_u32});
1747 self.ctx, self.solver.solver_config, self.solver.storage);
1749 std::vector<f64_3> slice
1750 = render.compute_column_integ(field, center, delta_x, delta_y, nx, ny)
1753 for (
u32 iy = 0; iy < ny; iy++) {
1754 for (
u32 ix = 0; ix < nx; ix++) {
1755 ret.mutable_at(iy, ix, 0) = slice[ix + nx * iy][0];
1756 ret.mutable_at(iy, ix, 1) = slice[ix + nx * iy][1];
1757 ret.mutable_at(iy, ix, 2) = slice[ix + nx * iy][2];
1770 "gen_config_from_phantom_dump",
1771 [](T &self,
PhantomDump &dump,
bool bypass_error) {
1772 return self.gen_config_from_phantom_dump(dump, bypass_error);
1775 py::arg(
"bypass_error") =
false,
1777 This function generate a shamrock sph solver config from a phantom dump
1782 bypass_error = false (default) bypass any error in the config
1785 "init_from_phantom_dump",
1786 [](T &self,
PhantomDump &dump, Tscal hpart_fact_load) {
1787 self.init_from_phantom_dump(dump, hpart_fact_load);
1790 py::arg(
"hpart_fact_load") = 1.0)
1792 "make_phantom_dump",
1794 return self.make_phantom_dump();
1796 .def(
"do_vtk_dump", &T::do_vtk_dump)
1797 .def(
"set_debug_dump", &T::set_debug_dump)
1798 .def(
"solver_logs_last_rate", &T::solver_logs_last_rate)
1799 .def(
"solver_logs_last_obj_count", &T::solver_logs_last_obj_count)
1801 "solver_logs_last_system_metrics",
1803 auto system_metrics = self.solver.solve_logs.get_last_system_metrics();
1805 ret[
"duration"] = system_metrics.wall_time;
1806 if (system_metrics.rank_energy_consummed.has_value()) {
1807 ret[
"rank_energy_consummed"] = system_metrics.rank_energy_consummed.value();
1809 if (system_metrics.gpu_energy_consummed.has_value()) {
1810 ret[
"gpu_energy_consummed"] = system_metrics.gpu_energy_consummed.value();
1812 if (system_metrics.cpu_energy_consummed.has_value()) {
1813 ret[
"cpu_energy_consummed"] = system_metrics.cpu_energy_consummed.value();
1815 if (system_metrics.dram_energy_consummed.has_value()) {
1816 ret[
"dram_energy_consummed"] = system_metrics.dram_energy_consummed.value();
1820 .def(
"solver_logs_cumulated_step_time", &T::solver_logs_cumulated_step_time)
1821 .def(
"solver_logs_reset_cumulated_step_time", &T::solver_logs_reset_cumulated_step_time)
1822 .def(
"solver_logs_step_count", &T::solver_logs_step_count)
1823 .def(
"solver_logs_reset_step_count", &T::solver_logs_reset_step_count)
1827 return self.get_time();
1832 return self.get_dt_sph();
1838 for (
auto &[name, value] : self.solver.get_last_cfl_dts()) {
1839 ret[py::str(name)] = value;
1844 Get the last CFL timesteps stored in the synchronized edges.
1846 Returns a dict keyed by synchronized edge name:
1848 - ``"cfl_dt"``: the global CFL timestep of the last step (min over all CFL conditions).
1849 - ``"cfl_dt_<type>"`` (e.g. ``"cfl_dt_courant"``, ``"cfl_dt_force"``,
1850 ``"cfl_dt_sink_sink"``): the CFL timestep of each condition enabled in the solver
1851 config, only present when ``show_cfl_detail`` is enabled.
1853 All values are 0 before the first step. A condition that was not evaluated during the
1854 last step (e.g. ``"cfl_dt_sink_sink"`` with no sinks) keeps its previous value.
1858 [](T &self, Tscal t) {
1859 return self.set_time(t);
1863 [](T &self, Tscal dt) {
1864 return self.set_next_dt(dt);
1868 [](T &self,
f64 dt) {
1869 self.set_next_dt(dt);
1872 "set_cfl_multipler",
1873 [](T &self, Tscal lambda) {
1874 return self.set_cfl_multipler(lambda);
1878 "set_cfl_mult_stiffness",
1879 [](T &self, Tscal cstiff) {
1880 return self.solver.solver_config.set_cfl_mult_stiffness(cstiff);
1884 "change_htolerance",
1885 [](T &self, Tscal in) {
1888 ".change_htolerance(val) is deprecated,\n"
1889 " -> calling this is replaced internally by "
1890 ".change_htolerances(coarse=val, fine=min(val, 1.1))\n"
1892 "https://shamrock-code.github.io/Shamrock/sphinx/user_guide/sph/"
1893 "smoothing_length_tolerance.html"););
1894 self.change_htolerances(in, std::min(in, (Tscal) 1.1));
1897 "change_htolerances",
1898 [](T &self, Tscal coarse, Tscal fine) {
1899 self.change_htolerances(coarse, fine);
1905 "make_analysis_sodtube",
1913 return std::make_unique<TAnalysisSodTube>(
1915 self.solver.solver_config,
1916 self.solver.storage,
1925 py::arg(
"direction"),
1926 py::arg(
"time_val"),
1931 "make_analysis_disc",
1933 return std::make_unique<TAnalysisDisc>(
1934 self.ctx, self.solver.solver_config, self.solver.storage);
1936 .def(
"load_from_dump", &T::load_from_dump)
1937 .def(
"dump", &T::dump)
1938 .def(
"get_setup", &T::get_setup)
1940 "get_patch_transform",
1943 return sched.get_patch_transform<Tvec>();
1945 .def(
"apply_momentum_offset", &T::apply_momentum_offset)
1946 .def(
"apply_position_offset", &T::apply_position_offset)
1948 "add_timestep_callback",
1950 std::optional<std::function<
void(
void)>> step_begin_callback,
1951 std::optional<std::function<
void(
void)>> step_end_callback) {
1952 self.solver.timestep_callbacks.push_back(
1953 {std::move(step_begin_callback), std::move(step_end_callback)});
1956 py::arg(
"step_begin") = std::nullopt,
1957 py::arg(
"step_end") = std::nullopt);
1960template<
class Tvec,
template<
class>
class SPHKernel>
1961void add_analysisBarycenter_instance(py::module &m,
const std::string &name_model) {
1964 using Tscal = shambase::VecComponent<Tvec>;
1968 py::class_<modules::AnalysisBarycenter<Tvec, SPHKernel>>(m, name_model.c_str())
1969 .def(py::init([](T &model) {
1970 return std::make_unique<modules::AnalysisBarycenter<Tvec, SPHKernel>>(model);
1973 auto result = self.get_barycenter();
1974 return py::make_tuple(result.barycenter, result.mass_disc);
1978template<
class Tvec,
template<
class>
class SPHKernel>
1979void add_analysisEnergyKinetic_instance(py::module &m,
const std::string &name_model) {
1982 using Tscal = shambase::VecComponent<Tvec>;
1985 py::class_<modules::AnalysisEnergyKinetic<Tvec, SPHKernel>>(m, name_model.c_str())
1986 .def(py::init([](T &model) {
1987 return std::make_unique<modules::AnalysisEnergyKinetic<Tvec, SPHKernel>>(model);
1990 return self.get_kinetic_energy();
1994template<
class Tvec,
template<
class>
class SPHKernel>
1995void add_analysisEnergyPotential_instance(py::module &m,
const std::string &name_model) {
1998 using Tscal = shambase::VecComponent<Tvec>;
2001 py::class_<modules::AnalysisEnergyPotential<Tvec, SPHKernel>>(m, name_model.c_str())
2002 .def(py::init([](T &model) {
2003 return std::make_unique<modules::AnalysisEnergyPotential<Tvec, SPHKernel>>(model);
2006 return self.get_potential_energy();
2010template<
class Tvec,
template<
class>
class SPHKernel>
2011void add_analysisTotalMomentum_instance(py::module &m,
const std::string &name_model) {
2014 using Tscal = shambase::VecComponent<Tvec>;
2017 py::class_<modules::AnalysisTotalMomentum<Tvec, SPHKernel>>(m, name_model.c_str())
2018 .def(py::init([](T &model) {
2019 return std::make_unique<modules::AnalysisTotalMomentum<Tvec, SPHKernel>>(model);
2022 return self.get_total_momentum();
2026template<
class Tvec,
template<
class>
class SPHKernel>
2027void add_analysisAngularMomentum_instance(py::module &m,
const std::string &name_model) {
2030 using Tscal = shambase::VecComponent<Tvec>;
2033 py::class_<modules::AnalysisAngularMomentum<Tvec, SPHKernel>>(m, name_model.c_str())
2034 .def(py::init([](T &model) {
2035 return std::make_unique<modules::AnalysisAngularMomentum<Tvec, SPHKernel>>(model);
2038 return self.get_angular_momentum();
2042template<
class Tvec,
template<
class>
class SPHKernel>
2043void add_analysisDustMass_instance(py::module &m,
const std::string &name_model) {
2046 using Tscal = shambase::VecComponent<Tvec>;
2049 py::class_<modules::AnalysisDustMass<Tvec, SPHKernel>>(m, name_model.c_str())
2050 .def(py::init([](T &model) {
2051 return std::make_unique<modules::AnalysisDustMass<Tvec, SPHKernel>>(model);
2054 return self.get_dust_mass();
2060template<
class Analysis,
typename Tvec,
template<
class>
class SPHKernel>
2062 return Analysis(model);
2065template<
template<
class,
template<
class>
class>
class Analysis>
2066void register_analysis_impl_for_each_kernel(py::module &msph, const char *name_class) {
2067 using namespace shammodels::sph;
2069 using SPHModel_f64_3_M4 = shammodels::sph::Model<f64_3, shammath::M4>;
2070 using SPHModel_f64_3_M6 = shammodels::sph::Model<f64_3, shammath::M6>;
2071 using SPHModel_f64_3_M8 = shammodels::sph::Model<f64_3, shammath::M8>;
2073 using SPHModel_f64_3_C2 = shammodels::sph::Model<f64_3, shammath::C2>;
2074 using SPHModel_f64_3_C4 = shammodels::sph::Model<f64_3, shammath::C4>;
2075 using SPHModel_f64_3_C6 = shammodels::sph::Model<f64_3, shammath::C6>;
2079 [](SPHModel_f64_3_M4 &model) {
2080 return analysis_impl<Analysis<f64_3, shammath::M4>>(model);
2087 [](SPHModel_f64_3_M6 &model) {
2088 return analysis_impl<Analysis<f64_3, shammath::M6>>(model);
2095 [](SPHModel_f64_3_M8 &model) {
2096 return analysis_impl<Analysis<f64_3, shammath::M8>>(model);
2103 [](SPHModel_f64_3_C2 &model) {
2104 return analysis_impl<Analysis<f64_3, shammath::C2>>(model);
2111 [](SPHModel_f64_3_C4 &model) {
2112 return analysis_impl<Analysis<f64_3, shammath::C4>>(model);
2119 [](SPHModel_f64_3_C6 &model) {
2120 return analysis_impl<Analysis<f64_3, shammath::C6>>(model);
2127 auto &m = root_module;
2129 py::module msph = m.def_submodule(
"model_sph",
"Shamrock sph solver");
2131 py::class_<shamrock::PatchDataLazyGetter>(m,
"PatchDataLazyGetter")
2132 .def(
"__getitem__", &shamrock::PatchDataLazyGetter::get_item);
2134 py::class_<EvolveUntilResults>(m,
"EvolveUntilResults")
2135 .def_readwrite(
"reach_target_time", &EvolveUntilResults::reach_target_time)
2136 .def_readwrite(
"reach_niter_max", &EvolveUntilResults::reach_niter_max)
2137 .def_readwrite(
"reach_max_walltime", &EvolveUntilResults::reach_max_walltime)
2138 .def_readwrite(
"iter_count", &EvolveUntilResults::iter_count)
2140 return sham::format(
2141 "EvolveUntilResults(reach_target_time={}, reach_niter_max={}, "
2142 "reach_max_walltime={}, iter_count={})",
2143 self.reach_target_time,
2144 self.reach_niter_max,
2145 self.reach_max_walltime,
2151 add_instance<f64_3, shammath::M4>(msph,
"SPHModel_f64_3_M4_SolverConfig",
"SPHModel_f64_3_M4");
2152 add_instance<f64_3, shammath::M6>(msph,
"SPHModel_f64_3_M6_SolverConfig",
"SPHModel_f64_3_M6");
2153 add_instance<f64_3, shammath::M8>(msph,
"SPHModel_f64_3_M8_SolverConfig",
"SPHModel_f64_3_M8");
2155 add_instance<f64_3, shammath::C2>(msph,
"SPHModel_f64_3_C2_SolverConfig",
"SPHModel_f64_3_C2");
2156 add_instance<f64_3, shammath::C4>(msph,
"SPHModel_f64_3_C4_SolverConfig",
"SPHModel_f64_3_C4");
2157 add_instance<f64_3, shammath::C6>(msph,
"SPHModel_f64_3_C6_SolverConfig",
"SPHModel_f64_3_C6");
2159 using VariantSPHModelBind = std::variant<
2160 std::unique_ptr<Model<f64_3, shammath::M4>>,
2161 std::unique_ptr<Model<f64_3, shammath::M6>>,
2162 std::unique_ptr<Model<f64_3, shammath::M8>>,
2163 std::unique_ptr<Model<f64_3, shammath::C2>>,
2164 std::unique_ptr<Model<f64_3, shammath::C4>>,
2165 std::unique_ptr<Model<f64_3, shammath::C6>>>;
2170 const std::string &vector_type,
2171 const std::string &kernel) -> VariantSPHModelBind {
2172 VariantSPHModelBind ret;
2174 if (vector_type ==
"f64_3" && kernel ==
"M4") {
2175 ret = std::make_unique<Model<f64_3, shammath::M4>>(ctx);
2176 }
else if (vector_type ==
"f64_3" && kernel ==
"M6") {
2177 ret = std::make_unique<Model<f64_3, shammath::M6>>(ctx);
2178 }
else if (vector_type ==
"f64_3" && kernel ==
"M8") {
2179 ret = std::make_unique<Model<f64_3, shammath::M8>>(ctx);
2180 }
else if (vector_type ==
"f64_3" && kernel ==
"C2") {
2181 ret = std::make_unique<Model<f64_3, shammath::C2>>(ctx);
2182 }
else if (vector_type ==
"f64_3" && kernel ==
"C4") {
2183 ret = std::make_unique<Model<f64_3, shammath::C4>>(ctx);
2184 }
else if (vector_type ==
"f64_3" && kernel ==
"C6") {
2185 ret = std::make_unique<Model<f64_3, shammath::C6>>(ctx);
2188 "unknown combination of representation and kernel");
2195 py::arg(
"vector_type"),
2196 py::arg(
"sph_kernel"));
2200 std::shared_ptr<shammodels::sph::modules::ISPHSetupNode>>(msph,
"ISPHSetupNode")
2201 .def(
"get_dot", [](std::shared_ptr<shammodels::sph::modules::ISPHSetupNode> &self) {
2202 return self->get_dot();
2205 py::class_<shammodels::sph::TimestepLog>(msph,
"TimestepLog")
2207 .def_readwrite(
"rank", &shammodels::sph::TimestepLog::rank)
2208 .def_readwrite(
"rate", &shammodels::sph::TimestepLog::rate)
2209 .def_readwrite(
"npart", &shammodels::sph::TimestepLog::npart)
2210 .def_readwrite(
"tcompute", &shammodels::sph::TimestepLog::tcompute)
2211 .def(
"rate_sum", &shammodels::sph::TimestepLog::rate_sum)
2212 .def(
"npart_sum", &shammodels::sph::TimestepLog::npart_sum);
2214 add_analysisBarycenter_instance<f64_3, shammath::M4>(msph,
"AnalysisBarycenter_f64_3_M4");
2215 add_analysisBarycenter_instance<f64_3, shammath::M6>(msph,
"AnalysisBarycenter_f64_3_M6");
2216 add_analysisBarycenter_instance<f64_3, shammath::M8>(msph,
"AnalysisBarycenter_f64_3_M8");
2218 add_analysisBarycenter_instance<f64_3, shammath::C2>(msph,
"AnalysisBarycenter_f64_3_C2");
2219 add_analysisBarycenter_instance<f64_3, shammath::C4>(msph,
"AnalysisBarycenter_f64_3_C4");
2220 add_analysisBarycenter_instance<f64_3, shammath::C6>(msph,
"AnalysisBarycenter_f64_3_C6");
2222 add_analysisEnergyKinetic_instance<f64_3, shammath::M4>(msph,
"AnalysisEnergyKinetic_f64_3_M4");
2223 add_analysisEnergyKinetic_instance<f64_3, shammath::M6>(msph,
"AnalysisEnergyKinetic_f64_3_M6");
2224 add_analysisEnergyKinetic_instance<f64_3, shammath::M8>(msph,
"AnalysisEnergyKinetic_f64_3_M8");
2226 add_analysisEnergyKinetic_instance<f64_3, shammath::C2>(msph,
"AnalysisEnergyKinetic_f64_3_C2");
2227 add_analysisEnergyKinetic_instance<f64_3, shammath::C4>(msph,
"AnalysisEnergyKinetic_f64_3_C4");
2228 add_analysisEnergyKinetic_instance<f64_3, shammath::C6>(msph,
"AnalysisEnergyKinetic_f64_3_C6");
2230 add_analysisEnergyPotential_instance<f64_3, shammath::M4>(
2231 msph,
"AnalysisEnergyPotential_f64_3_M4");
2232 add_analysisEnergyPotential_instance<f64_3, shammath::M6>(
2233 msph,
"AnalysisEnergyPotential_f64_3_M6");
2234 add_analysisEnergyPotential_instance<f64_3, shammath::M8>(
2235 msph,
"AnalysisEnergyPotential_f64_3_M8");
2237 add_analysisEnergyPotential_instance<f64_3, shammath::C2>(
2238 msph,
"AnalysisEnergyPotential_f64_3_C2");
2239 add_analysisEnergyPotential_instance<f64_3, shammath::C4>(
2240 msph,
"AnalysisEnergyPotential_f64_3_C4");
2241 add_analysisEnergyPotential_instance<f64_3, shammath::C6>(
2242 msph,
"AnalysisEnergyPotential_f64_3_C6");
2244 add_analysisTotalMomentum_instance<f64_3, shammath::M4>(msph,
"AnalysisTotalMomentum_f64_3_M4");
2245 add_analysisTotalMomentum_instance<f64_3, shammath::M6>(msph,
"AnalysisTotalMomentum_f64_3_M6");
2246 add_analysisTotalMomentum_instance<f64_3, shammath::M8>(msph,
"AnalysisTotalMomentum_f64_3_M8");
2248 add_analysisTotalMomentum_instance<f64_3, shammath::C2>(msph,
"AnalysisTotalMomentum_f64_3_C2");
2249 add_analysisTotalMomentum_instance<f64_3, shammath::C4>(msph,
"AnalysisTotalMomentum_f64_3_C4");
2250 add_analysisTotalMomentum_instance<f64_3, shammath::C6>(msph,
"AnalysisTotalMomentum_f64_3_C6");
2252 add_analysisAngularMomentum_instance<f64_3, shammath::M4>(
2253 msph,
"AnalysisAngularMomentum_f64_3_M4");
2254 add_analysisAngularMomentum_instance<f64_3, shammath::M6>(
2255 msph,
"AnalysisAngularMomentum_f64_3_M6");
2256 add_analysisAngularMomentum_instance<f64_3, shammath::M8>(
2257 msph,
"AnalysisAngularMomentum_f64_3_M8");
2259 add_analysisAngularMomentum_instance<f64_3, shammath::C2>(
2260 msph,
"AnalysisAngularMomentum_f64_3_C2");
2261 add_analysisAngularMomentum_instance<f64_3, shammath::C4>(
2262 msph,
"AnalysisAngularMomentum_f64_3_C4");
2263 add_analysisAngularMomentum_instance<f64_3, shammath::C6>(
2264 msph,
"AnalysisAngularMomentum_f64_3_C6");
2266 register_analysis_impl_for_each_kernel<modules::AnalysisBarycenter>(msph,
"analysisBarycenter");
2267 register_analysis_impl_for_each_kernel<modules::AnalysisEnergyKinetic>(
2268 msph,
"analysisEnergyKinetic");
2269 register_analysis_impl_for_each_kernel<modules::AnalysisEnergyPotential>(
2270 msph,
"analysisEnergyPotential");
2271 register_analysis_impl_for_each_kernel<modules::AnalysisTotalMomentum>(
2272 msph,
"analysisTotalMomentum");
2273 register_analysis_impl_for_each_kernel<modules::AnalysisAngularMomentum>(
2274 msph,
"analysisAngularMomentum");
2276 add_analysisDustMass_instance<f64_3, shammath::M4>(msph,
"AnalysisDustMass_f64_3_M4");
2277 add_analysisDustMass_instance<f64_3, shammath::M6>(msph,
"AnalysisDustMass_f64_3_M6");
2278 add_analysisDustMass_instance<f64_3, shammath::M8>(msph,
"AnalysisDustMass_f64_3_M8");
2280 add_analysisDustMass_instance<f64_3, shammath::C2>(msph,
"AnalysisDustMass_f64_3_C2");
2281 add_analysisDustMass_instance<f64_3, shammath::C4>(msph,
"AnalysisDustMass_f64_3_C4");
2282 add_analysisDustMass_instance<f64_3, shammath::C6>(msph,
"AnalysisDustMass_f64_3_C6");
2284 register_analysis_impl_for_each_kernel<modules::AnalysisDustMass>(msph,
"analysisDustMass");
AnalysisAngularMomentum class.
AnalysisBarycenter class with one method AnalysisBarycenter.get_barycenter().
AnalysisEnergyKinetic class with one method AnalysisEnergyKinetic.get_kinetic_energy().
AnalysisEnergyPotential class with one method AnalysisEnergyPotential.get_potential_energy().
AnalysisTotalMomentum class with one method AnalysisTotalMomentum.get_total_momentum().
Compute the gravitational wave quadrupole. Based on Toscani et. al. 2021.
double f64
Alias for double.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
std::uint16_t u16
16 bit unsigned integer
std::int32_t i32
32 bit integer
static constexpr Tscal default_init_h_factor
This class is an interface that all SPH setup nodes must implement. It describe an operation associat...
Lazily fetches a single named field of a patch as a numpy array, on demand.
constexpr Unit k
Boltzmann constant.
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.
T & get_check_ref(const std::unique_ptr< T > &ptr, SourceLocation loc=SourceLocation())
Takes a std::unique_ptr and returns a reference to the object it holds. It throws a std::runtime_erro...
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
std::shared_ptr< ISPHSetupNode > SetupNodePtr
Alias for a shared pointer to an ISPHSetupNode.
namespace for the sph model
std::vector< SinkParticle< Tvec > > to_sink_particles(const SinkEdges< Tvec > &e)
Build an AoS sink list from the current SoA edges (Python API / dump helpers).
SinkEdges< Tvec > get_sink_edges(shamrock::solvergraph::SolverGraphSerializable &sync)
Fetch mutable references to the sink SoA synchronized edges.
bool has_sink_accreted_dust_mass(shamrock::solvergraph::SolverGraphSerializable &sync)
Whether the per species accreted dust mass sink edge is registered.
Pybind11 include and definitions.
#define ON_PYTHON_INIT
Register a Python module init function using static initialization.
void warn_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Utilities to convert JSON objects to Python objects and vice versa. TODO: try to convert directly wit...
Helpers to access SPH sink particles stored as SoA synchronized data edges.
Ray representation for intersection testing.
Ring ray representation for intersection testing.
Class representing a Phantom dump file.
Functions related to the MPI communicator.
#define ON_RANK_0(x)
Macro to execute code only on rank 0.