Loading...
Searching...
No Matches
init.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * PlanewaveChargeDensity — planewave representation of charge density.
5 *
6 * Stores spin-resolved charge density on real-space FFT grids with
7 * reciprocal-space representation capability. This is a data container;
8 * computation is done by ChargeDensityCalculator subclasses.
9 */
10
11#include <algorithm>
12#include <cmath>
13#include <vector>
14
20
21namespace q3 {
22
23/// Type aliases for charge density storage
24using SpinResolvedRealSpaceGrid = std::vector<std::vector<Real>>;
25using ReciprocalSpaceCoefficients = std::vector<Complex>;
26
27/// Planewave charge density — stores ρ(r) on FFT grid.
29 : public ChargeDensity,
30 public FFTransformable<SpinResolvedRealSpaceGrid, ReciprocalSpaceCoefficients> {
31 public:
34 : basis_(&basis) {
36 int grid_points = basis.fft_rho().total_grid_points();
38 for (int spin_index = 0; spin_index < number_of_spin_components; spin_index++) {
39 real_space_data_[spin_index].assign(grid_points, 0.0);
40 }
42 }
43
44 // ─── ChargeDensity interface ────────────────────────────────
45 [[nodiscard]] Real get_total_charge() const override {
46 if (!basis_ || cell_volume_ < math::tolerances::cell_volume_threshold)
47 return 0.0;
48 int grid_points = basis_->fft_rho().total_grid_points();
49 Real charge = 0.0;
50 for (int spin_index = 0; spin_index < number_of_spin_components_; spin_index++) {
51 for (int grid_point_index = 0; grid_point_index < grid_points; grid_point_index++) {
52 charge += real_space_data_[spin_index][grid_point_index];
53 }
54 }
55 return charge * cell_volume_ / grid_points;
56 }
57 void reset() override {
58 int grid_points = basis_ ? basis_->fft_rho().total_grid_points() : 0;
59 for (int spin_index = 0; spin_index < number_of_spin_components_; spin_index++) {
60 real_space_data_[spin_index].assign(grid_points, 0.0);
61 }
62 if (!reciprocal_space_data_.empty()) {
63 std::fill(
64 reciprocal_space_data_.begin(), reciprocal_space_data_.end(), Complex(0.0, 0.0));
65 }
66 }
67
68 // ─── Spin-resolved access ───────────────────────────────────
69 std::vector<Real>& real_space_by_spin_index(int spin_index) {
70 return real_space_data_[spin_index];
71 }
72 [[nodiscard]] const std::vector<Real>& real_space_by_spin_index(int spin_index) const {
73 return real_space_data_[spin_index];
74 }
75
76 // ─── FFT transforms ────────────────────────────────────────
77 void to_reciprocal_space() override {
78 // FFT total density to G-space
79 int grid_points = basis_->fft_rho().total_grid_points();
80 std::vector<Real> total_density(grid_points, 0.0);
81 for (int spin_index = 0; spin_index < number_of_spin_components_; spin_index++) {
82 for (int grid_point_index = 0; grid_point_index < grid_points; grid_point_index++) {
83 total_density[grid_point_index] += real_space_data_[spin_index][grid_point_index];
84 }
85 }
86 basis_->rho_r_to_g(total_density, reciprocal_space_data_);
87 }
88 void to_real_space() override {
89 // FFT total density from G-space to R-space (only for single spin)
92 }
93 }
94
95 // ─── Band-by-band accumulation ─────────────────────────────
96 void add_band_contribution(int spin_index,
97 Real weight_times_occupation,
98 const Complex* psi_real_space,
99 int real_space_grid_size,
100 Real inverse_cell_volume) {
101 for (int grid_point_index = 0; grid_point_index < real_space_grid_size;
102 grid_point_index++) {
103 real_space_data_[spin_index][grid_point_index] +=
104 weight_times_occupation * std::norm(psi_real_space[grid_point_index]) *
105 inverse_cell_volume;
106 }
107 }
108
109 // ─── Smooth copy management ────────────────────────────────
111 smooth_real_space_data_ = real_space_data_;
112 }
114 return smooth_real_space_data_;
115 }
117 return smooth_real_space_data_;
118 }
119
120 // ─── MPI ───────────────────────────────────────────────────
121
122 // ─── Basis access ──────────────────────────────────────────
123 [[nodiscard]] const PlanewaveBasis& basis() const {
124 return *basis_;
125 }
126 [[nodiscard]] int grid_size() const {
127 return basis_ ? basis_->fft_rho().total_grid_points() : 0;
128 }
129 [[nodiscard]] Real cell_volume() const {
130 return cell_volume_;
131 }
132 void set_cell_volume(Real volume) {
133 cell_volume_ = volume;
134 }
135
136 private:
137 const PlanewaveBasis* basis_ = nullptr;
138 Real cell_volume_ = 0.0;
139 SpinResolvedRealSpaceGrid smooth_real_space_data_;
140};
141
142} // namespace q3
Abstract charge density — a scalar field property of a quantum system.
virtual int total_grid_points() const =0
int number_of_g_vectors() const
Definition init.hpp:145
void rho_g_to_r(const std::vector< Complex > &charge_density_g_space, std::vector< Real > &charge_density_real_space) const
Definition init.hpp:328
void rho_r_to_g(const std::vector< Real > &charge_density_real_space, std::vector< Complex > &charge_density_g_space) const
Definition init.hpp:334
FFTGrid & fft_rho()
Definition init.hpp:286
Planewave charge density — stores ρ(r) on FFT grid.
Definition init.hpp:30
SpinResolvedRealSpaceGrid & smooth_real_space_data()
Definition init.hpp:116
void add_band_contribution(int spin_index, Real weight_times_occupation, const Complex *psi_real_space, int real_space_grid_size, Real inverse_cell_volume)
Definition init.hpp:96
const std::vector< Real > & real_space_by_spin_index(int spin_index) const
Definition init.hpp:72
PlanewaveChargeDensity(const PlanewaveBasis &basis, int number_of_spin_components)
Definition init.hpp:33
void set_cell_volume(Real volume)
Definition init.hpp:132
Real cell_volume() const
Definition init.hpp:129
const PlanewaveBasis & basis() const
Definition init.hpp:123
void reset() override
Definition init.hpp:57
std::vector< Real > & real_space_by_spin_index(int spin_index)
Definition init.hpp:69
void to_reciprocal_space() override
Definition init.hpp:77
Real get_total_charge() const override
Definition init.hpp:45
void to_real_space() override
Definition init.hpp:88
const SpinResolvedRealSpaceGrid & smooth_real_space_data() const
Definition init.hpp:113
constexpr double cell_volume_threshold
std::vector< std::vector< Real > > SpinResolvedRealSpaceGrid
Type aliases for charge density storage.
Definition init.hpp:24
double Real
std::vector< Complex > ReciprocalSpaceCoefficients
Definition init.hpp:25
std::complex< double > Complex