ARTEMIS
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  const auto is_out = pxr_bw::evolve_optical_depth<
177  amrex::ParticleReal,
179  pxr_p::unit_system::SI>(
180  energy, chi_phot, dt, opt_depth, m_table_view);
181 
182  return is_out;
183  }
184 
185 private:
187  amrex::ParticleReal m_bw_minimum_chi_phot;
188 };
189 
195 {
196 public:
197 
202 
212  m_table_view{table_view}{}
213 
231  const amrex::ParticleReal ux, const amrex::ParticleReal uy,
232  const amrex::ParticleReal uz, const amrex::ParticleReal ex,
233  const amrex::ParticleReal ey, const amrex::ParticleReal ez,
234  const amrex::ParticleReal bx, const amrex::ParticleReal by,
235  const amrex::ParticleReal bz, amrex::ParticleReal& e_ux,
236  amrex::ParticleReal& e_uy, amrex::ParticleReal& e_uz,
237  amrex::ParticleReal& p_ux, amrex::ParticleReal& p_uy,
238  amrex::ParticleReal& p_uz,
239  amrex::RandomEngine const& engine) const noexcept
240  {
241  using namespace amrex;
242  namespace pxr_m = picsar::multi_physics::math;
243  namespace pxr_p = picsar::multi_physics::phys;
244  namespace pxr_bw = picsar::multi_physics::phys::breit_wheeler;
245 
246  const auto rand_zero_one_minus_epsi = amrex::Random(engine);
247 
248  constexpr ParticleReal me = PhysConst::m_e;
249  constexpr ParticleReal one_over_me = 1._prt/me;
250 
251  // Particle momentum is stored as gamma * velocity.
252  // Convert to m * gamma * velocity
253  auto px = ux*me;
254  auto py = uy*me;
255  auto pz = uz*me;
256 
257  const auto chi_photon = QedUtils::chi_photon(
258  px, py, pz, ex, ey, ez, bx, by, bz);
259 
260  const auto momentum_photon = pxr_m::vec3<amrex::ParticleReal>{px, py, pz};
261  auto momentum_ele = pxr_m::vec3<amrex::ParticleReal>();
262  auto momentum_pos = pxr_m::vec3<amrex::ParticleReal>();
263 
264  const auto is_out = pxr_bw::generate_breit_wheeler_pairs<
265  amrex::ParticleReal,
267  pxr_p::unit_system::SI>(
268  chi_photon, momentum_photon,
269  rand_zero_one_minus_epsi,
270  m_table_view,
271  momentum_ele, momentum_pos);
272 
273  e_ux = momentum_ele[0]*one_over_me;
274  e_uy = momentum_ele[1]*one_over_me;
275  e_uz = momentum_ele[2]*one_over_me;
276  p_ux = momentum_pos[0]*one_over_me;
277  p_uy = momentum_pos[1]*one_over_me;
278  p_uz = momentum_pos[2]*one_over_me;
279 
280  return is_out;
281  }
282 
283 private:
285 };
286 
287 // Factory class =============================
288 
293 {
294 public:
298  BreitWheelerEngine () = default;
299 
303  BreitWheelerGetOpticalDepth build_optical_depth_functor () const;
304 
308  BreitWheelerEvolveOpticalDepth build_evolve_functor () const;
309 
313  BreitWheelerGeneratePairs build_pair_functor () const;
314 
318  bool are_lookup_tables_initialized () const;
319 
326  std::vector<char> export_lookup_tables_data () const;
327 
335  bool init_lookup_tables_from_raw_data (
336  const std::vector<char>& raw_data,
337  const amrex::ParticleReal bw_minimum_chi_phot);
338 
344  void init_builtin_tables(const amrex::ParticleReal bw_minimum_chi_phot);
345 
352  void compute_lookup_tables (const PicsarBreitWheelerCtrl ctrl,
353  const amrex::ParticleReal bw_minimum_chi_phot);
354 
360  PicsarBreitWheelerCtrl get_default_ctrl() const;
361 
362  amrex::ParticleReal get_minimum_chi_phot() const;
363 
364 private:
365  bool m_lookup_tables_initialized = false;
366 
367  //Variables to store the minimum chi parameters to enable
368  //Quantum Synchrotron process
369  amrex::ParticleReal m_bw_minimum_chi_phot;
370 
373 
374  void init_builtin_dndt_table();
375  void init_builtin_pair_prod_table();
376 
377 
378 };
379 
380 //============================================
381 
382 #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:293
BreitWheelerEngine()=default
BW_pair_prod_table m_pair_prod_table
Definition: BreitWheelerEngineWrapper.H:372
amrex::ParticleReal m_bw_minimum_chi_phot
Definition: BreitWheelerEngineWrapper.H:369
BW_dndt_table m_dndt_table
Definition: BreitWheelerEngineWrapper.H:371
Definition: BreitWheelerEngineWrapper.H:107
BW_dndt_table_view m_table_view
Definition: BreitWheelerEngineWrapper.H:186
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:187
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:195
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:230
BW_pair_prod_table_view m_table_view
Definition: BreitWheelerEngineWrapper.H:284
BreitWheelerGeneratePairs(const BW_pair_prod_table_view table_view)
Definition: BreitWheelerEngineWrapper.H:211
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()
int dt
Definition: stencil.py:440
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