5
from math import fabs,sqrt
7
class epsilon(Callback):
8
def __init__(self, the_value = 1.0):
9
Callback.__init__(self)
11
def double_vec(self,r):
15
set_EPS_Callback(rods.__disown__())
17
def harmonics(freq, chi2, chi3, J):
25
s=structure(v, EPS, pml(dpml))
34
src=gaussian_src_time(freq, freq / 20)
35
f.add_point_source(Ex, src, vec(-0.5 * sz + dpml), J)
37
fpt=vec(0.5 * sz - dpml - 0.5)
38
d1 = f.add_dft_flux(Z, volume(fpt), freq, freq, 1)
39
d2 = f.add_dft_flux(Z, volume(fpt), 2*freq, 2*freq, 1)
40
d3 = f.add_dft_flux(Z, volume(fpt), 3*freq, 3*freq, 1)
44
while (f.time() < f.last_source_time()):
45
emax = max(emax, abs(f.get_field(Ex, fpt)))
52
e = abs(f.get_field(Ex, fpt))
54
emaxcur = max(emaxcur, e)
57
if (emaxcur < 1e-6 * emax):
60
#dft_flux.flux() returns a pointer !
61
# to obtain an array of fluxes use get_flux()
62
d1f = pdouble_value(d1.flux())
63
d2f = pdouble_value(d2.flux())
64
d3f = pdouble_value(d3.flux())
68
master_printf("harmonics(%g,%g,%g) = %g, %g\n", chi2, chi3, J, A2, A3)
71
def different(a, a0, thresh, msg):
72
if (fabs(a - a0) > thresh * fabs(a0)):
73
master_printf("error: %s\n --- %g vs. %g (%g error > %g)\n",msg, a, a0, fabs(a - a0)/fabs(a0), thresh)
84
a2,a3 = harmonics(freq, 0.27e-4, thresh, 1.0)
85
if (different(a2, 9.80298e-07, thresh, "2nd harmonic mismatches known value")):
87
if (different(a3, 9.97759e-07, thresh, "3rd harmonic mismatches known value")):
90
a2_2,a3_2 = harmonics(freq, 0.54e-4, 2e-4, 1.0)
91
master_printf("doubling chi2, chi3 = %g x 2nd harmonic, %g x 3rd\n", a2_2 / a2, a3_2 / a3)
92
if (different(a2_2 / a2, 4.0, 0.01, "incorrect chi2 scaling")):
94
if (different(a3_2 / a3, 4.0, 0.01, "incorrect chi3 scaling")):
97
a2_2, a3_2 = harmonics(freq, 0.27e-4, 1e-4, 2.0)
98
master_printf("doubling J = %g x 2nd harmonic, %g x 3rd\n", a2_2 / a2, a3_2 / a3)
99
if (different(a2_2 / a2, 4.0, 0.01, "incorrect J scaling for 2nd harm.")):
101
if (different(a3_2 / a3, 16.0, 0.01, "incorrect J scaling for 3rd harm.")):
104
a2_2, a3_2 = harmonics(freq, 0.27e-4, 0.0, 1.0)
105
if (different(a2, a2_2, 1e-2, "chi3 has too big effect on 2nd harmonic")):
107
if (a3_2 / a3 > 1e-4):
108
master_printf("error: too much 3rd harmonic without chi3\n");
111
a2_2, a3_2 = harmonics(freq, 0.0, 1e-4, 1.0)
112
if (different(a3, a3_2, 1e-3, "chi2 has too big effect on 3rd harmonic")):
114
if (a2_2 / a2 > 1e-5):
115
master_printf("error: too much 2nd harmonic without chi3\n");