-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_temperature.cpp
More file actions
85 lines (72 loc) · 1.88 KB
/
Copy pathexample_temperature.cpp
File metadata and controls
85 lines (72 loc) · 1.88 KB
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: main.cpp
* Author: dang
*
* Created on September 21, 2016, 3:53 PM
*/
#include <cstdlib>
#include <stdio.h>
#include <omp.h>
#define NT 4
using namespace std;
int M = 20;
float t = 0;
float time = 1;
float D = 0.1;
float dx = 0.1;
float dt = 0.01;
float bientrai = 100;
float bienphai = 25;
float T[100];
float Tnew[100];
float Daohambac2x[100];
int i = 0;
int Mc, start, stop, id;
/*
*
*/
float daohambac2(float a, float b, float c, float deltax) {
return (a - 2 * b + c) / (deltax * deltax);
}
int main(int argc, char** argv) {
// khoi tao
for (i = 0; i < M; i++) {
T[i] = 25;
}
Mc = M / NT;
omp_set_num_threads(NT);
#pragma omp parallel private(i,id,start,stop, t)
{
id = omp_get_thread_num();
start = Mc*id;
stop = start + Mc;
// lap
while (t < time) {
// tinh dao ham bac 2 theo x
#pragma omp barrier
{
for (i = start; i < stop; i++) {
if (i == 0) {
Daohambac2x[i] = daohambac2(bientrai, T[i], T[i + 1], dx);
} else if (i == M - 1) {
Daohambac2x[i] = daohambac2(T[i - 1], T[i], bienphai, dx);
} else {
Daohambac2x[i] = daohambac2(T[i - 1], T[i], T[i + 1], dx);
}
// printf("%f; ",Daoham2x[i]);
// printf("%f \n", Tnew[i]);
}
}
#pragma omp barrier
for (i = start; i < stop; i++) T[i] = T[i] + D * Daohambac2x[i] * dt;
t = t + dt;
}
}
for (i = 0; i < M; i++) printf("%f, ", T[i]);
return 0;
}