|
| 1 | +from orbit.core.bunch import Bunch |
| 2 | +from orbit.core import orbit_mpi |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +from numpy.typing import NDArray |
| 6 | + |
| 7 | + |
| 8 | +def collect_bunch( |
| 9 | + bunch: Bunch, |
| 10 | +) -> dict[str, np.float64 | np.int32 | NDArray[np.float64]]: |
| 11 | + """Collects attributes from a PyOrbit Bunch across all MPI ranks and returns it as a dictionary. |
| 12 | + Parameters |
| 13 | + ---------- |
| 14 | + bunch : Bunch |
| 15 | + The PyOrbit::Bunch object from which to collect attributes. |
| 16 | + Returns |
| 17 | + ------- |
| 18 | + dict[str, np.float64 | np.int32 | NDArray[np.float64]]: |
| 19 | + By default this returns a dictionary containing the following keys and their corresponding values: |
| 20 | + - "x": particle x-coordinates [m] |
| 21 | + - "xp": particle x-momenta [rad] |
| 22 | + - "y": particle y-coordinates [m] |
| 23 | + - "yp": particle y-momenta [rad] |
| 24 | + - "z": particle longitudinal coordinates [m] |
| 25 | + - "dE": particle energy deviations [GeV] |
| 26 | + - "sync_part_coords": coordinates of the synchronous particle (x,y,z) [m] |
| 27 | + - "sync_part_kin_energy": kinetic energy of the synchronous particle [GeV] |
| 28 | + - "sync_part_momentum": momentum of the synchronous particle [GeV/c] |
| 29 | + - "sync_part_beta": beta of the synchronous particle |
| 30 | + - "sync_part_gamma": gamma of the synchronous particle |
| 31 | + - "sync_part_time": time of the synchronous particle [s] |
| 32 | + - Any additional attributes defined in the bunch. |
| 33 | + """ |
| 34 | + n_particles = bunch.getSize() |
| 35 | + |
| 36 | + if n_particles == 0: |
| 37 | + return {} |
| 38 | + |
| 39 | + mpi_comm = bunch.getMPIComm() # orbit_mpi.mpi_comm.MPI_COMM_WORLD |
| 40 | + mpi_rank = orbit_mpi.MPI_Comm_rank(mpi_comm) |
| 41 | + mpi_size = orbit_mpi.MPI_Comm_size(mpi_comm) |
| 42 | + |
| 43 | + if mpi_rank == 0: |
| 44 | + bunch_dict = {"x": [], "xp": [], "y": [], "yp": [], "z": [], "dE": []} |
| 45 | + for attr in bunch.bunchAttrDoubleNames(): |
| 46 | + bunch_dict[attr] = np.float64(bunch.bunchAttrDouble(attr)) |
| 47 | + |
| 48 | + for attr in bunch.bunchAttrIntNames(): |
| 49 | + bunch_dict[attr] = np.int32(bunch.bunchAttrInt(attr)) |
| 50 | + |
| 51 | + sync_part = bunch.getSyncParticle() |
| 52 | + |
| 53 | + bunch_dict |= { |
| 54 | + "sync_part_coords": np.array(sync_part.pVector()), |
| 55 | + "sync_part_kin_energy": np.float64(sync_part.kinEnergy()), |
| 56 | + "sync_part_momentum": np.float64(sync_part.momentum()), |
| 57 | + "sync_part_beta": np.float64(sync_part.beta()), |
| 58 | + "sync_part_gamma": np.float64(sync_part.gamma()), |
| 59 | + "sync_part_time": np.float64(sync_part.time()), |
| 60 | + } |
| 61 | + |
| 62 | + for i in range(n_particles): |
| 63 | + bunch_dict["x"].append(bunch.x(i)) |
| 64 | + bunch_dict["xp"].append(bunch.xp(i)) |
| 65 | + bunch_dict["y"].append(bunch.y(i)) |
| 66 | + bunch_dict["yp"].append(bunch.yp(i)) |
| 67 | + bunch_dict["z"].append(bunch.z(i)) |
| 68 | + bunch_dict["dE"].append(bunch.dE(i)) |
| 69 | + |
| 70 | + mpi_tag = 42 # not sure this is necessary; seems like it can be any integer |
| 71 | + for cpu_idx in range(1, mpi_size): |
| 72 | + for i in range(n_particles): |
| 73 | + if mpi_rank == cpu_idx: |
| 74 | + coord_arr = ( |
| 75 | + bunch.x(i), |
| 76 | + bunch.xp(i), |
| 77 | + bunch.y(i), |
| 78 | + bunch.yp(i), |
| 79 | + bunch.z(i), |
| 80 | + bunch.dE(i), |
| 81 | + ) |
| 82 | + orbit_mpi.MPI_Send( |
| 83 | + coord_arr, orbit_mpi.mpi_datatype.MPI_DOUBLE, 0, mpi_tag, mpi_comm |
| 84 | + ) |
| 85 | + elif mpi_rank == 0: |
| 86 | + coord_arr = orbit_mpi.MPI_Recv( |
| 87 | + orbit_mpi.mpi_datatype.MPI_DOUBLE, cpu_idx, mpi_tag, mpi_comm |
| 88 | + ) |
| 89 | + bunch_dict["x"].append(coord_arr[0]) |
| 90 | + bunch_dict["xp"].append(coord_arr[1]) |
| 91 | + bunch_dict["y"].append(coord_arr[2]) |
| 92 | + bunch_dict["yp"].append(coord_arr[3]) |
| 93 | + bunch_dict["z"].append(coord_arr[4]) |
| 94 | + bunch_dict["dE"].append(coord_arr[5]) |
| 95 | + |
| 96 | + if mpi_rank == 0: |
| 97 | + for k, v in bunch_dict.items(): |
| 98 | + if isinstance(v, list): |
| 99 | + bunch_dict[k] = np.array(v, dtype=np.float64) |
| 100 | + return bunch_dict |
0 commit comments