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

Field projection validators #1288

Merged
merged 1 commit into from
Dec 6, 2023
Merged

Field projection validators #1288

merged 1 commit into from
Dec 6, 2023

Conversation

shashwat-sh
Copy link
Contributor

  • Added a validator to warn if projecting "backwards"
  • Added a validator to warn if projection distance is small but far_field_approx is True

@shashwat-sh shashwat-sh force-pushed the shash/warnings branch 2 times, most recently from 7188e6a to 5bf2a09 Compare December 5, 2023 03:41
@shashwat-sh shashwat-sh self-assigned this Dec 5, 2023
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.

looks good but the test log capture should be cleared between blocks

run_time=1e-12,
monitors=[monitor_n2f],
)
assert_log_level(log_capture, "WARNING")
Copy link
Collaborator

Choose a reason for hiding this comment

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

note, if you want to test that the next block of code is triggering a new warning, you should log_capture.clear() before entering that block. Otherwise it will still capture the first warning. As written, it is not testing that a new warning is logged with each block of code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I've made the change

"""Warn if field projection observation points are behind surface projection monitors."""
# This validator is in simulation.py rather than monitor.py because volume monitors are
# eventually converted to their bounding surface projection monitors, in which case we
# do not want this validator to be triggered.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmmm. Is there some pydantic magic than can allow us to choose whether to trigger this validator if it were in the monitors @tylerflex ? I don't know if we want to go this way but it may be a bit better in the long run, e.g. consider a standalone field projection solver like we've been thinking of implementing. Well, for that we could also move this validator to validators.py and import it both here and in other solvers.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure exactly what kind of magic we need to implement. But I'd say in general let's use less magic. Especially since if we do a pydantic v2 port we'll have to rethink it

Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess I'm just asking if there's a way to dynamically disable some validator when initializing a model. But we could live without it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Besides just an if statement in the validator that returns if triggered? We could maybe make it a post init validation?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure I understand, what I'm asking is if I can do something like Model(**model_dict) and add some kwarg or set a config somehow that would skip that particular validator. This is for when we need to initialize something intrenally without triggering the warnign.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think anything you pass to the models init method will be present in the values dict in the validator. So you could add a flag in there. But then you'd have to pop it from values at some point

Copy link
Collaborator

Choose a reason for hiding this comment

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

One of these things that should work in theory but might not when trying it out but worth a shot

Copy link
Collaborator

Choose a reason for hiding this comment

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

If there's no pydantic native way then probably not worth it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess let's just merge this as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, I can do that now


center = np.array(monitor.center) - np.array(monitor.local_origin)
pts = [np.array(i) for i in [x, y, z]]
normal_displacement = pts[normal_ind] - center[normal_ind]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is doing it this way easier (and also is it equivalent?) to checking if theta < 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The angle defined within the monitor is relative to the local origin, so we'd need to do some conversions anyway because theta < 0 in the local system may still not be "backwards" in the global system, relative to the monitor's position, depending on proj_distance. So I decided to always convert to the global system in Cartesian coords and treat all three cases in the same way - not sure if it's the best way

Copy link
Contributor Author

Choose a reason for hiding this comment

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

another way could be to always convert to spherical coords in the global system and then check if the global theta < 0

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh ok I see.

@shashwat-sh shashwat-sh merged commit 2156b42 into pre/2.5 Dec 6, 2023
14 checks passed
@shashwat-sh shashwat-sh deleted the shash/warnings branch December 6, 2023 20:21
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.

None yet

3 participants