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):
- Update lambda: lambda = initial_lambda + delta_lambda
- [direction_only] Project out parallel component of lambda
- cal_mw_from_lambda(): apply lambda -> solve -> compute new Mi
- Check gradient decay: if dM/dlambda < decay_grad, exit early
- Compute residual: delta_spin = Mi - M_target
- Compute RMS error: rms = sqrt(mean(delta_spin^2))
- 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
- [i_step >= 2] Compute Polak-Ribiere beta = rms^2 / rms_old^2
- Update search direction: search = delta_spin + beta * search_old
- Apply restriction: cap alpha_trial so that |alpha_trial * search| < restrict_current_
- Compute cumulative step: dnu = dnu + alpha_trial * search
- [direction_only] Project out parallel component of dnu
- Trial step: compute Mi at dnu, find optimal alpha via linear interpolation
- Update dnu with optimal alpha
- 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
- RMS(Mi - M_target) < current_sc_thr_ (adaptive threshold)
- Maximum gradient dM/dlambda < decay_grad[itype] per atom type
- 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