Reference documentation for deal.II version 9.2.0
\(\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\}}\)
symbolic_function.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2019 - 2020 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_symbolic_function_h
17 #define dealii_symbolic_function_h
18 
19 #include <deal.II/base/config.h>
20 
22 #include <deal.II/base/function.h>
23 #include <deal.II/base/point.h>
26 #include <deal.II/base/tensor.h>
27 
29 
30 #include <functional>
31 #include <iostream>
32 #include <vector>
33 
35 
36 // Forward declarations
37 #ifndef DOXYGEN
38 template <typename number>
39 class Vector;
40 namespace Functions
41 {
42  template <int dim, typename RangeNumberType>
43  class SymbolicFunction;
44 }
45 #endif
46 
47 namespace Functions
48 {
49 #ifdef DEAL_II_WITH_SYMENGINE
50 
153  template <int dim, typename RangeNumberType = double>
154  class SymbolicFunction : public Function<dim, RangeNumberType>
155  {
156  public:
200  const std::vector<Differentiation::SD::Expression> &function,
206  &user_substitution_map = {});
207 
220  SymbolicFunction(const std::string &expressions);
221 
229  void
231  const Differentiation::SD::types::substitution_map &substitutions);
232 
248  void
251 
262 
268  get_coordinate_symbols() const;
269 
274  get_time_symbol() const;
275 
279  const std::vector<Differentiation::SD::Expression> &
281 
287 
294  time_derivative() const;
295 
296  // documentation inherited from the base class
297  virtual RangeNumberType
298  value(const Point<dim> &p, const unsigned int component = 0) const override;
299 
300  // documentation inherited from the base class
302  gradient(const Point<dim> & p,
303  const unsigned int component = 0) const override;
304 
305  // documentation inherited from the base class
306  virtual RangeNumberType
307  laplacian(const Point<dim> & p,
308  const unsigned int component = 0) const override;
309 
310  // documentation inherited from the base class
312  hessian(const Point<dim> & p,
313  const unsigned int component = 0) const override;
314 
319  template <typename StreamType>
320  StreamType &
321  print(StreamType &out) const;
322 
323  private:
332 
338  void
339  update_values() const;
340 
346  void
347  update_first_derivatives() const;
348 
354  void
356 
369  const std::vector<Differentiation::SD::Expression> user_function;
370 
378 
385 
390  mutable std::vector<Differentiation::SD::Expression> function;
391 
397  mutable std::vector<Tensor<1, dim, Differentiation::SD::Expression>>
399 
405  mutable std::vector<Tensor<2, dim, Differentiation::SD::Expression>>
407 
413  mutable std::vector<Differentiation::SD::Expression> function_laplacian;
414 
419 
424  };
425 
429  template <int dim, typename RangeNumberType>
430  inline std::ostream &
431  operator<<(std::ostream &out, const SymbolicFunction<dim, RangeNumberType> &f)
432  {
433  return f.print(out);
434  }
435 
436 
437 
438  // Inline and template functions
439  template <int dim, typename RangeNumberType>
440  template <typename StreamType>
441  StreamType &
443  {
444  for (unsigned int i = 0; i < dim; ++i)
445  out << coordinate_symbols[i] << ", ";
446  for (const auto &argument_pair : additional_function_arguments)
447  out << argument_pair.first << ", ";
448  out << time_symbol << " -> " << user_function[0];
449  for (unsigned int i = 1; i < user_function.size(); ++i)
450  out << "; " << user_function[i];
451  if (!user_substitution_map.empty())
452  {
453  out << " # ( ";
454  std::string sep = "";
455  for (const auto &substitution : user_substitution_map)
456  {
457  out << sep << substitution.first << " = " << substitution.second;
458  sep = ", ";
459  }
460  out << " )";
461  }
462  return out;
463  }
464 #else
465  template <int dim, typename RangeNumberType = double>
466  class SymbolicFunction
467  {
468  public:
470  {
471  AssertThrow(
472  false,
473  ExcMessage(
474  "This class is not available if you did not enable SymEngine "
475  "when compiling deal.II."));
476  }
477  };
478 #endif
479 } // namespace Functions
480 
482 
483 #endif
void set_additional_function_arguments(const Differentiation::SD::types::substitution_map &arguments)
const std::vector< Differentiation::SD::Expression > user_function
std::vector< Tensor< 1, dim, Differentiation::SD::Expression > > function_gradient
const Tensor< 1, dim, Differentiation::SD::Expression > & get_coordinate_symbols() const
Tensor< 1, dim, Differentiation::SD::Expression > coordinate_symbols
Differentiation::SD::Expression time_symbol
virtual RangeNumberType laplacian(const Point< dim > &p, const unsigned int component=0) const override
#define AssertThrow(cond, exc)
Definition: exceptions.h:1531
std::vector< Tensor< 2, dim, Differentiation::SD::Expression > > function_hessian
std::ostream & print(std::ostream &stream) const
virtual RangeNumberType value(const Point< dim > &p, const unsigned int component=0) const override
Differentiation::SD::types::substitution_map create_evaluation_substitution_map(const Point< dim > &point) const
static Tensor< 1, dim, Differentiation::SD::Expression > get_default_coordinate_symbols()
const Differentiation::SD::types::substitution_map & get_user_substitution_map() const
static ::ExceptionBase & ExcMessage(std::string arg1)
const Differentiation::SD::Expression & get_time_symbol() const
SymbolicFunction< dim, RangeNumberType > time_derivative() const
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
SymbolicFunction(const std::vector< Differentiation::SD::Expression > &function, const Tensor< 1, dim, Differentiation::SD::Expression > &coordinate_symbols=get_default_coordinate_symbols(), const Differentiation::SD::Expression &time_symbol=Differentiation::SD::make_symbol("t"), const Differentiation::SD::types::substitution_map &user_substitution_map={})
Differentiation::SD::types::substitution_map additional_function_arguments
Point< spacedim > point(const gp_Pnt &p, const double tolerance=1e-10)
Definition: utilities.cc:188
std::vector< Differentiation::SD::Expression > function_laplacian
Expression make_symbol(const std::string &symbol)
const std::vector< Differentiation::SD::Expression > & get_symbolic_function_expressions() const
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
StreamType & print(StreamType &out) const
void update_user_substitution_map(const Differentiation::SD::types::substitution_map &substitutions)
virtual SymmetricTensor< 2, dim, RangeNumberType > hessian(const Point< dim > &p, const unsigned int component=0) const override
Differentiation::SD::types::substitution_map user_substitution_map
void update_first_derivatives() const
std::map< SD::Expression, SD::Expression, internal::ExpressionKeyLess > substitution_map
void update_second_derivatives() const
virtual Tensor< 1, dim, RangeNumberType > gradient(const Point< dim > &p, const unsigned int component=0) const override