|
ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
|
Singleton class implementing spin-constrained DFT (DeltaSpin). More...
#include <spin_constrain.h>
Public Member Functions | |
| void | init_sc (double sc_thr_in, int nsc_in, int nsc_min_in, double alpha_trial_in, double sccut_in, double sc_drop_thr_in, const UnitCell &ucell, bool direction_only_in, Parallel_Orbitals *ParaV_in, int nspin_in, const K_Vectors &kv_in, void *p_hamilt_in, void *psi_in, elecstate::ElecState *pelec_in, ModulePW::PW_Basis_K *pw_wfc_in=nullptr) |
| Master initialization: populate all SC parameters from UnitCell and input. | |
| void | cal_mi_lcao (const int &step, bool print=false) |
| Calculate atomic magnetic moments using real-space projection (LCAO basis). | |
| void | cal_mw_from_lambda (int i_step, const ModuleBase::Vector3< double > *delta_lambda=nullptr) |
| Core workflow: apply lambda -> solve Hamiltonian -> compute magnetic moments. | |
| double | cal_escon () |
| Calculate the spin constraint energy contribution: E_scon = -sum(lambda_i . Mi_i). | |
| double | get_escon () const |
| Get the cached constraint energy from the last cal_escon() call (Ry). | |
| void | run_lambda_loop (int outer_step, bool rerun, std::ostream &ofs_running) |
| Main lambda optimization loop using conjugate-gradient-like scheme. | |
| double | get_last_rms_error () const |
| RMS error of the most recent lambda optimization loop (-1.0 if none has run). | |
| void | run_lambda_linear_scan (int outer_step, std::ostream &ofs_running) |
| Alternative mode: sweep lambda values linearly for energy landscape mapping. | |
| void | reset_dspin_operator () |
| Reset DeltaSpin operator initialization state when constraints change. | |
| void | update_psi_charge (const ModuleBase::Vector3< double > *delta_lambda, bool pw_solve=true, bool full_update=false) |
| Update wavefunctions and charge density after lambda optimization. | |
| SpinConstrain (SpinConstrain const &)=delete | |
| Delete copy and move constructors and assign operators. | |
| SpinConstrain (SpinConstrain &&)=delete | |
| void | set_atomCounts (const std::map< int, int > &atomCounts_in) |
| set element index to atom index map | |
| const std::map< int, int > & | get_atomCounts () const |
| get element index to atom index map | |
| void | set_orbitalCounts (const std::map< int, int > &orbitalCounts_in) |
| set element index to orbital index map | |
| const std::map< int, int > & | get_orbitalCounts () const |
| get element index to orbital index map | |
| void | set_lnchiCounts (const std::map< int, std::map< int, int > > &lnchiCounts_in) |
| set lnchiCounts | |
| const std::map< int, std::map< int, int > > & | get_lnchiCounts () const |
| get lnchiCounts | |
| void | set_sc_lambda () |
| set sc_lambda | |
| void | set_sc_lambda (const ModuleBase::Vector3< double > *lambda_in, int nat_in) |
| set sc_lambda from variable | |
| void | set_target_mag () |
| set target_mag | |
| void | set_target_mag (const ModuleBase::Vector3< double > *target_mag_in, int nat_in) |
| set target_mag from variable | |
| void | set_target_mag (const std::vector< ModuleBase::Vector3< double > > &target_mag_in) |
| set target magnetic moment | |
| void | set_constrain () |
| set constrain | |
| void | set_constrain (const ModuleBase::Vector3< int > *constrain_in, int nat_in) |
| set constrain from variable | |
| const std::vector< ModuleBase::Vector3< double > > & | get_sc_lambda () const |
| get sc_lambda | |
| const std::vector< ModuleBase::Vector3< double > > & | get_target_mag () const |
| get target_mag | |
| const std::vector< ModuleBase::Vector3< int > > & | get_constrain () const |
| get constrain | |
| int | get_nat () const |
| get nat | |
| int | get_ntype () const |
| get ntype | |
| void | check_atomCounts () const |
| check atomCounts | |
| int | get_iat (int itype, int atom_index) const |
| get iat | |
| void | set_nspin (int nspin) |
| set nspin | |
| int | get_nspin () const |
| get nspin | |
| void | zero_Mi () |
| zero atomic magnetic moment | |
| double | get_decay_grad (int itype) const |
| get decay_grad | |
| void | set_decay_grad () |
| set decay_grad | |
| const std::vector< double > & | get_decay_grad () const |
| get decay_grad | |
| void | set_decay_grad (const double *decay_grad_in, int ntype_in) |
| set decay_grad from variable | |
| void | set_sc_drop_thr (double sc_drop_thr_in) |
| set decay grad switch | |
| void | set_input_parameters (double sc_thr_in, int nsc_in, int nsc_min_in, double alpha_trial_in, double sccut_in, double sc_drop_thr_in) |
| set input parameters | |
| double | get_sc_thr () const |
| get sc_thr | |
| double | get_current_sc_thr () const |
| get current adaptive sc threshold (max(initial_rms * sc_drop_thr_, sc_thr_)) | |
| int | get_nsc () const |
| get nsc | |
| int | get_nsc_min () const |
| get nsc_min | |
| double | get_alpha_trial () const |
| get alpha_trial | |
| double | get_sccut () const |
| get sccut | |
| double | get_sc_drop_thr () const |
| get sc_drop_thr | |
| const std::vector< ModuleBase::Vector3< double > > & | get_mi () const |
| get computed magnetic moments Mi per atom | |
| const std::vector< std::string > & | get_atomLabels () const |
| get human-readable atom labels ("Fe_0", "Fe_1", ...) for table printing | |
| void | set_ParaV (Parallel_Orbitals *ParaV_in) |
| set orbital parallel info | |
| void | set_solver_parameters (const K_Vectors &kv_in, void *p_hamilt_in, void *psi_in, elecstate::ElecState *pelec_in) |
| set parameters for solver | |
| void | set_operator (hamilt::Operator< TK > *op_in) |
| Set DeltaSpin operator pointer for magnetic moment calculation (LCAO). | |
| void | set_mag_converged (bool is_Mi_converged_in) |
| Set magnetic moment convergence flag. | |
| bool | mag_converged () const |
| Get magnetic moment convergence flag. | |
| void | set_npol (int npol) |
| int | get_npol () const |
| int | get_nw () const |
| Total number of orbitals across all constrained atoms. | |
| int | get_iwt (int itype, int iat, int orbital_index) const |
| int | get_spin_sign (int ik) const |
| Get spin sign for k-point ik: +1 for spin-up, -1 for spin-down (nspin=2 only). | |
| void | cal_mw_from_lambda (int i_step, const ModuleBase::Vector3< double > *delta_lambda) |
| Core workflow: apply lambda -> solve Hamiltonian -> compute magnetic moments. | |
| void | update_psi_charge (const ModuleBase::Vector3< double > *delta_lambda, bool pw_solve, bool full_update) |
| Dispatcher: route to LCAO or PW (CPU/GPU) wavefunction/charge update. | |
| void | run_lambda_loop (int outer_step, bool rerun, std::ostream &ofs_running) |
| void | run_lambda_linear_scan (int outer_step, std::ostream &ofs_running) |
| void | cal_mw_from_lambda (int i_step, const ModuleBase::Vector3< double > *delta_lambda) |
| cal_mw_from_lambda stub (TK=double): no-op | |
| void | cal_mi_lcao (const int &step, bool print) |
| cal_mi_lcao stub (TK=double): no-op | |
| void | run_lambda_loop (int outer_step, bool rerun, std::ostream &ofs_running) |
| run_lambda_loop stub (TK=double): no-op | |
| void | run_lambda_linear_scan (int outer_step, std::ostream &ofs_running) |
| run_lambda_linear_scan stub (TK=double): no-op | |
| void | reset_dspin_operator () |
| reset_dspin_operator stub (TK=double): no-op | |
Static Public Member Functions | |
| static SpinConstrain & | getScInstance () |
| Public method to access the Singleton instance. | |
Public Attributes | |
| bool | higher_mag_prec = false |
| Use full PW solver (rerun) for higher precision in lambda loop. | |
| Parallel_Orbitals * | ParaV = nullptr |
| Parallel orbitals distribution (row/col mapping for ScaLAPACK). | |
| void * | p_hamilt = nullptr |
| Pointer to HamiltLCAO or HamiltPW. | |
| void * | psi = nullptr |
| Pointer to Psi<TK> wavefunction container. | |
| elecstate::ElecState * | pelec = nullptr |
| Electronic state: ekb, wg, charge, klist. | |
| ModulePW::PW_Basis_K * | pw_wfc_ = nullptr |
| PW basis for wavefunction storage (PW only). | |
| const double | meV_to_Ry = 7.349864435130999e-05 |
| Conversion factor. | |
| K_Vectors | kv_ |
| K-point vector list. | |
| ScState | state_ |
| Constraint parameters and runtime state (lambda, Mi, target_mag, indexing). | |
| pw::SubspaceCache | pw_cache_ |
Private Member Functions | |
| SpinConstrain () | |
| ~SpinConstrain ()=default | |
| SpinConstrain & | operator= (SpinConstrain const &)=delete |
| Copy assignment deleted. | |
| SpinConstrain & | operator= (SpinConstrain &&)=delete |
| Move assignment deleted. | |
Private Attributes | |
| hamilt::Operator< TK > * | p_operator = nullptr |
| DeltaSpin operator pointer for LCAO magnetic moment calculation. | |
| double | last_rms_error_ = -1.0 |
Singleton class implementing spin-constrained DFT (DeltaSpin).
|
delete |
Delete copy and move constructors and assign operators.
|
delete |
|
inlineprivate |
|
privatedefault |
|
inline |
Calculate the spin constraint energy contribution: E_scon = -sum(lambda_i . Mi_i).
Returns 0.0 if magnetic moments are not yet converged, because the constraint energy is only physically meaningful when Mi ≈ M_target. This energy is added to the total DFT energy in the SCF loop.
| void spinconstrain::SpinConstrain< double >::cal_mi_lcao | ( | const int & | step, |
| bool | print ) |
cal_mi_lcao stub (TK=double): no-op
| void spinconstrain::SpinConstrain< TK >::cal_mi_lcao | ( | const int & | step, |
| bool | print = false ) |
Calculate atomic magnetic moments using real-space projection (LCAO basis).
Uses the DeltaSpin operator to compute magnetic moments from the density matrix. For nspin=2, extracts only the z-component. For nspin=4, extracts all three components from the interleaved 4-component spinor density matrix. The moments are stored in ScState (indexed by global atom index iat).
| step | Current SCF iteration number (for logging) |
| Whether to print moments to ofs_running |
| void spinconstrain::SpinConstrain< std::complex< double > >::cal_mw_from_lambda | ( | int | i_step, |
| const ModuleBase::Vector3< double > * | delta_lambda ) |
Core workflow: apply lambda -> solve Hamiltonian -> compute magnetic moments.
| i_step | Current inner lambda step (-1 = initialization, 0+ = optimization) |
| delta_lambda | Change in lambda from previous step (unused in this function, the full lambda_ is used for H correction) |
Compute H(k) and extract subspace matrices for this k-point
| void spinconstrain::SpinConstrain< double >::cal_mw_from_lambda | ( | int | i_step, |
| const ModuleBase::Vector3< double > * | delta_lambda ) |
cal_mw_from_lambda stub (TK=double): no-op
| void spinconstrain::SpinConstrain< TK >::cal_mw_from_lambda | ( | int | i_step, |
| const ModuleBase::Vector3< double > * | delta_lambda = nullptr ) |
Core workflow: apply lambda -> solve Hamiltonian -> compute magnetic moments.
This is the central function called repeatedly during lambda optimization.
| i_step | Current inner lambda step (-1 = initialization, 0+ = optimization) |
| delta_lambda | Change in lambda from previous step (for incremental H correction) |
|
inline |
check atomCounts
|
inline |
get alpha_trial
|
inline |
get element index to atom index map
|
inline |
get human-readable atom labels ("Fe_0", "Fe_1", ...) for table printing
|
inline |
get constrain
|
inline |
get current adaptive sc threshold (max(initial_rms * sc_drop_thr_, sc_thr_))
|
inline |
get decay_grad
|
inline |
get decay_grad
|
inline |
Get the cached constraint energy from the last cal_escon() call (Ry).
|
inline |
get iat
|
inline |
Convert (itype, iat, iw) to global orbital index
|
inline |
RMS error of the most recent lambda optimization loop (-1.0 if none has run).
|
inline |
get lnchiCounts
|
inline |
get computed magnetic moments Mi per atom
|
inline |
get nat
|
inline |
|
inline |
get nsc
|
inline |
get nsc_min
|
inline |
get nspin
|
inline |
get ntype
|
inline |
Total number of orbitals across all constrained atoms.
|
inline |
get element index to orbital index map
|
inline |
get sc_drop_thr
|
inline |
get sc_lambda
|
inline |
get sc_thr
|
inline |
get sccut
| int spinconstrain::SpinConstrain< TK >::get_spin_sign | ( | int | ik | ) | const |
Get spin sign for k-point ik: +1 for spin-up, -1 for spin-down (nspin=2 only).
Get spin sign for k-point: determines whether this k-point is spin-up (+1) or spin-down (-1) in collinear (nspin=2) calculations.
In collinear spin, the wavefunction is split into two k-point pools:
|
inline |
get target_mag
|
static |
Public method to access the Singleton instance.
Singleton instance accessor.
pubic methods for setting and getting spin-constrained DFT parameters (thin forwarding shells to state_; kept for backward compatibility)
Uses Meyers' Singleton pattern (local static variable). Guaranteed thread-safe initialization in C++11 and later. Each template instantiation (complex<double>, double) gets its own instance.
| void spinconstrain::SpinConstrain< TK >::init_sc | ( | double | sc_thr_in, |
| int | nsc_in, | ||
| int | nsc_min_in, | ||
| double | alpha_trial_in, | ||
| double | sccut_in, | ||
| double | sc_drop_thr_in, | ||
| const UnitCell & | ucell, | ||
| bool | direction_only_in, | ||
| Parallel_Orbitals * | ParaV_in, | ||
| int | nspin_in, | ||
| const K_Vectors & | kv_in, | ||
| void * | p_hamilt_in, | ||
| void * | psi_in, | ||
| elecstate::ElecState * | pelec_in, | ||
| ModulePW::PW_Basis_K * | pw_wfc_in = nullptr ) |
Master initialization: populate all SC parameters from UnitCell and input.
Called once at the start of a DeltaSpin calculation. Performs:
| sc_thr_in | Convergence threshold for RMS(Mi - M_target) in uB |
| nsc_in | Maximum number of inner lambda optimization steps |
| nsc_min_in | Minimum number of inner steps before early exit checks |
| alpha_trial_in | Initial trial step size (eV/uB^2), converted to Ry internally |
| sccut_in | Maximum lambda change per step (eV/uB), converted to Ry internally |
| sc_drop_thr_in | Fraction of initial RMS for adaptive threshold |
| ucell | Unit cell with atomic positions, STRU constraint data |
| direction_only_in | If true, only optimize spin direction (|lambda| -> 0) |
| ParaV_in | Parallel orbitals distribution info (LCAO only) |
| nspin_in | Spin type: 2=collinear, 4=non-collinear |
| kv_in | K-point vector list |
| p_hamilt_in | Pointer to Hamiltonian (HamiltLCAO or HamiltPW) |
| psi_in | Pointer to wavefunctions (Psi<TK>) |
| dm_in | Pointer to density matrix (LCAO only) |
| pelec_in | Pointer to electronic state (for charge, weights, ekb) |
| pw_wfc_in | PW basis for wavefunction storage (PW only) |
|
inline |
Get magnetic moment convergence flag.
|
privatedelete |
Move assignment deleted.
|
privatedelete |
Copy assignment deleted.
| void spinconstrain::SpinConstrain< TK >::reset_dspin_operator | ( | ) |
Reset DeltaSpin operator initialization state when constraints change.
Reset DeltaSpin operator initialization state.
The DeltaSpin operator caches internal state (projector matrices, etc.) from a previous SCF iteration. When the constraint parameters change (e.g., new target moments or lambda values), the cached state may be invalid. This function forces the operator to reinitialize on the next call.
| void spinconstrain::SpinConstrain< double >::reset_dspin_operator | ( | ) |
reset_dspin_operator stub (TK=double): no-op
| void spinconstrain::SpinConstrain< std::complex< double > >::run_lambda_linear_scan | ( | int | outer_step, |
| std::ostream & | ofs_running ) |
| void spinconstrain::SpinConstrain< TK >::run_lambda_linear_scan | ( | int | outer_step, |
| std::ostream & | ofs_running ) |
Alternative mode: sweep lambda values linearly for energy landscape mapping.
Used for debugging or plotting E(lambda) curves. Scans from sc_scan_lambda_start to sc_scan_lambda_end in sc_scan_steps steps. Results written to lambda_scan_results.dat.
| outer_step | Current SCF outer iteration number |
| void spinconstrain::SpinConstrain< double >::run_lambda_linear_scan | ( | int | outer_step, |
| std::ostream & | ofs_running ) |
run_lambda_linear_scan stub (TK=double): no-op
| void spinconstrain::SpinConstrain< std::complex< double > >::run_lambda_loop | ( | int | outer_step, |
| bool | rerun, | ||
| std::ostream & | ofs_running ) |
< Lambda with unconstrained components = 0
< Current lambda change from initial
< Cumulative step, previous step
< Temporary workspace
< Current Mi, residual (Mi - M_target)
< Search direction, previous direction
< Mi at current and trial lambda
< Optimal step size, correction to trial
< Polak-Ribiere conjugate gradient parameter
< Mean squared error, RMS error
< Adaptation factor for alpha_trial
< Current trial step size (Ry/uB^2)
| void spinconstrain::SpinConstrain< TK >::run_lambda_loop | ( | int | outer_step, |
| bool | rerun, | ||
| std::ostream & | ofs_running ) |
Main lambda optimization loop using conjugate-gradient-like scheme.
Iteratively adjusts Lagrange multipliers (lambda) to drive atomic magnetic moments (Mi) toward target values. Uses:
| outer_step | Current SCF outer iteration number |
| rerun | If true, use full PW solver for final charge update |
| void spinconstrain::SpinConstrain< double >::run_lambda_loop | ( | int | outer_step, |
| bool | rerun, | ||
| std::ostream & | ofs_running ) |
run_lambda_loop stub (TK=double): no-op
|
inline |
set element index to atom index map
|
inline |
set constrain
|
inline |
set constrain from variable
|
inline |
set decay_grad
|
inline |
set decay_grad from variable
|
inline |
set input parameters
|
inline |
set lnchiCounts
|
inline |
Set magnetic moment convergence flag.
|
inline |
|
inline |
set nspin
| void spinconstrain::SpinConstrain< TK >::set_operator | ( | hamilt::Operator< TK > * | op_in | ) |
Set DeltaSpin operator pointer for magnetic moment calculation (LCAO).
| op_in | Base pointer, actual type is DeltaSpin<OperatorLCAO<TK, TR>>* |
|
inline |
set element index to orbital index map
| void spinconstrain::SpinConstrain< TK >::set_ParaV | ( | Parallel_Orbitals * | ParaV_in | ) |
set orbital parallel info
set ParaV
|
inline |
set decay grad switch
|
inline |
set sc_lambda
|
inline |
set sc_lambda from variable
| void spinconstrain::SpinConstrain< TK >::set_solver_parameters | ( | const K_Vectors & | kv_in, |
| void * | p_hamilt_in, | ||
| void * | psi_in, | ||
| elecstate::ElecState * | pelec_in ) |
set parameters for solver
|
inline |
set target_mag
|
inline |
set target_mag from variable
|
inline |
set target magnetic moment
| void spinconstrain::SpinConstrain< std::complex< double > >::update_psi_charge | ( | const ModuleBase::Vector3< double > * | delta_lambda, |
| bool | pw_solve, | ||
| bool | full_update ) |
Dispatcher: route to LCAO or PW (CPU/GPU) wavefunction/charge update.
For LCAO: simply calls psiToRho() since the Hamiltonian already includes the DeltaSpin correction. For PW: calls update_psi_charge_pw_cpu or update_psi_charge_pw_gpu which perform subspace diagonalization and optional full-space refinement.
| void spinconstrain::SpinConstrain< TK >::update_psi_charge | ( | const ModuleBase::Vector3< double > * | delta_lambda, |
| bool | pw_solve = true, | ||
| bool | full_update = false ) |
Update wavefunctions and charge density after lambda optimization.
Dispatcher to LCAO or PW (CPU/GPU) update paths. For PW: performs subspace diagonalization + optional full-space refinement.
| delta_lambda | Lambda change for incremental H correction |
| pw_solve | If true, run full PW solver for refinement; if false, just update weights |
| full_update | If true, apply full lambda (not delta) to H correction |
|
inline |
zero atomic magnetic moment
| bool spinconstrain::SpinConstrain< TK >::higher_mag_prec = false |
Use full PW solver (rerun) for higher precision in lambda loop.
Lambda loop helpers (print_rms_stop, check_restriction, check_gradient_decay, cal_alpha_opt, print_header, print_termination) have been lifted to free functions in lambda_loop_helper.h. The class now only carries state and the core lambda-loop driver (run_lambda_loop). print_Mi and print_Mag_Force have also been lifted to lambda_loop_helper.h.
| K_Vectors spinconstrain::SpinConstrain< TK >::kv_ |
K-point vector list.
|
private |
RMS error of the most recent lambda optimization loop; -1.0 if no loop has run. Used by ESolver to pass the current DeltaSpin RMS into the SCF iteration table.
| const double spinconstrain::SpinConstrain< TK >::meV_to_Ry = 7.349864435130999e-05 |
Conversion factor.
| void* spinconstrain::SpinConstrain< TK >::p_hamilt = nullptr |
Pointer to HamiltLCAO or HamiltPW.
|
private |
DeltaSpin operator pointer for LCAO magnetic moment calculation.
| Parallel_Orbitals* spinconstrain::SpinConstrain< TK >::ParaV = nullptr |
Parallel orbitals distribution (row/col mapping for ScaLAPACK).
| elecstate::ElecState* spinconstrain::SpinConstrain< TK >::pelec = nullptr |
Electronic state: ekb, wg, charge, klist.
| void* spinconstrain::SpinConstrain< TK >::psi = nullptr |
Pointer to Psi<TK> wavefunction container.
| pw::SubspaceCache spinconstrain::SpinConstrain< TK >::pw_cache_ |
| ModulePW::PW_Basis_K* spinconstrain::SpinConstrain< TK >::pw_wfc_ = nullptr |
PW basis for wavefunction storage (PW only).
| ScState spinconstrain::SpinConstrain< TK >::state_ |
Constraint parameters and runtime state (lambda, Mi, target_mag, indexing).