| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 417 人关注过本帖
标题:请大家帮忙看看这个程序为什么会终止运行
只看楼主 加入收藏
jszjvictor
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2013-1-3
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
请大家帮忙看看这个程序为什么会终止运行
没有报错,但是在.exe时会停止运行
程序代码:
//general scalar equation: d(rho*phi)/dt=d/dx(rho*D*(dphi/dx))
//W=West(i); C=centre(i+1); E=East(i+2)
//w=west(i+1/2); e=east(i+3/2)
#include<iostream>                                 
#include<fstream>                                      //for files
using namespace std;
void Square(int n, double dx, double a, double b, double x[], double y[])
{
     int i;
     for(i=0; i<n; i++)
    {
             x[i]=(i+0.5)*dx;                          //define initial x value for each cell, mid-point
             if (i>a&&i<b)                             //define initial y value for each cell 
             y[i]=1.0;                               
             else
             y[i]=0.0;                               
    }
}
double Diffusion(int i, double dx, double y[], double rho[], double D[])
{
     double yW, yC, yE, rhoW, rhoC, rhoE, dW, dC, dE, phiW, phiC, phiE, phiw, phie, rhodw, rhode;
     yW=y[i]; yC=y[i+1]; yE=y[i+2];                               //y at W, C and E
     rhoW=rho[i]; rhoC=rho[i+1]; rhoE=rho[i+2];                   //density at W, C and E
     dW=D[i]; dC=D[i+1]; dE=D[i+2];                               //diffusivity at W, C and E
     phiW=yW/rhoW; phiC=yC/rhoC; phiE=yE/rhoE;                    //y divided by density to get phi
     phiw=phiC-phiW; phie=phiE-phiC;                              //dleta phi
     rhodw=(rhoW*dW+rhoC*dC)/2; rhode=(rhoE*dE+rhoC*dC)/2;        //density*diffusivity at w and e
     return((rhode*phie-rhodw*phiw)/(dx*dx));                     //diffusion term
}
void Output(int n, double x[], double y[])
{
    int i;
    ofstream outdata;                                            //libray for output                   
    outdata.open("output.dat");                                  //open the file
    if(outdata.is_open())                                        //check if the file is open
    {
             for(i=0; i<n; i++)                                  //for each point
             {
                      outdata<<x[i]<<" "<<y[i]<<endl;            //write the value of x and y to file
             }
    outdata.close();                                             //close the file
    }
    else                                                         //if _open() return falsch
    {
             for(i=0; i<n; i++)
             {
                      cout<<x[i]<<" "<<y[i]<<endl;
             }
    }
}
int main()
{
    double l=1.0;                                      //total length
    double tpmax=1.0;                                  //total time
    double dt=0.01;                                    //time step interval
    const int n=100;                                   //define cell number
    double dx, a, b, tp;
    double x[n], y[n], rho[n], D[n], RHS[n], ybuffer[n];
    dx=l/n;                                            //calculate the length of each cell
    a=0.3*n;                                           //define the critical point
    b=0.7*n;                                           //define the critical point
    int i;
    for(i=0; i<n; i++)                                 //define the density and diffusivity values of air
    {
             rho[i]=1.225;
             D[i]=0.00002;
    }
                                                      
    Square(n, dx, a, b, &x[n], &y[n]);                               //switch on square
  
    for(tp=0.0; tp<tpmax; tp=tp+dt)
    {
              for(i=0; i<(n-1); i++)
              {
                       RHS[i+1]=Diffusion(i, dx, &y[n], &rho[n], &D[n]);                //switch on diffusion                               
                       ybuffer[i+1]=y[i+1]+dt*RHS[i+1];                                 //explicit time integration to get y
                       y[i+1]=ybuffer[i+1];
              }
    }
  
    Output(n, &x[n], &y[n]);                                         //results output
  
    return 0;                                                        // finish
}

搜索更多相关主题的帖子: equation general include color 
2013-01-03 20:45
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:20 
英语不好,看不懂了

数组不是那么传参的
Square(n, dx, a, b, x, y); 


 


[fly]存在即是合理[/fly]
2013-01-03 21:15
ljw970243
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:43
专家分:105
注 册:2011-8-20
收藏
得分:0 
数组越界了,仔细检查一下.
2013-01-04 11:12
jszjvictor
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2013-1-3
收藏
得分:0 
回复 2楼 azzbcc
谢谢,运行不中止了
2013-01-04 17:54
jszjvictor
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2013-1-3
收藏
得分:0 
回复 3楼 ljw970243
谢谢!
2013-01-04 17:55
yangmingcout
Rank: 2
等 级:论坛游民
帖 子:12
专家分:12
注 册:2012-12-30
收藏
得分:0 
有点复杂
2013-01-04 18:21
快速回复:请大家帮忙看看这个程序为什么会终止运行
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019469 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved