Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
function_parser.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2005 - 2019 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_function_parser_h
17 #define dealii_function_parser_h
18 
19 
20 #include <deal.II/base/config.h>
21 
22 #include <deal.II/base/auto_derivative_function.h>
23 #include <deal.II/base/exceptions.h>
24 #include <deal.II/base/point.h>
25 #include <deal.II/base/tensor.h>
26 #include <deal.II/base/thread_local_storage.h>
27 
28 #include <map>
29 #include <memory>
30 #include <vector>
31 
32 namespace mu
33 {
34  class Parser;
35 }
36 
37 DEAL_II_NAMESPACE_OPEN
38 
39 
40 template <typename>
41 class Vector;
42 
43 
219 template <int dim>
221 {
222 public:
232  FunctionParser(const unsigned int n_components = 1,
233  const double initial_time = 0.0,
234  const double h = 1e-8);
235 
242  FunctionParser(const std::string &expression,
243  const std::string &constants = "",
244  const std::string &variable_names = default_variable_names() +
245  ",t",
246  const double h = 1e-8);
247 
252  ~FunctionParser() override;
253 
257  using ConstMap = std::map<std::string, double>;
258 
262  using ConstMapIterator = ConstMap::iterator;
263 
298  void
299  initialize(const std::string & vars,
300  const std::vector<std::string> &expressions,
301  const ConstMap & constants,
302  const bool time_dependent = false);
303 
311  void
312  initialize(const std::string &vars,
313  const std::string &expression,
314  const ConstMap & constants,
315  const bool time_dependent = false);
316 
322  static std::string
324 
331  virtual double
332  value(const Point<dim> &p, const unsigned int component = 0) const override;
333 
341  virtual void
342  vector_value(const Point<dim> &p, Vector<double> &values) const override;
343 
348  const std::vector<std::string> &
349  get_expressions() const;
350 
356  int,
357  std::string,
358  << "Parsing Error at Column " << arg1
359  << ". The parser said: " << arg2);
360 
362  int,
363  int,
364  << "The number of components (" << arg1
365  << ") is not equal to the number of expressions (" << arg2
366  << ").");
367 
369 
370 private:
371 #ifdef DEAL_II_WITH_MUPARSER
372 
376 
383  fp;
384 
389  std::map<std::string, double> constants;
390 
395  std::vector<std::string> var_names;
396 
403  void
404  init_muparser() const;
405 #endif
406 
411  std::vector<std::string> expressions;
412 
418 
426  unsigned int n_vars;
427 };
428 
429 
430 template <int dim>
431 std::string
433 {
434  switch (dim)
435  {
436  case 1:
437  return "x";
438  case 2:
439  return "x,y";
440  case 3:
441  return "x,y,z";
442  default:
443  Assert(false, ExcNotImplemented());
444  }
445  return "";
446 }
447 
448 
449 
450 DEAL_II_NAMESPACE_CLOSE
451 
452 #endif
FunctionParser(const unsigned int n_components=1, const double initial_time=0.0, const double h=1e-8)
#define DeclException2(Exception2, type1, type2, outsequence)
Definition: exceptions.h:541
const unsigned int n_components
Definition: function.h:162
A class that provides a separate storage location on each thread that accesses the object...
unsigned int n_vars
static ::ExceptionBase & ExcParseError(int arg1, std::string arg2)
LinearAlgebra::distributed::Vector< Number > Vector
static std::string default_variable_names()
virtual void vector_value(const Point< dim > &p, Vector< double > &values) const override
Threads::ThreadLocalStorage< std::vector< std::unique_ptr< mu::Parser > > > fp
#define Assert(cond, exc)
Definition: exceptions.h:1407
Threads::ThreadLocalStorage< std::vector< double > > vars
void init_muparser() const
static ::ExceptionBase & ExcInvalidExpressionSize(int arg1, int arg2)
std::vector< std::string > var_names
ConstMap::iterator ConstMapIterator
const std::vector< std::string > & get_expressions() const
void initialize(const std::string &vars, const std::vector< std::string > &expressions, const ConstMap &constants, const bool time_dependent=false)
std::map< std::string, double > ConstMap
static ::ExceptionBase & ExcNotImplemented()
virtual double value(const Point< dim > &p, const unsigned int component=0) const override
std::vector< std::string > expressions
~FunctionParser() override
std::map< std::string, double > constants