Skip to content

Commit

Permalink
Add callback to write values to external field multifabs (ECP-WarpX#5162
Browse files Browse the repository at this point in the history
)

* Add callback to write values to external fields

* add external fields after initial E solve

Signed-off-by: roelof-groenewald <regroenewald@gmail.com>

---------

Signed-off-by: roelof-groenewald <regroenewald@gmail.com>
  • Loading branch information
roelof-groenewald authored Aug 22, 2024
1 parent 5f9a37f commit b05d918
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
16 changes: 14 additions & 2 deletions Python/pywarpx/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Functions can be called at the following times:
* ``loadExternalFields``: during ``WarpX::LoadExternalFields`` to write ``B/Efield_fp_external`` values
* ``beforeInitEsolve``: before the initial solve for the E fields (i.e. before the PIC loop starts)
* ``afterinit``: immediately after the init is complete
* ``beforeEsolve``: before the solve for E fields
Expand Down Expand Up @@ -276,6 +277,7 @@ def callfuncsinlist(self, *args, **kw):
# =============================================================================

callback_instances = {
"loadExternalFields": {},
"beforeInitEsolve": {},
"afterInitEsolve": {},
"afterinit": {},
Expand Down Expand Up @@ -330,7 +332,7 @@ def clear_all():
val.clearlist()


# =============================================================================
# ============================================================================


def printcallbacktimers(tmin=1.0, lminmax=False, ff=None):
Expand Down Expand Up @@ -371,7 +373,17 @@ def printcallbacktimers(tmin=1.0, lminmax=False, ff=None):
ff.write("\n")


# =============================================================================
# ============================================================================


# ----------------------------------------------------------------------------
def callfromloadExternalFields(f):
installcallback("loadExternalFields", f)
return f


def installloadExternalFields(f):
installcallback("loadExternalFields", f)


# ----------------------------------------------------------------------------
Expand Down
10 changes: 8 additions & 2 deletions Source/Initialization/WarpXInitData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,11 @@ WarpX::InitData ()
if (electrostatic_solver_id == ElectrostaticSolverAlgo::LabFrameElectroMagnetostatic) {
ComputeMagnetostaticField();
}
// Add external fields to the fine patch fields. This makes it so that the
// net fields are the sum of the field solutions and any external fields.
for (int lev = 0; lev <= max_level; ++lev) {
AddExternalFields(lev);
}
}

if (restart_chkfile.empty() || write_diagnostics_on_restart) {
Expand Down Expand Up @@ -1013,8 +1018,6 @@ WarpX::InitLevelData (int lev, Real /*time*/)

// load external grid fields into E/Bfield_fp_external multifabs
LoadExternalFields(lev);
// add the external fields to the fine patch fields as initial conditions for the fields
AddExternalFields(lev);

if (costs[lev]) {
const auto iarr = costs[lev]->IndexArray();
Expand Down Expand Up @@ -1410,6 +1413,9 @@ WarpX::LoadExternalFields (int const lev)
#endif
}

// Call Python callback which might write values to external field multifabs
ExecutePythonCallback("loadExternalFields");

// External particle fields

if (mypc->m_B_ext_particle_s == "read_from_file") {
Expand Down

0 comments on commit b05d918

Please sign in to comment.