ekj
ekj

Reputation: 81

Modelica - How to use units in equations

How can i use units directly in equations in Modelica? Is that possible at all? Simple example, a parameter, that should be made dependant (as default value) to another parameter of a different unit. In the example below it would give a unit warning (naturally). How do i say i just want the nominal value of the variable and not the value+unit ?

model customspringdamper
  import SI = Modelica.SIunits;
  parameter SI.TranslationalSpringConstant c =100;
  parameter SI.TranslationalDampingConstant d= 0.01*c;
  ... < rest of stuff > ...
end customspringdamper;

Of course i could define another parameter with a unit and value 1 but this feels more laborous than neccessary?

Upvotes: 1

Views: 637

Answers (1)

Hans Olsson
Hans Olsson

Reputation: 12507

I can see three ways:

  1. Declare the parameter as Real without the unit: parameter Real c =100; You lose the unit-checking, but it doesn't seem that you use it.
  2. Use constants to convert the value to a different unit:

import SI = Modelica.SIunits; parameter SI.TranslationalSpringConstant c= 100; parameter SI.TranslationalDampingConstant d= 0.01*(c/unitSpring)*unitDamping; constant SI.TranslationalSpringConstant unitSpring=1; constant SI.TranslationalDampingConstant unitDamping=1;

Tools should be able to simplify the code to remove those constants. However, you circumvent unit-checking which makes your code more error-prone.

There are some cases where it is legitimate to remove units in this way - but not in cases like this.

  1. Basically the same as 2, but you put the units on 0.01; and give it an actual physical meaning:

    import SI = Modelica.SIunits; parameter SI.TranslationalSpringConstant c= 100; parameter SI.Time SpringTime=0.01; parameter SI.TranslationalDampingConstant d= SpringTime*c;

Upvotes: 2

Related Questions