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::Real>;
41 
42 using BW_dndt_table =
43  picsar::multi_physics::phys::breit_wheeler::
44  dndt_lookup_table<
45  amrex::Real,
46  amrex::Gpu::DeviceVector<amrex::Real>>;
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::Real>;
53 
54 using BW_pair_prod_table =
55  picsar::multi_physics::phys::breit_wheeler::
56  pair_prod_lookup_table<
57  amrex::Real,
58  amrex::Gpu::DeviceVector<amrex::Real>>;
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:
86  {}
87 
92  AMREX_GPU_HOST_DEVICE
93  AMREX_FORCE_INLINE
94  amrex::Real operator() (amrex::RandomEngine const& engine) const noexcept
95  {
96  namespace pxr_bw = picsar::multi_physics::phys::breit_wheeler;
97 
98  //A random number in [0,1) should be provided as an argument.
99  return pxr_bw::get_optical_depth(amrex::Random(engine));
100  }
101 };
102 //____________________________________________
103 
109 {
110 public:
111 
116 
117 
125  const BW_dndt_table_view table_view,
126  const amrex::Real bw_minimum_chi_phot):
127  m_table_view{table_view}, m_bw_minimum_chi_phot{bw_minimum_chi_phot}{}
128 
143  AMREX_GPU_DEVICE
144  AMREX_FORCE_INLINE
146  const amrex::ParticleReal ux, const amrex::ParticleReal uy,
147  const amrex::ParticleReal uz, const amrex::ParticleReal ex,
148  const amrex::ParticleReal ey, const amrex::ParticleReal ez,
149  const amrex::ParticleReal bx, const amrex::ParticleReal by,
150  const amrex::ParticleReal bz, const amrex::Real dt,
151  amrex::ParticleReal& opt_depth) const noexcept
152  {
153  namespace pxr_m = picsar::multi_physics::math;
154  namespace pxr_p = picsar::multi_physics::phys;
155  namespace pxr_bw = picsar::multi_physics::phys::breit_wheeler;
156 
157  constexpr amrex::Real m_e = PhysConst::m_e;
158  const auto u_norm = std::sqrt(ux*ux + uy*uy + uz*uz);
159  const auto energy = u_norm*m_e*PhysConst::c;
160 
161  const auto px = m_e*ux;
162  const auto py = m_e*uy;
163  const auto pz = m_e*uz;
164 
165  const auto chi_phot = QedUtils::chi_photon(
166  px, py, pz, ex, ey, ez, bx, by, bz);
167 
168  //Optical depth is not evolved for photons having less energy than what is
169  //required to generate a pair or a quantum parameter smaller than
170  //m_bw_minimum_chi_phot
171  const auto gamma_photon = pxr_p::compute_gamma_photon<
172  amrex::ParticleReal, pxr_p::unit_system::SI>(
173  px, py, pz);
174  if (gamma_photon < pxr_m::two<amrex::Real> ||
175  chi_phot < m_bw_minimum_chi_phot)
176  return 0;
177 
178  const auto is_out = pxr_bw::evolve_optical_depth<
179  amrex::Real,
181  pxr_p::unit_system::SI>(
182  energy, chi_phot, dt, opt_depth, m_table_view);
183 
184  return is_out;
185  }
186 
187 private:
190 };
191 
197 {
198 public:
199 
204 
214  m_table_view{table_view}{}
215 
230  AMREX_GPU_DEVICE
231  AMREX_FORCE_INLINE
233  const amrex::ParticleReal ux, const amrex::ParticleReal uy,
234  const amrex::ParticleReal uz, const amrex::ParticleReal ex,
235  const amrex::ParticleReal ey, const amrex::ParticleReal ez,
236  const amrex::ParticleReal bx, const amrex::ParticleReal by,
237  const amrex::ParticleReal bz, amrex::ParticleReal& e_ux,
238  amrex::ParticleReal& e_uy, amrex::ParticleReal& e_uz,
239  amrex::ParticleReal& p_ux, amrex::ParticleReal& p_uy,
240  amrex::ParticleReal& p_uz,
241  amrex::RandomEngine const& engine) const noexcept
242  {
243  using namespace amrex;
244  namespace pxr_m = picsar::multi_physics::math;
245  namespace pxr_p = picsar::multi_physics::phys;
246  namespace pxr_bw = picsar::multi_physics::phys::breit_wheeler;
247 
248  const auto rand_zero_one_minus_epsi = amrex::Random(engine);
249 
250  constexpr ParticleReal me = PhysConst::m_e;
251  constexpr ParticleReal one_over_me = 1._prt/me;
252 
253  // Particle momentum is stored as gamma * velocity.
254  // Convert to m * gamma * velocity
255  auto px = ux*me;
256  auto py = uy*me;
257  auto pz = uz*me;
258 
259  const auto chi_photon = QedUtils::chi_photon(
260  px, py, pz, ex, ey, ez, bx, by, bz);
261 
262  const auto momentum_photon = pxr_m::vec3<amrex::ParticleReal>{px, py, pz};
263  auto momentum_ele = pxr_m::vec3<amrex::ParticleReal>();
264  auto momentum_pos = pxr_m::vec3<amrex::ParticleReal>();
265 
266  const auto is_out = pxr_bw::generate_breit_wheeler_pairs<
267  amrex::ParticleReal,
269  pxr_p::unit_system::SI>(
270  chi_photon, momentum_photon,
271  rand_zero_one_minus_epsi,
272  m_table_view,
273  momentum_ele, momentum_pos);
274 
275  e_ux = momentum_ele[0]*one_over_me;
276  e_uy = momentum_ele[1]*one_over_me;
277  e_uz = momentum_ele[2]*one_over_me;
278  p_ux = momentum_pos[0]*one_over_me;
279  p_uy = momentum_pos[1]*one_over_me;
280  p_uz = momentum_pos[2]*one_over_me;
281 
282  return is_out;
283  }
284 
285 private:
287 };
288 
289 // Factory class =============================
290 
295 {
296 public:
301 
305  BreitWheelerGetOpticalDepth build_optical_depth_functor () const;
306 
310  BreitWheelerEvolveOpticalDepth build_evolve_functor () const;
311 
315  BreitWheelerGeneratePairs build_pair_functor () const;
316 
320  bool are_lookup_tables_initialized () const;
321 
328  std::vector<char> export_lookup_tables_data () const;
329 
337  bool init_lookup_tables_from_raw_data (
338  const std::vector<char>& raw_data,
339  const amrex::Real bw_minimum_chi_phot);
340 
346  void init_builtin_tables(const amrex::Real bw_minimum_chi_phot);
347 
354  void compute_lookup_tables (const PicsarBreitWheelerCtrl ctrl,
355  const amrex::Real bw_minimum_chi_phot);
356 
362  PicsarBreitWheelerCtrl get_default_ctrl() const;
363 
364  amrex::Real get_minimum_chi_phot() const;
365 
366 private:
367  bool m_lookup_tables_initialized = false;
368 
369  //Variables to store the minimum chi parameters to enable
370  //Quantum Synchrotron process
372 
375 
376  void init_builtin_dndt_table();
377  void init_builtin_pair_prod_table();
378 
379 
380 };
381 
382 //============================================
383 
384 #endif //WARPX_breit_wheeler_engine_wrapper_H_
BW_pair_prod_table m_pair_prod_table
Definition: BreitWheelerEngineWrapper.H:374
picsar::multi_physics::phys::breit_wheeler::dndt_lookup_table< amrex::Real, amrex::Gpu::DeviceVector< amrex::Real > > BW_dndt_table
Definition: BreitWheelerEngineWrapper.H:46
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:232
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:145
amrex::Real m_bw_minimum_chi_phot
Definition: BreitWheelerEngineWrapper.H:189
BW_pair_prod_table_view m_table_view
Definition: BreitWheelerEngineWrapper.H:286
BW_dndt_table_params dndt_params
Definition: BreitWheelerEngineWrapper.H:64
BreitWheelerEvolveOpticalDepth()
Definition: BreitWheelerEngineWrapper.H:115
me
Definition: yt3d_mpi.py:139
def uz
Definition: read_lab_particles.py:29
Definition: BreitWheelerEngineWrapper.H:62
BW_dndt_table::view_type BW_dndt_table_view
Definition: BreitWheelerEngineWrapper.H:48
BreitWheelerGetOpticalDepth()
Definition: BreitWheelerEngineWrapper.H:85
Definition: BreitWheelerEngineWrapper.H:108
BW_dndt_table m_dndt_table
Definition: BreitWheelerEngineWrapper.H:373
BreitWheelerEvolveOpticalDepth(const BW_dndt_table_view table_view, const amrex::Real bw_minimum_chi_phot)
Definition: BreitWheelerEngineWrapper.H:124
Definition: BreitWheelerEngineWrapper.H:78
BW_dndt_table_view m_table_view
Definition: BreitWheelerEngineWrapper.H:188
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
picsar::multi_physics::phys::breit_wheeler::pair_prod_lookup_table< amrex::Real, amrex::Gpu::DeviceVector< amrex::Real > > BW_pair_prod_table
Definition: BreitWheelerEngineWrapper.H:58
picsar::multi_physics::phys::breit_wheeler::dndt_lookup_table_params< amrex::Real > BW_dndt_table_params
Definition: BreitWheelerEngineWrapper.H:40
Definition: BreitWheelerEngineWrapper.H:196
def ux
Definition: read_lab_particles.py:28
amrex::Real m_bw_minimum_chi_phot
Definition: BreitWheelerEngineWrapper.H:371
Definition: BreitWheelerEngineWrapper.H:294
Definition: BreitWheelerEngineWrapper.H:35
BreitWheelerGeneratePairs(const BW_pair_prod_table_view table_view)
Definition: BreitWheelerEngineWrapper.H:213
picsar::multi_physics::phys::breit_wheeler::pair_prod_lookup_table_params< amrex::Real > BW_pair_prod_table_params
Definition: BreitWheelerEngineWrapper.H:52
BreitWheelerGeneratePairs()
Definition: BreitWheelerEngineWrapper.H:203
BW_pair_prod_table::view_type BW_pair_prod_table_view
Definition: BreitWheelerEngineWrapper.H:60