37 log::error(
"planewave_basis is null in NormConservingPlanewaveKohnShamSCFState during "
38 "wavefunction serialization");
42 log::error(
"execution_policy is null in NormConservingPlanewaveKohnShamSCFState during "
43 "wavefunction serialization");
48 const auto& g_vectors = planewave_basis.g_vectors();
50 const auto& g_vector_indices_all = planewave_basis.kpoint_g_vector_indices();
51 const auto& number_of_plane_waves_all = planewave_basis.kpoint_number_of_plane_waves();
58 for (
int kpoint_index = 0; kpoint_index < number_of_kpoints; kpoint_index++) {
59 const int global_kpoint_index = execution_policy.global_kpoint_index(kpoint_index);
61 std::string file_path =
62 path +
"/wfc" + std::to_string(global_kpoint_index + 1) +
".dat";
64 std::vector<char> file_buffer(4 * 1024 * 1024);
65 std::ofstream output_file;
66 output_file.rdbuf()->pubsetbuf(file_buffer.data(), file_buffer.size());
67 output_file.open(file_path, std::ios::binary);
68 if (!output_file.is_open()) {
69 log::warn(
"Cannot open {} for writing", file_path);
73 int number_of_plane_waves = number_of_plane_waves_all[kpoint_index];
74 const auto& g_vector_indices = g_vector_indices_all[kpoint_index];
77 write_kpoint_header(output_file, global_kpoint_index, kpoint);
78 write_dimensions(output_file, number_of_plane_waves, number_of_bands);
79 write_reciprocal_lattice_vectors(output_file, reciprocal_lattice_vectors);
80 write_miller_indices(output_file, g_vectors, g_vector_indices, number_of_plane_waves);
81 write_band_coefficients(
82 output_file, quantum_system, kpoint_index, number_of_bands, number_of_plane_waves);
84 log::info(
" Wrote wavefunction: {} ({} bands, {} PW)",
87 number_of_plane_waves);
110 write_kpoint_header(std::ofstream& output_file,
int kpoint_index,
const KPoint& kpoint)
const {
111 const Vector3D kpoint_cartesian = kpoint_cartesian_inverse_bohr(kpoint);
113 record.
append(
static_cast<int32_t
>(kpoint_index + 1));
114 record.
append(
static_cast<double>(kpoint_cartesian.
x));
115 record.
append(
static_cast<double>(kpoint_cartesian.
y));
116 record.
append(
static_cast<double>(kpoint_cartesian.
z));
117 record.
append(
static_cast<int32_t
>(1));
121 output_file, record.
data(),
static_cast<int32_t
>(record.
size()));
128 [[nodiscard]] Vector3D kpoint_cartesian_inverse_bohr(
const KPoint& kpoint)
const {
131 for (
int dimension = 0; dimension < 3; dimension++) {
132 cartesian[dimension] = kpoint.coordinates.x * reciprocal_vectors_bohr[0][dimension] +
133 kpoint.coordinates.y * reciprocal_vectors_bohr[1][dimension] +
134 kpoint.coordinates.z * reciprocal_vectors_bohr[2][dimension];
142 void write_dimensions(std::ofstream& output_file,
143 int number_of_plane_waves,
144 int number_of_bands)
const {
145 struct DimensionsRecord {
146 int32_t number_of_plane_waves_wavefunction;
147 int32_t maximum_wavefunction_index;
148 int32_t number_of_polarizations;
149 int32_t number_of_bands;
158 DimensionsRecord record;
159 std::memset(&record, 0,
sizeof(record));
160 record.number_of_plane_waves_wavefunction = number_of_plane_waves;
161 record.maximum_wavefunction_index = number_of_plane_waves;
162 record.number_of_polarizations = 1;
163 record.number_of_bands = number_of_bands;
164 record_format_.
write_record(output_file, &record,
sizeof(record));
170 void write_reciprocal_lattice_vectors(std::ofstream& output_file,
171 const Matrix3x3& reciprocal_lattice_vectors)
const {
172 double vectors_array[9];
173 vectors_array[0] = reciprocal_lattice_vectors[0].x;
174 vectors_array[1] = reciprocal_lattice_vectors[0].y;
175 vectors_array[2] = reciprocal_lattice_vectors[0].z;
176 vectors_array[3] = reciprocal_lattice_vectors[1].x;
177 vectors_array[4] = reciprocal_lattice_vectors[1].y;
178 vectors_array[5] = reciprocal_lattice_vectors[1].z;
179 vectors_array[6] = reciprocal_lattice_vectors[2].x;
180 vectors_array[7] = reciprocal_lattice_vectors[2].y;
181 vectors_array[8] = reciprocal_lattice_vectors[2].z;
182 record_format_.
write_record(output_file, vectors_array,
sizeof(vectors_array));
192 void write_miller_indices(std::ofstream& output_file,
193 const std::vector<GVector>& g_vectors,
194 const std::vector<int>& g_vector_indices,
195 int number_of_plane_waves)
const {
196 std::vector<int32_t> miller_indices(3 * number_of_plane_waves);
197 for (
int plane_wave_index = 0; plane_wave_index < number_of_plane_waves;
198 plane_wave_index++) {
199 int g_index = g_vector_indices[plane_wave_index];
200 miller_indices[3 * plane_wave_index + 0] = g_vectors[g_index].miller.x;
201 miller_indices[3 * plane_wave_index + 1] = g_vectors[g_index].miller.y;
202 miller_indices[3 * plane_wave_index + 2] = g_vectors[g_index].miller.z;
205 miller_indices.data(),
206 static_cast<int32_t
>(miller_indices.size() *
sizeof(int32_t)));
212 void write_band_coefficients(std::ofstream& output_file,
213 const PlanewaveKohnShamQuantumSystem& quantum_system,
216 int number_of_plane_waves)
const {
217 for (
int band_index = 0; band_index < number_of_bands; band_index++) {
219 quantum_system.wavefunction_band_data(kpoint_index, band_index);
223 static_cast<int32_t
>(number_of_plane_waves *
sizeof(
Complex)));