use portlib
implicit none
real*8, allocatable, dimension(:) :: A

integer :: i, j, N, M, freq
integer :: passo
character*5 :: texto
character :: temp

real*8 :: Lx, R0, Rf, r, t, A_total

real*8 :: r_estopim, dt, dx, pi

pi=dacos(-1.d0)

i= system("Notepad entrada.txt")

open(1,file='entrada.txt')
read(1,*) Lx
read(1,*) R0
read(1,*) Rf
read(1,*) r_estopim
read(1,*) M
read(1,*) N
read(1,*) freq
read(1,*) r

R0 = R0*0.5d0
Rf = Rf*0.5d0

! Passo de tempo
dt = Lx/r_estopim/N !s

! Passo de espaço
dx = Lx/N

close(1)

OPEN(2,FILE='./temp/grao.txt')
write(2,*) 0.d0, R0
write(2,*) Lx, R0
write(2,*) Lx, Rf
write(2,*) 0.d0, Rf
write(2,*) 0.d0, R0
close(2)


allocate(A(N))
A = 0
t = 0.d0
A_total = 0
passo = 0
open(1,file='./temp/saida.txt')
do i=1, M
    t = t + dt
    
    do j=1, N
        if(A(j) > 0.d0) then
            A(j) = A(j) + r*dt
        endif
        if(A(j) >= Rf) A(j) = 0
    enddo
    
    if(i<=N) A(i) = r0
    
    A_total= sum(A)*2*pi*dx
    
    write(1,*) t, A_total
    if(A_total == 0.d0) exit
    
    if(freq > 0) then
        if(mod(i,freq) == 0) then
        
        

        passo = passo + 1
        if(passo<10) then
            write(texto,'("0000",I1)') passo
        elseif(passo<100) then
            write(texto,'("000",I2)') passo
        elseif(passo<1000) then
            write(texto,'("00",I3)') passo
        elseif(passo<10000) then
            write(texto,'("0",I4)') passo
        elseif(passo<100000) then
            write(texto,'(I5)') passo
        endif
        
        OPEN(2,FILE='./temp/'// trim(adjustl(texto)) //'.txt')
        if(A(1)/= 0) write(2,*) 0.d0, A(1)
        DO J=1, n
            if(A(j) /= 0) then
                WRITE(2,*) dx*(j-1), a(J)
                WRITE(2,*) dx*j, a(J)
            endif
        ENDDO
        close(2)
    endif
    
    endif

enddo
close(1)

open(2,file='plot_Aqueima.gnu')

write(2,*) "set term png nocrop enhanced size 1280, 720"
write(2,*) "set out 'A_queima.png'"
write(2,*) "set style data lines"
write(2,*) "set grid"
write(2,*) "set xlabel 't (s)'"
write(2,*) "set ylabel 'A (m²)'"
write(2,*) "set time"
write(2,*) "plot './temp/saida.txt' using 1:2 title ''"

close(2)

i = system(".\gnuplot\gnuplot.exec plot_Aqueima.gnu")

if(freq>0) then
    open(2,file='plot_gif.gnu')

    write(2,*) "set terminal gif animate delay 10 size 1280, 720"
    write(2,*) "set out 'progresso.gif'"
    write(2,*) "set style data lines"
    write(2,*) "set grid"
    write(2,*) "set xlabel 'x (m)'"
    write(2,*) "set ylabel 'r (m)'"
    write(2,'(A,F13.4,A,F13.4,A)') "set xrange [", -0.001d0, ":", Lx+.001d0, "]"
    write(2,'(A,F13.4,A,F13.4,A)') "set yrange [", -0.001d0, ":", Rf+.001d0, "]"


    do i=1, passo
        if(i<10) then
            write(texto,'("0000",I1)') i
        elseif(i<100) then
            write(texto,'("000",I2)') i
        elseif(i<1000) then
            write(texto,'("00",I3)') i
        elseif(i<10000) then
            write(texto,'("0",I4)') i
        elseif(i<100000) then
            write(texto,'(I5)') i
        endif
        
        write(2,*) "plot './temp/"// trim(adjustl(texto)) //".txt' using 1:2 title 'Frente de chama' \"
        write(2,*) ", './temp/grao.txt' using 1:2 title 'Grão-propelente' lw 2 "

    enddo
    close(2)
    i = system(".\gnuplot\gnuplot.exec plot_gif.gnu")
endif

end