|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +import tidy3d as td |
| 4 | +import tidy3d.plugins.invdes as tdi |
| 5 | + |
| 6 | +from .web import Job |
| 7 | + |
| 8 | +# source info |
| 9 | +wavelength = 1.0 |
| 10 | + |
| 11 | +# waveguide parameters |
| 12 | +num_output_waveguides = 3 |
| 13 | +ly_wg = 0.5 * wavelength |
| 14 | +buffer_wg = 0.9 * wavelength |
| 15 | + |
| 16 | +# buffer between design region, pml, and sources |
| 17 | +buffer = 1 * wavelength |
| 18 | + |
| 19 | +# relative permittivity of material |
| 20 | +eps_mat = 4.0 |
| 21 | + |
| 22 | +# resolution (for both the FDTD simulation and for the design region) |
| 23 | +min_steps_per_wvl = 30 |
| 24 | +pixel_size = wavelength / min_steps_per_wvl / np.sqrt(eps_mat) |
| 25 | + |
| 26 | +# spectral information |
| 27 | +freq0 = td.C_0 / wavelength |
| 28 | +fwidth = freq0 / 10 |
| 29 | +run_time = 50 / fwidth |
| 30 | + |
| 31 | +# design region size in y |
| 32 | +ly_des = num_output_waveguides * (ly_wg + buffer_wg) |
| 33 | +lx_des = 4 * wavelength |
| 34 | + |
| 35 | +# simulation size |
| 36 | +Lx = 2 * buffer + lx_des + 2 * buffer |
| 37 | +Ly = buffer + ly_des + buffer |
| 38 | + |
| 39 | +# source and monitor locations |
| 40 | +x_src = -lx_des / 2 - buffer |
| 41 | +x_mnt = -x_src |
| 42 | + |
| 43 | +# material Medium |
| 44 | +medium = td.Medium(permittivity=eps_mat) |
| 45 | + |
| 46 | +# grid spec |
| 47 | +grid_spec = td.GridSpec.auto(wavelength=wavelength, min_steps_per_wvl=min_steps_per_wvl) |
| 48 | + |
| 49 | + |
| 50 | +# monitor names |
| 51 | +def output_monitor_name(i: int) -> str: |
| 52 | + return f"MNT_{i}" |
| 53 | + |
| 54 | + |
| 55 | +field_mnt_name = "field" |
| 56 | + |
| 57 | +# mode spec |
| 58 | +mode_spec = td.ModeSpec(num_modes=1) |
| 59 | + |
| 60 | + |
| 61 | +if __name__ == "__main__": |
| 62 | + # radius (um) of the conic filter that is convolved with th parameter array. |
| 63 | + # Larger values tend to help create larger feature sizes in the final device. |
| 64 | + projection_radius = 0.120 |
| 65 | + |
| 66 | + # projection strength, larger values lead to more binarization and tend to push intermediate parameters towards (0,1) density |
| 67 | + beta = 10.0 |
| 68 | + |
| 69 | + # transformations on the parameters that lead to the material density array (0,1) |
| 70 | + filter_project = tdi.FilterProject(radius=projection_radius, beta=beta) |
| 71 | + |
| 72 | + # length scale (um) of the erosion dilation penalty. |
| 73 | + # features smaller than this scale will be penalized |
| 74 | + length_scale = 0.120 |
| 75 | + |
| 76 | + # penalty weight, the penalty contributes its raw value (max of 1) times this weight to the objective function |
| 77 | + weight = 0.8 |
| 78 | + |
| 79 | + # penalties applied to the state of the material density, after these transformations are applied |
| 80 | + penalty = tdi.ErosionDilationPenalty(weight=weight, length_scale=length_scale) |
| 81 | + |
| 82 | + design_region = tdi.TopologyDesignRegion( |
| 83 | + size=(lx_des, ly_des, td.inf), |
| 84 | + center=(0, 0, 0), |
| 85 | + eps_bounds=(1.0, eps_mat), # the minimum and maximum permittivity values in the final grid |
| 86 | + transformations=[filter_project], |
| 87 | + penalties=[penalty], |
| 88 | + pixel_size=pixel_size, |
| 89 | + ) |
| 90 | + |
| 91 | + waveguide_in = td.Structure( |
| 92 | + geometry=td.Box( |
| 93 | + size=(Lx, ly_wg, td.inf), |
| 94 | + center=(-Lx + 2 * buffer, 0, 0), |
| 95 | + ), |
| 96 | + medium=medium, |
| 97 | + ) |
| 98 | + |
| 99 | + y_max_wg_centers = ly_des / 2 - buffer_wg / 2 - ly_wg / 2 |
| 100 | + wg_y_centers_out = np.linspace(-y_max_wg_centers, y_max_wg_centers, num_output_waveguides) |
| 101 | + |
| 102 | + # put a waveguide and mode monitor at each of the outputs |
| 103 | + waveguides_out = [] |
| 104 | + monitors_out = [] |
| 105 | + for i, wg_y_center in enumerate(wg_y_centers_out): |
| 106 | + wg_out = td.Structure( |
| 107 | + geometry=td.Box( |
| 108 | + size=(Lx, ly_wg, td.inf), |
| 109 | + center=(Lx - 2 * buffer, wg_y_center, 0), |
| 110 | + ), |
| 111 | + medium=medium, |
| 112 | + ) |
| 113 | + |
| 114 | + waveguides_out.append(wg_out) |
| 115 | + |
| 116 | + mnt_out = td.ModeMonitor( |
| 117 | + size=(0, ly_wg + 1.8 * buffer_wg, td.inf), |
| 118 | + center=(x_mnt, wg_y_center, 0), |
| 119 | + freqs=[freq0], |
| 120 | + name=output_monitor_name(i), |
| 121 | + mode_spec=mode_spec, |
| 122 | + ) |
| 123 | + |
| 124 | + monitors_out.append(mnt_out) |
| 125 | + |
| 126 | + source = td.ModeSource( |
| 127 | + size=(0, ly_wg + 1.8 * buffer_wg, td.inf), |
| 128 | + center=(x_src, 0, 0), |
| 129 | + source_time=td.GaussianPulse(freq0=freq0, fwidth=fwidth), |
| 130 | + mode_index=0, |
| 131 | + direction="+", |
| 132 | + ) |
| 133 | + |
| 134 | + # used to visualize fields in the plane, not for optimization |
| 135 | + fld_mnt = td.FieldMonitor( |
| 136 | + center=(0, 0, 0), |
| 137 | + size=(td.inf, td.inf, 0), |
| 138 | + freqs=[freq0], |
| 139 | + name=field_mnt_name, |
| 140 | + ) |
| 141 | + |
| 142 | + simulation = td.Simulation( |
| 143 | + size=(Lx, Ly, 0), |
| 144 | + grid_spec=grid_spec, |
| 145 | + boundary_spec=td.BoundarySpec.pml(x=True, y=True, z=False), |
| 146 | + run_time=run_time, |
| 147 | + structures=[waveguide_in] + waveguides_out, |
| 148 | + sources=[source], |
| 149 | + monitors=[fld_mnt] + monitors_out, |
| 150 | + ) |
| 151 | + |
| 152 | + design = tdi.InverseDesign( |
| 153 | + simulation=simulation, |
| 154 | + design_region=design_region, |
| 155 | + task_name="invdes", |
| 156 | + output_monitor_names=[mnt.name for mnt in monitors_out], |
| 157 | + ) |
| 158 | + |
| 159 | + optimizer = tdi.AdamOptimizer( |
| 160 | + design=design, |
| 161 | + num_steps=10, |
| 162 | + learning_rate=0.1, |
| 163 | + results_cache_fname="data/invdes_history.hdf5", |
| 164 | + ) |
| 165 | + |
| 166 | + optimizer.to_hdf5("data/invdes_history.hdf5") |
| 167 | + job = Job(optimizer=optimizer) |
0 commit comments