ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
spinconstrain::SpinConstrain< TK > Class Template Reference

Singleton class implementing spin-constrained DFT (DeltaSpin). More...

#include <spin_constrain.h>

Collaboration diagram for spinconstrain::SpinConstrain< TK >:

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

Detailed Description

template<typename TK>
class spinconstrain::SpinConstrain< TK >

Singleton class implementing spin-constrained DFT (DeltaSpin).

Template parameter TK
  • std::complex<double>: Used for nspin=4 (non-collinear) and internally for nspin=2
  • double: Stub specialization for nspin=2 collinear (all methods are no-ops)
Design rationale
  • Singleton pattern: Only one SpinConstrain instance per TK type is needed, shared across the SCF loop. Prevents duplicate state management.
  • void* pointers (p_hamilt, psi): Type-erased to avoid template dependency cycles with the Hamiltonian and Psi classes. Cast to concrete types at call sites.
  • subspace data caching (sub_h_save, sub_s_save, becp_save): For PW basis, the subspace Hamiltonian and becp are computed once per SCF iteration and reused across multiple lambda steps, avoiding expensive re-computation.
Key workflow (PW basis):
SCF iteration -> run_lambda_loop() -> cal_mw_from_lambda() [first call saves subspace data] -> calculate_delta_hcc() [H += becp^† * lambda * becp] -> diag_responce() [subspace diagonalization, update becp] -> accumulate_Mi_from_becp() [compute magnetic moments] -> BFGS optimizer updates lambda -> Repeat until RMS(Mi - M_target) < sc_thr -> update_psi_charge() [final full-space update]
Error handling

Constructor & Destructor Documentation

◆ SpinConstrain() [1/3]

template<typename TK>
spinconstrain::SpinConstrain< TK >::SpinConstrain ( SpinConstrain< TK > const & )
delete

Delete copy and move constructors and assign operators.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SpinConstrain() [2/3]

template<typename TK>
spinconstrain::SpinConstrain< TK >::SpinConstrain ( SpinConstrain< TK > && )
delete
Here is the call graph for this function:

◆ SpinConstrain() [3/3]

template<typename TK>
spinconstrain::SpinConstrain< TK >::SpinConstrain ( )
inlineprivate

◆ ~SpinConstrain()

template<typename TK>
spinconstrain::SpinConstrain< TK >::~SpinConstrain ( )
privatedefault

Member Function Documentation

◆ cal_escon()

template<typename TK>
double spinconstrain::SpinConstrain< TK >::cal_escon ( )
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.

Returns
Constraint energy in Ry (0.0 if not converged)
Here is the caller graph for this function:

◆ cal_mi_lcao() [1/2]

void spinconstrain::SpinConstrain< double >::cal_mi_lcao ( const int & step,
bool print )

cal_mi_lcao stub (TK=double): no-op

◆ cal_mi_lcao() [2/2]

template<typename TK>
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).

Parameters
stepCurrent SCF iteration number (for logging)
printWhether to print moments to ofs_running
Here is the caller graph for this function:

◆ cal_mw_from_lambda() [1/3]

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.

LCAO path:
  1. Update lambda in DeltaSpin operator (dspin->update_lambda())
  2. Solve HSolverLCAO with charge update disabled (last param = true means no charge update)
  3. Calculate weights from new eigenvalues
  4. Call cal_mi_lcao() to compute moments from density matrix
PW path:
  1. [First call only, i_step==-1] Save subspace H, S, becp from Hamiltonian This captures the "unperturbed" state before any lambda is applied.
  2. [i_step!=-1] Apply DeltaSpin correction via calculate_delta_hcc() For the first call (i_step==-1), no correction is applied (lambda=0).
  3. Diagonalize in subspace via diag_responce(), update becp coefficients
  4. Calculate weights from new eigenvalues
  5. Call accumulate_Mi_from_becp() for each k-point to compute Mi
  6. MPI reduce Mi across k-pools (each pool has a partial sum)
Parameters
i_stepCurrent inner lambda step (-1 = initialization, 0+ = optimization)
delta_lambdaChange 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

◆ cal_mw_from_lambda() [2/3]

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

◆ cal_mw_from_lambda() [3/3]

template<typename TK>
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.

LCAO path:
  1. Update lambda in DeltaSpin operator
  2. Solve HSolverLCAO (diagonalize without charge update)
  3. Calculate weights from eigenvalues
  4. Call cal_mi_lcao() to compute moments
