-
Notifications
You must be signed in to change notification settings - Fork 169
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 unit error in GenericHystTellinenEverett #4111
base: master
Are you sure you want to change the base?
Fix unit error in GenericHystTellinenEverett #4111
Conversation
Please add a description, and address the authors / library officers: @ThomasBoedrich and Johannes Ziske. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks OK
Note that it may be a different error instead (leading to unit errors); and based on the bad unit-casts in #4053 I think we need to investigate it more.
Even though unit-casting is sometimes needed, to me it smells too much like hiding the error without investigating it fully. But I don't have the detailed knowledge to investigate that (if I had copies of the references I could look into it). However, to me it looks suspicious that after this change all uses of M will be multiplied by unitT. For the units I could track down the discussion to #1620 - and since the unitH in that code was incorrect I now more strongly believe that something else should be changed as indicated above. |
Modelica/Magnetic/FluxTubes/Shapes/HysteresisAndMagnets/GenericHystTellinenEverett.mo
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unit-checking is supposed to help us find and guide us towards correcting errors.
Here unit-checking has detected an error, and instead of understanding the error this PR just seems to silence the check-error. (My estimate is that 0.001 is just a multiplicative small factor in this example - similar to the other similar models; I don't see that it means multiplying the unit-less number M with 0.001 Tesla as some universal small magnetic field - (about 3 times earth's magnetic field)).
That is not how we are supposed to use unit-checking.
It could instead be that it's a mistake in the formula, that M has the wrong unit, or something else.
You are right. The intention of the PR is to fix the error, but since I am not an expert in this domain I am not surprised that my initially suggested "fix" isn't deemed the right one. If a domain expert steps forward and suggests the proper way of fixing the unit error, I'll happily go with that fix instead of mine. |
@HansOlsson, as far as I can tell, this change is in the same spirit as the other PRs that fix unit errors, which the previous MAP-Lib meeting decided to go ahead with. Would you please unblock? |
As previously stated it's not: This model involves a material with
and also an equation with unit-casting inside it: So we already have one unit-cast, and several variables with unit="1"; combined this indicates some other underlying unit-error(s), and this PR is just trying to patch it over without investigating the actual error. I don't know what the actual unit-error is, in contrast to #4053 and #4158 (for related models) where it was fairly straightforward to find the error; but I doubt that all of M, P1, P2 should really have unit="1". Trying M.unit="T" gives:
and P1.unit="T", P2.unit="T"; which is consistent with only one unit-cast - but it's still not perfect. It could also be that there's some other error. |
Why don't we do both:
|
Because it doesn't fix anything, and it might be the wrong change. It's like adding pointer-casts in C code instead of looking up what the correct arguments should be and using them; basically silencing the compiler instead of improving the model - which makes it more likely that actual issue go uncorrected. It's just a distraction from finding the actual issue. Someone needs to look up what the units should be in this model, and preferably add a reference that explains them. |
See #4241 for an attempt to actually clean up these models. Note that there might be errors in the equations (instead of/in addition to) these unit-errors. I simply don't know, and I couldn't find the information in the references. |
With #4241 representing the bright future when MSL uses some Modelica specification which hasn't been released yet, shouldn't we avoid the potential mess by just hacking the equations in the simplest way (represented by the this PR) to make them unit consistent in the meantime? |
One conclusion in #4241 is that the line that is equation that this PR tries to unit-correct is just plain wrong (but it is just an epsilon so it doesn't matter that much), and thus the proposal is to replace it with another equation in d0dbf4b - and that specific change doesn't require any new features. That PR represent what unit-checking is supposed to do: help us find errors in equations. |
Well, that commit by itself doesn't look like a mess to me. Could you please turn that into a separate PR that would replace this one? |
@ThomasBoedrich resigned as library officer two weeks ago, so the ultimate decision about this PR rests with the new library officers @AHaumer and @christiankral. @christiankral already approved this. @AHaumer what do you think? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok for me
To be clear the line is: As far as I understand the slight change in eps doesn't matter in practice. Yes, I could do that as soon as someone could review #4241 and see that it long-term is the correct solution (avoiding unit-casting completely in the models, but instead having square root of T as unit which will have to wait for Modelica Language 3.7). |
As suggested by Hans. Co-authored-by: Hans Olsson <HansOlsson@users.noreply.github.com>
Requesting new reviews, since the recent change from @HansOlsson changes the PR entirely. |
To be honest: I need some time to dive into that topic. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me it seems to be a good short-term fix until taking care of #4241
Edit: See #4111 (comment). |
But this short-term fix is just saying: "The equation (and units all over the model) are wrong here as far as we can see, but instead of fixing that we instead make the unit-checker shut up by multiplying with unitT." |
Sorry @HansOlsson, I missed that @AHaumer pushed 6df5d99d. (I wasn't aware that others could push to my fork like this.) I take back my suggestion to merge. |
I'm against the solution changing the value of |
@HansOlsson I don't understand why equation and units all over the model should be wrong? |
Now that I see that you are speaking in the role of the library officer, I see things differently. As the current state of the PR is the same as the original quick-fix by me, I was afraid of pushing for this to be merged after there had been a discussion resulting in a different solution. Now, if you as the library officer see a problem with that solution, I have no reason to object to going back to the original short-term quick-fix. |
Let me try to explain: I agree that a lot of variables are declared as Real without any proper explanation, and we don't really have a good explanation for some of the units for the variables that have unit="1" either (like M). That is a minor warning sign. Additionally we have several multiplications with Multiplying by such an unit-factor makes sense in s-parametrizations (where we don't really compare the different values), if the value are part of an external interface (where units are missing), and in some other odd cases - but I cannot see that it makes sense here. And #4053 show that related models got it wrong. To me this indicates that the multiplication by The only thing that doesn't make sense with those units is the eps-formula, but I believe that the previous model was just plain wrong - (in an un-important way) and eps was just intended to be a small value, and the exact value really doesn't seem to matter (for the example models). It may be that the conclusion was wrong, and that the units should be something else, or there is some other issue else hiding, - but my point remain that we need to understand the formulas, and what the units should be, and not just multiply by |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This just hides the underlying unit-errors as explained in the longer explanation - at least as far as I can tell.
I understand this point, but I think of it as this PR changing a bad model with unit inconsistencies into a bad model with consistent units. Regardless of this change, we have #4241 as a reminder that the model needs improvement, but in the meantime we could at least have a model that shouldn't fail to build for users that opt in for treating unit inconsistencies as errors (either according to a future specification, or according to tool-specific rules if there is no specification to align with). |
But the "consistent units" are, as far as I can tell, based on incorrect units and I would only describe it as "passing unit checks" - not as having "consistent units". This mean that there's a risk that those bad units will spread more; and after correcting the underlying unit errors we will have to update this equation once more. |
Maybe we could look into a workaround for #4241 while waiting for rational exponents in the unit expression syntax. We could try the following pattern:
|
@henrikt-ma smart idea. I tried something similar with Dymola 2024x but without success:
I suppose we have to be patient:
|
You can get it to work, sort of, in Dymola:
when activating: We will see if we can the other variant to work for the next release. However, for constants it may be slightly messier. |
Yes, the idea doesn't work for constants, at least not if they are handled according to current plans for standardization. Actually, I shouldn't even have proposed this as a workaround with parameters, as I consider it quite problematic to allow expressions with empty unit to be implicitly associated with a unit determined by unit inference. In my opinion the way to do it would require changing the parameterization so that the user specifies a non-negative value in tesla, while the original parameter is made finally equal to the square root. Unfortunately, this would be a backwards incompatible change, as code modifying the original parameter would become illegal. |
@HansOlsson how could I check if this weird unit-cast is succesfull?
@henrikt-ma yes you're right: This is a dirty hack, it's much better to declare the units explicitly. |
see later discussions
There are three type of hysteresis models:
Regarding the tables: |
I think this is a topic way ahead of the current discussions around unit checking. Ideally, would have liked this to work:
However I'm afraid the definition of A workaround would be to define the two columns separately:
Of course, this makes it very inconvenient to edit the H-B data as sequence of pairs. |
Unfortunately it seems that support was added after Dymola 2024x was released.
That required additional logic - since previously Dymola didn't deduce the unit of |
@HansOlsson and @henrikt-ma I have some questions:
I've already started to check the units in all hysteresis models. As far as I see:
|
There can only be tool-specific answers at the moment, plus anecdotal mentions of various ideas for standardization being discussed – both within and outside the language group. At Wolfram, we are convinced that functions shouldn't escape unit checking, and the basic principles for how it could be defined see to follow naturally from the general principles of our unit checking machinery. We are working on a proposal for standardized unit checking based on the general principles of our machinery, but as we are aware of the poor status of unit consistency inside existing functions we plan to not propose that function bodies are considered in a first version of standardized unit checking. I hope, however, that there will still be tool-specific ways to opt in for also considering the bodies of functions. In the proposal we are working on, declared units on function inputs and outputs will be considered.
Yes, the longer discussion about how to do it right would fit better in #4241; here we should just decide about what to do short-term.
It is great to hear that you have initiated this work! Hopefully it will not be that complicated to also complete it – unless we hit the road block of not being able to make needed backwards-incompatible changes, such as replacing a |
Dymola has the possibility, but as default doesn't.
Dymola normally checks that. However, there were built-in functions that slipped by earlier - see below. Regarding functions (and classes in general) it is important to note that each call of the function is checked somewhat separately (so the same function can add "kg", "A" or "m" as long as they are different calls).
That was already corrected in #4053 replacing the units in those declarations.
I agree that would be good. |
Fixes unit error in binding equation: