QMCPACK
SizeLimitedDataQueue.hpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2023 QMCPACK developers.
6 //
7 // File developed by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
8 //
9 // File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 // -*- C++ -*-
12 #ifndef QMCPLUSPLUS_SIZELIMITEDDATAQUEUE_H
13 #define QMCPLUSPLUS_SIZELIMITEDDATAQUEUE_H
14 
15 #include <deque>
16 #include <array>
17 #include <cassert>
18 
19 namespace qmcplusplus
20 {
21 
22 /** collect data with a history limit.
23  * data stored in std::deque<std::array<T, NUM_FIELDS>>
24  */
25 template<typename T, size_t NUM_FIELDS>
27 {
28 public:
30  {
31  T weight;
32  std::array<T, NUM_FIELDS> properties;
33  };
34 
35  using value_type = HistoryElement;
36 
37  SizeLimitedDataQueue(size_t size_limit) : size_limit_(size_limit) {}
38 
39  /// add a new record
40  void push(const value_type& val)
41  {
42  if (data.size() == size_limit_)
43  data.pop_front();
44  assert(data.size() < size_limit_);
45  data.push_back(val);
46  }
47 
48  /// add a new record
49  void push(value_type&& val)
50  {
51  if (data.size() == size_limit_)
52  data.pop_front();
53  assert(data.size() < size_limit_);
54  data.push_back(val);
55  }
56 
57  /// return weighted average
58  auto weighted_avg() const
59  {
60  std::array<T, NUM_FIELDS> avg;
61  std::fill(avg.begin(), avg.end(), T(0));
62  T weight_sum = 0;
63  for (auto& element : data)
64  {
65  weight_sum += element.weight;
66  for (size_t i = 0; i < NUM_FIELDS; i++)
67  avg[i] += element.properties[i] * element.weight;
68  }
69  for (size_t i = 0; i < NUM_FIELDS; i++)
70  avg[i] /= weight_sum;
71  return avg;
72  }
73 
74  /// return the number of records
75  auto size() const { return data.size(); }
76 
77 private:
78  std::deque<value_type> data;
79  const size_t size_limit_;
80 };
81 
82 } // namespace qmcplusplus
83 #endif
collect data with a history limit.
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
void push(value_type &&val)
add a new record
auto size() const
return the number of records
void push(const value_type &val)
add a new record
auto weighted_avg() const
return weighted average