WarpX
WarpXParser.H
Go to the documentation of this file.
1 /* Copyright 2019 Weiqun Zhang
2  *
3  * This file is part of WarpX.
4  *
5  * License: BSD-3-Clause-LBNL
6  */
7 #ifndef WARPX_PARSER_H_
8 #define WARPX_PARSER_H_
9 
10 #include <array>
11 #include <vector>
12 #include <string>
13 #include <set>
14 
15 #include <AMReX_REAL.H>
16 
17 #include "wp_parser_c.h"
18 #include "wp_parser_y.h"
19 
20 #ifdef _OPENMP
21 #include <omp.h>
22 #endif
23 
24 template <int N> class GpuParser;
25 
27 {
28 public:
29  WarpXParser (std::string const& func_body);
30  WarpXParser () = default;
31  ~WarpXParser ();
32  void define (std::string const& func_body);
33 
34  void setConstant (std::string const& name, amrex::Real c);
35 
36  //
37  // Option 1: Register every variable to an address provided.
38  // Assign values to external variables.
39  // Call eval().
40  void registerVariable (std::string const& name, amrex::Real& var);
41  //
42  inline amrex::Real eval () const noexcept;
43 
44  //
45  // Option 2: Register all variables at once. Parser will create
46  // variables internally.
47  // Call eval(...) with variable values.
48  void registerVariables (std::vector<std::string> const& names);
49  //
50  template <typename T, typename... Ts> inline
51  amrex::Real eval (T x, Ts... yz) const noexcept;
52 
53  void print () const;
54 
55  int depth () const;
56 
57  std::string const& expr () const;
58 
59  std::set<std::string> symbols () const;
60 
61  template <int N> friend class GpuParser;
62 
63 private:
64  void clear ();
65 
66  template <typename T> inline
67  void unpack (amrex::Real* p, T x) const noexcept;
68 
69  template <typename T, typename... Ts> inline
70  void unpack (amrex::Real* p, T x, Ts... yz) const noexcept;
71 
72  std::string m_expression;
73 #ifdef _OPENMP
74  std::vector<struct wp_parser*> m_parser;
75  mutable std::vector<std::array<amrex::Real,16> > m_variables;
76  mutable std::vector<std::vector<std::string> > m_varnames;
77 #else
78  struct wp_parser* m_parser = nullptr;
79  mutable std::array<amrex::Real,16> m_variables;
80  mutable std::vector<std::string> m_varnames;
81 #endif
82 };
83 
84 inline
85 amrex::Real
86 WarpXParser::eval () const noexcept
87 {
88 #ifdef _OPENMP
89  return wp_ast_eval<0>(m_parser[omp_get_thread_num()]->ast,nullptr);
90 #else
91  return wp_ast_eval<0>(m_parser->ast,nullptr);
92 #endif
93 }
94 
95 template <typename T, typename... Ts>
96 inline
97 amrex::Real
98 WarpXParser::eval (T x, Ts... yz) const noexcept
99 {
100 #ifdef _OPENMP
101  unpack(m_variables[omp_get_thread_num()].data(), x, yz...);
102 #else
103  unpack(m_variables.data(), x, yz...);
104 #endif
105  return eval();
106 }
107 
108 template <typename T>
109 inline
110 void
111 WarpXParser::unpack (amrex::Real* p, T x) const noexcept
112 {
113  *p = x;
114 }
115 
116 template <typename T, typename... Ts>
117 inline
118 void
119 WarpXParser::unpack (amrex::Real* p, T x, Ts... yz) const noexcept
120 {
121  *p++ = x;
122  unpack(p, yz...);
123 }
124 
125 #endif
~WarpXParser()
Definition: WarpXParser.cpp:48
data
Definition: run_alltests_1node.py:320
void unpack(amrex::Real *p, T x) const noexcept
Definition: WarpXParser.H:111
def x
Definition: read_lab_particles.py:25
struct wp_parser * m_parser
Definition: WarpXParser.H:78
std::array< amrex::Real, 16 > m_variables
Definition: WarpXParser.H:79
void define(std::string const &func_body)
Definition: WarpXParser.cpp:17
void setConstant(std::string const &name, amrex::Real c)
Definition: WarpXParser.cpp:120
Definition: wp_parser_y.h:145
void registerVariables(std::vector< std::string > const &names)
Definition: WarpXParser.cpp:93
struct wp_node * ast
Definition: wp_parser_y.h:148
WarpXParser()=default
name
Definition: run_automated.py:204
Definition: WarpXParser.H:26
void print() const
Definition: WarpXParser.cpp:139
std::vector< std::string > m_varnames
Definition: WarpXParser.H:80
int depth() const
Definition: WarpXParser.cpp:150
names
Definition: plot_results.py:41
std::string const & expr() const
Definition: WarpXParser.cpp:162
std::set< std::string > symbols() const
Definition: WarpXParser.cpp:168
amrex::Real eval() const noexcept
Definition: WarpXParser.H:86
void clear()
Definition: WarpXParser.cpp:54
void registerVariable(std::string const &name, amrex::Real &var)
Definition: WarpXParser.cpp:80
std::string m_expression
Definition: WarpXParser.H:72
Definition: GpuParser.H:23