Программа не входит в цикл if на Фортране, даже если условие выполнено - PullRequest
0 голосов
/ 21 мая 2018

Это часть кода, которая работает не так, как ожидалось.Мне пришлось включить некоторые «паузы» в старом стиле, чтобы точно определить местоположение ошибки.


    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), которые тоже не работали.

1 Ответ

0 голосов
/ 21 мая 2018

В вашем коде нет петли!IF это не петля!Он НЕ будет выполняться повторно, если вы не поместите туда реальный цикл.

Если вы хотите зациклить некоторый код, пока условие все еще действует, используйте цикл DO WHILE или цикл DO с EXIT.

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 не является циклом.

...