Skip to content

Commit

Permalink
plotly much faster, only uses medium if shape match. and sim caches m…
Browse files Browse the repository at this point in the history
…edium and medium_map
  • Loading branch information
tylerflex committed Mar 30, 2022
1 parent d5ea65d commit 4dd7fc7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
3 changes: 3 additions & 0 deletions tidy3d/components/simulation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint: disable=too-many-lines
""" Container holding all information about simulation and its components"""
from typing import Dict, Tuple, List, Set
from functools import lru_cache

import pydantic
import numpy as np
Expand Down Expand Up @@ -532,6 +533,7 @@ def _validate_run_time(self) -> None:
""" Accounting """

@property
@lru_cache()
def mediums(self) -> Set[MediumType]:
"""Returns set of distinct :class:`AbstractMedium` in simulation.
Expand All @@ -545,6 +547,7 @@ def mediums(self) -> Set[MediumType]:
return list(medium_dict.keys())

@property
@lru_cache()
def medium_map(self) -> Dict[MediumType, pydantic.NonNegativeInt]:
"""Returns dict mapping medium to index in material.
``medium_map[medium]`` returns unique global index of :class:`AbstractMedium` in simulation.
Expand Down
35 changes: 18 additions & 17 deletions tidy3d/components/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,26 +229,27 @@ def plotly_sim(sim, x=None, y=None, z=None):
for struct in sim.structures:
geo = struct.geometry
shapes = geo.intersections(x=x, y=y, z=z)
mat_index = sim.medium_map[struct.medium]

params = StructMediumParams(medium=struct.medium, medium_map=sim.medium_map)
color = params.get_plot_params().facecolor
if len(shapes) > 0:
mat_index = sim.medium_map[struct.medium]
params = StructMediumParams(medium=struct.medium, medium_map=sim.medium_map)
color = params.get_plot_params().facecolor

for shape in shapes:
xs, ys = shape.exterior.coords.xy
xs = xs.tolist()
ys = ys.tolist()
for shape in shapes:
xs, ys = shape.exterior.coords.xy
xs = xs.tolist()
ys = ys.tolist()

plotly_trace = go.Scatter(
x=xs,
y=ys,
fill="toself",
fillcolor=color,
line=dict(width=0),
marker=dict(size=0.0001, line=dict(width=0)),
name=struct.medium.name if struct.medium.name else f"medium[{mat_index}]",
)
fig.add_trace(plotly_trace)
plotly_trace = go.Scatter(
x=xs,
y=ys,
fill="toself",
fillcolor=color,
line=dict(width=0),
marker=dict(size=0.0001, line=dict(width=0)),
name=struct.medium.name if struct.medium.name else f"medium[{mat_index}]",
)
fig.add_trace(plotly_trace)

for i, source in enumerate(sim.sources):
geo = source.geometry
Expand Down

0 comments on commit 4dd7fc7

Please sign in to comment.