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

Fix test for mixed-ness in Function.sub, and fix Cofunction.sub to match #3961

Merged
merged 3 commits into from
Jan 9, 2025

Conversation

JHopeCollins
Copy link
Member

Fixes #3933

  1. Fixes the test for mixed-ness to check the UFL element rather than the length - this gets the correct answer for "mixed" spaces with only one component.
  2. Updates Cofunction.sub in line with Function.sub.

Copy link

github-actions bot commented Jan 9, 2025

TestsPassed ✅Skipped ⏭️Failed ❌
Firedrake real8162 ran7487 passed675 skipped0 failed

Copy link

github-actions bot commented Jan 9, 2025

TestsPassed ✅Skipped ⏭️Failed ❌
Firedrake complex8156 ran6682 passed1474 skipped0 failed

@JHopeCollins
Copy link
Member Author

JHopeCollins commented Jan 9, 2025

@connorjward @ksagiyam I removed the bounds checking in sub for FunctionSpace, Function, and Cofunction because this check will be done by the subfunctions or _components tuple anyway. However, this is causing the tests to fail because we expect that FunctionSpace.sub will fail if passed a negative index. Given that negative indices are a standard python feature, and here they are doing the usual python thing, is there any reason to keep this restriction?

Copy link
Contributor

@connorjward connorjward left a comment

Choose a reason for hiding this comment

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

LGTM more or less

firedrake/cofunction.py Show resolved Hide resolved
@connorjward
Copy link
Contributor

@connorjward @ksagiyam I removed the bounds checking in sub for FunctionSpace, Function, and Cofunction because this check will be done by the subfunctions or _components tuple anyway. However, this is causing the tests to fail because we expect that FunctionSpace.sub will fail if passed a negative index. Given that negative indices are a standard python feature, and here they are doing the usual python thing, is there any reason to keep this restriction?

I'm happy to permit negative indices and do the Pythonic thing. There is no way that this will break user code to change.

@JHopeCollins
Copy link
Member Author

I'm happy to permit negative indices and do the Pythonic thing. There is no way that this will break user code to change.

Great, I've updated the test so that it checks a negative index that is actually out of bounds.

@ksagiyam
Copy link
Contributor

ksagiyam commented Jan 9, 2025

I think those bound checks are there to give clearer error message:

"Invalid component {i}, not in [0, {bound})"

vs.

IndexError: list index out of range

so I think we should keep them.

@JHopeCollins
Copy link
Member Author

I think those bound checks are there to give clearer error message:

"Invalid component {i}, not in [0, {bound})"

vs.

IndexError: list index out of range

so I think we should keep them.

That's the usual python message for bad indices so personally I'm fine with it. If we really want a slightly more detailed one then we should be catching the IndexError and then adding to the message. But if we think it's really that important then shouldn't we also add this message to subfunctions for FunctionSpace, DualSpace, MixedFunctionSpace, Function, Cofunction, and also anywhere else where we expose a tuple to the user?
It just seems quite heavy handed for the problem it's addressing.

@connorjward
Copy link
Contributor

connorjward commented Jan 9, 2025

I think those bound checks are there to give clearer error message:

"Invalid component {i}, not in [0, {bound})"

vs.

IndexError: list index out of range

so I think we should keep them.

That's the usual python message for bad indices so personally I'm fine with it. If we really want a slightly more detailed one then we should be catching the IndexError and then adding to the message. But if we think it's really that important then shouldn't we also add this message to subfunctions for FunctionSpace, DualSpace, MixedFunctionSpace, Function, Cofunction, and also anywhere else where we expose a tuple to the user? It just seems quite heavy handed for the problem it's addressing.

I agree. If we really cared about this we should probably have something like

try:
  return data[i]
except IndexError as e:
  raise FunctionSpaceIndexOutOfBoundsException("...") from e

but I don't think it's terribly important.


Providing an informative error message is fine. Adding our own bounds checking logic when Python already does the right thing is not.

@ksagiyam
Copy link
Contributor

ksagiyam commented Jan 9, 2025

I see. I am fine with that.

@connorjward connorjward merged commit 4109283 into master Jan 9, 2025
20 checks passed
@connorjward connorjward deleted the JHopeCollins/sub_subfunction_component branch January 9, 2025 19:24
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.

BUG: Function.sub return type depends on number of sub elements in a MixedElement
3 participants