-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvection_diffusion.c
168 lines (137 loc) · 3.83 KB
/
advection_diffusion.c
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "advection_diffusion.h"
#include <omp.h>
#include <math.h>
void
apply_thermal_boundary_conditions(double *t)
{
int i, j;
#pragma omp parallel for
for ( j = 0; j < NY; j++ ){
// Temp left wall, "freeslip" (temp doesn't escape)
t[NX*j + 0] = t[NX*j + 1];
// Temp right wall, "freeslip" (temp doesn't escape)
t[NX*j + (NX-1)] = t[NX*j + (NX-2)];
}
/*
#pragma omp parallel for
for ( j = 0; j < NY; j++ ){
// periodic BCs
t[NX*j + 0] = t[NX*j + (NX-1)];
}
*/
#pragma omp parallel for
for ( i = 0; i < NX; i++ ){
// Temp bottom wall,
t[0 + i] = 1000.;
// Temp bottom wall,
t[NX*(NY-1) + i] = 0.;
}
}
void
solve_advection_diffusion(double *t,
double *u,
double *v,
double dx,
double dy,
double *rho,
double dt,
double cp,
double *k,
double H)
{
int i,j;
double tn[NY*NX];
double kx;
double ky;
// Pre-compute some basics
double dx2 = dx * dx;
double dy2 = dy * dy;
double twodx = 2. * dx;
double twody = 2. * dy;
#pragma omp parallel for
for ( j = 0; j < NY; j++ ){
for ( i = 0; i < NX; i++ ){
tn[NX*j + i] = t[NX*j + i];
}
}
unsigned int n = 0;
unsigned int s = 0;
unsigned int m = 0;
unsigned int e = 0;
unsigned int w = 0;
#pragma omp parallel for
for ( j = 1; j < NY-1; j++ ){
#pragma omp simd safelen(3)
for ( i = 1; i < NX-1; i++){
m = NX*j + i;
n = NX*(j-1) + i;
s = NX*(j+1) + i;
e = NX*j + (i+1);
w = NX*j + (i-1);
kx = k[m] * (tn[e] - 2.*tn[m] + tn[w]) / dx2;
ky = k[m] * (tn[s] - 2.*tn[m] + tn[n]) / dy2;
t[m] = tn[m] + dt * ((H + kx + ky)/(rho[m] * cp) \
- (u[m] * ( (tn[e] - tn[w]) / twodx )) \
- (v[m] * ( (tn[s] - tn[n]) / twody )) );
}
}
apply_thermal_boundary_conditions(t);
}
void
update_nu(double *nu,
double *t)
{
/* Calculate temperature dependent viscosity. Based on
* Frank-Kamenetski formulation.
* Viscosity gets lower with increase in temperature
*/
int i, j;
double ref_nu = 1.;
double ref_temp = 500.;
double theta = 1.5;
#pragma omp parallel for
for ( j = 0; j < NY; j++ ){
for ( i = 0; i < NX; i++ ){
nu[NX*j + i] = ref_nu * exp(-theta * ((t[NX*j + i] - ref_temp)/ref_temp));
}
}
}
void
update_rho(double *rho,
double *t)
{
/* Calculate density change due to thermal expansion.
* Using boissinesq approximation (rho change does not
* effect velocity field). Change is linear.
*/
int i, j;
double ref_rho = 100.;
double ref_temp = 500.;
double thermal_expansivity = 0.001;
#pragma omp parallel for
for ( j = 0; j < NY; j++ ){
for ( i = 0; i < NX; i++ ){
rho[NX*j + i] = ref_rho * (1. - (thermal_expansivity * (t[NX*j + i] - ref_temp)));
}
}
}
void
update_k(double *k,
double *t)
{
/* Calculate temperature dependent conductivity.
* Using linear relationship where higher temp gives
* lower conductivity. This means slabs (cold drips)
* will suck up heat faster. It's also relatively natural.
*/
int i, j;
double ref_k = 100.;
double ref_temp = 500.;
double thermal_factor = 0.001;
#pragma omp parallel for
for ( j = 0; j < NY; j++ ){
for ( i = 0; i < NX; i++ ){
k[NX*j + i] = ref_k * (1. - (thermal_factor * (t[NX*j + i] - ref_temp)));
}
}
}