Skip to content

Commit 4338d8b

Browse files
committed
fix multi mode handling in tidy3d
1 parent afbb9d6 commit 4338d8b

File tree

3 files changed

+21
-51
lines changed

3 files changed

+21
-51
lines changed

gds_fdtd/lum_tools.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
"""
22
gds_fdtd simulation toolbox.
33
4-
Lumerical tools module.
4+
Lumerical tools interface module.
55
@author: Mustafa Hammood, 2025
66
"""
77

88
from gds_fdtd.core import structure, component
9-
from .core import port
109
import logging
1110
import lumapi
1211
import numpy as np

gds_fdtd/simprocessor.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ def make_source(
4747
Returns:
4848
td.ModeSource: Generated source.
4949
"""
50-
import tidy3d as td
5150

5251
if port.direction == 0:
5352
x_buffer = -buffer
@@ -92,9 +91,6 @@ def make_structures(device, buffer: float=4.):
9291
Returns:
9392
list: list of structures generated from the device.
9493
"""
95-
import tidy3d as td
96-
import numpy as np
97-
9894
# TODO find a better way to handle material..
9995

10096
# TODO fix box tox handling here
@@ -214,7 +210,6 @@ def make_field_monitor(device, freqs=2e14, axis="z", z_center=None):
214210
Returns:
215211
FieldMonitor: Generated Tidy3D field monitor object
216212
"""
217-
import numpy as np
218213

219214
# identify a device field z_center if None
220215
if z_center is None:
@@ -491,27 +486,6 @@ def load_component_from_tech(cell, tech, z_span=4, z_center=None):
491486
bounds=bounds,
492487
)
493488

494-
def build_sim_from_tech(tech: dict, cell, in_port=0, **kwargs):
495-
496-
z_span = kwargs.pop("z_span", 4) # Default value 4 if z_span is not provided
497-
498-
device = load_component_from_tech(cell=cell, tech=tech, z_span=z_span)
499-
500-
if isinstance(in_port, int):
501-
return make_t3d_sim(
502-
device=device,
503-
in_port=device.ports[in_port],
504-
z_span=z_span,
505-
**kwargs,
506-
)
507-
elif in_port == "all":
508-
return make_t3d_sim(
509-
device=device,
510-
in_port=device.ports[:],
511-
z_span=z_span,
512-
**kwargs,
513-
)
514-
515489

516490
def from_gdsfactory(c: 'gf.Component', tech: dict, z_span: float = 4.) -> 'component':
517491
"""Convert gdsfactory Component to a component.

gds_fdtd/t3d_tools.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def measure_transmission(in_port: port, in_mode_idx: int, out_mode_idx: int):
6767
logging.warning(
6868
"Multiple results handler is WIP, using first results entry"
6969
)
70-
results = self.results[0]
70+
results = self.results[-1]
7171
else:
7272
results = self.results
7373

@@ -130,28 +130,25 @@ def measure_transmission(in_port: port, in_mode_idx: int, out_mode_idx: int):
130130
if isinstance(self.results, list) and len(self.results) == 1:
131131
self.results = self.results[0]
132132

133+
def _plot_any_axis(self, job_result, freq):
134+
"""Try x/y/z until one works, put axis name in title."""
135+
for axis in ("x", "y", "z"):
136+
try:
137+
fig, ax = plt.subplots(1, 1, figsize=(16, 3))
138+
job_result.plot_field(f"{axis}_field", "Ey", freq=freq, ax=ax)
139+
ax.set_title(f"**{axis.upper()}‑field**") # highlight chosen axis
140+
fig.show()
141+
except Exception:
142+
plt.close(fig)
143+
# nothing matched → silently skip
144+
145+
133146
def visualize_results(self):
134147
self.s_parameters.plot()
148+
freq = td.C_0 / ((self.wavl_max + self.wavl_min) / 2)
135149

136-
try:
137-
if isinstance(self.results, list):
138-
for job_result in self.results:
139-
fig, ax = plt.subplots(1, 1, figsize=(16, 3))
140-
job_result.plot_field(
141-
"field",
142-
"Ey",
143-
freq=td.C_0 / ((self.wavl_max + self.wavl_min) / 2),
144-
ax=ax,
145-
)
146-
fig.show()
147-
else:
148-
fig, ax = plt.subplots(1, 1, figsize=(16, 3))
149-
self.results.plot_field(
150-
"field",
151-
"Ey",
152-
freq=td.C_0 / ((self.wavl_max + self.wavl_min) / 2),
153-
ax=ax,
154-
)
155-
fig.show()
156-
except:
157-
return
150+
if isinstance(self.results, list):
151+
for job_result in self.results:
152+
self._plot_any_axis(job_result, freq)
153+
else:
154+
self._plot_any_axis(self.results, freq)

0 commit comments

Comments
 (0)