Using ModeSimulation for optical mode analysis
Using ModeSimulation for optical mode analysis
This tutorial shows how to use ModeSimulation in Tidy3D.
For a tutorial on the legacy ModeSolver plugin, please refer to this notebook.
Setup
We first set up the mode simulation with information about our system. We start by setting the parameters.
We then define a Simulation, in this case including a straight waveguide and periodic boundary conditions. Note that Tidy3D warns us that we have not added any sources to the Simulation object. However, for mode solving, this is not necessary.
Visualizing Mode Data
The mode_info object contains information about the effective index of the mode and the field profiles, as well as the mode_spec used in the solver. The effective index data and the field profile data are in the form of xarray DataArrays.
We can, for example, plot the real part of the effective index for all three modes as follows.
The raw data can also be accessed.
first mode effective index at freq0: n_eff = 1.75, k_eff = 0.00e+00
The fields stored in mode_data can be visualized using built-in xarray methods.
Alternatively, we can use the built-in plot_field method of sim_data, which also allows us to overlay the structures in the simulation. The image also looks slightly different because the plot_field method uses the robust=True option by default, which scales the colorbar between the 2nd and 98th percentiles of the data.
Choosing the mode of interest
We can also look at the other modes that were computed.
This looks like an Ez-dominant mode. Finally, the next-order mode has mixed polarization.
Exporting Results
This looks promising!
Now we can choose the mode specifications to use in our mode source and mode monitors. These can be created separately, or they can be exported directly from the mode simulation, for example:
10:37:59 UTC Created task 'mode_simulation' with resource_id
'fdve-1d3e2e4d-b94b-427d-9def-c66f48d01082' and task_type 'FDTD'.
10:38:00 UTC Estimated FlexCredit cost: 0.025. This assumes the FDTD solver runs
for the full simulation time; if early shutoff is reached, the
billed cost can be lower. Use 'web.real_cost(task_id)' to get the
billed FlexCredit cost after a simulation run.
10:38:01 UTC status = success
10:38:03 UTC Loading results from data/simulation_data.hdf5
We can now plot the in-plane field and the modal amplitudes. Since we injected mode 2 and we have just a straight waveguide, all the power recorded by the modal monitor is in mode 2, going backwards.
Storing server-side computed modes
We can also use a `ModeSolverMonitor <https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.ModeSolverMonitor.html>`__ to store the modes as they are computed on the server. This is illustrated below. We will also request in the mode specification that the modes are sorted by their tm polarization. In this particular simulation, TM refers to Ez polarization. The effect of the sorting is that modes with a tm polarization fraction greater than or equal to
0.5 come first in the list of modes, while still being ordered by decreasing effective index. After that, the predominantly te-polarized modes follow.
10:38:04 UTC Created task 'mode_simulation' with resource_id
'fdve-dfc79572-4fa4-4a6c-9385-7080664d7b11' and task_type 'FDTD'.
10:38:05 UTC Estimated FlexCredit cost: 0.025. This assumes the FDTD solver runs
for the full simulation time; if early shutoff is reached, the
billed cost can be lower. Use 'web.real_cost(task_id)' to get the
billed FlexCredit cost after a simulation run.
10:38:06 UTC status = success
10:38:09 UTC Loading results from data/simulation_data.hdf5
Note the different ordering of the recorded modes compared to what we saw above, with the fundamental TM mode coming first.
Now the fundamental Ez-polarized mode is injected, and as before, it is the only one for which the mode monitor records any intensity.
We can also have a look at the mode fields stored in the ModeFieldMonitor, either directly using xarray methods as above or using the Tidy3D `SimulationData <https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.SimulationData.html>`__ built-in field plotting.
Computing modes on the server
We can also run `ModeSimulation <https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.ModeSimulation.html>`__ on the server. We use the previous mode simulation as an example:
10:38:10 UTC Created task 'mode_simulation' with resource_id
'mos-0d39805a-d360-4e54-ab57-36e9faa2fd5d' and task_type 'MODE'.
10:38:11 UTC Estimated FlexCredit cost: 0.005. For this solver type, the
estimate is the final billed cost.
10:38:12 UTC status = success
10:38:14 UTC Loading results from simulation_data.hdf5
WARNING: updating ModeSimulation from 2.12.0.dev2 to 2.12.0.dev3
To demonstrate the accuracy improvement, we can show a convergence plot comparing the local and remote solvers on a waveguide that does not align with the mesh, in this case a dielectric rod.
For more detailed information about local subpixel, please refer to this example.
WARNING: The default value of 'current_amplitude_definition' for
'UniformCurrentSource' will change from 'density' to 'total' in a
future release. To avoid this warning and ensure consistent
behavior, please explicitly set
current_amplitude_definition='density' or
current_amplitude_definition='total' when creating the source.
Solving for resolution = 10
10:38:18 UTC WARNING: updating ModeSimulation from 2.12.0.dev2 to 2.12.0.dev3
Solving for resolution = 12
10:38:21 UTC WARNING: updating ModeSimulation from 2.12.0.dev2 to 2.12.0.dev3
Solving for resolution = 14
10:38:25 UTC WARNING: updating ModeSimulation from 2.12.0.dev2 to 2.12.0.dev3
Solving for resolution = 16
10:38:29 UTC WARNING: updating ModeSimulation from 2.12.0.dev2 to 2.12.0.dev3
Solving for resolution = 18
10:38:33 UTC WARNING: updating ModeSimulation from 2.12.0.dev2 to 2.12.0.dev3
Solving for resolution = 20
10:39:01 UTC WARNING: updating ModeSimulation from 2.12.0.dev2 to 2.12.0.dev3
Solving for resolution = 22
10:39:05 UTC WARNING: updating ModeSimulation from 2.12.0.dev2 to 2.12.0.dev3
Solving for resolution = 24
10:39:09 UTC WARNING: updating ModeSimulation from 2.12.0.dev2 to 2.12.0.dev3
Solving for resolution = 26
10:39:12 UTC WARNING: updating ModeSimulation from 2.12.0.dev2 to 2.12.0.dev3
Solving for resolution = 28
10:39:16 UTC WARNING: updating ModeSimulation from 2.12.0.dev2 to 2.12.0.dev3
Solving for resolution = 30
10:39:20 UTC WARNING: updating ModeSimulation from 2.12.0.dev2 to 2.12.0.dev3
Local Subpixel Comparison
Below, we enable local subpixel averaging and repeat the local mode simulation sweep. This allows a direct comparison between:
local without subpixel,
local with subpixel,
server result.
With local subpixel enabled, the local result matches the server result.
Note that it is necessary to install Tidy3D extras with pip install "tidy3d[extras]".
Solving with local subpixel for resolution = 10
Solving with local subpixel for resolution = 12
Solving with local subpixel for resolution = 14
Solving with local subpixel for resolution = 16
Solving with local subpixel for resolution = 18
Solving with local subpixel for resolution = 20
Solving with local subpixel for resolution = 22
Solving with local subpixel for resolution = 24
Solving with local subpixel for resolution = 26
Solving with local subpixel for resolution = 28
Solving with local subpixel for resolution = 30
Mode tracking
As is typical for eigenvalue solvers, Tidy3D’s mode simulation returns the calculated modes in the order corresponding to the magnitudes of the found eigenvalues, that is, the effective indices. Since the effective index of a mode generally depends on frequency and can become larger or smaller in magnitude relative to the effective indices of other modes, the same mode_index value in the returned mode simulation data may correspond to physically different modes at different frequencies.
To avoid such mismatches, `ModeSimulation <https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.ModeSimulation.html>`__ performs tracking across frequencies by default based on mode overlap values. See the discussion on mode decomposition. In other words, modes at one frequency are matched to the most similar modes at another frequency. This behavior is controlled by
`ModeSpec.sort_spec.track_freq <https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.ModeSortSpec.html>`__, typically configured as sort_spec=td.ModeSortSpec(track_freq=...). It can be set to either None, "lowest", "central" (default), or "highest". In the first case, no tracking across frequencies is performed. In the other three cases, tracking is performed starting from the specified frequency.
Additionally, any unsorted modes stored in the ModeSimulationData.modes dataset returned by ModeSimulation.run_local() can be sorted afterwards using the overlap_sort() function. This function takes the argument track_freq (default: "central"), which uses the same values as ModeSortSpec.track_freq, and overlap_thresh (default: 0.9), which defines the threshold overlap value above which a pair of spatial fields is considered to correspond to physically the same
mode.
While this functionality is not expected to be used in most cases, we demonstrate it here to provide a clearer understanding of `ModeSimulation <https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.ModeSimulation.html>`__ results. Note that for mode sources and mode monitors, sorting is always performed internally.
We start by calculating twelve modes at eleven different frequencies for a layered waveguide with the default tracking across frequencies turned off.
By inspecting the values of the effective indices, we note that some of them come very close to each other, which is a sign that some mode index values might not correspond to the same modes throughout the considered frequency range.
Indeed, by plotting field distributions for the calculated modes, we see mixing of modes for mode_index > 4.
This situation can be remedied by using the overlap_sort() function as follows.
Plotting the same information as previously, one can confirm that the modes are now matched as closely as possible.
Note that depending on the particular situation, mode sorting, whether the default behavior or performed manually, might not successfully resolve the tracking of all modes throughout the chosen frequency range. Possible reasons include: - A mode physically disappears at a certain frequency point. - A mode is not included in the calculated number of modes, num_modes, for certain frequency points. In this case, increasing num_modes might alleviate the issue. - The presence of degenerate
modes at certain frequencies.
Group Index
The mode simulation can also be used to directly calculate the group index of the calculated modes. It is important to have mode tracking enabled to avoid differentiating the wrong mode pairs around mode crossings.
Notes / Considerations
This mode simulation runs locally with run_local(), which means it does not require credits.
The same `ModeSimulation <https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.ModeSimulation.html>`__ object can also be run on the server with web.run(...).
Symmetries are applied if they are defined in the simulation and the mode plane center sits on the simulation center.
In cases where the number of modes, frequency points, or mode plane dimensions are large and mode tracking is not important, it may be beneficial to turn off the default mode sorting for increased computational efficiency.