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_GpuQualifiers.H>
18 #include <AMReX_REAL.H>
19 #include <AMReX_Random.H>
20 
21 #include <picsar_qed/containers/picsar_array.hpp>
22 #include <picsar_qed/math/cmath_overloads.hpp>
23 #include <picsar_qed/math/math_constants.h>
24 #include <picsar_qed/math/vec_functions.hpp>
25 #include <picsar_qed/physics/breit_wheeler/breit_wheeler_engine_core.hpp>
26 #include <picsar_qed/physics/breit_wheeler/breit_wheeler_engine_tables.hpp>
27 #include <picsar_qed/physics/gamma_functions.hpp>
28 #include <picsar_qed/physics/phys_constants.h>
29 #include <picsar_qed/physics/unit_conversion.hpp>
30 
31 #include <cmath>
32 #include <vector>
33 
34 namespace amrex { struct RandomEngine; }
35 
36 // Aliases =============================
38  picsar::multi_physics::phys::breit_wheeler::
39  dndt_lookup_table_params<amrex::ParticleReal>;
40 
42  picsar::multi_physics::phys::breit_wheeler::
43  dndt_lookup_table<
44  amrex::ParticleReal,
46 
47 using BW_dndt_table_view = BW_dndt_table::view_type;
48 
50  picsar::multi_physics::phys::breit_wheeler::
51  pair_prod_lookup_table_params<amrex::ParticleReal>;
52 
54  picsar::multi_physics::phys::breit_wheeler::
55  pair_prod_lookup_table<
56  amrex::ParticleReal,
58 
59 using BW_pair_prod_table_view = BW_pair_prod_table::view_type;
60 
62 {
65 };
66 
67 // Functors ==================================
68 
69 // These functors allow using the core elementary functions of the library.
70 // They are generated by a factory class (BreitWheelerEngine, see below).
71 // They can be included in GPU kernels.
72 
78 {
79 public:
85 
92  amrex::ParticleReal operator() (amrex::RandomEngine const& engine) const noexcept
93  {
94  namespace pxr_bw = picsar::multi_physics::phys::breit_wheeler;
95 
96  //A random number in [0,1) should be provided as an argument.
97  return pxr_bw::get_optical_depth(amrex::Random(engine));
98  }
99 };
100 //____________________________________________
101 
107 {
108 public:
109 
114 
115 
123  const BW_dndt_table_view table_view,
124  const amrex::ParticleReal bw_minimum_chi_phot):
125  m_table_view{table_view}, m_bw_minimum_chi_phot{bw_minimum_chi_phot}{}
126 
144  const amrex::ParticleReal ux, const amrex::ParticleReal uy,
145  const amrex::ParticleReal uz, const amrex::ParticleReal ex,
146  const amrex::ParticleReal ey, const amrex::ParticleReal ez,
147  const amrex::ParticleReal bx, const amrex::ParticleReal by,
148  const amrex::ParticleReal bz, const amrex::Real dt,
149  amrex::ParticleReal& opt_depth) const noexcept
150  {
151  namespace pxr_m = picsar::multi_physics::math;
152  namespace pxr_p = picsar::multi_physics::phys;
153  namespace pxr_bw = picsar::multi_physics::phys::breit_wheeler;
154 
155  constexpr amrex::ParticleReal m_e = PhysConst::m_e;
156  const auto u_norm = std::sqrt(ux*ux + uy*uy + uz*uz);
157  const auto energy = u_norm*m_e*PhysConst::c;
158 
159  const auto px = m_e*ux;
160  const auto py = m_e*uy;
161  const auto pz = m_e*uz;
162 
163  const auto chi_phot = QedUtils::chi_photon(
164  px, py, pz, ex, ey, ez, bx, by, bz);
165 
166  //Optical depth is not evolved for photons having less energy than what is
167  //required to generate a pair or a quantum parameter smaller than
168  //m_bw_minimum_chi_phot
169  const auto gamma_photon = pxr_p::compute_gamma_photon<
170  amrex::ParticleReal, pxr_p::unit_system::SI>(
171  px, py, pz);
172  if (gamma_photon < pxr_m::two<amrex::ParticleReal> ||
173  chi_phot < m_bw_minimum_chi_phot) {
174  return 0;
175  }
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 
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  [[nodiscard]] BreitWheelerGetOpticalDepth build_optical_depth_functor () const;
305 
309  [[nodiscard]] BreitWheelerEvolveOpticalDepth build_evolve_functor () const;
310 
314  [[nodiscard]] BreitWheelerGeneratePairs build_pair_functor () const;
315 
319  [[nodiscard]] bool are_lookup_tables_initialized () const;
320 
327  [[nodiscard]] 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  amrex::ParticleReal bw_minimum_chi_phot);
339 
345  void init_builtin_tables(amrex::ParticleReal bw_minimum_chi_phot);
346 
353  void compute_lookup_tables (PicsarBreitWheelerCtrl ctrl,
354  amrex::ParticleReal bw_minimum_chi_phot);
355 
361  [[nodiscard]] PicsarBreitWheelerCtrl get_default_ctrl() const;
362 
363  [[nodiscard]] 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_
#define AMREX_FORCE_INLINE
#define AMREX_GPU_DEVICE
#define AMREX_GPU_HOST_DEVICE
picsar::multi_physics::phys::breit_wheeler::dndt_lookup_table< amrex::ParticleReal, PicsarQedVector< amrex::ParticleReal > > BW_dndt_table
Definition: BreitWheelerEngineWrapper.H:45
picsar::multi_physics::phys::breit_wheeler::pair_prod_lookup_table< amrex::ParticleReal, PicsarQedVector< amrex::ParticleReal > > BW_pair_prod_table
Definition: BreitWheelerEngineWrapper.H:57
picsar::multi_physics::phys::breit_wheeler::dndt_lookup_table_params< amrex::ParticleReal > BW_dndt_table_params
Definition: BreitWheelerEngineWrapper.H:39
BW_dndt_table::view_type BW_dndt_table_view
Definition: BreitWheelerEngineWrapper.H:47
picsar::multi_physics::phys::breit_wheeler::pair_prod_lookup_table_params< amrex::ParticleReal > BW_pair_prod_table_params
Definition: BreitWheelerEngineWrapper.H:51
BW_pair_prod_table::view_type BW_pair_prod_table_view
Definition: BreitWheelerEngineWrapper.H:59
std::vector< Real > PicsarQedVector
Definition: QedWrapperCommons.H:105
Definition: BreitWheelerEngineWrapper.H:294
BreitWheelerEngine()=default
BW_pair_prod_table m_pair_prod_table
Definition: BreitWheelerEngineWrapper.H:373
amrex::ParticleReal m_bw_minimum_chi_phot
Definition: BreitWheelerEngineWrapper.H:370
BW_dndt_table m_dndt_table
Definition: BreitWheelerEngineWrapper.H:372
Definition: BreitWheelerEngineWrapper.H:107
BW_dndt_table_view m_table_view
Definition: BreitWheelerEngineWrapper.H:187
BreitWheelerEvolveOpticalDepth(const BW_dndt_table_view table_view, const amrex::ParticleReal bw_minimum_chi_phot)
Definition: BreitWheelerEngineWrapper.H:122
amrex::ParticleReal m_bw_minimum_chi_phot
Definition: BreitWheelerEngineWrapper.H:188
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:143
Definition: BreitWheelerEngineWrapper.H:196
BreitWheelerGeneratePairs()=default
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
BW_pair_prod_table_view m_table_view
Definition: BreitWheelerEngineWrapper.H:285
BreitWheelerGeneratePairs(const BW_pair_prod_table_view table_view)
Definition: BreitWheelerEngineWrapper.H:212
Definition: BreitWheelerEngineWrapper.H:78
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal operator()(amrex::RandomEngine const &engine) const noexcept
Definition: BreitWheelerEngineWrapper.H:92
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
static constexpr auto c
vacuum speed of light [m/s]
Definition: constant.H:44
static constexpr auto m_e
electron mass [kg]
Definition: constant.H:52
Real Random()
float dt
Definition: stencil.py:442
me
Definition: yt3d_mpi.py:142
Definition: BreitWheelerEngineWrapper.H:62
BW_pair_prod_table_params pair_prod_params
Definition: BreitWheelerEngineWrapper.H:64
BW_dndt_table_params dndt_params
Definition: BreitWheelerEngineWrapper.H:63