数组大小会影响代码结果。

时间:2014-04-04 08:00:41

标签: c++ arrays

在下面的代码中,在Fluid类中,我定义了一个大小为[M] [N] [4]的3D矩阵。 (其中,出于测试原因,M = N) 当我运行M和N值低于105的代码时,它运行得很好,但如果我增加它们的值,编译器(Xcode)指向

Fluid U_func; 

并写

  

主题1:EXC_BAD_ACCESS

我忘了宣布什么? 谢谢!

这是我的代码,

#include <iostream>
#include <cmath>
using namespace std;
const int M = 100;
const int N = 100;
const double gama = 1.4;
const double alpha = 3;

const double Time = 0.3;
const double Cf = 0.75; //  The desired CFL number

const double x_1 = 0;
const double x_2 = 1;
const double y_1 = 0;
const double y_2 = 1;

const double rho1 = 1.5;
const double v1 = 0.0;
const double u1 = 0.0;
const double p1 = 1.5;

const double rho2 = 0.5323;
const double v2 = 1.206;
const double u2 = 0.0;
const double p2 = 0.3;

const double rho3 = 0.5323;
const double v3 = 0.0;
const double u3 = 1.206;
const double p3 = 0.3;

const double rho4 = 0.138;
const double v4 = 1.206;
const double u4 = 1.206;
const double p4 = 0.029;


class Fluid
{
public:
    double Array_U[M+1][N+1][4], Array_Prime[M+1][N+1][4],Array_F[M+1][N+1][4],Array_G[M+1][N+1][4], dU_dY[M+1][N+1][4], dU_dX[M+1][N+1][4], dU_dT[M+1][N+1][4], dF_dT[M+1][N+1][4], dG_dT[M+1][N+1][4], dF_dY[M+1][N+1][4], dG_dX[M+1][N+1][4];
    double dx=(double(x_2-x_1))/double(M);
    double dy=(double(y_2-y_1))/double(N);

    void SetInitialConditions(int initialX, int finalX, int initialY, int finalY, double rho, double u, double v, double p)  // initial conditions
    {
        for (int i = initialX; i<=finalX; i++)
            for (int j = initialY ; j<=finalY; j++) {
                Array_U[i][j][0]=rho;
                Array_U[i][j][1]=rho*u;;
                Array_U[i][j][2]=rho*v;
                Array_U[i][j][3]= (p/(gama-1)) + 0.5*rho*(u*u+v*v);

            }

        // get initial Uprime
        for (int i = 0; i <= M ; i++) {
            for (int j = 0 ; j <= N ; j++) {
                for (int k = 0 ; k < 4 ; k++) {
                    Array_Prime[i][j][k]=Array_U[i][j][k];
                }
            }
        }
        // get intial dU_dX and dU_dY
        for (int i=0 ; i<M; i++) {
            for (int j=0 ; j<N ; j++) {
                for (int k=0; k<4; k++) {
                    dU_dY[i][j][k]=dU_dX[i][j][k]=0;
                }
            }
        }
        for(int k=0 ; k<4 ; k++)
        {
            dU_dX[M][N][k]=0;
            dU_dY[M][N][k]=0;
        }

    }
    void get_F(double Array_U[M+1][N+1][4], double Array_F[M+1][N+1][4])
    {
        for (int i=0; i<=M; i++)
        {
            for (int j=0; j<=N; j++)
            {
                double u=Array_U[i][j][1]/Array_U[i][j][0];
                double v=Array_U[i][j][2]/Array_U[i][j][0];
                double rho=Array_U[i][j][0];
                double P=(gama-1)*(Array_U[i][j][3]-0.5*(Array_U[i][j][1]*Array_U[i][j][1]+Array_U[i][j][2]*Array_U[i][j][2])/
                            Array_U[i][j][0]);
                Array_F[i][j][0]=Array_U[i][j][1];
                Array_F[i][j][1]=rho*u*u+P;
                Array_F[i][j][2]=rho*u*v;
                Array_F[i][j][3]=(Array_U[i][j][3]+P)*u;
            }
        }

    }
    void get_G(double Array_U[M+1][N+1][4], double Array_G[M+1][N+1][4])
    {
        for (int i=0; i<=M; i++)
        {
            for (int j=0; j<=N; j++)
            {
                double u=Array_U[i][j][1]/Array_U[i][j][0];
                double v=Array_U[i][j][2]/Array_U[i][j][0];
                double rho=Array_U[i][j][0];
                double P=(gama-1)*(Array_U[i][j][3]-0.5*(Array_U[i][j][1]*Array_U[i][j][1]+Array_U[i][j][2]*Array_U[i][j][2])/
                                   Array_U[i][j][0]);
                Array_G[i][j][0]=Array_U[i][j][2];
                Array_G[i][j][1]=rho*u*v;
                Array_G[i][j][2]=rho*v*v+P;
                Array_G[i][j][3]=(Array_U[i][j][3]+P)*v;
            }
        }
    }

