免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1868 | 回复: 7
打印 上一主题 下一主题

[C++] 请大家帮满看看这个程序为什么停止运行 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-01-03 22:00 |只看该作者 |倒序浏览
  1. //general scalar equation: d(rho*phi)/dt=d/dx(rho*D*(dphi/dx))
  2. //W=West(i); C=centre(i+1); E=East(i+2)
  3. //w=west(i+1/2); e=east(i+3/2)

  4. #include<iostream>                                   
  5. #include<fstream>                                      //for files
  6. using namespace std;

  7. void Square(int n, double dx, double a, double b, double x[], double y[])
  8. {
  9.      int i;
  10.      for(i=0; i<n; i++)
  11.     {
  12.              x[i]=(i+0.5)*dx;                          //define initial x value for each cell, mid-point
  13.                  if (i>a&&i<b)                             //define initial y value for each cell   
  14.                  {
  15.                      y[i]=1.0;
  16.                  }                                
  17.                  else
  18.                  {
  19.                      y[i]=0.0;     
  20.                  }                        
  21.     }
  22. }

  23. double Diffusion(int i, double dx, double y[], double rho[], double D[])
  24. {
  25.      double yW, yC, yE, rhoW, rhoC, rhoE, dW, dC, dE, phiW, phiC, phiE, phiw, phie, rhodw, rhode;
  26.      yW=y[i]; yC=y[i+1]; yE=y[i+2];                               //y at W, C and E
  27.      rhoW=rho[i]; rhoC=rho[i+1]; rhoE=rho[i+2];                   //density at W, C and E
  28.      dW=D[i]; dC=D[i+1]; dE=D[i+2];                               //diffusivity at W, C and E
  29.      phiW=yW/rhoW; phiC=yC/rhoC; phiE=yE/rhoE;                    //y divided by density to get phi
  30.      phiw=phiC-phiW; phie=phiE-phiC;                              //dleta phi
  31.      rhodw=(rhoW*dW+rhoC*dC)/2; rhode=(rhoE*dE+rhoC*dC)/2;        //density*diffusivity at w and e
  32.      return((rhode*phie-rhodw*phiw)/(dx*dx));                     //diffusion term
  33. }

  34. void Output(int n, double x[], double y[])
  35. {
  36.     int i;
  37.     ofstream outdata;                                            //libray for output                     
  38.     outdata.open("output.dat");                                  //open the file
  39.     if(outdata.is_open())                                        //check if the file is open
  40.     {
  41.              for(i=0; i<n; i++)                                  //for each point
  42.              {
  43.                       outdata<<x[i]<<" "<<y[i]<<endl;            //write the value of x and y to file
  44.              }
  45.     outdata.close();                                             //close the file
  46.     }
  47.     else                                                         //if _open() return falsch
  48.     {
  49.              for(i=0; i<n; i++)
  50.              {
  51.                       cout<<x[i]<<" "<<y[i]<<endl;
  52.              }
  53.     }
  54. }

  55. int main()
  56. {
  57.     double l=1.0;                                      //total length
  58.     double tpmax=1.0;                                  //total time
  59.     double dt=0.01;                                    //time step interval
  60.     const int n=100;                                   //define cell number
  61.     double dx, a, b, tp;
  62.     double x[n], y[n], rho[n], D[n], RHS[n], ybuffer[n];
  63.     dx=l/n;                                            //calculate the length of each cell
  64.     a=0.3*n;                                           //define the critical point
  65.     b=0.7*n;                                           //define the critical point
  66.     int i;
  67.     for(i=0; i<n; i++)                                 //define the density and diffusivity values of air
  68.     {
  69.              rho[i]=1.225;
  70.              D[i]=0.00002;
  71.     }
  72.                                                         
  73.     Square(n, dx, a, b, &x[n], &y[n]);                               //switch on square
  74.    
  75.     for(tp=0.0; tp<tpmax; tp=tp+dt)
  76.     {
  77.               for(i=0; i<(n-1); i++)
  78.               {
  79.                        RHS[i+1]=Diffusion(i, dx, &y[n], &rho[n], &D[n]);                //switch on diffusion                                 
  80.                        ybuffer[i+1]=y[i+1]+dt*RHS[i+1];                                 //explicit time integration to get y
  81.                        y[i+1]=ybuffer[i+1];
  82.               }
  83.     }
  84.    
  85.     Output(n, &x[n], &y[n]);                                         //results output
  86.    
  87.     return 0;                                                        // finish
  88. }
复制代码
没有报错,但是在.exe中会停止运行

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
2 [报告]
发表于 2013-01-04 09:50 |只看该作者
Square(n, dx, a, b, &x[n], &y[n]);
--->
Square(n, dx, a, b, x, y);

论坛徽章:
6
技术图书徽章
日期:2013-11-13 11:11:27子鼠
日期:2014-02-20 17:54:13处女座
日期:2014-06-16 17:43:33午马
日期:2014-08-08 09:11:17未羊
日期:2014-08-10 11:57:072015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2013-01-04 17:16 |只看该作者
bruceteen 发表于 2013-01-04 09:50
Square(n, dx, a, b, &x[n], &y[n]);
--->
Square(n, dx, a, b, x, y);

我觉得LZ问的可能不是这个。

论坛徽章:
0
4 [报告]
发表于 2013-01-04 17:54 |只看该作者
回复 2# bruceteen


    谢谢,现在能运行完了

论坛徽章:
0
5
发表于 2013-01-04 17:56
回复 3# littledick

谢谢,能运行了,但可能还有其他问题,但最起码不会停止了
   

论坛徽章:
0
6
发表于 2013-01-04 17:57
回复 3# littledick

谢谢,能运行了,但可能还有其他问题,但最起码不会停止了
   

论坛徽章:
0
7
发表于 2013-01-04 17:58
回复 3# littledick

谢谢,能运行了,但可能还有其他问题,但最起码不会停止了
   

论坛徽章:
0
8 [报告]
发表于 2013-01-04 17:59 |只看该作者
回复 3# littledick

谢谢,能运行了,但可能还有其他问题,但最起码不会停止了
   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP