Skip to content

Commit 74c326a

Browse files
committed
passing: source, structure, time_modulation
1 parent 4bb4fbc commit 74c326a

3 files changed

Lines changed: 40 additions & 40 deletions

File tree

tests/test_components/test_source.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import matplotlib.pyplot as plt
44
import numpy as np
5-
import pydantic.v1 as pydantic
65
import pytest
76
import tidy3d as td
7+
from pydantic import ValidationError
88
from tidy3d.components.source.field import CHEB_GRID_WIDTH, DirectionalSource
99
from tidy3d.exceptions import SetupError
1010

@@ -141,7 +141,7 @@ def test_dipole():
141141
# p.plot(y=2)
142142
# plt.close()
143143

144-
with pytest.raises(pydantic.ValidationError):
144+
with pytest.raises(ValidationError):
145145
_ = td.PointDipole(size=(1, 1, 1), source_time=g, center=(1, 2, 3), polarization="Ex")
146146

147147

@@ -177,11 +177,11 @@ def test_FieldSource():
177177
# plt.close()
178178

179179
# test that non-planar geometry crashes plane wave and gaussian beams
180-
with pytest.raises(pydantic.ValidationError):
180+
with pytest.raises(ValidationError):
181181
_ = td.PlaneWave(size=(1, 1, 1), source_time=g, pol_angle=np.pi / 2, direction="+")
182-
with pytest.raises(pydantic.ValidationError):
182+
with pytest.raises(ValidationError):
183183
_ = td.GaussianBeam(size=(1, 1, 1), source_time=g, pol_angle=np.pi / 2, direction="+")
184-
with pytest.raises(pydantic.ValidationError):
184+
with pytest.raises(ValidationError):
185185
_ = td.AstigmaticGaussianBeam(
186186
size=(1, 1, 1),
187187
source_time=g,
@@ -190,14 +190,14 @@ def test_FieldSource():
190190
waist_sizes=(0.2, 0.4),
191191
waist_distances=(0.1, 0.3),
192192
)
193-
with pytest.raises(pydantic.ValidationError):
193+
with pytest.raises(ValidationError):
194194
_ = td.ModeSource(size=(1, 1, 1), source_time=g, mode_spec=mode_spec)
195195

196196
tfsf = td.TFSF(size=(1, 1, 1), direction="+", source_time=g, injection_axis=2)
197197
_ = tfsf.injection_plane_center
198198

199199
# assert that TFSF must be volumetric
200-
with pytest.raises(pydantic.ValidationError):
200+
with pytest.raises(ValidationError):
201201
_ = td.TFSF(size=(1, 1, 0), direction="+", source_time=g, injection_axis=2)
202202

203203
# s.plot(z=0)
@@ -295,11 +295,11 @@ def check_freq_grid(freq_grid, num_freqs):
295295
check_freq_grid(freq_grid, num_freqs)
296296

