|
| 1 | +""" |
| 2 | +This file is part of pyAMReX |
| 3 | +
|
| 4 | +Copyright 2023 AMReX community |
| 5 | +Authors: Axel Huebl |
| 6 | +License: BSD-3-Clause-LBNL |
| 7 | +""" |
| 8 | + |
| 9 | + |
| 10 | +def pc_to_df(self, local=True, comm=None, root_rank=0): |
| 11 | + """ |
| 12 | + Copy all particles into a pandas.DataFrame |
| 13 | +
|
| 14 | + Parameters |
| 15 | + ---------- |
| 16 | + self : amrex.ParticleContainer_* |
| 17 | + A ParticleContainer class in pyAMReX |
| 18 | + local : bool |
| 19 | + MPI-local particles |
| 20 | + comm : MPI Communicator |
| 21 | + if local is False, this defaults to mpi4py.MPI.COMM_WORLD |
| 22 | + root_rank : MPI root rank to gather to |
| 23 | + if local is False, this defaults to 0 |
| 24 | +
|
| 25 | + Returns |
| 26 | + ------- |
| 27 | + A concatenated pandas.DataFrame with particles from all levels. |
| 28 | +
|
| 29 | + Returns None if no particles were found. |
| 30 | + If local=False, then all ranks but the root_rank will return None. |
| 31 | + """ |
| 32 | + import pandas as pd |
| 33 | + |
| 34 | + # create a DataFrame per particle box and append it to the list of |
| 35 | + # local DataFrame(s) |
| 36 | + dfs_local = [] |
| 37 | + for lvl in range(self.finest_level + 1): |
| 38 | + for pti in self.const_iterator(self, level=lvl): |
| 39 | + if pti.size == 0: |
| 40 | + continue |
| 41 | + |
| 42 | + if self.is_soa_particle: |
| 43 | + next_df = pd.DataFrame() |
| 44 | + else: |
| 45 | + # AoS |
| 46 | + aos_np = pti.aos().to_numpy(copy=True) |
| 47 | + next_df = pd.DataFrame(aos_np) |
| 48 | + next_df.set_index("cpuid") |
| 49 | + next_df.index.name = "cpuid" |
| 50 | + |
| 51 | + # SoA |
| 52 | + soa_view = pti.soa().to_numpy(copy=True) |
| 53 | + soa_np_real = soa_view.real |
| 54 | + soa_np_int = soa_view.int |
| 55 | + |
| 56 | + for idx, array in enumerate(soa_np_real): |
| 57 | + next_df[f"SoA_real_{idx}"] = array |
| 58 | + for idx, array in enumerate(soa_np_int): |
| 59 | + next_df[f"SoA_int_{idx}"] = array |
| 60 | + |
| 61 | + dfs_local.append(next_df) |
| 62 | + |
| 63 | + # MPI Gather to root rank if requested |
| 64 | + if local: |
| 65 | + if len(dfs_local) == 0: |
| 66 | + df = None |
| 67 | + else: |
| 68 | + df = pd.concat(dfs_local) |
| 69 | + else: |
| 70 | + from mpi4py import MPI |
| 71 | + |
| 72 | + if comm is None: |
| 73 | + comm = MPI.COMM_WORLD |
| 74 | + rank = comm.Get_rank() |
| 75 | + |
| 76 | + # a list for each rank's list of DataFrame(s) |
| 77 | + df_list_list = comm.gather(dfs_local, root=root_rank) |
| 78 | + |
| 79 | + if rank == root_rank: |
| 80 | + flattened_list = [df for sublist in df_list_list for df in sublist] |
| 81 | + |
| 82 | + if len(flattened_list) == 0: |
| 83 | + df = pd.DataFrame() |
| 84 | + else: |
| 85 | + df = pd.concat(flattened_list, ignore_index=True) |
| 86 | + else: |
| 87 | + df = None |
| 88 | + |
| 89 | + return df |
| 90 | + |
| 91 | + |
| 92 | +def register_ParticleContainer_extension(amr): |
| 93 | + """ParticleContainer helper methods""" |
| 94 | + import inspect |
| 95 | + import sys |
| 96 | + |
| 97 | + # register member functions for every ParticleContainer_* type |
| 98 | + for _, ParticleContainer_type in inspect.getmembers( |
| 99 | + sys.modules[amr.__name__], |
| 100 | + lambda member: inspect.isclass(member) |
| 101 | + and member.__module__ == amr.__name__ |
| 102 | + and member.__name__.startswith("ParticleContainer_"), |
| 103 | + ): |
| 104 | + ParticleContainer_type.to_df = pc_to_df |
0 commit comments