-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
executable file
·94 lines (73 loc) · 2.28 KB
/
test.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
#!/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, length, pml):
from simutils.design.shape import LineWaveguide, World
a = 1
l = length + pml + a
wg = LineWaveguide(linewidth, 0, 2*l, rpos=[0, -l])
world = World(margins=[5, 5, -pml, -pml])
world['wg'] = wg
return world, wg
def plot_field(f, mode='normal', shape=None, prefix='', 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(wavelength, linewidth, n_eff, length, resolution, pml):
duration = 1.5 * length * n_eff
# geometry and material
world, wg = prepare_geom(linewidth, length, pml)
dev = Device(world, const.n_SiO2, n_eff)
# sources
sim = Simulation('test', dev, res=resolution, pml=pml)
sim.create_waveguide_source(wavelength=wavelength,
pos=[0, 0],
width=8 * linewidth,
direction='+x',
comp='y')
# run
sim.run(duration)
return sim
def inspect(sim):
ins = inspector.Inspector(sim)
matplotlib.rc('text', usetex=False)
# plot
try:
plot_field(ins['eps'], 'normal', sim.dev.shape)
for i in 'ex ey ez'.split():
plot_field(ins[i].abs, 'normal', sim.dev.shape)
except BaseException as e:
print(f'Plotting failed: {e}')
if __name__ == '__main__':
wavelength = 1.55
linewidth = 1
n_eff = 1.5842
length = 3
resolution = 16
pml = 4
sim = simulate(wavelength, linewidth, n_eff, length,
resolution, pml)
inspect(sim)