aboutsummaryrefslogtreecommitdiff
path: root/Source/Particles/Algorithms/KineticEnergy.H
blob: c14ff14fd0fe297f5f9adc0275b97b6729fef81f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* Copyright 2021 Luca Fedeli
 *
 * This file is part of WarpX.
 *
 * License: BSD-3-Clause-LBNL
 */

#ifndef PARTICLES_KINETIC_ENERGY_H_
#define PARTICLES_KINETIC_ENERGY_H_

#include "Utils/WarpXConst.H"

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

#include <cmath>

namespace Algorithms{

    // This marks the gamma threshold to switch between the full relativistic expression
    // for particle kinetic energy and a Taylor expansion.
    static constexpr auto gamma_relativistic_threshold =
        static_cast<amrex::ParticleReal>(1.005);

    /**
     * \brief Computes the kinetic energy of a particle. Below a threshold for the
     * Lorentz factor (gamma_relativistic_threshold) it uses a Taylor expansion instead of
     * the full relativistic expression. This method should not be used with photons.
     *
     * @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)
     */
    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)
    {
        using namespace amrex;

        constexpr auto c2 = PhysConst::c * PhysConst::c;
        constexpr auto inv_c2 = 1.0_prt/c2;

        const auto u2 = (ux*ux + uy*uy + uz*uz)*inv_c2;
        const auto gamma = std::sqrt(1.0_prt + u2);

        const auto kk = (gamma > gamma_relativistic_threshold)?
            (gamma-1.0_prt):
            (u2*0.5_prt - u2*u2*(1.0_prt/8.0_prt) + u2*u2*u2*(1.0_prt/16.0_prt)-
            u2*u2*u2*u2*(5.0_prt/128.0_prt) + (7.0_prt/256_prt)*u2*u2*u2*u2*u2); //Taylor expansion

        return kk*mass*c2;
    }

    /**
     * \brief Computes the kinetic energy of a photon.
     *
     * @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)
     *
     * @return the kinetic energy of the photon (in S.I. units)
     */
    AMREX_GPU_HOST_DEVICE AMREX_INLINE
    amrex::ParticleReal KineticEnergyPhotons(
        const amrex::ParticleReal ux, const amrex::ParticleReal uy, const amrex::ParticleReal uz)
    {
        // Photons have zero mass, but ux, uy and uz are calculated assuming a mass equal to the
        // electron mass. Hence, photons need a special treatment to calculate the total energy.
        constexpr auto me_c = PhysConst::m_e * PhysConst::c;

        return me_c * std::sqrt(ux*ux + uy*uy + uz*uz);
    }

}

#endif // PARTICLES_ALGORITHMS_H_