Hello. I am newbie in c++.
I got an assignment that asked me to make a one dimensional heat transfer problem by using finite difference explicit method with particular boundary condition.
The discretization should be u[i,j] = ((1 - 2 * 0.1)* u(i-1,j-1) + ((0.1)*(u(i+1,j-1)) + ((0.1)* (u(i - 1,j-1))
which i is counter of the percentage length and j is counter of time. I know it would be easier if I made the program into 2D vector i.e %length and time.
But, I was asked to make it just 1D, and put the counter of time as looping process. I supposed to save the iteration every 20 loops
I tried the program below. It seemed working, but the .dat file didn't occur.
So, I couldn't see the result.
could somebody help me finding the mistakes?
thank you in advance
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
|
/*
* One Dimensional Heat Transfer
*/
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
void saveArray(vector<double>&u, int nx, int nt, double dt, double dx, double e);
int main()
{
double t = 0;
int nx = 11;
double dt = 0.001;
double dx = 0.1;
double e = 0;
vector<double> u(nx);
vector<double> un(nx);
// Initial condition:
for (int i = 1; i <= nx - 1; ++i)
{
if (i*dx >= 0.0 && i*dx <= 0.5) //u(i,0)=2*dx, 0<=dx<=0.5
{
u[i] = 2 * i*dx;
}
else if (i*dx >= 0.5&&i*dx <= 1.0) //u(i,0)=2*(1-dx), 0.5<=dx<=1.0
{
u[i] = 2 * (1 - (i*dx));
}
cout << u[i] << endl;
}
// Finite-difference loop:
u[0] = un[0]; // Boundary Condition
for (int k = 0; k <= nx - 1; ++k)
{
un[k] = u[k];
}
for (int i = 1; i <= nx - 2; i++)
{
u[i] = ((1 - 2 * 0.1)* un[i]) + ((0.1)*(un[i + 1])) + ((0.1)* (un[i - 1]));
e = u[i] - un[i];
cout << u[i] << endl;
}
if (e >= 0.0001)
{
int nt = 0; nt++;
}
return 0;
}
// Save array to file:
void saveArray(vector<double>&u, int nx, int nt, double dt, double dx, double e)
{
ofstream myfile;
myfile.open("1d_convection02.dat");
myfile << "%\t" << "nx = " << nx << "\tnt = " << nt <<
"\tdt = " << dt << endl;
for (int i = 0; i <= nx - 1; i++)
{
myfile << i*dx << "\t\t";
for (int j = 0; j%nt==20; j++)
{
myfile << u[i] << "\t\t";
}
myfile << endl;
}
}
|