Reference documentation for deal.II version 9.1.0-pre
function_lib_cutoff.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2001 - 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 #include <deal.II/base/function_lib.h>
17 #include <deal.II/base/point.h>
18 #include <deal.II/base/tensor.h>
19 
20 #include <deal.II/lac/vector.h>
21 
22 #include <cmath>
23 
24 DEAL_II_NAMESPACE_OPEN
25 
26 
27 namespace Functions
28 {
29  template <int dim>
31  const Point<dim> p,
32  const unsigned int n_components,
33  const unsigned int select)
34  : Function<dim>(n_components)
35  , center(p)
36  , radius(r)
37  , selected(select)
38  {}
39 
40 
41  template <int dim>
42  void
44  {
45  center = p;
46  }
47 
48 
49  template <int dim>
50  void
52  {
53  radius = r;
54  }
55 
57 
58  template <int dim>
60  const double r,
61  const Point<dim> p,
62  const unsigned int n_components,
63  const unsigned int select)
64  : CutOffFunctionBase<dim>(r, p, n_components, select)
65  {}
66 
67 
68  template <int dim>
69  double
71  const unsigned int component) const
72  {
74  component == this->selected)
75  return ((this->center.distance(p) < this->radius) ? 1. : 0.);
76  return 0.;
77  }
78 
79 
80  template <int dim>
81  void
83  std::vector<double> & values,
84  const unsigned int component) const
85  {
86  Assert(values.size() == points.size(),
87  ExcDimensionMismatch(values.size(), points.size()));
88  Assert(component < this->n_components,
89  ExcIndexRange(component, 0, this->n_components));
90 
91 
93  component == this->selected)
94  for (unsigned int k = 0; k < values.size(); ++k)
95  values[k] = (this->center.distance(points[k]) < this->radius) ? 1. : 0.;
96  else
97  std::fill(values.begin(), values.end(), 0.);
98  }
99 
100 
101  template <int dim>
102  void
104  const std::vector<Point<dim>> &points,
105  std::vector<Vector<double>> & values) const
106  {
107  Assert(values.size() == points.size(),
108  ExcDimensionMismatch(values.size(), points.size()));
109 
110  for (unsigned int k = 0; k < values.size(); ++k)
111  {
112  const double val =
113  (this->center.distance(points[k]) < this->radius) ? 1. : 0.;
115  values[k] = val;
116  else
117  {
118  values[k] = 0;
119  values[k](this->selected) = val;
120  }
121  }
122  }
123 
124 
125  template <int dim>
127  const Point<dim> p,
128  const unsigned int n_components,
129  const unsigned int select)
130  : CutOffFunctionBase<dim>(r, p, n_components, select)
131  {}
132 
133 
134  template <int dim>
135  double
137  const unsigned int component) const
138  {
140  component == this->selected)
141  {
142  const double d = this->center.distance(p);
143  return ((d < this->radius) ? (this->radius - d) : 0.);
144  }
145  return 0.;
146  }
147 
148 
149  template <int dim>
150  void
151  CutOffFunctionW1<dim>::value_list(const std::vector<Point<dim>> &points,
152  std::vector<double> & values,
153  const unsigned int component) const
154  {
155  Assert(values.size() == points.size(),
156  ExcDimensionMismatch(values.size(), points.size()));
157 
159  component == this->selected)
160  for (unsigned int i = 0; i < values.size(); ++i)
161  {
162  const double d = this->center.distance(points[i]);
163  values[i] = ((d < this->radius) ? (this->radius - d) : 0.);
164  }
165  else
166  std::fill(values.begin(), values.end(), 0.);
167  }
168 
169 
170 
171  template <int dim>
172  void
174  const std::vector<Point<dim>> &points,
175  std::vector<Vector<double>> & values) const
176  {
177  Assert(values.size() == points.size(),
178  ExcDimensionMismatch(values.size(), points.size()));
179 
180  for (unsigned int k = 0; k < values.size(); ++k)
181  {
182  const double d = this->center.distance(points[k]);
183  const double val = (d < this->radius) ? (this->radius - d) : 0.;
185  values[k] = val;
186  else
187  {
188  values[k] = 0;
189  values[k](this->selected) = val;
190  }
191  }
192  }
193 
194 
195  template <int dim>
197  const double r,
198  const Point<dim> p,
199  const unsigned int n_components,
200  const unsigned int select)
201  : CutOffFunctionBase<dim>(r, p, n_components, select)
202  {}
203 
204 
205  template <int dim>
206  double
208  const unsigned int component) const
209  {
211  component == this->selected)
212  {
213  const double d = this->center.distance(p);
214  const double r = this->radius;
215  if (d >= r)
216  return 0.;
217  const double e = -r * r / (r * r - d * d);
218  return ((e < -50) ? 0. : numbers::E * exp(e));
219  }
220  return 0.;
221  }
222 
223 
224  template <int dim>
225  void
227  std::vector<double> & values,
228  const unsigned int component) const
229  {
230  Assert(values.size() == points.size(),
231  ExcDimensionMismatch(values.size(), points.size()));
232 
233  const double r = this->radius;
234 
236  component == this->selected)
237  for (unsigned int i = 0; i < values.size(); ++i)
238  {
239  const double d = this->center.distance(points[i]);
240  if (d >= r)
241  {
242  values[i] = 0.;
243  }
244  else
245  {
246  const double e = -r * r / (r * r - d * d);
247  values[i] = (e < -50) ? 0. : numbers::E * exp(e);
248  }
249  }
250  else
251  std::fill(values.begin(), values.end(), 0.);
252  }
253 
254 
255  template <int dim>
256  void
258  const std::vector<Point<dim>> &points,
259  std::vector<Vector<double>> & values) const
260  {
261  Assert(values.size() == points.size(),
262  ExcDimensionMismatch(values.size(), points.size()));
263 
264  for (unsigned int k = 0; k < values.size(); ++k)
265  {
266  const double d = this->center.distance(points[k]);
267  const double r = this->radius;
268  double val = 0.;
269  if (d < this->radius)
270  {
271  const double e = -r * r / (r * r - d * d);
272  if (e > -50)
273  val = numbers::E * exp(e);
274  }
275 
277  values[k] = val;
278  else
279  {
280  values[k] = 0;
281  values[k](this->selected) = val;
282  }
283  }
284  }
285 
286 
287 
288  template <int dim>
291  const unsigned int) const
292  {
293  const double d = this->center.distance(p);
294  const double r = this->radius;
295  if (d >= r)
296  return Tensor<1, dim>();
297  const double e = -d * d / (r - d) / (r + d);
298  return ((e < -50) ?
299  Point<dim>() :
300  (p - this->center) / d *
301  (-2.0 * r * r / pow(-r * r + d * d, 2.0) * d * exp(e)));
302  }
303 
304 
305  // explicit instantiations
306  template class CutOffFunctionLinfty<1>;
307  template class CutOffFunctionLinfty<2>;
308  template class CutOffFunctionLinfty<3>;
309 
310  template class CutOffFunctionW1<1>;
311  template class CutOffFunctionW1<2>;
312  template class CutOffFunctionW1<3>;
313 
314  template class CutOffFunctionCinfty<1>;
315  template class CutOffFunctionCinfty<2>;
316  template class CutOffFunctionCinfty<3>;
317 } // namespace Functions
318 
319 DEAL_II_NAMESPACE_CLOSE
virtual void value_list(const std::vector< Point< dim >> &points, std::vector< double > &values, const unsigned int component=0) const override
virtual void value_list(const std::vector< Point< dim >> &points, std::vector< double > &values, const unsigned int component=0) const override
const unsigned int n_components
Definition: function.h:160
virtual Tensor< 1, dim > gradient(const Point< dim > &p, const unsigned int component=0) const override
CutOffFunctionLinfty(const double radius=1., const Point< dim >=Point< dim >(), const unsigned int n_components=1, const unsigned int select=CutOffFunctionBase< dim >::no_component)
numbers::NumberTraits< Number >::real_type distance(const Point< dim, Number > &p) const
static::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
virtual double value(const Point< dim > &p, const unsigned int component=0) const override
void new_center(const Point< dim > &p)
#define Assert(cond, exc)
Definition: exceptions.h:1227
static::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static const double E
Definition: numbers.h:118
virtual void vector_value_list(const std::vector< Point< dim >> &points, std::vector< Vector< double >> &values) const override
virtual double value(const Point< dim > &p, const unsigned int component=0) const override
CutOffFunctionBase(const double radius=1., const Point< dim >=Point< dim >(), const unsigned int n_components=1, const unsigned int select=CutOffFunctionBase< dim >::no_component)
virtual void vector_value_list(const std::vector< Point< dim >> &points, std::vector< Vector< double >> &values) const override
const unsigned int selected
Definition: function_lib.h:967
virtual void value_list(const std::vector< Point< dim >> &points, std::vector< double > &values, const unsigned int component=0) const override
virtual double value(const Point< dim > &p, const unsigned int component=0) const override
virtual void vector_value_list(const std::vector< Point< dim >> &points, std::vector< Vector< double >> &values) const override
CutOffFunctionCinfty(const double radius=1., const Point< dim >=Point< dim >(), const unsigned int n_components=1, const unsigned int select=CutOffFunctionBase< dim >::no_component)
CutOffFunctionW1(const double radius=1., const Point< dim >=Point< dim >(), const unsigned int n_components=1, const unsigned int select=CutOffFunctionBase< dim >::no_component)