Skip to content

XY transposable sim.plot() plots (issue1072) VERSION 2 #2544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 39 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
cdede98
first attempt to add the "transpose" argument all 2D plot-related tid…
jewettaijfc Jun 5, 2025
22cf3c2
reverted changes to corner_finder.py and added more changes to geomet…
jewettaijfc Jun 5, 2025
7544d72
added minimal changes to primitives.py
jewettaijfc Jun 5, 2025
9c354fe
updated primitives.py and reverted monitor.py to its original state (…
jewettaijfc Jun 5, 2025
983d37d
removed the "transpose" arguments from the various `_gds` functions i…
jewettaijfc Jun 6, 2025
120b122
removed "transpose" argument from some validator functions. (oops. …
jewettaijfc Jun 6, 2025
4b1f764
I fixed most of the problems with Scene.plot(). But Polyslabs are no…
jewettaijfc Jun 6, 2025
b6d1a0f
It's finally possible to plot PolySlab objects when transpose=True, f…
jewettaijfc Jun 6, 2025
2b54ab8
removed some crud from PolySlab.make_shapely_polygon()
jewettaijfc Jun 6, 2025
75fbd50
plot_eps() works again (with transpose=True or False)
jewettaijfc Jun 7, 2025
096ef92
removed all the print() statements used for debugging (again. I put …
jewettaijfc Jun 7, 2025
d0fb501
changed the name of the "transpose" argument to "swap_axes" everywhere
jewettaijfc Jun 7, 2025
dcde9c8
found a few more pesky gds-related functions that still had a "transp…
jewettaijfc Jun 7, 2025
e25a759
added the "swap_axes" argument to TFSF.plot() to be consistent with t…
jewettaijfc Jun 7, 2025
63f2937
replaced (un)pop_axis() with (un)pop_axis_and_swap() everwhere where …
jewettaijfc Jun 8, 2025
b38ca42
Fixed a bug in Scene.plot_heat_charge_property(), and tested it (both…
jewettaijfc Jun 8, 2025
7faf00f
Fixed some bugs in HeatChargeSimulation.plot_sources(). It runs with…
jewettaijfc Jun 8, 2025
cdb8a9e
(Hopefully) Fixed a bug in HeatChargeSimulationData.plot_field() from…
jewettaijfc Jun 8, 2025
2f24e48
fixed a bug introduced when I renamed "pop_axis()" to "pop_axis_and_s…
jewettaijfc Jun 8, 2025
1b0c387
added test_pop_axis_and_swap() to test_geometry.py
jewettaijfc Jun 9, 2025
c5ed38e
deleted some additional print() statements I am no longer using
jewettaijfc Jun 9, 2025
de2d14d
fixed a bug that broke HeatChargeSimulationData
jewettaijfc Jun 9, 2025
1cefa2a
"vertices[:, (1, 0)]" --> "vertices[:, ::-1]"
jewettaijfc Jun 9, 2025
ba48eb0
I fixed the last remaining unit test failure. I stil need modify the…
jewettaijfc Jun 9, 2025
f0995ca
deleted test_polyslab.py
jewettaijfc Jun 9, 2025
e491d6e
removed 2 files ('report.log_BRANCH=develop' and 'results.prof_BRANCH…
jewettaijfc Jun 10, 2025
5f954ff
Added "swap_axes" argument to mode_solver.py. Updated unit tests for…
jewettaijfc Jun 10, 2025
d53fae0
Updated unit tests for test_simulation.py
jewettaijfc Jun 10, 2025
324fb89
updated unit tests for test_scene.py and test_geometry.py. Removed t…
jewettaijfc Jun 11, 2025
4bbf0b9
added/updated unit tests for test_source.py
jewettaijfc Jun 11, 2025
a22a5d4
updated unit tests for test_heat.py and test_heat_charge.py
jewettaijfc Jun 11, 2025
a9b91b7
renamed the "swap_axes" argument --> "transpose"
jewettaijfc Jun 11, 2025
86c035a
updated unit tests for test_eme.py
jewettaijfc Jun 11, 2025
97f7db8
added docstrings and removed the "transpose" argument from several fu…
jewettaijfc Jun 11, 2025
8e2c89a
corrected typo in the docstrings
jewettaijfc Jun 11, 2025
fd86c99
reformatting some files manually. afraid to use "black"
jewettaijfc Jun 12, 2025
a2b4b83
reformatted using `black . --line-length 100`
jewettaijfc Jun 12, 2025
c2b17b0
Ran `ruff check tidy3d --fix` to fix an import error in tcad/data/sim…
jewettaijfc Jun 12, 2025
d270599
attempting to fix a linter error
jewettaijfc Jun 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions tests/test_components/test_IO.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@ def test_simulation_load_export(split_string, tmp_path):
SIM.to_hdf5(path_hdf5)
SIM2 = td.Simulation.from_file(path)
SIM_HDF5 = td.Simulation.from_hdf5(path_hdf5)
assert set_datasets_to_none(SIM)._json_string == SIM2._json_string, (
"original and loaded simulations are not the same"
)
assert (
set_datasets_to_none(SIM)._json_string == SIM2._json_string
), "original and loaded simulations are not the same"
assert SIM == SIM_HDF5, "original and loaded from hdf5 simulations are not the same"


def test_simulation_load_export_yaml(tmp_path):
path = str(tmp_path / "simulation.yaml")
SIM.to_file(path)
SIM2 = td.Simulation.from_file(path)
assert set_datasets_to_none(SIM)._json_string == SIM2._json_string, (
"original and loaded simulations are not the same"
)
assert (
set_datasets_to_none(SIM)._json_string == SIM2._json_string
), "original and loaded simulations are not the same"


def test_component_load_export(tmp_path):
Expand Down
48 changes: 24 additions & 24 deletions tests/test_components/test_autograd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1247,18 +1247,18 @@ def test_adjoint_src_width():
adj_srcs_fwidth = td.SimulationData._adjoint_src_width_single(adj_srcs)

for src in adj_srcs_fwidth:
assert np.isclose((src.source_time.freq0 - f0) / f0, 0.0), (
"f0 of adjoint source should be centered on original f0"
)
assert np.isclose(
(src.source_time.freq0 - f0) / f0, 0.0
), "f0 of adjoint source should be centered on original f0"

check_fwidth = (
src.source_time.freq0
- td.components.data.sim_data.NUM_ADJOINT_FWIDTH_TO_ZERO * src.source_time.fwidth
) / src.source_time.freq0

assert np.isclose(check_fwidth, 0.0) or (check_fwidth > 0.0), (
"fwidth of adjoint source should decay sufficiently before f=0"
)
assert np.isclose(check_fwidth, 0.0) or (
check_fwidth > 0.0
), "fwidth of adjoint source should decay sufficiently before f=0"


def test_broadband_adjoint_src_width():
Expand Down Expand Up @@ -1305,12 +1305,12 @@ def test_broadband_adjoint_src_width():
f0_expected - np.min(f0_adj_all)
) / td.components.data.sim_data.NUM_ADJOINT_FWIDTH_TO_FMIN

assert np.isclose((f0_expected - broadband_f0) / f0_expected, 0.0), (
"Expected freq0 not matching for broadband source"
)
assert np.isclose((fwidth_expected - broadband_fwidth) / fwidth_expected, 0.0), (
"Expected fwidth not matching for broadband source"
)
assert np.isclose(
(f0_expected - broadband_f0) / f0_expected, 0.0
), "Expected freq0 not matching for broadband source"
assert np.isclose(
(fwidth_expected - broadband_fwidth) / fwidth_expected, 0.0
), "Expected fwidth not matching for broadband source"

# Test the case where we need a wider pulse to cover all the adjoint frequencies than we would otherwise choose for
# each individual adjoint source
Expand All @@ -1333,12 +1333,12 @@ def test_broadband_adjoint_src_width():
f0_expected - np.min(f0_broadband)
) / td.components.data.sim_data.NUM_ADJOINT_FWIDTH_TO_FMIN

assert np.isclose((f0_expected - broadband_f0) / f0_expected, 0.0), (
"Expected freq0 not matching for broadband source"
)
assert np.isclose((fwidth_expected - broadband_fwidth) / fwidth_expected, 0.0), (
"Expected fwidth not matching for broadband source"
)
assert np.isclose(
(f0_expected - broadband_f0) / f0_expected, 0.0
), "Expected freq0 not matching for broadband source"
assert np.isclose(
(fwidth_expected - broadband_fwidth) / fwidth_expected, 0.0
), "Expected fwidth not matching for broadband source"

# Test the case where we have a narrow set of frequencies for the adjoint sources and so we can
# choose a wider overall source than is needed for covering those frequencies. This larger pulse width
Expand All @@ -1360,12 +1360,12 @@ def test_broadband_adjoint_src_width():
f0_expected = 0.5 * (np.max(f0_broadband) + np.min(f0_broadband))
fwidth_expected = f0_expected / td.components.data.sim_data.NUM_ADJOINT_FWIDTH_TO_ZERO

assert np.isclose((f0_expected - broadband_f0) / f0_expected, 0.0), (
"Expected freq0 not matching for broadband source"
)
assert np.isclose((fwidth_expected - broadband_fwidth) / fwidth_expected, 0.0), (
"Expected fwidth not matching for broadband source"
)
assert np.isclose(
(f0_expected - broadband_f0) / f0_expected, 0.0
), "Expected freq0 not matching for broadband source"
assert np.isclose(
(fwidth_expected - broadband_fwidth) / fwidth_expected, 0.0
), "Expected fwidth not matching for broadband source"


@pytest.mark.parametrize("colocate", [True, False])
Expand Down
10 changes: 4 additions & 6 deletions tests/test_components/test_autograd_mode_polyslab_numerical.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,9 @@ def test_finite_difference_mode_data_polyslab(
"geometry_size_wvl",
"polyslab_permittivity",
"test_number",
)(mode_data_test_parameters)
)(
mode_data_test_parameters
)

adj_freq = td.C_0 / adj_wvl_um

Expand Down Expand Up @@ -372,11 +374,7 @@ def eval_fn(sim_data):
polyslab_height_um = POLYSLAB_HEIGHT_WVL * adj_wvl_um

objective = create_objective_function(
lambda mesh_wvl_um=mesh_wvl_um,
adj_wvl_um=adj_wvl_um,
geometry_size_wvl=geometry_size_wvl,
polyslab_permittivity=polyslab_permittivity,
box_for_override=box_for_override: make_base_sim(
lambda mesh_wvl_um=mesh_wvl_um, adj_wvl_um=adj_wvl_um, geometry_size_wvl=geometry_size_wvl, polyslab_permittivity=polyslab_permittivity, box_for_override=box_for_override: make_base_sim(
mesh_wvl_um=mesh_wvl_um,
adj_wvl_um=adj_wvl_um,
geometry_size_wvl=geometry_size_wvl,
Expand Down
10 changes: 4 additions & 6 deletions tests/test_components/test_autograd_numerical.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,9 @@ def test_finite_difference_field_data(field_data_test_parameters, rng, tmp_path,
"eval_fn",
"eval_fn_name",
"test_number",
)(field_data_test_parameters)
)(
field_data_test_parameters
)

dim_um = mesh_wvl_um
dim_um = mesh_wvl_um
Expand All @@ -279,11 +281,7 @@ def test_finite_difference_field_data(field_data_test_parameters, rng, tmp_path,

objective = create_objective_function(
block,
lambda mesh_wvl_um=mesh_wvl_um,
adj_wvl_um=adj_wvl_um,
monitor_size_wvl=monitor_size_wvl,
box_for_override=box_for_override,
monitor_bg_index=monitor_bg_index: make_base_sim(
lambda mesh_wvl_um=mesh_wvl_um, adj_wvl_um=adj_wvl_um, monitor_size_wvl=monitor_size_wvl, box_for_override=box_for_override, monitor_bg_index=monitor_bg_index: make_base_sim(
mesh_wvl_um=mesh_wvl_um,
adj_wvl_um=adj_wvl_um,
monitor_size_wvl=monitor_size_wvl,
Expand Down
27 changes: 14 additions & 13 deletions tests/test_components/test_eme.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,21 +283,22 @@ def test_eme_monitor():
)


def test_eme_simulation():
@pytest.mark.parametrize("transpose", [True, False])
def test_eme_simulation(transpose):
sim = make_eme_sim()
_ = sim.plot(x=0, ax=AX)
_ = sim.plot(y=0, ax=AX)
_ = sim.plot(z=0, ax=AX)
_ = sim.plot_grid(x=0, ax=AX)
_ = sim.plot_grid(y=0, ax=AX)
_ = sim.plot_grid(z=0, ax=AX)
_ = sim.plot_eps(x=0, ax=AX)
_ = sim.plot_eps(y=0, ax=AX)
_ = sim.plot_eps(z=0, ax=AX)
_ = sim.plot(x=0, ax=AX, transpose=transpose)
_ = sim.plot(y=0, ax=AX, transpose=transpose)
_ = sim.plot(z=0, ax=AX, transpose=transpose)
_ = sim.plot_grid(x=0, ax=AX, transpose=transpose)
_ = sim.plot_grid(y=0, ax=AX, transpose=transpose)
_ = sim.plot_grid(z=0, ax=AX, transpose=transpose)
_ = sim.plot_eps(x=0, ax=AX, transpose=transpose)
_ = sim.plot_eps(y=0, ax=AX, transpose=transpose)
_ = sim.plot_eps(z=0, ax=AX, transpose=transpose)
sim2 = sim.updated_copy(axis=1)
_ = sim2.plot(x=0, ax=AX)
_ = sim2.plot(y=0, ax=AX)
_ = sim2.plot(z=0, ax=AX)
_ = sim2.plot(x=0, ax=AX, transpose=transpose)
_ = sim2.plot(y=0, ax=AX, transpose=transpose)
_ = sim2.plot(z=0, ax=AX, transpose=transpose)

# need at least one freq
with pytest.raises(pd.ValidationError):
Expand Down
89 changes: 54 additions & 35 deletions tests/test_components/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,15 @@
_, AX = plt.subplots()


@pytest.mark.parametrize("component", GEO_TYPES)
def test_plot(component):
_ = component.plot(z=0, ax=AX)
@pytest.mark.parametrize("component, transpose", zip(GEO_TYPES, [True, False]))
def test_plot(component, transpose):
_ = component.plot(z=0, ax=AX, transpose=transpose)
plt.close()


def test_plot_with_units():
_ = BOX.plot(z=0, ax=AX, plot_length_units="nm")
@pytest.mark.parametrize("transpose", [True, False])
def test_plot_with_units(transpose):
_ = BOX.plot(z=0, ax=AX, plot_length_units="nm", transpose=transpose)
plt.close()


Expand Down Expand Up @@ -204,11 +205,11 @@ def test_array_to_vertices():
assert np.all(np.array(vertices) == np.array(vertices2))


@pytest.mark.parametrize("component", GEO_TYPES)
def test_intersections_plane(component):
assert len(component.intersections_plane(z=0.2)) > 0
assert len(component.intersections_plane(x=0.2)) > 0
assert len(component.intersections_plane(x=10000)) == 0
@pytest.mark.parametrize("component, transpose", zip(GEO_TYPES, [True, False]))
def test_intersections_plane(component, transpose):
assert len(component.intersections_plane(z=0.2, transpose=transpose)) > 0
assert len(component.intersections_plane(x=0.2, transpose=transpose)) > 0
assert len(component.intersections_plane(x=10000, transpose=transpose)) == 0


def test_intersections_plane_inf():
Expand Down Expand Up @@ -329,15 +330,15 @@ def test_polyslab_inf_to_finite_bounds(axis):
vertices=[[0, 0], [2.5, 1], [2, 3], [0.5, 4], [-1.5, 2.5]],
)

assert ps_low_inf.finite_length_axis == (LARGE_NUMBER + axis_bound), (
"Unexpected finite length for polyslab axis with -inf bound"
)
assert ps_high_inf.finite_length_axis == (LARGE_NUMBER + axis_bound), (
"Unexpected finite length for polyslab axis with inf bound"
)
assert ps_inf.finite_length_axis == 2 * LARGE_NUMBER, (
"Unexpected finite length for polyslab axis with two inf bounds"
)
assert ps_low_inf.finite_length_axis == (
LARGE_NUMBER + axis_bound
), "Unexpected finite length for polyslab axis with -inf bound"
assert ps_high_inf.finite_length_axis == (
LARGE_NUMBER + axis_bound
), "Unexpected finite length for polyslab axis with inf bound"
assert (
ps_inf.finite_length_axis == 2 * LARGE_NUMBER
), "Unexpected finite length for polyslab axis with two inf bounds"


def test_validate_polyslab_vertices_valid():
Expand Down Expand Up @@ -766,36 +767,49 @@ def test_geometry_touching_intersections_plane(x0):


def test_pop_axis():
b = td.Box(size=(1, 1, 1))
for axis in range(3):
coords = (1, 2, 3)
Lz, (Lx, Ly) = b.pop_axis(coords, axis=axis)
_coords = b.unpop_axis(Lz, (Lx, Ly), axis=axis)
Lz, (Lx, Ly) = td.Box.pop_axis(coords, axis=axis)
_coords = td.Box.unpop_axis(Lz, (Lx, Ly), axis=axis)
assert all(c == _c for (c, _c) in zip(coords, _coords))
_Lz, (_Lx, _Ly) = b.pop_axis(_coords, axis=axis)
_Lz, (_Lx, _Ly) = td.Box.pop_axis(_coords, axis=axis)
assert Lz == _Lz
assert Lx == _Lx
assert Ly == _Ly


def test_2b_box_intersections():
@pytest.mark.parametrize("transpose", [True, False])
def test_pop_axis_and_swap(transpose):
for axis in range(3):
coords = (1, 2, 3)
Lz, (Lx, Ly) = td.Box.pop_axis_and_swap(coords, axis=axis, transpose=transpose)
_coords = td.Box.unpop_axis_and_swap(Lz, (Lx, Ly), axis=axis, transpose=transpose)
assert all(c == _c for (c, _c) in zip(coords, _coords))
_Lz, (_Lx, _Ly) = td.Box.pop_axis_and_swap(_coords, axis=axis, transpose=transpose)
assert Lz == _Lz
assert Lx == _Lx
assert Ly == _Ly


@pytest.mark.parametrize("transpose", [True, False])
def test_2b_box_intersections(transpose):
plane = td.Box(size=(1, 4, 0))
box1 = td.Box(size=(1, 1, 1))
box2 = td.Box(size=(1, 1, 1), center=(3, 0, 0))

result = plane.intersections_with(box1)
result = plane.intersections_with(box1, transpose=transpose)
assert len(result) == 1
assert result[0].geom_type == "Polygon"
assert len(plane.intersections_with(box2)) == 0
assert len(plane.intersections_with(box2, transpose=transpose)) == 0

with pytest.raises(ValidationError):
_ = box1.intersections_with(box2)
_ = box1.intersections_with(box2, transpose=transpose)

assert len(box1.intersections_2dbox(plane)) == 1
assert len(box2.intersections_2dbox(plane)) == 0
assert len(box1.intersections_2dbox(plane, transpose=transpose)) == 1
assert len(box2.intersections_2dbox(plane, transpose=transpose)) == 0

with pytest.raises(ValidationError):
_ = box2.intersections_2dbox(box1)
_ = box2.intersections_2dbox(box1, transpose=transpose)


def test_polyslab_merge():
Expand Down Expand Up @@ -937,7 +951,8 @@ def test_to_gds(geometry, tmp_path):
assert len(cell.polygons) == 0


def test_custom_surface_geometry(tmp_path):
@pytest.mark.parametrize("transpose", [True, False])
def test_custom_surface_geometry(transpose, tmp_path):
# create tetrahedron STL
vertices = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]])
faces = np.array([[1, 2, 3], [0, 3, 2], [0, 1, 3], [0, 2, 1]])
Expand Down Expand Up @@ -970,9 +985,13 @@ def test_custom_surface_geometry(tmp_path):
assert np.isclose(geom.volume(), 1 / 6)

# test intersections
assert shapely.equals(geom.intersections_plane(x=0), shapely.Polygon([[0, 0], [0, 1], [1, 0]]))
assert shapely.equals(
geom.intersections_plane(z=0.5), shapely.Polygon([[0, 0], [0, 0.5], [0.5, 0]])
geom.intersections_plane(x=0, transpose=transpose),
shapely.Polygon([[0, 0], [0, 1], [1, 0]]),
)
assert shapely.equals(
geom.intersections_plane(z=0.5, transpose=transpose),
shapely.Polygon([[0, 0], [0, 0.5], [0.5, 0]]),
)

# test inside
Expand All @@ -981,7 +1000,7 @@ def test_custom_surface_geometry(tmp_path):

# test plot
_, ax = plt.subplots()
_ = geom.plot(z=0.1, ax=ax)
_ = geom.plot(z=0.1, ax=ax, transpose=transpose)
plt.close()

# test inconsistent winding
Expand Down Expand Up @@ -1031,7 +1050,7 @@ def test_custom_surface_geometry(tmp_path):
boundary_spec=td.BoundarySpec.all_sides(td.PML()),
)
_, ax = plt.subplots()
_ = sim.plot(y=0, ax=ax)
_ = sim.plot(y=0, ax=ax, transpose=transpose)
plt.close()

# allow small triangles
Expand Down
11 changes: 6 additions & 5 deletions tests/test_components/test_heat.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,12 @@ def make_heat_sim(include_custom_source: bool = True):
return heat_sim


def test_heat_sim():
@pytest.mark.parametrize("transpose", [True, False])
def test_heat_sim(transpose):
bc_temp, bc_flux, bc_conv = make_heat_bcs()
heat_sim = make_heat_sim()

_ = heat_sim.plot(x=0)
_ = heat_sim.plot(x=0, transpose=transpose)

# wrong names given
for pl in [
Expand Down Expand Up @@ -373,14 +374,14 @@ def test_heat_sim():
with pytest.raises(pd.ValidationError):
heat_sim.updated_copy(monitors=[temp_mnt, temp_mnt])

_ = heat_sim.plot(x=0)
_ = heat_sim.plot(x=0, transpose=transpose)
plt.close()

_ = heat_sim.plot_heat_conductivity(y=0)
_ = heat_sim.plot_heat_conductivity(y=0, transpose=transpose)
plt.close()

heat_sim_sym = heat_sim.updated_copy(symmetry=(0, 1, 1))
_ = heat_sim_sym.plot_heat_conductivity(z=0, colorbar="source")
_ = heat_sim_sym.plot_heat_conductivity(z=0, colorbar="source", transpose=transpose)
plt.close()

# no negative symmetry
Expand Down
Loading
Loading