Skip to content

Commit 7dd70a6

Browse files
authored
optimize
1 parent a14db21 commit 7dd70a6

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

source/source_hamilt/module_surchem/cal_vel.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include "source_hamilt/module_xc/xc_functional.h"
33
#include "source_io/module_parameter/parameter.h"
44
#include "surchem.h"
5-
#include <cmath>
65
#include <vector>
76
#include <algorithm>
87
#include <iostream>
@@ -45,10 +44,9 @@ void surchem::cal_smpbe_physics(const int nrxx,
4544
double u = z * beta * phi_R[ir];
4645

4746
// 限制 u 的范围防止溢出 (Though ABACUS uses double, safety first)
48-
if(u > 20.0) u = 20.0;
49-
if(u < -20.0) u = -20.0;
47+
u = std::max(-20.0, std::min(u, 20.0));
5048

51-
double exp_u = std::exp(u);
49+
double exp_u = exp(u);
5250
double exp_neg_u = 1.0 / exp_u;
5351
double sinh_u = 0.5 * (exp_u - exp_neg_u);
5452
double cosh_u = 0.5 * (exp_u + exp_neg_u);
@@ -108,7 +106,12 @@ void cal_dielectric_saturation(const int nrxx,
108106
if(x < 1e-4) {
109107
langevin = x / 3.0;
110108
} else {
111-
langevin = (1.0 / tanh(x)) - (1.0 / x);
109+
// 优化尝试:利用 libm::exp 替换 tanh
110+
// tanh(x) = 1 - 2 / (exp(2x) + 1)
111+
double exp_2x = exp(2.0 * x);
112+
double tanh_x = 1.0 - 2.0 / (exp_2x + 1.0);
113+
114+
langevin = (1.0 / tanh_x) - (1.0 / x);
112115
}
113116

114117
double term_dipole = 0.0;
@@ -140,6 +143,7 @@ void shape_gradn(const double* PS_TOTN_real, const ModulePW::PW_Basis* rho_basis
140143
// Gaussian error function derivative chain rule
141144
epr_z = log(std::max(PS_TOTN_real[ir], min) / PARAM.inp.nc_k) / sqrt(2) / PARAM.inp.sigma_k;
142145
eprime[ir] = epr_c * exp(-pow(epr_z, 2)) / std::max(PS_TOTN_real[ir], min);
146+
143147
}
144148
}
145149

0 commit comments

Comments
 (0)