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 "Parser/WarpXParser.H"
12 
13 #include <AMReX_REAL.H>
14 #include <AMReX_Vector.H>
15 #include <AMReX_MultiFab.H>
16 #include <AMReX_ParmParse.H>
17 #include <AMReX_Utility.H>
18 
19 #include <cstdint>
20 #include <string>
21 
22 
23 void ReadBoostedFrameParameters(amrex::Real& gamma_boost, amrex::Real& beta_boost,
24  amrex::Vector<int>& boost_direction);
25 
27 
32 
33 void NullifyMF(amrex::MultiFab& mf, int lev, amrex::Real zmin,
34  amrex::Real zmax);
35 
45 void Store_parserString(amrex::ParmParse &pp, std::string query_string,
46  std::string& stored_string);
47 
48 namespace WarpXUtilIO{
55 bool WriteBinaryDataOnFile(std::string filename, const amrex::Vector<char>& data);
56 
63 constexpr uint64_t
64 localIDtoGlobal(int const id, int const cpu)
65 {
66  static_assert(sizeof(int) * 2u <= sizeof(uint64_t),
67  "int size might cause collisions in global IDs");
68  // implementation:
69  // - we cast both 32-bit (or smaller) ints to a 64bit unsigned int
70  // - this will leave half of the "upper range" bits in the 64bit unsigned int zeroed out
71  // because the corresponding (extended) value range was not part of the value range in
72  // the int representation
73  // - we bit-shift the cpu into the upper half of zero bits in the 64 bit unsigned int
74  // (imagine this step as "adding a per-cpu/rank offset to the local integers")
75  // - then we add this offset
76  // note: the add is expressed as bitwise OR (|) since this saves us from writing
77  // brackets for operator precedence between + and <<
78  return uint64_t(id) | uint64_t(cpu) << 32u;
79 }
80 }
81 
82 namespace WarpXUtilAlgo{
83 
92 template<typename T> AMREX_GPU_DEVICE AMREX_FORCE_INLINE
93 const T* upper_bound(const T* first, const T* last, const T& val)
94 {
95  const T* it;
96  size_t count, step;
97  count = last-first;
98  while(count>0){
99  it = first;
100  step = count/2;
101  it += step;
102  if (!(val<*it)){
103  first = ++it;
104  count -= step + 1;
105  }
106  else{
107  count = step;
108  }
109  }
110  return first;
111 }
112 
118 template<typename T> AMREX_GPU_DEVICE AMREX_FORCE_INLINE
119 T linear_interp(T x0, T x1, T f0, T f1, T x)
120 {
121  return ((x1-x)*f0 + (x-x0)*f1)/(x1-x0);
122 }
123 
129 template<typename T> AMREX_GPU_DEVICE AMREX_FORCE_INLINE
130 T bilinear_interp(T x0, T x1, T y0, T y1, T f00, T f01, T f10, T f11, T x, T y)
131 {
132  const T fx0 = linear_interp(x0, x1, f00, f10, x);
133  const T fx1 = linear_interp(x0, x1, f01, f11, x);
134  return linear_interp(y0, y1, fx0, fx1, y);
135 }
136 
143 template<typename T> AMREX_GPU_DEVICE AMREX_FORCE_INLINE
144 T trilinear_interp(T x0, T x1,T y0, T y1, T z0, T z1,
145  T f000, T f001, T f010, T f011, T f100, T f101, T f110, T f111,
146  T x, T y, T z)
147 {
148  const T fxy0 = bilinear_interp(
149  x0, x1, y0, y1, f000, f010, f100, f110, x, y);
150  const T fxy1 = bilinear_interp(
151  x0, x1, y0, y1, f001, f011, f101, f111, x, y);
152  return linear_interp(z0, z1, fxy0, fxy1, z);
153 }
154 
155 }
156 
162 WarpXParser makeParser (std::string const& parse_function, std::vector<std::string> const& varnames);
163 
164 namespace WarpXUtilMsg{
165 
171 void AlwaysAssert(bool is_expression_true, const std::string& msg);
172 
173 }
174 
175 namespace WarpXUtilStr
176 {
182  bool is_in(const std::vector<std::string>& vect,
183  const std::string& elem);
184 
190  bool is_in(const std::vector<std::string>& vect,
191  const std::vector<std::string>& elems);
192 
207  template <typename Container>
208  auto split (std::string const& instr, std::string const& separator,
209  bool const trim = false, std::string const& trim_space = " \t")
210  {
211  Container cont;
212  std::size_t current = instr.find(separator);
213  std::size_t previous = 0;
214  while (current != std::string::npos) {
215  if (trim){
216  cont.push_back(amrex::trim(instr.substr(previous, current - previous),trim_space));}
217  else{
218  cont.push_back(instr.substr(previous, current - previous));}
219  previous = current + separator.size();
220  current = instr.find(separator, previous);
221  }
222  if (trim){
223  cont.push_back(amrex::trim(instr.substr(previous, current - previous),trim_space));}
224  else{
225  cont.push_back(instr.substr(previous, current - previous));}
226  return cont;
227  }
228 
229 }
230 
231 #endif //WARPX_UTILS_H_
Definition: WarpXUtil.cpp:296
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:144
WarpXParser makeParser(std::string const &parse_function, std::vector< std::string > const &varnames)
Initialize a WarpXParser object from a string containing a math expression.
Definition: WarpXUtil.cpp:194
list y1
Definition: plot_particle_path.py:129
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:119
int gamma_boost
Definition: compute_domain.py:41
Definition: WarpXUtil.cpp:307
def x
Definition: read_lab_particles.py:25
constexpr uint64_t localIDtoGlobal(int const id, int const cpu)
Definition: WarpXUtil.H:64
void ConvertLabParamsToBoost()
Definition: WarpXUtil.cpp:50
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:224
filename
Definition: write_atomic_data_cpp.py:17
bool is_in(const std::vector< std::string > &vect, const std::string &elem)
Definition: WarpXUtil.cpp:309
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:130
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:93
Definition: WarpXUtil.H:82
void Store_parserString(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:181
list y0
Definition: plot_particle_path.py:127
Definition: WarpXParser.H:26
Definition: WarpXUtil.cpp:171
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:208
list x1
Definition: plot_particle_path.py:128
void NullifyMF(amrex::MultiFab &mf, int lev, amrex::Real zmin, amrex::Real zmax)
Definition: WarpXUtil.cpp:129
bool WriteBinaryDataOnFile(std::string filename, const amrex::Vector< char > &data)
Definition: WarpXUtil.cpp:172
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:298