WarpX
BreitWheelerEngineWrapper.H
Go to the documentation of this file.
1 /* Copyright 2019 Luca Fedeli
2  *
3  * This file is part of WarpX.
4  *
5  * License: BSD-3-Clause-LBNL
6  */
7 #ifndef WARPX_breit_wheeler_engine_wrapper_h_
8 #define WARPX_breit_wheeler_engine_wrapper_h_
9 
11 
12 #include "QedChiFunctions.H"
13 #include "QedWrapperCommons.H"
14 #include "Utils/WarpXConst.H"
15 
16 #include <AMReX_Extension.H>
17 #include <AMReX_GpuContainers.H>
18 #include <AMReX_GpuQualifiers.H>
19 #include <AMReX_REAL.H>
20 #include <AMReX_Random.H>
21 
22 #include <picsar_qed/containers/picsar_array.hpp>
23 #include <picsar_qed/math/cmath_overloads.hpp>
24 #include <picsar_qed/math/math_constants.h>
25 #include <picsar_qed/math/vec_functions.hpp>
26 #include <picsar_qed/physics/breit_wheeler/breit_wheeler_engine_core.hpp>
27 #include <picsar_qed/physics/breit_wheeler/breit_wheeler_engine_tables.hpp>
28 #include <picsar_qed/physics/gamma_functions.hpp>
29 #include <picsar_qed/physics/phys_constants.h>
30 #include <picsar_qed/physics/unit_conversion.hpp>
31 
32 #include <cmath>
33 #include <vector>
34 
35 namespace amrex { struct RandomEngine; }
36 
37 // Aliases =============================
39  picsar::multi_physics::phys::breit_wheeler::
40  dndt_lookup_table_params<amrex::ParticleReal>;
41 
42 using BW_dndt_table =
43  picsar::multi_physics::phys::breit_wheeler::
44  dndt_lookup_table<
45  amrex::ParticleReal,
47 
48 using BW_dndt_table_view = BW_dndt_table::view_type;
49 
51  picsar::multi_physics::phys::breit_wheeler::
52  pair_prod_lookup_table_params<amrex::ParticleReal>;
53 
54 using BW_pair_prod_table =
55  picsar::multi_physics::phys::breit_wheeler::
56  pair_prod_lookup_table<
57  amrex::ParticleReal,
59 
60 using BW_pair_prod_table_view = BW_pair_prod_table::view_type;
61 
63 {
66 };
67 
68 // Functors ==================================
69 
70 // These functors allow using the core elementary functions of the library.
71 // They are generated by a factory class (BreitWheelerEngine, see below).
72 // They can be included in GPU kernels.
73 
79 {
80 public:
85  BreitWheelerGetOpticalDepth () = default;
86 
93  amrex::ParticleReal operator() (amrex::RandomEngine const& engine) const noexcept
94  {
95  namespace pxr_bw = picsar::multi_physics::phys::breit_wheeler;
96 
97  //A random number in [0,1) should be provided as an argument.
98  return pxr_bw::get_optical_depth(amrex::Random(engine));
99  }
100 };
101 //____________________________________________
102 
108 {
109 public:
110 
114  BreitWheelerEvolveOpticalDepth () = default;
115 
116 
124  const BW_dndt_table_view table_view,
125  const amrex::ParticleReal bw_minimum_chi_phot):
126  m_table_view{table_view}, m_bw_minimum_chi_phot{bw_minimum_chi_phot}{}
127 
145  const amrex::ParticleReal ux, const amrex::ParticleReal uy,
146  const amrex::ParticleReal uz, const amrex::ParticleReal ex,
147  const amrex::ParticleReal ey, const amrex::ParticleReal ez,
148  const amrex::ParticleReal bx, const amrex::ParticleReal by,
149  const amrex::ParticleReal bz, const amrex::Real dt,
150  amrex::ParticleReal& opt_depth) const noexcept
151  {
152  namespace pxr_m = picsar::multi_physics::math;
153  namespace pxr_p = picsar::multi_physics::phys;
154  namespace pxr_bw = picsar::multi_physics::phys::breit_wheeler;
155 
156  constexpr amrex::ParticleReal m_e = PhysConst::m_e;
157  const auto u_norm = std::sqrt(ux*ux + uy*uy + uz*uz);
158  const auto energy = u_norm*m_e*PhysConst::c;
159 
160  const auto px = m_e*ux;
161  const auto py = m_e*uy;
162  const auto pz = m_e*uz;
163 
164  const auto chi_phot = QedUtils::chi_photon(
165  px, py, pz, ex, ey, ez, bx, by, bz);
166 
167  //Optical depth is not evolved for photons having less energy than what is
168  //required to generate a pair or a quantum parameter smaller than
169  //m_bw_minimum_chi_phot
170  const auto gamma_photon = pxr_p::compute_gamma_photon<
171  amrex::ParticleReal, pxr_p::unit_system::SI>(
172  px, py, pz);
173  if (gamma_photon < pxr_m::two<amrex::ParticleReal> ||
174  chi_phot < m_bw_minimum_chi_phot)
175  return 0;
176 
177  const auto is_out = pxr_bw::evolve_optical_depth<
178  amrex::ParticleReal,
180  pxr_p::unit_system::SI>(
181  energy, chi_phot, dt, opt_depth, m_table_view);
182 
183  return is_out;
184  }
185 
186 private:
188  amrex::ParticleReal m_bw_minimum_chi_phot;
189 };
190 
196 {
197 public:
198 
202  BreitWheelerGeneratePairs () = default;
203 
213  m_table_view{table_view}{}
214 
232  const amrex::ParticleReal ux, const amrex::ParticleReal uy,
233  const amrex::ParticleReal uz, const amrex::ParticleReal ex,
234  const amrex::ParticleReal ey, const amrex::ParticleReal ez,
235  const amrex::ParticleReal bx, const amrex::ParticleReal by,
236  const amrex::ParticleReal bz, amrex::ParticleReal& e_ux,
237  amrex::ParticleReal& e_uy, amrex::ParticleReal& e_uz,
238  amrex::ParticleReal& p_ux, amrex::ParticleReal& p_uy,
239  amrex::ParticleReal& p_uz,
240  amrex::RandomEngine const& engine) const noexcept
241  {
242  using namespace amrex;
243  namespace pxr_m = picsar::multi_physics::math;
244  namespace pxr_p = picsar::multi_physics::phys;
245  namespace pxr_bw = picsar::multi_physics::phys::breit_wheeler;
246 
247  const auto rand_zero_one_minus_epsi = amrex::Random(engine);
248 
249  constexpr ParticleReal me = PhysConst::m_e;
250  constexpr ParticleReal one_over_me = 1._prt/me;
251 
252  // Particle momentum is stored as gamma * velocity.
253  // Convert to m * gamma * velocity
254  auto px = ux*me;
255  auto py = uy*me;
256  auto pz = uz*me;
257 
258  const auto chi_photon = QedUtils::chi_photon(
259  px, py, pz, ex, ey, ez, bx, by, bz);
260 
261  const auto momentum_photon = pxr_m::vec3<amrex::ParticleReal>{px, py, pz};
262  auto momentum_ele = pxr_m::vec3<amrex::ParticleReal>();
263  auto momentum_pos = pxr_m::vec3<amrex::ParticleReal>();
264 
265  const auto is_out = pxr_bw::generate_breit_wheeler_pairs<
266  amrex::ParticleReal,
268  pxr_p::unit_system::SI>(
269  chi_photon, momentum_photon,
270  rand_zero_one_minus_epsi,
271  m_table_view,
272  momentum_ele, momentum_pos);
273 
274  e_ux = momentum_ele[0]*one_over_me;
275  e_uy = momentum_ele[1]*one_over_me;
276  e_uz = momentum_ele[2]*one_over_me;
277  p_ux = momentum_pos[0]*one_over_me;
278  p_uy = momentum_pos[1]*one_over_me;
279  p_uz = momentum_pos[2]*one_over_me;
280 
281  return is_out;
282  }
283 
284 private:
286 };
287 
288 // Factory class =============================
289 
294 {
295 public:
299  BreitWheelerEngine () = default;
300 
304  BreitWheelerGetOpticalDepth build_optical_depth_functor () const;
305 
309  BreitWheelerEvolveOpticalDepth build_evolve_functor () const;
310 
314  BreitWheelerGeneratePairs build_pair_functor () const;
315 
319  bool are_lookup_tables_initialized () const;
320 
327  std::vector<char> export_lookup_tables_data () const;
328 
336  bool init_lookup_tables_from_raw_data (
337  const std::vector<char>& raw_data,
338  const amrex::ParticleReal bw_minimum_chi_phot);
339 
345  void init_builtin_tables(const amrex::ParticleReal bw_minimum_chi_phot);
346 
353  void compute_lookup_tables (const PicsarBreitWheelerCtrl ctrl,
354  const amrex::ParticleReal bw_minimum_chi_phot);
355 
361  PicsarBreitWheelerCtrl get_default_ctrl() const;
362 
363  amrex::ParticleReal get_minimum_chi_phot() const;
364 
365 private:
366  bool m_lookup_tables_initialized = false;
367 
368  //Variables to store the minimum chi parameters to enable
369  //Quantum Synchrotron process
370  amrex::ParticleReal m_bw_minimum_chi_phot;
371 
374 
375  void init_builtin_dndt_table();
376  void init_builtin_pair_prod_table();
377 
378 
379 };
380 
381 //============================================
382 
383 #endif //WARPX_breit_wheeler_engine_wrapper_H_
BW_pair_prod_table m_pair_prod_table
Definition: BreitWheelerEngineWrapper.H:373
BreitWheelerEvolveOpticalDepth(const BW_dndt_table_view table_view, const amrex::ParticleReal bw_minimum_chi_phot)
Definition: BreitWheelerEngineWrapper.H:123
AMREX_GPU_DEVICE AMREX_FORCE_INLINE int operator()(const amrex::ParticleReal ux, const amrex::ParticleReal uy, const amrex::ParticleReal uz, const amrex::ParticleReal ex, const amrex::ParticleReal ey, const amrex::ParticleReal ez, const amrex::ParticleReal bx, const amrex::ParticleReal by, const amrex::ParticleReal bz, amrex::ParticleReal &e_ux, amrex::ParticleReal &e_uy, amrex::ParticleReal &e_uz, amrex::ParticleReal &p_ux, amrex::ParticleReal &p_uy, amrex::ParticleReal &p_uz, amrex::RandomEngine const &engine) const noexcept
Definition: BreitWheelerEngineWrapper.H:231
AMREX_GPU_DEVICE AMREX_FORCE_INLINE int operator()(const amrex::ParticleReal ux, const amrex::ParticleReal uy, const amrex::ParticleReal uz, const amrex::ParticleReal ex, const amrex::ParticleReal ey, const amrex::ParticleReal ez, const amrex::ParticleReal bx, const amrex::ParticleReal by, const amrex::ParticleReal bz, const amrex::Real dt, amrex::ParticleReal &opt_depth) const noexcept
Definition: BreitWheelerEngineWrapper.H:144
picsar::multi_physics::phys::breit_wheeler::dndt_lookup_table< amrex::ParticleReal, amrex::Gpu::DeviceVector< amrex::ParticleReal > > BW_dndt_table
Definition: BreitWheelerEngineWrapper.H:46
BW_pair_prod_table_view m_table_view
Definition: BreitWheelerEngineWrapper.H:285
picsar::multi_physics::phys::breit_wheeler::dndt_lookup_table_params< amrex::ParticleReal > BW_dndt_table_params
Definition: BreitWheelerEngineWrapper.H:40
BW_dndt_table_params dndt_params
Definition: BreitWheelerEngineWrapper.H:64
picsar::multi_physics::phys::breit_wheeler::pair_prod_lookup_table< amrex::ParticleReal, amrex::Gpu::DeviceVector< amrex::ParticleReal > > BW_pair_prod_table
Definition: BreitWheelerEngineWrapper.H:58
static constexpr auto c
vacuum speed of light [m/s]
Definition: constant.H:44
me
Definition: yt3d_mpi.py:142
Real Random()
int dt
Definition: Stencil.py:468
Definition: BreitWheelerEngineWrapper.H:62
BW_dndt_table::view_type BW_dndt_table_view
Definition: BreitWheelerEngineWrapper.H:48
Definition: BreitWheelerEngineWrapper.H:107
BW_dndt_table m_dndt_table
Definition: BreitWheelerEngineWrapper.H:372
#define AMREX_FORCE_INLINE
int ey
Definition: Stencil.py:480
picsar::multi_physics::phys::breit_wheeler::pair_prod_lookup_table_params< amrex::ParticleReal > BW_pair_prod_table_params
Definition: BreitWheelerEngineWrapper.H:52
#define AMREX_GPU_DEVICE
Definition: BreitWheelerEngineWrapper.H:78
#define AMREX_GPU_HOST_DEVICE
amrex::ParticleReal m_bw_minimum_chi_phot
Definition: BreitWheelerEngineWrapper.H:188
int ez
Definition: Stencil.py:481
BW_dndt_table_view m_table_view
Definition: BreitWheelerEngineWrapper.H:187
BW_pair_prod_table_params pair_prod_params
Definition: BreitWheelerEngineWrapper.H:65
AMREX_GPU_DEVICE AMREX_FORCE_INLINE amrex::Real chi_photon(const amrex::ParticleReal px, const amrex::ParticleReal py, const amrex::ParticleReal pz, const amrex::ParticleReal ex, const amrex::ParticleReal ey, const amrex::ParticleReal ez, const amrex::ParticleReal bx, const amrex::ParticleReal by, const amrex::ParticleReal bz)
Definition: QedChiFunctions.H:31
Definition: BreitWheelerEngineWrapper.H:195
int ex
Definition: Stencil.py:479
Definition: BreitWheelerEngineWrapper.H:293
static constexpr auto m_e
electron mass [kg]
Definition: constant.H:52
amrex::ParticleReal m_bw_minimum_chi_phot
Definition: BreitWheelerEngineWrapper.H:370
BreitWheelerGeneratePairs(const BW_pair_prod_table_view table_view)
Definition: BreitWheelerEngineWrapper.H:212
BW_pair_prod_table::view_type BW_pair_prod_table_view
Definition: BreitWheelerEngineWrapper.H:60