Note
Go to the end to download the full example code.
CI test: external forces point mass accretion#
Creates a tiny SPH setup with a few point mass external forces with an accretion radius, evolves, dumps, reloads into a fresh context, evolves again, and checks the resulting particle count and field sums.
SPH setup: generating particles ...
SPH setup: Nstep = 0 ( 0.0e+00 ) Ntotal = 0 ( 0.0e+00 rank min = 0.0e+00 max = 0.0e+00) rate = 0.000000e+00 N.s^-1
SPH setup: Nstep = 70 ( 7.0e+01 ) Ntotal = 70 ( 7.0e+01 rank min = 9.0e+04 max = 7.0e+01) rate = 7.000000e+01 N.s^-1
SPH setup: Nstep = 106 ( 1.1e+02 ) Ntotal = 176 ( 1.8e+02 rank min = 1.5e+05 max = 1.8e+02) rate = 1.760000e+02 N.s^-1
SPH setup: Nstep = 118 ( 1.2e+02 ) Ntotal = 294 ( 2.9e+02 rank min = 1.8e+05 max = 2.9e+02) rate = 2.940000e+02 N.s^-1
SPH setup: Nstep = 109 ( 1.1e+02 ) Ntotal = 403 ( 4.0e+02 rank min = 4.7e+05 max = 4.0e+02) rate = 4.030000e+02 N.s^-1
SPH setup: Nstep = 110 ( 1.1e+02 ) Ntotal = 513 ( 5.1e+02 rank min = 1.5e+05 max = 5.1e+02) rate = 5.130000e+02 N.s^-1
SPH setup: Nstep = 106 ( 1.1e+02 ) Ntotal = 619 ( 6.2e+02 rank min = 7.2e+05 max = 6.2e+02) rate = 6.190000e+02 N.s^-1
SPH setup: Nstep = 118 ( 1.2e+02 ) Ntotal = 737 ( 7.4e+02 rank min = 7.2e+05 max = 7.4e+02) rate = 7.370000e+02 N.s^-1
SPH setup: Nstep = 110 ( 1.1e+02 ) Ntotal = 847 ( 8.5e+02 rank min = 1.5e+05 max = 8.5e+02) rate = 8.470000e+02 N.s^-1
SPH setup: Nstep = 109 ( 1.1e+02 ) Ntotal = 956 ( 9.6e+02 rank min = 5.7e+05 max = 9.6e+02) rate = 9.560000e+02 N.s^-1
SPH setup: Nstep = 115 ( 1.2e+02 ) Ntotal = 1071 ( 1.1e+03 rank min = 6.5e+05 max = 1.1e+03) rate = 1.071000e+03 N.s^-1
SPH setup: Nstep = 121 ( 1.2e+02 ) Ntotal = 1192 ( 1.2e+03 rank min = 5.6e+05 max = 1.2e+03) rate = 1.192000e+03 N.s^-1
SPH setup: Nstep = 118 ( 1.2e+02 ) Ntotal = 1310 ( 1.3e+03 rank min = 1.4e+05 max = 1.3e+03) rate = 1.310000e+03 N.s^-1
SPH setup: Nstep = 119 ( 1.2e+02 ) Ntotal = 1429 ( 1.4e+03 rank min = 7.3e+05 max = 1.4e+03) rate = 1.429000e+03 N.s^-1
SPH setup: Nstep = 121 ( 1.2e+02 ) Ntotal = 1550 ( 1.6e+03 rank min = 7.6e+05 max = 1.6e+03) rate = 1.550000e+03 N.s^-1
SPH setup: Nstep = 121 ( 1.2e+02 ) Ntotal = 1671 ( 1.7e+03 rank min = 7.7e+05 max = 1.7e+03) rate = 1.671000e+03 N.s^-1
SPH setup: Nstep = 118 ( 1.2e+02 ) Ntotal = 1789 ( 1.8e+03 rank min = 6.8e+05 max = 1.8e+03) rate = 1.789000e+03 N.s^-1
SPH setup: Nstep = 119 ( 1.2e+02 ) Ntotal = 1908 ( 1.9e+03 rank min = 7.4e+05 max = 1.9e+03) rate = 1.908000e+03 N.s^-1
SPH setup: Nstep = 122 ( 1.2e+02 ) Ntotal = 2030 ( 2.0e+03 rank min = 1.2e+05 max = 2.0e+03) rate = 2.030000e+03 N.s^-1
SPH setup: Nstep = 120 ( 1.2e+02 ) Ntotal = 2150 ( 2.2e+03 rank min = 8.1e+05 max = 2.2e+03) rate = 2.150000e+03 N.s^-1
SPH setup: Nstep = 118 ( 1.2e+02 ) Ntotal = 2268 ( 2.3e+03 rank min = 8.0e+05 max = 2.3e+03) rate = 2.268000e+03 N.s^-1
SPH setup: Nstep = 63 ( 6.3e+01 ) Ntotal = 2331 ( 2.3e+03 rank min = 4.0e+05 max = 2.3e+03) rate = 2.331000e+03 N.s^-1
SPH setup: the generation step took : 0.013143531 s
SPH setup: final particle count = 2331 beginning injection ...
SPH setup: injected 2000 / 2331 => 85.8% | ranks with patchs = 1 / 1 -> local loop <-
SPH setup: injected 2331 / 2331 => 100.0% | ranks with patchs = 1 / 1 <- global loop -> (msg count : 0)
SPH setup: the injection step took : 0.031490869000000005 s
SPH setup: the setup took : 0.049509334 s
---------------- t = 0, dt = 0 ----------------
Warning: High interface/patch volume ratio. [InterfaceGen][rank=0]
This can lead to high mpi overhead, try to increase the patch split crit
Warning: smoothing length is not converged, rerunning the iterator ... [Smoothinglength][rank=0]
largest h = 0.09799885899543734 unconverged cnt = 2273
Warning: High interface/patch volume ratio. [InterfaceGen][rank=0]
This can lead to high mpi overhead, try to increase the patch split crit
Warning: smoothing length is not converged, rerunning the iterator ... [Smoothinglength][rank=0]
largest h = 0.10779874489498108 unconverged cnt = 826
Warning: High interface/patch volume ratio. [InterfaceGen][rank=0]
This can lead to high mpi overhead, try to increase the patch split crit
Warning: smoothing length is not converged, rerunning the iterator ... [Smoothinglength][rank=0]
largest h = 0.1185786193844792 unconverged cnt = 55
Warning: High interface/patch volume ratio. [InterfaceGen][rank=0]
This can lead to high mpi overhead, try to increase the patch split crit
Warning: smoothing length is not converged, rerunning the iterator ... [Smoothinglength][rank=0]
largest h = 0.13043648132292712 unconverged cnt = 2
Warning: High interface/patch volume ratio. [InterfaceGen][rank=0]
This can lead to high mpi overhead, try to increase the patch split crit
---------------- t = 0, dt = 8.357077848469309e-06 ----------------
Warning: High interface/patch volume ratio. [InterfaceGen][rank=0]
This can lead to high mpi overhead, try to increase the patch split crit
---------------- t = 8.357077848469309e-06, dt = 0.00028414100657580014 ----------------
Warning: High interface/patch volume ratio. [InterfaceGen][rank=0]
This can lead to high mpi overhead, try to increase the patch split crit
---------------- t = 0.00029249808442426947, dt = 0.0004679350743395584 ----------------
Warning: High interface/patch volume ratio. [InterfaceGen][rank=0]
This can lead to high mpi overhead, try to increase the patch split crit
---------------- t = 0.0007604331587638279, dt = 0.0005899254064287 ----------------
Warning: High interface/patch volume ratio. [InterfaceGen][rank=0]
This can lead to high mpi overhead, try to increase the patch split crit
---------------- t = 0.0013503585651925278, dt = 0.0006698820679540462 ----------------
Warning: High interface/patch volume ratio. [InterfaceGen][rank=0]
This can lead to high mpi overhead, try to increase the patch split crit
---------------- t = 0.0020202406331465738, dt = 0.0007209398751302409 ----------------
Warning: High interface/patch volume ratio. [InterfaceGen][rank=0]
This can lead to high mpi overhead, try to increase the patch split crit
---------------- t = 0.002741180508276815, dt = 0.0007299132315052102 ----------------
Warning: High interface/patch volume ratio. [InterfaceGen][rank=0]
This can lead to high mpi overhead, try to increase the patch split crit
---------------- t = 0.0034710937397820253, dt = 0.0006701883402267496 ----------------
Warning: High interface/patch volume ratio. [InterfaceGen][rank=0]
This can lead to high mpi overhead, try to increase the patch split crit
---------------- t = 0.004141282080008775, dt = 0.0005932073639326705 ----------------
Warning: High interface/patch volume ratio. [InterfaceGen][rank=0]
This can lead to high mpi overhead, try to increase the patch split crit
run_test_ext_forces_accretion: OK
Current sums: [111.1334263135445, -15.790263346174845, -1.0024165122154156, 22523.573252726077, 0.16482394866531003, 25.96527810306147, -25326.180118363834, -69.26477834522996, 6203.398030417334, 245.61542121817263]
10 import numpy as np
11
12 import shamrock
13
14 DUMP_NAME = "ext_forces_accretion_test.sham"
15
16 si = shamrock.UnitSystem()
17 sicte = shamrock.Constants(si)
18 codeu = shamrock.UnitSystem(
19 unit_time=sicte.year(),
20 unit_length=sicte.au(),
21 unit_mass=sicte.sol_mass(),
22 )
23 ucte = shamrock.Constants(codeu)
24 G = ucte.G()
25
26
27 def build_model_with_ext_forces():
28 ctx = shamrock.Context()
29 ctx.pdata_layout_new()
30
31 model = shamrock.get_Model_SPH(context=ctx, vector_type="f64_3", sph_kernel="M4")
32
33 cfg = model.gen_default_config()
34 cfg.set_self_gravity_none()
35 cfg.set_artif_viscosity_Constant(alpha_u=1.0, alpha_AV=1.0, beta_AV=2.0)
36 cfg.set_eos_isothermal(1.0)
37 cfg.set_particle_mass(1e-3)
38 cfg.set_boundary_periodic()
39 cfg.set_show_cfl_detail(True)
40 cfg.set_units(codeu)
41
42 cfg.add_ext_force_paczynski_wiita(1.0, (0.1, 0.0, 0.0), 0.15)
43 cfg.add_ext_force_paczynski_wiita(0.5, (-0.2, 0.1, 0.0), 0.15)
44 cfg.add_ext_force_paczynski_wiita(0.25, (0.0, -0.15, 0.05), 0.15)
45
46 model.set_solver_config(cfg)
47
48 model.set_cfl_cour(0.1)
49 model.set_cfl_force(0.1)
50
51 model.init_scheduler(1000, 1)
52
53 dr = 0.05
54 bmin = (-0.6, -0.6, -0.6)
55 bmax = (0.6, 0.6, 0.6)
56 model.resize_simulation_box(bmin, bmax)
57
58 setup = model.get_setup()
59 gen = setup.make_generator_lattice_hcp(dr, bmin, bmax)
60 setup.apply_setup(gen)
61
62 def vel_func(r):
63 return (10.0, 0.0, 0.0)
64
65 model.set_field_value_lambda_f64_3("vxyz", vel_func)
66
67 return ctx, model
68
69
70 def main():
71 ctx, model = build_model_with_ext_forces()
72
73 for _ in range(5):
74 model.timestep()
75
76 model.dump(DUMP_NAME)
77
78 del model
79 del ctx
80
81 ctx2 = shamrock.Context()
82 ctx2.pdata_layout_new()
83 model2 = shamrock.get_Model_SPH(context=ctx2, vector_type="f64_3", sph_kernel="M4")
84 model2.load_from_dump(DUMP_NAME)
85
86 for _ in range(5):
87 model2.timestep()
88
89 if shamrock.sys.world_rank() == 0:
90 print("run_test_ext_forces_accretion: OK")
91
92 dic = ctx2.collect_data()
93
94 if shamrock.sys.world_rank() > 0:
95 return
96
97 assert 2262 == len(dic["xyz"])
98
99 sum_pos = np.sum(dic["xyz"], axis=0)
100 sum_vel = np.sum(dic["vxyz"], axis=0)
101 sum_acc = np.sum(dic["axyz"], axis=0)
102 sum_hpart = np.sum(dic["hpart"], axis=0)
103
104 dat = np.concatenate([sum_pos, sum_vel, sum_acc, np.atleast_1d(sum_hpart)])
105 print("Current sums: ", [float(dat[i]) for i in range(len(dat))])
106
107 ref_sums = [
108 111.13342631354446,
109 -15.790263346174845,
110 -1.0024165122154156,
111 22523.573252726077,
112 0.16482394866530714,
113 25.96527810306144,
114 -25326.18011836384,
115 -69.26477834526008,
116 6203.398030417404,
117 245.61542121817263,
118 ]
119
120 mismatch = False
121 for i in range(len(dat)):
122 if not np.isclose(dat[i], ref_sums[i], rtol=1e-11, atol=1e-18):
123 abs_diff = np.abs(dat[i] - ref_sums[i])
124 rel_diff = abs_diff / np.abs(ref_sums[i])
125 print(f"sum[{i}] mismatch: got {dat[i]}, expected {ref_sums[i]}")
126 print(f" max abs diff={np.max(abs_diff)}")
127 print(f" max rel diff={np.max(rel_diff)}")
128 mismatch = True
129 if mismatch:
130 raise RuntimeError("sums mismatch")
131
132
133 main()
Total running time of the script: (0 minutes 3.258 seconds)
Estimated memory usage: 217 MB