flexcompute / tidy3d

Fast electromagnetic solver (FDTD) at scale.
https://docs.flexcompute.com/projects/tidy3d/en/latest/
GNU Lesser General Public License v2.1
179 stars 40 forks source link

Field projection monitor intersects structures outside of the simulation domain and still causes error #1902

Open tomflexcompute opened 1 month ago

tomflexcompute commented 1 month ago

Field projection monitors such as FieldProjectionAngleMonitor can not intersect any structures whose medium is not background. Currently the validator seems to check outside of the simulation domain as well. For example, in a 2D simulation, I can have a cylinder of length 2 in the direction where the simulation domain has 0 size. Then if I have a 3D field projection monitor that intersects the cylinder at z=0.5, it will cause an error.

I think this error should be avoided since there is no intersection in the simulation domain and thus the result should still be correct.

momchil-flex commented 1 month ago

@QimingFlex could you take this?

QimingFlex commented 1 month ago

Will work on it shortly.

QimingFlex commented 1 month ago

In theory, a 2-D simulation represents an infinitely extended system along the 0-dim. Currently in Tidy3D, if we ensure that both the monitor and geometry sizes along the 0-dim are set to td.inf, we can avoid such errors, which aligns well with the theoretical concept. I propose implementing a simulation-level validator that would be triggered if users specify a non-td.inf size along the 0-dimension for either the geometry or monitor in a 2D simulation. This approach would help maintain consistency with the theoretical model. What are your thoughts on this? @momchil-flex @tomflexcompute

tomflexcompute commented 1 month ago

Thanks @QimingFlex . I think it's a good idea but has some practical complications. First issue is it will break some backward compatibility. Users who set an arbitrary value for length in 2D simulation will error if we implement this. More importantly it might not work in some cases. For example if user creates a box using the from_bounds method, they can not directly set the size to td.inf. For PolySlab I don't know if it's possible to set the min and max to td.inf either.

QimingFlex commented 1 month ago

Thanks @tomflexcompute ! I was also puzzled by from_bounds as this generally can't create structures that end by PMLs. If users want to achieve that, they would need use center and size.

momchil-flex commented 1 month ago

Yeah @QimingFlex in many cases we have to sacrifice defining things "rigorously" for the convenience of the user. In this particular case, and in general, only structures defined withing the simulation domain matter, for convenience things that lie outside of it can be whatever and we still handle it well. For example, think again of the grating coupler example where a user may be able to easily switch between a 2D and a 3D simulation. It is very convenient to be able to use the same structures and just change the size of the simulation domain, and everything just works.

Thus for this issue we should just fix the validator to maybe first take the intersection of the field projection monitor with the simulation domain, before doing the homogeneous medium check.

QimingFlex commented 1 month ago

Thanks @momchil-flex. The grating coupler is a good example of such implementation. The validator basically checks if the monitor intersects with any structures, and won't raise an error if the monitor size along the 0-dim is larger than the structure. I think we can ignore the size along the 0-dimn for that intersection check.