首页/文章/ 详情

关于fish函数按指定步数执行的探讨

1月前浏览1410

本文摘要(由AI生成):

本文对fish函数按指定步数执行进行粗略探讨。首先介绍了在①群中提出的问题,然后对fish函数的执行方式进行了讨论。在讨论中,提到了whilestepping关键字,它可以使函数在每一步自动执行,而无需使用“@函数名”的方式执行函数。此外,还介绍了fish callback命令,它可以在指定步数执行函数。最后,还讨论了fish history命令的使用,它可以通过指定间隔来控制函数执行的步距,且无需指定whilestepping。

摘要:本文对fish函数按指定步数执行进行粗略探讨。


1.问题来源

在①群中,有小伙伴提出了如下问题:

图1-1 问题来源

以上问题得到了如下回复:

图1-2 问题来源

基于以上背景,我对fish函数的执行方式进行粗略讨论。

2.正文及代码

    model newmodel largestrain off[i = 0]fish def test    i = i + 1    io.out('The function has been executed ' + string(i) + ' times.')end@testmodel cycle 100

    上述代码是常规的fish执行方式。虽然运算了100步(cycles or steps),但fish函数仅执行一次,输出结果为"The function has been executed 1 times"。

      model newmodel largestrain off[i = 0]fish def test    whilestepping    i = i + 1    io.out('The function has been executed ' + string(i) + ' times.')endmodel cycle 100

      如果在函数第一行加上whilestepping关键字,则函数将在每一步(every cycle or step)自动执行,无需使用“@函数名”的方式执行函数,输出结果为"The function has been executed 100 times"。

      如果在end后添加了“@函数名”,函数将在model cycle前执行一次,输出结果为"The function has been executed 101 times."。

      此外,help文件中还指出"This statement causes the FISH function to be executed at the cycle point -1.0(see list cyclesequence)",见下图:

      图2-1 whilestepping说明

      这就是说whilestepping与"fish callback add @函数名 -1.0"等价,测试代码如下:

        model newmodel largestrain off[i = 0]fish def test    i = i + 1    io.out('The function has been executed ' + string(i) + ' times.')endfish callback add @test -1.0model cycle 100

        上述代码输出结果为"The function has been executed 100 times"。

        如果仅想在某一段计算中采用whilestepping,则需要采用"fish callback remove"命令,详细介绍请查阅说明书。测试代码如下:

          model newmodel largestrain off[i = 0]fish def test    i = i + 1    io.out('The function has been executed ' + string(i) + ' times.')endfish callback add @test -1.0model cycle 100fish callback remove @test -1.0model cycle 100

          执行上述代码后发现,函数仅在前100步执行,输出结果为"The function has been executed 100 times"。

          现在回到最初的问题“如何每100步调用一次fish函数”,图1-2的代码是正确的,但是问题在于add关键字后面的数字如何选择?

          根据help文件的解释,add后面应该跟一个cycle point,见下图:

          图2-2 cycle point

          如果对于以上cycle point不了解而随意指定的话,则会出现一些问题,比如:

            fish callback add @xxx80 interval 100

            按照我的理解就是在流体计算中每100步调用一次函数。但如果我们不进行流体计算的话,上述命令就会和我们的初衷大相径庭。因此,我选用-1.0(即whilestepping的cycle point),防止出现一些错误。测试代码如下:

              model newmodel largestrain off[i = 0]fish def test    i = i + 1    io.out('The function has been executed ' + string(i) + ' times.')endfish callback add @test 1.0 interval 100model cycle 1000

              上述代码输出结果为"The function has been executed 10 times",实现了每100步调用一次函数。

              按理说,本文到此就应该结束,但我在写文章的时候突然想到了fish history命令。我们在使用这个命令记录变量值时,是否需要添加whilestepping呢?fish history 使用形式可以参考这篇文章:

              fish history用法

              呱太Gekota,公 众号:FLAC3D小技巧FLAC3D 6.0——history常见问题解答

              测试代码如下:

                model newmodel largestrain off[i = 0]fish def test    i += 1    io.out('The function has been executed ' + string(i) + ' times.')endhistory interval 1fish history @testfish history @imodel cycle 100

                图2-3 监测结果

                上述代码输出结果为"The function has been executed 100 times"

                  model newmodel largestrain off[i = 0]fish def test    i += 1    io.out('The function has been executed ' + string(i) + ' times.')endhistory interval 100fish history @testfish history @imodel cycle 1000

                  上述代码输出结果为"The function has been executed 10times"

                  由此可知,还可以通过fish history来控制函数执行的步距,且无需指定whilestepping。

                  来源:FLAC3D小技巧
                  STEPSFLAC3D控制
                  著作权归作者所有,欢迎分享,未经许可,不得转载
                  首次发布时间:2024-03-19
                  最近编辑:1月前
                  FLAC3D小技巧
                  硕士 专注FLAC3D中的小技巧分享...
                  获赞 22粉丝 112文章 40课程 0
                  点赞
                  收藏
                  未登录
                  还没有评论

                  课程
                  培训
                  服务
                  行家

                  VIP会员 学习 福利任务 兑换礼品
                  下载APP
                  联系我们
                  帮助与反馈