简体   繁体   English

在 Fortran 循环中重复一个步骤

[英]Repeating a step in a Fortran loop

I'm trying to write a Fortran 90 program to carry out Euler's method to solve ode's using an adaptive time step.我正在尝试编写一个 Fortran 90 程序来执行 Euler 的方法来使用自适应时间步来解决 ode 的问题。

I have an if statement inside of a do while loop, in which I check that the error at each iteration of the code is less than a certain tolerance.我在do while循环中有一个if语句,我在其中检查代码每次迭代时的错误是否小于某个容差。 However, if it is not less than certain tolerance, I must change a certain value (the step size) and carry out the calculation again to get a new error to compare with the tolerance.但是,如果它不小于某个容差,则必须更改某个值(步长)并再次进行计算以获得新的误差以与容差进行比较。

It looks something like (and forgive me this is my first time using this website):它看起来像(原谅我这是我第一次使用这个网站):

do while (some condition)
  (Get an approximation to the ODE with various subroutine calls)
  (Calculate the error)
if (error < tol)
  step = step/2
else
   step = 2*step
(Something that will return to the top of my do while loop)
end if
end do

Say for example, I had do while (i < 4) , where i starts at 1, and my error was not less than my tolerance, I would have to repeat the calculation again for i=1 with a new step size.举个例子,我do while (i < 4)i从 1 开始,我的错误不小于我的容忍度,我必须用新的步长对i=1再次重复计算。

I hope this makes sense to those of you who read this.我希望这对阅读本文的人有意义。 If you need any clarification, let me know.如果您需要任何说明,请告诉我。

Because there is no explicit counter in the do while loop unlike in a normal do i=1,... loop.因为do while循环中没有明确的计数器,这与正常的do i=1,...循环不同。 you can just use cycle to start a new iteration.您可以使用cycle来开始新的迭代。 It will be the same as repeating the current iteration.这将与重复当前迭代相同。 But the condition will be evaluated again.但条件将再次评估。 If it shouldn't be evaluated, you would have to use go to or restructure your code.如果它不应该被评估,你将不得不使用go to或重构你的代码。

Or another loop nested inside the main loop might be better, but that probably counts as the restructuring mentioned above.或者嵌套在主循环中的另一个循环可能会更好,但这可能算作上面提到的重组。 Depends what is the condition, how you change step and i and how the tolerance depends on the step and i .取决于条件是什么,您如何更改stepi以及公差如何取决于stepi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM