Skip to content
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
12 changes: 12 additions & 0 deletions engibench/problems/photonics2d/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
from engibench.problems.photonics2d.backend import insert_mode
from engibench.problems.photonics2d.backend import mode_overlap
from engibench.problems.photonics2d.backend import poly_ramp
from engibench.problems.photonics2d.backend import wavelength_to_frequency
from engibench.problems.photonics2d.v0 import Photonics2D as Photonics2D_v0


Expand Down Expand Up @@ -103,6 +104,17 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:

# ------------------------------------------------------------------ helpers

def _setup_simulation(self, config: dict[str, Any] | None = None) -> dict[str, Any]:
"""Set up the simulation and update the frequencies from ``lambda1`` / ``lambda2``.

v0 only computes ``omega1`` / ``omega2`` in ``__init__``, so wavelengths passed in ``config``
were ignored.
"""
conditions = super()._setup_simulation(config)
self.omega1 = wavelength_to_frequency(conditions["lambda1"])
self.omega2 = wavelength_to_frequency(conditions["lambda2"])
return conditions

def _check_design(self, design: npt.NDArray, config: dict[str, Any] | None) -> None:
"""Validate ``design`` (and any overridden config) against the declared constraints.

Expand Down
24 changes: 24 additions & 0 deletions tests/test_photonics2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
These tests use a small grid and few optimization steps to stay fast.
"""

import dataclasses

import numpy as np
import pytest

Expand Down Expand Up @@ -157,3 +159,25 @@ def test_design_to_epsr_is_linear_scaling(problem: Photonics2D) -> None:
design_mask = problem._design_region.astype(bool)
assert np.allclose(epsr_zeros[design_mask], problem._epsr_min)
assert np.allclose(epsr_ones[design_mask], problem._epsr_max)


# --------------------------------------------------------------------- conditions


def test_simulate_honors_wavelengths_passed_per_call(problem: Photonics2D, start_design: np.ndarray) -> None:
"""Wavelengths passed per call must give the same result as wavelengths passed to the constructor."""
wavelengths = {"lambda1": 0.7, "lambda2": 1.4}
built = Photonics2D(num_elems_x=NUM_X, num_elems_y=NUM_Y, config=wavelengths)
per_call = float(problem.simulate(start_design, config=wavelengths)[0])
at_construction = float(built.simulate(start_design)[0])
assert per_call == pytest.approx(at_construction, rel=1e-9)


def test_dataset_row_reproduces_its_own_objective() -> None:
"""Simulating a stored design under its own conditions returns the objective stored beside it."""
problem = Photonics2D()
for row in problem.dataset["test"].select(range(2)):
conditions = {f.name: row[f.name] for f in dataclasses.fields(Photonics2D.Conditions)}
design = np.asarray(row["optimal_design"], dtype=float)
got = float(problem.simulate(design, config=conditions)[0])
assert got == pytest.approx(row["total_overlap"], rel=1e-6)
Loading