DNDSR 0.1.0.dev1+gcd065ad
Distributed Numeric Data Structure for CFV
Loading...
Searching...
No Matches
ExprtkWrapper.cpp
Go to the documentation of this file.
1/// @file ExprtkWrapper.cpp
2/// @brief exprtk-backed implementations of @ref DNDS::ExprtkWrapperEvaluator "ExprtkWrapperEvaluator"'s Compile,
3/// Evaluate, and Clear methods. Kept out-of-line so that exprtk's heavy
4/// templates stay confined to one translation unit.
5
6#include "ExprtkWrapper.hpp"
7#include "ExprtkPCH.hpp"
8#include <exprtk.hpp>
9
10namespace DNDS
11{
12 using symbol_table_t = exprtk::symbol_table<real>;
13 using expression_t = exprtk::expression<real>;
14 using parser_t = exprtk::parser<real>;
15
16 void ExprtkWrapperEvaluator::Compile(const std::string &expr)
17 {
18 this->Clear();
19
20 auto *pst = new symbol_table_t;
21 symbol_table_t &st = *pst;
22 _ptr_st = static_cast<void *>(pst);
23
24 st.add_infinity();
25 st.add_pi();
26 // TODO: add thread safety?
27 static exprtk::rtl::io::println<real> println;
28 st.add_function("println", println);
29
30 for (auto &[k, v] : _vars)
31 st.add_variable(k, v);
32
33 for (auto &[k, v] : _varVecs)
34 st.add_vector(k, v.data(), v.size());
35
36 auto *pexp = new expression_t;
37 expression_t &exp = *pexp;
38 _ptr_exp = static_cast<void *>(pexp);
39
40 exp.register_symbol_table(st);
41
42 auto *pparser = new parser_t;
43 parser_t &parser = *pparser;
44 _ptr_parser = static_cast<void *>(pparser);
45
46 auto compile_ok = parser.compile(expr, exp);
47 std::string error_info = parser.error() + "\n";
48 for (size_t i = 0; i < parser.error_count(); i++)
49 {
50 auto err = parser.get_error(i);
51 error_info.append(
52 fmt::format("Error [{}], at [{}]\n", i, err.token.position) +
53 fmt::format("\tType: {}\n", exprtk::parser_error::to_str(err.mode)) +
54 fmt::format("\tMsg: {}\n", err.diagnostic));
55 }
57 compile_ok,
58 "exprtk compiling of === \n" +
59 expr +
60 "\n=== failed: " + error_info);
61 _compiled = true;
62 }
63
65 {
66 DNDS_assert(this->Compiled());
67 DNDS_assert(_ptr_exp);
68 return static_cast<expression_t *>(_ptr_exp)->value();
69 }
70
72 {
73 if (_ptr_parser)
74 {
75 delete static_cast<parser_t *>(_ptr_parser);
76 _ptr_parser = nullptr;
77 }
78 if (_ptr_exp)
79 {
80 delete static_cast<expression_t *>(_ptr_exp);
81 _ptr_exp = nullptr;
82 }
83 if (_ptr_st)
84 {
85 delete static_cast<symbol_table_t *>(_ptr_st);
86 _ptr_st = nullptr;
87 }
88 _compiled = false;
89 }
90}
#define DNDS_assert(expr)
Debug-only assertion (compiled out when DNDS_NDEBUG is defined). Prints the expression + file/line + ...
Definition Errors.hpp:108
#define DNDS_check_throw_info(expr, info)
Same as DNDS_check_throw but attaches a user-supplied info message to the thrown std::runtime_error.
Definition Errors.hpp:96
Precompiled-header include of the exprtk expression library. Used only by ExprtkWrapper....
Runtime mathematical expression compiler and evaluator wrapping exprtk.
real Evaluate()
Evaluate the compiled expression with the current variable values.
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.
void Clear()
Release the compiled expression and parser. Must be called before re-binding variables.
the host side operators are provided as implemented
exprtk::parser< real > parser_t
exprtk::symbol_table< real > symbol_table_t
exprtk::expression< real > expression_t
double real
Canonical floating-point scalar used throughout DNDSR (double precision).
Definition Defines.hpp:105
Eigen::Matrix< real, 5, 1 > v
real err