Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
expression_parser.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2026 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Matteo Caldana <matteo.caldana@polimi.it>
22// Author: Pedro Peixoto <ppeixoto@usp.br>
23
24#ifndef VULPES_UTILS_EXPRESSION_PARSER_HPP_
25#define VULPES_UTILS_EXPRESSION_PARSER_HPP_
26
27#include <mathpresso/mathpresso.h>
28
29#include <stdexcept>
30#include <string>
31#include <utility>
32
33namespace vulpes {
34namespace vutils {
35
45template <size_t N>
47 public:
57 template <typename... Args>
58 explicit ExpressionParser(const char* expression, Args&&... args) {
59 init(expression, args...);
60 }
61
70 template <typename... Args>
71 explicit ExpressionParser(const std::string& expression, Args&&... args) {
72 init(expression.c_str(), args...);
73 }
74
81 double evaluate(const double* data) const {
82 return m_expr.evaluate(const_cast<double*>(data));
83 }
84
85 private:
93 template <typename... Args>
94 void init(const char* expression, Args&&... args) {
95 static_assert(
96 sizeof...(Args) == N, "Incorrect number of variable names provided");
97 m_context.addBuiltIns();
98 std::array<std::string_view, N> variable_names{std::forward<Args>(args)...};
99 for (size_t i = 0; i < N; ++i) {
100 m_context.addVariable(variable_names[i].data(), i * sizeof(double));
101 }
102
103 const auto options = mathpresso::kNoOptions;
104 auto err = m_expr.compile(m_context, expression, options);
105 if (err != mathpresso::kErrorOk) {
106 throw std::runtime_error(
107 "Failed to compile expression: '" + std::string(expression) +
108 "'. Error: " + std::to_string(err));
109 }
110 }
111
112 mathpresso::Context m_context;
113 mathpresso::Expression m_expr;
114};
115
116} // namespace vutils
117} // namespace vulpes
118
119#endif // VULPES_UTILS_EXPRESSION_PARSER_HPP_
double evaluate(const double *data) const
Evaluate the compiled expression against a data buffer.
Definition expression_parser.hpp:81
ExpressionParser(const char *expression, Args &&... args)
Compile an expression given as a C string.
Definition expression_parser.hpp:58
mathpresso::Expression m_expr
Definition expression_parser.hpp:113
void init(const char *expression, Args &&... args)
Register variables and compile the expression.
Definition expression_parser.hpp:94
ExpressionParser(const std::string &expression, Args &&... args)
Compile an expression given as a std::string.
Definition expression_parser.hpp:71
mathpresso::Context m_context
Definition expression_parser.hpp:112
Definition checks.cpp:31
Definition cell_manager.hpp:38