spx_math.F90 Source File


Contents

Source Code


Source Code

!> author: 左志华
!> date: 2022-07-02
!>
!> 数学模块
module spx_math

    use spx_kinds, only: rk
    implicit none

    private
    public :: get_distance, sqrt_eps, r13, r23, dim

    real(rk), parameter :: sqrt_eps = sqrt(epsilon(0.0_rk))  !! sqrt(epsilon)
    real(rk), parameter :: r13 = 1.0_rk/3.0_rk
    real(rk), parameter :: r23 = 2.0_rk/3.0_rk

#ifdef SPX3D
    integer, parameter :: dim = 3  !! 3 dimension
#else
    integer, parameter :: dim = 2  !! 2 dimension
#endif

contains

    !> 粒子之间的距离 (distance)
    !> @note \( \sqrt(x*x + y*y) \) 比 \( \sqrt(x^2 + y^2) \) 、norm2 和 hypot 更快 <br>
    !> 所以在密集计算中,应该避免使用 norm2 和 hypot 。
    pure subroutine get_distance(x, y, d, r)
        real(rk), intent(in), dimension(2) :: x, y  !! 两点坐标
        real(rk), intent(out), dimension(2) :: d  !! 坐标轴距离
        real(rk), intent(out) :: r  !! 欧式距离

        d = x - y; r = sqrt(d(1)*d(1) + d(2)*d(2))

    end subroutine get_distance

end module spx_math