我用C编写的程序有一个问题,它解决了一维线性对流方程。基本上,我已经初始化了两个数组。第一个数组(u0_array)是一个数组,数组元素在间隔上设置为两个或0.5
我遇到的问题是代码末尾的嵌套for循环。此循环将计算下一个点所需的更新方程应用于数组中的每个元素。当我运行我的脚本并试图打印结果时,我得到的输出只是-1.IND 00,用于循环的每一次迭代。(我是跟随Matlab风格的伪代码,我也附在下面),我是非常新的C,所以我的经验显示。我不知道为什么会这样。如果有人能对此提出可能的解决办法,我将非常感激。到目前为止,我已经将我的代码附在下面,并附上了一些评论,这样您就可以遵循我的思维过程了。我还附加了Matlab风格的伪代码,我遵循。
# 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风格)下面。
发布于 2021-02-09 14:49:58
而不是整数除法,而是使用FP数学。
这避免了后面的除以0和-1.#IND00。
// int dx = 2 / (nx - 1); quotient is 0.
double dx = 2.0 / (nx - 1);OP的代码与注释不匹配
// 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,则更容易查看。
// 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?发布于 2021-02-09 15:18:22
您可能想要的是matlab术语。
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代码中必须是显式的
//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]);
}
}https://stackoverflow.com/questions/66121129
复制相似问题