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

#include <deltaspin_state.h>

Collaboration diagram for spinconstrain::ScState:

Public Member Functions

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 from ScData (parsed from STRU file)
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 from ScData (parsed from STRU file)
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 from ScData
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
std::vector< ModuleBase::Vector3< double > > & get_lambda ()
 get mutable 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 set_npol (int npol)
 set npol
int get_npol () const
 get npol
void zero_Mi ()
 zero atomic magnetic moment
double get_decay_grad (int itype) const
 get decay_grad (root process only: reads ScDecayGrad from json)
void set_decay_grad ()
 set decay_grad (zero-initialize per element type)
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
std::vector< ModuleBase::Vector3< double > > & get_mi ()
 get mutable 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
int get_nw () const
 Total number of orbitals across all constrained atoms.
int get_iwt (int itype, int iat, int orbital_index) const
 Convert (itype, iat, iw) to global orbital index.
void set_mag_converged (bool is_Mi_converged_in)
 Set magnetic moment convergence flag.
bool mag_converged () const
 Get magnetic moment convergence flag.
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).

Public Attributes

std::vector< ModuleBase::Vector3< double > > target_mag_
 Target magnetic moments (uB) per atom.
std::vector< ModuleBase::Vector3< int > > constrain_
 Per-atom/component constraint flags: 0=free, 1=constrained.
std::vector< std::string > atomLabels_
 Human-readable labels: "Fe_0", "Fe_1", etc.
int nspin_ = 0
 Spin type: 2=collinear, 4=non-collinear.
int npol_ = 1
 Number of spinor components: 1 for nspin=2, 2 for nspin=4.
double sc_thr_ = 0.0
 Convergence threshold for RMS(Mi - M_target) in uB.
double sc_drop_thr_ = 1e-3
 Fraction of initial RMS for adaptive threshold.
double current_sc_thr_ = 0.0
 Adaptive threshold: max(initial_rms * sc_drop_thr_, sc_thr_).
double alpha_trial_ = 0.0
 Initial trial step size (Ry/uB^2), adaptively adjusted during loop.
double restrict_current_ = 0.0
 Maximum allowed lambda change per step (Ry/uB).
int nsc_ = 0
 Maximum number of inner lambda optimization steps.
int nsc_min_ = 0
 Minimum steps before early exit checks (gradient decay).
double escon_ = 0.0
 Cached constraint energy from last cal_escon() call (Ry).
bool is_Mi_converged = false
 Has the magnetic moment converged in the current SCF iteration?
bool direction_only_ = false
 If true, only optimize spin direction.
double tpiba = 0.0
 2*pi/a lattice constant scaling factor, saved from UnitCell

Private Attributes

std::vector< ModuleBase::Vector3< double > > lambda_
 Lagrange multipliers (Ry/uB) per atom, 3 components.
std::vector< ModuleBase::Vector3< double > > Mi_
 Current computed magnetic moments (uB) per atom.
std::map< int, std::vector< ScAtomData > > ScData
 Raw constraint data indexed by element type (itype).
std::map< int, double > ScDecayGrad
 Gradient decay thresholds (uB^2/eV) per element type.
std::vector< double > decay_grad_
 Gradient decay thresholds converted to uB^2/Ry, per element type.
std::map< int, int > atomCounts
 Number of atoms per element type: {itype -> nat_itype}.
std::map< int, int > orbitalCounts
 Number of orbitals per element type: {itype -> nw_itype}.
std::map< int, std::map< int, int > > lnchiCounts
 {itype -> {L -> nchi}}: angular momentum channels

Member Function Documentation

◆ cal_escon()

double spinconstrain::ScState::cal_escon ( )

Calculate the spin constraint energy contribution: E_scon = -sum(lambda_i . Mi_i).

Calculate the spin constraint energy: E_scon = -sum_i (lambda_i . Mi_i).

Returns
Constraint energy in Ry

The constraint energy is the Lagrange multiplier term in the constrained DFT functional: E'[rho] = E_DFT[rho] - sum_i lambda_i . (Mi_i - M_target_i)

Returns
Constraint energy in Ry
Here is the call graph for this function:

◆ check_atomCounts()

void spinconstrain::ScState::check_atomCounts ( ) const

check atomCounts

Validate atom count data integrity.

Checks that atomCounts has been properly initialized and contains valid data. Called before any operation that depends on atom indexing.

