-
Notifications
You must be signed in to change notification settings - Fork 57
Fix k0 property to return actual value from h #741
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
base: main
Are you sure you want to change the base?
Conversation
|
This was actually the implementation we had until a few weeks ago. We had to move away from it because it generates an inconsistency. In several places, we make the assumption that the state of an element does not depend on the order in which its properties are set (e.g. when we clone elements propagating their expressions, or when building objects from dict/json). Unfortunately the implementation proposed here breaks this assumption. For example: b1 = xt.Bend()
b1.length = 1
b1.angle = 3
b1.k0 = 3 # sets b1.k0_from_h = False
b1.k0_from_h = True
# The resulting element has h=3, k0=3, k0_from_h=True
b2 = xt.Bend()
b2.length = 1
b2.angle = 3
b2.k0_from_h = True
b2.k0 = 3 # sets b1.k0_from_h = False
# The resulting element has h=3, k0=3, k0_from_h=FalseHere For this reason, we need to keep The value of the strength is always available in @property
def k0_value(self):
return self._k0We understand this kind of changes can be annoying. We are doing them now with the objective of stabilizing the API over 2026 (bear with us please :). Already today, there are no further changes foreseen for the magnets API. |
|
I understand your intention and I fully agree that consistent cloning is important. However, I don't understand the example you gave, as this PR does not touch the k0 setter, and the code in the example is not affected by this PR. What you probably meant to say, is that doing
For this, wouldn't the fix in the length setter part of this PR be required to also handle the (rare) case of length 0 ? If so, you could at least cherry pick e46c94b |
|
A possible alternative solution, if you are interested, would be the following: Such a refactoring will probably require a larger effort, let me know if you want to further discuss it or not. |
yes, I agree I will cherry pick that one |
The issue is that when we clone we need to preserve the xdeps expressions (which in general creates a lot of constraints but are a must to handle large machines). The expressions control user-exposed properties and not internal state |
Description
In recent versions,
Bend.k0may return"from_h"as a string. Getting a string where a float is expected likely breaks a lot of code, and requires users to always check for this special string to get the actual value. It's also not necessary, given thatBend.k0_from_hexists and can be used to determine ifk0is set manually or determined byh.Not sure what the rationale behind returning a special string here was, but I propose returning the actual value (the value of
h) in such cases.Also fixes a missing assignment of k0 if h is set to zero
Related: xsuite/xplt@3158107
Checklist
Mandatory:
Optional: