atomicrex  0.1
An advanced atomistic model building tool
GridCubicSpline.h
1 //
3 // Copyright (C) 2017, Alexander Stukowski and Paul Erhart
4 //
5 // This file is part of atomicrex.
6 //
7 // Atomicrex is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // Atomicrex is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 //
21 
22 #pragma once
23 
24 #include "../../Atomicrex.h"
25 
26 namespace atomicrex {
27 
41 {
42 public:
43 
45  GridCubicSpline() : _N(0), _delta(0) {}
46 
48  void init(int N, double delta, double derivStart = 0, double derivEnd = 0) {
49  BOOST_ASSERT(N >= 2);
50  BOOST_ASSERT(delta > 0);
51  _N = N;
52  _derivStart = derivStart;
53  _derivEnd = derivEnd;
54  _delta = delta;
55  _xcutoff = delta * (N-1);
56  Y.resize(N);
57  Y2.resize(N);
58  }
59 
61  void setKnot(int i, double y) {
62  BOOST_ASSERT(i >=0 && i < _N);
63  Y[i] = y;
64  }
65 
67  int knotCount() const { return _N; }
68 
70  double derivStart() const { return _derivStart; }
71 
73  double derivEnd() const { return _derivEnd; }
74 
76  double knotX(int i) const { BOOST_ASSERT(i >=0 && i < _N); return _delta * i; }
77 
79  double knotY(int i) const { BOOST_ASSERT(i >=0 && i < _N); return Y[i]; }
80 
82  double knotY2(int i) const { BOOST_ASSERT(i >=0 && i < _N); return Y2[i]; }
83 
85  bool parse(std::istream& stream);
86 
88  void prepareSpline();
89 
91  double eval(double x) const;
92 
94  double eval(double x, double& deriv) const;
95 
97  double cutoff() const { return _xcutoff; }
98 
100  void writeGnuplot(const FPString& filename, const FPString& title) const;
101 
102 private:
103 
104  int _N; // Number of spline knots
105  double _delta; // Distance between spline knots.
106  double _xcutoff; // The X coordinate of the last knot.
107  std::vector<double> Y; // Function values at spline knots
108  std::vector<double> Y2; // Second derivatives at spline knots
109  double _derivStart; // First derivative at knot 0
110  double _derivEnd; // First derivative at knot (N-1)
111 };
112 
113 } // End of namespace
double derivEnd() const
Returns first derivative of spline function at the last knot.
Definition: GridCubicSpline.h:73
double knotY2(int i) const
Returns the second derivative at the i-th knot.
Definition: GridCubicSpline.h:82
double eval(double x) const
Evaluates the spline function at position x.
Definition: GridCubicSpline.cpp:58
Definition: GridCubicSpline.h:40
bool parse(std::istream &stream)
Parses the spline knots from a file.
Definition: GridCubicSpline.cpp:110
double derivStart() const
Returns first derivative of spline function at knot 0.
Definition: GridCubicSpline.h:70
GridCubicSpline()
Default constructor.
Definition: GridCubicSpline.h:45
double cutoff() const
Returns the cutoff radius of this spline function.
Definition: GridCubicSpline.h:97
void writeGnuplot(const FPString &filename, const FPString &title) const
Create a Gnuplot script that displays the spline function.
Definition: GridCubicSpline.cpp:127
This file collects the definition of classes that define various simple crystal structures.
Definition: Atomicrex.h:67
double knotY(int i) const
Returns the Y coordinate of the i-th knot.
Definition: GridCubicSpline.h:79
std::string FPString
The default string type used throughout the code:
Definition: Atomicrex.h:70
double knotX(int i) const
Returns the X coordinate of the i-th knot.
Definition: GridCubicSpline.h:76
int knotCount() const
Returns the number of knots.
Definition: GridCubicSpline.h:67
void init(int N, double delta, double derivStart=0, double derivEnd=0)
Initialization of the spline function.
Definition: GridCubicSpline.h:48
void setKnot(int i, double y)
Sets the Y value of the function at a knot.
Definition: GridCubicSpline.h:61
void prepareSpline()
Calculates the second derivatives of the cubic spline.
Definition: GridCubicSpline.cpp:32