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 INJECTOR_FLUX_H_
8 #define 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 
28  amrex::Real
29  getFlux (amrex::Real, amrex::Real, amrex::Real, amrex::Real) const noexcept
30  {
31  return m_flux;
32  }
33 
34 private:
35  amrex::Real m_flux;
36 };
37 
38 // struct whose getFlux returns local flux computed from parser.
40 {
41  InjectorFluxParser (amrex::ParserExecutor<4> const& a_parser) noexcept
42  : m_parser(a_parser) {}
43 
45  amrex::Real
46  getFlux (amrex::Real x, amrex::Real y, amrex::Real z, amrex::Real t) const noexcept
47  {
48  return m_parser(x,y,z,t);
49  }
50 
52 };
53 
54 // Base struct for flux injector.
55 // InjectorFlux contains a union (called Object) that holds any one
56 // instance of:
57 // - InjectorFluxConstant : to generate constant flux;
58 // - InjectorFluxParser : to generate flux from parser;
59 // The choice is made at runtime, depending in the constructor called.
60 // This mimics virtual functions.
62 {
63  // This constructor stores a InjectorFluxConstant in union object.
64  InjectorFlux (InjectorFluxConstant* t, amrex::Real a_flux)
65  : type(Type::constant),
66  object(t,a_flux)
67  { }
68 
69  // This constructor stores a InjectorFluxParser in union object.
71  : type(Type::parser),
72  object(t,a_parser)
73  { }
74 
75  // Explicitly prevent the compiler from generating copy constructors
76  // and copy assignment operators.
77  InjectorFlux (InjectorFlux const&) = delete;
78  InjectorFlux (InjectorFlux&&) = delete;
79  void operator= (InjectorFlux const&) = delete;
80  void operator= (InjectorFlux &&) = delete;
81 
82  // Default destructor
83  ~InjectorFlux () = default;
84 
85  void clear ()
86  {
87  switch (type)
88  {
89  case Type::constant:
90  case Type::parser:
91  {
92  break;
93  }
94  }
95  }
96 
97  // call getFlux from the object stored in the union
98  // (the union is called Object, and the instance is called object).
100  amrex::Real
101  getFlux (amrex::Real x, amrex::Real y, amrex::Real z, amrex::Real t) const noexcept
102  {
103  switch (type)
104  {
105  case Type::parser:
106  {
107  return object.parser.getFlux(x,y,z,t);
108  }
109  case Type::constant:
110  {
111  return object.constant.getFlux(x,y,z,t);
112  }
113  default:
114  {
115  amrex::Abort("InjectorFlux: unknown type");
116  return 0.0;
117  }
118  }
119  }
120 
121 private:
122  enum struct Type { constant, parser };
124 
125  // An instance of union Object constructs and stores any one of
126  // the objects declared (constant or parser).
127  union Object {
128  Object (InjectorFluxConstant*, amrex::Real a_flux) noexcept
129  : constant(a_flux) {}
131  : parser(a_parser) {}
134  };
136 };
137 
138 // In order for InjectorFlux to be trivially copyable, its destructor
139 // must be trivial. So we have to rely on a custom deleter for unique_ptr.
141  void operator () (InjectorFlux* p) const {
142  if (p) {
143  p->clear();
144  delete p;
145  }
146  }
147 };
148 
149 #endif
#define AMREX_GPU_HOST_DEVICE
void Abort(const std::string &msg)
Definition: InjectorFlux.H:24
amrex::Real m_flux
Definition: InjectorFlux.H:35
AMREX_GPU_HOST_DEVICE amrex::Real getFlux(amrex::Real, amrex::Real, amrex::Real, amrex::Real) const noexcept
Definition: InjectorFlux.H:29
InjectorFluxConstant(amrex::Real a_flux) noexcept
Definition: InjectorFlux.H:25
Definition: InjectorFlux.H:140
void operator()(InjectorFlux *p) const
Definition: InjectorFlux.H:141
Definition: InjectorFlux.H:62
~InjectorFlux()=default
void clear()
Definition: InjectorFlux.H:85
void operator=(InjectorFlux const &)=delete
InjectorFlux(InjectorFluxConstant *t, amrex::Real a_flux)
Definition: InjectorFlux.H:64
InjectorFlux(InjectorFlux const &)=delete
Object object
Definition: InjectorFlux.H:135
InjectorFlux(InjectorFluxParser *t, amrex::ParserExecutor< 4 > const &a_parser)
Definition: InjectorFlux.H:70
Type
Definition: InjectorFlux.H:122
InjectorFlux(InjectorFlux &&)=delete
Type type
Definition: InjectorFlux.H:123
AMREX_GPU_HOST_DEVICE amrex::Real getFlux(amrex::Real x, amrex::Real y, amrex::Real z, amrex::Real t) const noexcept
Definition: InjectorFlux.H:101
Definition: InjectorFlux.H:40
amrex::ParserExecutor< 4 > m_parser
Definition: InjectorFlux.H:51
InjectorFluxParser(amrex::ParserExecutor< 4 > const &a_parser) noexcept
Definition: InjectorFlux.H:41
AMREX_GPU_HOST_DEVICE amrex::Real getFlux(amrex::Real x, amrex::Real y, amrex::Real z, amrex::Real t) const noexcept
Definition: InjectorFlux.H:46
Definition: InjectorFlux.H:127
Object(InjectorFluxParser *, amrex::ParserExecutor< 4 > const &a_parser) noexcept
Definition: InjectorFlux.H:130
Object(InjectorFluxConstant *, amrex::Real a_flux) noexcept
Definition: InjectorFlux.H:128
InjectorFluxParser parser
Definition: InjectorFlux.H:133
InjectorFluxConstant constant
Definition: InjectorFlux.H:132