Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
LinearInterpolant.cpp
Go to the documentation of this file.
1/* This file is part of MoDELib, the Mechanics Of Defects Evolution Library.
2 *
3 *
4 * MoDELib is distributed without any warranty under the
5 * GNU General Public License (GPL) v2 <http://www.gnu.org/licenses/>.
6 */
7
8#ifndef model_LinearInterpolant_cpp_
9#define model_LinearInterpolant_cpp_
10
11#include <LinearInterpolant.h>
12#include <iterator>
13#include <iostream>
14namespace model
15{
16
18 /* init*/ InterpolantBase(data,extrap)
19 {
20
21 }
22
24/* init*/ InterpolantBase(data,extrap)
25{
26
27}
28
29 double LinearInterpolant::_f(const double& x) const
30 {
31 switch (this->size())
32 {
33 case 0:
34 {
35 throw std::runtime_error("LinearInterpolant::operator() data is empty.");
36 return 0.0;
37 break;
38 }
39
40 case 1:
41 {// only one point
42 return this->begin()->second;
43 break;
44 }
45
46 default:
47 {// two or more points
48
49 const auto iterLower(this->lower_bound(x)); // first element in the map whose key is >= x
50 const auto iterUpper(this->upper_bound(x)); // first element in the map whose key is > x
51
52 if(iterUpper==this->begin())
53 {// x to the left of the domain
54 switch (this->extrapolation.type)
55 {
56 case 1:
57 {// Constant Extrapolation
58 return this->begin()->second;
59 break;
60 }
61
62 case 2:
63 {// Periodic Extrapolation
64 InterpolantBase::MapType exMap(this->map());
65 exMap.emplace(this->map().rbegin()->first-this->extrapolation.period,this->map().rbegin()->second);
66 exMap.emplace(this->map(). begin()->first+this->extrapolation.period,this->map(). begin()->second);
67 return linearSegment(exMap.begin(),std::next(exMap.begin()),x);
68 break;
69 }
70
71 default:
72 {// Linear Extrapolation
73 return linearSegment(this->begin(),std::next(this->begin()),x);
74 break;
75 }
76 }
77 }
78 else if(iterUpper==this->end())
79 {// x on the right of domain
80 switch (this->extrapolation.type)
81 {
82 case 1:
83 {// Constant Extrapolation
84 return std::prev(this->end())->second;
85 break;
86 }
87
88 case 2:
89 {// Periodic Extrapolation
90 InterpolantBase::MapType exMap(this->map());
91 exMap.emplace(this->map().rbegin()->first-this->extrapolation.period,this->map().rbegin()->second);
92 exMap.emplace(this->map(). begin()->first+this->extrapolation.period,this->map(). begin()->second);
93 return linearSegment(std::prev(exMap.end(),2),std::prev(exMap.end(),1),x);
94 break;
95 }
96
97 default:
98 {// Linear Extrapolation
99 return linearSegment(std::prev(this->end(),2),std::prev(this->end(),1),x);
100 break;
101 }
102 }
103 }
104 else
105 {
106 if(iterLower==iterUpper) // x is in between breakpoints.
107 {// Because of assertion x<=xMax, iter1 cannot be dataMap.end()
108 // Moreover, iterUpper can never be dataMap.begin()
109 // Terefore it is safe to use element before iterLower
110 return linearSegment(std::prev(iterLower),iterLower,x);
111 }
112 else
113 {// x is on breakpoint iterLower. iterUpper may be dataMap.end().
114 return iterLower->second;
115 }
116 }
117 break;
118 }
119 }
120 }
121
122 double LinearInterpolant::linearSegment(const InterpolantBase::MapType::const_iterator& iter1,const InterpolantBase::MapType::const_iterator& iter2,const double& x)
123 {
124 return iter1->second+(iter2->second-iter1->second)/(iter2->first-iter1->first)*(x-iter1->first);
125 }
126
127} // close namespace model
128#endif
Eigen::Matrix< double, Eigen::Dynamic, 2 > MatrixType
InterpolantBase(const MapType &data, const ExtrapolationMethod &extrap)
std::map< double, double > MapType
ExtrapolationMethod extrapolation
LinearInterpolant(const InterpolantBase::MatrixType &data, const ExtrapolationMethod &extrap=ExtrapolationMethod())
x
Definition int_R.m:12
int end[2]
vertex first