Help with the c++ code

++Hello guys,
I wrote this code to solve 2d heat diffusion problem by finite difference method. The code is compiling(generic compiler,ubuntu) and running fine. no problem with that. Although i initialized the top boundary condition to 10 here: f0[0][j] = 10.0, f[0][j] = 10.0 , you can see from the output part the first row all zero. I want it to be 10.
What i want to ask is, am i missing something? I am really new to C++, so i have no idea what is not working. Thank you in advance.


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <fstream>
#define m 10
#define n 10
using namespace std;


int main(){
double f0[n][m],f[n][m],x[n],y[m];
double dx, time, dy, dt, alpha, mstep, termx, termy, dd, w;
dx = 1.0;
dy = 1.0;
dt = 0.20;
alpha = 0.20;
mstep = 3200;
FILE * mFile;
w = (dt/(dx*dy))*alpha;

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

for(int i=0; i<n+1; i++){
f0[i][j] = 0.0;
f[i][j] = 0.0;
}
}

for(int j=0; j<m; j++){
f0[0][j] = 10.0;
f[0][j] = 10.0;
f0[n][j] = 0.0;
f[n][j] = 0.0;
}
for(int i=0; i<n; i++){
f0[i][0] = 0.0;
f[i][0] = 0.0;
//adiabatic bottom boundary
f0[i][0] = f0[i][1];
f[i][0] = f[i][1];
//zero temp bottom boundary
f0[i][m]=0.0;
f[i][m]=0.0;
}

for(int k=1; k<mstep; k++){
for(int j=1; j<m-1; j++){
for(int i=1; i<n-1; i++){
termx = (f0[i+1][j]+f0[i-1][j]); // /(dx*dx);
termy = (f0[i][j+1]+f0[i][j-1]); // /(dy*dy);
dd = 1./(dx*dx)+1./(dy*dy);
f[i][j] = w*(termx + termy) + (-4*w + alpha/dt)*f0[i][j];
}
}
for(int j=1; j<m-1; j++){
for(int i=1; i<n-1; i++){
f0[i][j] = f[i][j];

}
}
}

x[0] = 0.0;
for(int i=1; i<n; i++){
x[i] = x[i-1] + dx;
}
y[0] = 0.0;
for(int j=1; j<m; j++){
y[j] = y[j-1] + dy;
}

mFile = fopen("cppfda2.txt","w");
for(int i=0; i<n; i++){
fprintf(mFile,"%.1f\t",x[i]);
for(int j=1; j<m; j++){
fprintf(mFile,"%.1f\t",y[j]);
}
fprintf(mFile,"\n");
}
for(int j=0; j<m; j++){
for(int i=0; i<n; i++){

fprintf(mFile,"%.5f\t",f[j][i]);
}
}
fclose(mFile);
return 0;
}

*****output******
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 4.86291 6.69137 7.44755 7.73462 7.73462 7.44755 6.69137 4.86291 0.00000
0.00000 2.76026 4.45502 5.36422 5.75630 5.75630 5.36422 4.45502 2.76026 0.00000
0.00000 1.72311 3.00424 3.79801 4.17007 4.17007 3.79801 3.00424 1.72311 0.00000
0.00000 1.12792 2.04082 2.65351 2.95589 2.95589 2.65351 2.04082 1.12792 0.00000
0.00000 0.74777 1.37761 1.81930 2.04411 2.04411 1.81930 1.37761 0.74777 0.00000
0.00000 0.48555 0.90253 1.20199 1.35712 1.35712 1.20199 0.90253 0.48555 0.00000
0.00000 0.29191 0.54498 0.72901 0.82527 0.82527 0.72901 0.54498 0.29191 0.00000
0.00000 0.13709 0.25647 0.34379 0.38969 0.38969 0.34379 0.25647 0.13709 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
Topic archived. No new replies allowed.