1D heat transfer

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;
	}
}

You could try actually calling the method you've written :)))
I'm sorry, I can't catch up what you mean
Well, you created the function saveArray, but never actually called it. In other words, the function isn't being used.
ah ya, I forgot about it. Thank you.

I've placed it into main loop function as

 
saveArray(u, nx, nt, dx, dt, e)


before "return", but apparently still doesn't work :(

it said that



Error 1 error LNK2019: unresolved external symbol "void __cdecl saveArray(class std::vector<double,class std::allocator<double> >
&,int,double,double)" (?saveArray@@YAXAAV?$vector@NV?$allocator@N@std@@@std@@HNN@Z) referenced in function _main C:\Users\Hicha Aquino\Documents\Hicha\EFS Research Things\Programming\HeatTransfer1D\HeatTransfer1D\main.obj HeatTransfer1D
Last edited on
Could you show the updated code? The error indicates you seem to be trying to call saveArray() with only 4 parameters, but your definition has more than that.
Sure. Here they are.

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
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

void saveArray(vector<double>  &u, int nx, double dx, double dt);

void main()
{
	//Displaying welcome message
	std::cout << "1D Heat Transfer!\n";

	//Declaring variable
	int nx = 11;
	double dt = 0.001;
	double dx = 0.1;
	double r = dt / dx / dx;			//Constant of discretization
	double error = 0;


	vector<double> u(nx);
	vector<double> u_old(nx);

	//Initializing u
	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));
		}

	}

	error = 1000;
	//Executing iteration
	while (error >= 0.0001)
	{
		//Copying u to temporary array
		u_old = u;

		//Calculating new u
		for (int i = 1; i <= nx - 2; i++)
		{
			u[i] = (r*u_old[i - 1]) + ((1 - (2 * r))*u_old[i]) + (r*u_old[i + 1]);
		}

		//Calculating error

		for (int i = 0; i <= nx - 1; i++)
		{
			double de = abs(u[i] - u_old[i]);	//Absolute Error
			error = max(error, de);			//Maximum error

		}
		
	}

	//Saving result to output file
	saveArray(u, nx, dx, dt);

	//Displaying exit message
	std::cout << "Program ended correctly!\n";
	system("PAUSE");
}

void saveArray(vector<double>  &u, int nx, double dx)
{
	
	ofstream myfile;
	myfile.open("1d_convection02.dat");
	myfile << "%\t" << "nx = " << nx << endl;
	int j = 0; j++;
	for (int nt = 0; nt = nt + 1;)
	while (j <= nt - 1)
	{
		if (j%nt == 20)		//save data every 20 time step
			{
				for (int i = 0; i <= nx - 1; i++)
			{
				myfile << i*dx << "\t\t";
				myfile << u[i] << "\t\t";
				myfile << endl;
			}
			}
				else return;
			}
			myfile.close();
}
Your definition of saveArray() on line 71 takes only 3 parameters, but your call on line 64 and your prototype on line 7 give it 4 parameters.
Look at your function definition :

void saveArray(vector<double> &u, int nx, double dx, double dt);

And your function declaration :
1
2
3
4
void saveArray(vector<double> &u, int nx, double dx)
{
   //...
}


They are completely different. You forgot double dt in your function declaration.
Last edited on
The problem is that your saveArray declaration is different to your definition of it, so the linker treats them as 2 different functions. You may want to change the definition to take a 4th variable as input.
thank you so much for the answer. I've synchronized the definition and the declaration function. It seems work. But still there is no .dat file getting out.

I am sorry for asking to much.
while (error >= 0.0001)
it's stuck in this loop, because
error = max(error, de);

is always evaluating to 1000.

de is always 0. do you have your logic correct when you subract one element from the other?

edit: in fact it doesnt matter what value de takes, that loop will always be infinite.
Last edited on
Topic archived. No new replies allowed.