write in array format in fortran

ⅰ亾dé卋堺 提交于 2019-11-28 11:01:06

问题


I try to write an output file.dat with an nxn matrix format .

I write the code but the output is a column of value f.

Now the problem is: how can i change the output-format of the file to write?

from: 1 2 4 5 ...

to: 1,2,3,4 // 5,6,8,.. //

program eccen
    implicit none
    integer, parameter:: grid=800
    integer::i,j,k,n,m
    real*8,allocatable::f(:,:)
    real*8::xx(grid),yy(grid),mval,Mxval
    real*8,allocatable::x(:),y(:)

    open(10,file='3d_disk.txt')
    n=0
        DO
                READ(10,*,END=100)
                n=n+1
        END DO

 100     continue
        rewind(10)

    allocate(x(n),y(n))

    do i=1, n
        read(10,*) x(i),y(i)
    end do


    mval=-20.
    Mxval=20.
    do i=1, grid
        xx(i) = mval + ((Mxval - mval)*(i-1))/(grid-1)
        yy(i) = mval + ((Mxval - mval)*(i-1))/(grid-1)
    end do

    open(20,file='3d_map.dat')

    allocate(f(n,n))
    f=0
    do i=1,grid
        do j=1,grid
            m=0.
            do k=1, n
                if (x(k) > xx(i) .and. x(k) < xx(i+1) .and. &
                 & y(k) > yy(j) .and. y(k) < yy(j+1)) then  
                    m=m+1 ! CONTA IL NUMERO DI PARTICELLE
                end if
            end do
            f(i,j)=float(m+1)

I thing that the modification must be here from this:

                write(20,*) f(i,j)
            end do
            write(20,*)
     print *,i
        end do    
end program eccen

to:

    do i=1,grid
      do j=1,grid
        write(20,*) f(i,j)
      end do
    end do

end do
            write(20,*)
     print *,i
        end do

 end program eccen

回答1:


This statement

write(20,*) f(i,j)

will write the value of f(i,j) then move to the next line, so the file you're getting is exactly what your code is specifying. If you want the file to contain n rows each with n values try

write(20,*) f(i,:)

which ought to make a good stab at writing the whole of row i of the array to a single line in the output file. Of course, this makes your loop over j redundant so you can remove it.



来源:https://stackoverflow.com/questions/16654249/write-in-array-format-in-fortran

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!