Mallard 0.8.0
Unstructured, GPU accelerated, high-order finite volume CFD solver
Mallard website
Loading...
Searching...
No Matches
expression.h
Go to the documentation of this file.
1
12#ifndef EXPRESSION_H
13#define EXPRESSION_H
14
15#include <algorithm>
16#include <memory>
17#include <stdexcept>
18#include <string>
19#include <vector>
20
21#include <exprtk.hpp>
22
27 public:
28 Expression(const std::string & name, const std::string & text) :
29 vars(std::make_unique<Vars>()) {
30 vars->table.add_variable("x", vars->x);
31 vars->table.add_variable("y", vars->y);
32 vars->table.add_variable("z", vars->z);
33 vars->table.add_variable("t", vars->t);
34 vars->table.add_constants();
35 vars->expr.register_symbol_table(vars->table);
36 exprtk::parser<double> parser;
37 if (!parser.compile(text, vars->expr)) {
38 throw std::runtime_error("Failed to parse expression for " + name + ": " + parser.error());
39 }
40 std::vector<std::string> used;
41 time_dependent = !exprtk::collect_variables(text, vars->table, used) ||
42 std::find(used.begin(), used.end(), "t") != used.end();
43 }
44
46 bool depends_on_time() const { return time_dependent; }
47
51 double operator()(double x, double y, double t = 0.0) const {
52 return (*this)(x, y, 0.0, t);
53 }
54
58 double operator()(double x, double y, double z, double t) const {
59 vars->x = x;
60 vars->y = y;
61 vars->z = z;
62 vars->t = t;
63 return vars->expr.value();
64 }
65
69 template <typename T>
70 double at(const T & x, const int n_dim, double t = 0.0) const {
71 return (*this)(double(x(0)), double(x(1)), n_dim > 2 ? double(x(2)) : 0.0, t);
72 }
73
74 private:
75 // Heap-allocated so the symbol table's variable addresses survive moves
76 struct Vars {
77 double x = 0.0, y = 0.0, z = 0.0, t = 0.0;
78 exprtk::symbol_table<double> table;
79 exprtk::expression<double> expr;
80 };
81 std::unique_ptr<Vars> vars;
82 bool time_dependent = true;
83};
84
85#endif // EXPRESSION_H
An exprtk expression in the variables x, y, z and t.
Definition expression.h:26
bool depends_on_time() const
Whether the value can depend on t.
Definition expression.h:46
double at(const T &x, const int n_dim, double t=0.0) const
Evaluate at the point with coordinates x[0..n_dim) (z = 0 if n_dim = 2).
Definition expression.h:70
Expression(const std::string &name, const std::string &text)
Definition expression.h:28
double operator()(double x, double y, double z, double t) const
Evaluate at (x, y, z, t).
Definition expression.h:58
double operator()(double x, double y, double t=0.0) const
Evaluate at (x, y, z = 0, t).
Definition expression.h:51
KOKKOS_INLINE_FUNCTION rtype dot(const rtype *a, const rtype *b)
Compute the dot product of two arrays.
Definition common_math.h:32