Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source/Makefile.Objects
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,7 @@ OBJS_SRCPW=h_ewald_pw.o\
sto_hsolver_pw.o\
sto_iter.o\
sto_che.o\
sto_spectral_bounds.o\
sto_wf.o\
sto_func.o\
sto_forces.o\
Expand Down
101 changes: 73 additions & 28 deletions source/source_base/math_chebyshev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,31 @@ REAL Chebyshev<REAL, Device>::ddot_real(const std::complex<REAL>* psi_L,
return result;
}

template <typename REAL, typename Device>
void Chebyshev<REAL, Device>::prepare_nodes()
{
if (!midpoint_nodes.empty()) { return; }
midpoint_nodes.resize(norder2);
trapezoid_nodes.resize(norder2);
midpoint_cos.resize(norder);
midpoint_sin.resize(norder);
for (int i = 0; i < norder2; ++i)
{
midpoint_nodes[i] = cos((i + 0.5) * ModuleBase::TWO_PI / norder2);
trapezoid_nodes[i] = cos(i * ModuleBase::TWO_PI / norder2);
}
for (int i = 0; i < norder; ++i)
{
const REAL phi = i * ModuleBase::PI / norder2;
midpoint_cos[i] = cos(phi);
midpoint_sin[i] = sin(phi);
}
}

