我正在尝试计算晶体结构的压力张量。为此,我必须通过下面的简化代码中的所有粒子对
do i=1, atom_number ! sum over atoms i
type1 = ATOMS(i)
do nj=POINT(i), POINT(i+1)-1 ! sum over atoms j of i's atoms list
j = LIST(nj)
type2 = ATOMS(j)
call get_ff_param(type1,type2,Aab,Bab,Cab,Dab)
call distance_sqr2(i,j,r,VECT_R)
call gettensor_rij(i,j,T)
r = sqrt(r)
if (r<coub_cutoff) then
local_sum_real(id+1) = local_sum_real(id+1) + ( erfc(alpha_ewald*r)
derivative = -( erfc(alpha_ewald*r)
ff = - (1.0d0/r)*derivative
STRESS_EWALDD = STRESS_EWALDD + ff * T ! A 3x3 matrix
FDX(i) = FDX(i) + VECT_R(1) * ff
FDY(i) = FDY(i) + VECT_R(2) * ff
FDZ(i) = FDZ(i) + VECT_R(3) * ff
FDX(j) = FDX(j) - VECT_R(1) * ff
FDY(j) = FDY(j) - VECT_R(2) * ff
FDZ(j) = FDZ(j) - VECT_R(3) * ff
end if
end do ! sum over atoms j
sum_kin = sum_kin + ATMMASS(i) * (VX(i)**2 + VY(i)**2 + VZ(i)**2)
call gettensor_v(i,Q)
STRESS_KINETIC = STRESS_KINETIC + ATMMASS(i) * Q
end do
我尝试使用“paralle do”指令并行化这个双循环,但在张量 STRESS_EWALDD 和力 FDX 方面遇到了一些问题......因此我尝试为每个线程手动分配一些粒子,如以下代码中的但仍然得到错误的张量值。
!$omp parallel private(id,j,i,nj,type1,type2,T,Q,r,VECT_R,derivative,Aab,Bab,Cab,Dab,ff)
id = omp_get_thread_num()
do i=id+1, atom_number,nthreads ! sum over atoms i
type1 = ATOMS(i)
do nj=POINT(i), POINT(i+1)-1 ! sum over atoms j of i's atoms list
j = LIST(nj)
type2 = ATOMS(j)
call get_ff_param(type1,type2,Aab,Bab,Cab,Dab)
call distance_sqr2(i,j,r,VECT_R)
call gettensor_rij(i,j,T)
r = sqrt(r)
if (r<coub_cutoff) then
local_sum_real(id+1) = local_sum_real(id+1) + ( erfc(alpha_ewald*r)
derivative = -( erfc(alpha_ewald*r)
ff = - (1.0d0/r)*derivative
local_STRESS_EWALDD(id+1,:,:) = local_STRESS_EWALDD(id+1,:,:) + ff * T
FDX(i) = FDX(i) + VECT_R(1) * ff
FDY(i) = FDY(i) + VECT_R(2) * ff
FDZ(i) = FDZ(i) + VECT_R(3) * ff
FDX(j) = FDX(j) - VECT_R(1) * ff
FDY(j) = FDY(j) - VECT_R(2) * ff
FDZ(j) = FDZ(j) - VECT_R(3) * ff
end if
end do ! sum over atoms j
local_sum_kin(id+1) = local_sum_kin(id+1) + ATMMASS(i) * (VX(i)**2 + VY(i)**2 + VZ(i)**2)
call gettensor_v(i,Q)
local_STRESS_KINETIC(id+1,:,:) = local_STRESS_KINETIC(id+1,:,:) + ATMMASS(i) * Q
end do ! sum over atoms i
!$omp end parallel
do i=1,nthreads
sum_real = sum_real + local_sum_real(i)
sum_virial_buck = sum_virial_buck + local_sum_virial_buck(i)
STRESS_BUCK = STRESS_BUCK + local_STRESS_BUCK(i,:,:)
STRESS_EWALDD = STRESS_EWALDD + local_STRESS_EWALDD(i,:,:)
sum_buck = sum_buck + local_sum_buck(i)
sum_kin = sum_kin + local_sum_kin(i)
STRESS_KINETIC = STRESS_KINETIC + local_STRESS_KINETIC(i,:,:)
end do
标量和 STRESS_KINETIC 值是正确的,但 STRESS_EWALDD 是错误的,我不知道为什么。到目前为止,我对力量一无所知。所以我真的很感激这里的一些打击。谢谢,
Éric.