297297
# check validators for num_freqs
298-
with pytest.raises(pydantic.ValidationError):
298+
with pytest.raises(ValidationError):
299299
s = td.GaussianBeam(
300300
size=(0, 1, 1), source_time=g, pol_angle=np.pi / 2, direction="+", num_freqs=200
301301
)
302-
with pytest.raises(pydantic.ValidationError):
302+
with pytest.raises(ValidationError):
303303
s = td.AstigmaticGaussianBeam(
304304
size=(0, 1, 1),
305305
source_time=g,
@@ -309,7 +309,7 @@ def check_freq_grid(freq_grid, num_freqs):
309309
waist_distances=(0.1, 0.3),
310310
num_freqs=100,
311311
)
312-
with pytest.raises(pydantic.ValidationError):
312+
with pytest.raises(ValidationError):
313313
s = td.ModeSource(
314314
size=(0, 1, 1),
315315
direction="+",
@@ -355,7 +355,7 @@ def test_custom_source_time():
355355
)
356356
cst = td.CustomSourceTime.from_values(freq0=freq0, fwidth=0.1e12, values=[0, 1], dt=sim.dt)
357357
source = td.PointDipole(center=(0, 0, 0), source_time=cst, polarization="Ex")
358-
sim = sim.updated_copy(sources=[source])
358+
sim = sim.updated_copy(sources=(source,))
359359
assert np.allclose(cst.amp_time(sim.tmesh[0]), [0], rtol=0, atol=ATOL)
360360
assert np.allclose(
361361
cst.amp_time(sim.tmesh[1:]),
@@ -374,18 +374,18 @@ def test_custom_source_time():
374374
cst = td.CustomSourceTime(source_time_dataset=dataset, freq0=freq0, fwidth=0.1e12)
375375
source = td.PointDipole(center=(0, 0, 0), source_time=cst, polarization="Ex")
376376
with AssertLogLevel("WARNING", contains_str="defined over a time range"):
377-
sim = sim.updated_copy(sources=[source])
377+
sim = sim.updated_copy(sources=(source,))
378378

379379
# test normalization warning
380380
with AssertLogLevel("WARNING"):
381381
sim = sim.updated_copy(normalize_index=0)
382382

383383
with AssertLogLevel("WARNING"):
384384
source = source.updated_copy(source_time=td.ContinuousWave(freq0=freq0, fwidth=0.1e12))
385-
sim = sim.updated_copy(sources=[source])
385+
sim = sim.updated_copy(sources=(source,))
386386

387387
# test single value validation error
388-
with pytest.raises(pydantic.ValidationError):
388+
with pytest.raises(ValidationError):
389389
vals = td.components.data.data_array.TimeDataArray([1], coords=dict(t=[0]))
390390
dataset = td.components.data.dataset.TimeDataset(values=vals)
391391
cst = td.CustomSourceTime(source_time_dataset=dataset, freq0=freq0, fwidth=0.1e12)
@@ -411,7 +411,7 @@ def make_custom_field_source(field_ds):
411411
with AssertLogLevel(None):
412412
make_custom_field_source(field_dataset)
413413

414-
with pytest.raises(pydantic.ValidationError):
414+
with pytest.raises(ValidationError):
415415
# repeat some entries so data cannot be interpolated
416416
X2 = [X[0]] + list(X)
417417
n_data2 = np.vstack((n_data[0, :, :, :].reshape(1, Ny, Nz, Nf), n_data))

tests/test_components/test_structure.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import autograd.numpy as anp
33
import gdstk
44
import numpy as np
5-
import pydantic.v1 as pd
65
import pytest
76
import tidy3d as td
7+
from pydantic import ValidationError
88

99

1010
def test_to_gds(tmp_path):
@@ -116,23 +116,23 @@ def test_invalid_polyslab(axis):
116116
_ = td.Structure(geometry=geo1, medium=medium)
117117

118118
geo2 = ps.rotated(np.pi / 4, i)
119-
with pytest.raises(pd.ValidationError):
119+
with pytest.raises(ValidationError):
120120
_ = td.Structure(geometry=geo2, medium=medium)
121121

122122
geo3 = ps.rotated(np.pi / 5, j)
123-
with pytest.raises(pd.ValidationError):
123+
with pytest.raises(ValidationError):
124124
_ = td.Structure(geometry=geo3, medium=medium)
125125

126126
geo4 = ps.rotated(np.pi / 6, (1, 1, 1))
127-
with pytest.raises(pd.ValidationError):
127+
with pytest.raises(ValidationError):
128128
_ = td.Structure(geometry=geo4, medium=medium)
129129

130130
geo5 = td.GeometryGroup(geometries=[ps]).rotated(np.pi / 2, j)
131-
with pytest.raises(pd.ValidationError):
131+
with pytest.raises(ValidationError):
132132
_ = td.Structure(geometry=geo5, medium=medium)
133133

134134
geo6 = td.GeometryGroup(geometries=[ps - box]).rotated(np.pi / 2, i)
135-
with pytest.raises(pd.ValidationError):
135+
with pytest.raises(ValidationError):
136136
_ = td.Structure(geometry=geo6, medium=medium)
137137

138138
geo7 = td.GeometryGroup(geometries=[(ps - box).rotated(np.pi / 4, j)]).rotated(-np.pi / 4, j)
@@ -153,11 +153,11 @@ def test_invalid_polyslab(axis):
153153
_ = td.Structure(geometry=geo10, medium=medium)
154154

155155
geo11 = ps.reflected(n2)
156-
with pytest.raises(pd.ValidationError):
156+
with pytest.raises(ValidationError):
157157
_ = td.Structure(geometry=geo11, medium=medium)
158158

159159
geo12 = td.GeometryGroup(geometries=[ps]).reflected(n2)
160-
with pytest.raises(pd.ValidationError):
160+
with pytest.raises(ValidationError):
161161
_ = td.Structure(geometry=geo12, medium=medium)
162162

163163
geo13 = td.GeometryGroup(geometries=[(ps - box).reflected(n2)]).reflected(n2)
@@ -236,7 +236,7 @@ def test_validation_of_structures_with_2d_materials():
236236
]
237237

