0

First of all I must say I'm new to Fortran and I'm very excited about it's power for my area which is computational fluid dynamics (CFD).

But I have just found a bug which I can't seem to find the answer to. I want to make a simple thing: print an array (ok, I did it fine), and before that array I want to print either a new line (using print '(/)') or just print the array's name, like "P = ". The reason for this is because my array is calculated in many iterations and I want to see the progress of each iteration, so I want them to be separated by something.

But when I insert the code to print either one of these lines inside my function, my array which was previously being shown as numbers during all iterations, starts showing as NaN after some iterations. Does anyone know what can I do to be able to fix this? And can someone explain to me why this happens in Fortran? Like, why does a simple print statement changes the logic?

I would normally insert my "print '(/)' " anywhere inside the q = 0,nit loop, but outside the i,j loops and that is what makes it ??change the logic? of the program?

function pressure_poisson(p,dx,dy,nit,nx,ny)
    real, dimension(10,10) :: pressure_poisson,p,pn
    real :: dx, dy 
    integer :: i,j, q, nit, nx, ny

do q = 0,nit
    pn = p

    do i = 1,nx
        do j = 1,ny
            p(i,j) = ((( pn (i+1,j) + pn (i-1,j) )* dy ** 2 + &
            ( pn(i,j+1) + pn (i, j-1) ) * dx **2 ) / (2 * (dx**2 + dy ** 2)) - &
            dx **2 * dy **2 / (2 * (dx **2 + dy **2)) * b(i,j))
        end do
    end do

    do i = 1,nx
        do j = 1, ny
            write (*,'(3x, es10.3)', advance='no'), p(i,j)
        end do
        print '(/)'
    end do

end do
  • 1
    You should investigate your compiler's options for _bounds checking_ on array access. Questions here will cover this topic. – francescalus Dec 23 '17 at 01:17
  • 1
    besides the obvious array bounds violations, you have your loops nested backwards, which could be a serious performance bug if the compiler doesn't swap them. – tim18 Dec 23 '17 at 05:19
  • Welcome, please take the [tour]. Learn to compile your code for debugging. For example `gfortran -g -Wall -fcheck=all -fbacktrace`. If you have another compiler consult the manual. If your errors persist after fixing the obvious, make a [mcve] and show us your compiler command and the complete output of the comiler and of your code. Always tell us your compiler version. See also [ask]. – Vladimir F Героям слава Dec 23 '17 at 06:07
  • Also always search for similar questions first. These are results of a very quick search: https://stackoverflow.com/questions/1331608/fortran-runtime-error-fixed-by-writing-output https://stackoverflow.com/questions/24616204/writing-to-screen-solves-segmentation-error https://stackoverflow.com/questions/22025860/writing-to-stdout-throws-segmentation-fault-in-fortran – Vladimir F Героям слава Dec 23 '17 at 06:11

0 Answers0