Panzer Version of the Day
Loading...
Searching...
No Matches
Panzer_ExprEval.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Panzer: A partial differential equation assembly
4// engine for strongly coupled complex multiphysics systems
5//
6// Copyright 2011 NTESS and the Panzer contributors.
7// SPDX-License-Identifier: BSD-3-Clause
8// *****************************************************************************
9// @HEADER
10
11#ifndef PANZER_EXPR_EVAL_HPP
12#define PANZER_EXPR_EVAL_HPP
13
18#include <functional>
19#include <map>
20#include <type_traits>
21
22#include <Teuchos_Reader.hpp>
23
24#include <Kokkos_Core.hpp>
25
26namespace panzer
27{
28
32namespace Expr
33{
34
39enum class BinaryOpCode {
40 OR,
41 AND,
42 GT,
43 LT,
44 GEQ,
45 LEQ,
46 EQ,
47 ADD,
48 SUB,
49 MUL,
50 DIV,
51 POW,
52};
53
58class EvalBase : public Teuchos::Reader {
59 public:
63 EvalBase();
72 using Function = std::function<void(std::string const& name, Teuchos::any&, std::vector<Teuchos::any>& rhs)>;
76 void set(std::string const& name, Function const& value);
80 template <typename T>
81 T const& get(std::string const& name) const {
82 auto it = symbol_map.find(name);
83 TEUCHOS_TEST_FOR_EXCEPTION(it == symbol_map.end(), std::logic_error,
84 "EvalBase::get: \"" << name << "\" not found");
85 return Teuchos::any_ref_cast<T>(it->second);
86 }
87 protected:
91 std::map<std::string, Teuchos::any> symbol_map;
92
94 void at_shift(Teuchos::any& result, int token, std::string& text) override;
96 void at_reduce(Teuchos::any& result, int prod, std::vector<Teuchos::any>& rhs) override;
98 void ternary_op(Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right);
110 void binary_op(BinaryOpCode code, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right);
112 void neg_op(Teuchos::any& result, Teuchos::any& right);
113 protected:
114
117 virtual void make_constant(Teuchos::any& result, double const& value) = 0;
118 virtual void inspect_arg(Teuchos::any const& arg, bool& is_many, bool& is_bool) = 0;
119 virtual void single_single_ternary_op(Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) = 0;
120 virtual void single_many_ternary_op(Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) = 0;
121 virtual void many_single_ternary_op(Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) = 0;
122 virtual void many_many_ternary_op(Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) = 0;
123 virtual void single_single_binary_op(BinaryOpCode code, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) = 0;
124 virtual void single_many_binary_op(BinaryOpCode code, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) = 0;
125 virtual void many_single_binary_op(BinaryOpCode code, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) = 0;
126 virtual void many_many_binary_op(BinaryOpCode code, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) = 0;
127 virtual void many_neg_op(Teuchos::any& result, Teuchos::any& right) = 0;
128 virtual void single_neg_op(Teuchos::any& result, Teuchos::any& right) = 0;
130};
131
133template <typename DataType, typename NewScalarType>
136 using type = NewScalarType;
137};
138
139template <typename NestedDataType, typename NewScalarType>
140struct RebindDataType<NestedDataType*, NewScalarType> {
142};
143
144template <typename NestedDataType, typename NewScalarType>
145struct RebindDataType<NestedDataType[], NewScalarType> {
147};
148
149template <typename NestedDataType, typename NewScalarType, size_t N>
150struct RebindDataType<NestedDataType[N], NewScalarType> {
152};
153
155template <typename ViewType, typename NewScalarType>
157
158template <typename DT, typename NewScalarType, typename ... VP>
159struct RebindViewType<Kokkos::View<DT, VP ...>, NewScalarType> {
161 using type = Kokkos::View<typename RebindDataType<DT, NewScalarType>::type, VP ...>;
162};
163
196template <typename DT, typename ... VP>
197class Eval : public EvalBase {
198 public:
200 using original_view_type = Kokkos::View<DT, VP ...>;
202 using view_data_type = DT;
209 using scalar_type = typename original_view_type::non_const_value_type;
217 using const_view_type = Kokkos::View<typename RebindDataType<view_data_type, scalar_type const>::type, VP ...>;
221 using const_bool_view_type = Kokkos::View<typename RebindDataType<view_data_type, bool const>::type, VP ...>;
225 using single_view_type = Kokkos::View<scalar_type, VP ...>;
229 using const_single_view_type = Kokkos::View<scalar_type const, VP ...>;
233 using single_bool_view_type = Kokkos::View<bool, VP ...>;
237 using const_single_bool_view_type = Kokkos::View<bool const, VP ...>;
238
239 Eval();
240
244 void set(std::string const& name, bool value);
248 void set(std::string const& name, scalar_type const& value);
252 void set(std::string const& name, const_view_type const& value);
253 protected:
254 void make_constant(Teuchos::any& result, double const& value) override;
255 void inspect_arg(Teuchos::any const& arg, bool& is_many, bool& is_bool) override;
256 void single_single_ternary_op(Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) override;
257 void single_many_ternary_op(Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) override;
258 void many_single_ternary_op(Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) override;
259 void many_many_ternary_op(Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) override;
260 void single_single_binary_op(BinaryOpCode code, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) override;
261 void single_many_binary_op(BinaryOpCode code, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) override;
262 void many_single_binary_op(BinaryOpCode code, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) override;
263 void many_many_binary_op(BinaryOpCode code, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) override;
264 void many_neg_op(Teuchos::any& result, Teuchos::any& right) override;
265 void single_neg_op(Teuchos::any& result, Teuchos::any& right) override;
266};
267
285template <typename DT, typename ... VP>
287
288}} // end namespace panzer::Expr
289
290#endif // PANZER_EXPR_EVAL_HPP
View
PHX::MDField< ScalarT, panzer::Cell, panzer::IP > result
A field that will be used to build up the result of the integral we're performing.
Base class for panzer::Expr::Eval, does everything that is independent of the Kokkos::View template p...
virtual void single_many_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right)=0
virtual void inspect_arg(Teuchos::any const &arg, bool &is_many, bool &is_bool)=0
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 at_shift(Teuchos::any &result, int token, std::string &text) override
Called at every parsed token in the math language.
void at_reduce(Teuchos::any &result, int prod, std::vector< Teuchos::any > &rhs) override
Called at every reduced production in the math language.
virtual void single_single_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right)=0
virtual void single_neg_op(Teuchos::any &result, Teuchos::any &right)=0
virtual void single_many_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right)=0
virtual void single_single_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right)=0
virtual void many_many_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right)=0
virtual void make_constant(Teuchos::any &result, double const &value)=0
void binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right)
Executes a binary operator.
virtual void many_single_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right)=0
virtual void many_many_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right)=0
T const & get(std::string const &name) const
Get the value of a variable in the symbol map.
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.
std::map< std::string, Teuchos::any > symbol_map
Stores all current symbols including variables and functions.
void ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right)
Executes the ternary operator, e.g. (a > b) ? a : b.
virtual void many_single_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right)=0
virtual void many_neg_op(Teuchos::any &result, Teuchos::any &right)=0
void set(std::string const &name, Function const &value)
Registers an EvalBase::Function, binding it to a name and making it callable.
Interprets mathematical expressions in a string and evaluates them using Kokkos::View objects as valu...
Kokkos::View< scalar_type, VP ... > single_view_type
One scalar (same for all evaluation points)
Kokkos::View< typename RebindDataType< view_data_type, bool const >::type, VP ... > const_bool_view_type
One boolean for each evaluation point, read-only.
typename original_view_type::non_const_value_type scalar_type
The scalar type.
Kokkos::View< bool const, VP ... > const_single_bool_view_type
One boolean (same for all evaluation points), read-only.
Kokkos::View< DT, VP ... > original_view_type
The corresponding Kokkos::View type, using the same template arguments are were given to Eval.
void many_many_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right) override
void single_single_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right) override
void many_neg_op(Teuchos::any &result, Teuchos::any &right) override
DT view_data_type
The data type, including dimension information.
void make_constant(Teuchos::any &result, double const &value) override
void single_single_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right) override
void single_many_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right) override
void many_many_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right) override
void set(std::string const &name, bool value)
Assign a boolean value to a variable symbol.
void inspect_arg(Teuchos::any const &arg, bool &is_many, bool &is_bool) override
void many_single_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right) override
Kokkos::View< typename RebindDataType< view_data_type, scalar_type const >::type, VP ... > const_view_type
One scalar for each evaluation point, read-only.
void single_neg_op(Teuchos::any &result, Teuchos::any &right) override
Kokkos::View< scalar_type const, VP ... > const_single_view_type
One scalar (same for all evaluation points), read-only.
void single_many_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right) override
Kokkos::View< bool, VP ... > single_bool_view_type
One boolean (same for all evaluation points)
void many_single_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right) override
void set_cmath_functions(Eval< DT, VP ... > &eval)
Add support for functions such as sqrt(), sin(), and cos()
BinaryOpCode
Denotes the native binary operators in the Teuchos::MathExpr language.
typename RebindDataType< NestedDataType, NewScalarType >::type * type
typename RebindDataType< NestedDataType, NewScalarType >::type[N] type
typename RebindDataType< NestedDataType, NewScalarType >::type[] type
Rebinds a Kokkos::View data type to use a new scalar type.
NewScalarType type
The new data type, suitable as the first template argument to Kokkos::View.
Kokkos::View< typename RebindDataType< DT, NewScalarType >::type, VP ... > type
The new Kokkos::View type, whose scalar type is now NewScalarType.
Builds on RebindDataType, but acts directly on a Kokkos::View type.