Skip to content

Fix to mode solver simulation reduction in web.upload #2583

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

Merged
merged 2 commits into from
Jun 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
31 changes: 31 additions & 0 deletions tests/test_web/test_webapi_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,22 @@ def mock_upload(monkeypatch, set_api_key):
status=200,
)

# store the uploaded stub for verification
uploaded_stub = {}

def mock_upload_simulation(self, stub, **kwargs):
uploaded_stub["stub"] = stub

def mock_upload_file(*args, **kwargs):
pass

monkeypatch.setattr(
"tidy3d.web.core.task_core.SimulationTask.upload_simulation", mock_upload_simulation
)
monkeypatch.setattr("tidy3d.web.core.task_core.upload_file", mock_upload_file)

return uploaded_stub


@pytest.fixture
def mock_get_info(monkeypatch, set_api_key):
Expand Down Expand Up @@ -269,6 +280,26 @@ def test_upload(monkeypatch, mock_upload, mock_get_info, mock_metadata):
assert upload(sim, TASK_NAME, PROJECT_NAME, reduce_simulation=True)


@pytest.mark.parametrize("reduce_simulation", [True, False])
@responses.activate
def test_upload_with_reduction_parameter(
monkeypatch, mock_upload, mock_get_info, mock_metadata, reduce_simulation
):
"""Test that simulation reduction is properly applied before upload based on reduce_simulation parameter."""
sim = make_mode_sim()

upload(sim, TASK_NAME, PROJECT_NAME, reduce_simulation=reduce_simulation)

if reduce_simulation:
expected_sim = get_reduced_simulation(sim, reduce_simulation=True)
assert sim != expected_sim
else:
expected_sim = sim

uploaded_sim = mock_upload["stub"].simulation
assert uploaded_sim == expected_sim


@responses.activate
def test_get_info(mock_get_info):
assert get_info(TASK_ID).taskId == TASK_ID
Expand Down
31 changes: 31 additions & 0 deletions tests/test_web/test_webapi_mode_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,22 @@ def mock_upload(monkeypatch, set_api_key):
status=200,
)

# store the uploaded stub for verification
uploaded_stub = {}

def mock_upload_simulation(self, stub, **kwargs):
uploaded_stub["stub"] = stub

def mock_upload_file(*args, **kwargs):
pass

monkeypatch.setattr(
"tidy3d.web.core.task_core.SimulationTask.upload_simulation", mock_upload_simulation
)
monkeypatch.setattr("tidy3d.web.core.task_core.upload_file", mock_upload_file)

return uploaded_stub


@pytest.fixture
def mock_get_info(monkeypatch, set_api_key):
Expand Down Expand Up @@ -265,6 +276,26 @@ def test_upload(monkeypatch, mock_upload, mock_get_info, mock_metadata):
assert upload(sim, TASK_NAME, PROJECT_NAME, reduce_simulation=True)


@pytest.mark.parametrize("reduce_simulation", [True, False])
@responses.activate
def test_upload_with_reduction_parameter(
monkeypatch, mock_upload, mock_get_info, mock_metadata, reduce_simulation
):
"""Test that simulation reduction is properly applied before upload based on reduce_simulation parameter."""
sim = make_mode_sim()

upload(sim, TASK_NAME, PROJECT_NAME, reduce_simulation=reduce_simulation)

if reduce_simulation:
expected_sim = get_reduced_simulation(sim, reduce_simulation=True)
assert sim != expected_sim
else:
expected_sim = sim

uploaded_sim = mock_upload["stub"].simulation
assert uploaded_sim == expected_sim


@responses.activate
def test_get_info(mock_get_info):
assert get_info(TASK_ID).taskId == TASK_ID
Expand Down
7 changes: 3 additions & 4 deletions tidy3d/web/api/webapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ def upload(

"""

if isinstance(simulation, (ModeSolver, ModeSimulation)):
simulation = get_reduced_simulation(simulation, reduce_simulation)

stub = Tidy3dStub(simulation=simulation)
stub.validate_pre_upload(source_required=source_required)
log.debug("Creating task.")
Expand Down Expand Up @@ -279,8 +282,6 @@ def upload(
remote_sim_file = SIM_FILE_HDF5_GZ
if task_type == "MODE_SOLVER":
remote_sim_file = MODE_FILE_HDF5_GZ
if task_type == "MODE_SOLVER" or task_type == "MODE":
simulation = get_reduced_simulation(simulation, reduce_simulation)

task.upload_simulation(
stub=stub,
Expand Down Expand Up @@ -322,7 +323,6 @@ def get_reduced_simulation(simulation, reduce_simulation):
there. Note that if we do the latter we may want to also modify the warning below to only
happen if there are custom media *and* they extend beyond the simulation domain.
"""

if reduce_simulation == "auto":
if isinstance(simulation, ModeSimulation):
sim_mediums = simulation.scene.mediums
Expand All @@ -339,7 +339,6 @@ def get_reduced_simulation(simulation, reduce_simulation):
" Setting 'reduce_simulation=True' will force simulation reduction in all cases and"
" silence this warning."
)

if reduce_simulation:
return simulation.reduced_simulation_copy
return simulation
Expand Down
Loading