Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
incremental_function.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2007 - 2018 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 #include <deal.II/base/incremental_function.h>
17 #include <deal.II/base/signaling_nan.h>
18 
19 #include <deal.II/lac/vector.h>
20 
21 DEAL_II_NAMESPACE_OPEN
22 
23 namespace Functions
24 {
25  template <int dim, typename RangeNumberType>
28  : Function<dim, RangeNumberType>(base.n_components)
29  , base(base)
30  , delta_t(numbers::signaling_nan<time_type>())
31  , values_old(base.n_components)
32  {}
33 
34 
35 
36  template <int dim, typename RangeNumberType>
37  void
39  const time_type delta)
40  {
41  Assert(delta >= 0.0,
42  ExcMessage("The decrement must be set to a non-negative value."));
43  delta_t = delta;
44  }
45 
46 
47 
48  template <int dim, typename RangeNumberType>
49  RangeNumberType
51  const Point<dim> & p,
52  const unsigned int comp) const
53  {
54  // since we modify a mutable member variable, lock the
55  // the data via a mutex
56  std::lock_guard<std::mutex> lock(mutex);
57 
58  // Cache the time state of the base class in case it has been changed
59  // within the user code. We reset the wrapped function to the original
60  // state once we're done with our own evaluations.
61  const auto orig_time = base.get_time();
62 
63  base.set_time(this->get_time());
64  const RangeNumberType current = base.value(p, comp);
65 
66  base.set_time(this->get_time() - delta_t);
67  const RangeNumberType old = base.value(p, comp);
68 
69  // Reset wrapped function time setting
70  base.set_time(orig_time);
71 
72  return current - old;
73  }
74 
75 
76 
77  template <int dim, typename RangeNumberType>
78  void
80  const Point<dim> & p,
81  Vector<RangeNumberType> &values) const
82  {
83  // since we modify a mutable member variable, lock the
84  // the data via a mutex
85  std::lock_guard<std::mutex> lock(mutex);
86 
87  // Cache the time state of the base class in case it has been changed
88  // within the user code. We reset the wrapped function to the original
89  // state once we're done with our own evaluations.
90  const auto orig_time = base.get_time();
91 
92  base.set_time(this->get_time());
93  base.vector_value(p, values);
94 
95  base.set_time(this->get_time() - delta_t);
96  base.vector_value(p, values_old);
97 
98  values -= values_old;
99 
100  // Reset wrapped function time setting
101  base.set_time(orig_time);
102  }
103 
104 
105 // Explicit instantiations
106 #include "incremental_function.inst"
107 } // namespace Functions
108 DEAL_II_NAMESPACE_CLOSE
virtual void vector_value(const Point< dim > &p, Vector< RangeNumberType > &values) const override
static ::ExceptionBase & ExcMessage(std::string arg1)
typename FunctionTime< typename numbers::NumberTraits< RangeNumberType >::real_type >::time_type time_type
Definition: function.h:168
#define Assert(cond, exc)
Definition: exceptions.h:1407
void set_decrement(const time_type delta_t)
virtual RangeNumberType value(const Point< dim > &p, const unsigned int component=0) const override
IncrementalFunction(Function< dim, RangeNumberType > &base)