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

feat: add validation for units #2335

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

rahul-flex
Copy link
Contributor

Added setup errors for grid spacing dl and frequency (attached).

dl_error freq_error

Copy link

@yaugenst-flex
Copy link
Collaborator

We encountered users specifying the wrong units in their simulation setup, so the motivation here is to introduce range-based validators and link to the relevant docs page to make sure users can quickly identify and correct unit issues. While the validators impose strict bounds, the idea is not to necessarily allow those values but simply catch setups that are clearly off by orders of magnitude. We had a stab at selecting bounds that would cover that but if you could double check @momchil-flex and @tomflexcompute?

@momchil-flex
Copy link
Collaborator

I think it makes sense, the main concern is RF freqs, it seems like the warning will currently be raised for a wavelength larger than 1 meter, which is probably fine for all applications we're currently targeting? But may be close. But probably for a center frequency it's fine.

I remember we added a similar validator at some point, maybe only for the mode solver... But I can't find it now. @weiliangjin2021 do you remember?

Copy link
Collaborator

@yaugenst-flex yaugenst-flex left a comment

Choose a reason for hiding this comment

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

Thanks @rahul-flex this looks good! Left some minor comments. Make sure to also add tests for these validators and a changelog entry (under the "Changed" section probably).

Comment on lines 281 to 284
"""
Ensure 'dl' is within a physically sensible range to catch common
unit mistakes.
"""
Copy link
Collaborator

Choose a reason for hiding this comment

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

This fits on a single line, can remove everything after sensible range.

Ensure 'dl' is within a physically sensible range to catch common
unit mistakes.
"""
if not (1e-6 <= val <= 5e4):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably 1e4 is fine here? Not that it matters much.

"""
if not (1e-6 <= val <= 5e4):
raise SetupError(
f"Uniform grid spacing 'dl' is {val} µm."
Copy link
Collaborator

Choose a reason for hiding this comment

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

Space missing here: µm." -> µm. "

Comment on lines 104 to 105
min_freq = C_0 / (1e6)
max_freq = C_0 / (1e-6)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can remove the parantheses here

Comment on lines 100 to 103
"""
Ensure 'freq0' is within a physically sensible range to catch
common unit mistakes.
"""
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same comment as above, I'd put this on a single line.

@yaugenst-flex
Copy link
Collaborator

it seems like the warning will currently be raised for a wavelength larger than 1 meter, which is probably fine for all applications we're currently targeting? But may be close.

Hmm I guess that also means we might want dl > 1cm? @momchil-flex

Copy link
Contributor

@tomflexcompute tomflexcompute left a comment

Choose a reason for hiding this comment

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

This is nice but I think 1m wavelength is actually a bit too restrictive for some use cases like radar and long range communication, even though these use cases are not common within our current user base.

@yaugenst-flex
Copy link
Collaborator

@tomflexcompute I guess that would put us in the ~100m wavelength range?

@tomflexcompute
Copy link
Contributor

@tomflexcompute I guess that would put us in the ~100m wavelength range?

yeah potentially. It's probably fine for now but in the future if we have more users from RF and microwave it might be a problem.

@weiliangjin2021
Copy link
Collaborator

We have a validator here checking the minimal frequency. The lowest frequency is 1e5 Hz (here).

It's applied to FreqMonitor here, and source here.

@rahul-flex
Copy link
Contributor Author

Thanks for the comments @yaugenst-flex @momchil-flex @tomflexcompute @weiliangjin2021. Should we make upper bound of dl > 1cm (say 10 cm?) and lower bound of frequency: 1e5 Hz?

@weiliangjin2021
Copy link
Collaborator

Should we make upper bound of dl > 1cm (say 10 cm?) and lower bound of frequency: 1e5 Hz?

I wonder if we really need a validator for grid size. If the grid size is too large, you'll get just one grid in that dimension, and it should be easy to spot the issue? And if the grid size is too small, I think there is already a validator that checks the total number of grid points below a threshold value.

Now if we go for a validator for grid size, with lower bound frequency 1e5 Hz, corresponding to wavelength 300m, the upper bound of grid size will be 30 m.

@rahul-flex
Copy link
Contributor Author

And if the grid size is too small, I think there is already a validator that checks the total number of grid points below a threshold value.

@weiliangjin2021 Thanks for the bound values.
I’m not aware of an existing validator for grid size. Currently, the error for fine grid
seems to be due to high memory allocation, as reported by a user ( screenshot attached).
Screenshot 2025-03-25 at 7 32 44 PM

@weiliangjin2021
Copy link
Collaborator

And if the grid size is too small, I think there is already a validator that checks the total number of grid points below a threshold value.

@weiliangjin2021 Thanks for the bound values. I’m not aware of an existing validator for grid size. Currently, the error for fine grid seems to be due to high memory allocation, as reported by a user ( screenshot attached). Screenshot 2025-03-25 at 7 32 44 PM

See

def _validate_size(self) -> None:
"""Ensures the simulation is within size limits before simulation is uploaded."""
num_comp_cells = self.num_cells / 2 ** (np.sum(np.abs(self.symmetry)))
if num_comp_cells > MAX_GRID_CELLS:
raise SetupError(
f"Simulation has {num_comp_cells:.2e} computational cells, "
f"a maximum of {MAX_GRID_CELLS:.2e} are allowed."
)
. But it seems it errorred already before reaching here.

@rahul-flex
Copy link
Contributor Author

. But it seems it errorred already before reaching here.

Ohh, I see! Thanks!

Comment on lines +98 to +113
@pydantic.validator("freq0", always=True)
def _validate_freq0(cls, val):
"""
Ensure 'freq0' is within a physically sensible range.
"""
min_freq = C_0 / 3e9
max_freq = C_0 / 1e-6
if not (min_freq <= val <= max_freq):
raise SetupError(
f"Pulse central frequency 'freq0' is {val:.3e} Hz, which corresponds "
f"to a free-space wavelength of {C_0 / val:.3e} µm. "
"Check your units! For more info on Tidy3D units, see: "
"https://docs.flexcompute.com/projects/tidy3d/en/latest/faq/docs/faq/What-are-the-units-used-in-the-simulation.html"
)
return val

Copy link
Collaborator

Choose a reason for hiding this comment

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

As @weiliangjin2021 pointed out, there already is a frequency validator which we could reuse. Would it make sense to also add an upper frequency bound to that validator?

@yaugenst-flex
Copy link
Collaborator

And if the grid size is too small, I think there is already a validator that checks the total number of grid points below a threshold value.

@weiliangjin2021 Thanks for the bound values. I’m not aware of an existing validator for grid size. Currently, the error for fine grid seems to be due to high memory allocation, as reported by a user ( screenshot attached). Screenshot 2025-03-25 at 7 32 44 PM

See

def _validate_size(self) -> None:
"""Ensures the simulation is within size limits before simulation is uploaded."""
num_comp_cells = self.num_cells / 2 ** (np.sum(np.abs(self.symmetry)))
if num_comp_cells > MAX_GRID_CELLS:
raise SetupError(
f"Simulation has {num_comp_cells:.2e} computational cells, "
f"a maximum of {MAX_GRID_CELLS:.2e} are allowed."
)

. But it seems it errorred already before reaching here.

I think the size validator is not quite the same though, or at least if there is an opportunity to catch a bad dl earlier that'd still be beneficial. Actually we could also just change the dl validator have no upper but only a lower bound.

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.

5 participants