-
Notifications
You must be signed in to change notification settings - Fork 1
/
curve.py
executable file
·175 lines (145 loc) · 5.33 KB
/
curve.py
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
#!/usr/bin/env python
from simutils import Simulation, Device
from simutils import inspector
from simutils.inspector.profiler import WaveguideProfiler
from simutils.inspector.field import WaveguideRegion
import simutils.constants as const
import matplotlib
from matplotlib import pyplot as plt
import numpy as np
from sys import exit
import json
from subprocess import run
import meep as mp
def prepare_geom(linewidth, radius, pml):
from simutils.design.shape import World, LineWaveguide, ArcWaveguide
from simutils.design.space import attach
line1, arc, line2 = attach(
LineWaveguide(linewidth, 4.5, 0, rpos=[-0.5, 0]),
ArcWaveguide(linewidth, radius, -90, 0),
LineWaveguide(linewidth, 0, 6 + pml),
)
world = World(margins=[5, 5, 5, -pml])
world['waveguide'] = line1
return world, line1, arc, line2
def plot_field(f, mode='normal', shape=None, prefix=None, regions=[],
title=None):
fig, ax = plt.subplots()
if title:
ax.set_title(title)
f.plot(ax, mode)
if shape:
shape.plot(ax)
if prefix is None:
filepath = f.filepath
for reg in regions:
reg.plot(ax)
else:
filepath = f.directory / f'{prefix}{f.name}.pdf'
fig.savefig(filepath)
print(f'Image saved to {filepath}')
plt.close()
def simulate(n_eff, width, radius, resolution, wavelength, pml):
duration = 1.5 * (4.5 + 0.5 * np.pi * radius + 5) * n_eff
# geometry and material
world, line1, arc, line2 = prepare_geom(width, radius, pml)
dev = Device(world, const.n_SiO2, n_eff)
# sources
sim = Simulation('curve', dev, res=resolution, pml=pml)
sim.create_waveguide_source(wavelength=wavelength,
pos=[0, 0],
width=8)
# regions
regions = [
WaveguideRegion(sim.name, f'region_start', line1, 0.80,
margin=1, length=1),
WaveguideRegion(sim.name, f'region_end', line2, 0.25,
margin=1, length=1),
]
# run
sim.run(duration)
return sim, regions
def inspect(sim, regions, name):
ins = inspector.Inspector(sim)
matplotlib.rc('text', usetex=False)
# save
try:
for f in 'eps ex ey sx sy pwr'.split():
np.save(f'results/curve/{name}_{f}.npy', ins[f].data)
except BaseException as e:
print(f'Saving failed: {e}')
# plot
try:
prefix = f'{name}_'
plot_field(ins['eps'], 'normal', sim.dev.shape, prefix)
for i in 'ex ey'.split():
plot_field(ins[i].abs, 'normal', sim.dev.shape, prefix)
plot_field(ins[i].real, 'symetric', sim.dev.shape, prefix)
plot_field(ins['sx'], 'symetric', sim.dev.shape, prefix)
plot_field(ins['sy'], 'symetric', sim.dev.shape, prefix)
except BaseException as e:
print(f'Plotting failed: {e}')
# losses
try:
f = ins['pwr']
reg_start, reg_end = regions
insertion_losses = reg_end.cut(f).density / reg_start.cut(f).density
plot_field(ins['pwr'], 'symetric', sim.dev.shape, prefix, regions,
title=fr'Insertion: ${insertion_losses*100:05.2f}\%$')
except BaseException as e:
print(f'Plotting pwr failed: {e}')
return insertion_losses
def compute_insertion(thickness, n_eff, width, radius, resolution,
wavelength, pml):
name = f'h{thickness:04.3f}_w{width:04.2f}_n{n_eff:04.3}_r{radius:05.2f}'
print(f'Computing insertion for {name}')
sim, regions = simulate(n_eff, width, radius, resolution, wavelength, pml)
losses = inspect(sim, regions, name)
print(f'r{name} >>> {losses*100:05.2f}%')
try:
if mp.am_master():
run(['curl', f'https://api.simplepush.io/send/mjjFz4/Simulation step/{name}'
f' {losses*100:05.2f}'])
run(['git', 'add', '.'])
run(['git', 'commit', '-m', 'sim results'])
run(['git', 'push'])
print('Pushed')
except BaseException as e:
print(f'Push failed: {e}')
return losses
if __name__ == '__main__':
param = {
'resolution': 32,
'wavelength': 1.55,
'pml': 4,
}
radii = [50, 100, 200]
# thickness n_eff width
waveguides = [
(0.200, 1.5842, 0.80),
# (0.200, 1.5842, 1.00),
(0.200, 1.5842, 1.10),
# (0.500, 1.7952, 0.50),
(0.500, 1.7952, 0.60),
(0.500, 1.7952, 0.75)]
insertions = [[compute_insertion(h, n, w, r, **param)
for r in radii]
for h, n, w in waveguides]
# save summary
waveguides = np.asarray(waveguides).T
summary = {
'radii': radii,
'waveguides_width': list(waveguides[0]),
'waveguides_thickness': list(waveguides[1]),
'waveguides_n_eff': list(waveguides[2]),
'insertions': insertions,
}
summary.update(param)
print(json.dumps(summary, indent=4, sort_keys=True))
with open('results/curve/summary.json', mode='w') as f:
json.dump(summary, f, indent=4, sort_keys=True)
try:
run(['curl', f'https://api.simplepush.io/send/mjjFz4/Simulation Finished/'
'goood'])
except BaseException as e:
pass