ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
spinconstrain Namespace Reference

Namespaces

namespace  pw

Classes

struct  ScInitParams
struct  ScAtomData
 Per-atom spin constraint parameters parsed from STRU file. More...
class  ScState
class  SpinConstrain
 Singleton class implementing spin-constrained DFT (DeltaSpin). More...
class  SpinConstrainTest

Functions

void init_sc_state (const ScInitParams &params, const UnitCell &ucell, ScState &state)
 Populate ScState from UnitCell and input parameters.
template<typename TK>
void print_termination (const SpinConstrain< TK > &sc, std::ostream &ofs_running)
 Print final spin and lambda values when the lambda loop terminates.
template<typename TK>
bool check_rms_stop (const SpinConstrain< TK > &sc, int outer_step, int i_step, double rms_error, double duration, double total_duration, std::ostream &ofs_running)
 Check if RMS error is below convergence threshold or max steps reached.
template<typename TK>
void print_header (const SpinConstrain< TK > &sc, std::ostream &ofs_running)
 Print header at start of lambda optimization loop.
template<typename TK>
void check_restriction (const SpinConstrain< TK > &sc, const std::vector< ModuleBase::Vector3< double > > &search, double &alpha_trial, std::ostream &ofs_running)
 Cap step size to prevent overshooting in lambda optimization.
template<typename TK>
double cal_alpha_opt (const SpinConstrain< TK > &sc, std::vector< ModuleBase::Vector3< double > > spin, std::vector< ModuleBase::Vector3< double > > spin_plus, const double alpha_trial)
 Compute optimal step size via linear interpolation.
template<typename TK>
bool check_gradient_decay (const SpinConstrain< TK > &sc, const std::vector< ModuleBase::Vector3< double > > &new_spin, const std::vector< ModuleBase::Vector3< double > > &spin, const std::vector< ModuleBase::Vector3< double > > &delta_lambda, const std::vector< ModuleBase::Vector3< double > > &dnu_last_step, bool print, std::ostream &ofs_running)
 Check if the magnetic susceptibility gradient dM/dlambda has decayed below threshold.
template<typename TK>
void print_Mi (const SpinConstrain< TK > &sc, std::ostream &ofs_running)
 Print atomic magnetic moments Mi in a formatted table.
template<typename TK>
void print_Mag_Force (const SpinConstrain< TK > &sc, std::ostream &ofs_running)
 Print the magnetic force (-lambda) per atom in eV/uB.
template void print_termination< std::complex< double > > (const SpinConstrain< std::complex< double > > &, std::ostream &)
template bool check_rms_stop< std::complex< double > > (const SpinConstrain< std::complex< double > > &, int, int, double, double, double, std::ostream &)
template void print_header< std::complex< double > > (const SpinConstrain< std::complex< double > > &, std::ostream &)
template void check_restriction< std::complex< double > > (const SpinConstrain< std::complex< double > > &, const std::vector< ModuleBase::Vector3< double > > &, double &, std::ostream &)
template double cal_alpha_opt< std::complex< double > > (const SpinConstrain< std::complex< double > > &, std::vector< ModuleBase::Vector3< double > >, std::vector< ModuleBase::Vector3< double > >, const double)
template bool check_gradient_decay< std::complex< double > > (const SpinConstrain< std::complex< double > > &, const std::vector< ModuleBase::Vector3< double > > &, const std::vector< ModuleBase::Vector3< double > > &, const std::vector< ModuleBase::Vector3< double > > &, const std::vector< ModuleBase::Vector3< double > > &, bool, std::ostream &)
template void print_Mi< std::complex< double > > (const SpinConstrain< std::complex< double > > &, std::ostream &)
template void print_Mag_Force< std::complex< double > > (const SpinConstrain< std::complex< double > > &, std::ostream &)
template void print_Mi< double > (const SpinConstrain< double > &, std::ostream &)
template void print_Mag_Force< double > (const SpinConstrain< double > &, std::ostream &)
void accumulate_Mi_from_becp (const std::complex< double > *becp, int nkb, int nbands, int npol, int spin_sign, const double *wg_ik, const int *nh_iat, std::vector< ModuleBase::Vector3< double > > &mi)
 Accumulate atomic magnetic moments from becp coefficients for one k-point.
std::array< std::complex< double >, 4 > pauli_vector_to_spinor (const ModuleBase::Vector3< double > &lambda)
 Convert a Cartesian Pauli vector to a spinor-space operator.
ModuleBase::Vector3< double > pauli_to_moment (const std::complex< double > occ[4], double weight)
 Convert spinor occupation matrix to magnetic moment vector using Pauli matrices.
 TEST (LambdaLoopHelper, GradientDecayUsesAtomsOfEachType)

