Skip to content

Unit inference and multiplication with literal #3417

@henrikt-ma

Description

@henrikt-ma

This is a unit checking topic affecting #3266 and #3257.

What is the desired unit inference behavior in the following model?

model LiteralTimesInferredUnit
  Real power(unit = "W") = 10;
  constant Real i1(unit = "A") = 5;
  Real i3 = 5;
  Real u1;
  Real u2;
  Real u3;
equation
  power = u1 * i1;
  power = u2 * 5; /* "5" is a literal meant to represent a constant current equal to i1. */
  power = u3 * i3; /* Not getting a unit for u3 seems better than getting "W". */
end LiteralTimesInferredUnit;

To clarify the problem, wouldn't it be unfortunate if we ended up assigning the unit "1" to the literal 5, and then infer that u2 has the same unit as power?

The above example has no declared units in the expression u2 * 5, but the example can also be generalized to efficiency * u2 * 5 or u2 * 5 * efficiency, where efficiency has unit W/W, showing that the problem isn't unique to expressions without any declared units.

The problem makes me ready to give up on the pure bottom-up approach operating on the Modelica expression tree, making me ready to take a more global approach to multiplicative expressions. It would then be possible to say that we can't even infer the unit of a factor subexpression with empty unit as long as the whole multiplicative expression also contains variables whose units still remain to be inferred. That is, unit inference shouldn't be able to make any progress on u2 * 5, meaning that the unit of u2 cannot be inferred. However, if the unit of u2 would be inferred to V from some other equation, there wouldn't be any more variables with units remaining to be inferred in the product, and then the factor 5 with empty unit could safely be assigned the unit "1" to avoid the wildcard effect.

Edit: For comparison, I think we all agree that the expected behavior for u3 and i3 is that none of them get an inferred unit, as the splitting of "W" into two factors would be completely arbitrary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions