Reputation: 433
I was trying to code this problem in fortran. The output file contains the result in two columns. I am having hard time to modify my code (below) to get a gnuplot-ready output file containing nt columns and nx lines. Can anybody help me? Thanks!
PROGRAM odlc
IMPLICIT NONE
INTEGER::i,it,nx,nt,k,ierr
DOUBLE PRECISION::dx,dt,c
DOUBLE PRECISION,DIMENSION(2000)::u,un
ierr=0
nx=20
nt=50
dt=0.01
c=1.0
dx=2./(nx-1.)
!initial condition
DO i = 1,nx
IF(i*dx>=0.5 .and. i*dx<=1) THEN
u(i) = 2
ELSE
u(i)=1
ENDIF
ENDDO
!Finite Difference
OPEN(UNIT=200,FILE='tab2.txt',STATUS='REPLACE',ACTION='WRITE',IOSTAT=ierr)
DO it=1,nt
DO k=1,nx
un(k)=u(k)
ENDDO
DO i=2,nx-1
u(i)=un(i)-c*dt/dx*(un(i)-un(i-1))
ENDDO
DO i=1,nx
WRITE(200,'(I7,F10.2)')i,u(i)
ENDDO
ENDDO
CLOSE(UNIT=200)
END
Upvotes: 1
Views: 2034
Reputation: 32873
I'm not 100% sure that it is exactly what you want but here you go:
Declare
CHARACTER(len=32)::fmt
and initialize it like this
WRITE(fmt,*)'(',nx,'F10.2)'
This just creates a format string which writes a single line of nx
reals.
Then replace
DO i=1,nx
WRITE(200,'(I7,F10.2)')i,u(i)
ENDDO
by
WRITE(200,fmt) (/ (u(i), i=1,nx) /)
Now in your tab2.txt
file, you will get nt
lines and nx
columns.
After your update, here is the (slightly shortened) code that will do what you want:
PROGRAM odlc
IMPLICIT NONE
INTEGER::i,it
INTEGER,PARAMETER::nx=20,nt=50
DOUBLE PRECISION,PARAMETER::dx=2./(nx-1.),dt=0.01,c=1.0
DOUBLE PRECISION,DIMENSION(nt,nx)::u
CHARACTER(len=32)::fmt
WRITE(fmt,*)'(I3,',nt,'F5.2)'
!initial condition
DO i = 1,nx
IF(i*dx>=0.5 .and. i*dx<=1) THEN
u(1,i) = 2
ELSE
u(1,i)=1
ENDIF
ENDDO
!Finite Difference
DO it=2,nt
DO i=2,nx-1
u(it,i)=u(it-1,i)-c*dt/dx*(u(it-1,i)-u(it-1,i-1))
ENDDO
ENDDO
!Write to file
OPEN(UNIT=200,FILE='tab2.txt',STATUS='REPLACE',ACTION='WRITE')
DO i=1,nx
WRITE(200,fmt)REAL(i*(2./(nx-1.))),u(:,i)
ENDDO
CLOSE(UNIT=200)
END PROGRAM odlc
Upvotes: 2