module solvers

contains

! ***************************************************

subroutine GaussSeidel(N,iti, AW,AP,AE,BP,un)
	
	implicit none

		integer,intent(in)                 :: N, iti
		real*8,dimension(N),intent(in)     :: AW,AP,AE,BP
		real*8,dimension(N),intent(inout)  :: un
		integer::i, k

do k = 1, iti

	do i=2,N-1

		un(i)=( BP(i) + AW(i)*un(i-1)  +  AE(i)*un(i+1) ) / AP(i)
		
	end do

end do 

end subroutine GaussSeidel

! ***************************************************

subroutine TDMA(N,AW,AP,AE,BP,un)

    implicit none

    integer:: k                                        ! número do nó
    real*8 :: div                                      ! variável auxiliar
    integer,intent(in)  :: N                           ! número de nós ou volumes
    real*8,allocatable, dimension(:) :: P              ! coeficiente do tdma
    real*8,allocatable, dimension(:) :: Q              ! coeficiente do tdma
    real*8,intent(in), dimension(N) :: AP              ! coeficiente aP
    real*8,intent(in), dimension(N) :: AW              ! coeficiente aW
    real*8,intent(in), dimension(N) :: AE              ! coeficiente aE
    real*8,intent(in), dimension(N) :: BP              ! termo fonte bP

    real*8,intent(out),dimension(N) :: un  ! variável de interesse

    allocate(P(N), Q(N))

    P(1) = AE(1) / AP(1)
    Q(1) = BP(1) / AP(1)
    
  do k=2,N
      div = AP(k) - AW(k)*P(k-1)
      P(k) = AE(k) / div
      Q(k) = (BP(k) + AW(k)*Q(k-1))/ div
      
  end do
    
  un(N) = Q(N)

  do k = N-1,1,-1

      un(k) = P(k)*un(k+1) + Q(k)

  end do

  deallocate(P, Q)


end subroutine TDMA

! ***************************************************

end module solvers