    void Boundaries (double Array_U[M+1][N+1][4])
    {
        for(int k=0 ; k<4; k++)
        {
            for (int i = 0 ; i<=M ; i++) {
                Array_U[i][0][k]=Array_U[i][1][k];
                Array_U[i][N][k]=Array_U[i][N-1][k];
            }
            for (int j = 0 ; j<=N ; j++) {
                Array_U[0][j][k]=Array_U[1][j][k];
                Array_U[M][j][k]=Array_U[M-1][j][k];
            }
        }
    }

    void printOutput(double U[M+1][N+1][4])
    {
        double x,y,dx,dy, u,v, rho,p;
        dx=(double(x_2-x_1))/double(M);
        dy=(double(y_2-y_1))/double(N);

        FILE *fp;
        fp=fopen("result.dat","w+");
        fprintf(fp,"%2.38s\n %20.60s\n %20.18s\t %2.3d\t %2.18s\t %2.3d\t %2.18s\n","TITLE = \"2D-EULER.dat\"","variables = \"x\", \"y\", \"rho\", \"u\", \"v\", \"p\"","zone i=", M+1,"j=",N+1,"f=point");
        for(int i=0; i<=M; i++)
        {
            for(int j=0; j<=N; j++)
            {
                x=x_1+i*dx;
                y=y_1+j*dy;
                rho=U[i][j][0];
                u=U[i][j][1]/U[i][j][0];
                v=U[i][j][2]/U[i][j][0];
                p=(gama-1)*(U[i][j][3]-0.5*U[i][j][0]*(u*u+v*v));
                fprintf(fp,"%20.5f\t %20.5f\t %20.5f\t %20.5f\t %20.5f\t %20.5f\n",x,y,rho,u,v,p);
            }
        }
        fclose(fp);
    }
};
class Derivate
{
public:
    void get_derivative_values (double U[M+1][N+1][4],double dU_dX[M+1][N+1][4], double dU_dY[M+1][N+1][4],double dU_dT[M+1][N+1][4],double dF_dT[M+1][N+1][4],double dG_dT[M+1][N+1][4],double dF_dY[M+1][N+1][4],double dG_dX[M+1][N+1][4])
    {
        double gama_minus_1,gama_minus_3,a1,a2,a3,a4,a5,a6;
        gama_minus_1=gama-1.0;
        gama_minus_3=gama-3.0;
        for(int i = 0 ; i <= M ; i++)
        {
            for(int j = 0 ; j <= N ; j++)
            {
                double u=U[i][j][1]/U[i][j][0];
                double v=U[i][j][2]/U[i][j][0];

                a1=(-gama_minus_3*u*u-gama_minus_1*v*v)/2;
                a2=(-gama_minus_3*v*v-gama_minus_1*u*u)/2;
                a3=gama*u*(U[i][j][3]/U[i][j][0])-gama_minus_1*(u*u*u+u*v*v);
                a4=-gama*(U[i][j][3]/U[i][j][0])+(3*u*u+v*v)*gama_minus_1/2;
                a5=gama*v*(U[i][j][3]/U[i][j][0])-gama_minus_1*(v*v*v+u*u*v);
                a6=-gama*(U[i][j][3]/U[i][j][0])+(3*v*v+u*u)*gama_minus_1/2;

                dU_dT[i][j][0]=-dU_dX[i][j][1]-dU_dY[i][j][2];
                dU_dT[i][j][1]=a1*dU_dX[i][j][0]+gama_minus_3*u*dU_dX[i][j][1]+gama_minus_1*v*dU_dX[i][j][2]-gama_minus_1*dU_dX[i][j][3]+u*v*dU_dY[i][j][0]-v*dU_dY[i][j][1]-u*dU_dY[i][j][2];
                dU_dT[i][j][2]=u*v*dU_dX[i][j][0]-v*dU_dX[i][j][1]-u*dU_dX[i][j][2]+a2*dU_dY[i][j][0]+gama_minus_1*u*dU_dY[i][j][1]+gama_minus_3*v*dU_dY[i][j][2]-gama_minus_1*dU_dY[i][j][3];
                dU_dT[i][j][3]=a3*dU_dX[i][j][0]+a4*dU_dX[i][j][1]+gama_minus_1*u*v*dU_dX[i][j][2]-gama*u*dU_dX[i][j][3]+a5*dU_dY[i][j][0]+gama_minus_1*u*v*dU_dY[i][j][1]+a6*dU_dY[i][j][2]-gama*v*dU_dY[i][j][3];

                dF_dT[i][j][0]=dU_dT[i][j][1];
                dF_dT[i][j][1]=-a1*dU_dT[i][j][0]-gama_minus_3*u*dU_dT[i][j][1]-gama_minus_1*v*dU_dT[i][j][2]+gama_minus_1*dU_dT[i][j][3];
                dF_dT[i][j][2]=-u*v*dU_dT[i][j][0]+v*dU_dT[i][j][1]+u*dU_dT[i][j][2];
                dF_dT[i][j][3]=-a3*dU_dT[i][j][0]-a4*dU_dT[i][j][1]-gama_minus_1*u*v*dU_dT[i][j][2]+gama*u*dU_dT[i][j][3];

                dF_dY[i][j][0]=dU_dY[i][j][1];
                dF_dY[i][j][1]=-a1*dU_dY[i][j][0]-gama_minus_3*u*dU_dY[i][j][1]-gama_minus_1*v*dU_dY[i][j][2]+gama_minus_1*dU_dY[i][j][3];
                dF_dY[i][j][2]=-u*v*dU_dY[i][j][0]+v*dU_dY[i][j][1]+u*dU_dY[i][j][2];
                dF_dY[i][j][3]=-a3*dU_dY[i][j][0]-a4*dU_dY[i][j][1]-gama_minus_1*u*v*dU_dY[i][j][2]+gama*u*dU_dY[i][j][3];

                dG_dT[i][j][0]=dU_dT[i][j][2];
                dG_dT[i][j][1]=-u*v*dU_dT[i][j][0]+v*dU_dT[i][j][1]+u*dU_dT[i][j][2];
                dG_dT[i][j][2]=-a2*dU_dT[i][j][0]-gama_minus_1*u*dU_dT[i][j][1]-gama_minus_3*v*dU_dT[i][j][2]+gama_minus_1*dU_dT[i][j][3];
                dG_dT[i][j][3]=-a5*dU_dT[i][j][0]-gama_minus_1*u*v*dU_dT[i][j][1]-a6*dU_dT[i][j][2]+gama*v*dU_dT[i][j][3];

                dG_dX[i][j][0]=dU_dX[i][j][2];
                dG_dX[i][j][1]=-u*v*dU_dX[i][j][0]+v*dU_dX[i][j][1]+u*dU_dX[i][j][2];
                dG_dX[i][j][2]=-a2*dU_dX[i][j][0]-gama_minus_1*u*dU_dX[i][j][1]-gama_minus_3*v*dU_dX[i][j][2]+gama_minus_1*dU_dX[i][j][3];
                dG_dX[i][j][3]=-a5*dU_dX[i][j][0]-gama_minus_1*u*v*dU_dX[i][j][1]-a6*dU_dX[i][j][2]+gama*v*dU_dX[i][j][3];

            }
        }
    }

