Description
Creating a simulation with a monitor (or source, or structure) of the wrong type, generates a truly cryptic error message.
This is a user-facing pydantic failure message. The error below is due to a dumb mistake on my part. But the error message is ...yuck!
Example input
In the example below, I accidentally passed (a list containing) a tuple to the Simulation
constructor.
import tidy3d as td
# The following line has an extra comma at the end which causes tidy3d to print an incomprehensible error message.
structure = td.Structure(geometry=td.Box(center=(1.0, 1.0, 0.0), size=(0.5, 0.5, 0.5)), medium=td.Medium(permittivity=2.0)),
# make simulation
sim = td.Simulation(
size=(1, 1, 1),
grid_spec=td.GridSpec.auto(wavelength=4),
boundary_spec=td.BoundarySpec(
x=td.Boundary.pml(num_layers=10),
y=td.Boundary.periodic(),
z=td.Boundary.pml(num_layers=10),
),
monitors=[monitor],
run_time=1e-12,
)
This results in the following error message:
15:44:13 EDT WARNING: Could not execute validator
'_source_homogeneous_isotropic' because field 'structures' failed
validation.
WARNING: Could not execute validator 'plane_wave_boundaries'
because field 'structures' failed validation.
WARNING: Could not execute validator 'tfsf_boundaries' because
field 'structures' failed validation.
WARNING: Could not execute validator '_structures_not_close_pml'
because field 'structures' failed validation.
WARNING: Could not execute validator 'bloch_boundaries_diff_mnt'
because field 'structures' failed validation.
WARNING: Could not execute validator
'_warn_monitor_mediums_frequency_range' because field 'structures'
failed validation.
WARNING: Could not execute validator
'_projection_monitors_homogeneous' because field 'structures'
failed validation.
WARNING: Could not execute validator
'diffraction_and_directivity_monitor_medium' because field
'structures' failed validation.
WARNING: Could not execute validator '_warn_grid_size_too_small'
because field 'structures' failed validation.
WARNING: Could not execute validator
'_validate_num_lumped_elements' because field 'structures' failed
validation.
WARNING: Could not execute validator 'check_fixed_angle_components'
because field 'structures' failed validation.
---------------------------------------------------------------------------
ValidationError Traceback (most recent call last)
Cell In[7], line 17
14 monitor = td.FieldMonitor(center=(-1.0, 0, 0), size=(0.5, 0, 1), freqs=[100e14], name="field_monitor_1"),
16 # make simulation
---> 17 sim = td.Simulation(
18 size=(1, 1, 1),
19 grid_spec=td.GridSpec.auto(wavelength=4),
20 boundary_spec=td.BoundarySpec(
21 x=td.Boundary.pml(num_layers=10),
22 y=td.Boundary.periodic(),
23 z=td.Boundary.pml(num_layers=10),
24 ),
25 structures=[structure],
26 run_time=1e-12,
27 )
File [~/tidy3d/tidy3d/components/base.py:147](http://localhost:8888/home/jewett/tidy3d/tidy3d/components/base.py#line=146), in Tidy3dBaseModel.__init__(self, **kwargs)
145 """Init method, includes post-init validators."""
146 log.begin_capture()
--> 147 super().__init__(**kwargs)
148 self._post_init_validators()
149 log.end_capture(self)
File [~/anaconda3/envs/tidy3d_frontend_dev/lib/python3.11/site-packages/pydantic/v1/main.py:347](http://localhost:8888/home/jewett/anaconda3/envs/tidy3d_frontend_dev/lib/python3.11/site-packages/pydantic/v1/main.py#line=346), in BaseModel.__init__(__pydantic_self__, **data)
345 values, fields_set, validation_error = validate_model(__pydantic_self__.__class__, data)
346 if validation_error:
--> 347 raise validation_error
348 try:
349 object_setattr(__pydantic_self__, '__dict__', values)
ValidationError: 1 validation error for Simulation
structures -> 0
value is not a valid dict (type=type_error.dict)
Getting rid of the unnecessary warning messages is actually a different issue I reported earlier
If we strip them out, we get the last line of this error message:
ValidationError: 1 validation error for Simulation
structures -> 0
value is not a valid dict (type=type_error.dict)
...which is still cryptic (to me, at least).
What (I think) should happen
I think the Simulation
validators should check that
- all of the entries in the "sources" list are instances of
Source
- all of the entries in the "monitors" list are instances of
Monitor
- all of the entries in the "structures" list are instances of
Structure
...and print out a more comprehensible error message when these assertions fail.
Priority
Low. (User complaints and requests should come first.)
It can wait until after the pydantic v2 refactor.