module fonctions
  
  ! module principal contenant les sous-routines de ce tp  
  implicit none
  
  
contains
  

  ! Initialisation u
  subroutine init(u,x,nx)
    implicit none
   
    real*8, intent(inout), dimension (:) :: u,x
    integer , intent(in) :: nx
    integer  :: i
    
    do i=1, nx
       u(i) = 0.
       if (x(i).le.0.5) u(i)=1.
    enddo
    
  end subroutine  Init
  

  ! Euler explicite
  subroutine schema_num(u, u_next,dt,dx,nx,c)
    implicit none
    real*8, intent(in), dimension (:) :: u
    real*8, intent(inout), dimension (:) :: u_next
    real*8 , intent(in) :: dt,dx,c
    integer , intent(in) :: nx
    integer  :: i
     real*8 :: a, b

   
    do i=2, nx-1
       u_next(i) = u(i) - c*dt*(u(i+1)-u(i-1))/(2.*dx)
    enddo
    u_next(1) = u(1) - c*dt*(u(2)-u(nx))/(2.*dx)
    u_next(nx) = u(nx) - c*dt*(u(1)-u(nx-1))/(2.*dx)

    
  end subroutine  schema_num
  
  
  
end module fonctions




!  Programme principal

program Transport_DF
  
  use fonctions
  
  implicit none
 
  real*8,ALLOCATABLE:: u(:), u_next(:),x(:)
  real*8 :: dt, tmax, dx, xmax, xmin,cfl,c
  integer :: nb_iter,i, nfreq,k, nx,nbre
  character(len=50) :: mystr
  
  nx = 10

  write(*,*) 'Entrez le temps final'
  read(*,*) tmax
  write(*,*) 'Entrez vitesse propagation'
  read(*,*) c
  write(*,*) 'Entrez cfl'
  read(*,*) cfl
  write(*,*) 'Entrez nx'
  read(*,*) nx
  write(*,*) 'Entrez nfreq'
  read(*,*) nfreq
  
  xmin = 0.
  xmax = 1.

  dx = (xmax-xmin)/nx
  dt= cfl*dx
  nb_iter = ceiling(tmax/dt)
 
 
  ! Initialisation
   allocate(u(1:nx))
   allocate(u_next(1:nx))
   allocate(x(1:nx))

   do i=1,nx
      x(i) = i*dx
   enddo

   call init(u,x,nx)

 
  ! boucle temporelle principale
  do k = 1, nb_iter
     
     ! appel de la routine d'integration
     call schema_num(u, u_next,dt,dx,nx,c)
     u = u_next
     
     ! ecriture du resultat toutes les nfreq iterations
     
     if (mod(k,nfreq).eq.0) then
        
        nbre = k/nfreq
        write(mystr,'(i4.4)')nbre 
        open(222,file='u'//trim(mystr),position='append')
        
        !Ecriture dans le fichier 
        do i=1,nx
           write(222,*) x(i), u(i)
        enddo
        close(222)  

     endif
     
  end do
 
  deallocate(u,u_next,x)
  
end program

