module dados

	use portlib

	integer :: N     ! Numero de equacoes
	integer :: itmax ! Numero maximo de iteracoes

	real*8  :: tol   ! tolerancia

	real*8,dimension(:),allocatable :: b  ! Termos independ.
	real*8,dimension(:),allocatable :: x  ! Solucao
	real*8,dimension(:),allocatable :: xo ! Solucao it. anterior
	
	real*8,dimension(:,:),allocatable :: A ! Matriz coef.

	real*8,dimension(:,:),allocatable :: L, U

	integer :: dos, opcao

contains

!----------------------------------------------------------

subroutine entradas

	dos = system('notepad dados_iniciais.txt')
	
	open(1,file = 'dados_iniciais.txt')

	read(1,*) N
	read(1,*) opcao

	close(1)

	allocate( b(N), x(N), A(N,N) )

end subroutine entradas

!----------------------------------------------------------

subroutine matriz

	integer :: i, j

	dos = system('notepad matriz.txt')
	
	open(1,file = 'matriz.txt')
	
	do i = 1, N

		read(1,*) ( A(i,j), j = 1, N)
	
	end do 

	close(1)

	dos = system('notepad constantes.txt')

	open(2,file = 'constantes.txt')

	do i = 1, N

		read(2,*) b(i)
	
	end do

	close(2)

end subroutine matriz

!----------------------------------------------------------

subroutine dados_it

	integer :: i

	dos = system('notepad entrada_it.txt')
	
	open(1,file = 'entrada_it.txt')
	read(1,*) itmax
	read(1,*) tol

	do i = 1, N
		read(1,*) xo(i)
	end do

	close(1)

end subroutine dados_it

!----------------------------------------------------------

subroutine saida

	integer :: i, j

	open(1,file = 'saida.txt')

	select case (opcao)
	case(1)
		
		write(1,*) "Método da Eliminacao de Gauss"

	case(2)

		write(1,*) "Método da Decomposicao LU"
		write(1,*) "Matriz Triangular Superior - U"
		do i = 1, N
			write(1,3) ( U(i,j),j = 1, N)
		end do
		write(1,*) "Matriz Triangular Inferior - L"
		do i = 1, N
			write(1,3) ( L(i,j),j = 1, N)
		end do 

	case(3)
		
		write(1,*) "Metodo de Jacobi"

	case(4)

		write(1,*) "Metodo de Gauss-Seidel"

	end select

	do i = 1, N
		write(1,2) i, x(i)
	end do

	2 format ("x(", i2, ") = ", 1pe25.15)
	3 format ( <N>(1pe25.15) )

	close(1)

	dos = system('notepad saida.txt')

end subroutine saida

!----------------------------------------------------------

end module dados