Skip to content

Commit aba9fc8

Browse files
RevathiJambunathanpre-commit-ci[bot]RemiLehe
authored andcommitted
Exposing mf register (BLAST-WarpX#5846)
This PR exposes the multifab register so that when we create a local copy using pyamrex interface in a python script, 1. the multifab gets allocated and initialized 2. this allows the multifab to also be redistributed during load balancing 3. when finalize is called, the multifab memory is cleared in a clean way This PR AMReX-Codes/pyamrex#439) adds wrappers to ba.convert, ba.surroundingNodes, mf.copy so now , we could add a new "test_mf" with desired nodality ``` reg.alloc_init("test_nf",0,warpx.boxArray(0).convert(amrex.IntVect(1,1)),warpx.DistributionMap(0), 1,amrex.IntVect(1,1),0.0,True,True) ``` <img width="637" alt="Screenshot 2025-05-14 at 10 58 19 AM" src="https://github.com/user-attachments/assets/ecc3a5ae-9ce1-4b32-82ad-169167d8dfdb" /> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Remi Lehe <[email protected]>
1 parent 12eb6c9 commit aba9fc8

File tree

4 files changed

+80
-19
lines changed

4 files changed

+80
-19
lines changed

Examples/Physics_applications/spacecraft_charging/inputs_test_rz_spacecraft_charging_picmi.py

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,17 @@ def __init__(self):
3535
self.spacecraft_capacitance = None
3636

3737
# shortcuts
38-
self.Direction = libwarpx.libwarpx_so.Direction
39-
self.dir_r, self.dir_z = self.Direction(0), self.Direction(2)
38+
self.dir_r, self.dir_z = (
39+
libwarpx.libwarpx_so.Direction(0),
40+
libwarpx.libwarpx_so.Direction(2),
41+
)
4042

4143
def correct_space_charge_fields(self, q=None):
4244
"""
4345
Function that will be called at each iteration,
4446
after each electrostatic solve in WarpX
4547
"""
4648
assert self.saved_first_iteration_fields
47-
warpx = sim.extension.warpx
4849

4950
# Compute the charge that WarpX thinks there is on the spacecraft
5051
# from phi and rho after the Poisson solver
@@ -53,12 +54,17 @@ def correct_space_charge_fields(self, q=None):
5354
q = compute_actual_charge_on_spacecraft()
5455

5556
# Correct fields so as to recover the actual charge
56-
Er = warpx.multifab("Efield_fp", dir=self.dir_r, level=0)
57-
Er.saxpy(q - q_v, self.normalized_Er, 0, 0, 1, 0)
58-
Ez = warpx.multifab("Efield_fp", dir=self.dir_z, level=0)
59-
Ez.saxpy(q - q_v, self.normalized_Ez, 0, 0, 1, 0)
60-
phi = warpx.multifab("phi_fp", level=0)
61-
phi.saxpy(q - q_v, self.normalized_phi, 0, 0, 1, 0)
57+
warpx = sim.extension.warpx
58+
fields = warpx.multifab_register()
59+
Er = fields.get("Efield_fp", self.dir_r, 0)
60+
normalized_Er = fields.get("normalized_Er", 0)
61+
Er.saxpy(q - q_v, normalized_Er, 0, 0, 1, 0)
62+
Ez = fields.get("Efield_fp", self.dir_z, 0)
63+
normalized_Ez = fields.get("normalized_Ez", 0)
64+
Ez.saxpy(q - q_v, normalized_Ez, 0, 0, 1, 0)
65+
phi = fields.get("phi_fp", 0)
66+
normalized_phi = fields.get("normalized_phi", 0)
67+
phi.saxpy(q - q_v, normalized_phi, 0, 0, 1, 0)
6268

6369
self.spacecraft_potential += (q - q_v) * self.spacecraft_capacitance
6470
warpx.set_potential_on_eb("%f" % self.spacecraft_potential)
@@ -70,20 +76,65 @@ def correct_space_charge_fields(self, q=None):
7076
def save_normalized_vacuum_Efields(
7177
self,
7278
):
73-
warpx = sim.extension.warpx
74-
7579
# Compute the charge that WarpX thinks there is on the spacecraft
7680
# from phi and rho after the Poisson solver
7781
q_v = compute_virtual_charge_on_spacecraft()
7882
self.spacecraft_capacitance = 1.0 / q_v # the potential was set to 1V
7983

84+
warpx = sim.extension.warpx
85+
fields = warpx.multifab_register()
86+
87+
phi = fields.get("phi_fp", 0)
88+
Er = fields.get("Efield_fp", self.dir_r, 0)
89+
Ez = fields.get("Efield_fp", self.dir_z, 0)
90+
# Allocate the fields `normalized_Er`, `normalized_Ez`, and `normalized_phi
91+
# in WarpX's multifab register. This allows to get these fields at later
92+
# iterations with fields.get( ... ).
93+
# These new fields are automatically redistributed when doing load balancing.
94+
normalized_Er = fields.alloc_init(
95+
"normalized_Er",
96+
0,
97+
Er.box_array(),
98+
warpx.DistributionMap(0),
99+
1,
100+
Er.n_grow_vect,
101+
0.0,
102+
True,
103+
True,
104+
)
105+
106+
normalized_Ez = fields.alloc_init(
107+
"normalized_Ez",
108+
0,
109+
Ez.box_array(),
110+
warpx.DistributionMap(0),
111+
1,
112+
Ez.n_grow_vect,
113+
0.0,
114+
True,
115+
True,
116+
)
117+
118+
normalized_phi = fields.alloc_init(
119+
"normalized_phi",
120+
0,
121+
phi.box_array(),
122+
warpx.DistributionMap(0),
123+
1,
124+
phi.n_grow_vect,
125+
0.0,
126+
True,
127+
True,
128+
)
129+
80130
# Record fields
81-
self.normalized_Er = warpx.multifab("Efield_fp", dir=self.dir_r, level=0).copy()
82-
self.normalized_Er.mult(1 / q_v, 0)
83-
self.normalized_Ez = warpx.multifab("Efield_fp", dir=self.dir_z, level=0).copy()
84-
self.normalized_Ez.mult(1 / q_v, 0)
85-
self.normalized_phi = warpx.multifab("phi_fp", level=0).copy()
86-
self.normalized_phi.mult(1 / q_v, 0)
131+
132+
normalized_Er.copymf(Er, 0, 0, 1, Er.n_grow_vect)
133+
normalized_Er.mult(1 / q_v, 0)
134+
normalized_Ez.copymf(Ez, 0, 0, 1, Ez.n_grow_vect)
135+
normalized_Ez.mult(1 / q_v, 0)
136+
normalized_phi.copymf(phi, 0, 0, 1, phi.n_grow_vect)
137+
normalized_phi.mult(1 / q_v, 0)
87138

88139
self.saved_first_iteration_fields = True
89140
self.correct_space_charge_fields(q=0)
@@ -97,8 +148,9 @@ def compute_virtual_charge_on_spacecraft():
97148
that WarpX thinks there should be on the spacecraft.
98149
"""
99150
warpx = sim.extension.warpx
100-
rho = warpx.multifab("rho_fp", level=0)
101-
phi = warpx.multifab("phi_fp", level=0)
151+
fields = warpx.multifab_register()
152+
rho = fields.get("rho_fp", 0)
153+
phi = fields.get("phi_fp", 0)
102154

103155
dr, dz = warpx.Geom(lev=0).data().CellSize()
104156

Source/Python/MultiFabRegister.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void init_MultiFabRegister (py::module & m)
3232
bool,
3333
bool
3434
>(&MultiFabRegister::alloc_init<std::string>),
35+
py::return_value_policy::reference_internal,
3536
py::arg("name"),
3637
py::arg("level"),
3738
py::arg("ba"),
@@ -56,6 +57,7 @@ void init_MultiFabRegister (py::module & m)
5657
bool,
5758
bool
5859
>(&MultiFabRegister::alloc_init<std::string>),
60+
py::return_value_policy::reference_internal,
5961
py::arg("name"),
6062
py::arg("dir"),
6163
py::arg("level"),
@@ -75,6 +77,7 @@ void init_MultiFabRegister (py::module & m)
7577
int,
7678
std::optional<const amrex::Real>
7779
>(&MultiFabRegister::alias_init<std::string, std::string>),
80+
py::return_value_policy::reference_internal,
7881
py::arg("new_name"),
7982
py::arg("alias_name"),
8083
py::arg("level"),
@@ -89,6 +92,7 @@ void init_MultiFabRegister (py::module & m)
8992
int,
9093
std::optional<const amrex::Real>
9194
>(&MultiFabRegister::alias_init<std::string, std::string>),
95+
py::return_value_policy::reference_internal,
9296
py::arg("new_name"),
9397
py::arg("alias_name"),
9498
py::arg("dir"),
@@ -121,6 +125,7 @@ void init_MultiFabRegister (py::module & m)
121125
std::string,
122126
int
123127
>(&MultiFabRegister::get<std::string>),
128+
py::return_value_policy::reference_internal,
124129
py::arg("name"),
125130
py::arg("level")
126131
)
@@ -131,6 +136,7 @@ void init_MultiFabRegister (py::module & m)
131136
ablastr::fields::Direction,
132137
int
133138
>(&MultiFabRegister::get<std::string>),
139+
py::return_value_policy::reference_internal,
134140
py::arg("name"),
135141
py::arg("dir"),
136142
py::arg("level")

Source/Python/WarpX.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ void init_WarpX (py::module& m)
113113
//py::overload_cast< int >(&WarpX::boxArray, py::const_),
114114
py::arg("lev")
115115
)
116+
.def("multifab_register",&WarpX::GetMultiFabRegister,
117+
py::return_value_policy::reference_internal)
116118
.def("multifab",
117119
[](WarpX & wx, std::string internal_name) {
118120
if (wx.m_fields.internal_has(internal_name)) {

Source/WarpX.H

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,7 @@ public:
953953

954954
// Field container
955955
ablastr::fields::MultiFabRegister m_fields;
956+
ablastr::fields::MultiFabRegister& GetMultiFabRegister() {return m_fields;}
956957

957958
protected:
958959

0 commit comments

Comments
 (0)