tags:

views:

202

answers:

3

Hi..need help in Fortran...

This is the main loop of the program..

do iStep=0,nStep
    write(7,*)iStep

    !* Compute new temperature using FTCS scheme.
    do i=1,N
        if( istep==0) then  !only for t=0
            tt_new(i)=250
 write(7,*)tt_new(i)
        else
            if(i==1) then
                tt_new(i)=2*coeff*(tt(i+1)+35.494)-0.036*tt(i)
                write(7,*)tt(i)
            else 
                if(i==N) then
                    tt_new(i)=2*coeff*(tt(i-1)+35.494)-0.036*tt(i)
                    write(7,*)tt(i)
                else           
                    tt_new(i) = coeff*(tt(i+1) + tt(i-1)+33.333)+(1 - 2*coeff)*tt(i)
                    write (7,*) tt_new(i)
                end if
            end if
        end if     
    end do

    do i=1,N
        tt(i) = tt_new(i)     ! Reset temperature to new values
    enddo
end do

this is the output....

0
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
1
2.5000000E+02     <--
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.5000000E+02   <--

As you can see...the programm doesn't calculate the values for the first and last node...Can you tell me why???

+2  A: 

Try formatting your inner IF ELSE in a more readable way:

if( istep==0) then  !only for t=0
    tt_new(i)=250
    write(7,*)tt_new(i)
else if(i==1) then
    tt_new(i)=2*coeff*(tt(i+1)+35.494)-0.036*tt(i)
    write(7,*)tt(i)
else if(i==N) then
    tt_new(i)=2*coeff*(tt(i-1)+35.494)-0.036*tt(i)
    write(7,*)tt(i)
else           
    tt_new(i) = coeff*(tt(i+1) + tt(i-1)+33.333)+(1 - 2*coeff)*tt(i)
    write (7,*) tt_new(i)

end if
end if //redundant
end if //redundant

This way, you see that only one END IF is necessary, because leading IF (or ELSE IF) clauses are closed by the matching ELSE.

(EDIT: the leading 2 lines of code in the question are displayed OK - thanks to Jonathan Leffler.)

gimel
+2  A: 

For i=1 and i=N, you are printing tt(i) instead of tt_new(i) - the calculation is being performed correctly, but the results will not be displayed properly. Stepping through the code with a debugger is quite helpful in cases like this.

I would also suggest restructuring your if statement, but I wouldn't go quite as far as gimel - I think the intent would be clearer as

if (iStep == 0) then
    ! Perform actions for time 0
else
    ! Perform actions for time > 0
    if (i == 1) then
        ! Perform actions for first endpoint
    else if (i == N) then
        ! Perform actions for last endpoint
    else
        ! Perform actions for midsection
    end if
end if

since you have two types of special cases - a spatial constraint (how to handle the endpoints differently) and a temporal constraint (how to handle your initial condition).

Tim Whitcomb
+2  A: 

Tim Whitcomb and gimel have given good answers. You should revise your code still more, though - since there is no need to use multiple write statements (or the blank lines, or the extra END IF statements).

if (istep==0) then  !only for t=0
    tt_new(i)=250
else if (i==1) then
    tt_new(i) = 2*coeff*(tt(i+1)+35.494)-0.036*tt(i)
else if (i==N) then
    tt_new(i) = 2*coeff*(tt(i-1)+35.494)-0.036*tt(i)
else           
    tt_new(i) = coeff*(tt(i+1) + tt(i-1)+33.333)+(1 - 2*coeff)*tt(i)
end if
write(7,*)tt_new(i)

Were it my code, I'd make more liberal use of spacing in the lines, too.

Jonathan Leffler