首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(计算流体力学)数组问题。为什么我的C程序输出-1.#IND00

(计算流体力学)数组问题。为什么我的C程序输出-1.#IND00
EN

Stack Overflow用户
提问于 2021-02-09 14:39:11
回答 2查看 56关注 0票数 0

我用C编写的程序有一个问题,它解决了一维线性对流方程。基本上,我已经初始化了两个数组。第一个数组(u0_array)是一个数组,数组元素在间隔上设置为两个或0.5

我遇到的问题是代码末尾的嵌套for循环。此循环将计算下一个点所需的更新方程应用于数组中的每个元素。当我运行我的脚本并试图打印结果时,我得到的输出只是-1.IND 00,用于循环的每一次迭代。(我是跟随Matlab风格的伪代码,我也附在下面),我是非常新的C,所以我的经验显示。我不知道为什么会这样。如果有人能对此提出可能的解决办法,我将非常感激。到目前为止,我已经将我的代码附在下面,并附上了一些评论,这样您就可以遵循我的思维过程了。我还附加了Matlab风格的伪代码,我遵循。

代码语言:javascript
复制
# include <math.h>
# include <stdlib.h>
# include <stdio.h>
# include <time.h>


int main ( )
{
    //eqyation to be solved 1D linear convection -- du/dt + c(du/dx) = 0
    //initial conditions u(x,0) = u0(x)

    //after discretisation using forward difference time and backward differemce space
    //update equation becomes u[i] = u0[i] - c * dt/dx * (u0[i] - u[i - 1]);


    int nx = 41; //num of grid points

    int dx = 2 / (nx - 1); //magnitude of the spacing between grid points

    int nt = 25;//nt is the number of timesteps

    double dt = 0.25; //the amount of time each timestep covers

    int c = 1;  //assume wavespeed


    //set up our initial conditions. The initial velocity u_0
    //is 2 across the interval of 0.5 <x < 1 and u_0 = 1 everywhere else.
    //we will define an array of ones
    double* u0_array = (double*)calloc(nx, sizeof(double));

    for (int i = 0; i < nx; i++)
    {
        u0_array[i] = 1;
    }

    // set u = 2 between 0.5 and 1 as per initial conditions
    //note 0.5/dx = 10, 1/dx+1 = 21
    for (int i = 10; i < 21; i++)
    {
        u0_array[i] = 2;
        //printf("%f, ", u0_array[i]);
    }

    //make a temporary array that allows us to store the solution
    double* usol_array = (double*)calloc(nx, sizeof(double));


    //apply numerical scheme forward difference in
    //time an backward difference in space
    for (int i = 0; i < nt; i++)
    {
        //first loop iterates through each time step
        usol_array[i] = u0_array[i];
        //printf("%f", usol_array[i]);
        

        //MY CODE WORKS FINE AS I WANT UP TO THIS LOOP
        //second array iterates through each grid point for that time step and applies
        //the update equation
        for (int j = 1; j < nx - 1; j++)
        {
            u0_array[j] = usol_array[j] - c * dt/dx * (usol_array[j] - usol_array[j - 1]);
            printf("%f, ", u0_array[j]);
        }
    }

    return EXIT_SUCCESS;
}

作为参考,我下面的伪代码附在一维线性对流伪码(Matlab风格)下面。

EN

回答 2

Stack Overflow用户

发布于 2021-02-09 14:49:58

而不是整数除法,而是使用FP数学。

这避免了后面的除以0和-1.#IND00。

代码语言:javascript
复制
// int dx = 2 / (nx - 1);   quotient is 0.
double dx = 2.0 / (nx - 1);

OP的代码与注释不匹配

代码语言:javascript
复制
// u[i] = u0[i] - c * dt/dx * (u0[i] - u[i - 1]
u0_array[j] = usol_array[j] - c * dt/dx * (usol_array[j] - usol_array[j - 1]);

如果删除多余的_array,则更容易查看。

代码语言:javascript
复制
//                                     v correct?
// u[i] = u0[i] - c * dt/dx * (u0[i] - u[i - 1]
u0[j] = usol[j] - c * dt/dx * (usol[j] - usol[j - 1]);
//                                       ^^^^ Correct?
票数 2
EN

Stack Overflow用户

发布于 2021-02-09 15:18:22

您可能想要的是matlab术语。

代码语言:javascript
复制
for i = 1:nt
    usol = u0
    u0(2:nx) = usol(2:nx) - c*dt/dx*(usol(2:nx)-usol(1:nx-1))
end%for

这意味着在空间维上有两个内部循环,两个向量操作中的每一个都有一个。这两个单独的循环在C代码中必须是显式的

代码语言:javascript
复制
    //apply numerical scheme forward difference in
    //time an backward difference in space
    for (int i = 0; i < nt; i++) {
        //first loop iterates through each time step
        for (int j = 0; j < nx; j++) {
            usol_array[j] = u0_array[j];
            //printf("%f", usol_array[i]);
        } 
        

        //second loop iterates through each grid point for that time step 
        //and applies the update equation
        for (int j = 1; j < nx; j++)
        {
            u0_array[j] = usol_array[j] - c * dt/dx * (usol_array[j] - usol_array[j - 1]);
            printf("%f, ", u0_array[j]);
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66121129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档