From 2191e3925adfbf23122e9b71be3e58897337bb47 Mon Sep 17 00:00:00 2001 From: Nicolas Aunai Date: Tue, 17 Sep 2024 15:21:04 +0200 Subject: [PATCH] wip --- .../pyphare/pharesee/hierarchy/hierarchy.py | 53 +++++++++++-------- .../numerics/interpolator/interpolator.hpp | 17 +++--- tests/simulator/test_advance.py | 11 ++-- 3 files changed, 46 insertions(+), 35 deletions(-) diff --git a/pyphare/pyphare/pharesee/hierarchy/hierarchy.py b/pyphare/pyphare/pharesee/hierarchy/hierarchy.py index 7ff9bb714..50c55325d 100644 --- a/pyphare/pyphare/pharesee/hierarchy/hierarchy.py +++ b/pyphare/pyphare/pharesee/hierarchy/hierarchy.py @@ -353,6 +353,8 @@ def box_to_Rectangle(self, box): return Rectangle(box.lower, *box.shape) def plot_2d_patches(self, ilvl, collections, **kwargs): + from matplotlib.patches import Rectangle + if isinstance(collections, list) and all( [isinstance(el, Box) for el in collections] ): @@ -365,17 +367,35 @@ def plot_2d_patches(self, ilvl, collections, **kwargs): fig, ax = kwargs.get("subplot", plt.subplots(figsize=(16, 16))) + color = 1 + i0, j0 = level_domain_box.lower + i1, j1 = level_domain_box.upper + ij = np.zeros((i1 - i0 + 1, j1 - j0 + 1)) + np.nan + ix = np.arange(i0, i1 + 1) + iy = np.arange(j0, j1 + 1) + for collection in collections: - facecolor = collection.get("facecolor", "none") - edgecolor = collection.get("edgecolor", "purple") - alpha = collection.get("alpha", 1) - rects = [self.box_to_Rectangle(box) for box in collection["boxes"]] - - ax.add_collection( - PatchCollection( - rects, facecolor=facecolor, alpha=alpha, edgecolor=edgecolor - ) - ) + facecolor = collection.get("facecolor", np.nan) + for box in collection["boxes"]: + if isinstance(box, Box): + i0, j0 = box.lower + i1, j1 = box.upper + ij[i0 : i1 + 1, j0 : j1 + 1] = facecolor + else: + ij[box] = facecolor + + ax.pcolormesh(ix, iy, ij.T, edgecolors="k", cmap="jet") + ax.set_xticks(ix) + ax.set_yticks(iy) + + for patch in self.level(ilvl).patches: + box = patch.box + r = Rectangle(box.lower - 0.5, *(box.upper + 0.5)) + + r.set_edgecolor("r") + r.set_facecolor("none") + r.set_linewidth(2) + ax.add_patch(r) if "title" in kwargs: from textwrap import wrap @@ -383,19 +403,6 @@ def plot_2d_patches(self, ilvl, collections, **kwargs): xfigsize = int(fig.get_size_inches()[0] * 10) # 10 characters per inch ax.set_title("\n".join(wrap(kwargs["title"], xfigsize))) - major_ticks = np.arange(mi - 5, ma + 5 + 5, 1) - ax.set_xticks(major_ticks) - ax.set_yticks(major_ticks) - - minor_ticks = np.arange(mi - 4.5, ma + 5 + 5, 1) - ax.set_xticks(minor_ticks, minor=True) - ax.set_yticks(minor_ticks, minor=True) - - # Align the minor tick label - for label in ax.get_xticklabels(minor=True): - label.set_horizontalalignment("center") - - ax.grid(which="both") if "xlim" in kwargs: ax.set_xlim(kwargs["xlim"]) if "ylim" in kwargs: diff --git a/src/core/numerics/interpolator/interpolator.hpp b/src/core/numerics/interpolator/interpolator.hpp index 2aa5f4a86..e8858d6a4 100644 --- a/src/core/numerics/interpolator/interpolator.hpp +++ b/src/core/numerics/interpolator/interpolator.hpp @@ -469,8 +469,9 @@ class Interpolator : private Weighter * onto the particle. */ template - inline void operator()(ParticleRange& particleRange, Field& particleDensity, Field& chargeDensity, VecField& flux, - GridLayout const& layout, double coef = 1.) + inline void operator()(ParticleRange& particleRange, Field& particleDensity, + Field& chargeDensity, VecField& flux, GridLayout const& layout, + double coef = 1.) { auto begin = particleRange.begin(); auto end = particleRange.end(); @@ -487,11 +488,11 @@ class Interpolator : private Weighter currPart->delta); particleToMesh_( - particleDensity, *currPart, [](auto const& part) { return 1.; }, startIndex_, weights_, - coef); + particleDensity, *currPart, [](auto const& part) { return 1.; }, startIndex_, + weights_, coef); particleToMesh_( - chargeDensity, *currPart, [](auto const& part) { return part.charge; }, startIndex_, weights_, - coef); + chargeDensity, *currPart, [](auto const& part) { return part.charge; }, startIndex_, + weights_, coef); particleToMesh_( xFlux, *currPart, [](auto const& part) { return part.v[0]; }, startIndex_, weights_, coef); @@ -505,8 +506,8 @@ class Interpolator : private Weighter PHARE_LOG_STOP(3, "ParticleToMesh::operator()"); } template - inline void operator()(ParticleRange&& range, Field& particleDensity, Field& chargeDensity, VecField& flux, - GridLayout const& layout, double coef = 1.) + inline void operator()(ParticleRange&& range, Field& particleDensity, Field& chargeDensity, + VecField& flux, GridLayout const& layout, double coef = 1.) { (*this)(range, particleDensity, chargeDensity, flux, layout, coef); } diff --git a/tests/simulator/test_advance.py b/tests/simulator/test_advance.py index a7696d27d..e7343f243 100644 --- a/tests/simulator/test_advance.py +++ b/tests/simulator/test_advance.py @@ -293,15 +293,18 @@ def base_test_overlaped_fields_are_equal(self, datahier, coarsest_time): checks += 1 except AssertionError as e: print("AssertionError", pd1.name, e) - print( - np.where(~np.isclose(slice1, slice2, atol=2.5e-14, rtol=0)) + errors = np.where( + ~np.isclose(slice1, slice2, atol=2.5e-14, rtol=0) ) + errors[0][:] += loc_b1.lower[0] + pd1.ghost_box.lower[0] + errors[1][:] += loc_b1.lower[1] + pd1.ghost_box.lower[1] fig = datahier.plot_2d_patches( ilvl, collections=[ - {"boxes": [pd1.box], "facecolor": "red"}, - {"boxes": [pd2.box], "facecolor": "blue"}, + {"boxes": [pd1.box], "facecolor": 1}, + {"boxes": [pd2.box], "facecolor": 2}, + {"boxes": [errors], "facecolor": 3}, ], xlim=(-5, 50), ylim=(-5, 50),