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

Utility vector/array operations for per-atom 3D vector arrays. More...

#include <cmath>
#include <complex>
#include <vector>
#include <ostream>
#include "source_base/vector3.h"
Include dependency graph for basic_funcs.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

double maxval_abs_2d (const std::vector< ModuleBase::Vector3< double > > &array)
 Find the maximum absolute value across all atoms and components.
std::pair< int, int > maxloc_abs_2d (const std::vector< ModuleBase::Vector3< double > > &array)
 Find the maximum absolute value and its (atom, component) index.
template<typename T>
T sum_2d (const std::vector< ModuleBase::Vector3< T > > &array)
 Sum of all elements across all atoms and components.
void scalar_multiply_2d (const std::vector< ModuleBase::Vector3< double > > &array, double scalar, std::vector< ModuleBase::Vector3< double > > &result)
 Element-wise scalar multiplication: result = scalar * array.
void add_scalar_multiply_2d (const std::vector< ModuleBase::Vector3< double > > &array_1, const std::vector< ModuleBase::Vector3< double > > &array_2, double scalar, std::vector< ModuleBase::Vector3< double > > &result)
 Element-wise fused multiply-add: result = array_1 + scalar * array_2.
void subtract_2d (const std::vector< ModuleBase::Vector3< double > > &array_1, const std::vector< ModuleBase::Vector3< double > > &array_2, std::vector< ModuleBase::Vector3< double > > &result)
 Element-wise subtraction: result = array_1 - array_2.
void fill_scalar_2d (double scalar, std::vector< ModuleBase::Vector3< double > > &result)
 Fill all elements with a scalar value.
void where_fill_scalar_2d (const std::vector< ModuleBase::Vector3< int > > &array_mask, int mask, double scalar, std::vector< ModuleBase::Vector3< double > > &result)
 Conditional fill: if mask[iat][ic] == value, set result[iat][ic] = scalar.
void where_fill_scalar_else_2d (const std::vector< ModuleBase::Vector3< int > > &array_mask, int mask, double scalar, const std::vector< ModuleBase::Vector3< double > > &rest, std::vector< ModuleBase::Vector3< double > > &result)
 Conditional fill with else branch: if mask == value, set scalar; otherwise copy from rest.
void print_2d (const std::string info, const std::vector< ModuleBase::Vector3< double > > &array, const int nspin, const double unit_convert=1.0, std::ostream &ofs=std::cout)
 Formatted print of a 2D array.

Detailed Description

Utility vector/array operations for per-atom 3D vector arrays.

Data structure
All functions operate on std::vector<ModuleBase::Vector3<double>>, which represents a 2D array of shape [nat][3] where:
  • First index: atom index (iat = 0 to nat-1)
  • Second index: component (x=0, y=1, z=2)

These are NumPy-style element-wise operations used throughout the lambda optimization loop for manipulating magnetic moments, lambda values, search directions, and constraint masks.

Function Documentation

◆ add_scalar_multiply_2d()

void add_scalar_multiply_2d ( const std::vector< ModuleBase::Vector3< double > > & array_1,
const std::vector< ModuleBase::Vector3< double > > & array_2,
double scalar,
std::vector< ModuleBase::Vector3< double > > & result )

Element-wise fused multiply-add: result = array_1 + scalar * array_2.

Here is the caller graph for this function:

◆ fill_scalar_2d()

void fill_scalar_2d ( double scalar,
std::vector< ModuleBase::Vector3< double > > & result )

Fill all elements with a scalar value.

Here is the caller graph for this function:

◆ maxloc_abs_2d()

std::pair< int, int > maxloc_abs_2d ( const std::vector< ModuleBase::Vector3< double > > & array)

Find the maximum absolute value and its (atom, component) index.

Returns
pair<iat, ic> of the element with maximum absolute value
Here is the caller graph for this function:

◆ maxval_abs_2d()

double maxval_abs_2d ( const std::vector< ModuleBase::Vector3< double > > & array)

Find the maximum absolute value across all atoms and components.

Returns
max(|array[iat][ic]|) for all iat, ic
Here is the caller graph for this function:

◆ print_2d()

void print_2d ( const std::string info,
const std::vector< ModuleBase::Vector3< double > > & array,
const int nspin,
const double unit_convert = 1.0,
std::ostream & ofs = std::cout )

Formatted print of a 2D array.

Parameters
infoHeader string
arrayData to print
nspinSpin type: 2=z-only, 4=xyz
unit_convertMultiplicative factor (e.g., Ry_to_eV for unit conversion)
ofsOutput stream (default: stdout)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ scalar_multiply_2d()

void scalar_multiply_2d ( const std::vector< ModuleBase::Vector3< double > > & array,
double scalar,
std::vector< ModuleBase::Vector3< double > > & result )

Element-wise scalar multiplication: result = scalar * array.

Here is the caller graph for this function:

◆ subtract_2d()

void subtract_2d ( const std::vector< ModuleBase::Vector3< double > > & array_1,
const std::vector< ModuleBase::Vector3< double > > & array_2,
std::vector< ModuleBase::Vector3< double > > & result )

Element-wise subtraction: result = array_1 - array_2.

Here is the caller graph for this function:

◆ sum_2d()

template<typename T>
T sum_2d ( const std::vector< ModuleBase::Vector3< T > > & array)

Sum of all elements across all atoms and components.

Template Parameters
TNumeric type (int or double)
Returns
sum(array[iat][ic]) for all iat, ic
Here is the caller graph for this function:

◆ where_fill_scalar_2d()

void where_fill_scalar_2d ( const std::vector< ModuleBase::Vector3< int > > & array_mask,
int mask,
double scalar,
std::vector< ModuleBase::Vector3< double > > & result )

Conditional fill: if mask[iat][ic] == value, set result[iat][ic] = scalar.

Used to mask unconstrained components to zero: where_fill_scalar_2d(constrain_, 0, 0.0, delta_spin) sets delta_spin[ia][ic] = 0 where constrain[ia][ic] == 0.

Here is the caller graph for this function:

◆ where_fill_scalar_else_2d()

void where_fill_scalar_else_2d ( const std::vector< ModuleBase::Vector3< int > > & array_mask,
int mask,
double scalar,
const std::vector< ModuleBase::Vector3< double > > & rest,
std::vector< ModuleBase::Vector3< double > > & result )

Conditional fill with else branch: if mask == value, set scalar; otherwise copy from rest.

Used to create masked copies: where_fill_scalar_else_2d(constrain_, 0, 0.0, lambda_, initial_lambda) sets initial_lambda[ia][ic] = 0 if unconstrained, else lambda_[ia][ic].

Here is the caller graph for this function: