ndee
ndee

Reputation: 25

How to make a file with i number of rows

I'm using this code below to generate a file with i number (i=200 for instance) of rows, but the first and the second rows are fixed and I just want to create another i rows using a random extraction in a sphere of unitary radius in Fortran. Each row should start with m1 and then 6 random numbers between a range [0,1].

    program InputGen  
    implicit none 

        integer :: i,n,j
        character(len=500) :: firstLine, secondLine
        real *8 :: m1  
        real:: r(10) 

        m1=1.d0  
        firstLine='3  5   1.d-4    1.d5   0.e-3    0.0   1. 0.1 0.e0 1'  
        secondLine='4.d6 0. 0. 0. 0. 0. 0. ' 

        call random_seed()  
        call random_number(r) 

        open(unit=100, file='INPUT.TXT',Action='write', Status='replace')
        write(100,'(A)') trim(firstLine)
        write(100,'(A)') trim(secondLine)

          do i=1,200
            write(100,'(A)') '',m1,' ',(r(n),n=1,10), 
                             ' ',(r(n),n=1,10),'0.0',&
                             ' ',(r(n),n=1,10),&
                             ' ',(r(n),n=1,10),'0.0'

          end do  

    write(*,*) 'Input file generated.'  
    write(*,*) 'Press Enter to exit...'  
    read(*,*)  

    end program InputGen  

The first and second lines create perfectly, but the other rows in the loop not.

Upvotes: 1

Views: 85

Answers (1)

You did not tell us what is wrong (how the problem exhibits), but I suspect the format is incorrect. You are just specifying (A), but you have a mixed output list with strings and numbers.

You can just use a simple general format like (*(g0)) that will apply the generic g0 format to all items in the input list. You will want to add some manual spaces (although you already have some) in the input list to avoid joining two unrelated output items on the line.

Or you can just follow the input list and add a specific format for each item, like (a,1x,f12.6,a,10(f12.6,1x),a ... and so on. Adjust as needed, especially the spaces (either keep them as ' ' with the a descriptor, or use the 1x descriptor.

You are also currently writing the same r all the time. You should generate more numbers and re-generate them an each loop iteration

     real:: r(40) 

     do i=1,200
        call random_number(r)

        write(100,'(*(g0))') '',m1,' ',r(1:10), 
                         ' ',r(11:20),'0.0',&
                         ' ',r(21:30),&
                         ' ',r(31:40),'0.0'

      end do  

This does not do anything with points in a sphere or anything similar, this just prints random numbers. I hope that is clear.

Upvotes: 1

Related Questions