SLIDE  3.0.0
A simulator for lithium-ion battery pack degradation
Loading...
Searching...
No Matches
Histogram.hpp
Go to the documentation of this file.
1/*
2 * Histogram.hpp
3 *
4 * Histogram code to hold histogram data.
5 *
6 * Created on: 14 Dec 2021
7 * Author(s): Jorn Reniers, Volkan Kumtepeli
8 */
9
10#pragma once
11
12#include "../settings/settings.hpp"
13#include "../utility/utility.hpp"
14
15#include <vector>
16#include <cstdlib>
17#include <algorithm>
18#include <span>
19#include <fstream>
20
21namespace slide {
22
23enum class HistogramType {
24 slidepack = 0,
25 equidistant = 1
26
27};
28
29template <HistogramType histogramType = HistogramType::equidistant, typename Tdata = double>
31{
32 std::vector<size_t> bins;
33 Tdata x_min{}, x_max{}, dx{ 1 };
34 int Nbins;
35
36public:
37 Histogram() = default;
38
39 Histogram(Tdata x_min_, Tdata x_max_, int Nbins_ = settings::DATASTORE_NHIST)
40 : bins(Nbins_ + 2), x_min(x_min_), x_max(x_max_), dx((x_max_ - x_min_) / Nbins_), Nbins(Nbins_)
41 {
42 }
43
44 constexpr void add(Tdata x) noexcept
45 {
46 /*
47 * increase the correct bin counter
48 * data is in bin i if edge[i-1] <= data < edge[i]
49 * bins[0] is number of elements less than x_min
50 * bins[end] is number of elements more than x_max
51 * */
52 auto i = static_cast<int>(1 + (x - x_min) / dx);
53
54 const auto ssize = static_cast<int>(bins.size());
55 i = std::max(0, std::min(i, ssize - 1)); // #TODO what happens if container is empty?
56 bins[i]++;
57 }
58
59 std::span<const size_t> viewBinValues() const noexcept { return bins; }
60
61 [[nodiscard]] constexpr auto begin() noexcept { return bins.begin(); }
62 [[nodiscard]] constexpr auto end() noexcept { return bins.end(); }
63
64 auto getEdgeValues() const noexcept { return range_fix(x_min, x_max, dx); }
65
66 auto size() const noexcept { return bins.size(); }
67
68 friend std::ostream &operator<<(std::ostream &ofs, const Histogram<> &hist);
69};
70
71inline std::ostream &operator<<(std::ostream &ofs, const Histogram<> &hist)
72{
73 auto bins = hist.viewBinValues();
74 auto edges = hist.getEdgeValues();
75
76 ofs << "Edges:\n";
77
78 for (auto edge : edges)
79 ofs << edge << ',';
80
81 ofs << '\n';
82 ofs << "Bins:\n";
83
84 for (auto bin : bins)
85 ofs << bin << ',';
86
87 ofs << '\n';
88
89 return ofs;
90}
91
92static Histogram<> EmptyHistogram{};
93
94} // namespace slide
Definition: Histogram.hpp:31
constexpr auto end() noexcept
Definition: Histogram.hpp:62
Histogram(Tdata x_min_, Tdata x_max_, int Nbins_=settings::DATASTORE_NHIST)
Definition: Histogram.hpp:39
constexpr void add(Tdata x) noexcept
Definition: Histogram.hpp:44
constexpr auto begin() noexcept
Definition: Histogram.hpp:61
friend std::ostream & operator<<(std::ostream &ofs, const Histogram<> &hist)
Definition: Histogram.hpp:71
auto getEdgeValues() const noexcept
Definition: Histogram.hpp:64
std::span< const size_t > viewBinValues() const noexcept
Definition: Histogram.hpp:59
Histogram()=default
auto size() const noexcept
Definition: Histogram.hpp:66
constexpr int DATASTORE_NHIST
length of the arrays with the histograms (if 1)
Definition: settings.hpp:49
Slide namespace contains all the types, classes, and functions for the simulation framework.
Definition: Cell.hpp:27
std::ostream & operator<<(std::ostream &os, const DynamicMatrix< Tdata > &M)
Definition: DynamicMatrix.hpp:72
HistogramType
Definition: Histogram.hpp:23
FixedData< double > range_fix(double x_min, double x_max, double x_step)
Definition: util.hpp:90