WaveBlocksND
hawp_coeffs_loader.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <complex>
4 #include <fstream>
5 #include <memory>
6 #include <iostream>
7 
8 #include "boost/format.hpp"
9 
10 #include "../wavepackets/shapes/shape_enum.hpp"
12 
13 
14 namespace waveblocks {
15  namespace csv {
16 
17  template<dim_t D, class MultiIndex>
19  {
20  public:
21  Coefficients operator()(std::shared_ptr<ShapeEnum<D,MultiIndex> > shape, std::string filename)
22  {
23  Coefficients coeffs(shape->n_entries());
24 
25  std::vector<int> occurrences(coeffs.size());
26 
27  CoefficientsFileParser parser(filename, D, 1);
28 
29  while (parser.next()) {
30  MultiIndex index;
31 
32  int i_slice = 0;
33  for (int i = 0; i < D; i++) {
34  int entry = parser.lattice_node[i];
35  if (entry < 0)
36  throw std::runtime_error((boost::format("negative index at line %i") % parser.line_number()).str());
37 
38  index[i] = entry;
39  if (index[i] != entry)
40  throw std::runtime_error((boost::format("integer overflow at line %i") % parser.line_number()).str());
41 
42  i_slice += entry;
43  }
44 
45  std::size_t ordinal;
46  if (i_slice >= shape->n_slices() || !shape->slice(i_slice).try_find(index, ordinal))
47  throw std::runtime_error((boost::format("multi-index at line %i is not element of shape") % parser.line_number()).str());
48 
49  ordinal += shape->slice(i_slice).offset();
50 
51  if (occurrences[ordinal] >= 1)
52  throw std::runtime_error((boost::format("duplicate entry at line %i") % parser.line_number()).str());
53 
54  coeffs[ordinal] = parser.coefficients[0];
55  occurrences[ordinal] += 1;
56  }
57 
58  for (std::size_t i = 0; i < occurrences.size(); i++) {
59  if (occurrences[i] == 0) {
60  throw std::runtime_error((boost::format("missing entry for lattice node: %s") % shape->at(i)).str());
61  }
62  }
63 
64  return coeffs;
65  }
66  };
67  }
68 }
Definition: coefficients_file_parser.cpp:10
Eigen::Matrix< complex_t, Eigen::Dynamic, 1 > Coefficients
Definition: types.hpp:33
Definition: hawp_coeffs_loader.hpp:18
Coefficients operator()(std::shared_ptr< ShapeEnum< D, MultiIndex > > shape, std::string filename)
Definition: hawp_coeffs_loader.hpp:21
Definition: coefficients_file_parser.hpp:10