WaveBlocksND
vector_inner_product.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cmath>
4 #include <functional>
5 #include <iostream>
6 #include <tuple>
7 #include <vector>
8 
9 #include <Eigen/Core>
10 #include <Eigen/Dense>
11 
12 #include "../types.hpp"
13 #include "../wavepackets/hawp_commons.hpp"
14 
16 
17 
18 namespace waveblocks {
19  namespace innerproducts {
28  template<dim_t D, class MultiIndex, class QR>
30  {
31  public:
39  using CDiagonalNN = Eigen::DiagonalMatrix<complex_t, Eigen::Dynamic>;
40  using NodeMatrix = typename QR::NodeMatrix;
41  using WeightVector = typename QR::WeightVector;
42  using op_t = std::function<CMatrix1N(CMatrixDN,RMatrixD1,dim_t,dim_t)>;
43 
63  template<class Packet>
64  static CMatrixNN build_matrix(const Packet& packet,
65  const op_t& op=default_op) {
66  const dim_t n_components = packet.n_components();
67 
68  // Calculate offsets into output matrix.
69  // Needed for parallelization.
70  std::vector<dim_t> offsets(n_components);
71  offsets[0] = 0;
72  for (dim_t i = 1; i < n_components; ++i) {
73  offsets[i] = packet.component(i-1).coefficients().size();
74  }
75 
76  // Allocate output matrix.
77  size_t total_size = 0;
78  for (auto& comp : packet.components()) {
79  total_size += comp.coefficients().size();
80  }
81  CMatrixNN result(total_size, total_size);
82 
83  // Calculate matrix.
85  for (dim_t i = 0; i < n_components; ++i) {
86  for (dim_t j = 0; j < n_components; ++j) {
87  using namespace std::placeholders;
88  result.block(offsets[i], offsets[j],
89  packet.component(i).coefficients().size(),
90  packet.component(j).coefficients().size()) =
91  IP::build_matrix(packet.component(i), packet.component(j),
92  std::bind(op, _1, _2, i, j));
93  }
94  }
95  return result;
96  }
97 
119  template<class Pacbra, class Packet>
120  static CMatrixNN build_matrix_inhomog(const Pacbra& pacbra,
121  const Packet& packet,
122  const op_t& op=default_op) {
123  const dim_t n_components_bra = pacbra.n_components();
124  const dim_t n_components_ket = packet.n_components();
125 
126  // Calculate offsets into output matrix.
127  // Needed for parallelization.
128  std::vector<dim_t> offsets_bra(n_components_bra);
129  offsets_bra[0] = 0;
130  for (dim_t i = 1; i < n_components_bra; ++i) {
131  offsets_bra[i] = pacbra.component(i-1).coefficients().size();
132  }
133 
134  std::vector<dim_t> offsets_ket(n_components_ket);
135  offsets_ket[0] = 0;
136  for (dim_t i = 1; i < n_components_ket; ++i) {
137  offsets_ket[i] = packet.component(i-1).coefficients().size();
138  }
139 
140  // Allocate output matrix.
141  size_t total_rows = 0, total_cols = 0;
142  for (auto& comp : pacbra.components()) {
143  total_rows += comp.coefficients().size();
144  }
145  for (auto& comp : packet.components()) {
146  total_cols += comp.coefficients().size();
147  }
148  CMatrixNN result(total_rows, total_cols);
149 
150  // Calculate matrix.
152  for (dim_t i = 0; i < n_components_bra; ++i) {
153  for (dim_t j = 0; j < n_components_ket; ++j) {
154  using namespace std::placeholders;
155  result.block(offsets_bra[i], offsets_ket[j],
156  pacbra.component(i).coefficients().size(),
157  packet.component(j).coefficients().size()) =
158  IP::build_matrix(pacbra.component(i), packet.component(j),
159  std::bind(op, _1, _2, i, j));
160  }
161  }
162  return result;
163  }
164 
172  template<class Packet>
173  static CMatrixN1 quadrature(const Packet& packet,
174  const op_t& op=default_op) {
175  const dim_t n_components = packet.n_components();
176  CMatrixN1 result(n_components * n_components, 1);
177 
178  // Calculate matrix.
180  for (dim_t i = 0; i < n_components; ++i) {
181  for (dim_t j = 0; j < n_components; ++j) {
182  using namespace std::placeholders;
183  result(j + i * n_components) = packet.component(i).coefficients().adjoint() *
184  IP::build_matrix(packet.component(i), packet.component(j),
185  std::bind(op, _1, _2, i, j)) *
186  packet.component(j).coefficients();
187  }
188  }
189  return result;
190  }
191 
198  template<class Pacbra, class Packet>
199  static CMatrixN1 quadrature_inhomog(const Pacbra& pacbra,
200  const Packet& packet,
201  const op_t& op=default_op) {
202  const dim_t n_components_bra = pacbra.n_components();
203  const dim_t n_components_ket = packet.n_components();
204  CMatrixN1 result(n_components_bra * n_components_ket, 1);
205 
206  // Calculate matrix.
208  for (dim_t i = 0; i < n_components_bra; ++i) {
209  for (dim_t j = 0; j < n_components_ket; ++j) {
210  using namespace std::placeholders;
211  result(j + i * n_components_ket) = pacbra.component(i).coefficients().adjoint() *
212  IP::build_matrix(pacbra.component(i), packet.component(j),
213  std::bind(op, _1, _2, i, j)) *
214  packet.component(j).coefficients();
215  }
216  }
217  return result;
218  }
219 
220  private:
221  static CMatrix1N default_op(const CMatrixDN& nodes, const RMatrixD1& pos, dim_t i, dim_t j)
222  {
223  (void)pos;
224  if (i == j) return CMatrix1N::Ones(1, nodes.cols());
225  else return CMatrix1N::Zero(1, nodes.cols());
226  }
227  };
228  }
229 }
CMatrix< Eigen::Dynamic, Eigen::Dynamic > CMatrixNN
Definition: vector_inner_product.hpp:32
Definition: coefficients_file_parser.cpp:10
Eigen::Matrix< real_t, R, C > RMatrix
Definition: types.hpp:22
CMatrix< 1, Eigen::Dynamic > CMatrix1N
Definition: vector_inner_product.hpp:33
CMatrix< D, D > CMatrixDD
Definition: vector_inner_product.hpp:36
CMatrix< D, Eigen::Dynamic > CMatrixDN
Definition: vector_inner_product.hpp:37
Class providing inhomogeneous inner product calculation of scalar wavepackets.
Definition: inhomogeneous_inner_product.hpp:30
static CMatrixNN build_matrix(const Packet &packet, const op_t &op=default_op)
Calculate the matrix of the inner product.
Definition: vector_inner_product.hpp:64
typename QR::NodeMatrix NodeMatrix
Definition: vector_inner_product.hpp:40
static CMatrix1N default_op(const CMatrixDN &nodes, const RMatrixD1 &pos, dim_t i, dim_t j)
Definition: vector_inner_product.hpp:221
Class providing inner product calculation of multi-component wavepackets.
Definition: vector_inner_product.hpp:29
RMatrix< D, 1 > RMatrixD1
Definition: vector_inner_product.hpp:38
Eigen::DiagonalMatrix< complex_t, Eigen::Dynamic > CDiagonalNN
Definition: vector_inner_product.hpp:39
static CMatrixN1 quadrature(const Packet &packet, const op_t &op=default_op)
Perform quadrature.
Definition: vector_inner_product.hpp:173
Eigen::Matrix< complex_t, R, C > CMatrix
Definition: types.hpp:19
typename QR::WeightVector WeightVector
Definition: vector_inner_product.hpp:41
CMatrix< Eigen::Dynamic, 1 > CMatrixN1
Definition: vector_inner_product.hpp:34
static CMatrixN1 quadrature_inhomog(const Pacbra &pacbra, const Packet &packet, const op_t &op=default_op)
Perform quadrature.
Definition: vector_inner_product.hpp:199
static CMatrixNN build_matrix_inhomog(const Pacbra &pacbra, const Packet &packet, const op_t &op=default_op)
Calculate the matrix of the inner product.
Definition: vector_inner_product.hpp:120
CMatrix< D, 1 > CMatrixD1
Definition: vector_inner_product.hpp:35
int dim_t
Definition: types.hpp:16
std::function< CMatrix1N(CMatrixDN, RMatrixD1, dim_t, dim_t)> op_t
Definition: vector_inner_product.hpp:42