Skip to content

Commit

Permalink
[Draft] Mayes 2018 Test
Browse files Browse the repository at this point in the history
  • Loading branch information
ax3l committed Oct 7, 2022
1 parent 1cb9e77 commit b81c7af
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
72 changes: 62 additions & 10 deletions examples/expanding_beam/run_expanding.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#!/usr/bin/env python3
#
# Copyright 2022 ImpactX contributors
# Authors: Axel Huebl, Chad Mitchell
# Authors: Axel Huebl, Christopher E. Mayes
# License: BSD-3-Clause-LBNL
#
# -*- coding: utf-8 -*-

import numpy as np

import amrex
from impactx import ImpactX, RefPart, distribution, elements

pp_amr = amrex.ParmParse("amr")
# pp_amr.addarr("n_cell", [128, 128, 128])
pp_amr.addarr("n_cell", [40, 40, 32])

sim = ImpactX()
Expand All @@ -21,39 +24,88 @@
sim.prob_relative = 1.0

# beam diagnostics
# sim.diagnostics = False # benchmarking
sim.diagnostics = False
sim.slice_step_diagnostics = False

# domain decomposition & space charge mesh
sim.init_grids()

# load a 2 GeV electron beam with an initial
# load a 10 MeV electron beam with an initial
# unnormalized rms emittance of 2 nm
energy_MeV = 250 # reference energy
energy_MeV = 10 # reference energy
mass_MeV = 0.510998950 # electron mass in MeV/c^2
bunch_charge_C = 1.0e-9 # used with space charge
npart = 10000 # number of macro particles
npart = int(10000) # number of macro particles

# reference particle
ref = sim.particle_container().ref_particle()
ref.set_charge_qe(-1.0).set_mass_MeV(0.510998950).set_energy_MeV(energy_MeV)
ref.set_charge_qe(-1.0).set_mass_MeV(mass_MeV).set_energy_MeV(energy_MeV)

# particle bunch
r = 1.0 # aspect ratio = sigma_z / sigma_perp = 0.01 - 10
sigma_z = 1.0e-6 # mm
sigma_r = sigma_z / r
gamma = energy_MeV / mass_MeV
beta = (1.0 - (1.0 / gamma) ** 2) ** 0.5
c0 = 2.99792458e8 # speed of light in m/s
sigma_t = sigma_z / (c0 * beta)
print(f"sigma_t={sigma_t}s")

distr = distribution.Waterbag(
sigmaX=4.472135955e-4,
sigmaY=4.472135955e-4,
sigmaT=9.12241869e-7,
sigmaX=sigma_r,
sigmaY=sigma_r,
sigmaT=sigma_t,
sigmaPx=0.0,
sigmaPy=0.0,
sigmaPt=0.0,
)
sim.add_particles(bunch_charge_C, distr, npart)

# design the accelerator lattice
sim.lattice.append(elements.Drift(ds=6.0, nslice=30))
sim.lattice.append(elements.Drift(ds=1.0, nslice=30))

# run simulation
sim.evolve()

# calculate phase space
# hack:
import matplotlib.pyplot as plt

num_plots_per_row = 2
f, axs = plt.subplots(1, num_plots_per_row, figsize=(7, 2))
ax_x_px = axs[0]
ax_z_pz = axs[1]

pc = sim.particle_container()
lev = pc.GetParticles(0)
for tile_ind, pt in lev.items():
# positions + id + cpuid
aos = pt.GetArrayOfStructs()
aos_arr = np.array(aos, copy=False)

# momentum & particle weight
real_arrays = pt.GetStructOfArrays().GetRealData()
px = np.array(real_arrays[0], copy=False)
pz = np.array(real_arrays[2], copy=False)

print(f"tile_ind={tile_ind}, pt={pt}")
print(f"aos_arr={aos_arr}, aos_arr.shape={aos_arr.shape}")
print(f"px={px}, px.shape={px.shape}")

ax_x_px.scatter(aos_arr[()]["x"], px)
ax_z_pz.scatter(aos_arr[()]["z"], pz)
plt.show()

# MPI reduce phase space
# TODO SUM up histograms via MPI

# plot phase space
# import matplotlib.pyplot as plt
# f = plt.figure()
# ax = plt.gca()
# ax.scatter()

# clean shutdown
# note: timers turned stop here
del sim
amrex.finalize()
4 changes: 2 additions & 2 deletions src/particles/spacecharge/PoissonSolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ namespace impactx::spacecharge
amrex::Real const mlmg_relative_tolerance = 1.e-7; // relative TODO: make smaller for SP
amrex::Real const mlmg_absolute_tolerance = 0.0; // ignored

int const mlmg_max_iters = 100;
int const mlmg_verbosity = 1;
int const mlmg_max_iters = 250;
int const mlmg_verbosity = 2;

struct PoissonBoundaryHandler {
amrex::Array<amrex::LinOpBCType, AMREX_SPACEDIM> const lobc = {
Expand Down

0 comments on commit b81c7af

Please sign in to comment.