Detailed Description

Tested function:

  • spinconstrain::SpinConstrain::check_rms_stop
    • check if the rms error is small enough to stop the lambda loop
  • spinconstrain::SpinConstrain::print_termination
  • print termination message

Function Documentation

◆ accumulate_Mi_from_becp()

void spinconstrain::accumulate_Mi_from_becp ( const std::complex< double > * becp,
int nkb,
int nbands,
int npol,
int spin_sign,
const double * wg_ik,
const int * nh_iat,
std::vector< ModuleBase::Vector3< double > > & mi )

Accumulate atomic magnetic moments from becp coefficients for one k-point.

For npol=2 (nspin=4), computes full Pauli decomposition: occ[0] = sum(becp_up^* * becp_up), occ[1] = sum(becp_up^* * becp_dn), occ[2] = sum(becp_dn^* * becp_up), occ[3] = sum(becp_dn^* * becp_dn) Mi = pauli_to_moment(occ, weight) For npol=1 (nspin=2), only z-component: occ = sum(|becp|^2), Mi.z += weight * occ * spin_sign

Parameters
becpProjector coefficients <alpha_{l,m}|psi_{k,i}>
nkbTotal number of projectors
nbandsNumber of bands
npolNumber of spinor components (1 for collinear, 2 for non-collinear)
spin_sign+1 for spin-up, -1 for spin-down (nspin=2 only); unused for npol=2
wg_ikBand occupation weights for this k-point (from Fermi-Dirac)
nh_iatArray of projector counts per atom: nh_iat[iat] = nproj for atom iat
mi[in,out] Magnetic moments vector to accumulate into (size = nat)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ cal_alpha_opt()

template<typename TK>
double spinconstrain::cal_alpha_opt ( const SpinConstrain< TK > & sc,
std::vector< ModuleBase::Vector3< double > > spin,
std::vector< ModuleBase::Vector3< double > > spin_plus,
const double alpha_trial )

Compute optimal step size via linear interpolation.

Compute the optimal step size via two-point linear interpolation.

Algorithm
Uses the two-point linear interpolation (secant method) to find the step size that would drive Mi to M_target:

alpha_opt = sum_k / sum_k2 * alpha_trial

where: sum_k = sum((target - spin) . (spin_plus - spin)) over constrained components sum_k2 = sum(|spin - spin_plus|^2) over constrained components

Edge case handling
  • If |sum_k2| < 1e-30: spin and spin_plus are nearly identical, meaning the lambda change has no effect on Mi. Return alpha_trial as fallback.
Algorithm
alpha_opt = sum_k / sum_k2 * alpha_trial where sum_k = sum((target - spin) . (spin_plus - spin)) over constrained components sum_k2 = sum(|spin - spin_plus|^2) over constrained components
Parameters
scSpinConstrain instance
spinMi at current lambda
spin_plusMi at trial lambda (current + alpha_trial * search)
alpha_trialCurrent trial step size
Returns
Optimal step size; falls back to alpha_trial when sum_k2 ~ 0
Here is the call graph for this function:
Here is the caller graph for this function:

◆ cal_alpha_opt< std::complex< double > >()

template double spinconstrain::cal_alpha_opt< std::complex< double > > ( const SpinConstrain< std::complex< double > > & ,
std::vector< ModuleBase::Vector3< double > > ,
std::vector< ModuleBase::Vector3< double > > ,
const double  )
Here is the call graph for this function:

◆ check_gradient_decay()

template<typename TK>
bool spinconstrain::check_gradient_decay ( const SpinConstrain< TK > & sc,
const std::vector< ModuleBase::Vector3< double > > & new_spin,
const std::vector< ModuleBase::Vector3< double > > & spin,
const std::vector< ModuleBase::Vector3< double > > & delta_lambda,
const std::vector< ModuleBase::Vector3< double > > & dnu_last_step,
bool print,
std::ostream & ofs_running )

Check if the magnetic susceptibility gradient dM/dlambda has decayed below threshold.

Check whether the magnetic susceptibility gradient dM/dlambda has decayed below the per-atom-type threshold.

Algorithm
  1. Compute spin_change = new_spin - spin (change in magnetic moments)
  2. Compute nu_change = delta_lambda - dnu_last_step (change in lambda)
  3. Compute the diagonal self-response dM[ia][ic]/dlambda[ia][ic]
  4. Find the maximum diagonal response per atom type
  5. If max_gradient[itype] < decay_grad[itype], return true (early termination)
