Debian Codegraph @

include/deal.II/base/mu_parser_internal.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2019 - 2025 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_mu_parser_internal_h
16 #define dealii_mu_parser_internal_h
17
18 // This file contains functions used internally by the FunctionParser
19 // and the TensorFunctionParser class.
20
21 #include <deal.II/base/config.h>
22
23 #include <deal.II/base/array_view.h>
24 #include <deal.II/base/exceptions.h>
25 #include <deal.II/base/point.h>
26 #include <deal.II/base/thread_local_storage.h>
27
28 #include <memory>
29 #include <string>
30 #include <vector>
31
32 DEAL_II_NAMESPACE_OPEN
33
34 namespace internal
35 {
36 namespace FunctionParser
37 {
38 int
39 mu_round(double val);
40
41 double
42 mu_if(double condition, double thenvalue, double elsevalue);
43
44 double
45 mu_or(double left, double right);
46
47 double
48 mu_and(double left, double right);
49
50 double
51 mu_int(double value);
52
53 double
54 mu_ceil(double value);
55
56 double
57 mu_floor(double value);
58
59 double
60 mu_cot(double value);
61
62 double
63 mu_csc(double value);
64
65 double
66 mu_sec(double value);
67
68 double
69 mu_log(double value);
70
71 double
72 mu_pow(double a, double b);
73
74 double
75 mu_erf(double value);
76
77 double
78 mu_erfc(double value);
79
80 // returns a random value in the range [0,1] initializing the generator
81 // with the given seed
82 double
83 mu_rand_seed(double seed);
84
85 // returns a random value in the range [0,1]
86 double
87 mu_rand();
88
89 /**
90 * Get the array of all function names.
91 */
92 std::vector<std::string>
93 get_function_names();
94
95 /**
96 * @addtogroup Exceptions
97 * @{
98 */
99 DeclException2(ExcParseError,
100 int,
101 std::string,
102 << "Parsing Error at Column " << arg1
103 << ". The parser said: " << arg2);
104
105 /** @} */
106
107 /**
108 * deal.II uses muParser as a purely internal dependency. To this end, we do
109 * not include any muParser headers in our own headers (and the bundled
110 * version of the dependency does not install its headers or compile a
111 * separate muparser library). Hence, to interface with muParser, we use the
112 * PIMPL idiom here to wrap a pointer to mu::Parser objects.
113 */
114 class muParserBase
115 {
116 public:
117 virtual ~muParserBase() = default;
118 };
119
120 /**
121 * Class containing the mutable state required by muParser.
122 *
123 * @note For performance reasons it is best to put all mutable state in a
124 * single object so that, for each function call, we only need to get
125 * thread-local data exactly once.
126 */
127 struct ParserData
128 {
129 /**
130 * Default constructor. Threads::ThreadLocalStorage requires that objects
131 * be either default- or copy-constructible: make sure we satisfy the
132 * first case by declaring it here.
133 */
134 ParserData() = default;
135
136 /**
137 * std::is_copy_constructible gives the wrong answer for containers with
138 * non-copy constructible types (e.g., std::vector<std::unique_ptr<int>>)
139 * - for more information, see the documentation of
140 * Threads::ThreadLocalStorage. Hence, to avoid compilation failures, just
141 * delete the copy constructor completely.
142 */
143 ParserData(const ParserData &) = delete;
144
145 /**
146 * Scratch array used to set independent variables (i.e., x, y, and t)
147 * before each muParser call.
148 */
149 std::vector<double> vars;
150
151 /**
152 * The actual muParser parser objects (hidden with PIMPL).
153 */
154 std::vector<std::unique_ptr<muParserBase>> parsers;
155 };
156
157 template <int dim, typename Number>
158 class ParserImplementation
159 {
160 public:
161 ParserImplementation();
162
163 virtual ~ParserImplementation() = default;
164
165 /**
166 * Initialize the internal state of the object. This is the same as the
167 * inheriting class method - see FunctionParser::initialize() for more
168 * information.
169 */
170 virtual void
171 initialize(const std::string &vars,
172 const std::vector<std::string> &expressions,
173 const std::map<std::string, double> &constants,
174 const bool time_dependent = false);
175
176 /**
177 * Set up the internal muParser objects to parse and evaluate mathematical
178 * expressions.
179 */
180 void
181 init_muparser() const;
182
183 /**
184 * Compute the value of a single component.
185 */
186 Number
187 do_value(const Point<dim> &p,
188 const double time,
189 unsigned int component) const;
190
191 /**
192 * Compute the values of all components.
193 */
194 void
195 do_all_values(const Point<dim> &p,
196 const double time,
197 ArrayView<Number> &values) const;
198
199 /**
200 * An array of function expressions (one per component), required to
201 * initialize tfp in each thread.
202 */
203 std::vector<std::string> expressions;
204
205 private:
206 /**
207 * The muParser objects (hidden with the PIMPL idiom) for each thread (and
208 * one for each component).
209 */
210 mutable Threads::ThreadLocalStorage<internal::FunctionParser::ParserData>
211 parser_data;
212
213 /**
214 * An array to keep track of all the constants, required to initialize fp
215 * in each thread.
216 */
217 std::map<std::string, double> constants;
218
219 /**
220 * An array for the variable names, required to initialize fp in each
221 * thread.
222 */
223 std::vector<std::string> var_names;
224
225 /**
226 * State of usability. This variable is checked every time the function is
227 * called for evaluation. It's set to true in the initialize() methods.
228 */
229 bool initialized;
230
231 /**
232 * Number of variables. If this is also a function of time, then the
233 * number of variables is dim+1, otherwise it is dim. In the case that
234 * this is a time dependent function, the time is supposed to be the last
235 * variable. If #n_vars is not identical to the number of the variables
236 * parsed by the initialize() method, then an exception is thrown.
237 */
238 unsigned int n_vars;
239 };
240 } // namespace FunctionParser
241 } // namespace internal
242
243 DEAL_II_NAMESPACE_CLOSE
244
245 #endif