|
| 1 | +from pyphare.pharesee.hierarchy.hierarchy_utils import compute_hier_from |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | + |
| 6 | +def ions_mass_density_func1d(x, **kwargs): |
| 7 | + masses = kwargs["masses"] # list of float : the ion pop masses |
| 8 | + densities = kwargs["densities"] # list of callable : the ion pop density profiles |
| 9 | + |
| 10 | + assert len(masses) == len(densities) |
| 11 | + funcs = np.zeros((x.size, len(masses))) |
| 12 | + |
| 13 | + for i, (mass, density) in enumerate(zip(masses, densities)): |
| 14 | + funcs[:,i] = mass*density(x) |
| 15 | + |
| 16 | + return funcs.sum(axis=1) |
| 17 | + |
| 18 | + |
| 19 | +def ions_charge_density_func1d(x, **kwargs): |
| 20 | + charges = kwargs["charges"] # list of float : the ion pop charges |
| 21 | + densities = kwargs["densities"] # list of callable : the ion pop density profiles |
| 22 | + |
| 23 | + assert len(charges) == len(densities) |
| 24 | + |
| 25 | + funcs = np.zeros((x.size, len(charges))) |
| 26 | + |
| 27 | + for i, (charge, density) in enumerate(zip(charges, densities)): |
| 28 | + funcs[:,i] = charge*density(x) |
| 29 | + |
| 30 | + return funcs.sum(axis=1) |
| 31 | + |
| 32 | + |
| 33 | +def hierarchy_from_func1d(func, hier, **kwargs): |
| 34 | + assert hier.ndim == 1 |
| 35 | + |
| 36 | + def compute_(patch_datas, **kwargs): |
| 37 | + ref_name = next(iter(patch_datas.keys())) |
| 38 | + x_ = patch_datas[ref_name].x |
| 39 | + |
| 40 | + return ( |
| 41 | + {"name": "value", "data": func(x_, **kwargs), "centering": patch_datas[ref_name].centerings}, |
| 42 | + ) |
| 43 | + |
| 44 | + return compute_hier_from(compute_, hier, **kwargs) |
| 45 | + |
| 46 | + |
| 47 | +def ions_mass_density_func2d(x, y, **kwargs): |
| 48 | + masses = kwargs["masses"] # list of float : the ion pop masses |
| 49 | + densities = kwargs["densities"] # list of callable : the ion pop density profiles |
| 50 | + |
| 51 | + yv, xv = np.meshgrid(y, x) |
| 52 | + |
| 53 | + assert len(masses) == len(densities) |
| 54 | + funcs = np.zeros((x.size, y.size, len(masses))) |
| 55 | + |
| 56 | + for i, (mass, density) in enumerate(zip(masses, densities)): |
| 57 | + funcs[:,:,i] = mass*density(xv, yv) |
| 58 | + |
| 59 | + return funcs.sum(axis=2) |
| 60 | + |
| 61 | + |
| 62 | +def ions_charge_density_func2d(x, y, **kwargs): |
| 63 | + charges = kwargs["charges"] # list of float : the ion pop charges |
| 64 | + densities = kwargs["densities"] # list of callable : the ion pop density profiles |
| 65 | + |
| 66 | + yv, xv = np.meshgrid(y, x) |
| 67 | + |
| 68 | + assert len(charges) == len(densities) |
| 69 | + funcs = np.zeros((x.size, y.size, len(charges))) |
| 70 | + |
| 71 | + for i, (charge, density) in enumerate(zip(charges, densities)): |
| 72 | + funcs[:,:,i] = charge*density(xv, yv) |
| 73 | + |
| 74 | + return funcs.sum(axis=2) |
| 75 | + |
| 76 | + |
| 77 | +def hierarchy_from_func2d(func, hier, **kwargs): |
| 78 | + assert hier.ndim == 2 |
| 79 | + |
| 80 | + def compute_(patch_datas, **kwargs): |
| 81 | + ref_name = next(iter(patch_datas.keys())) |
| 82 | + x_ = patch_datas[ref_name].x |
| 83 | + y_ = patch_datas[ref_name].y |
| 84 | + |
| 85 | + return ( |
| 86 | + {"name": "value", "data": func(x_, y_, **kwargs), "centering": patch_datas[ref_name].centerings}, |
| 87 | + ) |
| 88 | + |
| 89 | + return compute_hier_from(compute_, hier, **kwargs) |
| 90 | + |
| 91 | + |
| 92 | +def hierarchy_from_func(func, hier, **kwargs): |
| 93 | + if hier.ndim == 1: |
| 94 | + return hierarchy_from_func1d(func, hier, **kwargs) |
| 95 | + if hier.ndim == 2: |
| 96 | + return hierarchy_from_func2d(func, hier, **kwargs) |
| 97 | + |
0 commit comments