-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Synchronize velocity for diagnostics (#1751)
This PR allows the synchronization in time of the particle velocities and positions when generating diagnostics. Without this option, the particle velocities will lag behind the position by a half time step. This adds the boolean input parameter `warpx.synchronize_velocity_for_diagnostics` to turn on this option, defaulting to false. There are several pieces to this PR: - Changes to `MultiDiagnostic` and `MultiReducedDiags` adding routines to check if any diagnostics will be done - Adds a call to `PushP` to just before the diagnostics are done (to get the updated fields from the electrostatic calculation) - Add the appropriate documentation What `Evolve` does is if the synchronization is to be done, advance the velocity a half step just before the diagnostics and sets `is_synchronized=true`. Then at the start of the next step, if `is_synchronized` is true, push the velocities back a half step to be ready for the full leap frog advance. Comments: - Is the documentation in the correct place in parameters.rst? - The reduced diagnostics could perhaps use the new DoDiags method instead of accessing `m_intervals` in its ComputeDiags. - This PR leaves the original PushP unchanged, even though it is possibly buggy. That PushP fetches the fields, but uses the particle positions before the particle boundary conditions have been applied, leading to a possible out of bounds reference. Also, that PushP may not be consistent with the backwards PushP since the fields may had changed. Comments are added to the code to note this potential problem. I avoided changing this since it breaks many CI tests. --------- Co-authored-by: Edoardo Zoni <ezoni@lbl.gov> Co-authored-by: Axel Huebl <axel.huebl@plasma.ninja> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Edoardo Zoni <59625522+EZoni@users.noreply.github.com>
- Loading branch information
1 parent
546a972
commit b858e36
Showing
13 changed files
with
180 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
Examples/Tests/single_particle/analysis_synchronize_velocity.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright 2025 David Grote | ||
# | ||
# This file is part of WarpX. | ||
# | ||
# License: BSD-3-Clause-LBNL | ||
|
||
import sys | ||
|
||
import numpy as np | ||
import yt | ||
|
||
# scipy.constants use CODATA2022 | ||
# from scipy.constants import e, m_e, c | ||
|
||
# These are CODATA2018 values, as used in WarpX | ||
e = 1.602176634e-19 | ||
m_e = 9.1093837015e-31 | ||
c = 299792458.0 | ||
|
||
# Integrate the test particle 5 timesteps, ending up with the position | ||
# and velocity synchronized. | ||
# In the simulation, with the synchronize_velocity_for_diagnostics flag set, | ||
# the velocity will be synchronized at the end of step 5 when the diagnostics | ||
# are written (even though that is not the final time step). | ||
|
||
z = 0.1 | ||
uz = 0.0 | ||
Ez = -1.0 | ||
dt = 1.0e-6 | ||
|
||
# Half backward advance of velocity | ||
uz -= -e / m_e * Ez * dt / 2.0 | ||
|
||
# Leap frog advance | ||
for _ in range(5): | ||
uz += -e / m_e * Ez * dt | ||
g = np.sqrt((uz / c) ** 2 + 1.0) | ||
z += (uz / g) * dt | ||
|
||
# Add half v advance to synchronize | ||
uz += -e / m_e * Ez * dt / 2.0 | ||
|
||
filename = sys.argv[1] | ||
ds = yt.load(filename) | ||
ad = ds.all_data() | ||
z_sim = ad["electron", "particle_position_x"] | ||
uz_sim = ad["electron", "particle_momentum_z"] / m_e | ||
|
||
print(f"Analysis Z = {z:18.16f}, Uz = {uz:18.10f}") | ||
print(f"Simulation Z = {z_sim.v[0]:18.16f}, Uz = {uz_sim.v[0]:18.10f}") | ||
|
||
tolerance_rel = 1.0e-15 | ||
error_rel = np.abs((uz - uz_sim.v[0]) / uz) | ||
|
||
print("error_rel : " + str(error_rel)) | ||
print("tolerance_rel: " + str(tolerance_rel)) | ||
|
||
assert error_rel < tolerance_rel |
36 changes: 36 additions & 0 deletions
36
Examples/Tests/single_particle/inputs_test_1d_synchronize_velocity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
max_step = 8 | ||
amr.n_cell = 8 | ||
amr.max_level = 0 | ||
amr.blocking_factor = 8 | ||
amr.max_grid_size = 8 | ||
geometry.dims = 1 | ||
geometry.prob_lo = 0 | ||
geometry.prob_hi = 3 | ||
|
||
# Boundary condition | ||
boundary.field_lo = pec | ||
boundary.field_hi = pec | ||
|
||
algo.maxwell_solver = none | ||
|
||
warpx.const_dt = 1.e-6 | ||
warpx.synchronize_velocity_for_diagnostics = 1 | ||
|
||
# Order of particle shape factors | ||
algo.particle_shape = 1 | ||
|
||
particles.species_names = electron | ||
electron.species_type = electron | ||
electron.injection_style = "SingleParticle" | ||
electron.single_particle_pos = 0.0 0.0 0.1 | ||
electron.single_particle_u = 0.0 0.0 0.0 | ||
electron.single_particle_weight = 1.0 | ||
|
||
# Apply a uniform Ez | ||
particles.E_ext_particle_init_style = constant | ||
particles.E_external_particle = 0.0 0.0 -1.0 | ||
|
||
# Diagnostics | ||
diagnostics.diags_names = diag1 | ||
diag1.intervals = 5 | ||
diag1.diag_type = Full |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters