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  void clear ()
83  {
84  switch (type)
85  {
86  case Type::constant:
87  case Type::parser:
88  {
89  break;
90  }
91  }
92  }
93 
94  // call getFlux from the object stored in the union
95  // (the union is called Object, and the instance is called object).
97  amrex::Real
98  getFlux (amrex::Real x, amrex::Real y, amrex::Real z, amrex::Real t) const noexcept
99  {
100  switch (type)
101  {
102  case Type::parser:
103  {
104  return object.parser.getFlux(x,y,z,t);
105  }
106  case Type::constant:
107  {
108  return object.constant.getFlux(x,y,z,t);
109  }
110  default:
111  {
112  amrex::Abort("InjectorFlux: unknown type");
113  return 0.0;
114  }
115  }
116  }
117 
118 private:
119  enum struct Type { constant, parser };
121 
122  // An instance of union Object constructs and stores any one of
123  // the objects declared (constant or parser).
124  union Object {
125  Object (InjectorFluxConstant*, amrex::Real a_flux) noexcept
126  : constant(a_flux) {}
128  : parser(a_parser) {}
131  };
133 };
134 
135 // In order for InjectorFlux to be trivially copyable, its destructor
136 // must be trivial. So we have to rely on a custom deleter for unique_ptr.
138  void operator () (InjectorFlux* p) const {
139  if (p) {
140  p->clear();
141  delete p;
142  }
143  }
144 };
145 
146 #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:137
void operator()(InjectorFlux *p) const
Definition: InjectorFlux.H:138
Definition: InjectorFlux.H:62
void clear()
Definition: InjectorFlux.H:82
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:132
InjectorFlux(InjectorFluxParser *t, amrex::ParserExecutor< 4 > const &a_parser)
Definition: InjectorFlux.H:70
Type
Definition: InjectorFlux.H:119
InjectorFlux(InjectorFlux &&)=delete
Type type
Definition: InjectorFlux.H:120
AMREX_GPU_HOST_DEVICE amrex::Real getFlux(amrex::Real x, amrex::Real y, amrex::Real z, amrex::Real t) const noexcept
Definition: InjectorFlux.H:98
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:124
Object(InjectorFluxParser *, amrex::ParserExecutor< 4 > const &a_parser) noexcept
Definition: InjectorFlux.H:127
Object(InjectorFluxConstant *, amrex::Real a_flux) noexcept
Definition: InjectorFlux.H:125
InjectorFluxParser parser
Definition: InjectorFlux.H:130
InjectorFluxConstant constant
Definition: InjectorFlux.H:129