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

Use KineticEnergy instead of getEnergy #5786

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions Source/Particles/Algorithms/KineticEnergy.H
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@

#include "AMReX_Extension.H"
#include "AMReX_GpuQualifiers.H"
#include "AMReX_Math.H"
#include "AMReX_REAL.H"

#include <cmath>
@@ -22,28 +23,30 @@ namespace Algorithms{
* \brief Computes the kinetic energy of a particle.
* This method should not be used with photons.
*
* @tparam T floating-point type to be used in internal calculations (by default equal to amrex::ParticleReal)
* @param[in] ux x component of the particle momentum (code units)
* @param[in] uy y component of the particle momentum (code units)
* @param[in] uz z component of the particle momentum (code units)
* @param[in] mass mass of the particle (in S.I. units)
*
* @return the kinetic energy of the particle (in S.I. units)
*/
template <typename T = amrex::ParticleReal>
AMREX_GPU_HOST_DEVICE AMREX_INLINE
amrex::ParticleReal KineticEnergy(
const amrex::ParticleReal ux, const amrex::ParticleReal uy, const amrex::ParticleReal uz,
const amrex::ParticleReal mass)
T KineticEnergy(
const amrex::ParticleReal ux, const amrex::ParticleReal uy, const amrex::ParticleReal uz, const amrex::ParticleReal mass)
{
using namespace amrex;

constexpr auto inv_c2 = 1.0_prt/(PhysConst::c * PhysConst::c);
constexpr auto one = static_cast<T>(1.0);
constexpr auto inv_c2 = one/Math::powi<2>(static_cast<T>(PhysConst::c));

// The expression used is derived by reducing the expression
// (gamma - 1)*(gamma + 1)/(gamma + 1)

const auto u2 = ux*ux + uy*uy + uz*uz;
const auto gamma = std::sqrt(1.0_prt + u2*inv_c2);
return 1.0_prt/(1.0_prt + gamma)*mass*u2;
const auto u2 = static_cast<T>(ux*ux + uy*uy + uz*uz);
const auto gamma = std::sqrt(one + u2*inv_c2);
return one/(one + gamma)*static_cast<T>(mass)*u2;
}

/**
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
#include "BackgroundMCCCollision.H"

#include "ImpactIonization.H"
#include "Particles/Algorithms/KineticEnergy.H"
#include "Particles/ParticleCreation/FilterCopyTransform.H"
#include "Particles/ParticleCreation/SmartCopy.H"
#include "Utils/Parser/ParserUtils.H"
@@ -435,8 +436,8 @@ void BackgroundMCCCollision::doBackgroundCollisionsWithinTile
// subtract any energy penalty of the collision from the
// projectile energy
if (scattering_process.m_energy_penalty > 0.0_prt) {
ParticleUtils::getEnergy(v_coll2, m, E_coll);
E_coll = (E_coll - scattering_process.m_energy_penalty) * PhysConst::q_e;
constexpr auto eV = PhysConst::q_e;
E_coll = (Algorithms::KineticEnergy<double>(vx, vy, vz, m) - scattering_process.m_energy_penalty*eV);
const auto scale_fac = static_cast<amrex::ParticleReal>(
std::sqrt(E_coll * (E_coll + 2.0_prt*mc2) / c2) / m / v_coll);
vx *= scale_fac;
12 changes: 7 additions & 5 deletions Source/Particles/Collision/BackgroundMCC/ImpactIonization.H
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@

#include "Particles/Collision/ScatteringProcess.H"

#include "Particles/Algorithms/KineticEnergy.H"
#include "Utils/ParticleUtils.H"
#include "Utils/WarpXConst.H"

@@ -94,13 +95,14 @@ public:
const ParticleReal uz = ptd.m_rdata[PIdx::uz][i];

// calculate kinetic energy
const ParticleReal u_coll2 = ux*ux + uy*uy + uz*uz;
ParticleUtils::getEnergy(u_coll2, m_mass, E_coll);
constexpr auto eV = PhysConst::q_e;
E_coll = Algorithms::KineticEnergy<double>(ux, uy, uz, m_mass)/eV;

// get collision cross-section
const ParticleReal sigma_E = m_mcc_process.getCrossSection(static_cast<amrex::ParticleReal>(E_coll));

// calculate normalized collision frequency
const ParticleReal u_coll2 = ux*ux + uy*uy + uz*uz;
const ParticleReal nu_i = n_a * sigma_E * sqrt(u_coll2) / m_nu_max;

// check if this collision should be performed
@@ -198,11 +200,11 @@ public:
auto& i_uz = dst2.m_rdata[PIdx::uz][i_dst2];

// calculate kinetic energy
const ParticleReal u_coll2 = ux*ux + uy*uy + uz*uz;
ParticleUtils::getEnergy(u_coll2, m_mass1, E_coll);
constexpr auto eV = PhysConst::q_e;
E_coll = Algorithms::KineticEnergy<double>(ux, uy, uz, m_mass1)/eV;

// each electron gets half the energy (could change this later)
const auto E_out = static_cast<amrex::ParticleReal>((E_coll - m_energy_cost) / 2.0_prt * PhysConst::q_e);
const auto E_out = static_cast<amrex::ParticleReal>((E_coll - m_energy_cost) / 2.0_prt * eV);

// precalculate often used value
constexpr auto c2 = PhysConst::c * PhysConst::c;
20 changes: 0 additions & 20 deletions Source/Utils/ParticleUtils.H
Original file line number Diff line number Diff line change
@@ -33,26 +33,6 @@ namespace ParticleUtils {
amrex::MFIter const & mfi,
WarpXParticleContainer::ParticleTileType & ptile);

/**
* \brief Return (relativistic) particle energy given velocity and mass.
* Note the use of `double` since this calculation is prone to error with
* single precision.
*
* @param[in] u2 square of particle speed (i.e. u dot u where u = gamma*v)
* @param[in] mass particle mass
* @param[out] energy particle energy in eV
*/
AMREX_GPU_HOST_DEVICE AMREX_INLINE
void getEnergy ( amrex::ParticleReal const u2, double const mass,
double& energy )
{
using std::sqrt;
using namespace amrex::literals;

constexpr auto c2 = PhysConst::c * PhysConst::c;
energy = mass * u2 / (sqrt(1.0_rt + u2 / c2) + 1.0_rt) / PhysConst::q_e;
}

/**
* \brief Return (relativistic) collision energy assuming the target (with
* mass M) is stationary and the projectile is approaching with the