Physical meaning
The diagonal gradient dM/dlambda represents how sensitive the magnetic moment is to changes in the Lagrange multiplier. When this gradient becomes very small, further increases in lambda produce diminishing returns in Mi, indicating that the optimization has reached its practical limit.
Output (when triggered)
"Reach limitation of current step ( maximum gradient < X uB^2/eV in atom type Y ), exit."
Algorithm
  1. Compute spin_change = new_spin - spin
  2. Compute nu_change = delta_lambda - dnu_last_step
  3. Compute only the diagonal response dM[ia][ic]/dlambda[ia][ic]
  4. Pick the maximum absolute diagonal response per atom type
  5. Return true if max(|diag|) < decay_grad[itype] for any type
Parameters
scSpinConstrain instance
new_spinMi at current lambda
spinMi at previous lambda
delta_lambdaCurrent lambda change
dnu_last_stepPrevious cumulative step
printWhether to print detailed gradient info
ofs_runningLog output stream
Returns
true if gradient decayed below threshold for any atom type
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_gradient_decay< std::complex< double > >()

template bool spinconstrain::check_gradient_decay< std::complex< double > > ( const SpinConstrain< std::complex< double > > & ,
const std::vector< ModuleBase::Vector3< double > > & ,
const std::vector< ModuleBase::Vector3< double > > & ,
const std::vector< ModuleBase::Vector3< double > > & ,
const std::vector< ModuleBase::Vector3< double > > & ,
bool ,
std::ostream &  )
Here is the call graph for this function:

◆ check_restriction()

template<typename TK>
void spinconstrain::check_restriction ( const SpinConstrain< TK > & sc,
const std::vector< ModuleBase::Vector3< double > > & search,
double & alpha_trial,
std::ostream & ofs_running )

Cap step size to prevent overshooting in lambda optimization.

Cap the step size to prevent the optimizer from overshooting.

If |alpha_trial * max(search)| > restrict_current_, reduce alpha_trial so that the maximum lambda change per step is bounded by restrict_current_.

This prevents the optimizer from taking steps that are too large, which could lead to oscillation or divergence.

Output (when restriction is applied)
  • "alpha after restrict = X eV/uB^2": The capped step size
  • "boundary after = X eV/uB": The actual maximum lambda change

    If |alpha_trial * max(search)| exceeds restrict_current_, the trial step is reduced so that the maximum lambda change per step is bounded. alpha_trial is modified in place.

Parameters
scSpinConstrain instance
searchCurrent search direction (per atom, 3 components)
alpha_trialTrial step size, modified in place if capped
ofs_runningLog output stream
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_restriction< std::complex< double > >()

template void spinconstrain::check_restriction< std::complex< double > > ( const SpinConstrain< std::complex< double > > & ,
const std::vector< ModuleBase::Vector3< double > > & ,
double & ,
std::ostream &  )
Here is the call graph for this function:

◆ check_rms_stop()

template<typename TK>
bool spinconstrain::check_rms_stop ( const SpinConstrain< TK > & sc,
int outer_step,
int i_step,
double rms_error,
double duration,
double total_duration,
std::ostream & ofs_running )

Check if RMS error is below convergence threshold or max steps reached.

Check whether RMS error is below the convergence threshold or the maximum number of inner steps has been reached.

Output
Prints step info: "Step (Outer -- Inner) = X -- Y RMS = Z TIME(s) = T"
Termination messages
  • "Meet convergence criterion": RMS < current_sc_thr_ (successfully converged)
  • "Reach maximum number of steps": i_step == nsc_ - 1 (did not converge)
Return value
  • true: loop should terminate (either converged or max steps)
  • false: continue optimization
Parameters
scSpinConstrain instance
outer_stepCurrent SCF outer iteration
i_stepCurrent inner lambda step
rms_errorCurrent RMS error of Mi - M_target
durationWall time for this step (s)
total_durationCumulative wall time for the inner loop (s)
ofs_runningLog output stream
Returns
true if the inner loop should terminate, false otherwise
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_rms_stop< std::complex< double > >()

template bool spinconstrain::check_rms_stop< std::complex< double > > ( const SpinConstrain< std::complex< double > > & ,
int ,
int ,
double ,
double ,
double ,
std::ostream &  )
Here is the call graph for this function:

◆ init_sc_state()

void spinconstrain::init_sc_state ( const ScInitParams & params,
const UnitCell & ucell,
ScState & state )

Populate ScState from UnitCell and input parameters.

Performs the solver-independent portion of init_sc():

  1. Set input parameters (thresholds, step sizes; unit conversion to Ry)
  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 constrained)
  6. Set atom labels, direction_only, tpiba; zero-initialize decay_grad
Parameters
paramsInput parameters (see ScInitParams)
ucellUnit cell with STRU constraint data
stateScState to fill (in/out)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pauli_to_moment()

ModuleBase::Vector3< double > spinconstrain::pauli_to_moment ( const std::complex< double > occ[4],
double weight )
inline

