-
Notifications
You must be signed in to change notification settings - Fork 29
2D Space Charge in Particle Tracking #909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ax3l
wants to merge
46
commits into
BLAST-ImpactX:development
Choose a base branch
from
ax3l:topic-space-charge-particles-2D
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,125
−79
Open
Changes from 26 commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
ca47387
2D: ForceFromSelfFields
ax3l b02e732
[Draft] GatherAndPush
ax3l 273c812
3D Force & Push
ax3l ee8485d
2D Poisson Solve: Flattened Rho
ax3l 5478a5b
2D Force & Push
ax3l 3896b3e
Read current for 2D PIC, add input for benchmark example.
cemitch99 d0b5eba
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 76b80b5
2D phi field
ax3l 2f1dfd0
Multi-Level rho_2d
ax3l 36599f1
Python: `flatten_charge_to_2D`
ax3l 111ab86
Add modified push constants and expanding beam example.
cemitch99 a3cc21c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3790b71
Update Tests for Phi
ax3l 19b3fce
HandleSpaceCharge: 2D Support
ax3l edc516e
Test: Deposit Again
ax3l 5c80023
Merge remote-tracking branch 'mainline/development' into topic-space-…
ax3l 334f95f
Draft: update candidate push.
cemitch99 86b87bd
Comment unused variable in GatherAndPush.cpp to fix CI.
cemitch99 26c985e
Remove num_guards_phi[2]=0 outside conditional in AmrCoreData.cpp.
cemitch99 d73ce28
Fix push constants.
cemitch99 cf34656
Add analysis scripts for tests.
cemitch99 0565103
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f608b30
Update ncells in test_charge_deposition_2D.py
cemitch99 0ce3537
Add Python equivalent tests.
cemitch99 e96b9a2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6cfefb7
Update documentation.
cemitch99 8da14c6
Update examples/CMakeLists.txt
cemitch99 ba7764e
Relax tolerance in analysis_expanding_fft_2D.py
cemitch99 376ebee
Relax tolerance in analysis_fodo_2d_sc.py
cemitch99 1926720
Relax tolerance for analysis_expanding_fft_2D.py
cemitch99 7db9d76
Relax tolerance in analysis_fodo_2d_sc.py
cemitch99 5e0bce6
Relax analysis_fodo_2d_sc.py tolerance again.
cemitch99 20ff802
Relax tolerance in analysis_fodo_2d_sc.py
cemitch99 bbbbb8d
Relax tolerance in analysis_fodo_2d_sc.py
cemitch99 a20b595
Merge development into 2D space charge PR (#9)
cemitch99 6859b3d
Same Grid, Same Mass
ax3l 954251d
Same grid, same mass for FODO.
cemitch99 8d77c8e
Add plotting script.
cemitch99 9d2b3fc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e8d4e5b
Add Weiqun's fix for duplicate values on shared nodes.
cemitch99 f7b6e14
Merge branch 'development' into topic-space-charge-particles-2D
cemitch99 0b4c71f
Doc: NAPAC25 Preprint Link Text (#10)
cemitch99 864b8fa
Build phi and force with flatten boxes in True_2D case
WeiqunZhang 033dadb
Update PoissonSolve for the new layout of phi and rho
WeiqunZhang d95f987
Fix index type
WeiqunZhang 739619d
Update examples/fodo_space_charge/run_fodo_2d_sc.py
cemitch99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,106 @@ | ||
| #!/usr/bin/env python3 | ||
| # | ||
| # Copyright 2022-2023 ImpactX contributors | ||
| # Authors: Axel Huebl, Chad Mitchell | ||
| # License: BSD-3-Clause-LBNL | ||
| # | ||
|
|
||
| import numpy as np | ||
| import openpmd_api as io | ||
| from scipy.stats import moment | ||
|
|
||
|
|
||
| def get_moments(beam): | ||
| """Calculate standard deviations of beam position & momenta | ||
| and emittance values | ||
|
|
||
| Returns | ||
| ------- | ||
| sigx, sigy, sigt, emittance_x, emittance_y, emittance_t | ||
| """ | ||
| sigx = moment(beam["position_x"], moment=2) ** 0.5 # variance -> std dev. | ||
| sigpx = moment(beam["momentum_x"], moment=2) ** 0.5 | ||
| sigy = moment(beam["position_y"], moment=2) ** 0.5 | ||
| sigpy = moment(beam["momentum_y"], moment=2) ** 0.5 | ||
| sigt = moment(beam["position_t"], moment=2) ** 0.5 | ||
| sigpt = moment(beam["momentum_t"], moment=2) ** 0.5 | ||
|
|
||
| epstrms = beam.cov(ddof=0) | ||
| emittance_x = (sigx**2 * sigpx**2 - epstrms["position_x"]["momentum_x"] ** 2) ** 0.5 | ||
| emittance_y = (sigy**2 * sigpy**2 - epstrms["position_y"]["momentum_y"] ** 2) ** 0.5 | ||
| emittance_t = (sigt**2 * sigpt**2 - epstrms["position_t"]["momentum_t"] ** 2) ** 0.5 | ||
|
|
||
| return (sigx, sigy, sigt, emittance_x, emittance_y, emittance_t) | ||
|
|
||
|
|
||
| # initial/final beam | ||
| series = io.Series("diags/openPMD/monitor.h5", io.Access.read_only) | ||
| last_step = list(series.iterations)[-1] | ||
| initial = series.iterations[1].particles["beam"].to_df() | ||
| final = series.iterations[last_step].particles["beam"].to_df() | ||
|
|
||
| # compare number of particles | ||
| num_particles = 10000 | ||
| assert num_particles == len(initial) | ||
| assert num_particles == len(final) | ||
|
|
||
| print("Initial Beam:") | ||
| sigx, sigy, sigt, emittance_x, emittance_y, emittance_t = get_moments(initial) | ||
| print(f" sigx={sigx:e} sigy={sigy:e} sigt={sigt:e}") | ||
| print( | ||
| f" emittance_x={emittance_x:e} emittance_y={emittance_y:e} emittance_t={emittance_t:e}" | ||
| ) | ||
|
|
||
| atol = 0.0 # ignored | ||
| rtol = 1.5 * num_particles**-0.5 # from random sampling of a smooth distribution | ||
| print(f" rtol={rtol} (ignored: atol~={atol})") | ||
|
|
||
| assert np.allclose( | ||
| [sigx, sigy, sigt, emittance_x, emittance_y, emittance_t], | ||
| [ | ||
| 5.0e-004, | ||
| 5.0e-004, | ||
| 1.0e-003, | ||
| 0.0e-006, | ||
| 0.0e-006, | ||
| 0.0e-006, | ||
| ], | ||
| rtol=rtol, | ||
| atol=atol, | ||
| ) | ||
|
|
||
|
|
||
| print("") | ||
| print("Final Beam:") | ||
| sigx, sigy, sigt, emittance_x, emittance_y, emittance_t = get_moments(final) | ||
| print(f" sigx={sigx:e} sigy={sigy:e} sigt={sigt:e}") | ||
| print( | ||
| f" emittance_x={emittance_x:e} emittance_y={emittance_y:e} emittance_t={emittance_t:e}" | ||
| ) | ||
|
|
||
| atol = 0.0 # ignored | ||
| rtol = 2.0 * num_particles**-0.5 # from random sampling of a smooth distribution | ||
| print(f" rtol={rtol} (ignored: atol~={atol})") | ||
|
|
||
| assert np.allclose( | ||
| [sigx, sigy, sigt], | ||
| [ | ||
| 1.0e-003, | ||
| 1.0e-003, | ||
| 1.0e-003, | ||
| ], | ||
| rtol=rtol, | ||
| atol=atol, | ||
| ) | ||
| atol = 1.0e-8 | ||
| rtol = 0.0 # ignored | ||
| assert np.allclose( | ||
| [emittance_x, emittance_y, emittance_t], | ||
| [ | ||
| 0.0, | ||
| 0.0, | ||
| 0.0, | ||
| ], | ||
| rtol=rtol, | ||
| atol=atol, | ||
| ) |
This file contains hidden or 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,49 @@ | ||
| ############################################################################### | ||
| # Particle Beam(s) | ||
| ############################################################################### | ||
| beam.npart = 10000 # outside tests, use 1e5 or more | ||
| beam.units = static | ||
| beam.kin_energy = 250.0 | ||
| beam.current = 0.15 | ||
| beam.particle = proton | ||
| beam.distribution = kvdist | ||
| beam.lambdaX = 5.0e-4 | ||
| beam.lambdaY = beam.lambdaX | ||
| beam.lambdaT = 1.0e-3 #result should not depend on this value | ||
| beam.lambdaPx = 0.0 | ||
| beam.lambdaPy = 0.0 | ||
| beam.lambdaPt = 0.0 | ||
|
|
||
|
|
||
| ############################################################################### | ||
| # Beamline: lattice elements and segments | ||
| ############################################################################### | ||
| lattice.elements = monitor drift1 monitor | ||
| lattice.nslice = 100 | ||
|
|
||
| drift1.type = drift | ||
| drift1.ds = 10.612823669911099 #doubling-distance | ||
|
|
||
| monitor.type = beam_monitor | ||
| monitor.backend = h5 | ||
|
|
||
|
|
||
| ############################################################################### | ||
| # Algorithms | ||
| ############################################################################### | ||
| algo.particle_shape = 2 | ||
| #algo.track = "envelope" | ||
| algo.space_charge = 2D | ||
| algo.poisson_solver = "fft" | ||
|
|
||
| amr.n_cell = 32 32 1 | ||
| amr.blocking_factor_x = 16 | ||
| amr.blocking_factor_y = 16 | ||
| amr.blocking_factor_z = 1 | ||
|
|
||
| geometry.prob_relative = 1.1 | ||
|
|
||
| ############################################################################### | ||
| # Diagnostics | ||
| ############################################################################### | ||
| diag.slice_step_diagnostics = true |
This file contains hidden or 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,68 @@ | ||
| #!/usr/bin/env python3 | ||
| # | ||
| # Copyright 2022-2023 ImpactX contributors | ||
| # Authors: Axel Huebl, Chad Mitchell | ||
| # License: BSD-3-Clause-LBNL | ||
| # | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| from impactx import ImpactX, distribution, elements | ||
|
|
||
| sim = ImpactX() | ||
|
|
||
| # set numerical parameters and IO control | ||
| sim.max_level = 0 | ||
| sim.n_cell = [32, 32, 1] | ||
| sim.blocking_factor_x = [16] | ||
| sim.blocking_factor_y = [16] | ||
| sim.blocking_factor_z = [1] | ||
|
|
||
| sim.particle_shape = 2 # B-spline order | ||
| sim.space_charge = "2D" | ||
| sim.poisson_solver = "fft" | ||
| sim.dynamic_size = True | ||
| sim.prob_relative = [1.1] | ||
|
|
||
| # beam diagnostics | ||
| # sim.diagnostics = False # benchmarking | ||
| sim.slice_step_diagnostics = True | ||
|
|
||
| # domain decomposition & space charge mesh | ||
| sim.init_grids() | ||
|
|
||
| # load a 2 GeV electron beam with an initial | ||
| # unnormalized rms emittance of 2 nm | ||
| kin_energy_MeV = 250 # reference energy | ||
| beam_current_A = 0.15 # beam current | ||
| npart = 10000 # number of macro particles (outside tests, use 1e5 or more) | ||
|
|
||
| # reference particle | ||
| ref = sim.particle_container().ref_particle() | ||
| ref.set_charge_qe(1.0).set_mass_MeV(938.27208816).set_kin_energy_MeV(kin_energy_MeV) | ||
|
|
||
| # particle bunch | ||
| distr = distribution.KVdist( | ||
| lambdaX=5.0e-4, | ||
| lambdaY=5.0e-4, | ||
| lambdaT=1.0e-3, | ||
| lambdaPx=0.0, | ||
| lambdaPy=0.0, | ||
| lambdaPt=0.0, | ||
| ) | ||
| sim.add_particles(beam_current_A, distr, npart) | ||
|
|
||
| # add beam diagnostics | ||
| monitor = elements.BeamMonitor("monitor", backend="h5") | ||
|
|
||
| # design the accelerator lattice | ||
| doubling_distance = 10.612823669911099 | ||
|
|
||
| sim.lattice.extend( | ||
| [monitor, elements.Drift(name="d1", ds=doubling_distance, nslice=100), monitor] | ||
| ) | ||
|
|
||
| # run simulation | ||
| sim.track_particles() | ||
|
|
||
| # clean shutdown | ||
| sim.finalize() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want 100% the same AMReX blocks ("grids output"):