簡體   English   中英

FORTRAN問題

[英]FORTRAN problem

在Fortran中需要幫助...

這是程序的主循環。

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

這是輸出...

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   <--

如您所見...程序無法計算第一個和最后一個節點的值...您能告訴我為什么嗎???

對於i=1i=N ,您正在打印tt(i)而不是tt_new(i) -計算已正確執行,但結果將無法正確顯示。 在這種情況下,使用調試器單步執行代碼非常有幫助。

我還建議重組您的if語句,但我不會深入探討gimel-我認為其意圖會更加清楚

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

因為您有兩種特殊情況-空間約束(如何處理端點)和時間約束(如何處理初始條件)。

嘗試以更易讀的方式格式化內部IF ELSE

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

這樣,您會看到只需要一個END IF ,因為前導IF (或ELSE IF )子句被匹配的ELSE關閉。

編輯:問題中的前兩行代碼顯示為OK-感謝Jonathan Leffler。)

蒂姆·惠特科姆和吉梅爾給出了很好的答案。 但是,由於不需要使用多個write語句(或空白行或多余的END IF語句),因此您應該進一步修改代碼。

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)

如果是我的代碼,我也將更自由地使用行間距。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM