WaveBlocksND
tensor_product_qr.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <tuple>
5 #include <vector>
6 
7 #include <Eigen/Core>
8 #include <Eigen/Dense>
9 
10 #include "../types.hpp"
11 
12 #include "tables_gausshermite.hpp"
13 
14 
15 namespace waveblocks {
16  namespace innerproducts {
17 
24  template <class... RULES>
26  {
27  static const dim_t D = sizeof...(RULES);
28 
29  using NodeMatrix = Eigen::Matrix<real_t,D,Eigen::Dynamic>;
30  using WeightVector = Eigen::Matrix<real_t,1,Eigen::Dynamic>;
31 
39  {
40  dim_t result = 1;
41  for(auto n_nodes : { RULES::number_nodes() ... })
42  {
43  result *= n_nodes;
44  }
45  return result;
46  }
47 
51  static NodeMatrix nodes()
52  {
54  return cached_nodes;
55  }
56 
57 
62  {
64  return cached_weights;
65  }
66 
70  static std::tuple<NodeMatrix,WeightVector>
72  {
74  return std::make_tuple(cached_nodes, cached_weights);
75  }
76 
80  static void clear_cache()
81  {
82  cached = false;
83  cached_nodes.resize(D, 0);
84  cached_weights.resize(1, 0);
85  }
86 
87  private:
88  static bool cached;
91 
96  {
97  const dim_t dim = D;
98  const dim_t n_nodes = number_nodes();
99  cached_nodes.resize(dim, n_nodes);
100  cached_weights.resize(1, n_nodes);
101  cached_weights.setOnes();
102 
103  const dim_t sizes[D] = { RULES::number_nodes() ...};
104  dim_t cycles[D];
105  cycles[D-1] = 1;
106  for(dim_t d = D-2; d >= 0; --d)
107  {
108  cycles[d] = cycles[d+1] * sizes[d+1];
109  }
110 
111  // Fill in nodes and weights.
112  {
113  dim_t d = 0;
114  for(auto nw : { RULES::nodes_and_weights() ... })
115  {
116  for(dim_t n = 0; n < n_nodes; ++n)
117  {
118  const dim_t base_idx = (n / cycles[d]) % sizes[d];
119  cached_nodes(d, n) = std::get<0>(nw)(base_idx);
120  cached_weights(n) *= std::get<1>(nw)(base_idx);
121  }
122 
123  ++d;
124  }
125  }
126  cached = true;
127  }
128  };
129 
130  // Initialize static members.
131  template <class... RULES>
132  bool TensorProductQR<RULES...>::cached = false;
133 
134  template <class... RULES>
135  typename TensorProductQR<RULES...>::NodeMatrix
137 
138  template <class... RULES>
139  typename TensorProductQR<RULES...>::WeightVector
141  }
142 }
Definition: coefficients_file_parser.cpp:10
static dim_t number_nodes()
Return the number of nodes for the given order.
Definition: tensor_product_qr.hpp:38
static void clear_cache()
Free the precalculated nodes and weights.
Definition: tensor_product_qr.hpp:80
static bool cached
Definition: tensor_product_qr.hpp:88
static const dim_t D
Definition: tensor_product_qr.hpp:27
static WeightVector cached_weights
Definition: tensor_product_qr.hpp:90
static std::tuple< NodeMatrix, WeightVector > nodes_and_weights()
Return the quadrature nodes and weights.
Definition: tensor_product_qr.hpp:71
static NodeMatrix cached_nodes
Definition: tensor_product_qr.hpp:89
static void calculate_nodes_and_weights()
Precalculate nodes and weights.
Definition: tensor_product_qr.hpp:95
static NodeMatrix nodes()
Return the quadrature nodes.
Definition: tensor_product_qr.hpp:51
static WeightVector weights()
Return the quadrature weights.
Definition: tensor_product_qr.hpp:61
Eigen::Matrix< real_t, 1, Eigen::Dynamic > WeightVector
Definition: tensor_product_qr.hpp:30
int dim_t
Definition: types.hpp:16
Structure providing weighted nodes for Tensor Product quadrature.
Definition: tensor_product_qr.hpp:25
Eigen::Matrix< real_t, D, Eigen::Dynamic > NodeMatrix
Definition: tensor_product_qr.hpp:29