Skip to content
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

Thickness upper bound #56

Merged
merged 2 commits into from
Nov 27, 2023
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
2 changes: 1 addition & 1 deletion src/invrs_gym/challenges/diffract/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class GratingSpec:
permittivity_encapsulation: complex
permittivity_substrate: complex

thickness_grating: float | jnp.ndarray
thickness_grating: float | jnp.ndarray | types.BoundedArray

period_x: float
period_y: float
Expand Down
11 changes: 4 additions & 7 deletions src/invrs_gym/challenges/diffract/splitter_challenge.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ def __init__(
self.thickness_initializer = thickness_initializer
self.density_initializer = density_initializer

self.seed_thickness = types.BoundedArray(
array=self.spec.thickness_grating,
lower_bound=0.0,
upper_bound=None,
)
self.seed_density = common.seed_density(
self.sim_params.grid_shape, **seed_density_kwargs
)
Expand All @@ -92,7 +87,9 @@ def init(self, key: jax.Array) -> Params:
"""Return the initial parameters for the diffractive splitter component."""
key_thickness, key_density = jax.random.split(key)
params = {
THICKNESS: self.thickness_initializer(key_thickness, self.seed_thickness),
THICKNESS: self.thickness_initializer(
key_thickness, self.spec.thickness_grating # type: ignore[arg-type]
),
DENSITY: self.density_initializer(key_density, self.seed_density),
}
# Ensure that there are no weak types in the initial parameters.
Expand Down Expand Up @@ -303,7 +300,7 @@ def extract_orders_for_splitting(
permittivity_grating=(1.46 + 0.00001j) ** 2,
permittivity_encapsulation=(1.0 + 0.00001j) ** 2,
permittivity_substrate=(1.0 + 0.0j) ** 2,
thickness_grating=0.692,
thickness_grating=types.BoundedArray(array=0.692, lower_bound=0.5, upper_bound=1.5),
period_x=7.2,
period_y=7.2,
)
Expand Down
17 changes: 5 additions & 12 deletions tests/challenges/diffract/test_metagrating_challenge.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@


class MetagratingComponentTest(unittest.TestCase):
def test_density_has_expected_properties(self):
mc = metagrating_challenge.MetagratingComponent(
spec=metagrating_challenge.METAGRATING_SPEC,
sim_params=LIGHTWEIGHT_SIM_PARAMS,
density_initializer=lambda _, seed_density: seed_density,
)
params = mc.init(jax.random.PRNGKey(0))
self.assertEqual(params.lower_bound, 0.0)
self.assertEqual(params.upper_bound, 1.0)
self.assertSequenceEqual(params.periodic, (True, True))

def test_can_jit_response(self):
mc = metagrating_challenge.MetagratingComponent(
spec=metagrating_challenge.METAGRATING_SPEC,
Expand Down Expand Up @@ -91,10 +80,14 @@ def step_fn(params, state):
@parameterized.expand([[1, 1], [2, 3]])
def test_density_has_expected_attrs(self, min_width, min_spacing):
mc = metagrating_challenge.metagrating(
minimum_width=min_width,
spec=metagrating_challenge.METAGRATING_SPEC,
sim_params=LIGHTWEIGHT_SIM_PARAMS,
density_initializer=lambda _, seed_density: seed_density,
minimum_spacing=min_spacing,
minimum_width=min_width,
)
params = mc.component.init(jax.random.PRNGKey(0))

self.assertEqual(params.lower_bound, 0.0)
self.assertEqual(params.upper_bound, 1.0)
self.assertSequenceEqual(params.periodic, (True, True))
Expand Down
30 changes: 11 additions & 19 deletions tests/challenges/diffract/test_splitter_challenge.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,6 @@


class SplitterComponentTest(unittest.TestCase):
def test_density_has_expected_properties(self):
mc = splitter_challenge.DiffractiveSplitterComponent(
spec=splitter_challenge.DIFFRACTIVE_SPLITTER_SPEC,
sim_params=LIGHTWEIGHT_SIM_PARAMS,
thickness_initializer=lambda _, thickness: thickness,
density_initializer=lambda _, seed_density: seed_density,
)
params = mc.init(jax.random.PRNGKey(0))
self.assertEqual(params["density"].lower_bound, 0.0)
self.assertEqual(params["density"].upper_bound, 1.0)
self.assertSequenceEqual(params["density"].periodic, (True, True))
self.assertEqual(
params["thickness"].array,
splitter_challenge.DIFFRACTIVE_SPLITTER_SPEC.thickness_grating,
)

def test_can_jit_response(self):
mc = splitter_challenge.DiffractiveSplitterComponent(
spec=splitter_challenge.DIFFRACTIVE_SPLITTER_SPEC,
Expand Down Expand Up @@ -97,8 +81,12 @@ def step_fn(params, state):
@parameterized.expand([[1, 1], [2, 3]])
def test_density_has_expected_attrs(self, min_width, min_spacing):
mc = splitter_challenge.diffractive_splitter(
minimum_width=min_width,
spec=splitter_challenge.DIFFRACTIVE_SPLITTER_SPEC,
sim_params=LIGHTWEIGHT_SIM_PARAMS,
thickness_initializer=lambda _, thickness: thickness,
density_initializer=lambda _, seed_density: seed_density,
minimum_spacing=min_spacing,
minimum_width=min_width,
)
params = mc.component.init(jax.random.PRNGKey(0))

Expand All @@ -113,5 +101,9 @@ def test_density_has_expected_attrs(self, min_width, min_spacing):
self.assertIsNone(params["density"].fixed_solid)
self.assertIsNone(params["density"].fixed_void)

self.assertEqual(params["thickness"].lower_bound, 0.0)
self.assertIsNone(params["thickness"].upper_bound)
self.assertEqual(
params["thickness"].array,
splitter_challenge.DIFFRACTIVE_SPLITTER_SPEC.thickness_grating.array,
)
self.assertEqual(params["thickness"].lower_bound, 0.5)
self.assertEqual(params["thickness"].upper_bound, 1.5)