LCOV - code coverage report
Current view: top level - src/utility - util.hpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 4 6 66.7 %
Date: 2023-04-08 04:19:02 Functions: 1 1 100.0 %

          Line data    Source code
       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             : 
      15             : //!< Include other util files.
      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             : 
      30             : namespace slide {
      31             : 
      32             : constexpr inline auto abs_sqrt(auto x) { return std::sqrt(std::abs(x)); }
      33             : 
      34             : constexpr inline auto sqr(auto x) { return x * x; }
      35             : constexpr inline auto cube(auto x) { return x * x * x; }
      36             : 
      37             : 
      38             : template <typename T>
      39             : void 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             : 
      55             : namespace slide::util {
      56             : 
      57             : } // namespace slide::util
      58             : 
      59             : namespace slide {
      60             : std::vector<double> linstep(double x_min, double x_step, int Nstep); // #TODO not defined.
      61             : std::vector<double> logstep(double x_min, double x_step, int Nstep); // #TODO not defined.
      62             : 
      63             : //!< FixedData<int> range(int stop); #TODO -> FixedData is not good since it has function
      64             : //!< FixedData<int> range(int start, int stop, int step = 1);
      65             : 
      66             : FixedData<double> linspace_fix(double x1, double x2, int N);
      67             : 
      68             : template <size_t N>
      69             : constexpr 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             : 
      88             : namespace slide {
      89             : 
      90             : inline 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             : 
      96             : inline FixedData<double> linstep_fix(double x_min, double x_step, int Nstep)
      97             : {
      98             :   return FixedData<double>(x_min, x_step, Nstep);
      99             : }
     100             : 
     101             : inline 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             : 
     108         252 : inline FixedData<double> linspace_fix(double x1, double x2, int N)
     109             : {
     110         252 :   if (N < 1)
     111           0 :     return FixedData(x2, 0.0, 0);
     112         252 :   else if (N == 1)
     113           0 :     return FixedData(x2, 0.0, 1);
     114             :   else
     115         252 :     return FixedData(x1, (x2 - x1) / static_cast<double>(N - 1), N);
     116             : }
     117             : 
     118             : inline 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

Generated by: LCOV version 1.14