Loading...
Searching...
No Matches
field.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * BerkeleyGWFieldFile — one scalar field on the charge-density G-set, per spin:
5 * the layout BerkeleyGW's RHO, VXC and VSC files share.
6 *
7 * Layout (the BerkeleyGW specification's RHO and VXC files; BerkeleyGW 4.0's
8 * ParaBands reads VSC with the same routines, accepting the title "VSC" or the
9 * legacy "VXC" -- `MeanField/ParaBands/parabands.f90`):
10 * records 1-8: BerkeleyGWCommonHeader (include_kpoint_fields=false)
11 * body: [nrecord=1][ng][gvec[3,ng]] then [nrecord=1][ng][field[ng,ns]]
12 *
13 * The field is written one spin channel after another, G fastest, each value
14 * multiplied by `scale`: the cell volume for RHO, whose G = 0 component the
15 * specification makes the electron count; 1 for the potentials, whose G = 0
16 * component is their cell average, in Rydberg.
17 */
18
19#include <fstream>
20#include <stdexcept>
21#include <string>
22#include <utility>
23#include <vector>
24
28
29namespace q3 {
30
31/// One per-spin field on the charge-density G-set, in RHO's layout; see the file comment.
33 public:
34 /**
35 * @param title "RHO-Complex", "VXC-Complex" or "VSC-Complex".
36 * @param values_per_spin one array per spin channel, each parallel to the
37 * record's charge-density G-set; there must be
38 * exactly `number_of_spin_components` of them.
39 */
41 std::string title,
42 const std::vector<std::vector<Complex>>& values_per_spin,
43 Real scale)
44 : record_(record)
45 , title_(std::move(title))
46 , values_per_spin_(values_per_spin)
47 , scale_(scale) {}
48 /// See the file writers' own deleted rvalue constructors: this holds references.
50 std::string,
51 const std::vector<std::vector<Complex>>&,
52 Real) = delete;
53
54 /// Writes header, G-set and field to an open stream; throws if the field is not on the G-set.
55 void write(std::ofstream& output_file) const {
56 int number_of_g_vectors = record_.number_of_g_vectors();
57 int number_of_spin_components = record_.number_of_spin_components;
58 if (static_cast<int>(values_per_spin_.size()) != number_of_spin_components) {
59 throw std::runtime_error(
60 title_ + ": the field has " + std::to_string(values_per_spin_.size()) +
61 " spin channel(s), the record " + std::to_string(number_of_spin_components));
62 }
63 for (const auto& values : values_per_spin_) {
64 if (static_cast<int>(values.size()) != number_of_g_vectors) {
65 throw std::runtime_error(title_ + ": the field has " +
66 std::to_string(values.size()) +
67 " G-vector components, the charge-density G-set " +
68 std::to_string(number_of_g_vectors));
69 }
70 }
71
72 BerkeleyGWCommonHeader header(record_, title_, /*include_kpoint_fields=*/false);
73 header.write(output_file);
74
75 BerkeleyGWRecordWriter record_writer(record_);
76 record_writer.write_charge_density_gvectors(output_file);
77
78 std::vector<Complex> scaled_values;
79 scaled_values.reserve(static_cast<size_t>(number_of_g_vectors) * number_of_spin_components);
80 for (const auto& values : values_per_spin_) {
81 for (const Complex& value : values) {
82 scaled_values.push_back(value * scale_);
83 }
84 }
85 record_writer.write_complex_block(
86 output_file, number_of_g_vectors, scaled_values.data(), scaled_values.size());
87 }
88
89 private:
90 const MeanFieldRecord& record_;
91 std::string title_;
92 const std::vector<std::vector<Complex>>& values_per_spin_;
93 Real scale_;
94};
95
96} // namespace q3
void write(std::ofstream &output_file) const
Write records 1 through 8 to output_file.
Definition header.hpp:256
One per-spin field on the charge-density G-set, in RHO's layout; see the file comment.
Definition field.hpp:32
BerkeleyGWFieldFile(const MeanFieldRecord &&, std::string, const std::vector< std::vector< Complex > > &, Real)=delete
See the file writers' own deleted rvalue constructors: this holds references.
void write(std::ofstream &output_file) const
Writes header, G-set and field to an open stream; throws if the field is not on the G-set.
Definition field.hpp:55
BerkeleyGWFieldFile(const MeanFieldRecord &record, std::string title, const std::vector< std::vector< Complex > > &values_per_spin, Real scale)
Definition field.hpp:40
Writes the records BerkeleyGW's files share; see the file comment.
void write_charge_density_gvectors(std::ofstream &output_file) const
[nrecord=1][ng][gvec(3,ng)] – the charge-density G-set, in the record's order.
void write_complex_block(std::ofstream &output_file, int32_t count, const Complex *values, size_t number_of_values) const
[nrecord=1][count][values] – one data block of count complex values per spin.
double Real
std::complex< double > Complex
int number_of_spin_components
Definition init.hpp:127
int number_of_g_vectors() const
Definition init.hpp:192