PW path:
  1. [First call only] Save subspace H, S, and becp from Hamiltonian
  2. Apply DeltaSpin correction via calculate_delta_hcc()
  3. Diagonalize in subspace via diag_responce(), update becp
  4. Calculate weights from new eigenvalues
  5. Call accumulate_Mi_from_becp() for each k-point
  6. MPI reduce Mi across k-pools
Parameters
i_stepCurrent inner lambda step (-1 = initialization, 0+ = optimization)
delta_lambdaChange in lambda from previous step (for incremental H correction)

◆ check_atomCounts()

template<typename TK>
void spinconstrain::SpinConstrain< TK >::check_atomCounts ( ) const
inline

check atomCounts

Here is the caller graph for this function:

◆ get_alpha_trial()

template<typename TK>
double spinconstrain::SpinConstrain< TK >::get_alpha_trial ( ) const
inline

get alpha_trial

◆ get_atomCounts()

template<typename TK>
const std::map< int, int > & spinconstrain::SpinConstrain< TK >::get_atomCounts ( ) const
inline

get element index to atom index map

Here is the caller graph for this function:

◆ get_atomLabels()

template<typename TK>
const std::vector< std::string > & spinconstrain::SpinConstrain< TK >::get_atomLabels ( ) const
inline

get human-readable atom labels ("Fe_0", "Fe_1", ...) for table printing

Here is the caller graph for this function:

◆ get_constrain()

template<typename TK>
const std::vector< ModuleBase::Vector3< int > > & spinconstrain::SpinConstrain< TK >::get_constrain ( ) const
inline

get constrain

Here is the caller graph for this function:

◆ get_current_sc_thr()

template<typename TK>
double spinconstrain::SpinConstrain< TK >::get_current_sc_thr ( ) const
inline

get current adaptive sc threshold (max(initial_rms * sc_drop_thr_, sc_thr_))

Here is the caller graph for this function:

◆ get_decay_grad() [1/2]

template<typename TK>
const std::vector< double > & spinconstrain::SpinConstrain< TK >::get_decay_grad ( ) const
inline

get decay_grad

◆ get_decay_grad() [2/2]

template<typename TK>
double spinconstrain::SpinConstrain< TK >::get_decay_grad ( int itype) const
inline

get decay_grad

Here is the caller graph for this function:

◆ get_escon()

template<typename TK>
double spinconstrain::SpinConstrain< TK >::get_escon ( ) const
inline

Get the cached constraint energy from the last cal_escon() call (Ry).

◆ get_iat()

template<typename TK>
int spinconstrain::SpinConstrain< TK >::get_iat ( int itype,
int atom_index ) const
inline

get iat

Here is the caller graph for this function:

◆ get_iwt()

template<typename TK>
int spinconstrain::SpinConstrain< TK >::get_iwt ( int itype,
int iat,
int orbital_index ) const
inline

Convert (itype, iat, iw) to global orbital index

◆ get_last_rms_error()

template<typename TK>
double spinconstrain::SpinConstrain< TK >::get_last_rms_error ( ) const
inline

RMS error of the most recent lambda optimization loop (-1.0 if none has run).

Here is the caller graph for this function:

◆ get_lnchiCounts()

template<typename TK>
const std::map< int, std::map< int, int > > & spinconstrain::SpinConstrain< TK >::get_lnchiCounts ( ) const
inline

get lnchiCounts

◆ get_mi()

template<typename TK>
const std::vector< ModuleBase::Vector3< double > > & spinconstrain::SpinConstrain< TK >::get_mi ( ) const
inline

get computed magnetic moments Mi per atom

Here is the caller graph for this function:

◆ get_nat()

template<typename TK>
int spinconstrain::SpinConstrain< TK >::get_nat ( ) const
inline

get nat

Here is the caller graph for this function:

◆ get_npol()

template<typename TK>
int spinconstrain::SpinConstrain< TK >::get_npol ( ) const
inline

◆ get_nsc()

template<typename TK>
int spinconstrain::SpinConstrain< TK >::get_nsc ( ) const
inline

get nsc

Here is the caller graph for this function:

◆ get_nsc_min()

template<typename TK>
int spinconstrain::SpinConstrain< TK >::get_nsc_min ( ) const
inline

get nsc_min

◆ get_nspin()

template<typename TK>
int spinconstrain::SpinConstrain< TK >::get_nspin ( ) const
inline

get nspin

Here is the caller graph for this function:

◆ get_ntype()

template<typename TK>
int spinconstrain::SpinConstrain< TK >::get_ntype ( ) const
inline

get ntype

Here is the caller graph for this function:

◆ get_nw()

template<typename TK>
int spinconstrain::SpinConstrain< TK >::get_nw ( ) const
inline

Total number of orbitals across all constrained atoms.

