50 void read(
const std::string& path) {
51 std::ifstream stream(path, std::ios::binary);
53 throw std::runtime_error(
"Cannot read charge density file: " + path);
56 std::vector<char> header = read_record(stream);
57 if (header.size() != 3 *
sizeof(int32_t))
58 throw_corrupt(
"its first record is not the three-integer header");
59 const auto* header_values =
reinterpret_cast<const int32_t*
>(header.data());
60 is_gamma_only_ = header_values[0] != 0;
62 number_of_spin_components_ = header_values[2];
64 number_of_spin_components_ > 2) {
66 " G-vector(s) and " + std::to_string(number_of_spin_components_) +
67 " spin component(s)");
70 std::vector<char> second = read_record(stream);
71 has_reciprocal_lattice_vectors_ = second.size() == 9 *
sizeof(double);
72 if (has_reciprocal_lattice_vectors_) {
73 const auto* vectors =
reinterpret_cast<const double*
>(second.data());
74 for (
int row = 0; row < 3; row++)
75 for (
int column = 0; column < 3; column++)
76 reciprocal_lattice_vectors_[row][column] = vectors[row * 3 + column];
77 second = read_record(stream);
81 throw_corrupt(
"its Miller-index record is the wrong length");
82 const auto* miller_values =
reinterpret_cast<const int32_t*
>(second.data());
85 if (has_reciprocal_lattice_vectors_) {
86 miller_indices_[g_index] = {miller_values[3 * g_index],
87 miller_values[3 * g_index + 1],
88 miller_values[3 * g_index + 2]};
90 miller_indices_[g_index] = {miller_values[g_index],
96 charge_density_g_.assign(number_of_spin_components_, {});
97 for (
int spin = 0; spin < number_of_spin_components_; spin++) {
98 std::vector<char> density = read_record(stream);
100 throw_corrupt(
"its rho(G) record for spin channel " + std::to_string(spin + 1) +
101 " is the wrong length");
102 const auto* values =
reinterpret_cast<const Complex*
>(density.data());