SLIDE  3.0.0
A simulator for lithium-ion battery pack degradation
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1/*
2 * matrix.hpp
3 *
4 * matrix data type
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 "DynamicMatrix.hpp"
14
15#include <cstdlib>
16#include <array>
17#include <cmath>
18
19namespace slide {
20template <typename T, size_t ROW, size_t COL>
21using Matrix = std::array<std::array<T, COL>, ROW>;
22
23template <size_t N, size_t M = N>
24auto eye(double k = 1.0)
25{
26 auto A = Matrix<double, M, N>{};
27 size_t m = std::min(M, N);
28 for (size_t i = 0; i < m; i++)
29 A[i][i] = k;
30
31 return A;
32}
33
34template <size_t N, size_t M = N>
35auto zeros()
36{
37 auto A = Matrix<double, M, N>{};
38 return A;
39}
40
41template <int N, int M = N>
42auto ones(double k = 1.0)
43{
44 auto A = Matrix<double, M, N>{};
45
46 for (int i = 0; i < M; i++)
47 for (int j = 0; j < N; j++)
48 A[i][j] = k;
49
50 return A;
51}
52
53template <int N>
54void cholUpdate(slide::Matrix<double, N, N> &L, std::array<double, N> x, bool isDowndate = false)
55{
57 for (int k = 0; k < N; k++) {
58 const double Lk = L[k][k];
59 const double s = x[k] / Lk;
60 double r;
61 if (isDowndate)
62 r = std::sqrt(Lk * Lk - x[k] * x[k]);
63 else
64 r = std::hypot(Lk, x[k]);
65
66 const double c = r / Lk;
67 L[k][k] = r;
68
69 for (int j = k + 1; (k != (N - 1) && (j < N)); j++) {
70 if (isDowndate)
71 L[j][k] -= s * x[j] / c;
72 else
73 L[j][k] += s * x[j] / c;
74 x[j] = c * x[j] - s * L[j][k];
75 }
76 }
77}
78
79} // namespace slide
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
void cholUpdate(slide::Matrix< double, N, N > &L, std::array< double, N > x, bool isDowndate=false)
Definition: matrix.hpp:54
auto ones(double k=1.0)
Definition: matrix.hpp:42
auto eye(double k=1.0)
Definition: matrix.hpp:24
auto zeros()
Definition: matrix.hpp:35