在Fortran中运行一段代码时,我得到一些奇怪的行为(很可能是我引起的)。
这是代码部分:
SUBROUTINE BMCalc(a_lat_in, unit_vector_in, atoms_in_i, atoms_in_dp, &
rcut_in, rcut_nl_in, &
expansion_in, perturb_id, perturb_xyz, &
result_arr)
REAL(8), INTENT(IN) :: a_lat_in
REAL(8), INTENT(IN) :: rcut_in
REAL(8), INTENT(IN) :: rcut_nl_in
REAL(8), INTENT(IN), DIMENSION(1:3,1:3) :: unit_vector_in
INTEGER, INTENT(IN), DIMENSION(:) :: atoms_in_i
REAL(8), INTENT(IN), DIMENSION(:,:) :: atoms_in_dp
INTEGER, INTENT(IN), DIMENSION(1:3) :: expansion_in
INTEGER, INTENT(IN), DIMENSION(:) :: perturb_id
REAL(8), INTENT(IN), DIMENSION(:,:) :: perturb_xyz
REAL(8), INTENT(OUT), DIMENSION(:) :: result_arr
INTEGER :: n
REAL(8), DIMENSION(1:3,1:3) :: strain
REAL(8), DIMENSION(1:3,1:3) :: unit_vector
INTEGER :: threads, thread_id
REAL(8), DIMENSION(1:size(result_arr,1)) :: t_result_arr
strain(1,1) = 1.00D0
strain(1,2) = 0.0D0
strain(1,3) = 0.0D0
strain(2,1) = 0.0D0
strain(2,2) = 1.00D0
strain(2,3) = 0.0D0
strain(3,1) = 0.0D0
strain(3,2) = 0.0D0
strain(3,3) = 1.00D0
!$OMP PARALLEL DO PRIVATE(strain, unit_vector, t_result_arr)
SHARED(a_lat_in, unit_vector_in)
Do n=1,17
strain(1,1) = 1.0D0 + (n-8) * 0.001D0
strain(2,2) = 1.0D0 + (n-8) * 0.001D0
strain(3,3) = 1.0D0 + (n-8) * 0.001D0
unit_vector = matmul(strain,unit_vector_in)
Call FullCalc(a_lat_in, unit_vector, atoms_in_i, atoms_in_dp, rcut_in, rcut_nl_in, &
expansion_in, perturb_id, perturb_xyz, 1, t_result_arr)
result_arr(9000+n) = strain(1,1)
result_arr(9100+n) = t_result_arr(1)
print *, t_result_arr(1)
End Do
!$OMP END PARALLEL DO
Do n=1,17
print *, n, result_arr(9000+n), result_arr(9100+n)
End Do
END SUBROUTINE BMCalc
然后将其打印出来:
1 0.99299999999999999 78.651434125940156
2 0.99399999999999999 78.651434125940156
"
16 1.0080000000000000 78.651434125940156
17 1.0089999999999999 78.651434125940156
如果我更改第一个循环以打印出该值,那么它将打印出正确的值。
修订代码:
result_arr(9000+n) = strain(1,1)
result_arr(9100+n) = t_result_arr(1)
print *, t_result_arr(1)
End Do
!$OMP END PARALLEL DO
值:
1 0.99299999999999999 -860.32575653894082
2 0.99399999999999999 -860.44626262594090
"
16 1.0080000000000000 -860.08077932360152
17 1.0089999999999999 -859.91597159744981
我过去曾发生过这种情况,这很可能是我做错了。我不确定如何描述问题,但是我正在寻找任何帮助或建议。