File: function_cspline.cc

package info (click to toggle)
deal.ii 9.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 181,876 kB
  • sloc: cpp: 265,739; ansic: 52,054; python: 1,507; perl: 645; sh: 506; xml: 437; makefile: 73
file content (154 lines) | stat: -rw-r--r-- 4,621 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// ---------------------------------------------------------------------
//
// Copyright (C) 2016 - 2017 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE at
// the top level of the deal.II distribution.
//
// ---------------------------------------------------------------------

#include <deal.II/base/point.h>
#include <deal.II/base/function_cspline.h>

#ifdef DEAL_II_WITH_GSL
#include <cmath>
#include <algorithm>

DEAL_II_NAMESPACE_OPEN
namespace Functions
{

  template <int dim>
  CSpline<dim>::CSpline(const std::vector<double> &x_,
                        const std::vector<double> &y_)
    :
    interpolation_points(x_),
    interpolation_values(y_)
  {
    Assert (interpolation_points.size() > 0,
            ExcCSplineEmpty(interpolation_points.size()));

    Assert (interpolation_points.size() == interpolation_values.size(),
            ExcCSplineSizeMismatch(interpolation_points.size(),interpolation_values.size()));

    // check that input vector @p interpolation_points is provided in ascending order:
    for (unsigned int i = 0; i < interpolation_points.size()-1; i++)
      AssertThrow(interpolation_points[i]<interpolation_points[i+1],
                  ExcCSplineOrder(i,interpolation_points[i],interpolation_points[i+1]));

    acc = gsl_interp_accel_alloc ();
    const unsigned int n = interpolation_points.size();
    cspline = gsl_spline_alloc (gsl_interp_cspline, n);
    // gsl_spline_init returns something but it seems nobody knows what
    gsl_spline_init (cspline, interpolation_points.data(), interpolation_values.data(), n);
  }



  template <int dim>
  CSpline<dim>::~CSpline()
  {
    gsl_interp_accel_free (acc);
    gsl_spline_free (cspline);
    acc = nullptr;
    cspline = nullptr;
  }



  template <int dim>
  double
  CSpline<dim>::value (const Point<dim>   &p,
                       const unsigned int) const
  {
    // GSL functions may modify gsl_interp_accel *acc object (last argument).
    // This can only work in multithreaded applications if we lock the data
    // structures via a mutex.
    Threads::Mutex::ScopedLock lock (acc_mutex);

    const double &x = p[0];
    Assert (x >= interpolation_points.front() && x <= interpolation_points.back(),
            ExcCSplineRange(x,interpolation_points.front(),interpolation_points.back()));

    return gsl_spline_eval (cspline, x, acc);
  }



  template <int dim>
  Tensor<1,dim>
  CSpline<dim>::gradient (const Point<dim>   &p,
                          const unsigned int) const
  {
    // GSL functions may modify gsl_interp_accel *acc object (last argument).
    // This can only work in multithreaded applications if we lock the data
    // structures via a mutex.
    Threads::Mutex::ScopedLock lock (acc_mutex);

    const double &x = p[0];
    Assert (x >= interpolation_points.front() && x <= interpolation_points.back(),
            ExcCSplineRange(x,interpolation_points.front(),interpolation_points.back()));

    const double deriv = gsl_spline_eval_deriv (cspline, x, acc);
    Tensor<1,dim> res;
    res[0] = deriv;
    return res;
  }



  template <int dim>
  double
  CSpline<dim>::laplacian (const Point<dim>   &p,
                           const unsigned int) const
  {
    // GSL functions may modify gsl_interp_accel *acc object (last argument).
    // This can only work in multithreaded applications if we lock the data
    // structures via a mutex.
    Threads::Mutex::ScopedLock lock (acc_mutex);

    const double &x = p[0];
    Assert (x >= interpolation_points.front() && x <= interpolation_points.back(),
            ExcCSplineRange(x,interpolation_points.front(),interpolation_points.back()));

    return gsl_spline_eval_deriv2 (cspline, x, acc);
  }



  template <int dim>
  SymmetricTensor<2,dim>
  CSpline<dim>::hessian (const Point<dim>   &p,
                         const unsigned int) const
  {
    SymmetricTensor<2,dim> res;
    res[0][0] = laplacian(p);
    return res;
  }



  template <int dim>
  std::size_t
  CSpline<dim>::memory_consumption () const
  {
    // only simple data elements, so
    // use sizeof operator
    return sizeof (*this) + 2*sizeof(double)*interpolation_values.size();
  }


// explicit instantiations
  template class CSpline<1>;

}

DEAL_II_NAMESPACE_CLOSE

#endif