DNDSR 0.2.1
Distributed Numeric Data Structure for CFV
Loading...
Searching...
No Matches
ExprtkWrapper.hpp
Go to the documentation of this file.
1#pragma once
2/// @file ExprtkWrapper.hpp
3/// @brief Runtime mathematical expression compiler and evaluator wrapping exprtk.
4
5#include <map>
6#include "Defines.hpp"
7
8namespace DNDS
9{
10 /// @brief Scalar name -> value map fed into an @ref DNDS::ExprtkWrapperEvaluator "ExprtkWrapperEvaluator".
11 using tExprVars = std::map<std::string, real>;
12 /// @brief Vector name -> Eigen dense vector map fed into an @ref DNDS::ExprtkWrapperEvaluator "ExprtkWrapperEvaluator".
13 using tExprVarVecs = std::map<std::string, Eigen::Vector<real, Eigen::Dynamic>>;
14
15 /**
16 * @brief Thin RAII wrapper over the exprtk expression library.
17 *
18 * @details Compiles a user-supplied math expression once (with named
19 * scalar and vector variables) and evaluates it many times against
20 * variable values set through @ref Var / @ref VarVec. Used for user-defined
21 * initial conditions and boundary functions read from config JSON
22 * (see `docs/guides/array_usage.md` and CFV config files).
23 *
24 * Typical usage:
25 * ```cpp
26 * ExprtkWrapperEvaluator e;
27 * e.AddScalar("t");
28 * e.AddVector("x", 3);
29 * e.Compile("sin(x[0]) * cos(t)");
30 * e.Var("t") = 0.5;
31 * e.VarVec("x", 0) = 0.1;
32 * real val = e.Evaluate();
33 * ```
34 *
35 * The type-erased `void*` members hide the exprtk symbol_table / expression
36 * / parser types so exprtk headers stay out of the public API.
37 */
39 {
40 void *_ptr_st = nullptr;
41 void *_ptr_exp = nullptr;
42 void *_ptr_parser = nullptr;
43 tExprVars _vars;
44 tExprVarVecs _varVecs;
45 bool _compiled = false;
46
47 public:
48 // Rule-of-five closure. Holds raw `new`/`delete`-owned Exprtk
49 // parser/symbol-table/expression pointers (opaque forward-declared
50 // types); copy / move are deleted because they would alias the
51 // same `new`-ed objects and double-delete on destruction.
57
58 /// @brief Register a scalar variable. `init` is accepted for API
59 /// symmetry but currently ignored (scalars default to 0).
60 /// @note Calling any `Add*` invalidates a previously compiled expression.
61 void AddScalar(const std::string &name, real init = 0)
62 {
63 Clear();
64 _vars[name] = 0;
65 }
66
67 /// @brief Register a dense vector variable named `name` of length `size`.
68 void AddVector(const std::string &name, int size)
69 {
70 Clear();
71 _varVecs[name].resize(size);
72 }
73
74 /// @brief Mutable reference to a scalar variable's current value.
75 real &Var(const std::string &name) { return _vars.at(name); }
76 /// @brief Mutable reference to element `i` of a vector variable.
77 real &VarVec(const std::string &name, int i) { return _varVecs.at(name)(i); }
78 /// @brief Length of a registered vector variable.
79 index VarVecSize(const std::string &name) { return _varVecs.at(name).size(); }
80
81 /// @brief Whether @ref Compile has been called and the expression parsed successfully.
82 [[nodiscard]] bool Compiled() const
83 {
84 return _compiled;
85 }
86
87 /// @brief Compile `expr`. Throws (via @ref DNDS_check_throw) on parse error.
88 void Compile(const std::string &expr);
89
90 /// @brief Evaluate the compiled expression with the current variable values.
91 real Evaluate();
92
93 /// @brief Release the compiled expression and parser. Must be called
94 /// before re-binding variables.
95 void Clear();
96
98 };
99}
Core type aliases, constants, and metaprogramming utilities for the DNDS framework.
Thin RAII wrapper over the exprtk expression library.
ExprtkWrapperEvaluator & operator=(const ExprtkWrapperEvaluator &)=delete
real Evaluate()
Evaluate the compiled expression with the current variable values.
void AddScalar(const std::string &name, real init=0)
Register a scalar variable. init is accepted for API symmetry but currently ignored (scalars default ...
ExprtkWrapperEvaluator(const ExprtkWrapperEvaluator &)=delete
ExprtkWrapperEvaluator(ExprtkWrapperEvaluator &&)=delete
void AddVector(const std::string &name, int size)
Register a dense vector variable named name of length size.
real & Var(const std::string &name)
Mutable reference to a scalar variable's current value.
ExprtkWrapperEvaluator & operator=(ExprtkWrapperEvaluator &&)=delete
index VarVecSize(const std::string &name)
Length of a registered vector variable.
void Compile(const std::string &expr)
Compile expr. Throws (via DNDS_check_throw) on parse error.
bool Compiled() const
Whether Compile has been called and the expression parsed successfully.
real & VarVec(const std::string &name, int i)
Mutable reference to element i of a vector variable.
void Clear()
Release the compiled expression and parser. Must be called before re-binding variables.
the host side operators are provided as implemented
std::map< std::string, Eigen::Vector< real, Eigen::Dynamic > > tExprVarVecs
Vector name -> Eigen dense vector map fed into an ExprtkWrapperEvaluator.
std::map< std::string, real > tExprVars
Scalar name -> value map fed into an ExprtkWrapperEvaluator.
int64_t index
Global row / DOF index type (signed 64-bit; handles multi-billion-cell meshes).
Definition Defines.hpp:112
double real
Canonical floating-point scalar used throughout DNDSR (double precision).
Definition Defines.hpp:110