Error conditions
  • "atomCounts is not set": init_sc() was not called
  • "nat <= 0": no atoms in the system
  • "itype out of range": element type index exceeds ntype
  • "number of atoms <= 0": some element type has no atoms
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_alpha_trial()

double spinconstrain::ScState::get_alpha_trial ( ) const

get alpha_trial

◆ get_atomCounts()

const std::map< int, int > & spinconstrain::ScState::get_atomCounts ( ) const

get element index to atom index map

◆ get_atomLabels()

const std::vector< std::string > & spinconstrain::ScState::get_atomLabels ( ) const

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

get human-readable atom labels for table printing

◆ get_constrain()

const std::vector< ModuleBase::Vector3< int > > & spinconstrain::ScState::get_constrain ( ) const

get constrain

get_constrain

◆ get_current_sc_thr()

double spinconstrain::ScState::get_current_sc_thr ( ) const

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

get current adaptive sc threshold

◆ get_decay_grad() [1/2]

const std::vector< double > & spinconstrain::ScState::get_decay_grad ( ) const

get decay_grad

◆ get_decay_grad() [2/2]

double spinconstrain::ScState::get_decay_grad ( int itype) const

get decay_grad (root process only: reads ScDecayGrad from json)

get grad_decay this function can only be called by the root process because only root process reads the ScDecayGrad from json file

◆ get_escon()

double spinconstrain::ScState::get_escon ( ) const

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

◆ get_iat()

int spinconstrain::ScState::get_iat ( int itype,
int atom_index ) const

get iat

Convert (element_type, local_atom_index) to global atom index.

Atoms in ABACUS are organized by element type. Within each type, atoms are indexed locally (0, 1, ..., nat_itype-1). This function maps to the global index that runs across all atoms (0, 1, ..., nat-1).

Example: If type 0 has 2 Fe atoms and type 1 has 3 O atoms: get_iat(0, 0) -> 0 (Fe_0) get_iat(0, 1) -> 1 (Fe_1) get_iat(1, 0) -> 2 (O_0) get_iat(1, 1) -> 3 (O_1) get_iat(1, 2) -> 4 (O_2)

Parameters
itypeElement type index (0 to ntype-1)
atom_indexLocal index within the element type
Returns
Global atom index
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_iwt()

int spinconstrain::ScState::get_iwt ( int itype,
int iat,
int orbital_index ) const

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

Convert (itype, local_atom_index, orbital_index) to global orbital index.

The global orbital index is used to access elements in distributed matrices (ScaLAPACK format). The mapping is: iwt = sum_{t < itype} orbitalCounts[t] + iat * orbitalCounts[itype] + orbital_index where iat = get_iat(itype, local_atom_index).

Returns
Global orbital index, or 0 if itype not found

◆ get_lambda()

std::vector< ModuleBase::Vector3< double > > & spinconstrain::ScState::get_lambda ( )
inline

get mutable sc_lambda

Here is the caller graph for this function:

◆ get_lnchiCounts()

const std::map< int, std::map< int, int > > & spinconstrain::ScState::get_lnchiCounts ( ) const

get lnchiCounts

◆ get_mi() [1/2]

std::vector< ModuleBase::Vector3< double > > & spinconstrain::ScState::get_mi ( )
inline

get mutable computed magnetic moments Mi per atom

◆ get_mi() [2/2]

const std::vector< ModuleBase::Vector3< double > > & spinconstrain::ScState::get_mi ( ) const

get computed magnetic moments Mi per atom

Here is the caller graph for this function:

◆ get_nat()

int spinconstrain::ScState::get_nat ( ) const

get nat

Get total number of atoms across all element types.

Here is the caller graph for this function:

◆ get_npol()

int spinconstrain::ScState::get_npol ( ) const

get npol

◆ get_nsc()

int spinconstrain::ScState::get_nsc ( ) const

get nsc

◆ get_nsc_min()

int spinconstrain::ScState::get_nsc_min ( ) const

get nsc_min

◆ get_nspin()

int spinconstrain::ScState::get_nspin ( ) const

get nspin

◆ get_ntype()

int spinconstrain::ScState::get_ntype ( ) const

get ntype

Get number of element types.

Here is the caller graph for this function:

◆ get_nw()

int spinconstrain::ScState::get_nw ( ) const

Total number of orbitals across all constrained atoms.

◆ get_orbitalCounts()

const std::map< int, int > & spinconstrain::ScState::get_orbitalCounts ( ) const

get element index to orbital index map

