Reference documentation for deal.II version 8.5.1
function_cspline.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2016 - 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 at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #include <deal.II/base/point.h>
17 #include <deal.II/base/function_cspline.h>
18 
19 #ifdef DEAL_II_WITH_GSL
20 #include <cmath>
21 #include <algorithm>
22 
23 DEAL_II_NAMESPACE_OPEN
24 namespace Functions
25 {
26 
27  template <int dim>
28  CSpline<dim>::CSpline(const std::vector<double> &x_,
29  const std::vector<double> &y_)
30  :
31  interpolation_points(x_),
32  interpolation_values(y_)
33  {
34  Assert (interpolation_points.size() > 0,
36 
39 
40  // check that input vector @p interpolation_points is provided in ascending order:
41  for (unsigned int i = 0; i < interpolation_points.size()-1; i++)
44 
45  acc = gsl_interp_accel_alloc ();
46  const unsigned int n = interpolation_points.size();
47  cspline = gsl_spline_alloc (gsl_interp_cspline, n);
48  // gsl_spline_init returns something but it seems nobody knows what
49  gsl_spline_init (cspline, &interpolation_points[0], &interpolation_values[0], n);
50  }
51 
52 
53 
54  template <int dim>
56  {
57  gsl_interp_accel_free (acc);
58  gsl_spline_free (cspline);
59  acc = NULL;
60  cspline = NULL;
61  }
62 
63 
64 
65  template <int dim>
66  double
68  const unsigned int) const
69  {
70  // GSL functions may modify gsl_interp_accel *acc object (last argument).
71  // This can only work in multithreaded applications if we lock the data
72  // structures via a mutex.
73  Threads::Mutex::ScopedLock lock (acc_mutex);
74 
75  const double &x = p[0];
76  Assert (x >= interpolation_points.front() && x <= interpolation_points.back(),
77  ExcCSplineRange(x,interpolation_points.front(),interpolation_points.back()));
78 
79  return gsl_spline_eval (cspline, x, acc);
80  }
81 
82 
83 
84  template <int dim>
87  const unsigned int) const
88  {
89  // GSL functions may modify gsl_interp_accel *acc object (last argument).
90  // This can only work in multithreaded applications if we lock the data
91  // structures via a mutex.
92  Threads::Mutex::ScopedLock lock (acc_mutex);
93 
94  const double &x = p[0];
95  Assert (x >= interpolation_points.front() && x <= interpolation_points.back(),
96  ExcCSplineRange(x,interpolation_points.front(),interpolation_points.back()));
97 
98  const double deriv = gsl_spline_eval_deriv (cspline, x, acc);
99  Tensor<1,dim> res;
100  res[0] = deriv;
101  return res;
102  }
103 
104 
105 
106  template <int dim>
107  double
109  const unsigned int) const
110  {
111  // GSL functions may modify gsl_interp_accel *acc object (last argument).
112  // This can only work in multithreaded applications if we lock the data
113  // structures via a mutex.
114  Threads::Mutex::ScopedLock lock (acc_mutex);
115 
116  const double &x = p[0];
117  Assert (x >= interpolation_points.front() && x <= interpolation_points.back(),
118  ExcCSplineRange(x,interpolation_points.front(),interpolation_points.back()));
119 
120  return gsl_spline_eval_deriv2 (cspline, x, acc);
121  }
122 
123 
124 
125  template <int dim>
128  const unsigned int) const
129  {
130  Tensor<2,dim> res;
131  res[0][0] = laplacian(p);
132  return res;
133  }
134 
135 
136 
137  template <int dim>
138  std::size_t
140  {
141  // only simple data elements, so
142  // use sizeof operator
143  return sizeof (*this) + 2*sizeof(double)*interpolation_values.size();
144  }
145 
146 
147 // explicit instantiations
148  template class CSpline<1>;
149 
150 }
151 
152 DEAL_II_NAMESPACE_CLOSE
153 
154 #endif
virtual SymmetricTensor< 2, dim > hessian(const Point< dim > &p, const unsigned int component=0) const
virtual double laplacian(const Point< dim > &p, const unsigned int component=0) const
#define AssertThrow(cond, exc)
Definition: exceptions.h:369
virtual Tensor< 1, dim > gradient(const Point< dim > &p, const unsigned int component=0) const
static ::ExceptionBase & ExcCSplineEmpty(int arg1)
#define Assert(cond, exc)
Definition: exceptions.h:313
virtual double value(const Point< dim > &point, const unsigned int component=0) const
const std::vector< double > interpolation_points
static ::ExceptionBase & ExcCSplineOrder(int arg1, double arg2, double arg3)
gsl_interp_accel * acc
const std::vector< double > interpolation_values
static ::ExceptionBase & ExcCSplineRange(double arg1, double arg2, double arg3)
static ::ExceptionBase & ExcCSplineSizeMismatch(int arg1, int arg2)
CSpline(const std::vector< double > &interpolation_points, const std::vector< double > &interpolation_values)