我的浅水实施中出现奇怪的振荡波纹

2023-12-29

我一直在尝试在 Unity 中实现浅水方程,但遇到了一个奇怪的错误。我的水中出现了奇怪的振荡波纹。我做了一些截图:

您可以在这里找到视频:https://www.youtube.com/watch?v=crXLrvETdjA https://www.youtube.com/watch?v=crXLrvETdjA

我的代码基于论文GPU 上的快速水力侵蚀模拟和可视化 http://evasion.imag.fr/Publications/2007/MDH07/FastErosion_PG07.pdf星梅.您可以在这里找到完整的求解器代码:http://pastebin.com/JktpizHW http://pastebin.com/JktpizHW(或见下文。)每次我使用论文中的公式时,我都会添加其编号作为注释。

我尝试了不同的时间步长,对于视频我使用了 0.02,降低它只会让它振荡更慢。我还尝试了更大的网格(视频使用100,我尝试了200,但波纹变小了。)我检查了所有公式几次,找不到任何错误。

这里有人能弄清楚出了什么问题吗?

额外信息:

正如你从pastebin 中看到的,我用c# 对其进行了编程。我使用 Unity 作为可视化引擎,我只是使用网格来可视化水。我更改网格的顶点 y 分量以匹配我计算的高度。

DoUpdate 方法得到一个float[][] lowerLayersHeight参数,基本上就是水下地形的高度。视频里只有这一切0.

public override void DoUpdate(float dt, float dx, float[][] lowerLayersHeight) {
        int x, y;
        float totalHeight, dhL, dhR, dhT, dhB;
        float dt_A_g_l = dt * _A * g / dx; //all constants for equation 2
        float K; // scaling factor for the outflow flux
        float dV;

        for (x=1 ; x <= N ; x++ ) {
                for (y=1 ; y <= N ; y++ ) {
                        //
                        // 3.2.1 Outflow Flux Computation
                        // --------------------------------------------------------------
                        totalHeight = lowerLayersHeight[x][y] + _height[x][y];
                        dhL = totalHeight - lowerLayersHeight[x-1][y] - _height[x-1][y]; //(3)
                        dhR = totalHeight - lowerLayersHeight[x+1][y] - _height[x+1][y];
                        dhT = totalHeight - lowerLayersHeight[x][y+1] - _height[x][y+1];
                        dhB = totalHeight - lowerLayersHeight[x][y-1] - _height[x][y-1];

                        _tempFlux[x][y].left =   Mathf.Max(0.0f, _flux[x][y].left        + dt_A_g_l * dhL ); //(2)
                        _tempFlux[x][y].right =  Mathf.Max(0.0f, _flux[x][y].right       + dt_A_g_l * dhR );
                        _tempFlux[x][y].top =    Mathf.Max(0.0f, _flux[x][y].top         + dt_A_g_l * dhT );
                        _tempFlux[x][y].bottom = Mathf.Max(0.0f, _flux[x][y].bottom      + dt_A_g_l * dhB );

                        float totalFlux = _tempFlux[x][y].left + _tempFlux[x][y].right + _tempFlux[x][y].top + _tempFlux[x][y].bottom;
                        if (totalFlux > 0) {
                                K = Mathf.Min(1.0f, _height[x][y] * dx * dx / totalFlux / dt);  //(4)

                                _tempFlux[x][y].left =   K * _tempFlux[x][y].left;  //(5)
                                _tempFlux[x][y].right =  K * _tempFlux[x][y].right;
                                _tempFlux[x][y].top =    K * _tempFlux[x][y].top;
                                _tempFlux[x][y].bottom = K * _tempFlux[x][y].bottom;
                        }
                        //swap temp and the real one after the for-loops

                        //
                        // 3.2.2 Water Surface
                        // ----------------------------------------------------------------------------------------
                        dV = dt * (
                                //sum in
                                _tempFlux[x-1][y].right + _tempFlux[x][y-1].top + _tempFlux[x+1][y].left + _tempFlux[x][y+1].bottom
                                //minus sum out
                                - _tempFlux[x][y].right - _tempFlux[x][y].top - _tempFlux[x][y].left - _tempFlux[x][y].bottom
                                ); //(6)
                        _tempHeight[x][y] = _height[x][y] + dV / (dx*dx); //(7)
                        //swap temp and the real one after the for-loops
                }
        }
        Helpers.Swap(ref _tempFlux, ref _flux);
        Helpers.Swap(ref _tempHeight, ref _height);
}

我自己修好了!虽然是在开车去见朋友的时候想到的。问题很简单,我在有缺陷的代码中所做的是为每个单元格(或网格点)计算通量,然后计算高度,然后转到下一个单元格。我应该做的是首先计算所有单元的通量,然后对所有单元进行第二次迭代并计算它们的高度。所以代码就变成了:

for (x=1 ; x <= N ; x++ ) {
    for (y=1 ; y <= N ; y++ ) {
        //
        // 3.2.1 Outflow Flux Computation
        // --------------------------------------------------------------
        ***
    }
}

for (x=1 ; x <= N ; x++ ) {
    for (y=1 ; y <= N ; y++ ) {
        //
        // 3.2.2 Water Surface
        // ---------------------------------------------------------------------------
        ***
    }
}
Helpers.Swap(ref _tempFlux, ref _flux);
Helpers.Swap(ref _tempHeight, ref _height);

(当然***变成上面问题中的相应代码。)

现在我有了一个有效的水模拟。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我的浅水实施中出现奇怪的振荡波纹 的相关文章

随机推荐