◆ get_sc_drop_thr()

double spinconstrain::ScState::get_sc_drop_thr ( ) const

get sc_drop_thr

◆ get_sc_lambda()

const std::vector< ModuleBase::Vector3< double > > & spinconstrain::ScState::get_sc_lambda ( ) const

get sc_lambda

Here is the caller graph for this function:

◆ get_sc_thr()

double spinconstrain::ScState::get_sc_thr ( ) const

get sc_thr

◆ get_sccut()

double spinconstrain::ScState::get_sccut ( ) const

get sccut

◆ get_target_mag()

const std::vector< ModuleBase::Vector3< double > > & spinconstrain::ScState::get_target_mag ( ) const

get target_mag

◆ mag_converged()

bool spinconstrain::ScState::mag_converged ( ) const
inline

Get magnetic moment convergence flag.

◆ set_atomCounts()

void spinconstrain::ScState::set_atomCounts ( const std::map< int, int > & atomCounts_in)

set element index to atom index map

Here is the caller graph for this function:

◆ set_constrain() [1/2]

void spinconstrain::ScState::set_constrain ( )

set constrain from ScData

Set constraint flags from ScData.

The constrain array determines which components of each atom's magnetic moment are actively constrained:

  • constrain[ia].x = 1: Mx is constrained to target_mag[ia].x
  • constrain[ia].y = 1: My is constrained to target_mag[ia].y
  • constrain[ia].z = 1: Mz is constrained to target_mag[ia].z
  • constrain[ia].c = 0: component is free (determined by the system)

Default is all zeros (no constraints). Components with constrain=0 are excluded from the lambda optimization and RMS error calculation.

Here is the call graph for this function:

◆ set_constrain() [2/2]

void spinconstrain::ScState::set_constrain ( const ModuleBase::Vector3< int > * constrain_in,
int nat_in )

set constrain from variable

Here is the call graph for this function:

◆ set_decay_grad() [1/2]

void spinconstrain::ScState::set_decay_grad ( )

set decay_grad (zero-initialize per element type)

set grad_decy

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

◆ set_decay_grad() [2/2]

void spinconstrain::ScState::set_decay_grad ( const double * decay_grad_in,
int ntype_in )

set decay_grad from variable

set grad_decy from variable

Here is the call graph for this function:

◆ set_input_parameters()

void spinconstrain::ScState::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

Here is the caller graph for this function:

◆ set_lnchiCounts()

void spinconstrain::ScState::set_lnchiCounts ( const std::map< int, std::map< int, int > > & lnchiCounts_in)

set lnchiCounts

Here is the caller graph for this function:

◆ set_mag_converged()

void spinconstrain::ScState::set_mag_converged ( bool is_Mi_converged_in)
inline

Set magnetic moment convergence flag.

◆ set_npol()

void spinconstrain::ScState::set_npol ( int npol)

set npol

Here is the caller graph for this function:

◆ set_nspin()

void spinconstrain::ScState::set_nspin ( int nspin)

set nspin

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

◆ set_orbitalCounts()

void spinconstrain::ScState::set_orbitalCounts ( const std::map< int, int > & orbitalCounts_in)

set element index to orbital index map

Here is the caller graph for this function:

◆ set_sc_drop_thr()

void spinconstrain::ScState::set_sc_drop_thr ( double sc_drop_thr_in)

set decay grad switch

set sc_drop_thr

◆ set_sc_lambda() [1/2]

void spinconstrain::ScState::set_sc_lambda ( )

set sc_lambda from ScData (parsed from STRU file)

Here is the call graph for this function:

◆ set_sc_lambda() [2/2]

void spinconstrain::ScState::set_sc_lambda ( const ModuleBase::Vector3< double > * lambda_in,
int nat_in )

set sc_lambda from variable

Here is the call graph for this function:

◆ set_target_mag() [1/3]

void spinconstrain::ScState::set_target_mag ( )

set target_mag from ScData (parsed from STRU file)

Set target magnetic moments from ScData (parsed from STRU file).

Supports two specification modes:

  • mag_type=0: Direct Cartesian (mx, my, mz) in uB
  • mag_type=1: Spherical (|M|, theta, phi) converted to Cartesian: Mx = |M| * sin(theta) * cos(phi) My = |M| * sin(theta) * sin(phi) Mz = |M| * cos(theta) Angles are in degrees and converted to radians.

Near-zero components (< 1e-14) are explicitly set to 0.0 to avoid floating-point noise in constraint checks.

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

