Skip to content

Commit 5b64306

Browse files
dbochkov-flexcomputemomchil-flex
authored andcommitted
use simulation reduction automatically only in case of custom mediums
1 parent 90f2dac commit 5b64306

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

tidy3d/components/simulation.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3563,7 +3563,10 @@ def subsection(
35633563
# adjust region bounds to perfectly coincide with the grid
35643564
# note, sometimes (when a box already seems to perfrecty align with the grid)
35653565
# this causes the new region to expand one more pixel because of numerical roundoffs
3566-
aux_box = Box.from_bounds(*new_bounds)
3566+
# To help to avoid that we shrink new region by a small amount.
3567+
center = [(bmin + bmax) / 2 for bmin, bmax in zip(*new_bounds)]
3568+
size = [max(0.0, bmax - bmin - 2 * fp_eps) for bmin, bmax in zip(*new_bounds)]
3569+
aux_box = Box(center=center, size=size)
35673570
grid_inds = self.grid.discretize_inds(box=aux_box)
35683571

35693572
new_bounds = [

tidy3d/plugins/mode/mode_solver.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,8 @@ def reduced_simulation_copy(self):
973973
This might significantly reduce upload time in the presence of custom mediums.
974974
"""
975975

976-
# we preserve extracells along the normal direction to ensure
976+
# we preserve extracells along the normal direction to ensure there is enough data for
977+
# subpixel
977978
extended_grid = self._get_solver_grid(preserve_layer_behind=True, truncate_symmetry=False)
978979
grids_1d = extended_grid.boundaries
979980
new_sim_box = Box.from_bounds(

tidy3d/web/api/mode.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
from ...components.simulation import Simulation
1616
from ...components.data.monitor_data import ModeSolverData
17+
from ...components.medium import AbstractCustomMedium
18+
from ...components.types import Literal
1719
from ...exceptions import WebError
1820
from ...log import log, get_logging_console
1921
from ..core.core_config import get_logger_console
@@ -46,7 +48,7 @@ def run(
4648
verbose: bool = True,
4749
progress_callback_upload: Callable[[float], None] = None,
4850
progress_callback_download: Callable[[float], None] = None,
49-
reduce_simulation: bool = True,
51+
reduce_simulation: Literal["auto", True, False] = "auto",
5052
) -> ModeSolverData:
5153
"""Submits a :class:`.ModeSolver` to server, starts running, monitors progress, downloads,
5254
and loads results as a :class:`.ModeSolverData` object.
@@ -69,9 +71,9 @@ def run(
6971
Optional callback function called when uploading file with ``bytes_in_chunk`` as argument.
7072
progress_callback_download : Callable[[float], None] = None
7173
Optional callback function called when downloading file with ``bytes_in_chunk`` as argument.
72-
reduce_simulation : bool = True
73-
Restrict simulation to mode solver region. This can help reduce the amount of uploaded and
74-
dowloaded data.
74+
reduce_simulation : Literal["auto", True, False] = "auto"
75+
Restrict simulation to mode solver region. If "auto", then simulation is automatically
76+
restricted if it contains custom mediums.
7577
7678
Returns
7779
-------
@@ -83,6 +85,21 @@ def run(
8385
if verbose:
8486
console = get_logging_console()
8587

88+
if reduce_simulation == "auto":
89+
sim = mode_solver.simulation
90+
contains_custom = any(isinstance(s.medium, AbstractCustomMedium) for s in sim.structures)
91+
contains_custom = contains_custom or isinstance(sim.medium, AbstractCustomMedium)
92+
reduce_simulation = contains_custom
93+
94+
if reduce_simulation:
95+
log.warning(
96+
"The associated `Simulation` object contains custom mediums. It will be "
97+
"automatically restricted to the mode solver plane to reduce data for uploading. "
98+
"To force uploading the original `Simulation` object use `reduce_simulation=False`."
99+
" Setting `reduce_simulation=True` will force simulation reduction in all cases and"
100+
" silence this warning."
101+
)
102+
86103
if reduce_simulation:
87104
mode_solver = mode_solver.reduced_simulation_copy
88105

0 commit comments

Comments
 (0)