Loading...
Searching...
No Matches
init.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * PlanewaveHamiltonianApplicator — abstract interface for H|ψ⟩ application.
5 *
6 * Represents the full Kohn-Sham Hamiltonian application in the planewave
7 * representation: H|ψ⟩ = (T + V_local + V_NL)|ψ⟩.
8 *
9 * Lifecycle:
10 * 1. Construct with run-constant context (crystal, basis, pseudopotentials, FFT grid)
11 * 2. Call update_potential() at each SCF iteration (computes smooth-grid potential)
12 * 3. Call prepare_kpoint() once per k-point (sets up FFT mappings, KB projectors)
13 * 4. Call apply_batch() per Davidson iteration (lean H|ψ⟩ application)
14 *
15 * GPU implementations override to use GPU BLAS, GPU FFT, and device memory.
16 */
17
18#include <complex>
19#include <stdexcept>
20#include <vector>
21
24
25namespace q3 {
26
28 public:
30
31 /**
32 * Update the effective potential for a new SCF iteration.
33 * Must be called before prepare_kpoint() / apply_batch().
34 *
35 * Internally computes the smooth-grid potential (FFT → filter → inverse FFT)
36 * and caches the result. For GPU implementations, copies the smooth potential
37 * to a GPUDeviceBuffer.
38 */
39 virtual void update_potential(const std::vector<Real>& real_space_potential) = 0;
40
41 /**
42 * Configure for a specific k-point. Must be called before apply_batch().
43 * Sets up k+G vectors, kinetic factors, KB projectors, FFT mappings.
44 * For GPU implementations, copies per-kpoint data to GPUDeviceBuffer.
45 */
46 virtual void prepare_kpoint(int kpoint_index,
47 int number_of_plane_waves,
48 const std::vector<int>& g_vector_indices,
49 const std::vector<Real>& kinetic_energy_squared) = 0;
50
51 /**
52 * Apply H|ψ⟩ in double precision (FP64) to a batch of vectors.
53 *
54 * Only takes data that changes per Davidson iteration: the wavefunctions
55 * and their output. All other context (potential, k-point data, projectors)
56 * was configured via update_potential() and prepare_kpoint().
57 */
58 virtual void apply_batch(const Complex* input_wavefunctions,
59 Complex* output_hamiltonian_wavefunctions,
60 int number_of_vectors) = 0;
61
62 /**
63 * Which spin channel the following applications belong to.
64 *
65 * The nonlocal D matrix is spin-dependent for ultrasoft and PAW —
66 * D_ij = D_ion + ∫v_eff·Q_ij, and v_eff differs between channels — so
67 * applying H needs to know which channel it is in. The caller sets this
68 * beside `update_potential`, which already receives that channel's
69 * potential; the cached dense D is dropped whenever it changes, since it
70 * was built for the other channel.
71 *
72 * Unpolarized runs never call it and stay on channel 0.
73 */
80
81 [[nodiscard]] int spin_channel() const {
82 return spin_channel_;
83 }
84
85 /**
86 * Invalidate any cached smooth potential.
87 * Called when the effective potential changes (e.g., at start of each SCF iteration).
88 */
90
91 /**
92 * Whether the smooth potential cache is currently valid.
93 * Default: false (conservative — forces recomputation).
94 */
95 virtual bool is_smooth_potential_cache_valid() const {
96 return false;
97 }
98
99 /**
100 * Whether the current k-point uses gamma-only half-sphere G-vectors.
101 * Default: false.
102 */
103 virtual bool is_gamma_only_current() const {
104 return false;
105 }
106
107 /**
108 * Whether this rank's plane-wave list for the current k-point begins with
109 * G = 0, so that the gamma trick's origin correction belongs to it. True
110 * whenever the coefficients are replicated; under a reciprocal-space
111 * distribution exactly one rank answers true. Meaningful only when
112 * is_gamma_only_current().
113 */
114 virtual bool gamma_origin_is_local_current() const {
115 return true;
116 }
117
118 /**
119 * Get pre-computed KB projectors for a specific k-point, or nullptr if none.
120 * Default: nullptr (no cached projectors).
121 */
122 virtual const q3::Matrix<Complex>* get_cached_projectors(int kpoint_index) const {
123 (void)kpoint_index;
124 return nullptr;
125 }
126
127 /**
128 * Set pre-computed KB projectors for a specific k-point.
129 * Default: no-op (implementations that cache projectors override this).
130 */
131 virtual void set_cached_projectors(int kpoint_index, const q3::Matrix<Complex>* projectors) {
132 (void)kpoint_index;
133 (void)projectors;
134 }
135
136 /**
137 * Set pre-computed DFT+U (Hubbard) atomic-orbital projectors for a specific k-point.
138 * Geometry-only (like KB projectors): set once per k-point at SCF setup.
139 * Default: no-op -- only the CPU serial applicator implements DFT+U (v1: NC + CPU only).
140 */
141 virtual void set_cached_hubbard_projectors(int kpoint_index,
142 const q3::Matrix<Complex>* projectors) {
143 (void)kpoint_index;
144 (void)projectors;
145 }
146
147 /**
148 * Set the dense block-diagonal Hubbard D-matrix (Dudarev V_U coefficients), spanning all
149 * Hubbard-active orbitals across all manifolds. Column-major (Fortran/BLAS convention),
150 * dimension total_hubbard_orbitals x total_hubbard_orbitals. Unlike the KB D-matrix (fixed,
151 * lazily built from geometry), this depends on the occupation matrix and is refreshed once
152 * per SCF iteration (not per k-point). Default: no-op.
153 */
154 virtual void set_hubbard_d_matrix(const std::vector<Complex>* dense_d_matrix_column_major) {
155 (void)dense_d_matrix_column_major;
156 }
157
158 protected:
159 /// Spin channel the current potential and D belong to; 0 when unpolarized.
161};
162
163} // namespace q3
void set_spin_channel(int spin_channel)
Definition init.hpp:74
virtual void set_cached_projectors(int kpoint_index, const q3::Matrix< Complex > *projectors)
Definition init.hpp:131
virtual void apply_batch(const Complex *input_wavefunctions, Complex *output_hamiltonian_wavefunctions, int number_of_vectors)=0
virtual ~PlanewaveHamiltonianApplicator()=default
virtual void prepare_kpoint(int kpoint_index, int number_of_plane_waves, const std::vector< int > &g_vector_indices, const std::vector< Real > &kinetic_energy_squared)=0
int spin_channel_
Spin channel the current potential and D belong to; 0 when unpolarized.
Definition init.hpp:160
virtual const q3::Matrix< Complex > * get_cached_projectors(int kpoint_index) const
Definition init.hpp:122
virtual void invalidate_smooth_potential_cache()=0
virtual bool is_gamma_only_current() const
Definition init.hpp:103
virtual void set_cached_hubbard_projectors(int kpoint_index, const q3::Matrix< Complex > *projectors)
Definition init.hpp:141
virtual void update_potential(const std::vector< Real > &real_space_potential)=0
virtual bool gamma_origin_is_local_current() const
Definition init.hpp:114
virtual bool is_smooth_potential_cache_valid() const
Definition init.hpp:95
virtual void set_hubbard_d_matrix(const std::vector< Complex > *dense_d_matrix_column_major)
Definition init.hpp:154
std::complex< double > Complex