ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
gram_schmidt_orth-inl.h
Go to the documentation of this file.
1//==========================================================
2// AUTHOR : Peize Lin
3// DATE : 2016-08-03
4//==========================================================
5
6#ifndef GRAM_SCHMIDT_ORTH_INL_H
7#define GRAM_SCHMIDT_ORTH_INL_H
8
9#include "gram_schmidt_orth.h"
10
11#include "mathzone.h"
13#include "math_integral.h" // mohan add 2021-04-03
14namespace ModuleBase
15{
16
17template<typename Func_Type, typename R_Type>
18Gram_Schmidt_Orth<Func_Type,R_Type>::Gram_Schmidt_Orth( const std::vector<R_Type> &rab_in, const Coordinate &coordinate_in )
19 :rab(rab_in),
20 coordinate(coordinate_in)
21{
22 if( Coordinate::Sphere == coordinate )
23 {
24 std::vector<R_Type> radial( rab.size() );
25 radial[0] = 0;
26 for( int ir=1; ir!=radial.size(); ++ir )
27 radial[ir] = radial[ir-1] + rab[ir-1];
28 this->radial_2 = Mathzone::Pointwise_Product( radial, radial );
29 }
30}
31
32template<typename Func_Type, typename R_Type>
33std::vector<std::vector<Func_Type>> Gram_Schmidt_Orth<Func_Type,R_Type>::cal_orth(
34 const std::vector<std::vector<Func_Type>> &func,
35 const Func_Type norm_threshold )
36{
37 // Schmidt: hn to en
38 // e1 = h1 / ||h1||
39 // gn = hn - \sum{i=1 to n-1}(hn,ei)ei
40 // en = gn / ||gn||
41
42 std::vector<std::vector<Func_Type>> func_new;
43
44 for( size_t if1=0; if1!=func.size(); ++if1 )
45 {
46 //use CGS2 algorithm to do twice orthogonalization
47 //DOI 10.1007/s00211-005-0615-4
48 std::vector<Func_Type> func_try = func[if1];
49 for(int niter=0;niter<3;niter++)
50 {
51 std::vector<Func_Type> func_tmp = func_try;
52 for( size_t if_minus=0; if_minus!=func_new.size(); ++if_minus )
53 {
54 // (hn,ei)
55 const std::vector<Func_Type> && mul_func = Mathzone::Pointwise_Product( func_tmp, func_new[if_minus] );
56 const Func_Type in_product = cal_norm(mul_func);
57
58 // hn - (hn,ei)ei
59 BlasConnector::axpy( mul_func.size(), -in_product, ModuleBase::GlobalFunc::VECTOR_TO_PTR(func_new[if_minus]), 1, ModuleBase::GlobalFunc::VECTOR_TO_PTR(func_try), 1);
60 }
61 }
62
63 // ||gn||
64 const std::vector<Func_Type> && func_2 = Mathzone::Pointwise_Product( func_try, func_try );
65 const Func_Type norm = sqrt(cal_norm(func_2));
66
67 // en = gn / ||gn||
68 // if ||gn|| too small, filter out
69 if( norm >= norm_threshold )
70 {
71 BlasConnector::scal( func_try.size(), 1.0/norm, ModuleBase::GlobalFunc::VECTOR_TO_PTR(func_try), 1 );
72 func_new.push_back( func_try );
73 }
74 }
75 return func_new;
76}
77
78// cal ||f||
79template<typename Func_Type, typename R_Type>
80Func_Type Gram_Schmidt_Orth<Func_Type,R_Type>::cal_norm( const std::vector<Func_Type> &f )
81{
82 Func_Type norm = 0.0;
83 switch( this->coordinate )
84 {
85 case Coordinate::Cartesian:
86 {
87 Integral::Simpson_Integral( f.size(), ModuleBase::GlobalFunc::VECTOR_TO_PTR(f), ModuleBase::GlobalFunc::VECTOR_TO_PTR(rab), norm);
88 break;
89 }
90 case Coordinate::Sphere:
91 {
92 const std::vector<Func_Type> &&tmp_func = Mathzone::Pointwise_Product( f, radial_2 );
93 Integral::Simpson_Integral( f.size(), ModuleBase::GlobalFunc::VECTOR_TO_PTR(tmp_func), ModuleBase::GlobalFunc::VECTOR_TO_PTR(rab), norm);
94 break;
95 }
96 default:
97 {
98 throw std::invalid_argument("coordinate must be Cartesian or Sphere "+std::string(__FILE__)+" line "+std::to_string(__LINE__));
99 break;
100 }
101 }
102 return norm;
103}
104
105}
106
107#endif // GRAM_SCHMIDT_ORTH_INL_H
static void axpy(const int n, const float alpha, const float *X, const int incX, float *Y, const int incY, base_device::AbacusDevice_t device_type=base_device::AbacusDevice_t::CpuDevice)
Definition blas_connector_vector.cpp:18
static void scal(const int n, const float alpha, float *X, const int incX, base_device::AbacusDevice_t device_type=base_device::AbacusDevice_t::CpuDevice)
Definition blas_connector_vector.cpp:80
Gram_Schmidt_Orth(const std::vector< R_Type > &rab, const Coordinate &coordinate)
Definition gram_schmidt_orth-inl.h:18
Func_Type cal_norm(const std::vector< Func_Type > &f)
Definition gram_schmidt_orth-inl.h:80
Coordinate
Definition gram_schmidt_orth.h:19
std::vector< std::vector< Func_Type > > cal_orth(const std::vector< std::vector< Func_Type > > &func, const Func_Type norm_threshold=std::numeric_limits< Func_Type >::min())
Definition gram_schmidt_orth-inl.h:33
static void Simpson_Integral(const int mesh, const double *const func, const double *const rab, double &asum)
simpson integral.
Definition math_integral.cpp:66
static std::vector< Type > Pointwise_Product(const std::vector< Type > &f1, const std::vector< Type > &f2)
Pointwise product of two vectors with same size.
Definition mathzone.h:38
Definition array_pool.h:6
double norm(const Vec3 &v)
Definition test_partition.cpp:25
double func(const Vec3 &r, const std::vector< Vec3 > &R, const std::vector< double > &a, const std::vector< double > &n)
Definition test_partition.cpp:50