    // Get dudx and dudy for complete time step
    void dU_dX_dU_dY_n(double U[M+1][N+1][4],double U_p[M+1][N+1][4],double dU_dX[M+1][N+1][4], double dU_dY[M+1][N+1][4], double dU_dT[M+1][N+1][4],double dt)
    {
        double dU_dX_plus,dU_dX_min,dx, dU_dY_plus,dU_dY_min,dy;
        dx=(double(x_2-x_1))/double(M);
        dy=(double(y_2-y_1))/double(N);
        for(int i = 1; i <= M ; i++)
        {
            for(int j = 1 ; j <= N ; j++)
            {
                for(int k = 0 ; k < 4 ; k++)
                {
                    dU_dX_plus=(U_p[i][j-1][k]+dU_dT[i][j-1][k]*dt/2+U_p[i][j][k]+dU_dT[i][j][k]*dt/2-2*U[i][j][k])/dx;
                    dU_dX_min=-(U_p[i-1][j-1][k]+dU_dT[i-1][j-1][k]*dt/2+U_p[i-1][j][k]+dU_dT[i-1][j][k]*dt/2-2*U[i][j][k])/dx;
                    dU_dX[i][j][k]=Wfunct(dU_dX_plus, dU_dX_min);

                    dU_dY_plus=(U_p[i][j][k]+dU_dT[i][j][k]*dt/2+U_p[i-1][j][k]+dU_dT[i-1][j][k]*dt/2-2*U[i][j][k])/dy;
                    dU_dY_min=-(U_p[i-1][j-1][k]+dU_dT[i-1][j-1][k]*dt/2+U_p[i][j-1][k]+dU_dT[i][j-1][k]*dt/2-2*U[i][j][k])/dy;
                    dU_dY[i][j][k]=Wfunct(dU_dY_plus, dU_dY_min);
                }
            }
        }
    }

