Reference documentation for deal.II version 9.1.0-pre
function_parser.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2005 - 2017 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 
197 template <int dim>
199 {
200 public:
210  FunctionParser(const unsigned int n_components = 1,
211  const double initial_time = 0.0,
212  const double h = 1e-8);
213 
220  FunctionParser(const std::string &expression,
221  const std::string &constants = "",
222  const std::string &variable_names = default_variable_names() +
223  ",t",
224  const double h = 1e-8);
225 
230  ~FunctionParser() override;
231 
235  using ConstMap = std::map<std::string, double>;
236 
240  using ConstMapIterator = ConstMap::iterator;
241 
276  void
277  initialize(const std::string & vars,
278  const std::vector<std::string> &expressions,
279  const ConstMap & constants,
280  const bool time_dependent = false);
281 
289  void
290  initialize(const std::string &vars,
291  const std::string &expression,
292  const ConstMap & constants,
293  const bool time_dependent = false);
294 
300  static std::string
301  default_variable_names();
302 
309  virtual double
310  value(const Point<dim> &p, const unsigned int component = 0) const override;
311 
318  virtual void
319  vector_value(const Point<dim> &p, Vector<double> &values) const override;
320 
325  DeclException2(ExcParseError,
326  int,
327  std::string,
328  << "Parsing Error at Column " << arg1
329  << ". The parser said: " << arg2);
330 
331  DeclException2(ExcInvalidExpressionSize,
332  int,
333  int,
334  << "The number of components (" << arg1
335  << ") is not equal to the number of expressions (" << arg2
336  << ").");
337 
339 
340 private:
341 #ifdef DEAL_II_WITH_MUPARSER
342 
346 
352 # if TBB_VERSION_MAJOR >= 4
354  fp;
355 # else
356  // older TBBs have a bug in which they want to return thread-local
357  // objects by value. this doesn't work for std::unique_ptr, so use a
358  // std::shared_ptr
360  fp;
361 # endif
362 
367  std::map<std::string, double> constants;
368 
373  std::vector<std::string> var_names;
374 
379  std::vector<std::string> expressions;
380 
387  void
388  init_muparser() const;
389 #endif
390 
396 
404  unsigned int n_vars;
405 };
406 
407 
408 template <int dim>
409 std::string
411 {
412  switch (dim)
413  {
414  case 1:
415  return "x";
416  case 2:
417  return "x,y";
418  case 3:
419  return "x,y,z";
420  default:
421  Assert(false, ExcNotImplemented());
422  }
423  return "";
424 }
425 
426 
427 
428 DEAL_II_NAMESPACE_CLOSE
429 
430 #endif
Threads::ThreadLocalStorage< std::vector< std::shared_ptr< mu::Parser > > > fp
#define DeclException2(Exception2, type1, type2, outsequence)
Definition: exceptions.h:420
A class that provides a separate storage location on each thread that accesses the object...
unsigned int n_vars
static std::string default_variable_names()
#define Assert(cond, exc)
Definition: exceptions.h:1227
Threads::ThreadLocalStorage< std::vector< double > > vars
std::vector< std::string > var_names
ConstMap::iterator ConstMapIterator
std::map< std::string, double > ConstMap
static::ExceptionBase & ExcNotImplemented()
std::vector< std::string > expressions
std::map< std::string, double > constants