I have a code that is randomly crashing on me without any explanations. I'm not sure what is going on. When I run the code, it says that the .exe file has stopped working, and the command window displays the following:
"Process returned -1073741819 <0xC0000005> execution time : 16.747 s. Press any key to continue.
My compiler says the same thing without giving me any details.
I thought maybe I had an infinite while loop going on, but when I turn off my while loop, the code still crashes. I also tested out my function, SGN, to make sure that it works, and that doesn't seem to be the problem. The only thing that I can think of is the way I'm dynamically allocating my 2D arrays, U and E, and my 1D array, t. But on a previous thread, I thought I had this resolved, so I'm not totally sure.
I've attached my code for reference. I'm using CodeBlocks as my compiler. I really appreciate any help that I can get!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <fstream>
#include <cmath>
#include <algorithm>
#define pi 3.14159265
using namespace std;
const int M = 40; //X-Grid Intervals
double L0 = -1;
double LF = 1;
double tmax = 0.15; //Final Time
int N = 40; //Maximum t-vector Size
//Dynamic Memory Allocation
double ** U;
double ** E;
double * t = new double [N];
int SGN(double aP)
{
if (aP > 0)
return 1;
else if (aP == 0)
return 0;
else if (aP < 0)
return -1;
}
int main()
{
//Create X Vector and Initial Conditions
double x [M+1];
U = new double * [M+1];
E = new double * [M+1];
for ( int i = 0; i < M + 1; i++ ) * U = new double [N];
for ( int i = 0; i < M + 1; i++ ) * E = new double [N];
x[0] = L0;
x[M] = LF;
for (int i = 1; i < M; i++)
{
x[i] = -1 + (i - 0.5)*2/M;
U[i][0] = 0.25 + 0.5*sin(pi*x[i]);
E[i][0] = 0.5*U[i][0]*U[i][0];
}
U[0][0] = U[M-1][0];
E[0][0] = 0.5*U[0][0]*U[0][0];
U[M][0] = U[1][0];
E[M][0] = 0.5*U[M][0]*U[M][0];
double dx = x[2] - x[1];
//Allocate Variables and Vectors
double DUP;
double DUM;
double aP [M+1];
double aM [M+1];
double dtt [M+1];
double dt;
t[0] = 0;
int s = 0;
int ERR = 0;
while (ERR == 0)
{
//Calculate Alpha and Time Step
for (int i = 1; i < M; i++)
{
DUP = U[i+1][s] - U[i][s];
DUM = U[i][s] - U[i-1][s];
if (DUP == 0)
{
aP[i] = U[i][s];
}
else
{
aP[i] = (E[i+1][s] - E[i][s])/(U[i+1][s] - U[i][s]);
}
if (DUM == 0)
{
aM[i] = U[i][s];
}
else
{
aM[i] = (E[i][s] - E[i-1][s])/(U[i][s] - U[i-1][s]);
}
dtt[i] = dx/abs(aP[i]);
}
//Find Minimum Time Step and Round To Low Value
dt = dtt[1];
for (int i = 2; i < M; i++)
{
if (dtt[i] < dt)
{
dt = dtt[i];
}
}
//Even Out Time Step
if (t[s] + dt > tmax)
{
dt = tmax - t[s];
}
//Calculate Solution For New Time Step
for (int i = 1; i < M; i++)
{
U[i][s+1] = U[i][s] - 0.5*dt/dx*(1 - SGN(aP[i]))*(E[i+1][s] - E[i][s]) - 0.5*dt/dx*(1 + SGN(aM[i]))*(E[i][s] - E[i-1][s]);
E[i][s+1] = 0.5*U[i][s+1]*U[i][s+1];
}
//Period Boundary Condition
U[0][s+1] = U[M-1][s+1];
U[M][s+1] = U[1][s+1];
E[0][s+1] = 0.5*U[0][s+1]*U[0][s+1];
E[M][s+1] = 0.5*U[M][s+1]*U[M][s+1];
//Calculate New Time
t[s+1] = t[s] + dt;
s = s + 1;
//Expand Arrays
if (s >= N)
{
N = N*2;
double ** temp;
double ** temp2;
double * temp3 = new double [N];
temp = new double * [M+1];
temp2 = new double * [M+1];
for ( int i = 0; i < M + 1; i++ ) * temp = new double [N];
for ( int i = 0; i < M + 1; i++ ) * temp2 = new double [N];
for (int j = 0; j < s; j++)
{
for (int i = 0; i < M+1; i++)
{
temp[i][j] = U[i][j];
temp2[i][j] = E[i][j];
}
temp3[j] = t[j];
}
delete [] U;
delete [] E;
delete [] t;
U = temp;
E = temp2;
t = temp3;
}
//Check To See If T-max Has Been Reached
if (t[s] >= tmax)
{
ERR = 1;
}
}
//Output Data Into Text File
ofstream outputdata("timevector.txt");
for (int i = 0; i<N; i++)
{
outputdata << t[i] << endl;
}
outputdata.close();
ofstream outputdata3("xvector.txt");
for (int j = 0; j<M+1; j++)
{
outputdata3 << x[j] << endl;
}
outputdata3.close();
ofstream outputdata2("U.txt");
for (int i = 0; i<N; i++)
{
for (int j = 0; j<M+1; j++)
{
outputdata2 << U[j][i] << endl;
}
}
outputdata2.close();
return 0;
}
|