Skip to content

Commit

Permalink
convergence testing examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafacc committed Mar 20, 2024
1 parent 76d23ec commit cf44d13
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
80 changes: 80 additions & 0 deletions examples/04_import_technology/04b_convergence_sbend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# %%!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
convergence testing examples using an unideal S-bend test case.
@author: Mustafa Hammood
"""
import gds_tidy3d as gtd
import tidy3d as td
import os
import matplotlib.pyplot as plt
import numpy as np

def convergence_z_span(
layout: gtd.core.layout,
tech: dict,
z_span: list=np.logspace(np.log10(0.221), np.log10(2), num=12)):

sims_z_span = []
sims = []

for z in z_span:
device = gtd.simprocessor.load_component_from_tech(ly=layout, tech=technology, z_span=z)
sims.append(gtd.simprocessor.build_sim_from_tech(
tech=technology,
layout=layout,
in_port=0,
wavl_min=1.545,
wavl_max=1.555,
wavl_pts=51,
mode_index=[0,1],
num_modes=2,
symmetry=(
0,
0,
0,
), # ensure structure is symmetric across symmetry axis before triggering this!
z_span=z,
))

sims[-1].upload()
# run the simulation. CHECK THE SIMULATION IN THE UI BEFORE RUNNING!
sims[-1].execute()
# visualize the results
#sims[-1].visualize_results()

fig, ax = plt.subplots()
te_log = []
tm_log = []
s12_te = 'S12_idx00'
s12_tm = 'S12_idx11'
for idx, sim in enumerate(sims):
s_te = sim.s_parameters.S[s12_te].s
s_tm = sim.s_parameters.S[s12_tm].s
f = sim.s_parameters.S[s12_te].freq
te_log.append(10*np.log10(np.abs(s_te[len(s_te) // 2])**2)) # middle
tm_log.append(10*np.log10(np.abs(s_tm[len(s_tm) // 2])**2)) # middle entry ax.plot(z_span, te_log, 'x-', label="TE", color='b')
ax.plot(z_span, te_log, 'x-', label="TE", color='r')
ax.plot(z_span, tm_log, 'x-', label="TM", color='r')
ax.legend()
ax.set_xlabel('z_span [um]')
ax.set_ylabel(f'Transmission [dB]')

return sims


if __name__ == "__main__":

tech_path = os.path.join(os.path.dirname(__file__), "tech.yaml")
technology = gtd.core.parse_yaml_tech(tech_path)

file_gds = os.path.join(os.path.dirname(__file__), "sbend.gds")

layout = gtd.lyprocessor.load_layout(file_gds)

# log sampling space, i expect output transission to be log too..
z_span_sweep = convergence_z_span(
layout=layout,
tech=technology,
z_span=np.logspace(np.log10(0.221), np.log10(2), num=12),
)
Binary file added examples/04_import_technology/sbend.gds
Binary file not shown.
12 changes: 6 additions & 6 deletions gds_tidy3d/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

import tidy3d as td
import logging
import numpy as np
import matplotlib.pyplot as plt
import os


Expand Down Expand Up @@ -294,7 +292,7 @@ def measure_transmission(
idx_out=get_port_name(monitor.name),
mode_in=sim_job["source"].mode_index,
mode_out=mode,
freq=td.C_0 / (wavl*1e-6), # convert back to SI. (m)
freq=td.C_0 / (wavl),
s=amp,
)
)
Expand Down Expand Up @@ -360,7 +358,7 @@ def plot(self):
logging.info("Mode amplitudes in each port: \n")
mag = [10 * np.log10(abs(i) ** 2) for i in i.s]
phase = [np.angle(i) ** 2 for i in i.s]
ax.plot(td.C_0 / i.freq, mag, label=i.label)
ax.plot(1e6 * td.C_0 / i.freq, mag, label=i.label)
ax.legend()
return fig, ax

Expand All @@ -378,8 +376,10 @@ def __init__(self, idx_in, idx_out, mode_in, mode_out, freq, s):
def label(self):
return f"S{self.idx_out}{self.idx_in}_idx{self.mode_out}{self.mode_in}"

def plot_freq_vs_s(self):
plt.plot(np.array(self.freq)/(1e-6*td.C_0), 10*np.log10(self.s**2))
def plot(self):
import numpy as np
import matplotlib.pyplot as plt
plt.plot((1e-6*td.C_0)/np.array(self.freq), 10*np.log10(self.s**2))
plt.xlabel('Wavelength [um]')
plt.ylabel('Transmission [dB]')
plt.title('Frequency vs S')
Expand Down

0 comments on commit cf44d13

Please sign in to comment.