deal.II version GIT relicensing-6834-g5b78e6bcdf 2026-10-01 11:20:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
parsed_function.cc
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2007 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#include <deal.II/base/config.h>
14
24
25#include <Kokkos_Macros.hpp>
26
27#include <algorithm>
28#include <map>
29#include <string>
30#include <vector>
31
33
34namespace Functions
35{
36 template <int dim>
37 ParsedFunction<dim>::ParsedFunction(const unsigned int n_components,
38 const double h)
39 : AutoDerivativeFunction<dim>(h, n_components)
40 , function_object(n_components)
41 {}
42
43
44
45 template <int dim>
46 void
48 const unsigned int n_components,
49 const std::string &input_expr)
50 {
51 Assert(n_components > 0, ExcZero());
52
53 std::string vnames;
54 switch (dim)
55 {
56 case 1:
57 vnames = "x,t";
58 break;
59 case 2:
60 vnames = "x,y,t";
61 break;
62 case 3:
63 vnames = "x,y,z,t";
64 break;
65 default:
67 break;
68 }
69 prm.declare_entry(
70 "Variable names",
71 vnames,
73 "Names of the independent variables, separated by commas.");
74
75 // The expression of the function
76 // If the string is an empty string, 0 is set for each components.
77 std::string expr = input_expr;
78 if (expr == "")
79 {
80 expr = "0";
81 for (unsigned int i = 1; i < n_components; ++i)
82 expr += "; 0";
83 }
84 else
85 {
86 // If the user specified an input expr, the number of component
87 // specified need to match n_components.
88 AssertDimension((std::count(expr.begin(), expr.end(), ';') + 1),
89 n_components);
90 }
91
92
93 prm.declare_entry(
94 "Function expression",
95 expr,
97 "Semicolon-separated formulas for the function components. Supports standard "
98 "operations, functions `sin`, `cos`, etc., and conditionals `if(x>0, 1, -1)`.");
99 prm.declare_entry(
100 "Function constants",
101 "",
103 "Symbolic constants for the function expression, in the form "
104 "'var1=value1, var2=value2, ...'. Note: 'pi', 'Pi', 'PI', and 'E' are predefined.");
105 }
106
107
108
109 template <int dim>
110 void
112 {
113 std::string vnames = prm.get("Variable names");
114 std::string expression = prm.get("Function expression");
115 std::string constants_list = prm.get("Function constants");
116
117 std::vector<std::string> const_list =
118 Utilities::split_string_list(constants_list, ',');
119 std::map<std::string, double> constants;
120
121 // set pi, Pi, and PI as synonyms for the corresponding value, and set the
122 // Nepero number E.
123 constants["pi"] = numbers::PI;
124 constants["Pi"] = numbers::PI;
125 constants["PI"] = numbers::PI;
126 constants["E"] = numbers::E;
127
128 for (const auto &constant : const_list)
129 {
130 std::vector<std::string> this_c =
131 Utilities::split_string_list(constant, '=');
132 AssertThrow(this_c.size() == 2,
133 ExcMessage("The list of constants, <" + constants_list +
134 ">, is not a comma-separated list of "
135 "entries of the form 'name=value'."));
136 constants[this_c[0]] = Utilities::string_to_double(this_c[1]);
137 }
138
139 const unsigned int nn = (Utilities::split_string_list(vnames)).size();
140 switch (nn)
141 {
142 case dim:
143 // Time independent function
144 function_object.initialize(vnames, expression, constants);
145 break;
146 case dim + 1:
147 // Time dependent function
148 function_object.initialize(vnames, expression, constants, true);
149 break;
150 default:
151 AssertThrow(false,
153 "The list of variables specified is <" + vnames +
154 "> which is a list of length " +
156 " but it has to be a list of length equal to" +
157 " either dim (for a time-independent function)" +
158 " or dim+1 (for a time-dependent function)."));
159 }
160 }
161
162
163
164 template <int dim>
165 void
167 Vector<double> &values) const
168 {
169 function_object.vector_value(p, values);
170 }
171
172
173
174 template <int dim>
175 double
176 ParsedFunction<dim>::value(const Point<dim> &p, unsigned int comp) const
177 {
178 return function_object.value(p, comp);
179 }
180
181
182
183 template <int dim>
184 void
185 ParsedFunction<dim>::set_time(const double newtime)
186 {
187 function_object.set_time(newtime);
189 }
190
191
192 // Explicit instantiations
193 template class ParsedFunction<1>;
194 template class ParsedFunction<2>;
195 template class ParsedFunction<3>;
196} // namespace Functions
virtual void set_time(const Number new_time)
virtual void vector_value(const Point< dim > &p, Vector< double > &values) const override
ParsedFunction(const unsigned int n_components=1, const double h=1e-8)
virtual double value(const Point< dim > &p, const unsigned int component=0) const override
static void declare_parameters(ParameterHandler &prm, const unsigned int n_components=1, const std::string &input_expr="")
virtual void set_time(const double newtime) override
void parse_parameters(ParameterHandler &prm)
void declare_entry(const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation="", const bool has_to_be_set=false)
std::string get(const std::string &entry_string) const
Definition point.h:111
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
static ::ExceptionBase & ExcZero()
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
std::size_t size
Definition mpi.cc:733
std::vector< std::string > split_string_list(const std::string &s, const std::string &delimiter=",")
Definition utilities.cc:695
std::string int_to_string(const unsigned int value, const unsigned int digits=numbers::invalid_unsigned_int)
Definition utilities.cc:464
double string_to_double(const std::string &s)
Definition utilities.cc:646
constexpr double E
Definition numbers.h:215
constexpr double PI
Definition numbers.h:240