PDA

View Full Version : My program crashs when I run it



Ryan111
4th May 2017, 18:17
Hi guys
I'm an aerospace enginer and today wrote a program to use it as a calculator for determining some parameter of boundary layer (https://en.wikipedia.org/wiki/Boundary_layer). it's simple and no need to make it complex. ok, enough.:) now about the program, this is my program:


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtMath>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{

// Sample input data
quint32 Mmax = 100, Mest=21;
quint32 Uinf = 10;
double_t Cnu = 0.0002 , Xi = 1 , Xf = 2, Fm1, Yod, st, ta, A, B, C, H1, H, F1, F2, Y[100] , U[100], U0[100], V[100], V0[100], Re, DX, DY=0.00112, Ue=1, DUeDX=-0.05, X, Delt=0.0224, Delst=0.00771, Thetha=0.00297, Cf=0.00297;
Re = Uinf/Cnu;
X=Xi;
quint32 i=0;
QString text = " Program ILBLE\n 2-D Boundary layer, incompressible laminar\n 1ST order\n Explicit Method\n Input: MMAX, Re, Dy\n";
text.append(" ");
QString str = QVariant(Mmax).toString();
text.append(str);
text.append(" ");
str = QVariant(Re).toString();
text.append(str);
text.append(" ");
str = QVariant(DY).toString();
text.append(str);
text.append(" \n Flat plate plus ramp flow\n");

for(i=1 ; i <= Mmax ; i++){
U[i]= Ue;
U0[i]=Ue;
V[i]=0;
V0[i]=0;
}
U[1]=0;
U0[1]=0;
Fm1= Mest-1;
for(i=2; i<=Mest; i++){
Yod=(i-1)/Fm1;
U0[i]=Ue*((1.5*Yod)-(0.5*qPow(Yod, 3)));
}
text.append(" X , Delt , Delst , Thetha , Cf , Ue , DUeDX");
str = QVariant(X).toString();
text.append(str);
text.append(" ");
str = QVariant(Delt).toString();
text.append(str);
text.append(" ");
str = QVariant(Delst).toString();
text.append(str);
text.append(" ");
str = QVariant(Thetha).toString();
text.append(str);
text.append(" ");
str = QVariant(Cf).toString();
text.append(str);
text.append(" ");
str = QVariant(Ue).toString();
text.append(str);
text.append(" ");
str = QVariant(DUeDX).toString();
text.append(str);
text.append(" \n");

Hezar:

DX=0.4*U0[2]*DY*DY*Re;
if(X <= Xf){goto navadnoh;}
if(X+DX > Xf){
DX=Xf-Xf;
}
X=X+DX;
Ue=1.05-(X/20);
DUeDX=-0.05;
U0[Mmax]=Ue;

//Explicit

st=0.5;
ta=0;

for(i=2; i <= Mmax-1 ; i++){
A=(1/Re)-(V0[i]*(DY/2));
B=(1/Re)+(V0[i]*(DY/2));
C=(Ue*DUeDX*(qPow(DY,2)))-2*U0[i]/Re;
H1=0.5*DY/DX;
H=DX/(U0[i]*DY*DY);
U[i]=U0[i]+H*((A*U0[i+1])+(B*U0[i-1])+C);
V[i]=V[i-1]-H1*(U[i]-U0[i]+U[i-1]-U0[i-1]);

F1=1-(U[i]/Ue);
F2=F1*U[i]/Ue;
st=st+F1;
ta=ta+F2;

}

//end

Delst=Delst*DY;
Thetha=Thetha*DY;

for(i=0;i<=Mmax ; i++){
if(U[i] > 0.99*Ue){
Mest=i;
break;
}
}

Delt=(Mest-1)*DY;
Cf= (4*U[2]-U[3])/(qPow(Ue,2)*DY*Re);
if(Cf <= 0.0001){
text.append(" Near seperation at X= ");
str = QVariant(X).toString();
text.append(str);
text.append(" \n");
goto navadnoh;
}

str = QVariant(X).toString();
text.append(str);
text.append(" ");
str = QVariant(Delt).toString();
text.append(str);
text.append(" ");
str = QVariant(Delst).toString();
text.append(str);
text.append(" ");
str = QVariant(Thetha).toString();
text.append(str);
text.append(" ");
str = QVariant(Cf).toString();
text.append(str);
text.append(" ");
str = QVariant(Ue).toString();
text.append(str);
text.append(" ");
str = QVariant(DUeDX).toString();
text.append(str);
text.append(" \n");

for(i=2 ; i <= Mmax ; i++){
U0[i]=U[i];
V0[i]=V[i];

}

goto Hezar;

navadnoh:

text.append(" i , Y , U , V \n");
for(i=1; i<= Mmax ; i++){
Y[i]=(i-1)*DY;

str = QVariant(i).toString();
text.append(str);
text.append(" ");
str = QVariant(Y[i]).toString();
text.append(str);
text.append(" ");
str = QVariant(U[i]).toString();
text.append(str);
text.append(" ");
str = QVariant(V[i]).toString();
text.append(str);
text.append(" \n");

}

ui->textEdit->setText(text);



}


There is a button in the form of the program that when I click it, it's supposed to do the calculation and show me the result but when I run the program and click on "calculate", get this message:

http://sl.uploads.im/hGcux.png


What's the problem?

d_stranz
4th May 2017, 18:55
Are you running your program in the debugger? Crashes like this are found by running in the debugger, and when the crash occurs you examine the call stack to see exactly which line the program is executing when it crashes.

But I can give you a big hint. You're obviously porting this code from FORTRAN, where arrays are indexed starting at 1, so valid indexes are [1, ..., 100] for an array of size 100. In C/C++, arrays are indexed beginning at 0, so valid indexes are [0, ..., 99] for the same size array. For starters, look at the bounds on the indexes in your for() loops.

But just fixing the for() loops isn't going to make this code run correctly - there are lots of hard-coded indexes (like U[2]. U[3], etc.) which will also need to be changed.

The QVariant(X).toString() stuff is a little weird. It is more common to convert a number to a string using the QString::number() static methods:



text += QString::number( X, 'g', 6 );


which will convert X to a string with 6 digits of precision and a format suitable for its magnitude. (That is, fixed point or scientific notation, whichever gives the shorter string).

If you want to format a whole line of output, you can use the QString::arg() syntax:



double X = 3.1415927e42;
double Y = 3.1415927;
long Z = 42;
text = QString( "Here is a line with values for X: %1, Y: %2, and Z: %3\n" ).arg( X, 'g', 6 ).arg( Y, 'f', 3 ).arg( Z );


which will give the line: Here is a line with values for X: 3.141593E+42, Y: 3.141593, and Z: 42

ars
4th May 2017, 19:03
Hello,

your arrays U, ... have a size of 100, i.e. they provide access to the elements U[0], ..., U[99]. Your for-loop runs with indices 1 through 100 (inclusive), thus you access the arrays out of bound. A debugger will show you where the crashes occur.

Best regards
ars

Edit: a little bit too late :-)