Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_ExprEval.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Panzer: A partial differential equation assembly
5 // engine for strongly coupled complex multiphysics systems
6 // Copyright (2011) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roger P. Pawlowski (rppawlo@sandia.gov) and
39 // Eric C. Cyr (eccyr@sandia.gov)
40 // ***********************************************************************
41 // @HEADER
42 
43 #ifndef PANZER_EXPR_EVAL_HPP
44 #define PANZER_EXPR_EVAL_HPP
45 
50 #include <functional>
51 #include <map>
52 #include <type_traits>
53 
54 #include <Teuchos_Reader.hpp>
55 
56 #include <Kokkos_Core.hpp>
57 
58 namespace panzer
59 {
60 
64 namespace Expr
65 {
66 
71 enum class BinaryOpCode {
72  OR,
73  AND,
74  GT,
75  LT,
76  GEQ,
77  LEQ,
78  EQ,
79  ADD,
80  SUB,
81  MUL,
82  DIV,
83  POW,
84 };
85 
90 class EvalBase : public Teuchos::Reader {
91  public:
95  EvalBase();
104  using Function = std::function<void(std::string const& name, Teuchos::any&, std::vector<Teuchos::any>& rhs)>;
108  void set(std::string const& name, Function const& value);
112  template <typename T>
113  T const& get(std::string const& name) const {
114  auto it = symbol_map.find(name);
115  TEUCHOS_TEST_FOR_EXCEPTION(it == symbol_map.end(), std::logic_error,
116  "EvalBase::get: \"" << name << "\" not found");
117  return Teuchos::any_ref_cast<T>(it->second);
118  }
119  protected:
123  std::map<std::string, Teuchos::any> symbol_map;
124 
126  void at_shift(Teuchos::any& result, int token, std::string& text) override;
128  void at_reduce(Teuchos::any& result, int prod, std::vector<Teuchos::any>& rhs) override;
144  void neg_op(Teuchos::any& result, Teuchos::any& right);
145  protected:
146 
149  virtual void make_constant(Teuchos::any& result, double const& value) = 0;
150  virtual void inspect_arg(Teuchos::any const& arg, bool& is_many, bool& is_bool) = 0;
151  virtual void single_single_ternary_op(Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) = 0;
152  virtual void single_many_ternary_op(Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) = 0;
153  virtual void many_single_ternary_op(Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) = 0;
154  virtual void many_many_ternary_op(Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) = 0;
155  virtual void single_single_binary_op(BinaryOpCode code, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) = 0;
156  virtual void single_many_binary_op(BinaryOpCode code, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) = 0;
157  virtual void many_single_binary_op(BinaryOpCode code, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) = 0;
158  virtual void many_many_binary_op(BinaryOpCode code, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) = 0;
159  virtual void many_neg_op(Teuchos::any& result, Teuchos::any& right) = 0;
160  virtual void single_neg_op(Teuchos::any& result, Teuchos::any& right) = 0;
162 };
163 
165 template <typename DataType, typename NewScalarType>
168  using type = NewScalarType;
169 };
170 
171 template <typename NestedDataType, typename NewScalarType>
172 struct RebindDataType<NestedDataType*, NewScalarType> {
174 };
175 
176 template <typename NestedDataType, typename NewScalarType>
177 struct RebindDataType<NestedDataType[], NewScalarType> {
179 };
180 
181 template <typename NestedDataType, typename NewScalarType, size_t N>
182 struct RebindDataType<NestedDataType[N], NewScalarType> {
184 };
185 
187 template <typename ViewType, typename NewScalarType>
189 
190 template <typename DT, typename NewScalarType, typename ... VP>
191 struct RebindViewType<Kokkos::View<DT, VP ...>, NewScalarType> {
193  using type = Kokkos::View<typename RebindDataType<DT, NewScalarType>::type, VP ...>;
194 };
195 
228 template <typename DT, typename ... VP>
229 class Eval : public EvalBase {
230  public:
232  using original_view_type = Kokkos::View<DT, VP ...>;
234  using view_data_type = DT;
241  using scalar_type = typename original_view_type::non_const_value_type;
249  using const_view_type = Kokkos::View<typename RebindDataType<view_data_type, scalar_type const>::type, VP ...>;
253  using const_bool_view_type = Kokkos::View<typename RebindDataType<view_data_type, bool const>::type, VP ...>;
257  using single_view_type = Kokkos::View<scalar_type, VP ...>;
261  using const_single_view_type = Kokkos::View<scalar_type const, VP ...>;
265  using single_bool_view_type = Kokkos::View<bool, VP ...>;
269  using const_single_bool_view_type = Kokkos::View<bool const, VP ...>;
270 
271  Eval();
272 
276  void set(std::string const& name, bool value);
280  void set(std::string const& name, scalar_type const& value);
284  void set(std::string const& name, const_view_type const& value);
285  protected:
286  void make_constant(Teuchos::any& result, double const& value) override;
287  void inspect_arg(Teuchos::any const& arg, bool& is_many, bool& is_bool) override;
289  void single_many_ternary_op(Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) override;
290  void many_single_ternary_op(Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) override;
291  void many_many_ternary_op(Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) override;
293  void single_many_binary_op(BinaryOpCode code, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) override;
294  void many_single_binary_op(BinaryOpCode code, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) override;
295  void many_many_binary_op(BinaryOpCode code, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) override;
296  void many_neg_op(Teuchos::any& result, Teuchos::any& right) override;
297  void single_neg_op(Teuchos::any& result, Teuchos::any& right) override;
298 };
299 
317 template <typename DT, typename ... VP>
319 
320 }} // end namespace panzer::Expr
321 
322 #endif // PANZER_EXPR_EVAL_HPP
void many_many_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right) override
Base class for panzer::Expr::Eval, does everything that is independent of the Kokkos::View template p...
void make_constant(Teuchos::any &result, double const &value) override
virtual void many_many_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right)=0
Kokkos::View< scalar_type const, VP...> const_single_view_type
One scalar (same for all evaluation points), read-only.
Kokkos::View< typename RebindDataType< view_data_type, scalar_type const >::type, VP...> const_view_type
One scalar for each evaluation point, read-only.
void set(std::string const &name, bool value)
Assign a boolean value to a variable symbol.
Rebinds a Kokkos::View data type to use a new scalar type.
virtual void make_constant(Teuchos::any &result, double const &value)=0
BinaryOpCode
Denotes the native binary operators in the Teuchos::MathExpr language.
void single_many_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right) override
virtual void many_many_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right)=0
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual void single_single_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right)=0
std::function< void(std::string const &name, Teuchos::any &, std::vector< Teuchos::any > &rhs)> Function
The type of user-defined functions which are callable in the math language.
virtual void many_neg_op(Teuchos::any &result, Teuchos::any &right)=0
typename RebindDataType< NestedDataType, NewScalarType >::type * type
typename RebindDataType< NestedDataType, NewScalarType >::type[] type
virtual void inspect_arg(Teuchos::any const &arg, bool &is_many, bool &is_bool)=0
void ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right)
Executes the ternary operator, e.g. (a &gt; b) ? a : b.
void single_neg_op(Teuchos::any &result, Teuchos::any &right) override
Kokkos::View< typename RebindDataType< DT, NewScalarType >::type, VP...> type
The new Kokkos::View type, whose scalar type is now NewScalarType.
void at_reduce(Teuchos::any &result, int prod, std::vector< Teuchos::any > &rhs) override
Called at every reduced production in the math language.
void binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right)
Executes a binary operator.
Kokkos::View< bool const, VP...> const_single_bool_view_type
One boolean (same for all evaluation points), read-only.
void set(std::string const &name, Function const &value)
Registers an EvalBase::Function, binding it to a name and making it callable.
Kokkos::View< typename RebindDataType< view_data_type, bool const >::type, VP...> const_bool_view_type
One boolean for each evaluation point, read-only.
PHX::MDField< ScalarT, panzer::Cell, panzer::IP > result
A field that will be used to build up the result of the integral we&#39;re performing.
virtual void single_single_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right)=0
void single_many_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right) override
void inspect_arg(Teuchos::any const &arg, bool &is_many, bool &is_bool) override
typename original_view_type::non_const_value_type scalar_type
The scalar type.
void many_many_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right) override
typename RebindDataType< NestedDataType, NewScalarType >::type[N] type
virtual void many_single_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right)=0
std::map< std::string, Teuchos::any > symbol_map
Stores all current symbols including variables and functions.
virtual void single_neg_op(Teuchos::any &result, Teuchos::any &right)=0
Builds on RebindDataType, but acts directly on a Kokkos::View type.
Interprets mathematical expressions in a string and evaluates them using Kokkos::View objects as valu...
virtual void single_many_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right)=0
void many_neg_op(Teuchos::any &result, Teuchos::any &right) override
void neg_op(Teuchos::any &result, Teuchos::any &right)
Executes the only native unary operator in the math language, numeric negation via a minus sign...
void many_single_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right) override
Kokkos::View< DT, VP...> original_view_type
The corresponding Kokkos::View type, using the same template arguments are were given to Eval...
Kokkos::View< bool, VP...> single_bool_view_type
One boolean (same for all evaluation points)
void at_shift(Teuchos::any &result, int token, std::string &text) override
Called at every parsed token in the math language.
Kokkos::View< scalar_type, VP...> single_view_type
One scalar (same for all evaluation points)
NewScalarType type
The new data type, suitable as the first template argument to Kokkos::View.
virtual void many_single_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right)=0
DT view_data_type
The data type, including dimension information.
void single_single_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right) override
void single_single_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right) override
virtual void single_many_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right)=0
void set_cmath_functions(Eval< DT, VP...> &eval)
Add support for functions such as sqrt(), sin(), and cos()
void many_single_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right) override