Skip to content

Commit

Permalink
Reference Particle: GPU Capable (#230)
Browse files Browse the repository at this point in the history
Make all methods of the reference particle type:
- GPU capable (`__host__ __device__`)
- forced inline

Create a separate header file for the type.
  • Loading branch information
ax3l authored Aug 31, 2022
1 parent 4483972 commit be22037
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 203 deletions.
90 changes: 2 additions & 88 deletions src/particles/ImpactXParticleContainer.H
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#ifndef IMPACTX_PARTICLE_CONTAINER_H
#define IMPACTX_PARTICLE_CONTAINER_H

#include "ReferenceParticle.H"

#include <AMReX_AmrCoreFwd.H>
#include <AMReX_BaseFwd.H>
#include <AMReX_MultiFab.H>
Expand Down Expand Up @@ -70,94 +72,6 @@ namespace impactx
};
};

/** This struct stores the reference particle attributes
* stored in ImpactXParticleContainer
*/
struct RefPart
{
amrex::ParticleReal s = 0.0; ///< integrated orbit path length, in meters
amrex::ParticleReal x = 0.0; ///< horizontal position x, in meters
amrex::ParticleReal y = 0.0; ///< vertical position y, in meters
amrex::ParticleReal z = 0.0; ///< longitudinal position y, in meters
amrex::ParticleReal t = 0.0; ///< clock time * c in meters
amrex::ParticleReal px = 0.0; ///< momentum in x, normalized to proper velocity
amrex::ParticleReal py = 0.0; ///< momentum in y, normalized to proper velocity
amrex::ParticleReal pz = 0.0; ///< momentum in z, normalized to proper velocity
amrex::ParticleReal pt = 0.0; ///< energy deviation, normalized by rest energy
amrex::ParticleReal mass = 0.0; ///< reference rest mass, in kg
amrex::ParticleReal charge = 0.0; ///< reference charge, in C

/** Get reference particle relativistic gamma
*
* @returns relativistic gamma
*/
amrex::ParticleReal
gamma () const;

/** Get reference particle relativistic beta
*
* @returns relativistic beta
*/
amrex::ParticleReal
beta () const;

/** Get reference particle beta*gamma
*
* @returns relativistic beta*gamma
*/
amrex::ParticleReal
beta_gamma () const;

/** Get reference particle rest mass
*
* @returns rest mass in MeV/c^2
*/
amrex::ParticleReal
mass_MeV () const;

/** Set reference particle rest mass
*
* @param massE particle rest mass (MeV/c^2)
*/
RefPart &
set_mass_MeV (amrex::ParticleReal const massE);

/** Get reference particle energy
*
* @returns kinetic energy in MeV
*/
amrex::ParticleReal
energy_MeV () const;

/** Set reference particle energy
*
* @param energy initial kinetic energy (MeV)
*/
RefPart &
set_energy_MeV (amrex::ParticleReal const energy);

/** Get reference particle charge
*
* @returns charge in multiples of the (positive) elementary charge
*/
amrex::ParticleReal
charge_qe () const;

/** Set reference particle charge
*
* @param charge_qe in multiples of the (positive) elementary charge
*/
RefPart &
set_charge_qe (amrex::ParticleReal const charge_qe);

/** Get reference particle charge to mass ratio
*
* @returns charge to mass ratio (elementary charge/eV)
*/
amrex::ParticleReal
qm_qeeV () const;
};

/** Beam Particles in ImpactX
*
* This class stores particles, distributed over MPI ranks.
Expand Down
115 changes: 1 addition & 114 deletions src/particles/ImpactXParticleContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ namespace impactx
ImpactXParticleContainer::SetRefParticle (RefPart const refpart)
{
m_refpart = refpart;
}

RefPart &
} RefPart &
ImpactXParticleContainer::GetRefParticle ()
{
return m_refpart;
Expand All @@ -140,117 +138,6 @@ namespace impactx
return m_refpart;
}

// Reference particle helper functions

RefPart &
RefPart::set_mass_MeV (amrex::ParticleReal const massE)
{
using namespace amrex::literals;

AMREX_ASSERT_WITH_MESSAGE(massE != 0.0_prt,
"set_mass_MeV: Mass cannot be zero!");

constexpr amrex::ParticleReal MeVc2_kg = 1.78266192e-30;
mass = massE * MeVc2_kg;

// re-scale pt and pz
if (pt != 0.0_prt)
{
pt = -energy_MeV() / massE - 1.0_prt;
pz = sqrt(pow(pt, 2) - 1.0_prt);
}

return *this;
}

RefPart &
RefPart::set_energy_MeV (amrex::ParticleReal const energy)
{
using namespace amrex::literals;

AMREX_ASSERT_WITH_MESSAGE(mass != 0.0_prt,
"set_energy_MeV: Set mass first!");

px = 0.0;
py = 0.0;
pt = -energy / mass_MeV() - 1.0_prt;
pz = sqrt(pow(pt, 2) - 1.0_prt);

return *this;
}

RefPart &
RefPart::set_charge_qe (amrex::ParticleReal const charge_qe)
{
using namespace amrex::literals;

constexpr double qe = 1.602176634e-19;
this->charge = charge_qe * qe;

return *this;
}

amrex::ParticleReal
RefPart::gamma () const
{
amrex::ParticleReal ref_gamma = -pt;
return ref_gamma;
}

amrex::ParticleReal
RefPart::beta () const
{
using namespace amrex::literals;

amrex::ParticleReal ref_gamma = -pt;
amrex::ParticleReal ref_beta = sqrt(1.0_prt - 1.0_prt/pow(ref_gamma,2));
return ref_beta;
}

amrex::ParticleReal
RefPart::beta_gamma () const
{
using namespace amrex::literals;

amrex::ParticleReal ref_gamma = -pt;
amrex::ParticleReal ref_betagamma = sqrt(pow(ref_gamma, 2) - 1.0_prt);
return ref_betagamma;
}

amrex::ParticleReal
RefPart::mass_MeV () const
{
using namespace amrex::literals;

constexpr double MeVc2_kg = 1.78266192e-30;
return amrex::ParticleReal(mass / MeVc2_kg);
}

amrex::ParticleReal
RefPart::energy_MeV () const
{
using namespace amrex::literals;

amrex::ParticleReal ref_gamma = -pt;
amrex::ParticleReal ref_energy = mass_MeV() * (ref_gamma - 1.0_prt);
return ref_energy;
}

amrex::ParticleReal
RefPart::charge_qe () const
{
using namespace amrex::literals;

constexpr double qe = 1.602176634e-19;
return amrex::ParticleReal(charge / qe);
}

amrex::ParticleReal
RefPart::qm_qeeV () const
{
return charge / mass;
}

std::tuple<
amrex::ParticleReal, amrex::ParticleReal,
amrex::ParticleReal, amrex::ParticleReal,
Expand Down
Loading

0 comments on commit be22037

Please sign in to comment.