forked from NanoComp/meep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bragg_transmission.cpp
251 lines (212 loc) · 7.74 KB
/
bragg_transmission.cpp
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* Copyright (C) 2005-2023 Massachusetts Institute of Technology
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* Compute the transmission spectrum through a 4-layer 1d Bragg mirror,
and compare to the result from the analytical transfer matrices.
The transmission spectrum is computed via the dft_flux feature,
which dynamically updates the DFTs of the fields on the flux plane
as we go along. */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <meep.hpp>
using namespace meep;
using std::complex;
const double nhi = 3, nlo = 1;
const double wlo = nhi / (nlo + nhi);
const int Nperiods = 4;
const double zsize = 10;
double eps_nlo(const vec &) { return nlo * nlo; }
double eps_bragg(const vec &pt) {
double z = pt.z() - zsize * 0.5;
if (fabs(z) * 2 > Nperiods)
return nlo * nlo;
else {
double zi;
double zf = modf(z, &zi);
if (zf < 0) zf += 1;
if (zf < wlo)
return (nlo * nlo);
else
return (nhi * nhi);
}
}
typedef complex<double> matrix2x2[2][2];
/* multiply m by transfer matrix from n1 to n2 */
inline void byT12(matrix2x2 m, double n1, double n2) {
complex<double> m00, m01, m10, m11;
double td, tod;
double n12 = n1 / n2;
td = 0.5 * (1 + n12);
tod = 0.5 * (1 - n12);
m00 = m[0][0];
m01 = m[0][1];
m10 = m[1][0];
m11 = m[1][1];
m[0][0] = m00 * td + m01 * tod;
m[0][1] = m00 * tod + m01 * td;
m[1][0] = m10 * td + m11 * tod;
m[1][1] = m10 * tod + m11 * td;
}
/* multiply m by propagation matrix through dz of index n, frequency w */
inline void byP(matrix2x2 m, double n, double w, double dz) {
complex<double> p, pc;
p = exp(complex<double>(0, n * w * dz));
pc = conj(p);
m[0][0] *= p;
m[0][1] *= pc;
m[1][0] *= p;
m[1][1] *= pc;
}
inline double abs2(complex<double> x) {
double ax = abs(x);
return ax * ax;
}
void bragg_transmission_analytic(double freq_min, double freq_max, int nfreq, double *T,
double *R) {
for (int i = 0; i < nfreq; ++i) {
double omega = 2 * pi * (freq_min + i * (freq_max - freq_min) / (nfreq - 1));
matrix2x2 Tm = {{1, 0}, {0, 1}};
for (int j = 0; j < Nperiods; ++j) {
byT12(Tm, nlo, nhi);
byP(Tm, nhi, omega, 1 - wlo);
byT12(Tm, nhi, nlo);
byP(Tm, nlo, omega, wlo);
}
complex<double> refl = -Tm[1][0] / Tm[1][1];
T[i] = abs2(Tm[0][0] + refl * Tm[0][1]);
R[i] = abs2(refl);
}
}
void bragg_transmission(double a, double freq_min, double freq_max, int nfreq, double *T, double *R,
bool use_hdf5) {
const grid_volume gv = volone(zsize, a);
structure *s = new structure(gv, eps_bragg, pml(0.5));
fields f(s);
f.use_real_fields();
structure s0(gv, eps_nlo, pml(0.5));
fields f0(&s0);
f0.use_real_fields();
vec srcpt(0.1), Tfluxpt(zsize - 0.1), Rfluxpt(0.1);
gaussian_src_time src((freq_min + freq_max) * 0.5, 0.5 / fabs(freq_max - freq_min), 0,
5 / fabs(freq_max - freq_min));
f.add_point_source(Ex, src, srcpt);
f0.add_point_source(Ex, src, srcpt);
dft_flux ft = f.add_dft_flux_plane(Tfluxpt, freq_min, freq_max, nfreq);
dft_flux fr = f.add_dft_flux_plane(Rfluxpt, freq_min, freq_max, nfreq);
dft_flux ft0 = f0.add_dft_flux_plane(Tfluxpt, freq_min, freq_max, nfreq);
dft_flux fr0 = f0.add_dft_flux_plane(Rfluxpt, freq_min, freq_max, nfreq);
while (f0.time() < nfreq / fabs(freq_max - freq_min) / 2)
f0.step();
/* we want to subtract the fields for the reflection... */
if (use_hdf5) {
/* simulate a case where the normalization is done
by a separate run and saved to a file */
fr0.save_hdf5(f, "flux", "reflection");
fr.load_hdf5(f, "flux", "reflection");
fr.scale_dfts(-1.0);
// clean up after ourselves: delete the file
h5file *ff = f.open_h5file("flux", h5file::READONLY);
ff->remove();
delete ff;
}
else
fr -= fr0;
while (f.time() < nfreq / fabs(freq_max - freq_min) / 2)
f.step();
double *flux = ft.flux();
double *flux0 = ft0.flux();
for (int i = 0; i < nfreq; ++i)
T[i] = flux[i] / flux0[i];
delete[] flux;
flux = fr.flux();
for (int i = 0; i < nfreq; ++i)
R[i] = -flux[i] / flux0[i];
delete[] flux;
delete[] flux0;
delete s; // tests whether okay to delete s before f
}
inline double max2(double a, double b) { return (a > b ? a : b); }
inline double min2(double a, double b) { return (a < b ? a : b); }
inline double max2a(double a, double b) { return max2(abs(a), abs(b)); }
inline double sqr(double x) { return x * x; }
/* The discretization errors tend to result in a *shift* of the spectral
features more than a change in their amplitude. Because these features
are very sharp (e.g. at the gap edges), it is more appropriate to compute
errors via the distance from a point to the curve, rather than just
the difference of the abscissae. That's what this function does. */
double distance_from_curve(int n, double dx, double ys[], double x, double y) {
double d = meep::infinity;
for (int i = 1; i < n; ++i) {
double theta = atan2(ys[i] - ys[i - 1], dx);
double L = sqrt(sqr(dx) + sqr(ys[i] - ys[i - 1]));
double x0 = x - (i - 1) * dx;
double y0 = y - ys[i - 1];
double x0p = x0 * cos(theta) + y0 * sin(theta);
double y0p = y0 * cos(theta) - x0 * sin(theta);
if (x0p < 0)
d = min2(sqrt(sqr(x0) + sqr(y0)), d);
else if (x0p > L)
d = min2(sqrt(sqr(x - i * dx) + sqr(y - ys[i])), d);
else
d = min2(abs(y0p), d);
}
return d;
}
void doit(bool use_hdf5) {
const int nfreq = 100;
const double freq_min = 0.1, freq_max = 0.5;
double *T = new double[nfreq];
double *R = new double[nfreq];
bragg_transmission(40.0, freq_min, freq_max, nfreq, T, R, use_hdf5);
double *T0 = new double[nfreq];
double *R0 = new double[nfreq];
bragg_transmission_analytic(freq_min, freq_max, nfreq, T0, R0);
double dfreq = (freq_max - freq_min) / (nfreq - 1);
if (0) { // output transmission & reflection spectra for debugging
master_printf("transmission:, freq (c/a), T, R, T0, R0\n");
for (int i = 0; i < nfreq; ++i)
master_printf("transmission:, %g, %g, %g, %g, %g\n", freq_min + i * dfreq, T[i], R[i], T0[i],
R0[i]);
}
double maxerrT = 0, maxerrR = 0;
for (int i = 0; i < nfreq; ++i) {
double errT = distance_from_curve(nfreq, dfreq, T0, i * dfreq, T[i]);
double errR = distance_from_curve(nfreq, dfreq, R0, i * dfreq, R[i]);
if (errT > maxerrT) maxerrT = errT;
if (errR > maxerrR) maxerrR = errR;
if (errT * sqr(freq_min / (freq_min + i * dfreq)) > 0.01)
meep::abort("large error %g at freq = %g: T = %g instead of %g\n", errT, freq_min + i * dfreq,
T[i], T0[i]);
if (errR * sqr(freq_min / (freq_min + i * dfreq)) > 0.01)
meep::abort("large error %g at freq = %g: R = %g instead of %g\n", errR, freq_min + i * dfreq,
R[i], R0[i]);
}
master_printf("Done (max. err in T = %e, in R = %e)\n", maxerrT, maxerrR);
delete[] R0;
delete[] T0;
delete[] R;
delete[] T;
}
int main(int argc, char **argv) {
initialize mpi(argc, argv);
verbosity = 0;
#ifdef HAVE_HDF5
doit(true);
#endif
doit(false);
return 0;
}