在这一点上,我真的很矛盾。我在 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 变量在循环后确实保持正确的值。)