16 #include <deal.II/base/parsed_function.h>
17 #include <deal.II/base/utilities.h>
21 DEAL_II_NAMESPACE_OPEN
29 function_object(n_components)
37 const unsigned int n_components)
58 "The name of the variables as they will be used in the "
59 "function, separated by commas. By default, the names of variables "
60 "at which the function will be evaluated is `x' (in 1d), `x,y' (in 2d) or "
61 "`x,y,z' (in 3d) for spatial coordinates and `t' for time. You can then "
62 "use these variable names in your function expression and they will be "
63 "replaced by the values of these variables at which the function is "
64 "currently evaluated. However, you can also choose a different set "
65 "of names for the independent variables at which to evaluate your function "
66 "expression. For example, if you work in spherical coordinates, you may "
67 "wish to set this input parameter to `r,phi,theta,t' and then use these "
68 "variable names in your function expression.");
71 std::string expr =
"0";
72 for (
unsigned int i=1; i<n_components; ++i)
76 "The formula that denotes the function you want to evaluate for "
77 "particular values of the independent variables. This expression "
78 "may contain any of the usual operations such as addition or "
79 "multiplication, as well as all of the common functions such as "
80 "`sin' or `cos'. In addition, it may contain expressions like "
81 "`if(x>0, 1, -1)' where the expression evaluates to the second "
82 "argument if the first argument is true, and to the third argument "
83 "otherwise. For a full overview of possible expressions accepted "
84 "see the documentation of the muparser library at http://muparser.beltoforion.de/."
86 "If the function you are describing represents a vector-valued "
87 "function with multiple components, then separate the expressions "
88 "for individual components by a semicolon.");
90 "Sometimes it is convenient to use symbolic constants in the "
91 "expression that describes the function, rather than having to "
92 "use its numeric value everywhere the constant appears. These "
93 "values can be defined using this parameter, in the form "
94 "`var1=value1, var2=value2, ...'."
96 "A typical example would be to set this runtime parameter to "
97 "`pi=3.1415926536' and then use `pi' in the expression of the "
98 "actual formula. (That said, for convenience this class actually "
99 "defines both `pi' and `Pi' by default, but you get the idea.)");
107 std::string vnames = prm.
get(
"Variable names");
108 std::string expression = prm.
get(
"Function expression");
109 std::string constants_list = prm.
get(
"Function constants");
111 std::vector<std::string> const_list =
113 std::map<std::string, double> constants;
114 for (
unsigned int i = 0; i < const_list.size(); ++i)
116 std::vector<std::string> this_c =
120 AssertThrow( std::sscanf(this_c[1].c_str(),
"%lf", &tmp),
122 constants[this_c[0]] = tmp;
135 function_object.initialize(vnames, expression, constants);
139 function_object.initialize(vnames, expression, constants,
true);
143 ExcMessage(
"The list of variables specified is <" + vnames
144 +
"> which is a list of length "
146 +
" but it has to be a list of length equal to"
147 +
" either dim (for a time-independent function)"
148 +
" or dim+1 (for a time-dependent function)."));
158 function_object.vector_value(p, values);
165 unsigned int comp)
const
167 return function_object.value(p, comp);
175 function_object.set_time(newtime);
185 DEAL_II_NAMESPACE_CLOSE
#define AssertThrow(cond, exc)
std::string get(const std::string &entry_string) const
static void declare_parameters(ParameterHandler &prm, const unsigned int n_components=1)
virtual void vector_value(const Point< dim > &p, Vector< double > &values) const
static::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
void parse_parameters(ParameterHandler &prm)
virtual double value(const Point< dim > &p, const unsigned int component=0) const
std::string int_to_string(const unsigned int value, const unsigned int digits=numbers::invalid_unsigned_int)
virtual void set_time(const Number new_time)
ParsedFunction(const unsigned int n_components=1, const double h=1e-8)
std::vector< std::string > split_string_list(const std::string &s, const char delimiter= ',')
void declare_entry(const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation=std::string())
static::ExceptionBase & ExcNotImplemented()
static::ExceptionBase & ExcZero()
virtual void set_time(const double newtime)