238238
for geom in not_allowed_geometries:
239-
with pytest.raises(pd.ValidationError):
239+
with pytest.raises(ValidationError):
240240
_ = td.Structure(geometry=geom, medium=med2d)
241241

242242

tests/test_components/test_time_modulation.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from math import isclose
44

55
import numpy as np
6-
import pydantic.v1 as pydantic
76
import pytest
87
import tidy3d as td
8+
from pydantic import ValidationError
99

1010
from ..utils import cartesian_to_unstructured
1111

@@ -112,22 +112,22 @@ def test_space_modulation():
112112
check_sp_reduction(SP_UNIFORM)
113113

114114
# uniform in phase, but custom in amplitude
115-
with pytest.raises(pydantic.ValidationError):
115+
with pytest.raises(ValidationError):
116116
sp = SP_UNIFORM.updated_copy(amplitude=ARRAY_CMP)
117117

118118
sp = SP_UNIFORM.updated_copy(amplitude=ARRAY)
119119
assert isclose(sp.max_modulation, np.max(ARRAY))
120120
check_sp_reduction(sp)
121121

122122
# uniform in amplitude, but custom in phase
123-
with pytest.raises(pydantic.ValidationError):
123+
with pytest.raises(ValidationError):
124124
sp = SP_UNIFORM.updated_copy(phase=ARRAY_CMP)
125125
sp = SP_UNIFORM.updated_copy(phase=ARRAY)
126126
assert isclose(sp.max_modulation, 1)
127127
check_sp_reduction(sp)
128128

129129
# custom in both
130-
with pytest.raises(pydantic.ValidationError):
130+
with pytest.raises(ValidationError):
131131
sp = SP_UNIFORM.updated_copy(phase=ARRAY_CMP, amplitude=ARRAY_CMP)
132132
sp = SP_UNIFORM.updated_copy(phase=ARRAY, amplitude=ARRAY)
133133
check_sp_reduction(sp)
@@ -171,7 +171,7 @@ def test_modulated_medium():
171171
# permittivity modulated
172172
modulation_spec = MODULATION_SPEC.updated_copy(permittivity=ST)
173173
# modulated permitivity <= 0
174-
with pytest.raises(pydantic.ValidationError):
174+
with pytest.raises(ValidationError):
175175
medium = td.Medium(modulation_spec=modulation_spec)
176176
medium = td.Medium(permittivity=2, modulation_spec=modulation_spec)
177177
assert isclose(medium.n_cfl, np.sqrt(2 - AMP_TIME))
@@ -180,7 +180,7 @@ def test_modulated_medium():
180180
# conductivity modulated
181181
modulation_spec = MODULATION_SPEC.updated_copy(conductivity=ST)
182182
# modulated conductivity <= 0
183-
with pytest.raises(pydantic.ValidationError):
183+
with pytest.raises(ValidationError):
184184
medium = td.Medium(modulation_spec=modulation_spec)
185185
medium_sometimes_active = td.Medium(modulation_spec=modulation_spec, allow_gain=True)
186186
medium = td.Medium(conductivity=2, modulation_spec=modulation_spec)
@@ -191,7 +191,7 @@ def test_modulated_medium():
191191
st_freq2 = ST.updated_copy(
192192
time_modulation=td.ContinuousWaveTimeModulation(freq0=2e12, amplitude=2)
193193
)
194-
with pytest.raises(pydantic.ValidationError):
194+
with pytest.raises(ValidationError):
195195
modulation_spec = MODULATION_SPEC.updated_copy(permittivity=ST, conductivity=st_freq2)
196196
# both modulated, but different space modulation: fine
197197
st_space2 = ST.updated_copy(space_modulation=td.SpaceModulation(amplitude=0.1))
@@ -209,26 +209,26 @@ def test_unsupported_modulated_medium_types():
209209
modulation_spec = MODULATION_SPEC.updated_copy(permittivity=ST)
210210

211211
# PEC cannot be modulated
212-
with pytest.raises(pydantic.ValidationError):
212+
with pytest.raises(ValidationError):
213213
td.PECMedium(modulation_spec=modulation_spec)
214214

215215
# For Anisotropic medium, one should modulate the components, not the whole medium
216-
with pytest.raises(pydantic.ValidationError):
216+
with pytest.raises(ValidationError):
217217
td.AnisotropicMedium(
218218
xx=td.Medium(), yy=td.Medium(), zz=td.Medium(), modulation_spec=modulation_spec
219219
)
220220

221221
# Modulation to fully Anisotropic medium unsupported
222-
with pytest.raises(pydantic.ValidationError):
222+
with pytest.raises(ValidationError):
223223
td.FullyAnisotropicMedium(modulation_spec=modulation_spec)
224224

225225
# 2D material
226-
with pytest.raises(pydantic.ValidationError):
226+
with pytest.raises(ValidationError):
227227
drude_medium = td.Drude(eps_inf=2.0, coeffs=[(1, 2), (3, 4)])
228228
td.Medium2D(ss=drude_medium, tt=drude_medium, modulation_spec=modulation_spec)
229229

230230
# together with nonlinear_spec
231-
with pytest.raises(pydantic.ValidationError):
231+
with pytest.raises(ValidationError):
232232
td.Medium(
233233
permittivity=2,
234234
nonlinear_spec=td.NonlinearSusceptibility(chi3=1),
@@ -250,10 +250,10 @@ def test_supported_modulated_medium_types(unstructured, z):
250250
assert mat_p.is_time_modulated
251251
assert isclose(mat_p.n_cfl, np.sqrt(2 - AMP_TIME))
252252
# too much modulation resulting in eps_inf < 0
253-
with pytest.raises(pydantic.ValidationError):
253+
with pytest.raises(ValidationError):
254254
mat = mat_p.updated_copy(eps_inf=1.0)
255255
# conductivity modulation
256-
with pytest.raises(pydantic.ValidationError):
256+
with pytest.raises(ValidationError):
257257
mat = mat_p.updated_copy(modulation_spec=modulation_both_spec)
258258
mat = mat_p.updated_copy(modulation_spec=modulation_both_spec, allow_gain=True)
259259
check_med_reduction(mat)
@@ -269,10 +269,10 @@ def test_supported_modulated_medium_types(unstructured, z):
269269
assert mat_c.is_time_modulated
270270
assert isclose(mat_c.n_cfl, np.sqrt(2 - AMP_TIME))
271271
# too much modulation resulting in eps_inf < 0
272-
with pytest.raises(pydantic.ValidationError):
272+
with pytest.raises(ValidationError):
273273
mat = mat_c.updated_copy(permittivity=permittivity * 0.5)
274274
# conductivity modulation
275-
with pytest.raises(pydantic.ValidationError):
275+
with pytest.raises(ValidationError):
276276
mat = mat_c.updated_copy(modulation_spec=modulation_both_spec)
277277
mat = mat_c.updated_copy(modulation_spec=modulation_both_spec, allow_gain=True)
278278
check_med_reduction(mat_c)

0 commit comments

Comments
 (0)