Teaching Modelica Medical Non-SI Units
Asked Answered
J

3

7

I want to use non-SI units in a Modelica model in a medical context. I am using Modelica in Dymola. Being an engineer I prefer the SI units. But my model parameters are sourced from medical publications and I want to avoid conversion mistakes. Medical units also simplify communication with doctors.

For example, the unit "mmHg" (millimetres of mercury) is used instead of "Pa" for pressure. Pressure variables are defined in the model as follows:

type Pressure_mmHg = Real (final quantity="Pressure", final unit="mmHg")

Translating the model for simulation yields the following warning for every single non-SI variable in the model (~300 warnings in total):

Could not decode the unit symbol "mmHg" appearing in the unit string "mmHg"

If changes to the model cause a new warning it is near impossible to detect. Therefore I need to teach Modelica the unit "mmHg".

My current knowledge on the topic:
- Modelica Language Specification 3.3, Chapter 19 Unit Expressions: "It is possible to support user defined unit symbols." However the document does not elaborate on this.
- users may define display units (Real attribute "displayUnit") for use in the simulation GUI, as long as a conversion factor is defined in the "displayunit.mos" file in the Dymola directory as follows:

defineUnitConversion("Pa", "mmHg", 760/101325);

This is not an option because it diminishes portability of the model. It has to be performed manually on every computer that runs the model and requires administrator rights (which I don't have).

Thanks in advance for your help, it is much appreciated!

EDIT (might be helpful to anyone coming across this in the future):
Versions at time of posting: Modelica Standard Library 3.2, Dymola Version 2014 (64-bit) 2013-03-25

I forgot to add the following to my current knowledge on the topic: The Physiolibrary 2.1.1 defines displayUnits for the GUI. For coding, the authors use the following trick:

type Pressure =  Modelica.SIunits.Pressure(displayUnit="mmHg", nominal=133.322387415);

This way Modelica handles variables of type Pressure as having the unit Pascal (N/m2), but divides every value entered in code by the given nominal.

CORRECTION:
In this section, I previously stated that the Physiolibrary uses the Real attribute nominal for unit conversion. This is NOT the case. Thank you Marek Matejak (author of the Physiolibrary) for correcting me. The sole purpose of the attribute nominal is scaling of the numerical problem for stability reasons.

Joint answered 16/7, 2014 at 8:54 Comment(2)
I don't really have an answer for you, but you will find a related discussion on the Modelica Trac site.Ardys
@MichaelTiller: this is very helpful and does answer my question in a way. According to the related discussion it seems to be impossible to define a relationship between user defined units and a SI base unit. At least I now know that I can stop searching the internet for a non-existent solution:) Thanks a lot!Joint
F
4

@Jay_At_Play, you may want to take another look at the Modelica Trac site that @MichaelTiller shared. I just discussed an approach that may help. However, as a warning, is is nonstandard and not directly compatible with the Modelica Standard Library.

(I wanted to add this to the comments under the original question, but I don't have the reputation points.)

Frankhouse answered 25/7, 2014 at 20:30 Comment(7)
Thanks for the update, much appreciated. I like your approach:) It's definitely an option for future implementations! Lacking reputation points, I cannot mark your answer as useful. Sorry for the late reply, I've been busy with exam preparation lately.Joint
I downloaded your FCSys package today to look at your unit implementation in detail and discovered what you mean by "not directly compatible". Model checking in Dymola yields the error "Missing base class Modelica.Icons.TypesPackage". Can I simply delete the "extends" lines referring to your extension of MSL? If the missing packages are a requirement for correct unit implementation, might you be able to send me copy please? Thanks again for your help! I really like your clean unit implementation based on international standards:)Joint
...also in your documentation of FCSys.Quantities you write "Methods for unit checking have been established [Mattsson2008, Broman2008, Aronsson2009] and can, in theory, be applied to dimension checking instead". What do you mean by "in theory"? Is unit checking deactivated as described by Adrian Pop, or does Dymola's unit checking now function as dimension checking?Joint
Thanks, @Jay_At_Play. That error ("Missing base class Modelica.Icons.TypesPackage") is probably due to the version of the MSL (requires 3.2.1). Yes, you can delete that extends clause; it's only for graphics. What I meant by "not directly compatible" is that you usually need to create adapters when using MSL components with components using the new approach. Those adapters will have code like this: v = v_SI*U.V; where v is the potential using the new approach, v_SI is the voltage in SI from MSL, and U.V is the volt. I'll try to share a unit/quantity-only package soon.Frankhouse
In regards to "Methods for unit checking ... applied to dimension checking...", I mean that I haven't been able to get this to work in Dymola. Dimension checking follows the same rules as unit checking and is actually easier because everything is written in terms of the base dimensions (no derived dimensions or relationships like "Pa" = "N/m2" to encode). However, Dymola expects SI units, not symbols for the dimensions, it the unit attribute, so it doesn't work. In the meantime, I deactivate unit checking in Dymola (Advanced.CheckUnits = false;).Frankhouse
Thank you very much again, you have been an amazing help! I'll keep a lookout for the unit/quantity package on your github.Joint
Sure! I just posted a stand-alone units/quantities package: QCalc. It doesn't have any dependencies (not even MSL), so it should work without trouble as long as you turn off unit checking. The documentation is rough, so I haven't released it and shared it to the Modelica libraries page yet. Please share any suggestions or comments (good or bad).Frankhouse
L
3

As far as I know there is no good way to do this in a portable way.

The Modelica specification does not say how to handle user defined units so each tool does it (or not) its own way.

The best way I think is to do as you did by defining types in your model or library even if you have warnings in some tools.

In Dymola you might get rid of the warnings via:

Advanced.CheckUnits = false

in the command window.

Lenee answered 17/7, 2014 at 2:49 Comment(0)
P
3

Modelica Medical Non-SI (display) Units in Dymola:

  1. To have a suport for medical non-SI (display) units try to extend the Dymola file "C:\Program Files*\Dymola*\insert\displayunit.mos" such as https://github.com/MarekMatejak/Physiolibrary/blob/master/Physiolibrary/Resources/DisplayUnits/displayunit.mos

  2. definition of parameter or variable should have the attribute unit always set to SI unit and displayUnit can be everything what have defined conversion in Dymola file "displayunit.mos":

for example:

parameter Real systolicPressure(final unit="Pa", displayUnit="mmHg") = 16000;

parameter Real diastolicPressure(final unit="Pa", displayUnit="torr") = 10000;

Modelica will always calculate the models in SI units, but the parameter dialogs and output plots in Dymola will be in display units. So be carefull - every value in text code must be in SI unit.

Nominals are not directly connected with conversions.They are only for relative tolerances in numerical mathemathics behind.

If you find any mistake in Physiolibrary display units, please do not hesitate to report that. Thank you!

Pseudonymous answered 27/8, 2014 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.