# Angular frequency of the source in Hz
= 2 * np.pi * 200e12
omega # Spatial resolution in meters
= 40e-9
dl # Number of pixels in x-direction
= 100
Nx # Number of pixels in y-direction
= 100
Ny # Number of pixels in the PMLs in each direction
= 20
Npml # Initial value of the structure's relative permittivity
= 12.0
epsr_init # Space between the PMLs and the design region (in pixels)
= 10
space # Width of the waveguide (in pixels)
= 12
wg_width # Length in pixels of the source/probe slices on each side of the center point
= 8
space_slice # Number of epochs in the optimization
= 100
Nsteps # Step size for the Adam optimizer
= 1e-2 step_size
Inverse Design
This notebook was adapted from Ceviche’s inverse design introduction to use a JAX-based optimization loop in stead of the default Ceviche optimization loop.
Parameters
Our toy optimization problem will be to design a device that converts an input in the first-order mode into an output as the second-order mode. First, we define the parameters of our device and optimization:
Brush
= notched_square_brush(5, 1)
brush show_mask(brush)
No GPU/TPU found, falling back to CPU. (Set TF_CPP_MIN_LOG_LEVEL=0 and rerun for more info.)
Initial Device
# Initialize the parametrization rho and the design region
= init_domain(
epsr, bg_epsr, design_region, input_slice, output_slice =space, wg_width=wg_width, space_slice=space_slice
Nx, Ny, Npml, space
)
= mask_combine_epsr(epsr, bg_epsr, design_region)
epsr_total
# Setup source
= insert_mode(omega, dl, input_slice.x, input_slice.y, epsr_total, m=1)
source
# Setup probe
= insert_mode(omega, dl, output_slice.x, output_slice.y, epsr_total, m=2) probe
# Simulate initial device
= viz_sim(epsr_total, source, slices=[input_slice, output_slice])
simulation, ax
# get normalization factor (field overlap before optimizing)
= simulation.solve(source)
_, _, Ez = mode_overlap(Ez, probe) E0
get_design_region
get_design_region (epsr, design_region=array([[0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.]]))
set_design_region
set_design_region (epsr, value, design_region=array([[0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.]]))
Latent Weights
#latent = get_design_region(new_latent_design((Nx, Ny), r=0))
= new_latent_design((Nx, Ny), r=0)
latent = transform(latent, brush)
latent_t ="Greys", vmin=-1, vmax=1)
plt.imshow(get_design_region(latent_t), cmap
plt.colorbar() plt.show()
Forward Pass
= generate_feasible_design(latent_t, brush, verbose=False) design
= generate_feasible_design_mask(latent_t, brush) mask
= np.zeros_like(epsr, dtype=bool)
full_mask #full_mask = set_design_region(full_mask, mask)
="Greys")
plt.imshow(mask, cmap
plt.colorbar()
plt.show()
="Greys")
plt.imshow(full_mask, cmap
plt.colorbar() plt.show()
CPU times: user 159 ms, sys: 12.9 ms, total: 172 ms
Wall time: 189 ms
def forward(latent_weights, brush):
= transform(latent_weights, brush)
latent_t = generate_feasible_design_mask(latent_t, brush)
design_mask = np.where(design_mask, 12.0, 1.0) epsr
forward
forward (latent_weights, brush)
def loss_fn(epsr):
= epsr.reshape((Nx, Ny))
epsr = mask_combine_epsr(epsr, bg_epsr, design_region)
simulation.eps_r = simulation.solve(source)
_, _, Ez return -mode_overlap(Ez, probe) / E0
loss_fn
loss_fn (epsr)
= jacobian(loss_fn, mode='reverse') grad_fn
Optimization
# Simulate initial device
= viz_sim(epsr_total, source, slices=[input_slice, output_slice]) simulation, ax
= adam(step_size)
init_fn, update_fn, params_fn = init_fn(epsr.reshape(1, -1)) state
this is the optimization step:
step_fn
step_fn (step, state)
we can now loop over the optimization:
= trange(500)
range_ for step in range_:
= step_fn(step, state)
loss, state =float(loss)) range_.set_postfix(loss
= params_fn(state)
epsr_optimum = epsr_optimum.reshape((Nx, Ny)) epsr_optimum
# Simulate and show the optimal device
= mask_combine_epsr(epsr_optimum, bg_epsr, design_region)
epsr_optimum_total = viz_sim(epsr_optimum_total, source, slices=[input_slice, output_slice]) simulation, ax
At the end of the optimization we can see our final device. From the field pattern, we can easily observe that the device is doing what we intend: the even mode enters from the left and exits as the odd mode on the right.
However, an additional observation is that our device’s permittivity changes continuously. This is not ideal if we wanted to fabricated our device. We’re also not constraining the minimum and maximum values of \(\epsilon_r\). Thus, we need to consider alternative ways of parameterizing our device.
="plasma", vmin=1, vmax=4)
plt.imshow(np.sqrt(epsr_optimum_total.T), cmap*plt.ylim()[::-1])
plt.ylim(=[1,2,3,4], label="n")
plt.colorbar(ticks"x")
plt.xlabel("y")
plt.xlabel(True) plt.grid(