[Fortran] 纯文本查看 复制代码
program main
use mod_assembly
use mod_solver
implicit none
integer n
real(kind=8),allocatable::a(:,:),b(:)
real(kind=8),allocatable::x(:)
n=40000
allocate(a(n,n),b(n),x(n))
call assembly(n,a,b)
call lu_crout(a,b,x,n)
end program main
module mod_assembly
contains
subroutine assembly(n,a,b)
implicit none
integer n
real(kind=8) a(n,n),b(n)
integer i,j
a=0.0_8
do i=1,n
do j=1,n
if (i.eq.j) then
a(i,j)=3.0_8
elseif (abs(i-j).eq.2) then
a(i,j)=1.0_8
elseif (abs(i-j).eq.1) then
a(i,j)=2.0_8
endif
enddo
enddo
b=1.0_8
return
end subroutine assembly
end module mod_assembly
module mod_solver
contains
subroutine lu_crout(A,b,xx,N)
implicit none
integer N
real(kind=8) A(n,n),b(n),xx(n)
real(kind=8) L(N,N),U(N,N)
real(kind=8) y(N)
call crout(A,L,U,N)
call downtri(L,b,y,N)
call uptri(U,y,xx,N)
end subroutine lu_crout
subroutine crout(A,L,U,N)
implicit none
integer N,i,k,r,j
real(kind=8) s
real(kind=8) A(n,n)
real(kind=8) L(N,N),U(N,N)
l=0.0_8
u=0.0_8
L(:,1)=a(:,1)
U(1,:)=a(1,:)/L(1,1)
do k=2,N
do i=k,n
s=0
do r=1,k-1
s=s+l(i,r)*u(r,k)
end do
l(i,k)=a(i,k)-s
end do
do j=k+1,n
s=0
do r=1,k-1
s=s+l(k,r)*u(r,j)
end do
u(k,j)=(a(k,j)-s)/l(k,k)
end do
u(k,k)=1
end do
return
end subroutine crout
subroutine uptri(A,b,xx,N)
implicit none
integer::i,j,N
real(kind=8) A(n,n),b(N),xx(N)
xx(N)=b(N)/A(N,N)
do i=n-1,1,-1
xx(i)=b(i)
do j=i+1,N
xx(i)=xx(i)-a(i,j)*xx(j)
end do
xx(i)=xx(i)/A(i,i)
end do
return
end subroutine uptri
subroutine downtri(A,b,xx,N)
implicit none
integer N,k,i
real(kind=8) A(n,n),b(N),xx(N)
xx(1)=b(1)/a(1,1)
do k=2,N
xx(k)=b(k)
do i=1,k-1
xx(k)=xx(k)-a(k,i)*xx(i)
end do
xx(k)=xx(k)/a(k,k)
end do
end subroutine downtri
end module mod_solver