◆ get_orbitalCounts()

template<typename TK>
const std::map< int, int > & spinconstrain::SpinConstrain< TK >::get_orbitalCounts ( ) const
inline

get element index to orbital index map

◆ get_sc_drop_thr()

template<typename TK>
double spinconstrain::SpinConstrain< TK >::get_sc_drop_thr ( ) const
inline

get sc_drop_thr

◆ get_sc_lambda()

template<typename TK>
const std::vector< ModuleBase::Vector3< double > > & spinconstrain::SpinConstrain< TK >::get_sc_lambda ( ) const
inline

get sc_lambda

Here is the caller graph for this function:

◆ get_sc_thr()

template<typename TK>
double spinconstrain::SpinConstrain< TK >::get_sc_thr ( ) const
inline

get sc_thr

Here is the caller graph for this function:

◆ get_sccut()

template<typename TK>
double spinconstrain::SpinConstrain< TK >::get_sccut ( ) const
inline

get sccut

Here is the caller graph for this function:

◆ get_spin_sign()

template<typename TK>
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:

  • isk[ik] == 0: spin-up channel (majority spin) -> sign = +1
  • isk[ik] == 1: spin-down channel (minority spin) -> sign = -1 For non-collinear (npol=2), always returns +1 since both components are handled together.
Returns
+1 for spin-up, -1 for spin-down, +1 for non-collinear

◆ get_target_mag()

template<typename TK>
const std::vector< ModuleBase::Vector3< double > > & spinconstrain::SpinConstrain< TK >::get_target_mag ( ) const
inline

get target_mag

Here is the caller graph for this function:

◆ getScInstance()

template<typename TK>
SpinConstrain< TK > & spinconstrain::SpinConstrain< TK >::getScInstance ( )
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.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_sc()

template<typename TK>
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.


PUBLIC INTERFACE - Main entry points for the ESolver layer

Called once at the start of a DeltaSpin calculation. Performs:

  1. Set input parameters (convergence threshold, max steps, trial alpha)
  2. Get atom/orbital/lnchi counts from UnitCell for indexing
  3. Set nspin and npol (nspin=4 -> npol=2, nspin=2 -> npol=1)
  4. Load target_mag, lambda, constrain from UnitCell (parsed from STRU)
  5. For nspin=2: force x,y constraint flags to 0 (collinear: only z is constrained)
  6. Set solver parameters (k-point list, Hamiltonian, psi, electronic state)
Parameters
sc_thr_inConvergence threshold for RMS(Mi - M_target) in uB
nsc_inMaximum number of inner lambda optimization steps
nsc_min_inMinimum number of inner steps before early exit checks
alpha_trial_inInitial trial step size (eV/uB^2), converted to Ry internally
sccut_inMaximum lambda change per step (eV/uB), converted to Ry internally
sc_drop_thr_inFraction of initial RMS for adaptive threshold
ucellUnit cell with atomic positions, STRU constraint data
direction_only_inIf true, only optimize spin direction (|lambda| -> 0)
ParaV_inParallel orbitals distribution info (LCAO only)
nspin_inSpin type: 2=collinear, 4=non-collinear
kv_inK-point vector list
p_hamilt_inPointer to Hamiltonian (HamiltLCAO or HamiltPW)
psi_inPointer to wavefunctions (Psi<TK>)
dm_inPointer to density matrix (LCAO only)
pelec_inPointer to electronic state (for charge, weights, ekb)
pw_wfc_inPW basis for wavefunction storage (PW only)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mag_converged()

template<typename TK>
bool spinconstrain::SpinConstrain< TK >::mag_converged ( ) const
inline

Get magnetic moment convergence flag.

Here is the caller graph for this function:

◆ operator=() [1/2]

template<typename TK>
SpinConstrain & spinconstrain::SpinConstrain< TK >::operator= ( SpinConstrain< TK > && )
privatedelete

Move assignment deleted.

Here is the call graph for this function:

◆ operator=() [2/2]

template<typename TK>
SpinConstrain & spinconstrain::SpinConstrain< TK >::operator= ( SpinConstrain< TK > const & )
privatedelete

Copy assignment deleted.

Here is the call graph for this function:

◆ reset_dspin_operator() [1/2]

template<typename TK>
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.

When to call
  • After changing target_mag_ or constrain_ arrays
  • When restarting from a previous SCF calculation with different constraints
  • When switching between LCAO and PW basis sets

◆ reset_dspin_operator() [2/2]

void spinconstrain::SpinConstrain< double >::reset_dspin_operator ( )

reset_dspin_operator stub (TK=double): no-op

◆ run_lambda_linear_scan() [1/3]

