View difference between Paste ID: 4ZXMYJKJ and g7GkWMwa
SHOW: | | - or go back to the newest paste.
1-
module global_vars
1+
subroutine LU(n, m, A, P, Y)
2-
    integer :: N = 100, l = 1
2+
    integer, intent(in) :: n, m
3-
    real :: R = 15.0, V_0 = 4.0, a = 5.0, lk = 0.05, rk = 10.0, dk = 0.05, k
3+
    integer :: i, j, k, pivot, temp
4-
    save
4+
    double precision :: pivotValue
5-
end module
5+
    integer, dimension(:), intent(inout) :: P
6-
   
6+
    double precision, dimension(:), allocatable :: x
7-
function V(x)
7+
    double precision, dimension(:, :), intent(inout) :: A, Y
8-
    use global_vars
8+
    allocate(x(n))
9
    do i = 1, n
10-
    real :: V
10+
        !searching for max
11-
    real, intent(in) :: x
11+
        pivotValue = 0.0
12-
    if (x < a) then
12+
        pivot = -1
13-
        V = -V_0
13+
        do k = i, n
14-
    else
14+
            if (abs(A(k, i)) > pivotValue) then
15-
        V = 0.0
15+
                pivotValue = abs(A(k, i))
16
                pivot = k
17-
    return
17+
            end if
18-
end function
18+
19-
 
19+
        !is matrix invertible?
20-
function F(x)
20+
        if (abs(pivotValue) < 1.0/10000) then
21-
    use global_vars
21+
            print *, "matrix is invertiblle"
22
        end if
23-
    real :: F, V
23+
        !row swap
24-
    real, intent(in) :: x
24+
        !call dswap(1, P(i), 0, P(pivot), 0)
25-
    F = V(x) + l * (l + 1) / x**2 - k**2
25+
        temp = P(i)
26-
    return
26+
        P(i) = P(pivot)
27-
end function
27+
        P(pivot) = temp
28-
 
28+
        call dswap(n, A(i,1:n), 1, A(pivot, 1:n), 1)
29-
recursive function jl(x, l) result(ans)
29+
        do j = i + 1, n
30-
    real, intent(in) :: x
30+
            A(j, i) = A(j, i) / A(i, i)
31-
    real :: ans
31+
            do k = i + 1, n
32-
    integer, intent(in) :: l
32+
                A(j, k) = A(j, k) - A(j, i) * A(i, k)    
33-
    if (l == 0) then
33+
            end do
34-
        ans = sin(x) / x
34+
35-
    else if (l == 1) then
35+
36-
        ans = sin(x) / x**2 - cos(x) / x
36+
    !A = L + U - I
37-
    else
37+
    !Gauss
38-
        ans = (2 * (l - 1) + 1) / x * jl(x, l - 1) - jl(x, l - 2)
38+
    do k = 1, m
39
        !forward
40-
end function
40+
        do i = 1, n
41
            x(i) = Y(P(i), k)
42-
function jl_hat(x, l)
42+
            x(i) = x(i) - ddot(i-1, A(i,1:i-1), 1, x(1:i-1), 1)
43-
    real, intent(in) :: x
43+
            !do j = 1, i - 1
44-
    integer, intent(in) :: l
44+
            !    x(i) = x(i) - A(i, j) * x(j)
45-
    real :: jl, jl_hat
45+
            !end do
46-
    jl_hat = jl(x, l) * x
46+
47-
    return
47+
        !backward
48-
end function
48+
        do i = n, 1, -1
49
            if (i < n) then
50-
subroutine Simpson(h, f, N, ans)
50+
                x(i) = x(i) - ddot(n-i, x(i+1:n), 1, A(i,i+1:n), 1)
51
            end if
52-
    integer :: i, N
52+
            !do j = i + 1, n
53-
    real :: h
53+
            !    x(i) = x(i) - A(i, j) * x(j)
54-
    complex :: f(N), ans
54+
            !end do
55-
    ans = 0.0
55+
            x(i) = x(i) / A(i, i)
56-
    do i = 2, N - 1, 2
56+
57-
        ans = ans + f(i - 1) + 4.0 * f(i) + f(i + 1)
57+
        !answer
58
        do i = 1, n
59-
    ans = ans * h / 3.0
59+
            Y(i, k) = x(i)
60
        end do
61-
 
61+
62-
program scattering
62+
    deallocate(x)
63-
    use global_vars
63+
64
65-
    integer :: i, j, out = 42
65+
program lu_decomposition
66-
    real :: F, V, h, jl_hat,  pi = 3.1415926
66+
67-
    complex :: Al, delta
67+
    integer :: n, m, i, inp, outp
68-
    integer, allocatable :: ipiv(:)
68+
    integer, dimension(:), allocatable :: P, sP
69-
    complex, allocatable :: X(:),  mA(:, :)
69+
    double precision, dimension(:,:), allocatable :: A, sA, tempY, Y, sY
70-
    allocate(X(N))
70+
    interface
71-
    allocate(ipiv(N))
71+
        subroutine LU(n, m, A, P, Y)
72-
    allocate(mA(N, N))
72+
            integer, intent(in) :: n, m
73-
    h = R / N
73+
            integer :: i, j, k, pivot, temp
74-
    k = lk
74+
            double precision :: pivotValue
75-
    open(unit = out, file = "output.txt")
75+
            integer, dimension(:), intent(inout) :: P
76-
    do while (k <= rk)
76+
            double precision, dimension(:), allocatable :: x
77-
        !mA = -d^2/dr^2 + F(r) - k^2
77+
            double precision, dimension(:, :), intent(inout) :: A, Y
78-
        mA(:, :) = 0.0
78+
        end subroutine LU
79-
        do i = 1, N
79+
    end interface
80-
            if(i > 1) mA(i, i - 1) = 12.0 / h**2  - F(h * (i - 1))
80+
    !input
81-
            mA(i, i) = -24.0 / h**2 - 10.0 * F(h * i)
81+
    inp = 42
82-
            if(i < N) mA(i, i + 1) = 12.0 / h**2 - F(h * (i + 1))
82+
    outp = 43
83
    open(unit = inp, file = "input.txt")
84-
        mA(N, N) = mA(N, N) + exp((0.0, 1.0) * k * h) * (12.0 / h**2 - F(h * (N + 1)))
84+
    read(inp, *) n, m
85-
        !X = -V(r)jl(kr)
85+
    allocate(P(n))
86-
        do i = 1, N
86+
    do i = 1, n
87-
            X(i) = 10.0 * V(h * i) * jl_hat(k * i * h, l)
87+
        P(i) = i
88-
            if(i > 1) X(i) = X(i) + V(h * (i - 1)) * jl_hat(k * (i - 1) * h, l)
88+
89-
            if(i < n) X(i) = X(i) + V(h * (i + 1)) * jl_hat(k * (i + 1) * h, l)
89+
    allocate(sP(n))
90
    sP = P
91-
        !X = chi_l
91+
    allocate(A(n, n))
92-
        call cgesv(N, 1, mA, N, ipiv, X, N, i)
92+
    allocate(sA(n, n))
93-
        !X = jl(kr)V(r)f_l(r, k) = jl(kr)V(r)(jl(kr) + chi_l(r, k))
93+
    allocate(Y(n, m))
94-
        do i =1,n
94+
    allocate(sY(n, m))
95-
            X(i) = jl_hat(k * h * i, l) * V(h * i) * (jl_hat(k * h * i, l) + X(i))
95+
    allocate(tempY(m, n))
96
    read (inp, *) A
97-
        call Simpson(h, X, N, Al)
97+
    A = transpose(A)
98-
        Al = -1.0 * Al / k**2
98+
    sA = A
99-
        delta = log(1 + (0.0, 2.0) * k * Al) / (0.0, 2.0)
99+
    read (inp, *) tempY
100-
        write(out, *) k, real(delta)
100+
    Y = transpose(tempY)
101-
        k = k + dk
101+
    sY = Y
102
    close(inp)
103-
    close(out)
103+
    open(unit = outp, file = "output.txt")
104
    !LU decomposition
105
    call LU(n, m, A, P, Y)
106
    !LU output
107
    write(outp, *) "A"
108
    do i = 1, n
109
        write(outp, *) A(i, :)
110
    end do
111
    write(outp, *) "P"
112
    write(outp, *) P
113
    write(outp, *) "Y"
114
    do i = 1, n
115
        write(outp, "(1000F10.3)") Y(i, :)
116
    end do
117
    !sgesv
118
    call dgesv(n, m, sA, n, sP, sY, n, i)
119
    !sgesv output
120
    if (i .eq. 0) then
121
        print *, "successful sgesv execution"
122
    end if
123
    write(outp, *) "A by sgesv"
124
    do i = 1, n
125
        write(outp, *) sA(i, :)
126
    end do
127
    write(outp, *) "P by sgesv"
128
    write(outp, *) sP
129
    write(outp, *) "Y by sgesv"
130
    do i = 1, n
131
        write(outp, "(1000F10.3)") sY(i, :)
132
    end do
133
    close(outp)
134
    !memory
135
    deallocate(P)
136
    deallocate(sP)
137
    deallocate(A)
138
    deallocate(Y)
139
    deallocate(sY)
140
end program