SLIDE  3.0.0
A simulator for lithium-ion battery pack degradation
Loading...
Searching...
No Matches
MCMC.hpp
Go to the documentation of this file.
1/*
2 * MCMC.hpp
3 *
4 * Markov Chain Monte Carlo
5 *
6 * Created on: 12 Nov 2021
7 * Author(s): Volkan Kumtepeli
8 *
9 *
10 * See : Aitio, Antti, Scott G. Marquis, Pedro Ascencio, and David Howey.
11 * "Bayesian parameter estimation applied to the Li-ion battery single particle model with electrolyte dynamics."
12 * IFAC-PapersOnLine 53, no. 2 (2020): 12497-12504.
13 *
14 * Copyright (c) 2021, The Chancellor, Masters and Scholars of the University
15 * of Oxford, VITO nv, and the 'Slide' Developers.
16 * See the licence file LICENCE.txt for more information.
17 */
18
19#pragma once
20
21#include <cstdlib>
22#include <array>
23
24namespace slide {
25template <size_t N>
26auto S_update(slide::Matrix<double, N, N> &S, std::array<double, N> w, double alpha, double n)
27{
28 constexpr double gamma = 0.5;
29 constexpr double alpha_star = 0.234;
30
31 const double m = std::pow(n, -gamma) * (alpha - alpha_star) / slide::norm_sq<2>(w);
32 const double m_root = std::sqrt(std::abs(m));
33
34 for (auto &w_i : w)
35 w_i *= m_root;
36
37 auto I = slide::eye<N>(1.0);
38
39 slide::cholUpdate<N>(I, w, m < 0);
40
41 auto S_new = slide::zeros<N, N>();
42
43 for (size_t i = 0; i < w.size(); i++)
44 for (size_t j = 0; j <= i; j++)
45 for (size_t o = 0; o <= i; o++)
46 S_new[i][j] += S[i][o] * I[o][j];
47
48 return S_new;
49}
50
51template <size_t N_PARAM, size_t N_OUTPUT = 1>
53{
54 std::array<double, N_PARAM> param;
55 std::array<double, N_OUTPUT> logR;
56
57 auto scale(const std::array<double, N_PARAM> &param_scalar) const
58 {
59 auto param_scaled = param;
60
61 for (size_t i = 0; i < param_scaled.size(); i++)
62 param_scaled[i] *= param_scalar[i];
63
64 return param_scaled;
65 }
66
67 auto &operator[](size_t i)
68 {
69 if (i < N_PARAM)
70 return param[i];
71 else if (i == N_PARAM)
72 return logR[i - N_PARAM];
73 else {
74 std::cout << "THIS SHOULD NOT HAPPEN out of bounds for MCMC_Input.\n";
75 throw 123511;
76 }
77 }
78
79 auto operator[](size_t i) const
80 {
81 if (i < N_PARAM)
82 return param[i];
83 else if (i == N_PARAM)
84 return logR[i - N_PARAM];
85 else {
86 std::cout << "THIS SHOULD NOT HAPPEN out of bounds for MCMC_Input.\n";
87 throw 123511;
88 }
89 }
90
91 auto input_size() const { return N_PARAM; }
92
93 auto output_size() const { return N_OUTPUT; }
94
95 auto size() const { return N_PARAM + N_OUTPUT; }
96
97 auto get_R() const { return std::exp(logR[0]); };
98};
99
100template <size_t N_PARAM, size_t N_OUTPUT = 1>
102{
104 std::array<double, N_PARAM> theta_scalar{};
106
107 size_t n_iter = 1000;
108};
109
110template <typename Tdistribution>
111struct Prior
112{
113 std::vector<Tdistribution> distributionVec;
114
115 Prior(std::vector<Tdistribution> distributionVec) : distributionVec(distributionVec) {}
116
117 template <typename Tparam>
118 auto operator()(const Tparam &b)
119 {
120 for (const auto b_i : b)
121 if (b_i <= 0)
122 return std::pair(-1e25, false);
123
124 double sum{ 0 };
125 for (size_t i = 0; i < b.size(); i++)
126 sum += std::log(distributionVec[i](b[i]));
127
128 return std::pair(sum, true);
129 }
130};
131
132using LL_Result = std::array<double, 5>;
137
138template <typename ThetaType, typename PriorType, typename CostType, typename SettingsType>
139auto LL(const ThetaType &theta_cand, PriorType &prior, const CostType &costFun, const size_t Nobservations, const SettingsType &mcmcSettings)
140{
141 auto [P, flag] = prior(theta_cand.param);
142
143 if (!flag)
144 return std::pair(LL_Result{}, false);
145 else {
146 auto err_sqr = costFun(theta_cand.scale(mcmcSettings.theta_scalar));
147
148 const auto R_volt_exp = theta_cand.get_R();
149
150 const double LLout1 = 0.5 * std::log(2.0 * PhyConst::pi * R_volt_exp) * Nobservations;
151 const double LLout2 = 0.5 * err_sqr / R_volt_exp;
152 double LLout = -P + LLout1 + LLout2;
153
154 return std::pair(LL_Result{ LLout, LLout1, LLout2, P, err_sqr }, flag);
155 }
156}
157
158template <size_t N_PARAM, size_t N_OUTPUT = 1>
160{
163
164 auto operator[](size_t i) const
165 {
166 if (i < input.size())
167 return input[i];
168 else
169 return LLt_struct[i - input.size()];
170 }
171
172 auto back() const
173 {
174 return LLt_struct.back();
175 }
176
177 auto size() const
178 {
179 return input.size() + LLt_struct.size();
180 }
181};
182
183} // namespace slide
constexpr double pi
Definition: constants.hpp:23
Slide namespace contains all the types, classes, and functions for the simulation framework.
Definition: Cell.hpp:27
std::array< std::array< T, COL >, ROW > Matrix
See source: http://cpptruths.blogspot.com/2011/10/multi-dimensional-arrays-in-c11....
Definition: matrix.hpp:21
auto LL(const ThetaType &theta_cand, PriorType &prior, const CostType &costFun, const size_t Nobservations, const SettingsType &mcmcSettings)
Definition: MCMC.hpp:139
std::array< double, 5 > LL_Result
Definition: MCMC.hpp:136
auto S_update(slide::Matrix< double, N, N > &S, std::array< double, N > w, double alpha, double n)
Definition: MCMC.hpp:26
out I
Definition: squeeze_variables.m:14
Definition: MCMC.hpp:102
MCMC_Input< N_PARAM, N_OUTPUT > theta_init
Definition: MCMC.hpp:103
Matrix< double, N_PARAM, 2 > distParam
Definition: MCMC.hpp:105
size_t n_iter
Definition: MCMC.hpp:107
std::array< double, N_PARAM > theta_scalar
Definition: MCMC.hpp:104
Definition: MCMC.hpp:53
auto & operator[](size_t i)
Definition: MCMC.hpp:67
auto get_R() const
Definition: MCMC.hpp:97
auto operator[](size_t i) const
Definition: MCMC.hpp:79
auto input_size() const
Definition: MCMC.hpp:91
auto scale(const std::array< double, N_PARAM > &param_scalar) const
Definition: MCMC.hpp:57
std::array< double, N_OUTPUT > logR
Definition: MCMC.hpp:55
std::array< double, N_PARAM > param
Definition: MCMC.hpp:54
auto size() const
Definition: MCMC.hpp:95
auto output_size() const
Definition: MCMC.hpp:93
Definition: MCMC.hpp:160
LL_Result LLt_struct
Definition: MCMC.hpp:162
MCMC_Input< N_PARAM, N_OUTPUT > input
Definition: MCMC.hpp:161
auto size() const
Definition: MCMC.hpp:177
auto operator[](size_t i) const
Definition: MCMC.hpp:164
auto back() const
Definition: MCMC.hpp:172
Definition: MCMC.hpp:112
std::vector< Tdistribution > distributionVec
Definition: MCMC.hpp:113
Prior(std::vector< Tdistribution > distributionVec)
Definition: MCMC.hpp:115
auto operator()(const Tparam &b)
Definition: MCMC.hpp:118