【问题标题】:Program not entering an if-loop in Fortran even when condition is satisfied即使满足条件,程序也不会在 Fortran 中进入 if 循环
【发布时间】:2018-05-21 15:19:23
【问题描述】:

这是未按预期运行的代码部分。我必须包含一些老式的“暂停”语句来确定错误位置。


    iteration_index = 1
    y_refined = y_current + (0.5d0*dx*(dydx_predictor + dydx_refined_corrector))               ! Solution corresponds to i = 2, i.e., the refined Heun's method.

    relative_percent_error = 100.d0*(abs((y_refined - y_next)/y_refined))                         ! Calculation of relative percentage error. This is NOT true error.

    if (relative_percent_error > heun_percent_tolerance) then

        iteration_index = iteration_index + 1

        print*, 'first loop enter', x_next, relative_percent_error, iteration_index
        pause

        if (iteration_index < max_heun_number) then

            y_next = y_refined
            call dydx(x_next, y_next, dydx_refined_corrector)
            y_refined = y_current + (0.5d0*dx*(dydx_predictor + dydx_refined_corrector))
            relative_percent_error = 100.d0*(abs((y_refined - y_next)/y_refined))

            print*, 'second loop enter', x_next, relative_percent_error, iteration_index
            pause

        end if

    end if

输出如下:

first loop enter   1.0000000000000000        6.7763423346068707          2

PAUSE

To resume execution, type go.  Other input will terminate the job.

go

RESUMED

second loop enter   1.0000000000000000        1.6658644147581094        2

PAUSE 

To resume execution, type go.  Other input will terminate the job.

go

RESUMED

first loop enter   2.0000000000000000        6.6615482639252761         2

PAUSE 

To resume execution, type go.  Other input will terminate the job.

heun_percent_tolerance 的值是 0.01,max_heun_number 是 15。我希望执行进入第二个 if 循环进行更多迭代,直到达到最大限制 15,但似乎代码跳转到下一个 x_next 值为 2。

我什至尝试将这两个条件组合为If (cond1 .and. cond2),但这也不起作用。

【问题讨论】:

    标签: if-statement fortran gfortran fortran95 numerical-computing


    【解决方案1】:

    您的代码中没有循环! IF 不是循环!除非你放一个实际的循环,否则它不会重复执行。

    如果您想在条件仍然有效时循环某些代码,请使用 DO WHILE 循环或带有 EXITDO 循环。

    do while (relative_percent_error > heun_percent_tolerance) 
    
        iteration_index = iteration_index + 1
    
        print*, 'first loop enter', x_next, relative_percent_error, iteration_index
        pause
    
        if (iteration_index < max_heun_number) then
    
            y_next = y_refined
            call dydx(x_next, y_next, dydx_refined_corrector)
            y_refined = y_current + (0.5d0*dx*(dydx_predictor + dydx_refined_corrector))
            relative_percent_error = 100.d0*(abs((y_refined - y_next)/y_refined))
    
            print*, 'second loop enter', x_next, relative_percent_error, iteration_index
            pause
    
        end if
    
    end do
    

    请注意,代码仍然可能不正确,您将不得不对其进行重构,但关键是您需要一个实际的 DO 循环,而不仅仅是一个 IF 条件。 IF 不是循环。

    【讨论】:

    • 谢谢@Vladimir F。它有效,您的输入是正确的。
    • 仅供参考,我使用了一个“do while”语句并使用“.and”强制执行这两个数学条件。逻辑条件。我得到了我想要的。
    猜你喜欢
    • 2021-11-18
    • 1970-01-01
    • 2012-10-01
    • 2016-03-14
    • 1970-01-01
    • 2019-08-09
    • 2013-04-08
    • 2021-08-07
    • 2020-04-15
    相关资源
    最近更新 更多