void spinconstrain::SpinConstrain< std::complex< double > >::run_lambda_linear_scan ( int outer_step,
std::ostream & ofs_running )

◆ run_lambda_linear_scan() [2/3]

template<typename TK>
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.

Parameters
outer_stepCurrent SCF outer iteration number
Here is the caller graph for this function:

◆ run_lambda_linear_scan() [3/3]

void spinconstrain::SpinConstrain< double >::run_lambda_linear_scan ( int outer_step,
std::ostream & ofs_running )

run_lambda_linear_scan stub (TK=double): no-op

◆ run_lambda_loop() [1/3]

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)

◆ run_lambda_loop() [2/3]

template<typename TK>
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:

  • Polak-Ribiere formula for beta (conjugate direction)
  • Linear interpolation for optimal step size (alpha_opt)
  • Adaptive alpha_trial adjustment based on convergence behavior
  • Gradient decay check for early termination
Parameters
outer_stepCurrent SCF outer iteration number
rerunIf true, use full PW solver for final charge update
Here is the caller graph for this function:

◆ run_lambda_loop() [3/3]

void spinconstrain::SpinConstrain< double >::run_lambda_loop ( int outer_step,
bool rerun,
std::ostream & ofs_running )

run_lambda_loop stub (TK=double): no-op

◆ set_atomCounts()

template<typename TK>
void spinconstrain::SpinConstrain< TK >::set_atomCounts ( const std::map< int, int > & atomCounts_in)
inline

set element index to atom index map

Here is the caller graph for this function:

◆ set_constrain() [1/2]

template<typename TK>
void spinconstrain::SpinConstrain< TK >::set_constrain ( )
inline

set constrain

Here is the caller graph for this function:

◆ set_constrain() [2/2]

template<typename TK>
void spinconstrain::SpinConstrain< TK >::set_constrain ( const ModuleBase::Vector3< int > * constrain_in,
int nat_in )
inline

set constrain from variable

◆ set_decay_grad() [1/2]

template<typename TK>
void spinconstrain::SpinConstrain< TK >::set_decay_grad ( )
inline

set decay_grad

Here is the caller graph for this function:

◆ set_decay_grad() [2/2]

template<typename TK>
void spinconstrain::SpinConstrain< TK >::set_decay_grad ( const double * decay_grad_in,
int ntype_in )
inline

set decay_grad from variable

◆ set_input_parameters()

template<typename TK>
void spinconstrain::SpinConstrain< TK >::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 )
inline

set input parameters

◆ set_lnchiCounts()

template<typename TK>
void spinconstrain::SpinConstrain< TK >::set_lnchiCounts ( const std::map< int, std::map< int, int > > & lnchiCounts_in)
inline

set lnchiCounts

◆ set_mag_converged()

template<typename TK>
void spinconstrain::SpinConstrain< TK >::set_mag_converged ( bool is_Mi_converged_in)
inline

Set magnetic moment convergence flag.

Here is the caller graph for this function:

◆ set_npol()

template<typename TK>
void spinconstrain::SpinConstrain< TK >::set_npol ( int npol)
inline

◆ set_nspin()

template<typename TK>
void spinconstrain::SpinConstrain< TK >::set_nspin ( int nspin)
inline

set nspin

◆ set_operator()

template<typename TK>
void spinconstrain::SpinConstrain< TK >::set_operator ( hamilt::Operator< TK > * op_in)

Set DeltaSpin operator pointer for magnetic moment calculation (LCAO).

Parameters
op_inBase pointer, actual type is DeltaSpin<OperatorLCAO<TK, TR>>*
Here is the caller graph for this function:

◆ set_orbitalCounts()

template<typename TK>
void spinconstrain::SpinConstrain< TK >::set_orbitalCounts ( const std::map< int, int > & orbitalCounts_in)
inline

set element index to orbital index map

◆ set_ParaV()

template<typename TK>
void spinconstrain::SpinConstrain< TK >::set_ParaV ( Parallel_Orbitals * ParaV_in)

set orbital parallel info

set ParaV

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_sc_drop_thr()

template<typename TK>
void spinconstrain::SpinConstrain< TK >::set_sc_drop_thr ( double sc_drop_thr_in)
inline

set decay grad switch

◆ set_sc_lambda() [1/2]

template<typename TK>
void spinconstrain::SpinConstrain< TK >::set_sc_lambda ( )
inline

set sc_lambda

◆ set_sc_lambda() [2/2]

template<typename TK>
void spinconstrain::SpinConstrain< TK >::set_sc_lambda ( const ModuleBase::Vector3< double > * lambda_in,
int nat_in )
inline

