-
Notifications
You must be signed in to change notification settings - Fork 8
/
fdtd.cuh
126 lines (107 loc) · 2.75 KB
/
fdtd.cuh
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
#ifndef FDTD_CUH_
#define FDTD_CUH_
#include <algorithm>
constexpr float C0 = 299792458.0f; /// Speed of light [metres per second]
float calculate_dt (float dx, float dy)
{
const float cfl = 0.3;
return cfl * std::min (dx, dy) / C0;
}
/**
* Calculate curl of Ex with periodic boundary condition
* @param i Column index
* @param j Row index
*/
__device__ float update_curl_ex (
int nx,
int cell_x,
int cell_y,
int cell_id,
float dy,
const float * ez)
{
const int top_neighbor_id = nx * (cell_y + 1) + cell_x;
return (ez[top_neighbor_id] - ez[cell_id]) / dy;
}
/**
* @param i Column index
* @param j Row index
*/
__device__ float update_curl_ey (
int nx,
int cell_x,
int cell_y,
int cell_id,
float dx,
const float * ez)
{
const int right_neighbor_id = cell_x == nx - 1 ? cell_y * nx + 0 : cell_id + 1;
return -(ez[right_neighbor_id] - ez[cell_id]) / dx;
}
__device__ void update_h (
int nx,
int cell_id,
float dx,
float dy,
const float * __restrict__ ez,
const float * __restrict__ mh,
float * __restrict__ hx,
float * __restrict__ hy)
{
const int cell_x = cell_id % nx;
const int cell_y = cell_id / nx;
const float cex = update_curl_ex (nx, cell_x, cell_y, cell_id, dy, ez);
const float cey = update_curl_ey (nx, cell_x, cell_y, cell_id, dx, ez);
// update_h
hx[cell_id] -= mh[cell_id] * cex;
hy[cell_id] -= mh[cell_id] * cey;
}
__device__ static float update_curl_h (
int nx,
int cell_id,
int cell_x,
int cell_y,
float dx,
float dy,
const float * __restrict__ hx,
const float * __restrict__ hy)
{
const int left_neighbor_id = cell_x == 0 ? cell_y * nx + nx - 1 : cell_id - 1;
const int bottom_neighbor_id = nx * (cell_y - 1) + cell_x;
return (hy[cell_id] - hy[left_neighbor_id]) / dx
- (hx[cell_id] - hx[bottom_neighbor_id]) / dy;
}
__device__ float gaussian_pulse (float t, float t_0, float tau)
{
return __expf (-(((t - t_0) / tau) * (t - t_0) / tau));
}
__device__ float calculate_source (float t, float frequency)
{
const float tau = 0.5f / frequency;
const float t_0 = 6.0f * tau;
return gaussian_pulse (t, t_0, tau);
}
__device__ void update_e (
int nx,
int cell_id,
int own_in_process_begin,
int source_position,
float t,
float dx,
float dy,
float C0_p_dt,
float * __restrict__ ez,
float * __restrict__ dz,
const float * __restrict__ er,
const float * __restrict__ hx,
const float * __restrict__ hy)
{
const int cell_x = cell_id % nx;
const int cell_y = cell_id / nx;
const float chz = update_curl_h (nx, cell_id, cell_x, cell_y, dx, dy, hx, hy);
dz[cell_id] += C0_p_dt * chz;
if ((own_in_process_begin + cell_y) * nx + cell_x == source_position)
dz[cell_id] += calculate_source (t, 5E+7);
ez[cell_id] = dz[cell_id] / er[cell_id];
}
#endif