Loading...
Searching...
No Matches
qe.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * QEPlanewaveKohnShamChargeDensityCheckpoint — QE-format binary writer.
5 *
6 * Serializes the converged reciprocal-space charge density ρ(G) to disk in the
7 * Fortran unformatted record layout of Quantum ESPRESSO's charge-density.dat
8 * (Modules/io_base.f90, write_rhog): header, reciprocal vectors, Miller
9 * indices, then ρ(G). The full G sphere is written, so gamma_only is false.
10 */
11
12#include <fstream>
13#include <string>
14#include <vector>
15
17#include "io/checkpoint/qe.hpp"
18#include "io/logger.hpp"
20
21namespace q3 {
22
24 public:
26
27 void write_to_file(const std::string& filename) const override {
28 /**
29 * Buffer must be declared BEFORE the ofstream so it outlives the stream.
30 * The stream's destructor flushes to the buffer, so the buffer must still be valid.
31 */
32 std::vector<char> file_buffer(4 * 1024 * 1024);
33 std::ofstream output_file;
34 output_file.rdbuf()->pubsetbuf(file_buffer.data(), file_buffer.size());
35 output_file.open(filename, std::ios::binary);
36 if (!output_file.is_open()) {
37 log::warn("Cannot open {} for writing", filename);
38 return;
39 }
40
42 log::error("planewave_basis is null in NormConservingPlanewaveKohnShamSCFState during "
43 "charge density serialization");
44 return;
45 }
47 log::error("crystal_structure is null during charge density serialization");
48 return;
49 }
50 const auto& planewave_basis = *scf_state_.planewave_basis;
51 const auto& charge_density_g = scf_state_.charge_density_g;
52 int number_of_g_vectors = planewave_basis.number_of_g_vectors();
53
54 QEBinaryRecordFormat record_format;
55
56 // Record 1: gamma_only, number_of_g_vectors, number_of_spin_components
57 struct {
58 int32_t gamma_only = 0;
59 int32_t number_of_g_vectors_header;
60 int32_t number_of_spin_components = 1;
61 } header;
62 header.number_of_g_vectors_header = number_of_g_vectors;
63 record_format.write_record(output_file, &header, sizeof(header));
64
65 // Record 2: b1, b2, b3 in 1/Bohr
66 const Matrix3x3 reciprocal_vectors_bohr =
68 double reciprocal_vectors_array[9];
69 for (int vector_index = 0; vector_index < 3; vector_index++) {
70 for (int dimension = 0; dimension < 3; dimension++) {
71 reciprocal_vectors_array[3 * vector_index + dimension] =
72 reciprocal_vectors_bohr[vector_index][dimension];
73 }
74 }
75 record_format.write_record(
76 output_file, reciprocal_vectors_array, sizeof(reciprocal_vectors_array));
77
78 /**
79 * Record 3: Miller indices — QE's mill_g(1:3, 1:ngm_g) is column-major
80 * with the component first, so components are interleaved per G-vector.
81 */
82 std::vector<int32_t> miller_indices(3 * number_of_g_vectors);
83 const auto& g_vectors = planewave_basis.g_vectors();
84 for (int g_index = 0; g_index < number_of_g_vectors; g_index++) {
85 miller_indices[3 * g_index + 0] = g_vectors[g_index].miller.x;
86 miller_indices[3 * g_index + 1] = g_vectors[g_index].miller.y;
87 miller_indices[3 * g_index + 2] = g_vectors[g_index].miller.z;
88 }
89 record_format.write_record(output_file,
90 miller_indices.data(),
91 static_cast<int32_t>(miller_indices.size() * sizeof(int32_t)));
92
93 // Record 4: charge_density_g(G) — complex*16 array
94 record_format.write_record(output_file,
95 charge_density_g.data(),
96 static_cast<int32_t>(number_of_g_vectors * sizeof(Complex)));
97
98 log::info(" Wrote charge density: {} ({} G-vectors)", filename, number_of_g_vectors);
99 }
100};
101
102} // namespace q3
Matrix3x3 reciprocal_vectors_bohr() const
Definition crystal.hpp:331
PlanewaveKohnShamChargeDensityCheckpoint(const NormConservingPlanewaveKohnShamSCFState &scf_state)
Definition init.hpp:17
const NormConservingPlanewaveKohnShamSCFState & scf_state_
Definition init.hpp:22
void write_record(std::ofstream &output_file, const void *data, int32_t number_of_bytes) const override
Definition qe.hpp:16
void write_to_file(const std::string &filename) const override
Write binary data to a file or directory.
Definition qe.hpp:27
void warn(spdlog::format_string_t< Args... > fmt, Args &&... args)
Definition logger.hpp:187
void error(spdlog::format_string_t< Args... > fmt, Args &&... args)
Definition logger.hpp:192
void info(spdlog::format_string_t< Args... > fmt, Args &&... args)
Definition logger.hpp:183
std::complex< double > Complex
3×3 matrix stored row-major: row i = mat[i]
const Crystal * crystal_structure
Definition init.hpp:55
const PlanewaveBasis * planewave_basis
Definition init.hpp:53
std::vector< Complex > charge_density_g
Definition init.hpp:36