Skip to content

Commit 2737981

Browse files
committed
Move 1D deposition into a separate function.
1 parent 7348362 commit 2737981

File tree

4 files changed

+131
-37
lines changed

4 files changed

+131
-37
lines changed

src/particles/spacecharge/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ target_sources(lib
44
Gauss3dPush.cpp
55
Gauss2p5dPush.cpp
66
GatherAndPush.cpp
7+
Deposit1D.cpp
78
HandleSpacecharge.cpp
89
PoissonSolve.cpp
910
)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* Copyright 2022-2025 The Regents of the University of California, through Lawrence
2+
* Berkeley National Laboratory (subject to receipt of any required
3+
* approvals from the U.S. Dept. of Energy). All rights reserved.
4+
*
5+
* This file is part of ImpactX.
6+
*
7+
* Authors: Ji Qiang
8+
* License: BSD-3-Clause-LBNL
9+
*/
10+
#ifndef IMPACT_DEPOSIT1D_PUSH_H
11+
#define IMPACT_DEPOSIT1D_PUSH_H
12+
13+
#include "particles/ImpactXParticleContainer.H"
14+
15+
#include <AMReX_REAL.H>
16+
17+
18+
namespace impactx::particles::spacecharge
19+
{
20+
/** Deposit charge using longitudinal binning for the calculation of space charge
21+
*
22+
* This function performs particle binning for particles in the beam along the
23+
* longitudinal direction, for the purposes of computing space charge in
24+
* so-called 2.5D models.
25+
*
26+
* @param[in] pc container of the particles that deposited rho
27+
* @param[out] beam_profile deposited charge density
28+
* @param[out] beam_profile_slope derivative of deposited charge density
29+
* @param[in] bin_max location of lower endpoint for binning
30+
* @param[in] bin_max location of upper endpoint for binning
31+
* @param[in] num_bins number of longitudinal bins
32+
*/
33+
void Deposit1D (
34+
ImpactXParticleContainer & pc,
35+
amrex::Real * beam_profile,
36+
amrex::Real * beam_profile_slope,
37+
amrex::Real bin_min,
38+
amrex::Real bin_max,
39+
int num_bins
40+
);
41+
42+
} // namespace impactx::particles::spacecharge
43+
44+
#endif // IMPACT_DEPOSIT1D_H
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* Copyright 2022-2025 The Regents of the University of California, through Lawrence
2+
* Berkeley National Laboratory (subject to receipt of any required
3+
* approvals from the U.S. Dept. of Energy). All rights reserved.
4+
*
5+
* This file is part of ImpactX.
6+
*
7+
* Authors: Ji Qiang
8+
* License: BSD-3-Clause-LBNL
9+
*/
10+
#include "Deposit1D.H"
11+
12+
#include "diagnostics/ReducedBeamCharacteristics.H"
13+
#include "particles/wakefields/ChargeBinning.H"
14+
15+
#include <AMReX_REAL.H>
16+
#include <AMReX_BLProfiler.H>
17+
#include <AMReX_GpuContainers.H>
18+
#include <AMReX_ParmParse.H>
19+
20+
#include <cmath>
21+
22+
23+
namespace impactx::particles::spacecharge
24+
{
25+
26+
void Deposit1D (
27+
ImpactXParticleContainer & pc,
28+
amrex::Real * beam_profile,
29+
amrex::Real * beam_profile_slope,
30+
amrex::Real bin_min,
31+
amrex::Real bin_max,
32+
int num_bins
33+
)
34+
{
35+
BL_PROFILE("impactx::spacecharge::Deposit1D");
36+
37+
using namespace amrex::literals;
38+
39+
amrex::ParticleReal const charge = pc.GetRefParticle().charge;
40+
41+
// Set parameters for charge deposition
42+
bool const is_unity_particle_weight = false;
43+
bool const GetNumberDensity = true;
44+
45+
amrex::Real const bin_size = (bin_max - bin_min) / (num_bins - 1); // number of evaluation points
46+
// Allocate memory for the charge profile
47+
amrex::Gpu::DeviceVector<amrex::Real> charge_distribution(num_bins + 1, 0.0);
48+
// Call charge deposition function
49+
impactx::particles::wakefields::DepositCharge1D(pc, charge_distribution, bin_min, bin_size, is_unity_particle_weight);
50+
51+
// Sum up all partial charge histograms to each MPI process to calculate
52+
// the global charge slope.
53+
amrex::ParallelAllReduce::Sum(
54+
charge_distribution.data(),
55+
charge_distribution.size(),
56+
amrex::ParallelDescriptor::Communicator()
57+
);
58+
59+
// Call charge density derivative function
60+
amrex::Gpu::DeviceVector<amrex::Real> slopes(charge_distribution.size() - 1, 0.0);
61+
impactx::particles::wakefields::DerivativeCharge1D(charge_distribution, slopes, bin_size,GetNumberDensity);
62+
63+
beam_profile = charge_distribution.data();
64+
beam_profile_slope = slopes.data();
65+
66+
}
67+
68+
} // namespace impactx::particles::spacecharge

