SLIDE  3.0.0
A simulator for lithium-ion battery pack degradation
Loading...
Searching...
No Matches
util.hpp
Go to the documentation of this file.
1/*
2 * util.hpp
3 *
4 * Some utility functions. So the code is less verbose.
5 *
6 * A cycler implements check-up procedures and degradation procedures.
7 * The data from the check-up procedures is written in csv files in the same subfolder as where the cycling data of a cell is written (see BasicCycler.cpp).
8 * There is one file per 'type' of check-up (capacity measurement, OCV measurement, CCCV cycles and a pulse discharge).
9 *
10 * Copyright (c) 2019, The Chancellor, Masters and Scholars of the University
11 * of Oxford, VITO nv, and the 'Slide' Developers.
12 * See the licence file LICENCE.txt for more information.
13 */
14
16#pragma once
17
18#include "../settings/settings.hpp"
19#include "slide_aux.hpp"
20#include "util_debug.hpp"
21#include "units.hpp"
22
23
24#include <string>
25#include <iostream>
26#include <array>
27#include <vector>
28#include <cmath>
29
30namespace slide {
31
32constexpr inline auto abs_sqrt(auto x) { return std::sqrt(std::abs(x)); }
33
34constexpr inline auto sqr(auto x) { return x * x; }
35constexpr inline auto cube(auto x) { return x * x * x; }
36
37
38template <typename T>
39void output_printer(const std::vector<T> &vec, const auto &save_path)
40{
41 std::ofstream out_file(save_path, std::ios_base::out);
42
43 for (const auto &state : vec) {
44 for (size_t i{ 0 }; i < state.size(); i++) {
45 if (i != 0) out_file << ',';
46 out_file << state[i];
47 }
48 }
49
50 out_file.close();
51}
52} // namespace slide
53
54
55namespace slide::util {
56
57} // namespace slide::util
58
59namespace slide {
60std::vector<double> linstep(double x_min, double x_step, int Nstep); // #TODO not defined.
61std::vector<double> logstep(double x_min, double x_step, int Nstep); // #TODO not defined.
62
65
66FixedData<double> linspace_fix(double x1, double x2, int N);
67
68template <size_t N>
69constexpr std::array<double, N> linspace(double x1, double x2)
70{
71 std::array<double, N> out;
72
73 double dx{ 1 };
74 if (N < 1)
75 return out;
76 else if (N > 1)
77 dx = (x2 - x1) / static_cast<double>(N - 1);
78
79 for (size_t n{ 0 }; n < N; n++) {
80 out[N - 1 - n] = x2 - n * dx;
81 }
82
83 return out;
84}
85
86} // namespace slide
87
88namespace slide {
89
90inline FixedData<double> range_fix(double x_min, double x_max, double x_step)
91{
92 int Nstep = static_cast<int>((x_max - x_min) / x_step) + 1;
93 return FixedData<double>(x_min, x_step, Nstep);
94}
95
96inline FixedData<double> linstep_fix(double x_min, double x_step, int Nstep)
97{
98 return FixedData<double>(x_min, x_step, Nstep);
99}
100
101inline FixedData<double> logstep_fix(double x_min, double x_step, int Nstep)
102{
103 auto fun = [](double x_min, double x_step, int i) { return x_min * std::pow(x_step, i); };
104
105 return FixedData<double>(x_min, x_step, Nstep, fun);
106}
107
108inline FixedData<double> linspace_fix(double x1, double x2, int N)
109{
110 if (N < 1)
111 return FixedData(x2, 0.0, 0);
112 else if (N == 1)
113 return FixedData(x2, 0.0, 1);
114 else
115 return FixedData(x1, (x2 - x1) / static_cast<double>(N - 1), N);
116}
117
118inline std::vector<double> linspace(double x1, double x2, int N)
119{
120 N = std::max(0, N);
121 std::vector<double> out(N);
122
123 double dx{ 1 };
124 if (N < 1)
125 return out;
126 else if (N > 1)
127 dx = (x2 - x1) / static_cast<double>(N - 1);
128
129 for (int n{ 0 }; n < N; n++) {
130 out[N - 1 - n] = x2 - n * dx;
131 }
132
133 return out;
134}
135} // namespace slide
Definition: FixedData.hpp:92
Definition: predicate_functions.hpp:12
Slide namespace contains all the types, classes, and functions for the simulation framework.
Definition: Cell.hpp:27
constexpr std::array< double, N > linspace(double x1, double x2)
Definition: util.hpp:69
FixedData< double > linspace_fix(double x1, double x2, int N)
Definition: util.hpp:108
std::vector< double > logstep(double x_min, double x_step, int Nstep)
FixedData< double > linstep_fix(double x_min, double x_step, int Nstep)
Definition: util.hpp:96
std::vector< double > linstep(double x_min, double x_step, int Nstep)
constexpr auto cube(auto x)
Definition: util.hpp:35
void output_printer(const std::vector< T > &vec, const auto &save_path)
Definition: util.hpp:39
constexpr auto abs_sqrt(auto x)
Definition: util.hpp:32
constexpr auto sqr(auto x)
Definition: util.hpp:34
FixedData< double > range_fix(double x_min, double x_max, double x_step)
Definition: util.hpp:90
FixedData< double > logstep_fix(double x_min, double x_step, int Nstep)
Definition: util.hpp:101
out
Definition: run_test.m:22