1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// *****************************************************************************
/*!
  \file      src/Base/Table.hpp
  \copyright 2012-2015 J. Bakosi,
             2016-2018 Los Alamos National Security, LLC.,
             2019-2021 Triad National Security, LLC.,
             2022-2025 J. Bakosi
             All rights reserved. See the LICENSE file for details.
  \brief     Basic functionality for storing and sampling a discrete
             (y1,y2,...,yN) = f(x) function
  \details   Basic functionality for storing and sampling a discrete
             (y1,y2,...,yN) = f(x) function.
*/
// *****************************************************************************
#ifndef Table_h
#define Table_h

#include <array><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <vector><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <limits><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include "Types.hpp"
#include "Exception.hpp"

namespace tk {

//! Type alias for storing a discrete (y1,y2,...,yN) = f(x) function
//! \tparam N Number of ordinates in the table
template< std::size_t N >
using Table = std::vector< std::array< real, N+1 > >;

//! Sample a discrete (y1,y2,...,yN) = f(x) function at x
//! \tparam N Number of ordinates in the table
//! \param[in] x Abscissa to sample at
//! \param[in] table Table to sample
//! \return Ordinates sampled
template< std::size_t N >
std::array< real, N > sample( real x, const Table< N >& table ) {<--- The function 'sample' is never used.

  Assert( not table.empty(), "Empty table to sample from" );

  // Shortcut for the type of a single line
  using Line = std::array< tk::real, N+1 >;
  // Shortcut for the type of all ordinates
  using Ord = std::array< tk::real, N >;

  // Lambda to return the abscissa of a Table (return the first value)
  auto abscissa = []( const Line& t ){ return t[0]; };

  // Lambda to return ordinates of a tk::Table
  auto ordinate = []( const Line& t ){
    Ord o;
    for (std::size_t i=0; i<N; ++i) o[i] = t[i+1];
    return o;
  };

  auto eps = std::numeric_limits< real >::epsilon();
  if (x < abscissa(table.front())+eps) return ordinate( table.front() );

  for (std::size_t i=0; i<table.size()-1; ++i) {
    auto t1 = abscissa( table[i] );
    auto t2 = abscissa( table[i+1] );
    if (t1 < x and x < t2) {
      auto d = (t2-t1)/(x-t1);
      auto p = ordinate( table[i] );
      auto q = ordinate( table[i+1] );
      Ord r;
      for (std::size_t j=0; j<N; ++j) r[j] = p[j]+(q[j]-p[j])/d;
      return r;
    }
  }

  return ordinate( table.back() );
}

} // tk::

#endif // Table_h