    // Get dudx and dudy for half time step
    void dU_dX_dU_dY_p(double U[M+1][N+1][4],double U_p[M+1][N+1][4],double dU_dX[M+1][N+1][4], double dU_dY[M+1][N+1][4], double dU_dT[M+1][N+1][4],double dt)
    {
        double dU_dX_plus,dU_dX_min,dx, dU_dY_plus,dU_dY_min,dy;
        dx=(double(x_2-x_1))/double(M);
        dy=(double(y_2-y_1))/double(N);

        for(int i = 1 ; i < M ; i++)
        {
            for(int j = 1 ; j < N ; j++)
            {
                for(int k = 0 ; k < 4 ; k++)
                {
                    dU_dX_plus=(U[i+1][j+1][k]+dU_dT[i+1][j+1][k]*dt/2+U[i+1][j][k]+dU_dT[i+1][j][k]*dt/2-2*U_p[i][j][k])/dx;
                    dU_dX_min=-(U[i][j+1][k]+dU_dT[i][j+1][k]*dt/2+U[i][j][k]+dU_dT[i][j][k]*dt/2-2*U_p[i][j][k])/dx;
                    dU_dX[i][j][k]=Wfunct(dU_dX_plus, dU_dX_min);

                    dU_dY_plus=(U[i+1][j+1][k]+dU_dT[i+1][j+1][k]*dt/2+U[i][j+1][k]+dU_dT[i][j+1][k]*dt/2-2*U_p[i][j][k])/dy;
                    dU_dY_min=-(U[i][j][k]+dU_dT[i][j][k]*dt/2+U[i+1][j][k]+dU_dT[i+1][j][k]*dt/2-2*U_p[i][j][k])/dy;
                    dU_dY[i][j][k]=Wfunct(dU_dY_plus, dU_dY_min);
                }
            }
        }
    }




private:
    double Wfunct(double value_plus, double value_min)
    {
        return (pow(fabs(value_plus),alpha)*value_min+pow(fabs(value_min),alpha)*value_plus)/(pow(fabs(value_plus),alpha)+pow(fabs(value_min),alpha)+1e-8);
    }

};

