找回密码
 注册
Simdroid-非首页
查看: 103|回复: 1

[2. C/C++/C#] 为什么文件输出的数据不对

[复制链接]
发表于 2011-6-19 19:27:26 | 显示全部楼层 |阅读模式 来自 英国
新手请教各位前辈。在init_f中试图输出两个向量f and hrhs,这两个向量都随着x的变动而变动,x从-4.5逐渐递增至1.5。两个文件ff1.dat and fhrhs1.dat的第一列均为x,第二列为对应的f and hrhs的值。由定义可知,f恒为零,而hrhs为抛物线曲线形状,但为什么fhrhs1.dat的第二列输出的hrhs的数据不对?请教各位。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>

  4. /* FMG Solver of the EHL circular contact */
  5. #define pi 3.1415926535897931
  6. typedef struct
  7. {
  8. double hx;
  9. int ii;
  10. double *p, *f;
  11. double *hfi, *hrhs;
  12. double *w;
  13. double *K;
  14. double *pjac;  /*old pressure for use in Jacobi relaxation*/
  15. double rg;
  16. double Hm;
  17. } Level;

  18. typedef struct
  19. {
  20. int nx0;
  21. int maxlevel;
  22. double xa,xb;
  23. double h0;   /*global constant and work unit*/
  24. Level *Lk;
  25. } Stack;

  26. void initialize(Stack *U, int nx0, int maxl, double xa, double xb, double h0)
  27. {
  28. /* initialize values in datastructure */
  29. double hx;
  30. Level *L;
  31. int i,ii;
  32. U->xa=xa;
  33. U->xb=xb;
  34. U->maxlevel=maxl;
  35. U->h0=h0;
  36. U->Lk=(Level *)calloc(maxl+1,sizeof(Level));
  37. hx=(xb-xa)/nx0;
  38. ii=nx0;
  39. for (i=1; i<=maxl; i++)
  40. {
  41. L=U->Lk+i;
  42. L->hx=hx;
  43. L->ii=ii;
  44. L->p =(double *)calloc(U->maxlevel+1,sizeof(double));
  45. L->w =(double *)calloc(U->maxlevel+1,sizeof(double));
  46. L->f =(double *)calloc(U->maxlevel+1,sizeof(double));
  47. L->pjac =(double *)calloc(U->maxlevel+1,sizeof(double));
  48. L->hfi =(double *)calloc(U->maxlevel+1,sizeof(double));
  49. L->hrhs=(double *)calloc(U->maxlevel+1,sizeof(double));
  50. L->K =(double *)calloc(U->maxlevel+1,sizeof(double));
  51. printf("\n level: %2d ii=%4d, hx=%f",i,ii,hx);
  52. hx*=0.5; ii*=2;
  53. }
  54. }
  55. /********* SINGLE GRID ROUTINES *****/
  56. void init_f(Stack *U, int lev)
  57. {
  58. int i;
  59. Level *L;
  60. double x;
  61. L=U->Lk+lev;
  62. for (i=0; i<=L->ii; i++)
  63. {
  64. x=U->xa+i*L->hx;
  65. L->hrhs[i]=0.5*x*x;
  66. L->f[i]=0.0;
  67. }
  68. printf("\n xa and hx are %f and %f",U->xa,L->hx);
  69. FILE *ff1,*fhrhs1;
  70. ff1=fopen("f1.dat","w");
  71. fhrhs1=fopen("hrhs1.dat","w");
  72. for (i=0; i<=L->ii; i++)
  73. {
  74. x=U->xa+i*L->hx;
  75. fprintf(ff1,"%f %f\n",x,L->f[i]);
  76. fprintf(fhrhs1,"%f %f\n",x,L->hrhs[i]);
  77. }
  78. fprintf (ff1, "\n");
  79. fprintf (fhrhs1, "\n");
  80. fclose(ff1);
  81. fclose(fhrhs1);
  82. L->rg=-pi/2.0;
  83. }

  84. /********** MAIN PROGRAM **********/
  85. void main()
  86. {
  87. Stack U;
  88. int i;
  89. initialize(&U,128,1,-4.5,1.5,-0.5);//initialize(Stack *U, int nx0, int maxl, double xa, double xb, double h0)
  90. init_f(&U,1);
  91. }


复制代码
 楼主| 发表于 2011-6-20 01:59:40 | 显示全部楼层 来自 英国
Simdroid开发平台
问题已解决 感谢关注
initialize中的几条中的maxlevel改为ii
L->p =(double *)calloc(U->maxlevel+1,sizeof(double));

L->w =(double *)calloc(U->maxlevel+1,sizeof(double));

L->f =(double *)calloc(U->maxlevel+1,sizeof(double));

L->pjac =(double *)calloc(U->maxlevel+1,sizeof(double));

L->hfi =(double *)calloc(U->maxlevel+1,sizeof(double));

L->hrhs=(double *)calloc(U->maxlevel+1,sizeof(double));

L->K =(double *)calloc(U->maxlevel+1,sizeof(double));
回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Simapps系列直播

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-11-1 11:36 , Processed in 0.036021 second(s), 14 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表