- #1
ssatonreb
- 5
- 0
I'm learning c++ and currently trying to write a little programme for 1D linear convection (wave equation).
I have managed set all boundary and initial conditions as well as a mesh.
But I have reached the point where I can't understand why the program does what it does.
In red I highlighted place where I am confused.
If someone could help me please.
In particularly I do not understand why does no matter what I do in the highlighted area of the code my mesh points are changed afterwards.
I really appreciate someones help.
I have managed set all boundary and initial conditions as well as a mesh.
But I have reached the point where I can't understand why the program does what it does.
In red I highlighted place where I am confused.
If someone could help me please.
In particularly I do not understand why does no matter what I do in the highlighted area of the code my mesh points are changed afterwards.
Code:
// 1D linear convection
#include <iostream>
#include <cmath>
using namespace std;
int main(){
double nx=10; // mesh points
double nt=2; // time steps
double dt=0.01; // time step size
double c=1; // constant (wave propagation velocity)
double dx=2/(nx-1); // mesh size
int i,n ;
const int NX = nx, NT = nt; // array size assignment
double x[NX], u[NT][NX]; // array declaration
memset( u, 0, sizeof u ); // Zeroing out array u[NT][NX]
memset( x, 0, sizeof x ); // Zeroing out array x[NX]
// Construction of the mesh
for (i = 1 ; i < NX ; i++){
x[i]=x[i-1]+dx;
}
// Initial and Boundary Conditions
for (i = 0 ; i < NX ; i++){
if ((x[i]>=0.5) && (x[i]<=1)){
u[0][i]=2;
}
else
u[0][i]=1;
}
[COLOR="Red"]// The problem lies here
// --------------------------------------------------------------------------
for (n=0; n<NT ; n++){
for (i=1; i<NX-1; i++) {
u[n+1][i]=u[n][i]-c*dt/dx*(u[n][i]-u[n][i-1]);
}
}
// -------------------------------------------------------------------------- [/COLOR]
return 0;}
I really appreciate someones help.