繁体   English   中英

发生错误 EXC_BAD_ACCESS 时如何找到我的错误?

[英]How do I find my Bug when Error EXC_BAD_ACCESS occurs?

我的代码有问题。

我总是得到错误

消息:EXC_BAD_ACCESS(代码=2,地址=0x7ffee99613e8)

你能帮我么。 我很绝望。

这是我的主要内容:

#include <iostream>
#include <fstream>
#include <cmath>
#define PI 3.14159265359

using namespace std;

int main() {

    //------------------------
    // SPATIAL INPUT VARIABLES
    //------------------------
    double StorageHeight=1;
    double StorageDiameter=2;
    int nCell=3000;

    //------------------------
    // TIME INPUT VARIABLES
    //------------------------
    double tCharge=10;
    double tDischarge=0;
    double tDwell=0;
    int nCycles=6;
    int nTimeStepsPerCycle=2000;

    //------------------------
    // OTHER INPUT VARIABLES
    //------------------------
    double u_f=0.1;
    double alpha_f=2e-7;
    double alpha_s=2e-7;

    double T_f_0=200;
    double T_f_inlet=1200;
    double T_s_0=210;
    double T_s_inlet=1000;

    //------------------------
    // USER INPUTS
    //------------------------
/*   cout << "Enter Storage Height (m):" << "\n";
     cin >> StorageHeight;

     cout << "Enter Storage Diameter (m):" << "\n";
     cin >> StorageDiameter;

     cout << "Enter Initial Fluid Temperature (K):" << "\n";
     cin >> T_f_0;

     cout << "Enter Initial Solid Temperature (K):" << "\n";
     cin >> T_s_0;

     cout << "Enter Number of Cells (-):" << "\n";
     cin >> nCell;

    cout << "Enter Charging State Duration (s):" << "\n";
    cin >> tCharge;

    cout << "Enter Discharging State Duration (s):" << "\n";
    cin >> tDischarge;

    cout << "Enter Dwell State Duration (s):" << "\n";
    cin >> tDwell;

    cout << "Enter Number of Cycles (-):" << "\n";
    cin >> nCycles;

    cout << "Enter Number of Timesteps per Cycle (-):" << "\n";
    cin >> dt;*/

    //Time (Cycle) related calculations
    double tFullCycle = tCharge+tDischarge+tDwell;
    double tCycle = tFullCycle;
    double dt = tFullCycle/nTimeStepsPerCycle;  //timestepwidth
    //-------------------------------
    // ORDER VERIFICATION STUDY (OVS)
    //-------------------------------

    //Check from User if MMS required
    int MMStest;
    cout << "Would you like to run MMS? If YES press 1, if NO press 0" << endl;
    cin >> MMStest;
    int p;

    //Number of iterations for varying numbers of cells
    int MaxNbrCell = 16; //For MaxNbrCells = 2 ^ MaxNbrCells
    int PlotNumber = 3;  //For PlotNumber = 2 ^ PlotNumber

    p = (1-MMStest) * 1 + MMStest * (MaxNbrCell-2);

    int m =0;

    //Start itration for different numbers of cells
    for(int m = 0; m < p; m++)
    {
        nCell = pow(2,(m+3))* MMStest + (1-MMStest) * nCell;
        //nCell = 50*(m+1) * MMStest + (1-MMStest) * nCell;*/

        cout << ">>>>>>>>>>"<< nCell <<"<<<<<<<<<<" << endl;

        //Temperature matrix initialisation
        double T_f[nTimeStepsPerCycle][nCell]; //Fluid
        double T_s[nTimeStepsPerCycle][nCell]; //Solid

        //Gridwith calculation
        double dx = (StorageHeight)/double(nCell);

        //---------------------------------
        // METHODS OF MANUFACTURED SOLUTION
        //---------------------------------
        double MMSArray[int(nCell)];
        int n1 = 1;
        double k = 2 * PI * n1 / StorageHeight;

        //Manufactured Solution
        for(int i = 1; i < nCell; i++) {
            MMSArray[i] = cos(k * i * dx);
        }

        //Stability Testing
        if ((dt <= dx / u_f + (2 * alpha_f) / (u_f * u_f)) && (dt <= (dx * dx) / (u_f * dx + 2 * alpha_f)) && dt <= dx*dx/alpha_s) {
            cout << "Stable!" << endl;
        } else {
            cout << "Not Stable" << endl;
        }

        //Initial Condition Fluid
        for (int i = 0; i < nCell; i++) {
            T_f[0][i] = T_f_0 * (1 - MMStest) + MMStest * cos(k * i * dx);
        }

        //--------------------
        //SOLVER FOR THE FLUID
        //--------------------

        for (int i = 0; i < nTimeStepsPerCycle - 1; i++) {

            for (int j = 0; j < nCell; j++) {

                //Boundary condition (left border)
                if (j == 0) {
                    T_f[i][0] = T_f_inlet * (1 - MMStest) + (MMStest);
                }

                //Boundary condition (right border)
                if (j == nCell - 1) {
                    T_f[i + 1][j] = -(u_f * dt) * (T_f[i][j] - T_f[i][j - 1]) / (dx) + T_f[i][j]
                                    + (MMStest) * u_f * (dt/dx) * (cos(k * (j+0.5) * dx) - cos(k * (j-0.5) * dx));
                                    //+ (MMStest) * alpha_f * k * (dt/dx) * (sin(k * (j+0.5)  * dx) - sin(k * (j-0.5) * dx));
                }

                //Discretized equation solver
                else {
                    T_f[i + 1][j] = dt * (alpha_f * (T_f[i][j + 1] - 2 * T_f[i][j] + T_f[i][j - 1]) / (dx * dx)
                                          - (u_f) * (T_f[i][j] - T_f[i][j - 1]) / (dx)) + T_f[i][j]
                                                  + (MMStest) * u_f * (dt/dx) * (cos(k * (j+0.5) * dx) - cos(k * (j-0.5) * dx))
                                                  + (MMStest) * alpha_f * k * (dt/dx) * (sin(k * (j+0.5)  * dx) - sin(k * (j-0.5) * dx));
                }
            }
        }

        //Initial Condition Solid
        for (int i = 0; i < nCell; i++) {
            T_s[0][i] = T_f[40][i] * (1 - MMStest) + MMStest * cos(k * i * dx);
        }

        //--------------------
        //SOLVER FOR THE SOLID
        //--------------------

        for (int i = 0; i < nTimeStepsPerCycle - 1; i++) {

            for (int j = 0; j < nCell; j++) {

                //Boundary condition (left border)
                if (j == 0) {
                    T_s[i][0] = T_s_inlet * (1 - MMStest) + (MMStest);
                }

                //Boundary condition (right border)
                if (j == nCell - 1) {
                    T_s[i+1][j] = T_s[i][j];
                }

                //Discretized equation solver
                else {
                    T_s[i + 1][j] = T_s[i][j] + ((alpha_s*dt)/(dx*dx))*(T_s[i][j + 1] - 2 * T_s[i][j] + T_s[i][j - 1])
                                                + MMStest * (-alpha_s * k * dt / dx * (sin(k * (j+0.5) * dx) - sin(k * (j+0.5) * dx))) ;
                }
            }
        }

        //-------------------------------------------------------------------------


        double T_f_M[nCell]; //Solid

        T_f_M[0] = 1;

        for (int j = 1; j < nCell-1; j++){
                T_f_M[j + 1] = 2 * T_f_M[j] - T_f_M[j - 1] + u_f * dx / alpha_f * (T_f_M[j] - T_f_M[j - 1])
                - k*dx*(sin(k * (j+0.5) * dx) - sin(k * (j-0.5) * dx)) - u_f / dx * (cos(k * (j+0.5) * dx) - cos(k * (j-0.5) * dx));

        }

        if(m==(PlotNumber-3) || m==0) {
            ofstream myfile333;
            myfile333.open("MMS.csv");

            if (myfile333.is_open()) {
                for (int k = 0; k < nCell; k++) {

                    myfile333 << T_f_M[k] << endl;

                }
            }
            myfile333.close();
        }



        //-------------------------------------------------------------------------

        //Threshold for steady state condition
        double threshhold_fluid = 0.00005;
        double threshhold_solid = 0.005;
        int tSteadyState;

        //--------------
        // OVS for fluid
        //--------------

        if(MMStest == 1) {

            cout << "---------FLUID---------" << endl;
            //Find steady state
            double delta_x_num = 0;

            //Calculation L1-Norm for successive time steps
            for (int i = 0; i < nTimeStepsPerCycle; i++) {

                for (int j = 0; j < nCell; j++) {
                    delta_x_num = delta_x_num + fabs((T_f[i + 1][j] - T_f[i][j]) / T_f[i][j]);
                }

                delta_x_num = delta_x_num / double(nCell);

                //Condition for steady state
                if (delta_x_num < threshhold_fluid) {
                    tSteadyState = i;
                    //tSteadyState = nTimeStepsPerCycle - 1;
                    cout << "Steady State at " << tSteadyState << endl;
                    break;

                 //Else, take last timestep
                } else if (i == nTimeStepsPerCycle - 1) {
                    cout << "No Steady State found!" << endl;
                    tSteadyState = nTimeStepsPerCycle - 1;
                    break;
                }
            }

            //Error calculation for E1
            double e1 = 0;

            //Calculate L1-Norm
            for (int i = 0; i < nCell; i++) {
                e1 = e1 + fabs((T_f[tSteadyState][i] - MMSArray[i]));
                //cout << fabs((T_f[tSteadyState][i] - MMSArray[i])) << endl;
            }

            double E1 = e1 / double(nCell);
            cout << "L1-Norm " << E1 << endl;

            //CSV File for order of accuracy plot
            if (myfile4.is_open()) {
                myfile4 << E1 << ";" << e1 << ";" << delta_x_num << ";" << nCell << ";" << dx << ";"
                        << nCell * nTimeStepsPerCycle << ";" << k << "\n";
            }
        }
        //--------------
        // OVS for solid
        //--------------

        if(MMStest == 1) {

            cout << "---------SOLID---------" << endl;
            //Find steady state
            double delta_x_num = 0;

            //Calculation L1-Norm for successive timesteps
            for (int i = 0; i < nTimeStepsPerCycle; i++) {

                for (int j = 0; j < nCell; j++) {
                    delta_x_num = delta_x_num + fabs((T_s[i + 1][j] - T_s[i][j]) / T_s[i][j]);
                }

                delta_x_num = delta_x_num / double(nCell);

                //Condition for steady state
                if (delta_x_num < threshhold_solid) {
                    tSteadyState = i;
                    //tSteadyState = nTimeStepsPerCycle - 1;
                    cout << "Steady State at " << tSteadyState << endl;
                    break;

                    //Else, take last timestep
                } else if (i == nTimeStepsPerCycle - 1) {
                    cout << "No Steady State found!" << endl;
                    tSteadyState = nTimeStepsPerCycle - 1;
                    break;
                }
            }

            //Error calculation for E1
            double e1 = 0;

            //Calculate L1-Norm
            for (int i = 0; i < nCell; i++) {
                e1 = e1 + fabs((T_s[tSteadyState][i] - MMSArray[i]));
                //cout << fabs((T_f[tSteadyState][i] - MMSArray[i])) << endl;
            }

            double E1 = e1 / double(nCell);
            cout << "L1-Norm " << E1 << endl << endl << endl;

            //CSV File for order of accuracy plot
            if (myfile41.is_open()) {
                myfile41 << E1 << ";" << e1 << ";" << delta_x_num << ";" << nCell << ";" << dx << ";"
                        << nCell * nTimeStepsPerCycle << ";" << k << "\n";
            }
        }
}

这里有一些随意的想法:居住在别处肯定是她喜欢的法律。 令他惊讶的是,从前夫人认为很少有斯坦希尔温和。 在权力比赛上的真相更糟糕的声音会。 大的意义应该是一场比赛。 出乎意料的是,它在正式的结果中保持沉默。 询问其他地方描述的饮食问题的能力保证。 食欲在解锁高级育种 position 关注。 欢快的百叶窗还没有被反复放映。 没有,因为希望在三点钟。 防止表现出肥他是错误的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM