Skip to content
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

[WIP] 1D CSR Model #627

Draft
wants to merge 48 commits into
base: development
Choose a base branch
from

Conversation

xbojanic
Copy link

@xbojanic xbojanic commented May 31, 2024

This PR implements a 1D CSR model. Currently, this is the ultrarelativistic steady-state wakefield model (eg, eq 19 of
E. L. Saldin et al, NIMA 398, p. 373-394 (1997), DOI:10.1016/S0168-9002(97)00822-X).

This includes:

  • wake function calculation
  • beam profile convolution
  • apply the force "kick" to the particle momentum.
  • benchmark example

Close #452

@ax3l ax3l requested review from ax3l and cemitch99 May 31, 2024 22:45
@ax3l ax3l added the component: CSR coherent synchrotron radiation models label May 31, 2024
Copy link
Member

@cemitch99 cemitch99 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congratulations on posting your first ImpactX pull request! The changes to the examples (your local tests) probably don't need to appear in this request. We can add a new test that includes CSR, once the wakefield kick is fully implemented.

Alex Bojanich and others added 21 commits June 3, 2024 16:57
…es into it. Created 2 new files for the wake force momentum kick.
…ge_Deposit_Step

Created the directory wakefields for all new wakefield scripts.
…ge_Deposit_Step

Deleted copy files and added paths to new wakefields directory for compiling
xbojanic and others added 7 commits June 7, 2024 14:08
Co-authored-by: Chad Mitchell <46825199+cemitch99@users.noreply.github.com>
Co-authored-by: Chad Mitchell <46825199+cemitch99@users.noreply.github.com>
Co-authored-by: Chad Mitchell <46825199+cemitch99@users.noreply.github.com>
Merge remote-tracking branch 'xbojanic/Charge_Deposit_Step' into Charge_Deposit_Step
/*
else if constexpr (std::is_same_v<std::decay_t<decltype(element)>, ExactSbend>) //Currently internal calculation for m_rc
{
R = element.m_rc;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case (the ExactSBend) will cause a problem because m_rc is not a member variable. Instead, rc is computed internally from other parameters. For now, we could add a comment that this case will be addressed later. We can make a simple change to ExactSBend to address this.

@ax3l ax3l mentioned this pull request Jun 17, 2024
src/ImpactX.cpp Outdated Show resolved Hide resolved
Comment on lines +212 to +300
/*bool element_has_csr = false; // Updates to true for example with bend element
double R = 0.0; // Updates for bend element rc

//Define lambda function inside of std::visit
std::visit([&R, &element_has_csr](auto &&element)
{
if constexpr (std::is_same_v<std::decay_t<decltype(element)>, Sbend>)
{
R = element.m_rc;
std::cout << "My radius of curvature is:" << R << std::endl;
element_has_csr = true;
}
else if constexpr (std::is_same_v<std::decay_t<decltype(element)>, CFbend>)
{
R = element.m_rc;
std::cout << "My radius of curvature is:" << R << std::endl;
element_has_csr = true;
}

else if constexpr (std::is_same_v<std::decay_t<decltype(element)>, ExactSbend>) //Currently internal calculation for m_rc
{
R = element.m_rc;
std::cout << "My radius of curvature is:" << R << std::endl;
element_has_csr = true;
}

}, element_variant);

Enter loop if lattice has bend element
if (element_has_csr)
{
// Measure beam size, extract the min, max of particle positions
auto const [x_min, y_min, t_min, x_max, y_max, t_max] =
amr_data->m_particle_container->MinAndMaxPositions();

using amrex::Real;

// Set parameters for charge deposition
bool is_unity_particle_weight = true;
bool GetNumberDensity = true;

int num_bins = 100; // Set resolution
Real bin_min = t_min;
Real bin_max = t_max;
Real bin_size = (bin_max - bin_min) / num_bins;

// Allocate memory for the charge profile
Real* dptr_data = new Real[num_bins]();
auto& particle_container = *(amr_data->m_particle_container);

//Call charge deposition function
DepositCharge1D(particle_container, dptr_data, num_bins, bin_min, bin_size, is_unity_particle_weight);

// Call charge density derivative function
std::vector<double> charge_distribution(dptr_data, dptr_data + num_bins);
std::vector<double> slopes(num_bins - 1);
DerivativeCharge1D(charge_distribution.data(), slopes.data(), num_bins, bin_size, GetNumberDensity); //Use number derivatives for convolution with CSR

// Call wake function

// Read in external variable bunch_charge
std::cout << "My beam charge is:" << bunch_charge << std::endl;

std::vector<double> wake_function(num_bins);
for (int i = 0; i < num_bins; ++i)
{
double s = bin_min + i * bin_size;
wake_function[i] = w_l_csr(s, R, bunch_charge);
}

// Call convolution function
std::vector<double> convoluted_wakefield(2 * num_bins - 1);
convolve_fft(slopes.data(), wake_function.data(), slopes.size(), wake_function.size(), bin_size, convoluted_wakefield.data(), 1);

//Check convolution
std::cout << "Convoluted wakefield: ";
std::ofstream outfile("convoluted_wakefield.txt");
for (int i = 0; i < convoluted_wakefield.size(); ++i)
{
std::cout << convoluted_wakefield[i] << " ";
outfile << convoluted_wakefield[i] << std::endl;
}
std::cout << std::endl;
outfile.close();
delete[] dptr_data;

// Kick particles with wake
impactx::wakepush::WakePush(particle_container, convoluted_wakefield, bin_size);
}*/

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
Comment on lines +73 to +75
//Other dimensions (x, y) remain unchanged
// px = px;
// py = py;

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
amrex::ParallelFor(np, [=] AMREX_GPU_DEVICE (int i)
{
//Access SoA Real data
amrex::ParticleReal & AMREX_RESTRICT x = part_x[i];

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable x is not used.
{
//Access SoA Real data
amrex::ParticleReal & AMREX_RESTRICT x = part_x[i];
amrex::ParticleReal & AMREX_RESTRICT y = part_y[i];

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable y is not used.
amrex::ParticleReal & AMREX_RESTRICT y = part_y[i];
amrex::ParticleReal & AMREX_RESTRICT z = part_z[i];

amrex::ParticleReal & AMREX_RESTRICT px = part_px[i];

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable px is not used.
amrex::ParticleReal & AMREX_RESTRICT z = part_z[i];

amrex::ParticleReal & AMREX_RESTRICT px = part_px[i];
amrex::ParticleReal & AMREX_RESTRICT py = part_py[i];

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable py is not used.
@@ -219,7 +221,7 @@
amrex::ParticleReal kin_energy = 0.0; // Beam kinetic energy (MeV)
pp_dist.get("kin_energy", kin_energy);

amrex::ParticleReal bunch_charge = 0.0; // Bunch charge (C)
amrex::ParticleReal bunch_charge = 0.0; // Bunch charge (C) - Define externally to make global variable

Check notice

Code scanning / CodeQL

Local variable hides global variable Note

Local variable bunch_charge hides a
global variable
with the same name.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: CSR coherent synchrotron radiation models
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1D CSR Model
3 participants