template <typename REAL, typename Device>
void Chebyshev<REAL, Device>::calcoef_real(std::function<REAL(REAL)> fun)
{
this->prepare_nodes();
std::complex<REAL>* pcoef = (std::complex<REAL>*)this->fftw.ccoef;

// three point = 2/3 M + 1/3 T;
Expand All @@ -167,22 +189,21 @@ void Chebyshev<REAL, Device>::calcoef_real(std::function<REAL(REAL)> fun)
//-----------------------------------------------
for (int i = 0; i < norder2; ++i)
{
this->fftw.dcoef[i] = fun((REAL)cos((i + 0.5) * ModuleBase::TWO_PI / norder2));
this->fftw.dcoef[i] = fun(midpoint_nodes[i]);
}

// this->fftw.dcoef --FFT--> fftw.pcoef
this->fftw.execute_fftw();

for (int i = 0; i < norder; ++i)
{
REAL phi = i * ModuleBase::PI / norder2;
if (i == 0)
{
coefr_cpu[i] = (cos(phi) * pcoef[i].real() + sin(phi) * pcoef[i].imag()) / norder2 * 2 / 3;
coefr_cpu[i] = (midpoint_cos[i] * pcoef[i].real() + midpoint_sin[i] * pcoef[i].imag()) / norder2 * 2 / 3;
}
else
{
coefr_cpu[i] = (cos(phi) * pcoef[i].real() + sin(phi) * pcoef[i].imag()) / norder2 * 4 / 3;
coefr_cpu[i] = (midpoint_cos[i] * pcoef[i].real() + midpoint_sin[i] * pcoef[i].imag()) / norder2 * 4 / 3;
}
}

Expand All @@ -191,7 +212,7 @@ void Chebyshev<REAL, Device>::calcoef_real(std::function<REAL(REAL)> fun)
//-----------------------------------------------
for (int i = 0; i < norder2; ++i)
{
this->fftw.dcoef[i] = fun(cos(i * ModuleBase::TWO_PI / norder2));
this->fftw.dcoef[i] = fun(trapezoid_nodes[i]);
}

// this->fftw.dcoef --FFT--> fftw.pcoef
Expand Down Expand Up @@ -221,6 +242,7 @@ void Chebyshev<REAL, Device>::calcoef_real(std::function<REAL(REAL)> fun)
template <typename REAL, typename Device>
void Chebyshev<REAL, Device>::calcoef_complex(std::function<std::complex<REAL>(std::complex<REAL>)> fun)
{
this->prepare_nodes();
std::complex<REAL>* pcoef = (std::complex<REAL>*)this->fftw.ccoef;

// three point = 2/3 M + 1/3 T;
Expand All @@ -229,37 +251,35 @@ void Chebyshev<REAL, Device>::calcoef_complex(std::function<std::complex<REAL>(s
//-----------------------------------------------
for (int i = 0; i < norder2; ++i)
{
this->fftw.dcoef[i] = fun(cos((i + 0.5) * ModuleBase::TWO_PI / norder2)).real();
this->fftw.dcoef[i] = fun(midpoint_nodes[i]).real();
}
this->fftw.execute_fftw();
for (int i = 0; i < norder; ++i)
{
REAL phi = i * ModuleBase::PI / norder2;
if (i == 0)
{
coefc_cpu[i].real((cos(phi) * pcoef[i].real() + sin(phi) * pcoef[i].imag()) / norder2 * 2 / 3);
coefc_cpu[i].real((midpoint_cos[i] * pcoef[i].real() + midpoint_sin[i] * pcoef[i].imag()) / norder2 * 2 / 3);
}
else
{
coefc_cpu[i].real((cos(phi) * pcoef[i].real() + sin(phi) * pcoef[i].imag()) / norder2 * 4 / 3);
coefc_cpu[i].real((midpoint_cos[i] * pcoef[i].real() + midpoint_sin[i] * pcoef[i].imag()) / norder2 * 4 / 3);
}
}

for (int i = 0; i < norder2; ++i)
{
this->fftw.dcoef[i] = fun(cos((i + 0.5) * ModuleBase::TWO_PI / norder2)).imag();
this->fftw.dcoef[i] = fun(midpoint_nodes[i]).imag();
}
this->fftw.execute_fftw();
for (int i = 0; i < norder; ++i)
{
REAL phi = i * ModuleBase::PI / norder2;
if (i == 0)
{
coefc_cpu[i].imag((cos(phi) * pcoef[i].real() + sin(phi) * pcoef[i].imag()) / norder2 * 2 / 3);
coefc_cpu[i].imag((midpoint_cos[i] * pcoef[i].real() + midpoint_sin[i] * pcoef[i].imag()) / norder2 * 2 / 3);
}
else
{
coefc_cpu[i].imag((cos(phi) * pcoef[i].real() + sin(phi) * pcoef[i].imag()) / norder2 * 4 / 3);
coefc_cpu[i].imag((midpoint_cos[i] * pcoef[i].real() + midpoint_sin[i] * pcoef[i].imag()) / norder2 * 4 / 3);
}
}

Expand All @@ -268,7 +288,7 @@ void Chebyshev<REAL, Device>::calcoef_complex(std::function<std::complex<REAL>(s
//-----------------------------------------------
for (int i = 0; i < norder2; ++i)
{
this->fftw.dcoef[i] = fun(cos(i * ModuleBase::TWO_PI / norder2)).real();
this->fftw.dcoef[i] = fun(trapezoid_nodes[i]).real();
}
this->fftw.execute_fftw();
for (int i = 0; i < norder; ++i)
Expand All @@ -285,7 +305,7 @@ void Chebyshev<REAL, Device>::calcoef_complex(std::function<std::complex<REAL>(s

for (int i = 0; i < norder2; ++i)
{
this->fftw.dcoef[i] = fun(cos(i * ModuleBase::TWO_PI / norder2)).imag();
this->fftw.dcoef[i] = fun(trapezoid_nodes[i]).imag();
}
this->fftw.execute_fftw();
for (int i = 0; i < norder; ++i)
Expand All @@ -311,6 +331,7 @@ void Chebyshev<REAL, Device>::calcoef_complex(std::function<std::complex<REAL>(s
template <typename REAL, typename Device>
void Chebyshev<REAL, Device>::calcoef_pair(std::function<REAL(REAL)> fun1, std::function<REAL(REAL)> fun2)
{
this->prepare_nodes();
std::complex<REAL>* pcoef = (std::complex<REAL>*)this->fftw.ccoef;

// three point = 2/3 M + 1/3 T;
Expand All @@ -319,37 +340,35 @@ void Chebyshev<REAL, Device>::calcoef_pair(std::function<REAL(REAL)> fun1, std::
//-----------------------------------------------
for (int i = 0; i < norder2; ++i)
{
this->fftw.dcoef[i] = fun1(cos((i + 0.5) * ModuleBase::TWO_PI / norder2));
this->fftw.dcoef[i] = fun1(midpoint_nodes[i]);
}
this->fftw.execute_fftw();
for (int i = 0; i < norder; ++i)
{
REAL phi = i * ModuleBase::PI / norder2;
if (i == 0)
{
coefc_cpu[i].real((cos(phi) * pcoef[i].real() + sin(phi) * pcoef[i].imag()) / norder2 * 2 / 3);
coefc_cpu[i].real((midpoint_cos[i] * pcoef[i].real() + midpoint_sin[i] * pcoef[i].imag()) / norder2 * 2 / 3);
}
else
{
coefc_cpu[i].real((cos(phi) * pcoef[i].real() + sin(phi) * pcoef[i].imag()) / norder2 * 4 / 3);
coefc_cpu[i].real((midpoint_cos[i] * pcoef[i].real() + midpoint_sin[i] * pcoef[i].imag()) / norder2 * 4 / 3);
}
}

for (int i = 0; i < norder2; ++i)
{
this->fftw.dcoef[i] = fun2(cos((i + 0.5) * ModuleBase::TWO_PI / norder2));
this->fftw.dcoef[i] = fun2(midpoint_nodes[i]);
}
this->fftw.execute_fftw();
for (int i = 0; i < norder; ++i)
{
REAL phi = i * ModuleBase::PI / norder2;
if (i == 0)
{
coefc_cpu[i].imag((cos(phi) * pcoef[i].real() + sin(phi) * pcoef[i].imag()) / norder2 * 2 / 3);
coefc_cpu[i].imag((midpoint_cos[i] * pcoef[i].real() + midpoint_sin[i] * pcoef[i].imag()) / norder2 * 2 / 3);
}
else
{
coefc_cpu[i].imag((cos(phi) * pcoef[i].real() + sin(phi) * pcoef[i].imag()) / norder2 * 4 / 3);
coefc_cpu[i].imag((midpoint_cos[i] * pcoef[i].real() + midpoint_sin[i] * pcoef[i].imag()) / norder2 * 4 / 3);
}
}

Expand All @@ -358,7 +377,7 @@ void Chebyshev<REAL, Device>::calcoef_pair(std::function<REAL(REAL)> fun1, std::
//-----------------------------------------------
for (int i = 0; i < norder2; ++i)
{
this->fftw.dcoef[i] = fun1(cos(i * ModuleBase::TWO_PI / norder2));
this->fftw.dcoef[i] = fun1(trapezoid_nodes[i]);
}
this->fftw.execute_fftw();
for (int i = 0; i < norder; ++i)
Expand All @@ -375,7 +394,7 @@ void Chebyshev<REAL, Device>::calcoef_pair(std::function<REAL(REAL)> fun1, std::

for (int i = 0; i < norder2; ++i)
{
this->fftw.dcoef[i] = fun2(cos(i * ModuleBase::TWO_PI / norder2));
this->fftw.dcoef[i] = fun2(trapezoid_nodes[i]);
}
this->fftw.execute_fftw();
for (int i = 0; i < norder; ++i)
Expand Down Expand Up @@ -546,8 +565,6 @@ void Chebyshev<REAL, Device>::calpolyvec_complex(
assert(N >= 0 && LDA >= N);
const int ndmxt = LDA * m;

std::complex<REAL>* arraynp1 = polywaveout + 2 * ndmxt;
std::complex<REAL>* arrayn = polywaveout + ndmxt;
std::complex<REAL>* arrayn_1 = polywaveout;

std::complex<REAL>*tmpin = wavein, *tmpout = arrayn_1;
Expand All @@ -559,6 +576,14 @@ void Chebyshev<REAL, Device>::calpolyvec_complex(
tmpout += LDA;
}

if (norder == 1)
{
return;
}

std::complex<REAL>* arraynp1 = polywaveout + 2 * ndmxt;
std::complex<REAL>* arrayn = polywaveout + ndmxt;

// 1-st order
funA(arrayn_1, arrayn, m);

Expand All @@ -581,6 +606,25 @@ void Chebyshev<REAL, Device>::tracepolyA(
const int LDA,
const int m)
{
const std::function<void(const std::complex<REAL>*)> observe_final;
this->tracepolyA(funA, wavein, N, LDA, m, observe_final);
}

template <typename REAL, typename Device>
void Chebyshev<REAL, Device>::tracepolyA(
std::function<void(std::complex<REAL>*, std::complex<REAL>*, const int)> funA,
std::complex<REAL>* wavein,
const int N,
const int LDA,
const int m,
const std::function<void(const std::complex<REAL>*)>& observe_final)
{
polytrace[0] = this->ddot_real(wavein, wavein, N, LDA, m);
if (norder == 1)
{
if (observe_final) { observe_final(wavein); }
return;
}
std::complex<REAL>* arraynp1 = nullptr;
std::complex<REAL>* arrayn = nullptr;
std::complex<REAL>* arrayn_1 = nullptr;
Expand All @@ -604,7 +648,6 @@ void Chebyshev<REAL, Device>::tracepolyA(

funA(arrayn_1, arrayn, m);

polytrace[0] = this->ddot_real(wavein, wavein, N, LDA, m);
polytrace[1] = this->ddot_real(wavein, arrayn, N, LDA, m);

// more than 1-st orders
Expand All @@ -618,6 +661,8 @@ void Chebyshev<REAL, Device>::tracepolyA(
arraynp1 = tem;
}

if (observe_final) { observe_final(arrayn); }

delmem_complex_op()(arraynp1);
delmem_complex_op()(arrayn);
delmem_complex_op()(arrayn_1);
Expand Down
16 changes: 16 additions & 0 deletions source/source_base/math_chebyshev.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <complex>
#include <functional>
#include <vector>

namespace ModuleBase
{
Expand Down Expand Up @@ -140,6 +141,15 @@ class Chebyshev
const int LDA = 1,
const int m = 1);

// Observe T_{norder-1}(A) wavein before its workspace is released.
// The pointer uses this Chebyshev object's device and is valid only in the callback.
void tracepolyA(std::function<void(std::complex<REAL>*, std::complex<REAL>*, const int)> funA,
std::complex<REAL>* wavein,
const int N,
const int LDA,
const int m,
const std::function<void(const std::complex<REAL>*)>& observe_final);

// get T_n(x)
void getpolyval(REAL x, REAL* polyval, const int N);

Expand Down Expand Up @@ -225,6 +235,12 @@ class Chebyshev
const int m = 1);

private:
void prepare_nodes();
std::vector<REAL> midpoint_nodes;
std::vector<REAL> trapezoid_nodes;
std::vector<REAL> midpoint_cos;
std::vector<REAL> midpoint_sin;

Device* ctx = {};
base_device::DEVICE_CPU* cpu_ctx = {};
using ct_Device = typename container::PsiToContainer<Device>::type;
Expand Down
5 changes: 5 additions & 0 deletions source/source_base/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
abacus_disable_feature_definitions(__MPI)
abacus_copy_test_files(support)

addtest(
TARGET MODULE_BASE_chebyshev_final_vector
LIBS parameter base device container
SOURCES test_math_chebyshev.cpp)

# Retain the existing MPI-build scope of unrelated unit-test targets.
if(ENABLE_MPI)
addtest(
Expand Down
Loading
Loading