class CESE: public Derivate, Fluid {
    public:
    void CE_SE_method (double U[M+1][N+1][4],double U_p[M+1][N+1][4],double dU_dX[M+1][N+1][4],double dU_dY[M+1][N+1][4],double dU_dT[M+1][N+1][4],double F[M+1][N+1][4],double dF_dY[M+1][N+1][4],double dF_dT[M+1][N+1][4],double G[M+1][N+1][4],double dG_dX[M+1][N+1][4],double dG_dT[M+1][N+1][4])
    {

        double dt,t,dx,dy,U1,G1,F1;
        dx=(double(x_2-x_1))/double(M);
        dy=(double(y_2-y_1))/double(N);
        t=0;

        // Initial_Values(U, U_p, dU_dX, dU_dY); To be removed of the function and will be put before the program is run.

        while(t<Time)
        {
            dt=CFL(U);
            t=t+dt;
            printf("t=%f\n",t);
            Boundaries(U);
            get_F(U,F);
            get_G(U,G);
            get_derivative_values (U,dU_dX,dU_dY,dU_dT,dF_dT,dG_dT,dF_dY,dG_dX);
            // GET U_p
            for(int i = 0 ; i < M ; i++) // To get U_prime
            {
                for(int j = 0 ; j < N ; j++)
                {
                    for(int k = 0 ; k < 4 ; k++)
                    {
                        U1=U[i][j][k]+0.25*dx*dU_dX[i][j][k]+0.25*dy*dU_dY[i][j][k]+U[i+1][j][k]-0.25*dx*dU_dX[i+1][j][k]+0.25*dy*dU_dY[i+1][j][k]+U[i+1][j+1][k]-0.25*dx*dU_dX[i+1][j+1][k]-0.25*dy*dU_dY[i+1][j+1][k]+U[i][j+1][k]+0.25*dx*dU_dX[i][j+1][k]-0.25*dy*dU_dY[i][j+1][k];

                        G1=G[i][j][k]+0.25*dx*dG_dX[i][j][k]+0.25*dt*dG_dT[i][j][k]+G[i+1][j][k]-0.25*dx*dG_dX[i+1][j][k]+0.25*dt*dG_dT[i+1][j][k]-G[i+1][j+1][k]+0.25*dx*dG_dX[i+1][j+1][k]-0.25*dt*dG_dT[i+1][j+1][k]-G[i][j+1][k]-0.25*dx*dG_dX[i][j+1][k]-0.25*dt*dG_dT[i][j+1][k];

                        F1=F[i][j][k]+0.25*dy*dF_dY[i][j][k]+0.25*dt*dF_dT[i][j][k]-F[i+1][j][k]-0.25*dy*dF_dY[i+1][j][k]-0.25*dt*dF_dT[i+1][j][k]-F[i+1][j+1][k]+0.25*dy*dF_dY[i+1][j+1][k]-0.25*dt*dF_dT[i+1][j+1][k]+F[i][j+1][k]-0.25*dy*dF_dY[i][j+1][k]+0.25*dt*dF_dT[i][j+1][k];

                        U_p[i][j][k]=0.25*U1+(0.25*dt/dy)*G1+(0.25*dt/dx)*F1;
                    }
                }
            }


            dU_dX_dU_dY_p(U,U_p,dU_dX,dU_dY,dU_dT,dt);
            get_F(U_p,F);
            get_G(U_p,G);
            get_derivative_values (U_p,dU_dX,dU_dY,dU_dT,dF_dT,dG_dT,dF_dY,dG_dX);
            for(int i = 1 ; i < M ; i++) // To get U
            {
                for(int j = 1 ; j < N ; j++)
                {
                    for(int k = 0 ; k < 4 ; k++)
                    {
                        U1=U_p[i-1][j-1][k]+0.25*dx*dU_dX[i-1][j-1][k]+0.25*dy*dU_dY[i-1][j-1][k]+U_p[i][j-1][k]-0.25*dx*dU_dX[i][j-1][k]+0.25*dy*dU_dY[i][j-1][k]+U_p[i][j][k]-0.25*dx*dU_dX[i][j][k]-0.25*dy*dU_dY[i][j][k]+U_p[i-1][j][k]+0.25*dx*dU_dX[i-1][j][k]-0.25*dy*dU_dY[i-1][j][k];

                        G1=G[i-1][j-1][k]+0.25*dx*dG_dX[i-1][j-1][k]+0.25*dt*dG_dT[i-1][j-1][k]+G[i][j-1][k]-0.25*dx*dG_dX[i][j-1][k]+0.25*dt*dG_dT[i][j-1][k]-G[i][j][k]+0.25*dx*dG_dX[i][j][k]-0.25*dt*dG_dT[i][j][k]-G[i-1][j][k]-0.25*dx*dG_dX[i-1][j][k]-0.25*dt*dG_dT[i-1][j][k];

                        F1=F[i-1][j-1][k]+0.25*dy*dF_dY[i-1][j-1][k]+0.25*dt*dF_dT[i-1][j-1][k]-F[i][j-1][k]-0.25*dy*dF_dY[i][j-1][k]-0.25*dt*dF_dT[i][j-1][k]-F[i][j][k]+0.25*dy*dF_dY[i][j][k]-0.25*dt*dF_dT[i][j][k]+F[i-1][j][k]-0.25*dy*dF_dY[i-1][j][k]+0.25*dt*dF_dT[i-1][j][k];

                        U[i][j][k]=0.25*U1+(0.25*dt/dy)*G1+(0.25*dt/dx)*F1;
                    }
                }
            }
            //Boundaries(U);
            dU_dX_dU_dY_n(U,U_p,dU_dX,dU_dY,dU_dT,dt);

        }
    }
private:
    double CFL (double U[M+1][N+1][4])
    {
        double dx=double(x_2-x_1)/double(M),dy=double(y_2-y_1)/double(N);
        double max_vel, vel, p,v,u;
        max_vel=1E-8;
        for (int i = 1 ; i <= M ; i++)
        {
            for (int j = 1 ; j <= N ; j++)
            {
                u=U[i][j][1]/U[i][j][0];
                v=U[i][j][2]/U[i][j][0];
                p=(gama-1)*(U[i][j][3]-0.5*U[i][j][0]*(u*u+v*v));
                vel = sqrt(gama*p/U[i][j][0])+sqrt(u*u+v*v);
                if (vel>max_vel)max_vel=vel;
            }
        }
        return Cf*dx*dy/(dy*max_vel+dx*max_vel);
    }
};





