WarpX
InjectorFlux.H
Go to the documentation of this file.
1 /* Copyright 2023 Remi Lehe
2  *
3  * This file is part of WarpX.
4  *
5  * License: BSD-3-Clause-LBNL
6  */
7 #ifndef WARPX_INJECTOR_FLUX_H_
8 #define WARPX_INJECTOR_FLUX_H_
9 
10 #include "Utils/WarpXConst.H"
11 
12 #include <AMReX.H>
13 #include <AMReX_Array.H>
14 #include <AMReX_GpuQualifiers.H>
15 #include <AMReX_Math.H>
16 #include <AMReX_Parser.H>
17 #include <AMReX_REAL.H>
18 
19 #include <cmath>
20 #include <string>
21 
22 // struct whose getFlux returns constant flux.
24 {
25  InjectorFluxConstant (amrex::Real a_flux) noexcept : m_flux(a_flux) {}
26 
27  [[nodiscard]]
29  amrex::Real
30  getFlux (amrex::Real, amrex::Real, amrex::Real, amrex::Real) const noexcept
31  {
32  return m_flux;
33  }
34 
35 private:
36  amrex::Real m_flux;
37 };
38 
39 // struct whose getFlux returns local flux computed from parser.
41 {
42  InjectorFluxParser (amrex::ParserExecutor<4> const& a_parser) noexcept
43  : m_parser(a_parser) {}
44 
45  [[nodiscard]]
47  amrex::Real
48  getFlux (amrex::Real x, amrex::Real y, amrex::Real z, amrex::Real t) const noexcept
49  {
50  return m_parser(x,y,z,t);
51  }
52 
54 };
55 
56 // Base struct for flux injector.
57 // InjectorFlux contains a union (called Object) that holds any one
58 // instance of:
59 // - InjectorFluxConstant : to generate constant flux;
60 // - InjectorFluxParser : to generate flux from parser;
61 // The choice is made at runtime, depending in the constructor called.
62 // This mimics virtual functions.
64 {
65  // This constructor stores a InjectorFluxConstant in union object.
66  InjectorFlux (InjectorFluxConstant* t, amrex::Real a_flux)
67  : type(Type::constant),
68  object(t,a_flux)
69  { }
70 
71  // This constructor stores a InjectorFluxParser in union object.
73  : type(Type::parser),
74  object(t,a_parser)
75  { }
76 
77  // Explicitly prevent the compiler from generating copy constructors
78  // and copy assignment operators.
79  InjectorFlux (InjectorFlux const&) = delete;
80  InjectorFlux (InjectorFlux&&) = delete;
81  void operator= (InjectorFlux const&) = delete;
82  void operator= (InjectorFlux &&) = delete;
83 
84  // Default destructor
85  ~InjectorFlux () = default;
86 
87  void clear ()
88  {
89  switch (type)
90  {
91  case Type::constant:
92  case Type::parser:
93  {
94  break;
95  }
96  }
97  }
98 
99  // call getFlux from the object stored in the union
100  // (the union is called Object, and the instance is called object).
101  [[nodiscard]]
103  amrex::Real
104  getFlux (amrex::Real x, amrex::Real y, amrex::Real z, amrex::Real t) const noexcept
105  {
106  switch (type)
107  {
108  case Type::parser:
109  {
110  return object.parser.getFlux(x,y,z,t);
111  }
112  case Type::constant:
113  {
114  return object.constant.getFlux(x,y,z,t);
115  }
116  default:
117  {
118  amrex::Abort("InjectorFlux: unknown type");
119  return 0.0;
120  }
121  }
122  }
123 
124 private:
125  enum struct Type { constant, parser };
127 
128  // An instance of union Object constructs and stores any one of
129  // the objects declared (constant or parser).
130  union Object {
131  Object (InjectorFluxConstant*, amrex::Real a_flux) noexcept
132  : constant(a_flux) {}
134  : parser(a_parser) {}
137  };
139 };
140 
141 // In order for InjectorFlux to be trivially copyable, its destructor
142 // must be trivial. So we have to rely on a custom deleter for unique_ptr.
144  void operator () (InjectorFlux* p) const {
145  if (p) {
146  p->clear();
147  delete p;
148  }
149  }
150 };
151 
152 #endif //WARPX_INJECTOR_FLUX_H_
#define AMREX_GPU_HOST_DEVICE
void Abort(const std::string &msg)
Definition: InjectorFlux.H:24
amrex::Real m_flux
Definition: InjectorFlux.H:36
AMREX_GPU_HOST_DEVICE amrex::Real getFlux(amrex::Real, amrex::Real, amrex::Real, amrex::Real) const noexcept
Definition: InjectorFlux.H:30
InjectorFluxConstant(amrex::Real a_flux) noexcept
Definition: InjectorFlux.H:25
Definition: InjectorFlux.H:143
void operator()(InjectorFlux *p) const
Definition: InjectorFlux.H:144
Definition: InjectorFlux.H:64
~InjectorFlux()=default
void clear()
Definition: InjectorFlux.H:87
void operator=(InjectorFlux const &)=delete
InjectorFlux(InjectorFluxConstant *t, amrex::Real a_flux)
Definition: InjectorFlux.H:66
InjectorFlux(InjectorFlux const &)=delete
Object object
Definition: InjectorFlux.H:138
InjectorFlux(InjectorFluxParser *t, amrex::ParserExecutor< 4 > const &a_parser)
Definition: InjectorFlux.H:72
Type
Definition: InjectorFlux.H:125
InjectorFlux(InjectorFlux &&)=delete
Type type
Definition: InjectorFlux.H:126
AMREX_GPU_HOST_DEVICE amrex::Real getFlux(amrex::Real x, amrex::Real y, amrex::Real z, amrex::Real t) const noexcept
Definition: InjectorFlux.H:104
Definition: InjectorFlux.H:41
amrex::ParserExecutor< 4 > m_parser
Definition: InjectorFlux.H:53
InjectorFluxParser(amrex::ParserExecutor< 4 > const &a_parser) noexcept
Definition: InjectorFlux.H:42
AMREX_GPU_HOST_DEVICE amrex::Real getFlux(amrex::Real x, amrex::Real y, amrex::Real z, amrex::Real t) const noexcept
Definition: InjectorFlux.H:48
Definition: InjectorFlux.H:130
Object(InjectorFluxParser *, amrex::ParserExecutor< 4 > const &a_parser) noexcept
Definition: InjectorFlux.H:133
Object(InjectorFluxConstant *, amrex::Real a_flux) noexcept
Definition: InjectorFlux.H:131
InjectorFluxParser parser
Definition: InjectorFlux.H:136
InjectorFluxConstant constant
Definition: InjectorFlux.H:135