src/particles/spacecharge/GatherAndPush.cpp

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "initialization/Algorithms.H"
1313
#include "particles/wakefields/ChargeBinning.H"
14+
#include "particles/spacecharge/Deposit1D.H"
1415

1516
#include <ablastr/particles/NodalFieldGather.H>
1617

@@ -37,6 +38,23 @@ namespace impactx::particles::spacecharge
3738

3839
amrex::ParticleReal const charge = pc.GetRefParticle().charge;
3940

41+
// Deposit 1D charge density in cases where it is required.
42+
amrex::Real * beam_profile = nullptr;
43+
amrex::Real * beam_profile_slope = nullptr;
44+
int num_bins = 129;
45+
46+
[[maybe_unused]] auto const [x_min, y_min, t_min, x_max, y_max, t_max] =
47+
pc.MinAndMaxPositions();
48+
49+
amrex::Real bin_min = t_min;
50+
amrex::Real bin_max = t_max;
51+
amrex::Real const bin_size = (bin_max - bin_min) / (num_bins - 1);
52+
53+
if (space_charge == SpaceChargeAlgo::True_2p5D) {
54+
55+
Deposit1D ( pc, beam_profile, beam_profile_slope, bin_min, bin_max, num_bins);
56+
}
57+
4058
// loop over refinement levels
4159
int const nLevel = pc.finestLevel();
4260
for (int lev = 0; lev <= nLevel; ++lev)
@@ -120,43 +138,6 @@ namespace impactx::particles::spacecharge
120138
auto prob_lo_2D = gm.ProbLoArray();
121139
prob_lo_2D[2] = 0.0_rt;
122140

123-
// Calculate z-dependent scaling by current
124-
int tp5d_bins = 129;
125-
amrex::ParmParse pp_algo("algo.space_charge");
126-
pp_algo.queryAddWithParser("gauss_charge_z_bins", tp5d_bins);
127-
128-
// Set parameters for charge deposition
129-
bool const is_unity_particle_weight = false;
130-
bool const GetNumberDensity = true;
131-
132-
// Measure beam size, extract the min, max of particle positions
133-
[[maybe_unused]] auto const [x_min, y_min, t_min, x_max, y_max, t_max] =
134-
pc.MinAndMaxPositions();
135-
136-
int const num_bins = tp5d_bins; // Set resolution
137-
amrex::Real const bin_min = t_min;
138-
amrex::Real const bin_max = t_max;
139-
amrex::Real const bin_size = (bin_max - bin_min) / (num_bins - 1); // number of evaluation points
140-
// Allocate memory for the charge profile
141-
amrex::Gpu::DeviceVector<amrex::Real> charge_distribution(num_bins + 1, 0.0);
142-
// Call charge deposition function
143-
impactx::particles::wakefields::DepositCharge1D(pc, charge_distribution, bin_min, bin_size, is_unity_particle_weight);
144-
145-
// Sum up all partial charge histograms to each MPI process to calculate
146-
// the global charge slope.
147-
amrex::ParallelAllReduce::Sum(
148-
charge_distribution.data(),
149-
charge_distribution.size(),
150-
amrex::ParallelDescriptor::Communicator()
151-
);
152-
153-
// Call charge density derivative function
154-
amrex::Gpu::DeviceVector<amrex::Real> slopes(charge_distribution.size() - 1, 0.0);
155-
impactx::particles::wakefields::DerivativeCharge1D(charge_distribution, slopes, bin_size,GetNumberDensity);
156-
157-
amrex::Real const * const beam_profile_slope = slopes.data();
158-
amrex::Real const * const beam_profile = charge_distribution.data();
159-
160141
// group together constants for the momentum push
161142
amrex::ParticleReal const chargesign = charge / std::abs(charge);
162143

0 commit comments

Comments
 (0)