int main( )
{
    Fluid U_func;
    CESE Calculate;

    clock_t start,finish;
    double duration;
    start=clock();

    U_func.SetInitialConditions(0, M, 0, N, rho1, u1, v1, p1);
    U_func.SetInitialConditions(M/2, M, 0, N/2, rho2, u2, v2, p2);
    U_func.SetInitialConditions(0, M/2, N/2, N, rho3, u3, v3, p3);
    U_func.SetInitialConditions(0, M/2, 0, N/2, rho4, u4, v4, p4);
    Calculate.CE_SE_method(U_func.Array_U, U_func.Array_Prime, U_func.dU_dX, U_func.dU_dY, U_func.dU_dT, U_func.Array_F, U_func.dF_dY, U_func.dF_dT, U_func.Array_G, U_func.dG_dX, U_func.dG_dT);

    U_func.printOutput(U_func.Array_U);
    finish=clock();
    duration=(double)(finish-start)/CLOCKS_PER_SEC;
    cout << "Complete in \n" << duration << "s \n";

    return 0;
}

1 个答案:

答案 0 :(得分:3)

您的fluid对象将占用相当多的空间:有10个106 * 106 * 4 * 8字节的数组(假设double为8字节)。这是每个阵列360K,或总共不到4MB。

您尝试在main中用于存储fluid对象的堆栈空间有限。通常,使用兆字节的堆栈空间是导致此类崩溃的一种方法。可能会增加堆栈空间,但最有可能相对有限,因此如果您希望在维度中运行超过100-200个元素,则需要采用不同的策略。

有几种可能的解决方案 - 所有这些都涉及或多或少直接使用动态分配:

  1. std::vector<std::vector<double [4]>>对象中使用fluid存储空间。
  2. fluid对象使用动态内存分配 - 例如fluid* U_func = new fluid;
  3. 重新组织您的计算数据以存储在struct data { ... }中,并具有此类数据的向量。这可能更复杂,但不一定有助于提高性能。
  4. 编辑:我忘了在StackOverflow.com上询问如何解决Stack Overflow问题。对不起,有时我发现自己很有趣,但没有其他人这样做...... :)