From 2490f922d606dd4b4e4a6e4152ff8edd20f3c15c Mon Sep 17 00:00:00 2001 From: Casey Wojcik Date: Wed, 13 Nov 2024 11:07:12 +0000 Subject: [PATCH] Validate against nonlinear and modulation in FullyAnisotropicMedium.from_diagonal --- CHANGELOG.md | 1 + tests/test_components/test_medium.py | 32 ++++++++++++++++++++++++++++ tidy3d/components/medium.py | 12 +++++++++++ 3 files changed, 45 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc94d0da5..f6454f51b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Some failing examples in the expressions plugin documentation. - Inaccuracy in transforming gradients from edge to `PolySlab.vertices`. - Bug in `run_async` where an adjoint simulation would sometimes be assigned to the wrong forward simulation. +- Validate against nonlinearity or modulation in `FullyAnisotropicMedium.from_diagonal`. ## [2.7.6] - 2024-10-30 diff --git a/tests/test_components/test_medium.py b/tests/test_components/test_medium.py index 1b9eafaab..2b54ad76b 100644 --- a/tests/test_components/test_medium.py +++ b/tests/test_components/test_medium.py @@ -516,6 +516,38 @@ def test_fully_anisotropic_media(): assert all(np.isin(np.round(perm_d), np.round(np.diag(perm_diag)))) assert all(np.isin(np.round(cond_d), np.round(np.diag(cond_diag)))) + with pytest.raises(ValidationError): + _ = td.FullyAnisotropicMedium.from_diagonal( + xx=td.Medium( + permittivity=2, + nonlinear_spec=td.NonlinearSpec( + models=[ + td.NonlinearSusceptibility(chi3=2), + td.TwoPhotonAbsorption(beta=1.3), + td.KerrNonlinearity(n2=1.3), + ] + ), + ), + yy=td.Medium(permittivity=4), + zz=td.Medium(permittivity=1), + rotation=td.RotationAroundAxis(axis=2, angle=np.pi / 4), + ) + + with pytest.raises(ValidationError): + _ = td.FullyAnisotropicMedium.from_diagonal( + xx=td.Medium(permittivity=2), + yy=td.Medium( + permittivity=4, + modulation_spec=td.ModulationSpec( + permittivity=td.SpaceTimeModulation( + time_modulation=td.ContinuousWaveTimeModulation(freq0=1e12, amplitude=0.02) + ) + ), + ), + zz=td.Medium(permittivity=1), + rotation=td.RotationAroundAxis(axis=2, angle=np.pi / 4), + ) + def test_nonlinear_medium(log_capture): med = td.Medium( diff --git a/tidy3d/components/medium.py b/tidy3d/components/medium.py index 5e60901fc..0d353aca7 100644 --- a/tidy3d/components/medium.py +++ b/tidy3d/components/medium.py @@ -5191,6 +5191,18 @@ def from_diagonal(cls, xx: Medium, yy: Medium, zz: Medium, rotation: RotationTyp Resulting fully anisotropic medium. """ + if any(comp.nonlinear_spec is not None for comp in [xx, yy, zz]): + raise ValidationError( + "Nonlinearities are not currently supported for the components " + "of a fully anisotropic medium." + ) + + if any(comp.modulation_spec is not None for comp in [xx, yy, zz]): + raise ValidationError( + "Modulation is not currently supported for the components " + "of a fully anisotropic medium." + ) + permittivity_diag = np.diag([comp.permittivity for comp in [xx, yy, zz]]).tolist() conductivity_diag = np.diag([comp.conductivity for comp in [xx, yy, zz]]).tolist()