Skip to content

Type checking versus unit checking #3382

@gwr-de

Description

@gwr-de

Cursory (don't beat me) reading of some recent discussions(e.g., #3259, #3381) finds me wondering about whether the unit attribute is to become the "one attribute to rule them all". As the author of the Business Simulation library—a system dynamics library making use of acausal connectors and (notoriously) unconventional types like Amount and AmountRate—I am coming more from the "cybernetic" side of "cyber-physical" modeling. Nonetheless—or even exactly because of this more abstract nature—verification(e.g., will the model result in a solvable set of equations) and validation(e.g., is it meaningful with regard to the intended purpose) of models are important.

In programming, we typically use type to catch errors (statically or dynamically inferred by compilers). And indeed I adapted this principle by having modellers make type choices for causal (mostly output) connectors, which then are used within a component for variables and parameters (the nature of abstract libraries makes it much harder to make these definite choices when designing components).

But setting an explicit quantity attribute for causal connectors has just been called "inconvenient" and "unusual" (#3381) by Hans Olsson. I would argue on the contrary: It could be very convenient to set the type using a replaceable type and the beauty of it is that next to the quantity and unit attribute one can also set displayUnit or min/max in a single choice as to nudge users not to change the unit when what they need is a different displayUnit (e.g, have a derived type `Time_months).

I note that the predefined types Boolean, Integer, String, and even Enumeration all carry the quantity attribute. Maybe it should help us to distinguish fundamental differences even when a unit-system is not applicable:

  String str1(quantity = "Message") = "This is likely a unit error.";
  String str2(quantity = "Documentation") = "This is a basic converter class.";
  String str3(quantity = "Label") = "SuperClass";

  type Color = enumeration(red, yellow, green);

  parameter Color condition(quantity = "Reliability") = Color.red;
  Color signal(quantity = "TrafficSignal"); 

Make no mistake: It is absolutely essential that unit errors are caught (e.g., as to not mix mm and m in Quentins example in #3381). But to me quantity is more likely to catch fundamental equation errors (e.g., there is no meaningful way to propagate magnitudes from one side of an equation to the other), while unit mismatch has a higher likeliness to be an error in the order of magnitudes (e.g., numerical error only).

I had believed that this fundamental quality of quantity was the reason to make it—not the unit attribute—a restriction for connections in the section 9.3 of the specs:

  • In a connection set all variables having non-empty quantity attribute must have the same quantity attribute.

Hans Olssons post suggests that setting the quantity attribute for causal connectors is "inconvenient" to do and that it "cannot even be inferred". But that is exactly what makes quantity so valuable in catching fundamental errors and this even applies to causal connections because if you want to measure Time you should not connect to a sensor that measures Length or Speed.

I would like to stipulate the discussion here with the following:

  1. While unit checking is important, make setting quantity and especially type more convenient for modelers in order to catch fundamental errors in equation formulation and to make it convenient to set multiple attributes at once.

  2. A model may have numerical errors, but still give a solvable set of equations, if quantities match up, "mere" unit errors should give a warning, but not necessarily prevent models from being simulated (Note, that many of the unit error examples given in Infering units for empty unit variable. #3381 are in fact (dimensional) quantity errors mixing say m and s).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions