Skip to content

Commit 125ccef

Browse files
Read external fields from file in restarted simulation (#4547)
* Read external fields from file also in restarted simulation * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add automated test with restart * Add function to avoid duplication * Fix restart test * Move function outside of #ifdef * Fix restart test * Update Source/WarpX.H * Update Source/Initialization/WarpXInitData.cpp * Clarify documentation on external fields * Add docstring --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 9b48df9 commit 125ccef

File tree

5 files changed

+67
-40
lines changed

5 files changed

+67
-40
lines changed

Docs/source/usage/parameters.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1508,9 +1508,15 @@ Laser initialization
15081508
External fields
15091509
---------------
15101510

1511-
Grid initialization
1511+
Applied to the grid
15121512
^^^^^^^^^^^^^^^^^^^
15131513

1514+
The external fields defined with input parameters that start with ``warpx.B_ext_grid_init_`` or ``warpx.E_ext_grid_init_``
1515+
are applied to the grid directly. In particular, these fields can be seen in the diagnostics that output the fields on the grid.
1516+
1517+
- When using an **electromagnetic** field solver, these fields are applied to the grid at the beginning of the simulation, and serve as initial condition for the Maxwell solver.
1518+
- When using an **electrostatic** or **magnetostatic** field solver, these fields are added to the fields computed by the Poisson solver, at each timestep.
1519+
15141520
* ``warpx.B_ext_grid_init_style`` (string) optional
15151521
This parameter determines the type of initialization for the external
15161522
magnetic field. By default, the
@@ -1597,6 +1603,9 @@ Grid initialization
15971603
Applied to Particles
15981604
^^^^^^^^^^^^^^^^^^^^
15991605

1606+
The external fields defined with input parameters that start with ``warpx.B_ext_particle_init_`` or ``warpx.E_ext_particle_init_``
1607+
are applied to the particles directly, at each timestep. As a results, these fields **cannot** be seen in the diagnostics that output the fields on the grid.
1608+
16001609
* ``particles.E_ext_particle_init_style`` & ``particles.B_ext_particle_init_style`` (string) optional (default "none")
16011610
These parameters determine the type of the external electric and
16021611
magnetic fields respectively that are applied directly to the particles at every timestep.
@@ -1657,6 +1666,9 @@ Applied to Particles
16571666
Applied to Cold Relativistic Fluids
16581667
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16591668

1669+
The external fields defined with input parameters that start with ``warpx.B_ext_init_`` or ``warpx.E_ext_init_``
1670+
are applied to the fluids directly, at each timestep. As a results, these fields **cannot** be seen in the diagnostics that output the fields on the grid.
1671+
16601672
* ``<fluid_species_name>.E_ext_init_style`` & ``<fluid_species_name>.B_ext_init_style`` (string) optional (default "none")
16611673
These parameters determine the type of the external electric and
16621674
magnetic fields respectively that are applied directly to the cold relativistic fluids at every timestep.

Examples/Tests/LoadExternalField/inputs_rz

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ algo.particle_shape = 1
4646
#################################
4747
############ PLASMA #############
4848
#################################
49-
particles.species_names = proton #electron
49+
particles.species_names = proton
5050
proton.injection_style = "SingleParticle"
5151
proton.single_particle_pos = 0.0 0.2 2.5
5252
proton.single_particle_u = 9.506735958279367e-05 0.0 0.00013435537232359165
@@ -55,14 +55,11 @@ proton.do_not_deposit = 1
5555
proton.mass = m_p
5656
proton.charge = q_e
5757

58-
#electron.injection_style = "SingleParticle"
59-
#electron.single_particle_pos = 0.0 0.2 2.5
60-
#electron.single_particle_u = 0.0 0.0 0.0
61-
#electron.single_particle_weight = 1.0
62-
#electron.mass = 1.0
63-
#electron.charge = -q_e*1.0e-20
64-
6558
# Diagnostics
66-
diagnostics.diags_names = diag1
59+
diagnostics.diags_names = diag1 chk
6760
diag1.intervals = 300
6861
diag1.diag_type = Full
62+
63+
chk.intervals = 150
64+
chk.diag_type = Full
65+
chk.format = checkpoint

Regression/WarpX-tests.ini

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,11 +2214,12 @@ analysisRoutine = Examples/Tests/resampling/analysis_leveling_thinning.py
22142214
[LoadExternalFieldRZ]
22152215
buildDir = .
22162216
inputFile = Examples/Tests/LoadExternalField/inputs_rz
2217-
runtime_params = warpx.abort_on_warning_threshold=medium
2217+
runtime_params = warpx.abort_on_warning_threshold=medium chk.file_prefix=LoadExternalFieldRZ_chk chk.file_min_digits=5
22182218
dim = 2
22192219
addToCompileString = USE_RZ=TRUE
22202220
cmakeSetupOpts = -DWarpX_DIMS=RZ -DWarpX_OPENPMD=ON
2221-
restartTest = 0
2221+
restartTest = 1
2222+
restartFileNum = 150
22222223
useMPI = 1
22232224
numprocs = 1
22242225
useOMP = 1

Source/Initialization/WarpXInitData.cpp

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,9 @@ void
728728
WarpX::PostRestart ()
729729
{
730730
mypc->PostRestart();
731+
for (int lev = 0; lev <= maxLevel(); ++lev) {
732+
LoadExternalFieldsFromFile(lev);
733+
}
731734
}
732735

733736

@@ -901,34 +904,7 @@ WarpX::InitLevelData (int lev, Real /*time*/)
901904
}
902905
}
903906

904-
// Reading external fields from data file
905-
if (m_p_ext_field_params->B_ext_grid_type == ExternalFieldType::read_from_file) {
906-
907-
#if defined(WARPX_DIM_RZ)
908-
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(n_rz_azimuthal_modes == 1,
909-
"External field reading is not implemented for more than one RZ mode (see #3829)");
910-
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Bfield_fp_external[lev][0].get(), "B", "r");
911-
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Bfield_fp_external[lev][1].get(), "B", "t");
912-
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Bfield_fp_external[lev][2].get(), "B", "z");
913-
#else
914-
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Bfield_fp_external[lev][0].get(), "B", "x");
915-
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Bfield_fp_external[lev][1].get(), "B", "y");
916-
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Bfield_fp_external[lev][2].get(), "B", "z");
917-
#endif
918-
}
919-
if (m_p_ext_field_params->E_ext_grid_type == ExternalFieldType::read_from_file) {
920-
#if defined(WARPX_DIM_RZ)
921-
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(n_rz_azimuthal_modes == 1,
922-
"External field reading is not implemented for more than one RZ mode (see #3829)");
923-
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Efield_fp_external[lev][0].get(), "E", "r");
924-
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Efield_fp_external[lev][1].get(), "E", "t");
925-
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Efield_fp_external[lev][2].get(), "E", "z");
926-
#else
927-
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Efield_fp_external[lev][0].get(), "E", "x");
928-
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Efield_fp_external[lev][1].get(), "E", "y");
929-
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Efield_fp_external[lev][2].get(), "E", "z");
930-
#endif
931-
}
907+
LoadExternalFieldsFromFile(lev);
932908

933909
if (costs[lev]) {
934910
const auto iarr = costs[lev]->IndexArray();
@@ -1324,6 +1300,37 @@ void WarpX::CheckKnownIssues()
13241300
#endif
13251301
}
13261302

1303+
void
1304+
WarpX::LoadExternalFieldsFromFile (int const lev)
1305+
{
1306+
if (m_p_ext_field_params->B_ext_grid_type == ExternalFieldType::read_from_file) {
1307+
#if defined(WARPX_DIM_RZ)
1308+
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(n_rz_azimuthal_modes == 1,
1309+
"External field reading is not implemented for more than one RZ mode (see #3829)");
1310+
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Bfield_fp_external[lev][0].get(), "B", "r");
1311+
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Bfield_fp_external[lev][1].get(), "B", "t");
1312+
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Bfield_fp_external[lev][2].get(), "B", "z");
1313+
#else
1314+
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Bfield_fp_external[lev][0].get(), "B", "x");
1315+
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Bfield_fp_external[lev][1].get(), "B", "y");
1316+
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Bfield_fp_external[lev][2].get(), "B", "z");
1317+
#endif
1318+
}
1319+
if (m_p_ext_field_params->E_ext_grid_type == ExternalFieldType::read_from_file) {
1320+
#if defined(WARPX_DIM_RZ)
1321+
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(n_rz_azimuthal_modes == 1,
1322+
"External field reading is not implemented for more than one RZ mode (see #3829)");
1323+
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Efield_fp_external[lev][0].get(), "E", "r");
1324+
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Efield_fp_external[lev][1].get(), "E", "t");
1325+
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Efield_fp_external[lev][2].get(), "E", "z");
1326+
#else
1327+
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Efield_fp_external[lev][0].get(), "E", "x");
1328+
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Efield_fp_external[lev][1].get(), "E", "y");
1329+
ReadExternalFieldFromFile(m_p_ext_field_params->external_fields_path, Efield_fp_external[lev][2].get(), "E", "z");
1330+
#endif
1331+
}
1332+
}
1333+
13271334
#if defined(WARPX_USE_OPENPMD) && !defined(WARPX_DIM_1D_Z) && !defined(WARPX_DIM_XZ)
13281335
void
13291336
WarpX::ReadExternalFieldFromFile (

Source/WarpX.H

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,16 @@ public:
10201020
char field,
10211021
int lev, PatchType patch_type);
10221022

1023+
/**
1024+
* \brief Load field values from a user-specified openPMD file,
1025+
* for the fields Ex, Ey, Ez, Bx, By, Bz
1026+
*/
1027+
void LoadExternalFieldsFromFile (int lev);
1028+
1029+
/**
1030+
* \brief Load field values from a user-specified openPMD file
1031+
* for a specific field (specified by `F_name`)
1032+
*/
10231033
void ReadExternalFieldFromFile (
10241034
std::string read_fields_from_path, amrex::MultiFab* mf,
10251035
std::string F_name, std::string F_component);

0 commit comments

Comments
 (0)