Loading...
Searching...
No Matches
init.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * Mixer — abstract base class for charge-density mixing algorithms.
5 *
6 * Defines the interface for mixing input and output densities during
7 * the self-consistent field cycle. Concrete implementations (Anderson,
8 * Broyden) live in the real_space/ and reciprocal_space/ subdirectories.
9 */
10
11#include <functional>
12#include <memory>
13#include <string>
14#include <vector>
15
18
19namespace q3 {
20
21/**
22 * Everything a mixer might need from the surrounding representation.
23 *
24 * Real-space mixers ignore the reciprocal-space members; reciprocal-space
25 * mixers use the transforms and metric weights. Passing a single struct lets
26 * the iterator initialize any mixer without knowing its space.
27 */
29 std::function<void(const std::vector<Real>&, std::vector<Complex>&)> to_reciprocal_space;
30 std::function<void(const std::vector<Complex>&, std::vector<Real>&)> to_real_space;
31 std::vector<Real> charge_metric_weights;
32 /**
33 * Residual preconditioner, applied to the total-charge channel only. Never
34 * null: "no preconditioning" is a NoPreconditioner, not an absent pointer,
35 * so mixers call it unconditionally.
36 */
37 std::shared_ptr<Preconditioner> preconditioner;
40 bool is_gamma_only = false;
41 int number_of_smooth_g_vectors = 0; // For dual-grid mixing (QE-style)
42 std::function<void(std::vector<Real>&)> global_reduce;
43};
44
45/// Abstract base class for all charge density mixers.
46class Mixer {
47 public:
50 virtual ~Mixer() = default;
51
52 [[nodiscard]] virtual std::string name() const = 0;
53 virtual void reset() {}
54
55 /// Supply the representation context the mixer needs before the first mix().
56 virtual void initialize(const MixerEnvironment&) {}
57
58 /**
59 * Mix one self-consistent field step using real-space densities.
60 *
61 * @param input_density density used to build the potential (rho_in), per spin.
62 * @param output_density density from the bands (rho_out) on entry; overwritten
63 * with the mixed next-iteration density on exit, per spin.
64 * @param number_of_spin_components number of spin channels.
65 *
66 * Each mixer performs whatever internal transforms it needs (e.g. FFT to
67 * reciprocal space) so callers never branch on the mixer's space.
68 */
69 virtual void mix(const std::vector<std::vector<Real>>& input_density,
70 std::vector<std::vector<Real>>& output_density,
71 int number_of_spin_components) = 0;
72
73 [[nodiscard]] Real mixing_parameter() const {
74 return mixing_parameter_;
75 }
76 [[nodiscard]] int history_depth() const {
77 return history_depth_;
78 }
79
80 /**
81 * Return the density residual norm (dr2) computed during the last mix() call.
82 *
83 * Computing dr2 inside mix() avoids the FFT round-trip noise that occurs when
84 * the SCF loop independently FFTs real-space densities back to G-space.
85 * Returns -1.0 if not yet computed (callers should fall back to their own dr2).
86 */
87 [[nodiscard]] Real last_residual_norm() const {
89 }
90
91 protected:
95};
96
97} // namespace q3
Abstract base class for all charge density mixers.
Definition init.hpp:46
virtual std::string name() const =0
Mixer(Real mixing_parameter, int history_depth)
Definition init.hpp:48
int history_depth_
Definition init.hpp:93
int history_depth() const
Definition init.hpp:76
Real mixing_parameter_
Definition init.hpp:92
Real last_residual_norm() const
Definition init.hpp:87
virtual void reset()
Definition init.hpp:53
virtual ~Mixer()=default
Real last_residual_norm_
Definition init.hpp:94
virtual void mix(const std::vector< std::vector< Real > > &input_density, std::vector< std::vector< Real > > &output_density, int number_of_spin_components)=0
virtual void initialize(const MixerEnvironment &)
Supply the representation context the mixer needs before the first mix().
Definition init.hpp:56
Real mixing_parameter() const
Definition init.hpp:73
double Real
int first_nonzero_g_index
Definition init.hpp:39
Real magnetization_metric_weight
Definition init.hpp:38
std::function< void(const std::vector< Complex > &, std::vector< Real > &)> to_real_space
Definition init.hpp:30
int number_of_smooth_g_vectors
Definition init.hpp:41
std::shared_ptr< Preconditioner > preconditioner
Definition init.hpp:37
std::function< void(const std::vector< Real > &, std::vector< Complex > &)> to_reciprocal_space
Definition init.hpp:29
std::function< void(std::vector< Real > &)> global_reduce
Definition init.hpp:42
std::vector< Real > charge_metric_weights
Definition init.hpp:31