Skip to content

Commit

Permalink
Adding test for custom medium
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-flex committed May 8, 2024
1 parent 3647e5c commit d42bb24
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion tests/test_components/test_medium.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import matplotlib.pyplot as plt
import tidy3d as td
from tidy3d.exceptions import ValidationError, SetupError
from ..utils import assert_log_level, log_capture, AssertLogLevel
from ..utils import assert_log_level, AssertLogLevel
from typing import Dict

MEDIUM = td.Medium()
Expand Down Expand Up @@ -780,3 +780,39 @@ def test_lumped_resistor():
voltage_axis=2,
name="R",
)


def test_custom_medium(log_capture):
Nx, Ny, Nz, Nf = 4, 3, 1, 1
X = np.linspace(-1, 1, Nx)
Y = np.linspace(-1, 1, Ny)
Z = [0]
freqs = [2e14]
n_data = np.ones((Nx, Ny, Nz, Nf))
n_dataset = td.ScalarFieldDataArray(n_data, coords=dict(x=X, y=Y, z=Z, f=freqs))

def create_mediums(n_dataset):
## Three equivalent ways of defining custom medium for the lens

# define custom medium with n/k data
_ = td.CustomMedium.from_nk(n_dataset, interp_method="nearest")

# define custom medium with permittivity data
eps_dataset = td.ScalarFieldDataArray(n_dataset**2, coords=dict(x=X, y=Y, z=Z, f=freqs))
_ = td.CustomMedium.from_eps_raw(eps_dataset, interp_method="nearest")

# define each component of permittivity via "PermittivityDataset"
eps_xyz_dataset = td.PermittivityDataset(
eps_xx=eps_dataset, eps_yy=eps_dataset, eps_zz=eps_dataset
)
_ = td.CustomMedium(eps_dataset=eps_xyz_dataset, interp_method="nearest")

create_mediums(n_dataset=n_dataset)
assert_log_level(log_capture, None)

with pytest.raises(pydantic.ValidationError):
# repeat some entries so data cannot be interpolated
X2 = [X[0]] + list(X)
n_data2 = np.vstack((n_data[0, :, :, :].reshape(1, Ny, Nz, Nf), n_data))
n_dataset2 = td.ScalarFieldDataArray(n_data2, coords=dict(x=X2, y=Y, z=Z, f=freqs))
create_mediums(n_dataset=n_dataset2)

0 comments on commit d42bb24

Please sign in to comment.