SLIDE  3.0.0
A simulator for lithium-ion battery pack degradation
Loading...
Searching...
No Matches
interpolation.hpp
Go to the documentation of this file.
1/*
2 * interpolation.hpp
3 *
4 * Groups functions to do linear interpolation
5 *
6 * Copyright (c) 2019, The Chancellor, Masters and Scholars of the University
7 * of Oxford, VITO nv, and the 'Slide' Developers.
8 * See the licence file LICENCE.txt for more information.
9 */
10
11#pragma once
12
13#include "../settings/settings.hpp"
14
15#include <vector>
16#include <array>
17#include <iostream>
18#include <cassert>
19#include <algorithm>
20#include <utility>
21#include <cmath>
22#include <cstdlib>
23
24namespace slide {
25template <typename Tx, typename Ty>
26auto linInt_noexcept(bool bound, Tx &xdat, Ty &ydat, int nin, double x, bool is_fixed = false)
27{
28 /*
29 * function for linear interpolation with the data points provided as two arrays
30 *
31 * IN
32 * bound boolean deciding what to do if the value of x is out of range of xdat
33 * if true, an error is thrown
34 * if false, the value closest to x is returned
35 * xdat x data points in strictly increasing order
36 * ydat y data points
37 * nin number of data points
38 * x x point at which value is needed
39 * is_fixed If the difference between values are fixed.
40 *
41 * OUT
42 * y y value corresponding to x
43 * status 0 if successful, 1 if x>first and -1 if x < last
44 */
45
46 double yy{ 0.0 };
47 int status = 0;
49
50 if (bound) {
51 if (x < xdat[0]) {
52 status = -1;
53 return std::make_pair(yy, status);
54 } else if (x > xdat[nin - 1]) {
55 status = 1;
56 return std::make_pair(yy, status);
57 }
58 }
59
60 if (x <= xdat[0])
61 yy = ydat[0];
62 else if (x >= xdat[nin - 1])
63 yy = ydat[nin - 1];
64 else {
66 int i_low{ 0 };
68
69 if (is_fixed) {
70 double dt = xdat[1] - xdat[0];
71 i_low = static_cast<int>((x - xdat[0]) / dt) + 1;
72 } else {
76 const auto it = std::lower_bound(xdat.begin(), xdat.begin() + nin, x);
77 i_low = static_cast<int>(it - xdat.begin());
78 }
79
80 const double xr = xdat[i_low];
81 const double yr = ydat[i_low];
82 const double xl = xdat[i_low - 1];
83 const double yl = ydat[i_low - 1];
84 yy = yl + (yr - yl) * (x - xl) / (xr - xl);
85 }
86
87 return std::make_pair(yy, status);
88}
89
90template <typename Tx, typename Ty>
91double linInt(bool verbose, bool bound, Tx &xdat, Ty &ydat, int nin, double x, bool is_fixed = false)
92{
93 /*
94 * function for linear interpolation with the data points provided as two arrays
95 *
96 * IN
97 * verbose if false, no error message is written (but the error is still thrown)
98 * if true, an error message is written
99 * bound boolean deciding what to do if the value of x is out of range of xdat
100 * if true, an error is thrown
101 * if false, the value closest to x is returned
102 * xdat x data points in strictly increasing order
103 * ydat y data points
104 * nin number of data points
105 * x x point at which value is needed
106 * is_fixed If the difference between values are fixed.
107 *
108 * OUT
109 * y y value corresponding to x
110 *
111 * THROWS
112 * 1 if bound = true && if x is out of bounds, i.e. smaller than the smallest value of xdat or larger than the largest value of xdat
113 * i.e. bound AND (x < xdat [0] OR x > xdat[end])
114 */
115
116 auto [yy, status] = linInt_noexcept(bound, xdat, ydat, nin, x, is_fixed);
117
118 if (status) {
119 if (verbose)
120 std::cerr << "ERROR in Interpolation::linInt: x is out of bounds. x = " << x << " while xmin = " << xdat[0] << " and xmax is " << xdat[nin - 1] << ".\n";
121 throw 1;
122 }
123
124 return yy;
125}
126
127} // namespace slide
Slide namespace contains all the types, classes, and functions for the simulation framework.
Definition: Cell.hpp:27
double linInt(bool verbose, bool bound, Tx &xdat, Ty &ydat, int nin, double x, bool is_fixed=false)
Definition: interpolation.hpp:91
auto linInt_noexcept(bool bound, Tx &xdat, Ty &ydat, int nin, double x, bool is_fixed=false)
Definition: interpolation.hpp:26