Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved warnings with Medium2D #1097

Merged
merged 1 commit into from
Aug 29, 2023
Merged

Conversation

caseyflex
Copy link
Contributor

@caseyflex caseyflex commented Aug 25, 2023

This adds a warning when a 2D geometry doesn't have a 2D medium.
@tylerflex it seems to break the adjoint tests, which has 0-thickness structures in a 2D sim. Is it possible to change these thicknesses to some other value, like td.inf? This may be more robust anyway without changing the intended behavior.

@tomflexcompute , this should also fix the plot_eps warning

@tylerflex
Copy link
Collaborator

tylerflex commented Aug 25, 2023

@caseyflex I'm a bit confused. This seems to break both the regular and 2D adjoint tests with an error

E               pydantic.v1.error_wrappers.ValidationError: 1 validation error for JaxStructure
E               medium -> size -> 0
E                 ensure this value is greater than or equal to 0 (type=value_error.number.not_ge; limit_value=0)

Mediums shouldn't have sizes so that's strange.

Also, the adjoint tests don't have 2D geometries in them (I just verified on the regular test_adjoint_pipeline)

It seems this warning was also triggered in the tests as I see the warning message about 2D structure detected with no 2D medium.

Something seems strange here, I'm not sure what to make of it.

@@ -99,14 +101,24 @@ def eps_diagonal(self, frequency: float, coords: Coords) -> Tuple[complex, compl
@pydantic.validator("medium", always=True)
def _check_2d_geometry(cls, val, values):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't help but wonder if this can just be simplified a lot

geom = values.get("geometry")
if geom is None:
    raise SetupError("'Structure.geometry' did not pass validation, can't check 2D medium".)

if isinstance(val, Medium2D) and not geom.is_2D:
    raise SetupError("Structure with a `Medium2D` medium needs a `2D` geometry.")

if geom.is_2D and not isinstance(val, Medium2D):
    zero_axis = geom.bounding_box.size.index(0)
    log.warning("found a geometry with < 3 dimensions not associated with a Medium2D ...")

with a simple property Geometry.is_2D that is implemented in each of the geometry subclasses?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatives:

  • add a Geometry.is_3D -> bool method and check if not geom.is_3D
  • add a Geometry.ndims -> int and check if geom.ndims < 3

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the complicated logic is a bit necessary, since we are checking different things. On the one hand, we need to check that if the medium is a Medium2D, the geometry must be a supported type and it must be 2D. Here, _normal_2dmaterial gives a detailed error message depending on the geometry and setup. On the other hand, if the bounding box has zero size in a certain direction and the medium is not Medium2D, it is helpful to give a warning. I'll add some comments to clarify this but I'm inclined to leave the logic as-is

tidy3d/components/structure.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@tylerflex tylerflex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, the comments do help, looks good to me, thanks.

@momchil-flex momchil-flex merged commit 47fcf41 into pre/2.4 Aug 29, 2023
16 checks passed
@momchil-flex momchil-flex deleted the casey/issue2dgeometry branch August 29, 2023 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warn if user tries to make a geometry with 0 size along dim(s)
4 participants