aboutsummaryrefslogtreecommitdiff
path: root/code/sunlab/fortran_src/fortran_library.f90
blob: d47799502f0675fd8373ba571afa4964776f6f04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
! fortran_library

! Mean-Squared Displacement for a time lag `t`
function msd(a, t) result(b)
    implicit none
    
    real(kind=8), intent(in) :: a(:,:)
    integer(kind=8), intent(in) :: t
    integer(kind=8) :: i, j
    real(kind=8) :: s(size(a,1)-t, size(a,2))
    real(kind=8) :: b(size(a,1)-t)

    s(:, :) = a((t + 1):(size(a,1)), :) - a(1:(size(a,1)-t),:)
    do i = 1, size(a, 1)-t
        b(i) = 0.0
        do j = 1, size(a, 2)
            b(i) = b(i) + s(i, j) ** 2
        end do
        b(i) = sqrt(b(i))
    end do
end function msd

! Mean-Squared Displacement for a time lag `t`
function mmsd(a, t) result(z)
    implicit none
    
    real(kind=8), intent(in) :: a(:,:)
    integer(kind=8), intent(in) :: t
    integer(kind=8) :: i, j
    real(kind=8) :: s(size(a,1)-t, size(a,2))
    real(kind=8) :: b(size(a,1)-t)
    real(kind=8) :: z

    s(:, :) = a((t + 1):(size(a,1)), :) - a(1:(size(a,1)-t),:)
    do i = 1, size(a, 1)-t
        b(i) = 0.0
        do j = 1, size(a, 2)
            b(i) = b(i) + s(i, j) ** 2
        end do
        b(i) = sqrt(b(i))
    end do
    z = sum(b(:)) / (size(b, 1))
end function mmsd

! Mean-Squared Displacement for time lags up to `T`
function msds(a, Ts) result(b)
    implicit none
    
    real(kind=8), intent(in) :: a(:,:)
    integer(kind=8), intent(in) :: Ts
    integer(kind=8) :: i, j
    integer(kind=8) :: t
    real(kind=8) :: s(size(a,1), size(a,2))
    real(kind=8) :: b(size(a,1),Ts+1)

    do t = 0, Ts
        s(:, :) = a((t + 1):(size(a,1)), :) - a(1:(size(a,1)-t),:)
        do i = 1, size(a, 1)
            b(i,t+1) = 0.0
            if (i <= size(a, 1) - t) then
                do j = 1, size(a, 2)
                    b(i,t+1) = b(i,t+1) + s(i, j) ** 2
                end do
                b(i,t+1) = sqrt(b(i,t+1))
            end if
        end do
    end do
end function msds

! Mean-Squared Displacement for time lags up to `T`
! Return the mean for each time lag
function mmsds(a, Ts) result(z)
    implicit none
    
    real(kind=8), intent(in) :: a(:,:)
    integer(kind=8), intent(in) :: Ts
    integer(kind=8) :: i, j
    integer(kind=8) :: t
    real(kind=8) :: s(size(a,1), size(a,2))
    real(kind=8) :: b(size(a,1),Ts+1)
    real(kind=8) :: z(Ts+1)

    do t = 0, Ts
        s(:, :) = a((t + 1):(size(a,1)), :) - a(1:(size(a,1)-t),:)
        do i = 1, size(a, 1)
            b(i,t+1) = 0.0
            if (i <= size(a, 1) - t) then
                do j = 1, size(a, 2)
                    b(i,t+1) = b(i,t+1) + s(i, j) ** 2
                end do
                b(i,t+1) = sqrt(b(i,t+1))
            end if
        end do
        z(t+1) = sum(b(1:(size(a, 1) - t),t+1)) / (size(a, 1) - t)
    end do
end function mmsds