-1

在这一点上,我真的很矛盾。我在 C 中填充一个三维数组,如下所示:

double maxEval = 0;        
double Evals[2][le-1];  
double Waves[2][2][le-1];

for(int i=0; i<le-1; i++){

  getEvalsandWaves(&Evals[0][i], &Evals[1][i], &Waves[0][0][i], &Waves[0][1][i],
                   &Waves[1][0][i], &Waves[1][1][i], g, dx, &maxEval,
                   Q[0][i], Q[0][i+1], Q[1][i], Q[1][i+1]);

  if(myRank==0){
    printf("i= %d, %f  %f\n",i,Waves[0][0][i],Waves[0][1][i]);
    printf("       %f  %f\n\n",Waves[1][0][i],Waves[1][1][i]);
  }
}

getEvalsandWaves 的原型函数和函数在这里:

/*The objective of this function is to calculate eigenvalues which have a close*/
/*form (e1 and e2) as well as the wave speeds associated with each eigenvalue  */
/*[w00; w10]) and [w01;w11] which then are stored in a thee dimensional matrix */

void getEvalsandWaves(double *e1, double *e2, double *w00,double *w01, double *w10,
                      double *w11, double g,double dx, double *max, double him, 
                      double hi, double uim, double ui);

void getEvalsandWaves(double *e1, double *e2, double *w00, double *w01, double *w10, 
                      double *w11, double g, double dx, double *max, double him, 
                      double hi, double uim, double ui){

                      double hbar=0; double ubar=0;

                      /*this function only returns the Roe averages */
                      /*using the values him,hi,uim,ui which are all*/
                      /*doubles */
                      RoeAvg(&hbar, &ubar,him,hi,uim,ui);

                      (*e1) = ubar - sqrt(g*hbar);
                      (*e2) = ubar + sqrt(g*hbar);

                      /*I tried to use this values instead of (*e1) and (*e2)*/
                      /*in calculating the w'sbelow to see if it fixed the   */
                      /*problem but it didn't work.                          */
                      double ei1 = ubar - sqrt(g*hbar);
                      double ei2 = ubar + sqrt(g*hbar);

                      if(fabs((*e1))>(*max))
                      (*max)=fabs((*e1));

                      if(abs((*e2))>(*max))
                      (*max)=fabs((*e2));

                      double c = 1/(2*sqrt(g*hbar));

                      (*w00) = c * ( ei2*(hi-him)+(ui-uim) ) * 1;      
                      (*w01) = c * ( (-1)*ei1*(hi-him)-(ui-uim) ) * 1;
                      (*w10) = c * ( ei2*(hi-him)+(ui-uim) ) * ei1;    
                      (*w11) = c * ( (-1)*ei1*(hi-him)-(ui-uim) ) * ei2;
}

所以这里是我不知道发生了什么的地方。如果您注意到在 for 循环中,我正在打印三维数组 Waves 的值,给出以下示例输出:

...
i= 47, 0.000000  0.000000
       -0.000000  0.000000

i= 48, 0.000000  0.000000
       -0.000000  0.000000

i= 49, -1.000000  -1.000000
        1.414214  -1.414214
...

这是正确的值。但是,如果我在 for 循环之后立即打印 Waves 的值,这就是我得到的:

...
i= 47, 0.000000  0.000000
      -1.000000  -1.000000

i= 48, 0.000000  0.000000
       0.000000  0.000000

i= 49, -1.000000  -1.000000
        0.000000  0.000000
...

我真的不明白为什么当 for 循环完成时值会发生变化。任何帮助将不胜感激。(Evals 数组和 maxEval 变量在循环后确实保持正确的值。)

4

1 回答 1

0

感谢那些花时间阅读我的问题的人,并对一些评论的回复很晚感到抱歉,但我正忙于另一个项目。

所以,我在阅读这篇文章后解决了这个问题:

3D 数组如何存储在 C 中?

所以我知道我的评论根本没有帮助试图解释我在做什么,但最终它归结为试图存储一系列矩阵。所以我们可以将数组 A[x][y] 视为一个二维数组,我想将其中的 z 个存储为 A[x][y][z]。

我知道 C 将二维数组存储为线性数组,但是在阅读了上面链接中的文章后,我意识到如果我尝试将 7 存储在数组的第三个矩阵中的 [0][1] 位置为:

A[0][1][3]=7;

然后尝试稍后检索它,我会得到不同的东西。原因是如果我尝试以这种方式存储它,我必须考虑 C 如何将三维数组存储为一维数组,因此索引会有所不同。

我应该将 A[x][y][z] 作为一个 x 数组的数组来阅读,每个数组都有 z 个元素。最后,出于我的目的(存储一组 z 矩阵),工作是:

A[z][x][y];
A[3][0][1]=7;

这样,如果我以后检索,

printf("The value at A[3][0][1]= %d",A[3][0][1]);

它会打印出正确的值七。

这篇文章更好地解释了我刚刚想说的话。如果有人有更多想法或意见,请告诉我。

于 2013-03-26T15:30:54.023 回答