SLIDE  3.0.0
A simulator for lithium-ion battery pack degradation
Loading...
Searching...
No Matches
FixedData.hpp
Go to the documentation of this file.
1/*
2 * FixedData.hpp
3 *
4 * Created on: 07 Feb 2022
5 * Author(s): Jorn Reniers, Volkan Kumtepeli
6 */
7
8#pragma once
9
10#include <stdexcept>
11#include <vector>
12#include <array>
13#include <functional>
14#include <cmath>
15#include <filesystem>
16
17namespace slide {
18template <typename T, bool extrapolation = true>
19class FixedData;
20
21template <typename T, bool extrapolation = true>
23{
24public:
25 using iterator_category = std::input_iterator_tag;
26 using difference_type = int;
27 using value_type = T;
30
31 FixedDataIter(FixedData<T, extrapolation> *const f_data_ptr_, int n_) : f_data_ptr{ f_data_ptr_ }, n{ n_ } {}
32
34 {
35 n++;
36 return *this;
37 }
38
40 {
41 FixedDataIter temp = *this;
42 ++(*this);
43 return temp;
44 }
45
47 {
48 n--;
49 return *this;
50 }
51
53 {
54 FixedDataIter temp = *this;
55 --(*this);
56 return temp;
57 }
58
60 {
61 return f_data_ptr->operator[](i);
62 }
63
64 const value_type operator*() const { return f_data_ptr->operator[](n); }
65
66 friend bool operator==(const FixedDataIter &a, const FixedDataIter &b) { return a.n == b.n; }
67 friend bool operator!=(const FixedDataIter &a, const FixedDataIter &b) { return a.n != b.n; }
68
69 friend FixedDataIter operator+(const FixedDataIter &a, const int b)
70 {
71 return FixedDataIter{ a.f_data_ptr, a.n + b };
72 }
73
75 {
76 return a.n - b.n;
77 }
78
79private:
81 int n;
82};
83
84template <typename T, bool extrapolation = true>
86{
87 return a - b;
88}
89
90template <typename T, bool extrapolation>
92{
93 using size_type = int;
94 T x_min{};
95 T dx{};
96 size_type n{};
97 std::function<T(T, T, int)> F = [](T x_min_, T dx_, size_type i) { return x_min_ + i * dx_; }; // #TODO important problem here!
98
99public:
100 FixedData() = default;
101 FixedData(T x_min_, T dx_, int n_) : x_min(x_min_), dx(dx_), n(n_) {}
102 FixedData(T x_min_, T dx_, int n_, std::function<T(T, T, int)> F_) : x_min(x_min_), dx(dx_), n(n_), F(F_) {}
103
104 T operator[](int i) const
105 {
106
107 if constexpr (!extrapolation)
108 {
109 if (i >= n || i < 0) {
110 std::cout << i << ' ' << n << '\n';
111 throw std::out_of_range("fixed data is out of range");
112 }
113 }
114
115 auto x = F(x_min, dx, i);
116 return x;
117 }
118
119 T operator()(int i) const
120 {
121 return F(x_min, dx, i);
122 }
123
124 [[nodiscard]] constexpr FixedDataIter<T, extrapolation> begin() noexcept { return FixedDataIter(this, 0); }
125 [[nodiscard]] constexpr FixedDataIter<T, extrapolation> end() noexcept { return FixedDataIter(this, n); }
126
127 [[nodiscard]] constexpr const FixedDataIter<T, extrapolation> cbegin() const noexcept { return FixedDataIter(this, 0); }
128 [[nodiscard]] constexpr const FixedDataIter<T, extrapolation> cend() const noexcept { return FixedDataIter(this, n); }
129
130 T back() const { return operator[](n - 1); }
131 T front() const { return operator[](0); }
132 T dstep() const noexcept { return dx; }
133
134 T prev(T x_current) noexcept
135 {
137 auto temp_data = *this;
138 temp_data.x_min = x_current;
139 return temp_data(-1);
140 }
141
142 T next(T x_current) noexcept
143 {
145 auto temp_data = *this;
146 temp_data.x_min = x_current;
147 return temp_data(+1);
148 }
149
150 constexpr void reserve(int n_) noexcept {}
151 constexpr void clear() noexcept
152 {
153 x_min = 0;
154 dx = 0;
155 n = 0;
156 }
157 constexpr auto size() const noexcept { return static_cast<size_t>(n); }
158};
159} // namespace slide
Definition: FixedData.hpp:23
friend bool operator==(const FixedDataIter &a, const FixedDataIter &b)
Definition: FixedData.hpp:66
const value_type operator*() const
Definition: FixedData.hpp:64
std::input_iterator_tag iterator_category
< For more information, see: https://internalpointers.com/post/writing-custom-iterators-modern-cpp
Definition: FixedData.hpp:25
FixedDataIter & operator++()
Definition: FixedData.hpp:33
value_type * pointer
Definition: FixedData.hpp:28
friend difference_type operator-(const FixedDataIter &a, const FixedDataIter &b)
Definition: FixedData.hpp:74
FixedDataIter(FixedData< T, extrapolation > *const f_data_ptr_, int n_)
Definition: FixedData.hpp:31
FixedDataIter operator++(int)
Definition: FixedData.hpp:39
friend FixedDataIter operator+(const FixedDataIter &a, const int b)
Definition: FixedData.hpp:69
friend bool operator!=(const FixedDataIter &a, const FixedDataIter &b)
Definition: FixedData.hpp:67
value_type operator[](int i)
Definition: FixedData.hpp:59
value_type & reference
Definition: FixedData.hpp:29
FixedDataIter & operator--()
Definition: FixedData.hpp:46
int difference_type
Definition: FixedData.hpp:26
T value_type
Definition: FixedData.hpp:27
FixedDataIter operator--(int)
Definition: FixedData.hpp:52
Definition: FixedData.hpp:92
T next(T x_current) noexcept
Definition: FixedData.hpp:142
T operator[](int i) const
Definition: FixedData.hpp:104
constexpr FixedDataIter< T, extrapolation > begin() noexcept
Definition: FixedData.hpp:124
T front() const
Definition: FixedData.hpp:131
constexpr void reserve(int n_) noexcept
Definition: FixedData.hpp:150
T operator()(int i) const
< Can extrapolate (go out of bounds).
Definition: FixedData.hpp:119
constexpr const FixedDataIter< T, extrapolation > cend() const noexcept
Definition: FixedData.hpp:128
FixedData()=default
FixedData(T x_min_, T dx_, int n_)
Definition: FixedData.hpp:101
constexpr FixedDataIter< T, extrapolation > end() noexcept
Definition: FixedData.hpp:125
T back() const
Definition: FixedData.hpp:130
T prev(T x_current) noexcept
Definition: FixedData.hpp:134
FixedData(T x_min_, T dx_, int n_, std::function< T(T, T, int)> F_)
Definition: FixedData.hpp:102
T dstep() const noexcept
Definition: FixedData.hpp:132
constexpr const FixedDataIter< T, extrapolation > cbegin() const noexcept
Definition: FixedData.hpp:127
constexpr auto size() const noexcept
Definition: FixedData.hpp:157
constexpr void clear() noexcept
Definition: FixedData.hpp:151
Slide namespace contains all the types, classes, and functions for the simulation framework.
Definition: Cell.hpp:27
int distance(const FixedDataIter< T, extrapolation > &a, const FixedDataIter< T, extrapolation > &b)
Definition: FixedData.hpp:85