ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
lambda_loop.cpp File Reference

Core lambda optimization algorithms for DeltaSpin. More...

#include "spin_constrain.h"
#include <iostream>
#include <cmath>
#include <chrono>
#include "basic_funcs.h"
#include "deltaspin_pw_mi.h"
#include "lambda_loop_helper.h"
#include "source_base/constants.h"
#include "source_io/module_parameter/parameter.h"
Include dependency graph for lambda_loop.cpp:

Detailed Description

Core lambda optimization algorithms for DeltaSpin.

run_lambda_loop: Conjugate-gradient-like BFGS optimizer
Iteratively adjusts Lagrange multipliers (lambda) to drive atomic magnetic moments (Mi) toward target values (M_target).
Algorithm overview
The optimizer follows a modified Polak-Ribiere conjugate gradient scheme:

Step -1 (Initialization):

  • Compute initial Mi from current wavefunction
  • Save initial lambda (lambda with unconstrained components zeroed)
  • Set adaptive convergence threshold: current_sc_thr_ = max(rms_0 * sc_drop_thr_, sc_thr_)

Each inner step (i_step = 0, 1, ..., nsc-1):

  1. Update lambda: lambda = initial_lambda + delta_lambda
  2. [direction_only] Project out parallel component of lambda
  3. cal_mw_from_lambda(): apply lambda -> solve -> compute new Mi
  4. Check gradient decay: if dM/dlambda < decay_grad, exit early
  5. Compute residual: delta_spin = Mi - M_target
  6. Compute RMS error: rms = sqrt(mean(delta_spin^2))
  7. Check convergence: if rms < current_sc_thr_, update_psi_charge() and exit [PW basis] Re-check with cal_mi_pw(), recursively rerun if RMS too large
  8. [i_step >= 2] Compute Polak-Ribiere beta = rms^2 / rms_old^2
  9. Update search direction: search = delta_spin + beta * search_old
  10. Apply restriction: cap alpha_trial so that |alpha_trial * search| < restrict_current_
  11. Compute cumulative step: dnu = dnu + alpha_trial * search
  12. [direction_only] Project out parallel component of dnu
  13. Trial step: compute Mi at dnu, find optimal alpha via linear interpolation
  14. Update dnu with optimal alpha
  15. Adapt alpha_trial: if |alpha_opt| >> alpha_trial, increase; else decrease
Key variables
  • initial_lambda: lambda with unconstrained components set to 0
  • delta_lambda: current lambda change from initial
  • dnu: cumulative lambda change (search path integral)
  • search: current search direction (steepest descent or conjugate)
  • spin, spin_plus: Mi at current and trial lambda values
  • alpha_trial: current step size (adaptively adjusted)
  • alpha_opt: optimal step size from linear interpolation
Convergence criteria
  1. RMS(Mi - M_target) < current_sc_thr_ (adaptive threshold)
  2. Maximum gradient dM/dlambda < decay_grad[itype] per atom type
  3. Maximum steps reached (nsc)
Error output and solutions
  • "RMS error is too large, rerun the loop": The subspace diagonalization was not accurate enough. The loop is rerun with rerun=false to use the full PW solver for better precision. If this persists, check:
    • PW_DIAG_NMAX and PW_DIAG_THR in DiagoIterAssist
    • higher_mag_prec flag for forced high-precision mode
  • "Reach maximum number of steps": Lambda optimization did not converge within nsc steps. Check:
    • target_mag values are physically reasonable
    • alpha_trial is not too small (slow convergence)
    • decay_grad thresholds are not too aggressive