Skip to content

Commit

Permalink
Change missing source from warning to info (#813) (#985)
Browse files Browse the repository at this point in the history
Also included a flag in the pre-upload validator to optionally raise an
error if no sources are present in the simulation. This flag is true by
default on `run` and manual job creation, but false for the mode solver.
It cannot be changed from `run` (only in `Job), because simulations
with no sources should be very rare and we already have a lot of
arguments in `run`.

Signed-off-by: Lucas Heitzmann Gabrielli <lucas@flexcompute.com>
  • Loading branch information
lucas-flexcompute authored Jul 4, 2023
1 parent 6291b4d commit c2473f0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

# Changed
- Source validation happens before simulation upload and raises an error if no source is present.

## [2.3.0] - 2023-6-30

### Added
Expand Down
21 changes: 18 additions & 3 deletions tests/test_web/test_webapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from responses import matchers

from tidy3d import Simulation
from tidy3d.exceptions import SetupError
from tidy3d.web.environment import Env
from tidy3d.web.webapi import delete, delete_old, download, download_json, run
from tidy3d.web.webapi import download_log, estimate_cost, get_info, get_run_info, get_tasks
Expand All @@ -32,7 +32,14 @@

def make_sim():
"""Makes a simulation."""
return td.Simulation(size=(1, 1, 1), grid_spec=td.GridSpec.auto(wavelength=1.0), run_time=1e-12)
pulse = td.GaussianPulse(freq0=200e12, fwidth=20e12)
pt_dipole = td.PointDipole(source_time=pulse, polarization="Ex")
return td.Simulation(
size=(1, 1, 1),
grid_spec=td.GridSpec.auto(wavelength=1.0),
run_time=1e-12,
sources=[pt_dipole],
)


@pytest.fixture
Expand Down Expand Up @@ -226,6 +233,14 @@ def mock_webapi(
"""Mocks all webapi operation."""


@responses.activate
def test_source_validation(mock_upload):
sim = make_sim().copy(update={"sources": []})
assert upload(sim, TASK_NAME, PROJECT_NAME, source_required=False)
with pytest.raises(SetupError):
upload(sim, TASK_NAME, PROJECT_NAME)


@responses.activate
def test_upload(mock_upload):
sim = make_sim()
Expand Down Expand Up @@ -453,7 +468,7 @@ def test_main(mock_webapi, monkeypatch, mock_job_status):
# batch_data = run_async(sims, folder_name=PROJECT_NAME)

def save_sim_to_path(path: str) -> None:
sim = Simulation(size=(1, 1, 1), grid_spec=td.GridSpec.auto(wavelength=1.0), run_time=1e-12)
sim = make_sim()
sim.to_file(path)

monkeypatch.setattr("builtins.input", lambda _: "Y")
Expand Down
14 changes: 11 additions & 3 deletions tidy3d/components/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ def _warn_monitor_simulation_frequency_range(cls, val, values):

source_ranges = [source.source_time.frequency_range() for source in values["sources"]]
if not source_ranges:
log.warning("No sources in simulation.")
log.info("No sources in simulation.")
return val

freq_min = min((freq_range[0] for freq_range in source_ranges), default=0.0)
Expand Down Expand Up @@ -890,14 +890,22 @@ def _validate_tfsf_nonuniform_grid(self) -> None:

""" Pre submit validation (before web.upload()) """

def validate_pre_upload(self) -> None:
"""Validate the fully initialized simulation is ok for upload to our servers."""
def validate_pre_upload(self, source_required: bool = True) -> None:
"""Validate the fully initialized simulation is ok for upload to our servers.
Parameters
----------
source_required: bool = True
If ``True``, validation will fail in case no sources are found in the simulation.
"""
self._validate_size()
self._validate_monitor_size()
self._validate_datasets_not_none()
self._validate_tfsf_structure_intersections()
# self._validate_run_time()
_ = self.volumetric_structures
if source_required and len(self.sources) == 0:
raise SetupError("No sources in simulation.")

def _validate_size(self) -> None:
"""Ensures the simulation is within size limits before simulation is uploaded."""
Expand Down
2 changes: 1 addition & 1 deletion tidy3d/plugins/mode/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def create(
mode_spec = mode_solver.mode_spec.copy(update={"precision": "single"})
mode_solver = mode_solver.copy(update={"mode_spec": mode_spec})

mode_solver.simulation.validate_pre_upload()
mode_solver.simulation.validate_pre_upload(source_required=False)
resp = http.post(
MODESOLVER_API,
{
Expand Down
5 changes: 4 additions & 1 deletion tidy3d/web/webapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def upload( # pylint:disable=too-many-locals,too-many-arguments
progress_callback: Callable[[float], None] = None,
simulation_type: str = "tidy3d",
parent_tasks: List[str] = None,
source_required: bool = True,
) -> TaskId:
"""Upload simulation to server, but do not start running :class:`.Simulation`.
Expand All @@ -164,6 +165,8 @@ def upload( # pylint:disable=too-many-locals,too-many-arguments
Type of simulation being uploaded.
parent_tasks : List[str]
List of related task ids.
source_required: bool = True
If ``True``, simulations without sources will raise an error before being uploaded.
Returns
-------
Expand All @@ -175,7 +178,7 @@ def upload( # pylint:disable=too-many-locals,too-many-arguments
To start the simulation running, must call :meth:`start` after uploaded.
"""

simulation.validate_pre_upload()
simulation.validate_pre_upload(source_required=source_required)
log.debug("Creating task.")

task = SimulationTask.create(
Expand Down

0 comments on commit c2473f0

Please sign in to comment.