◆ set_target_mag() [2/3]

void spinconstrain::ScState::set_target_mag ( const ModuleBase::Vector3< double > * target_mag_in,
int nat_in )

set target_mag from variable

Here is the call graph for this function:

◆ set_target_mag() [3/3]

void spinconstrain::ScState::set_target_mag ( const std::vector< ModuleBase::Vector3< double > > & target_mag_in)

set target magnetic moment

Here is the call graph for this function:

◆ zero_Mi()

void spinconstrain::ScState::zero_Mi ( )

zero atomic magnetic moment

Reset all atomic magnetic moments to zero. Called before each Mi calculation.

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

Member Data Documentation

◆ alpha_trial_

double spinconstrain::ScState::alpha_trial_ = 0.0

Initial trial step size (Ry/uB^2), adaptively adjusted during loop.

◆ atomCounts

std::map<int, int> spinconstrain::ScState::atomCounts
private

Number of atoms per element type: {itype -> nat_itype}.

◆ atomLabels_

std::vector<std::string> spinconstrain::ScState::atomLabels_

Human-readable labels: "Fe_0", "Fe_1", etc.

◆ constrain_

std::vector<ModuleBase::Vector3<int> > spinconstrain::ScState::constrain_

Per-atom/component constraint flags: 0=free, 1=constrained.

◆ current_sc_thr_

double spinconstrain::ScState::current_sc_thr_ = 0.0

◆ decay_grad_

std::vector<double> spinconstrain::ScState::decay_grad_
private

Gradient decay thresholds converted to uB^2/Ry, per element type.

◆ direction_only_

bool spinconstrain::ScState::direction_only_ = false

If true, only optimize spin direction.

◆ escon_

double spinconstrain::ScState::escon_ = 0.0

Cached constraint energy from last cal_escon() call (Ry).

◆ is_Mi_converged

bool spinconstrain::ScState::is_Mi_converged = false

Has the magnetic moment converged in the current SCF iteration?

◆ lambda_

std::vector<ModuleBase::Vector3<double> > spinconstrain::ScState::lambda_
private

Lagrange multipliers (Ry/uB) per atom, 3 components.

◆ lnchiCounts

std::map<int, std::map<int, int> > spinconstrain::ScState::lnchiCounts
private

{itype -> {L -> nchi}}: angular momentum channels

◆ Mi_

std::vector<ModuleBase::Vector3<double> > spinconstrain::ScState::Mi_
private

Current computed magnetic moments (uB) per atom.

◆ npol_

int spinconstrain::ScState::npol_ = 1

Number of spinor components: 1 for nspin=2, 2 for nspin=4.

◆ nsc_

int spinconstrain::ScState::nsc_ = 0

Maximum number of inner lambda optimization steps.

◆ nsc_min_

int spinconstrain::ScState::nsc_min_ = 0

Minimum steps before early exit checks (gradient decay).

◆ nspin_

int spinconstrain::ScState::nspin_ = 0

Spin type: 2=collinear, 4=non-collinear.

◆ orbitalCounts

std::map<int, int> spinconstrain::ScState::orbitalCounts
private

Number of orbitals per element type: {itype -> nw_itype}.

◆ restrict_current_

double spinconstrain::ScState::restrict_current_ = 0.0

Maximum allowed lambda change per step (Ry/uB).

◆ sc_drop_thr_

double spinconstrain::ScState::sc_drop_thr_ = 1e-3

Fraction of initial RMS for adaptive threshold.

◆ sc_thr_

double spinconstrain::ScState::sc_thr_ = 0.0

Convergence threshold for RMS(Mi - M_target) in uB.

◆ ScData

std::map<int, std::vector<ScAtomData> > spinconstrain::ScState::ScData
private

Raw constraint data indexed by element type (itype).

◆ ScDecayGrad

std::map<int, double> spinconstrain::ScState::ScDecayGrad
private

Gradient decay thresholds (uB^2/eV) per element type.

◆ target_mag_

std::vector<ModuleBase::Vector3<double> > spinconstrain::ScState::target_mag_

Target magnetic moments (uB) per atom.


PUBLIC FIELDS - transitional, accessed directly by the lambda loop and Mi accumulation code. Will be reduced to accessors in a

later refactoring step.

◆ tpiba

double spinconstrain::ScState::tpiba = 0.0

2*pi/a lattice constant scaling factor, saved from UnitCell


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