DNDSR 0.1.0.dev1+gcd065ad
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 /// @brief Register a scalar variable. `init` is accepted for API
49 /// symmetry but currently ignored (scalars default to 0).
50 /// @note Calling any `Add*` invalidates a previously compiled expression.
51 void AddScalar(const std::string &name, real init = 0)
52 {
53 Clear();
54 _vars[name] = 0;
55 }
56
57 /// @brief Register a dense vector variable named `name` of length `size`.
58 void AddVector(const std::string &name, int size)
59 {
60 Clear();
61 _varVecs[name].resize(size);
62 }
63
64 /// @brief Mutable reference to a scalar variable's current value.
65 real &Var(const std::string &name) { return _vars.at(name); }
66 /// @brief Mutable reference to element `i` of a vector variable.
67 real &VarVec(const std::string &name, int i) { return _varVecs.at(name)(i); }
68 /// @brief Length of a registered vector variable.
69 index VarVecSize(const std::string &name) { return _varVecs.at(name).size(); }
70
71 /// @brief Whether @ref Compile has been called and the expression parsed successfully.
72 [[nodiscard]] bool Compiled() const
73 {
74 return _compiled;
75 }
76
77 /// @brief Compile `expr`. Throws (via @ref DNDS_check_throw) on parse error.
78 void Compile(const std::string &expr);
79
80 /// @brief Evaluate the compiled expression with the current variable values.
81 real Evaluate();
82
83 /// @brief Release the compiled expression and parser. Must be called
84 /// before re-binding variables.
85 void Clear();
86
88 };
89}
Core type aliases, constants, and metaprogramming utilities for the DNDS framework.
Thin RAII wrapper over the exprtk expression library.
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 ...
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.
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:107
double real
Canonical floating-point scalar used throughout DNDSR (double precision).
Definition Defines.hpp:105