Octopus
spline.h
Go to the documentation of this file.
1
2// Copyright (c) 2013, Lawrence Livermore National Security, LLC.
3// qb@ll: Qbox at Lawrence Livermore
4//
5// This file is part of qb@ll.
6//
7// Produced at the Lawrence Livermore National Laboratory.
8// Written by Xavier Andrade (xavier@llnl.gov), Erik Draeger
9// (draeger1@llnl.gov) and Francois Gygi (fgygi@ucdavis.edu).
10// Based on the Qbox code by Francois Gygi Copyright (c) 2008
11// LLNL-CODE-635376. All rights reserved.
12//
13// qb@ll is free software: you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation, either version 3 of the License, or
16// (at your option) any later version.
17//
18// This program is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details, in the file COPYING in the
22// root directory of this distribution or <http://www.gnu.org/licenses/>.
23//
24
25#include <config.h>
26/*******************************************************************************
27 *
28 * spline.h
29 *
30 ******************************************************************************/
31#ifndef SPLINE_H
32#define SPLINE_H
33
34#include <vector>
35
36#define SPLINE_FLAT_BC 0.0 /* Flat boundary condition (y'=0) */
37#define SPLINE_NATURAL_BC 1.e31 /* Natural boundary condition (Y"=0) */
38
39void spline(const double *x, const double *y, int n, double yp1, double ypn,
40 double *y2);
41void splint(const double *xa, const double *ya, const double *y2a, int n,
42 double x, double *y);
43void splintd(const double *xa, const double *ya, const double *y2a, int n,
44 double x, double *y, double *dy);
45
46class Spline {
47
48public:
49 Spline() {}
50
51 void fit(const double *x, double *y, int n, double yp1, double ypn) {
52 x_.resize(n);
53 y_.resize(n);
54 y2_.resize(n);
55
56 for (int ii = 0; ii < n; ii++) {
57 x_[ii] = x[ii];
58 y_[ii] = y[ii];
59 }
60 spline(x, y, n, yp1, ypn, &y2_[0]);
61 }
62
63 double value(const double &x) const {
64 double y;
65 splint(&x_[0], &y_[0], &y2_[0], x_.size(), x, &y);
66 return y;
67 }
68
69 void derivative(const double &x, double &y, double &dy) const {
70 splintd(&x_[0], &y_[0], &y2_[0], x_.size(), x, &y, &dy);
71 }
72
73private:
74 std::vector<double> x_;
75 std::vector<double> y_;
76 std::vector<double> y2_;
77};
78
79#endif
80
81// Local Variables:
82// mode: c++
83// End:
real(real64), dimension(:), allocatable x_