WarpX
WarpXUtil.H
Go to the documentation of this file.
1 /* Copyright 2019-2020 Andrew Myers, Luca Fedeli, Maxence Thevenet
2  * Revathi Jambunathan
3  *
4  * This file is part of WarpX.
5  *
6  * License: BSD-3-Clause-LBNL
7  */
8 #ifndef WARPX_UTILS_H_
9 #define WARPX_UTILS_H_
10 
11 #include <AMReX_Extension.H>
12 #include <AMReX_GpuQualifiers.H>
13 #include <AMReX_Parser.H>
14 #include <AMReX_REAL.H>
15 #include <AMReX_Utility.H>
16 #include <AMReX_Vector.H>
17 
18 #include <AMReX_BaseFwd.H>
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include <string>
23 #include <vector>
24 
25 void ParseGeometryInput();
26 
27 void ReadBoostedFrameParameters(amrex::Real& gamma_boost, amrex::Real& beta_boost,
28  amrex::Vector<int>& boost_direction);
29 
31 
35 void ReadBCParams ();
36 
41 
42 void NullifyMF(amrex::MultiFab& mf, int lev, amrex::Real zmin,
43  amrex::Real zmax);
44 
54 void Store_parserString(const amrex::ParmParse &pp, std::string query_string,
55  std::string& stored_string);
56 
57 namespace WarpXUtilIO{
64 bool WriteBinaryDataOnFile(std::string filename, const amrex::Vector<char>& data);
65 
72 constexpr uint64_t
73 localIDtoGlobal(int const id, int const cpu)
74 {
75  static_assert(sizeof(int) * 2u <= sizeof(uint64_t),
76  "int size might cause collisions in global IDs");
77  // implementation:
78  // - we cast both 32-bit (or smaller) ints to a 64bit unsigned int
79  // - this will leave half of the "upper range" bits in the 64bit unsigned int zeroed out
80  // because the corresponding (extended) value range was not part of the value range in
81  // the int representation
82  // - we bit-shift the cpu into the upper half of zero bits in the 64 bit unsigned int
83  // (imagine this step as "adding a per-cpu/rank offset to the local integers")
84  // - then we add this offset
85  // note: the add is expressed as bitwise OR (|) since this saves us from writing
86  // brackets for operator precedence between + and <<
87  return uint64_t(id) | uint64_t(cpu) << 32u;
88 }
89 }
90 
91 namespace WarpXUtilAlgo{
92 
101 template<typename T> AMREX_GPU_DEVICE AMREX_FORCE_INLINE
102 const T* upper_bound(const T* first, const T* last, const T& val)
103 {
104  const T* it;
105  size_t count, step;
106  count = last-first;
107  while(count>0){
108  it = first;
109  step = count/2;
110  it += step;
111  if (!(val<*it)){
112  first = ++it;
113  count -= step + 1;
114  }
115  else{
116  count = step;
117  }
118  }
119  return first;
120 }
121 
127 template<typename T> AMREX_GPU_DEVICE AMREX_FORCE_INLINE
128 T linear_interp(T x0, T x1, T f0, T f1, T x)
129 {
130  return ((x1-x)*f0 + (x-x0)*f1)/(x1-x0);
131 }
132 
138 template<typename T> AMREX_GPU_DEVICE AMREX_FORCE_INLINE
139 T bilinear_interp(T x0, T x1, T y0, T y1, T f00, T f01, T f10, T f11, T x, T y)
140 {
141  const T fx0 = linear_interp(x0, x1, f00, f10, x);
142  const T fx1 = linear_interp(x0, x1, f01, f11, x);
143  return linear_interp(y0, y1, fx0, fx1, y);
144 }
145 
152 template<typename T> AMREX_GPU_DEVICE AMREX_FORCE_INLINE
153 T trilinear_interp(T x0, T x1,T y0, T y1, T z0, T z1,
154  T f000, T f001, T f010, T f011, T f100, T f101, T f110, T f111,
155  T x, T y, T z)
156 {
157  const T fxy0 = bilinear_interp(
158  x0, x1, y0, y1, f000, f010, f100, f110, x, y);
159  const T fxy1 = bilinear_interp(
160  x0, x1, y0, y1, f001, f011, f101, f111, x, y);
161  return linear_interp(z0, z1, fxy0, fxy1, z);
162 }
163 
164 }
165 
174 int
175 safeCastToInt(amrex::Real x, const std::string& real_name);
176 
183 amrex::Parser makeParser (std::string const& parse_function, amrex::Vector<std::string> const& varnames);
184 
193 amrex::Real parseStringtoReal(std::string str);
194 
204 int parseStringtoInt(std::string str, std::string name);
205 
206 template <int N>
207 amrex::ParserExecutor<N> compileParser (amrex::Parser const* parser)
208 {
209  if (parser) {
210  return parser->compile<N>();
211  } else {
212  return amrex::ParserExecutor<N>{};
213  }
214 }
215 
230 int queryWithParser (const amrex::ParmParse& a_pp, char const * const str, amrex::Real& val);
231 int queryArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<amrex::Real>& val,
232  const int start_ix, const int num_val);
233 int queryWithParser (const amrex::ParmParse& a_pp, char const * const str, int& val);
234 int queryArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<int>& val,
235  const int start_ix, const int num_val);
236 
251 void getWithParser (const amrex::ParmParse& a_pp, char const * const str, amrex::Real& val);
252 void getArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<amrex::Real>& val);
253 void getArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<amrex::Real>& val,
254  const int start_ix, const int num_val);
255 void getWithParser (const amrex::ParmParse& a_pp, char const * const str, int& val);
256 void getArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<int>& val);
257 void getArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<int>& val,
258  const int start_ix, const int num_val);
259 
260 namespace WarpXUtilMsg{
261 
267 void AlwaysAssert(bool is_expression_true, const std::string& msg);
268 
269 }
270 
271 namespace WarpXUtilStr
272 {
278  bool is_in(const std::vector<std::string>& vect,
279  const std::string& elem);
280 
286  bool is_in(const std::vector<std::string>& vect,
287  const std::vector<std::string>& elems);
288 
303  template <typename Container>
304  auto split (std::string const& instr, std::string const& separator,
305  bool const trim = false, std::string const& trim_space = " \t")
306  {
307  Container cont;
308  std::size_t current = instr.find(separator);
309  std::size_t previous = 0;
310  while (current != std::string::npos) {
311  if (trim){
312  cont.push_back(amrex::trim(instr.substr(previous, current - previous),trim_space));}
313  else{
314  cont.push_back(instr.substr(previous, current - previous));}
315  previous = current + separator.size();
316  current = instr.find(separator, previous);
317  }
318  if (trim){
319  cont.push_back(amrex::trim(instr.substr(previous, current - previous),trim_space));}
320  else{
321  cont.push_back(instr.substr(previous, current - previous));}
322  return cont;
323  }
324 
325 }
326 
327 #endif //WARPX_UTILS_H_
Definition: WarpXUtil.cpp:675
void getArrWithParser(const amrex::ParmParse &a_pp, char const *const str, std::vector< amrex::Real > &val)
Definition: WarpXUtil.cpp:426
int parseStringtoInt(std::string str, std::string name)
Definition: WarpXUtil.cpp:372
AMREX_GPU_DEVICE AMREX_FORCE_INLINE T trilinear_interp(T x0, T x1, T y0, T y1, T z0, T z1, T f000, T f001, T f010, T f011, T f100, T f101, T f110, T f111, T x, T y, T z)
Performs a trilinear interpolation.
Definition: WarpXUtil.H:153
str
Definition: run_alltests_1node.py:67
list y1
Definition: plot_particle_path.py:129
int safeCastToInt(amrex::Real x, const std::string &real_name)
Do a safe cast of a real to an int This ensures that the float value is within the range of ints and ...
Definition: WarpXUtil.cpp:274
void ReadBCParams()
Definition: WarpXUtil.cpp:597
parser
Definition: run_alltests.py:107
data
Definition: run_alltests_1node.py:320
AMREX_GPU_DEVICE AMREX_FORCE_INLINE T linear_interp(T x0, T x1, T f0, T f1, T x)
Performs a linear interpolation.
Definition: WarpXUtil.H:128
int gamma_boost
Definition: compute_domain.py:41
Definition: WarpXUtil.cpp:686
def x
Definition: read_lab_particles.py:25
constexpr uint64_t localIDtoGlobal(int const id, int const cpu)
Definition: WarpXUtil.H:73
void ParseGeometryInput()
Definition: WarpXUtil.cpp:54
int queryArrWithParser(const amrex::ParmParse &a_pp, char const *const str, std::vector< amrex::Real > &val, const int start_ix, const int num_val)
Definition: WarpXUtil.cpp:406
void ConvertLabParamsToBoost()
Definition: WarpXUtil.cpp:133
int count
Definition: run_alltests.py:318
void ReadBoostedFrameParameters(amrex::Real &gamma_boost, amrex::Real &beta_boost, amrex::Vector< int > &boost_direction)
def z
Definition: read_lab_particles.py:26
void CheckGriddingForRZSpectral()
Ensures that the blocks are setup correctly for the RZ spectral solver.
Definition: WarpXUtil.cpp:509
filename
Definition: write_atomic_data_cpp.py:19
bool is_in(const std::vector< std::string > &vect, const std::string &elem)
Definition: WarpXUtil.cpp:688
AMREX_GPU_DEVICE AMREX_FORCE_INLINE T bilinear_interp(T x0, T x1, T y0, T y1, T f00, T f01, T f10, T f11, T x, T y)
Performs a bilinear interpolation.
Definition: WarpXUtil.H:139
AMREX_GPU_DEVICE AMREX_FORCE_INLINE const T * upper_bound(const T *first, const T *last, const T &val)
Returns a pointer to the first element in the range [first, last) that is greater than val...
Definition: WarpXUtil.H:102
Definition: WarpXUtil.H:91
int queryWithParser(const amrex::ParmParse &a_pp, char const *const str, amrex::Real &val)
Similar to amrex::ParmParse::query, but also supports math expressions for the value.
Definition: WarpXUtil.cpp:380
amrex::ParserExecutor< N > compileParser(amrex::Parser const *parser)
Definition: WarpXUtil.H:207
amrex::Real parseStringtoReal(std::string str)
Definition: WarpXUtil.cpp:363
amrex::Parser makeParser(std::string const &parse_function, amrex::Vector< std::string > const &varnames)
Initialize an amrex::Parser object from a string containing a math expression.
Definition: WarpXUtil.cpp:298
void getWithParser(const amrex::ParmParse &a_pp, char const *const str, amrex::Real &val)
Similar to amrex::ParmParse::get, but also supports math expressions for the value.
Definition: WarpXUtil.cpp:397
name
Definition: run_automated.py:204
list y0
Definition: plot_particle_path.py:127
Definition: WarpXUtil.cpp:252
int it
Definition: read_lab_particles.py:11
list x0
Definition: plot_particle_path.py:126
auto split(std::string const &instr, std::string const &separator, bool const trim=false, std::string const &trim_space=" \)
Splits a string using a string separator. This is somewhat similar to amrex::Tokenize. The main difference is that, if the separator ":" is used, amrex::Tokenize will split ":3::2" into ["3","2"] while this functio will split ":3::2" into ["","3","","2"]. This function can also perform a trimming to remove whitespaces (or any other arbitrary string) from the split string.
Definition: WarpXUtil.H:304
list x1
Definition: plot_particle_path.py:128
void NullifyMF(amrex::MultiFab &mf, int lev, amrex::Real zmin, amrex::Real zmax)
Definition: WarpXUtil.cpp:210
Definition: GetExternalFields.H:15
bool WriteBinaryDataOnFile(std::string filename, const amrex::Vector< char > &data)
Definition: WarpXUtil.cpp:253
void AlwaysAssert(bool is_expression_true, const std::string &msg="ERROR!")
If is_expression_true is false, this function prints msg and calls amrex::abort() ...
Definition: WarpXUtil.cpp:677
void Store_parserString(const amrex::ParmParse &pp, std::string query_string, std::string &stored_string)
Parse a string (typically a mathematical expression) from the input file and store it into a variable...
Definition: WarpXUtil.cpp:262