set sc_lambda from variable

◆ set_solver_parameters()

template<typename TK>
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

Here is the caller graph for this function:

◆ set_target_mag() [1/3]

template<typename TK>
void spinconstrain::SpinConstrain< TK >::set_target_mag ( )
inline

set target_mag

◆ set_target_mag() [2/3]

template<typename TK>
void spinconstrain::SpinConstrain< TK >::set_target_mag ( const ModuleBase::Vector3< double > * target_mag_in,
int nat_in )
inline

set target_mag from variable

◆ set_target_mag() [3/3]

template<typename TK>
void spinconstrain::SpinConstrain< TK >::set_target_mag ( const std::vector< ModuleBase::Vector3< double > > & target_mag_in)
inline

set target magnetic moment

◆ update_psi_charge() [1/2]

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.

◆ update_psi_charge() [2/2]

template<typename TK>
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.

Parameters
delta_lambdaLambda change for incremental H correction
pw_solveIf true, run full PW solver for refinement; if false, just update weights
full_updateIf true, apply full lambda (not delta) to H correction

◆ zero_Mi()

template<typename TK>
void spinconstrain::SpinConstrain< TK >::zero_Mi ( )
inline

zero atomic magnetic moment

Member Data Documentation

◆ higher_mag_prec

template<typename TK>
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.

◆ kv_

template<typename TK>
K_Vectors spinconstrain::SpinConstrain< TK >::kv_

K-point vector list.

◆ last_rms_error_

template<typename TK>
double spinconstrain::SpinConstrain< TK >::last_rms_error_ = -1.0
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.

◆ meV_to_Ry

template<typename TK>
const double spinconstrain::SpinConstrain< TK >::meV_to_Ry = 7.349864435130999e-05

Conversion factor.

◆ p_hamilt

template<typename TK>
void* spinconstrain::SpinConstrain< TK >::p_hamilt = nullptr

Pointer to HamiltLCAO or HamiltPW.

◆ p_operator

template<typename TK>
hamilt::Operator<TK>* spinconstrain::SpinConstrain< TK >::p_operator = nullptr
private

DeltaSpin operator pointer for LCAO magnetic moment calculation.

◆ ParaV

template<typename TK>
Parallel_Orbitals* spinconstrain::SpinConstrain< TK >::ParaV = nullptr

Parallel orbitals distribution (row/col mapping for ScaLAPACK).


EXTERNAL POINTERS - Set by init_sc(), used throughout the module

Design rationale for void* pointers
The Hamiltonian and Psi types differ between LCAO and PW bases. Using void* avoids template coupling and allows the same SpinConstrain code to work with both basis sets. Concrete types are recovered via static_cast at call sites.

◆ pelec

template<typename TK>
elecstate::ElecState* spinconstrain::SpinConstrain< TK >::pelec = nullptr

Electronic state: ekb, wg, charge, klist.

◆ psi

template<typename TK>
void* spinconstrain::SpinConstrain< TK >::psi = nullptr

Pointer to Psi<TK> wavefunction container.

◆ pw_cache_

template<typename TK>
pw::SubspaceCache spinconstrain::SpinConstrain< TK >::pw_cache_

SUBSPACE DATA CACHING (PW basis only)

Purpose
In the PW basis, the subspace Hamiltonian H_sub = <psi|H|psi> and becp coefficients are expensive to compute. They are cached on the first call to cal_mw_from_lambda() and reused across multiple lambda steps within the same SCF iteration.
Layout
  • sub_h_save[ik * nbands * nbands + i * nbands + j]: H_sub for k-point ik
  • sub_s_save: same layout for overlap matrix S_sub
  • becp_save[ik * size_becp + ib * nkb * npol + ip]: becp coefficients
  • lambda_in_sub_: lambda values at the time subspace data was saved, used to compute delta_lambda for incremental H corrections
Memory management
Allocated with new[] on first cal_mw_from_lambda() call, freed in update_psi_charge_pw_cpu/gpu() after final subspace diagonalization. PW subspace data cache (H_sub/S_sub/becp + lambda snapshot). Owned object; buffers are device/host memory managed via allocate_cpu/gpu + release_cpu/gpu.

◆ pw_wfc_

template<typename TK>
ModulePW::PW_Basis_K* spinconstrain::SpinConstrain< TK >::pw_wfc_ = nullptr

PW basis for wavefunction storage (PW only).

◆ state_

template<typename TK>
ScState spinconstrain::SpinConstrain< TK >::state_

Constraint parameters and runtime state (lambda, Mi, target_mag, indexing).


The documentation for this class was generated from the following files: