55import gdstk
66import matplotlib .pyplot as plt
77import numpy as np
8- import pydantic .v1 as pydantic
98import pytest
109import tidy3d as td
1110from matplotlib .testing .compare import compare_images
11+ from pydantic import ValidationError
1212from tidy3d .components import simulation
1313from tidy3d .components .scene import MAX_GEOMETRY_COUNT , MAX_NUM_MEDIUMS
1414from tidy3d .components .simulation import MAX_NUM_SOURCES
@@ -282,7 +282,7 @@ def test_sim_size():
282282 s ._validate_size ()
283283
284284 # check too many time steps
285- with pytest .raises (pydantic . ValidationError ):
285+ with pytest .raises (ValidationError ):
286286 s = td .Simulation (
287287 size = (1 , 1 , 1 ),
288288 run_time = 1e-7 ,
@@ -395,7 +395,7 @@ def test_validate_monitor_simulation_frequency_range():
395395
396396
397397def test_validate_bloch_with_symmetry ():
398- with pytest .raises (pydantic . ValidationError ):
398+ with pytest .raises (ValidationError ):
399399 td .Simulation (
400400 size = (1 , 1 , 1 ),
401401 run_time = 1e-12 ,
@@ -422,7 +422,7 @@ def test_validate_normalize_index():
422422 )
423423
424424 # negative normalize index
425- with pytest .raises (pydantic . ValidationError ):
425+ with pytest .raises (ValidationError ):
426426 td .Simulation (
427427 size = (1 , 1 , 1 ),
428428 run_time = 1e-12 ,
@@ -431,7 +431,7 @@ def test_validate_normalize_index():
431431 )
432432
433433 # normalize index out of bounds
434- with pytest .raises (pydantic . ValidationError ):
434+ with pytest .raises (ValidationError ):
435435 td .Simulation (
436436 size = (1 , 1 , 1 ),
437437 run_time = 1e-12 ,
@@ -445,7 +445,7 @@ def test_validate_normalize_index():
445445 )
446446
447447 # normalize by zero-amplitude source
448- with pytest .raises (pydantic . ValidationError ):
448+ with pytest .raises (ValidationError ):
449449 td .Simulation (
450450 size = (1 , 1 , 1 ),
451451 run_time = 1e-12 ,
@@ -513,7 +513,7 @@ def test_validate_plane_wave_boundaries():
513513 )
514514
515515 # angled incidence plane wave with PMLs / absorbers should error
516- with pytest .raises (pydantic . ValidationError ):
516+ with pytest .raises (ValidationError ):
517517 td .Simulation (
518518 size = (1 , 1 , 1 ),
519519 run_time = 1e-12 ,
@@ -560,7 +560,7 @@ def test_validate_zero_dim_boundaries():
560560 pol_angle = 0.0 ,
561561 )
562562
563- with pytest .raises (pydantic . ValidationError ):
563+ with pytest .raises (ValidationError ):
564564 td .Simulation (
565565 size = (1 , 1 , 0 ),
566566 run_time = 1e-12 ,
@@ -598,7 +598,7 @@ def test_validate_symmetry_boundaries():
598598 z = td .Boundary .pml (),
599599 ),
600600 )
601- with pytest .raises (pydantic . ValidationError ):
601+ with pytest .raises (ValidationError ):
602602 td .Simulation (
603603 size = (1 , 1 , 1 ),
604604 symmetry = (0 , 1 , 0 ),
@@ -688,7 +688,7 @@ def test_max_geometry_validation():
688688 medium = td .Medium (permittivity = 2.0 ),
689689 ),
690690 ]
691- with pytest .raises (pydantic . ValidationError , match = f" { MAX_GEOMETRY_COUNT + 2 } " ):
691+ with pytest .raises (ValidationError , match = f" { MAX_GEOMETRY_COUNT + 2 } " ):
692692 _ = td .Simulation (size = (1 , 1 , 1 ), run_time = 1 , grid_spec = gs , structures = not_fine )
693693
694694
@@ -1198,7 +1198,7 @@ def test_sim_plane_wave_error():
11981198 )
11991199
12001200 # with non-transparent box, raise
1201- with pytest .raises (pydantic . ValidationError ):
1201+ with pytest .raises (ValidationError ):
12021202 _ = td .Simulation (
12031203 size = (1 , 1 , 1 ),
12041204 medium = medium_bg ,
@@ -1208,15 +1208,15 @@ def test_sim_plane_wave_error():
12081208 )
12091209
12101210 # raise with anisotropic medium
1211- with pytest .raises (pydantic . ValidationError ):
1211+ with pytest .raises (ValidationError ):
12121212 _ = td .Simulation (
12131213 size = (1 , 1 , 1 ),
12141214 medium = medium_bg_diag ,
12151215 sources = [src ],
12161216 boundary_spec = td .BoundarySpec .all_sides (boundary = td .Periodic ()),
12171217 )
12181218
1219- with pytest .raises (pydantic . ValidationError ):
1219+ with pytest .raises (ValidationError ):
12201220 _ = td .Simulation (
12211221 size = (1 , 1 , 1 ),
12221222 medium = medium_bg_full ,
@@ -1282,7 +1282,7 @@ def test_sim_monitor_homogeneous():
12821282 )
12831283
12841284 # with non-transparent box, raise
1285- with pytest .raises (pydantic . ValidationError ):
1285+ with pytest .raises (ValidationError ):
12861286 _ = td .Simulation (
12871287 size = (1 , 1 , 1 ),
12881288 medium = medium_bg ,
@@ -1524,7 +1524,7 @@ def test_diffraction_medium():
15241524 pol_angle = - 1.0 ,
15251525 )
15261526
1527- with pytest .raises (pydantic . ValidationError ):
1527+ with pytest .raises (ValidationError ):
15281528 _ = td .Simulation (
15291529 size = (2 , 2 , 2 ),
15301530 structures = [box_cond ],
@@ -1534,7 +1534,7 @@ def test_diffraction_medium():
15341534 boundary_spec = td .BoundarySpec .all_sides (boundary = td .Periodic ()),
15351535 )
15361536
1537- with pytest .raises (pydantic . ValidationError ):
1537+ with pytest .raises (ValidationError ):
15381538 _ = td .Simulation (
15391539 size = (2 , 2 , 2 ),
15401540 structures = [box_disp ],
@@ -1687,7 +1687,7 @@ def test_num_mediums(monkeypatch):
16871687 boundary_spec = td .BoundarySpec .all_sides (boundary = td .Periodic ()),
16881688 )
16891689
1690- with pytest .raises (pydantic . ValidationError ):
1690+ with pytest .raises (ValidationError ):
16911691 structures .append (
16921692 td .Structure (geometry = td .Box (size = (1 , 1 , 1 )), medium = td .Medium (permittivity = i + 2 ))
16931693 )
@@ -1708,7 +1708,7 @@ def test_num_sources():
17081708
17091709 _ = td .Simulation (size = (5 , 5 , 5 ), run_time = 1e-12 , sources = [src ] * MAX_NUM_SOURCES )
17101710
1711- with pytest .raises (pydantic . ValidationError ):
1711+ with pytest .raises (ValidationError ):
17121712 _ = td .Simulation (size = (5 , 5 , 5 ), run_time = 1e-12 , sources = [src ] * (MAX_NUM_SOURCES + 1 ))
17131713
17141714
@@ -1771,7 +1771,7 @@ def _test_names_default():
17711771
17721772
17731773def test_names_unique ():
1774- with pytest .raises (pydantic . ValidationError ):
1774+ with pytest .raises (ValidationError ):
17751775 _ = td .Simulation (
17761776 size = (2.0 , 2.0 , 2.0 ),
17771777 run_time = 1e-12 ,
@@ -1790,7 +1790,7 @@ def test_names_unique():
17901790 boundary_spec = td .BoundarySpec .all_sides (boundary = td .Periodic ()),
17911791 )
17921792
1793- with pytest .raises (pydantic . ValidationError ):
1793+ with pytest .raises (ValidationError ):
17941794 _ = td .Simulation (
17951795 size = (2.0 , 2.0 , 2.0 ),
17961796 run_time = 1e-12 ,
@@ -1813,7 +1813,7 @@ def test_names_unique():
18131813 boundary_spec = td .BoundarySpec .all_sides (boundary = td .Periodic ()),
18141814 )
18151815
1816- with pytest .raises (pydantic . ValidationError ):
1816+ with pytest .raises (ValidationError ):
18171817 _ = td .Simulation (
18181818 size = (2.0 , 2.0 , 2.0 ),
18191819 run_time = 1e-12 ,
@@ -1830,7 +1830,7 @@ def test_mode_object_syms():
18301830 g = td .GaussianPulse (freq0 = 1e12 , fwidth = 0.1e12 )
18311831
18321832 # wrong mode source
1833- with pytest .raises (pydantic . ValidationError ):
1833+ with pytest .raises (ValidationError ):
18341834 _ = td .Simulation (
18351835 center = (1.0 , - 1.0 , 0.5 ),
18361836 size = (2.0 , 2.0 , 2.0 ),
@@ -1842,7 +1842,7 @@ def test_mode_object_syms():
18421842 )
18431843
18441844 # wrong mode monitor
1845- with pytest .raises (pydantic . ValidationError ):
1845+ with pytest .raises (ValidationError ):
18461846 _ = td .Simulation (
18471847 center = (1.0 , - 1.0 , 0.5 ),
18481848 size = (2.0 , 2.0 , 2.0 ),
@@ -1896,7 +1896,7 @@ def test_tfsf_symmetry():
18961896 injection_axis = 2 ,
18971897 )
18981898
1899- with pytest .raises (pydantic . ValidationError ):
1899+ with pytest .raises (ValidationError ):
19001900 _ = td .Simulation (
19011901 size = (2.0 , 2.0 , 2.0 ),
19021902 grid_spec = td .GridSpec .auto (wavelength = td .C_0 / 1.0 ),
@@ -1996,7 +1996,7 @@ def test_tfsf_boundaries():
19961996 )
19971997
19981998 # cannot cross any boundary in the direction of injection
1999- with pytest .raises (pydantic . ValidationError ):
1999+ with pytest .raises (ValidationError ):
20002000 _ = td .Simulation (
20012001 size = (2.0 , 2.0 , 0.5 ),
20022002 grid_spec = td .GridSpec .auto (wavelength = 1.0 ),
@@ -2005,7 +2005,7 @@ def test_tfsf_boundaries():
20052005 )
20062006
20072007 # cannot cross any non-periodic boundary in the transverse direction
2008- with pytest .raises (pydantic . ValidationError ):
2008+ with pytest .raises (ValidationError ):
20092009 _ = td .Simulation (
20102010 center = (0.5 , 0 , 0 ), # also check the case when the boundary is crossed only on one side
20112011 size = (0.5 , 0.5 , 2.0 ),
@@ -2516,7 +2516,7 @@ def test_sim_volumetric_structures(tmp_path):
25162516 assert np .isclose (sim .volumetric_structures [1 ].medium .xx .permittivity , 2 , rtol = RTOL )
25172517
25182518 # test simulation.medium can't be Medium2D
2519- with pytest .raises (pydantic . ValidationError ):
2519+ with pytest .raises (ValidationError ):
25202520 sim = td .Simulation (
25212521 size = (10 , 10 , 10 ),
25222522 structures = [],
@@ -2532,16 +2532,16 @@ def test_sim_volumetric_structures(tmp_path):
25322532 )
25332533
25342534 # test 2d medium is added to 2d geometry
2535- with pytest .raises (pydantic . ValidationError ):
2535+ with pytest .raises (ValidationError ):
25362536 _ = td .Structure (geometry = td .Box (center = (0 , 0 , 0 ), size = (1 , 1 , 1 )), medium = box .medium )
2537- with pytest .raises (pydantic . ValidationError ):
2537+ with pytest .raises (ValidationError ):
25382538 _ = td .Structure (geometry = td .Cylinder (radius = 1 , length = 1 ), medium = box .medium )
2539- with pytest .raises (pydantic . ValidationError ):
2539+ with pytest .raises (ValidationError ):
25402540 _ = td .Structure (
25412541 geometry = td .PolySlab (vertices = [(0 , 0 ), (1 , 0 ), (1 , 1 )], slab_bounds = (- 1 , 1 )),
25422542 medium = box .medium ,
25432543 )
2544- with pytest .raises (pydantic . ValidationError ):
2544+ with pytest .raises (ValidationError ):
25452545 _ = td .Structure (geometry = td .Sphere (radius = 1 ), medium = box .medium )
25462546
25472547 # test warning for 2d geometry in simulation without Medium2D
@@ -3141,7 +3141,7 @@ def test_advanced_material_intersection():
31413141 struct1 = td .Structure (geometry = td .Box (size = (1 , 1 , 1 ), center = (0 , 0 , 0.5 )), medium = pair [0 ])
31423142 struct2 = td .Structure (geometry = td .Box (size = (1 , 1 , 1 ), center = (0 , 0 , - 0.5 )), medium = pair [1 ])
31433143 # this pair cannot intersect
3144- with pytest .raises (pydantic . ValidationError ):
3144+ with pytest .raises (ValidationError ):
31453145 sim = sim .updated_copy (structures = [struct1 , struct2 ])
31463146
31473147 for pair in incompatible_pairs :
@@ -3168,7 +3168,7 @@ def test_num_lumped_elements():
31683168 lumped_elements = [resistor ] * MAX_NUM_MEDIUMS ,
31693169 run_time = 1e-12 ,
31703170 )
3171- with pytest .raises (pydantic . ValidationError ):
3171+ with pytest .raises (ValidationError ):
31723172 _ = td .Simulation (
31733173 size = (5 , 5 , 5 ),
31743174 grid_spec = grid_spec ,
@@ -3190,15 +3190,15 @@ def test_validate_lumped_elements():
31903190 lumped_elements = [resistor ],
31913191 )
31923192 # error for 1D/2D simulation with lumped elements
3193- with pytest .raises (pydantic . ValidationError ):
3193+ with pytest .raises (ValidationError ):
31943194 td .Simulation (
31953195 size = (1 , 0 , 3 ),
31963196 run_time = 1e-12 ,
31973197 grid_spec = td .GridSpec .uniform (dl = 0.1 ),
31983198 lumped_elements = [resistor ],
31993199 )
32003200
3201- with pytest .raises (pydantic . ValidationError ):
3201+ with pytest .raises (ValidationError ):
32023202 td .Simulation (
32033203 size = (1 , 0 , 0 ),
32043204 run_time = 1e-12 ,
@@ -3331,15 +3331,15 @@ def test_validate_sources_monitors_in_bounds():
33313331 )
33323332
33333333 # check that a source at y- simulation domain edge errors
3334- with pytest .raises (pydantic . ValidationError ):
3334+ with pytest .raises (ValidationError ):
33353335 sim = td .Simulation (
33363336 size = (2 , 2 , 2 ),
33373337 run_time = 1e-12 ,
33383338 grid_spec = td .GridSpec (wavelength = 1.0 ),
33393339 sources = [mode_source ],
33403340 )
33413341 # check that a monitor at y+ simulation domain edge errors
3342- with pytest .raises (pydantic . ValidationError ):
3342+ with pytest .raises (ValidationError ):
33433343 sim = td .Simulation (
33443344 size = (2 , 2 , 2 ),
33453345 run_time = 1e-12 ,
@@ -3468,7 +3468,7 @@ def test_fixed_angle_sim():
34683468
34693469 assert sim ._is_fixed_angle
34703470
3471- with pytest .raises (pydantic . ValidationError ):
3471+ with pytest .raises (ValidationError ):
34723472 _ = sim .updated_copy (
34733473 boundary_spec = td .BoundarySpec (
34743474 x = td .Boundary .pml (),
@@ -3477,25 +3477,25 @@ def test_fixed_angle_sim():
34773477 )
34783478 )
34793479
3480- with pytest .raises (pydantic . ValidationError ):
3480+ with pytest .raises (ValidationError ):
34813481 _ = sim .updated_copy (med = td .Medium (conductivity = 0.001 ))
34823482
34833483 anisotropic_med = td .FullyAnisotropicMedium (permittivity = [[2 , 0 , 0 ], [0 , 1 , 0 ], [0 , 0 , 3 ]])
3484- with pytest .raises (pydantic . ValidationError ):
3484+ with pytest .raises (ValidationError ):
34853485 _ = sim .updated_copy (structures = [sphere .updated_copy (medium = anisotropic_med )])
34863486
3487- with pytest .raises (pydantic . ValidationError ):
3487+ with pytest .raises (ValidationError ):
34883488 _ = sim .updated_copy (sources = [source , source ])
34893489
3490- with pytest .raises (pydantic . ValidationError ):
3490+ with pytest .raises (ValidationError ):
34913491 _ = sim .updated_copy (
34923492 structures = [sphere .updated_copy (medium = td .Medium (conductivity = - 0.1 , allow_gain = True ))]
34933493 )
34943494
3495- with pytest .raises (pydantic . ValidationError ):
3495+ with pytest .raises (ValidationError ):
34963496 _ = sim .updated_copy (monitors = [td .FieldTimeMonitor (size = [td .inf , td .inf , 0 ], name = "time" )])
34973497
3498- with pytest .raises (pydantic . ValidationError ):
3498+ with pytest .raises (ValidationError ):
34993499 _ = sim .updated_copy (monitors = [td .FluxTimeMonitor (size = [td .inf , td .inf , 0 ], name = "time" )])
35003500
35013501 nonlinear_med = td .Medium (
@@ -3507,7 +3507,7 @@ def test_fixed_angle_sim():
35073507 num_iters = 20 ,
35083508 ),
35093509 )
3510- with pytest .raises (pydantic . ValidationError ):
3510+ with pytest .raises (ValidationError ):
35113511 _ = sim .updated_copy (structures = [sphere .updated_copy (medium = nonlinear_med )])
35123512
35133513 time_modulated_med = td .Medium (
@@ -3518,7 +3518,7 @@ def test_fixed_angle_sim():
35183518 )
35193519 ),
35203520 )
3521- with pytest .raises (pydantic . ValidationError ):
3521+ with pytest .raises (ValidationError ):
35223522 _ = sim .updated_copy (structures = [sphere .updated_copy (medium = time_modulated_med )])
35233523
35243524
0 commit comments