Search code examples
animationfortrangnuplotanimated-gif

How to make animated gif in Gnuplot 5


Basically, I have solved the heat equation for (x,y,t) and I want to show the variation of the temperature function with time.The program was written in Fortran 90 and the solution data was stored in a file diffeqn3D_file.txt.

This is the program:

Program diffeqn3D
  Implicit none
  Integer:: b,c,d,l,i,j,k,x,y,t
  Real:: a,r,s,h,t1,k1,u,v,tt,p
  Real,Dimension(0:500,0:500,0:500):: f1 !f=f(x,t)
  !t1=time step and h=position step along x and
  !k=position step along y and a=conductivity
  open(7, file='diffeqn3D_file.txt', status='unknown')
  a=0.024
  t1=0.1
  h=0.1
  k1=0.1
  r=(h**2)/(k1**2)
  s=(h**2)/(a*t1)
  l=10
  tt=80.5
  b=100
  c=100
  d=100
  !The temperature is TT at x=0 and 0 at x=l.
  !The rod is heated along the line x=0.
  !Initial conditions to be changed as per problem..
  Do x=0,b
     Do y=0,c
        Do t=0,d
           If(x==0) Then
              f1(x,y,t)=tt
           Else If((x.ne.0).and.t==0) Then
              f1(x,y,t)=0
           End If
        End Do   
     End Do
  End Do
  print *,f1(9,7,5)
  print *,r
  print *,a,h,t1,h**2,a*t1,(h**2)/(a*t1)
  print *,f1(0,1,1)
  print *,f1(3,1,1)
  !num_soln_of_eqnwrite(7,*)
  Do t=1,d
     Do y=1,c-1
        Do x=1,b-1
           p=f1(x-1,y,t-1)+f1(x+1,y,t-1)+r*f1(x,y-1,t-1)+r*f1(x,y+1,t-1)-(2+2*r-s)*f1(x,y,t-1)
           f1(x,y,t)=p/s
           !f1(x,t)=0.5*(f1(x-1,t-1)+f1(x+1,t-1))
           !print *,f1(x,t),b
        End Do
     End Do
  End Do
  Do i=0,d
     Do k=0,b
        Do j=0,c
           u=k*h
           v=j*k1
           write(7,*) u,v,f1(k,j,i)
        End Do
     End Do
     write(7,*) "  "
     write(7,*) "  "
  End Do
  close(7)
End Program diffeqn3D

And after compilation and run, I enter the following code in gnuplot but it does not run, rather it hangs up or creates a gif picture, not animation.

set terminal gif animate delay 1
set output 'diffeqn3D.gif'
stats 'diffeqn3D_file.txt' nooutput
do for [i=1:int(STATS_blocks)] {
  splot 'diffeqn3D_file.txt'
   }

Sometimes it also puts up a warning message, citing no z-values for autoscale range.

What is wrong with my code and how should I proceed?


Solution

  • First, try to add some print commands for "debug" information:

    set terminal gif animate delay 1
    set output 'diffeqn3D.gif'
    stats 'diffeqn3D_file.txt' nooutput
    print int(STATS_blocks)
    do for [i=1:int(STATS_blocks)] {
      print i
      splot 'diffeqn3D_file.txt'
    }
    

    Second, what happens?

    The splot command does not have an index specifier, try to use:

    splot 'diffeqn3D_file.txt' index i
    

    Without the index i gnuplots always plots the whole file which has two consequences:

    1. The data file is quite large. Plotting takes quite a long time and it seems that gnuplot hangs.
    2. Gnuplot plots always the same data, there are no changes which show up in an animation.

    Now gnuplot runs much faster and we will fix the autoscale error. Again, there are two points:

    1. The index specifies a data set within the data file. The stats command counts those sets which "are separated by pairs of blank records" (from gnuplot documentation). Your data file ends with a pair of blank records - this starts a new data set in gnuplot. But this data set is empty which finally leads to the error. There are only STATS_blocks-1 data sets.

    2. The index is zero based. The loop should start with 0 and end at STATS_blocks-2.

    So we arrive at this plot command:

    do for [i=0:int(STATS_blocks)-2] {
      print i
      splot 'diffeqn3D_file.txt' index i
    }