FORTRAN problem

need help with 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 result ....

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 program is not calculating values ​​for the first and last node ... Can you tell me why ???

+1


source to share


3 answers


For i=1

and, i=N

you type tt(i)

instead tt_new(i)

- the calculation is correct, but the results will not display correctly. Executing your code with a debugger is very useful in such cases.

I would also suggest restructuring your operator if

, but I would not go to gimel - I think the intent would be clearer than



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 - spatial constraint (how to handle endpoints differently) and time constraint (how to handle your initial condition).

+5


source


Try to format your inner in a IF ELSE

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

      



So you can see that only one is needed END IF

because leading sentences IF

(or ELSE IF

) are matched ELSE

.

( EDIT: The leading 2 lines of code in the question appear to be OK - thanks to Jonathan Leffler.)

+3


source


Tim Whitcomb and Gimel gave good answers. You should revisit your code again as there is no need to use multiple write statements (or blank lines or additional 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)

      

If this were my code, I would use line spacing more liberally.

+3


source







All Articles