-
Notifications
You must be signed in to change notification settings - Fork 3
/
assignment.c
201 lines (188 loc) · 4.87 KB
/
assignment.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "excitation.h"
#include "assignment.h"
#include "fdtd2d.h"
#include <cairo.h>
#include <stdlib.h>
#include <stdio.h>
long iterate_section_3(fdtd_2d_t *fdtd)
{
FDTD_2D_BEGIN(fdtd);
static long n = 0;
long i = 0, j = 0;
/* Excitation on the surface i = 0 */
for (j = 0; j < J; ++j) {
E_Y(0, j) = fdtd_excitation(n, N_P) * 377.0;
}
/* Regular solution for 0<i<I */
for (i = 1; i < I; ++i) {
for (j = 0; j < J; ++j) {
E_Y(i, j) += -alpha_Ey * (H_Z(i, j) - H_Z(i-1, j));
}
}
/* PEC at the surface i = I */
for (j = 0; j < J; ++j) {
E_Y(I, j) = 0;
}
/* Ex and Hz are calculated regularly */
FDTD_2D_iterate_Ex(fdtd);
FDTD_2D_iterate_Hz(fdtd);
++n;
return n;
}
long iterate_section_4(fdtd_2d_t *fdtd)
{
FDTD_2D_BEGIN(fdtd);
static long n = 0;
long i = 0, j = 0;
if (0 == n) {
copy_simulation_up_to(fdtd, 2*N_P);
} else {
FDTD_2D_iterate_Hz(fdtd);
FDTD_2D_iterate_Ex(fdtd);
FDTD_2D_iterate_Ey(fdtd);
}
++n;
return n;
}
long iterate_section_5(fdtd_2d_t *fdtd)
{
FDTD_2D_BEGIN(fdtd);
static long n = 0;
long i = 0, j = 0;
long S = (long)round(6 * N_P * fdtd->gamma_x);
if (0 == n) {
copy_simulation_up_to(fdtd, 2*N_P);
} else {
FDTD_2D_iterate_Hz(fdtd);
FDTD_2D_iterate_Ex(fdtd);
FDTD_2D_iterate_Ey(fdtd);
place_PEC_obstacle(fdtd, S, J/2, J/2);
}
++n;
return n;
}
long iterate_section_6(fdtd_2d_t *fdtd)
{
FDTD_2D_BEGIN(fdtd);
static long n = 0;
long i = 0, j = 0;
/* Excitation on the surface i = 0 */
for (j = 0; j < J; ++j) {
E_Y(0, j) = fdtd_excitation2(n, N_P) * 377.0;
}
/* Regular solution for 0<i<I */
for (i = 1; i < I; ++i) {
for (j = 0; j < J; ++j) {
E_Y(i, j) += -alpha_Ey * (H_Z(i, j) - H_Z(i-1, j));
}
}
/* PEC at the surface i = I */
for (j = 0; j < J; ++j) {
E_Y(I, j) = 0;
}
/* Ex and Hz are calculated regularly */
FDTD_2D_iterate_Ex(fdtd);
FDTD_2D_iterate_Hz(fdtd);
++n;
return n;
}
long iterate_section_7(fdtd_2d_t *fdtd)
{
FDTD_2D_BEGIN(fdtd);
static long n = 0;
long i = 0, j = 0;
if (0 == n) {
copy_simulation_up_to(fdtd, 2*N_P);
} else {
FDTD_2D_iterate_Hz(fdtd);
FDTD_2D_iterate_Ex(fdtd);
FDTD_2D_iterate_Ey_mur(fdtd);
}
++n;
return n;
}
long iterate_section_8(fdtd_2d_t *fdtd)
{
FDTD_2D_BEGIN(fdtd);
static long n = 0;
long i = 0, j = 0;
long S = (long)round(6 * N_P * fdtd->gamma_x);
if (0 == n) {
copy_simulation_up_to(fdtd, 2*N_P);
} else {
FDTD_2D_iterate_Hz(fdtd);
FDTD_2D_iterate_Ex(fdtd);
FDTD_2D_iterate_Ey_mur(fdtd);
place_PEC_obstacle(fdtd, S, J/2, J/2);
}
++n;
return n;
}
long iterate_section_9(fdtd_2d_t *fdtd)
{
FDTD_2D_BEGIN(fdtd);
static long n = 0;
long i = 0, j = 0;
long S = (long)round(6 * N_P * fdtd->gamma_x);
if (0 == n) {
copy_simulation_up_to(fdtd, 2*N_P);
} else {
FDTD_2D_iterate_Hz_PML(fdtd, 20, 1);
FDTD_2D_iterate_Ex_PML(fdtd, 20, 1);
FDTD_2D_iterate_Ey_PML(fdtd, 20, 1);
place_PEC_obstacle(fdtd, S, J/2, J/2);
}
++n;
return n;
}
/* Render functions */
void render_regular(cairo_t *c, fdtd_2d_t *fdtd)
{
FDTD_2D_render_Hz(c, fdtd);
}
void render_with_obstacle(cairo_t *c, fdtd_2d_t *fdtd)
{
FDTD_2D_BEGIN(fdtd);
long S = (long)round(6 * N_P * fdtd->gamma_x);
FDTD_2D_render_Hz(c, fdtd);
cairo_set_source_rgb(c, 0.0f, 0.0f, 0.0f);
cairo_rectangle(c, S-J/4-0.5f, J/4+0.5f, J/2, J/2);
cairo_fill(c);
}
/* Helper functions */
void copy_simulation_up_to(fdtd_2d_t *fdtd, long T)
{
FDTD_2D_BEGIN(fdtd);
fdtd_2d_t tmp_fdtd;
long m = 0;
long i = 0, j = 0;
/* Allow the simulation in section 3 for gamma=1 to run until 2*N_P */
FDTD_2D_init(&tmp_fdtd, 1.0f);
while (m < T) {
m = iterate_section_3(&tmp_fdtd);
}
/* Now copy Hz of the temporary simulation to the active simulation.
* We need to copy 2*N_P*gamma=2*N_P cells in x direction */
for (i = 0; i < T; ++i) {
for (j = 0; j < J; ++j) {
H_Z(i, j) = tmp_fdtd.Hz[i];
E_X(i, j) = tmp_fdtd.Ex[i];
E_Y(i, j) = tmp_fdtd.Ey[i];
}
}
/* we can free the memory the temporary simulation occupied */
FDTD_2D_free(&tmp_fdtd);
}
void place_PEC_obstacle(fdtd_2d_t *fdtd, long center_x, long center_y, long side)
{
long i = 0, j = 0;
FDTD_2D_BEGIN(fdtd);
for (i = (center_x - side/2); i < (center_x + side/2); ++i) {
E_X(i, center_y - side/2) = 0;
E_X(i, center_y + side/2) = 0;
}
for (j = (center_y - side/2); j < (center_y + side/2); ++j) {
E_Y(center_x - side/2, j) = 0;
E_Y(center_x + side/2, j) = 0;
}
}