0

I have the following problem. I have a file that I must:

  1. divide in cells to create an NxN grid;
  2. counts the particles in each cell;
  3. write the resulting numbers in a particular way.

The problem is the third point: I must write an NxN array each numbers represent the total numbers of particles in each cell, but I don't know how to write the file in an NxN array.

program eccen
    implicit none
    integer, parameter:: grid=200
    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='coordXY.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

! create a grid
    mval=-15.
    Mxval=15.
    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='fluxXY.dat')

! counts the paticles in each cell of the grid


    allocate(f(grid,grid))
    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)    
! THIS IS HOW I WRITE THE FILE BUT THIS SHOULD BE CHANGED
            write(20,*) f(i,:)

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

Thanks a lot for Your help!

5
  • Even after studying your sample program I must admit to still not understand what is being asked here. Are you asking how to represent a multidimensional array (a matrix, so to say) and how to write it out? Commented Jan 16, 2014 at 21:50
  • @ Idigas: Yes, a matrix of gridgrid (grid is the numbers of cells in x and y axes). But in my program the file in unit 20 has only 3 columns and a lot of lines. I want, however, an array with gridgrid (columns*lines) Commented Jan 16, 2014 at 23:13
  • Without knowing how those three columns look it's hard to say, but try writing with an explicit format. Commented Jan 17, 2014 at 8:38
  • Do you want the data to be formatted or unformatted? Are you sure there are only 3 columns? Switch off the line wrap on your editor - is it still 3 columns? Commented Jan 17, 2014 at 10:01
  • to @cup , yes because i put the output.dat in a program that read only NxN files and give me errors that demonstrate me the file is written as an Mx3 rray, where M is a very high number higher of N or (or using my f90 code syntax) higher of grid. Commented Jan 17, 2014 at 16:56

2 Answers 2

2

The Intel Fortran Compiler (ifort) performs an automatic wrapping which fits 3 double precision numbers, while gfortran does not.

You should create an explicit format (mentioned by @francescalus):

1000  FORMAT(<grid>F16.8)

In this format, the variable grid can be used directly. Now you can specify your WRITE statement as

write(20,1000) f(i,:)
Sign up to request clarification or add additional context in comments.

1 Comment

ifort has the -no-wrap-margin compile flag (different spelling for Windows). <n> isn't portable, but there are ways around that (including *(F...) if one can call that portable).
0

[SOLVED]

I finally solve all my problems:

  1. using ifort with -no-wrap-margin -> work !

  2. using ifort with @Stefan tips -> work!

  3. use gfortran alone -> work !

The difference is in the file size: using the 1) solution the file is a bit larger than with the 2) solution. The 3) case, again, shows difference in file size respect the other cases (higher than the other two). Respectively:

  1. 60kb;
  2. 40kb;
  3. 65kb.

For a test file with grid=50 and an input file of 4001 lines and 2 rows (132kb).

Thanks You all!!!!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.