Skip to content

Commit 5abea5d

Browse files
committed
cherry pick chargeNA on charge
1 parent e65baac commit 5abea5d

File tree

7 files changed

+404
-5
lines changed

7 files changed

+404
-5
lines changed

.gdbinit

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
break __assert_fail
2+
break abort
3+
catch throw

pyphare/pyphare/pharesee/hierarchy/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111

1212

1313
def hierarchy_from(
14-
simulator=None, qty=None, pop="", h5_filename=None, times=None, hier=None, **kwargs
14+
simulator=None, qty=None, pop="", h5_filename=None, times=None, hier=None, func=None, **kwargs
1515
):
1616
from .fromh5 import hierarchy_fromh5
1717
from .fromsim import hierarchy_from_sim
18+
from .fromfunc import hierarchy_from_func
1819

1920
"""
2021
this function reads an HDF5 PHARE file and returns a PatchHierarchy from
@@ -39,4 +40,7 @@ def hierarchy_from(
3940
if simulator is not None and qty is not None:
4041
return hierarchy_from_sim(simulator, qty, pop=pop)
4142

43+
if func is not None and hier is not None:
44+
return hierarchy_from_func(func, hier, **kwargs)
45+
4246
raise ValueError("can't make hierarchy")
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+

pyphare/pyphare/pharesee/hierarchy/hierarchy.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,8 @@ def plot1d(self, **kwargs):
445445
qty = pdata_names[0]
446446

447447
layout = patch.patch_datas[qty].layout
448-
nbrGhosts = layout.nbrGhostFor(qty)
448+
# nbrGhosts = layout.nbrGhostFor(qty) # bad !!!
449+
nbrGhosts = patch.patch_datas[qty].ghosts_nbr
449450
val = patch.patch_datas[qty][patch.box]
450451
x = patch.patch_datas[qty].x[nbrGhosts[0] : -nbrGhosts[0]]
451452
label = "L{level}P{patch}".format(level=lvl_nbr, patch=ip)
@@ -556,9 +557,10 @@ def plot2d(self, **kwargs):
556557
if "ylim" in kwargs:
557558
ax.set_ylim(kwargs["ylim"])
558559

559-
divider = make_axes_locatable(ax)
560-
cax = divider.append_axes("right", size="5%", pad=0.08)
561-
plt.colorbar(im, ax=ax, cax=cax)
560+
if kwargs.get("cbar", True):
561+
divider = make_axes_locatable(ax)
562+
cax = divider.append_axes("right", size="5%", pad=0.08)
563+
fig.colorbar(im, ax=ax, cax=cax)
562564

563565
if kwargs.get("legend", None) is not None:
564566
ax.legend()

tests/core/numerics/interpolator/test_main.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ class ACollectionOfParticles_1d : public ::testing::Test
535535
part.iCell[0] = 19; // AMR index
536536
part.delta[0] = 0.5;
537537
part.weight = 1.0;
538+
part.charge = 2.0;
538539
part.v[0] = +2.;
539540
part.v[1] = -1.;
540541
part.v[2] = +1.;
@@ -543,6 +544,7 @@ class ACollectionOfParticles_1d : public ::testing::Test
543544
part.iCell[0] = 20; // AMR index
544545
part.delta[0] = 0.5;
545546
part.weight = 0.4;
547+
part.charge = 1.85;
546548
part.v[0] = +2.;
547549
part.v[1] = -1.;
548550
part.v[2] = +1.;
@@ -551,6 +553,7 @@ class ACollectionOfParticles_1d : public ::testing::Test
551553
part.iCell[0] = 20; // AMR index
552554
part.delta[0] = 0.5;
553555
part.weight = 0.6;
556+
part.charge = 2.1;
554557
part.v[0] = +2.;
555558
part.v[1] = -1.;
556559
part.v[2] = +1.;
@@ -562,6 +565,7 @@ class ACollectionOfParticles_1d : public ::testing::Test
562565
part.iCell[0] = 19; // AMR index
563566
part.delta[0] = 0.0;
564567
part.weight = 1.0;
568+
part.charge = 2.0;
565569
part.v[0] = +2.;
566570
part.v[1] = -1.;
567571
part.v[2] = +1.;
@@ -570,6 +574,7 @@ class ACollectionOfParticles_1d : public ::testing::Test
570574
part.iCell[0] = 20; // AMR index
571575
part.delta[0] = 0.0;
572576
part.weight = 0.2;
577+
part.charge = 3.2;
573578
part.v[0] = +2.;
574579
part.v[1] = -1.;
575580
part.v[2] = +1.;
@@ -578,6 +583,7 @@ class ACollectionOfParticles_1d : public ::testing::Test
578583
part.iCell[0] = 20; // AMR index
579584
part.delta[0] = 0.0;
580585
part.weight = 0.8;
586+
part.charge = 1.7;
581587
part.v[0] = +2.;
582588
part.v[1] = -1.;
583589
part.v[2] = +1.;
@@ -586,6 +592,7 @@ class ACollectionOfParticles_1d : public ::testing::Test
586592
part.iCell[0] = 21; // AMR index
587593
part.delta[0] = 0.0;
588594
part.weight = 1.0;
595+
part.charge = 2.0;
589596
part.v[0] = +2.;
590597
part.v[1] = -1.;
591598
part.v[2] = +1.;
@@ -597,6 +604,7 @@ class ACollectionOfParticles_1d : public ::testing::Test
597604
part.iCell[0] = 18; // AMR index
598605
part.delta[0] = 0.5;
599606
part.weight = 1.0;
607+
part.charge = 2.0;
600608
part.v[0] = +2.;
601609
part.v[1] = -1.;
602610
part.v[2] = +1.;
@@ -605,6 +613,7 @@ class ACollectionOfParticles_1d : public ::testing::Test
605613
part.iCell[0] = 19; // AMR index
606614
part.delta[0] = 0.5;
607615
part.weight = 1.0;
616+
part.charge = 2.0;
608617
part.v[0] = +2.;
609618
part.v[1] = -1.;
610619
part.v[2] = +1.;
@@ -613,6 +622,7 @@ class ACollectionOfParticles_1d : public ::testing::Test
613622
part.iCell[0] = 20; // AMR index
614623
part.delta[0] = 0.5;
615624
part.weight = 1.0;
625+
part.charge = 2.0;
616626
part.v[0] = +2.;
617627
part.v[1] = -1.;
618628
part.v[2] = +1.;
@@ -621,6 +631,7 @@ class ACollectionOfParticles_1d : public ::testing::Test
621631
part.iCell[0] = 21; // AMR index
622632
part.delta[0] = 0.5;
623633
part.weight = 0.1;
634+
part.charge = 3.35;
624635
part.v[0] = +2.;
625636
part.v[1] = -1.;
626637
part.v[2] = +1.;
@@ -629,6 +640,7 @@ class ACollectionOfParticles_1d : public ::testing::Test
629640
part.iCell[0] = 21; // AMR index
630641
part.delta[0] = 0.5;
631642
part.weight = 0.9;
643+
part.charge = 1.85;
632644
part.v[0] = +2.;
633645
part.v[1] = -1.;
634646
part.v[2] = +1.;
@@ -652,6 +664,7 @@ TYPED_TEST_P(ACollectionOfParticles_1d, DepositCorrectlyTheirWeight_1d)
652664

653665
auto const& [vx, vy, vz] = this->v();
654666
EXPECT_DOUBLE_EQ(this->rho(idx), 1.0);
667+
EXPECT_DOUBLE_EQ(this->rho_c(idx), 2.0);
655668
EXPECT_DOUBLE_EQ(vx(idx), 2.0);
656669
EXPECT_DOUBLE_EQ(vy(idx), -1.0);
657670
EXPECT_DOUBLE_EQ(vz(idx), 1.0);

tests/simulator/initialize/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ if(HighFive)
2020

2121
endif()
2222

23+
phare_mpi_python3_exec(9 ${PHARE_MPI_PROCS} init-densities density_check.py ${CMAKE_CURRENT_BINARY_DIR})
24+
2325
endif()

0 commit comments

Comments
 (0)