Convert spinor occupation matrix to magnetic moment vector using Pauli matrices.

For a two-component spinor wavefunction, the occupation array is: occ = |a|^2 a^*b | = | (1+Mz)/2 (Mx+iMy)/2 | |b^*a |b|^2 | | (Mx-iMy)/2 (1-Mz)/2 | In this bra-first storage convention, the magnetic moment components are: Mx = occ[1] + occ[2] (real part) My = Im(occ[1] - occ[2]) Mz = occ[0] - occ[3] (real part) where occ = {|a|^2, a^*b, b^*a, |b|^2} from becp coefficients.

Parameters
occ4-element array of occupation matrix elements (complex)
weightk-point weight for integration
Returns
3D magnetic moment vector (Mx, My, Mz) in Bohr magnetons
Here is the caller graph for this function:

◆ pauli_vector_to_spinor()

std::array< std::complex< double >, 4 > spinconstrain::pauli_vector_to_spinor ( const ModuleBase::Vector3< double > & lambda)
inline

Convert a Cartesian Pauli vector to a spinor-space operator.

For sigma_y = [[0, -i], [i, 0]], lambda dot sigma is stored in row-major order as {lambda_z, lambda_x - i lambda_y, lambda_x + i lambda_y, -lambda_z}.

Here is the caller graph for this function:

◆ print_header()

template<typename TK>
void spinconstrain::print_header ( const SpinConstrain< TK > & sc,
std::ostream & ofs_running )

Print header at start of lambda optimization loop.

Print header at the start of the lambda optimization loop.

Parameters
scSpinConstrain instance
ofs_runningLog output stream
Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_header< std::complex< double > >()

template void spinconstrain::print_header< std::complex< double > > ( const SpinConstrain< std::complex< double > > & ,
std::ostream &  )
Here is the call graph for this function:

◆ print_Mag_Force()

template<typename TK>
void spinconstrain::print_Mag_Force ( const SpinConstrain< TK > & sc,
std::ostream & ofs_running )

Print the magnetic force (-lambda) per atom in eV/uB.

Lifted from SpinConstrain<TK>::print_Mag_Force; accesses state via getters. lambda is read via get_sc_lambda() and converted from Ry to eV on output.

Physical meaning
Magnetic force = dL/dMi = -lambda. Large |lambda| means the system strongly resists the target moment constraint.
Parameters
scSpinConstrain instance
ofs_runningLog output stream
Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_Mag_Force< double >()

template void spinconstrain::print_Mag_Force< double > ( const SpinConstrain< double > & ,
std::ostream &  )

◆ print_Mag_Force< std::complex< double > >()

template void spinconstrain::print_Mag_Force< std::complex< double > > ( const SpinConstrain< std::complex< double > > & ,
std::ostream &  )
Here is the call graph for this function:

◆ print_Mi()

template<typename TK>
void spinconstrain::print_Mi ( const SpinConstrain< TK > & sc,
std::ostream & ofs_running )

Print atomic magnetic moments Mi in a formatted table.

Output format
  • nspin=2: "Total Magnetism (uB)" with single z-component column
  • nspin=4: three columns (Mx, My, Mz)

Lifted from SpinConstrain<TK>::print_Mi; accesses state via getters.

Output format
  • nspin=2: "Total Magnetism (uB)" with single z-component column
  • nspin=4: three columns (Mx, My, Mz)
Parameters
scSpinConstrain instance
ofs_runningLog output stream
Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_Mi< double >()

template void spinconstrain::print_Mi< double > ( const SpinConstrain< double > & ,
std::ostream &  )

◆ print_Mi< std::complex< double > >()

template void spinconstrain::print_Mi< std::complex< double > > ( const SpinConstrain< std::complex< double > > & ,
std::ostream &  )
Here is the call graph for this function:

◆ print_termination()

template<typename TK>
void spinconstrain::print_termination ( const SpinConstrain< TK > & sc,
std::ostream & ofs_running )

Print final spin and lambda values when the lambda loop terminates.

Output
  • "after-optimization spin (uB)": Final magnetic moments Mi per atom
  • "after-optimization lambda (eV/uB)": Final Lagrange multipliers per atom
  • "Inner optimization for lambda ends.": Termination marker
Parameters
scSpinConstrain instance (read Mi_ and lambda_)
ofs_runningLog output stream
Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_termination< std::complex< double > >()

template void spinconstrain::print_termination< std::complex< double > > ( const SpinConstrain< std::complex< double > > & ,
std::ostream &  )
Here is the call graph for this function:

◆ TEST()

spinconstrain::TEST ( LambdaLoopHelper ,
GradientDecayUsesAtomsOfEachType  )
Here is the call graph for this function: