Note
Go to the end to download the full example code.
CI test: Field.collect_data returns arrays that line up across Fields#
Two Fields built with different histories (one from model.compute_field, one derived from it with shamrock.map_fields_f64) hold their patches in hash maps that can iterate in different orders. collect_data must still concatenate the patches in the same (patch id) order, so that element i of both arrays refers to the same particle.
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 = 8.6e+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.1e+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.4e+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 = 2.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.2e+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 = 4.4e+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 = 5.6e+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.0e+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.0e+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 = 4.4e+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 = 4.0e+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.0e+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 = 5.2e+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 = 6.9e+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 = 6.8e+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 = 7.1e+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 = 5.8e+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.0e+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 = 6.4e+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 = 6.6e+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 = 3.0e+05 max = 2.3e+03) rate = 2.331000e+03 N.s^-1
SPH setup: the generation step took : 0.016946499 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.040342314000000004 s
SPH setup: the setup took : 0.063186702 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 = 2331
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 = 682
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 = 36
Warning: High interface/patch volume ratio. [InterfaceGen][rank=0]
This can lead to high mpi overhead, try to increase the patch split crit
collect_data alignment (2331 values): OK
run_field_collect_data_alignment: OK
12 import numpy as np
13
14 import shamrock
15
16
17 def build_model():
18 ctx = shamrock.Context()
19 ctx.pdata_layout_new()
20
21 model = shamrock.get_Model_SPH(context=ctx, vector_type="f64_3", sph_kernel="M4")
22
23 cfg = model.gen_default_config()
24 cfg.set_self_gravity_none()
25 cfg.set_artif_viscosity_Constant(alpha_u=1.0, alpha_AV=1.0, beta_AV=2.0)
26 cfg.set_eos_isothermal(1.0)
27 cfg.set_particle_mass(1e-3)
28 cfg.set_boundary_periodic()
29 model.set_solver_config(cfg)
30
31 model.set_cfl_cour(0.1)
32 model.set_cfl_force(0.1)
33
34 # low split criterion so that every rank holds several patches
35 model.init_scheduler(1000, 1)
36
37 dr = 0.05
38 bmin = (-0.6, -0.6, -0.6)
39 bmax = (0.6, 0.6, 0.6)
40 model.resize_simulation_box(bmin, bmax)
41
42 setup = model.get_setup()
43 gen = setup.make_generator_lattice_hcp(dr, bmin, bmax)
44 setup.apply_setup(gen)
45
46 model.timestep()
47
48 return ctx, model
49
50
51 def main():
52 ctx, model = build_model()
53
54 rho_field = model.compute_field("rho", "f64")
55
56 def scale_by_two(size, x):
57 return 2.0 * x
58
59 rho_x2_field = shamrock.map_fields_f64(scale_by_two, x=rho_field)
60
61 rho = np.asarray(rho_field.collect_data())
62 rho_x2 = np.asarray(rho_x2_field.collect_data())
63
64 if rho.shape != rho_x2.shape:
65 raise RuntimeError(f"shape mismatch: rho={rho.shape}, rho_x2={rho_x2.shape}")
66
67 # multiplying by two is exact, so any mismatch means the patches are not aligned
68 if not np.array_equal(rho_x2, 2.0 * rho):
69 bad = np.count_nonzero(rho_x2 != 2.0 * rho)
70 raise RuntimeError(
71 "map_fields_f64(2*rho).collect_data() is not aligned with rho.collect_data()\n"
72 f" {bad} / {rho.size} mismatching elements"
73 )
74
75 if shamrock.sys.world_rank() == 0:
76 print(f"collect_data alignment ({rho.size} values): OK")
77 print("run_field_collect_data_alignment: OK")
78
79
80 main()
Total running time of the script: (0 minutes 1.898 seconds)
Estimated memory usage: 164 MB