如何在 Modelica 中使用模型中的单元属性? [英] How to make use of the unit attribute within a model in Modelica?

查看:87
本文介绍了如何在 Modelica 中使用模型中的单元属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Modelica 确实将测量单位(例如 SI 单位和非 SI 单位)存储为变量的属性.以下是非 SI 单位的示例:

Modelica does store units of measurement (e.g. SI units and Non-SI units) as an attribute with regard to a variable. Here is an example for a Non-SI-unit:

type Time_months = Real( quantity = "Time", unit = "mo", displayUnit = "months" )

对于经济学模型来说,以秒为单位给出利率是相当尴尬的,我想编写一个相当通用的单位转换函数,允许转换时间单位.因此,理想情况下,转换为另一个时基的函数应该使用三个输入和一个输出:

Since for models in economics it will be rather akward to give rates in seconds, I would like to write a rather general unit conversion function that will allow to convert units of time. So ideally a function to convert to another time base should work with three inputs and one output:

input Real timeValue "the value of time to be converted";
input String timeBaseA "the time base for timeValue, e.g. \"mo\" ";
input String timeBaseB "the time base to convert to, e.g. \"yr\" ";
output Real convertedTimeValue "the result of the conversion";

问题

如果我们假设某个时间值的变量已经具有特定的单位属性(例如mo"),那么在模型中使用该元信息是有意义的.

问题 1:如何在模型中访问 unit 等元信息?

Question 1: How can meta information like unit be accessed within a model?

理想情况下,以下内容会很棒:

Ideally something like the following would be great:

String timeBaseA := timeValue.unit;

String timeBaseA := getUnit( timeValue ) "some function to read unit information";

问题 2:如何在函数内分配像 unit 这样的元信息?

Question 2: How can meta information like unit be assigned within a function?

在示例中,我们当然希望以正确的单位时间返回输出值.所以理想情况下,我们希望:

In the example we would of course like to return the output value with the correct unit of time. So ideally we would like to have:

output Real convertedTime( quantity = "Time", unit = strTimeBaseB )

不幸的是,由于可变性不同,使用 input 会引起错误:unit 属性应该具有恒定可变性,但是输入变量具有参数可变性.(使用函数 - 这会很好 - 也因同样的原因而失败.)

Unfortunately, using an input will give rise to an error as the variability is different: The unit attribute should have constant variability but the input variable has parameter variability. (Using a function - which would be nice - also fails for the same reason.)

推荐答案

关于问题 1:

我从未使用过 Wolfram SystemModeler,但 Modelica 语言规范 3.4 在第 4.8 章(预定义类型和类)中说:

Regarding Question 1:

I have never used Wolfram SystemModeler, but the Modelica Language Specification 3.4 says in chapter 4.8 (Predefined Types and Classes):

预定义变量类型(实数、整数、布尔值、字符串)的属性...不能使用点表示法访问,并且不受方程和算法部分的约束.

The attributes of the predefined variable types (Real, Integer, Boolean, String) ... cannot be accessed using dot notation, and are not constrained by equations and algorithm sections.

关于问题 2:

我认为只能在从文字或最终参数声明时定义变量的单位 - 至少这是我在 Dymola 中观察到的.

Regarding Question 2:

I think it is only possible to define the unit of a variable on declaration from a literal or from a final parameter - at least this is what I observed in Dymola.

您可以将操作员记录用于您的任务.这将允许您以秒为单位存储时间,并在值使用时将其转换为所需的时间.

You could use operator records for your task. This will allow you to store the time in seconds and convert it to what ever needed when the value comes to use.

运算符记录允许您定义多个函数来创建它们、比较或添加它们、转换为字符串等.

Operator records allow you to define several function to create them, compare or add them, convert to String, etc.

看下面的简单例子,其中定义了一个操作记录Time,它可以用两个不同的构造函数从秒或天创建,并可以转换为带天或秒的字符串

See the brief example below, where a operator record Time is defined, which can be created with two different constructor functions from seconds or days and can be converted to Strings with day or seconds

operator record Time
  Integer s "Second";

  encapsulated operator 'constructor'
    import Time;

    function from_s
      input Integer s "Seconds";
      output Time t(s=s);
    algorithm 
    end from_s;

    function from_d
      input Integer d "Days";
      output Time t(s=d*24*3600);
    algorithm 
    end from_d;
  end 'constructor';

  encapsulated operator 'String' "Convert Time to string"
    import Time;

    function formated
      input Time t;
      input String format = "s" annotation(choices(choice="s" "seconds", choice="d" "days"));
      output String str;

    algorithm 
      if format == "d" then
        str :=String(t.s/24/3600);
      else
        str :=String(t.s);
      end if;
    end formated;
  end 'String';

  encapsulated operator function '==' "Compare time records"
    import Time;
    input Time t1;
    input Time t2;
    output Boolean result "= t1 == t2";
  algorithm 
    result := t1.s == t2.s;
  end '==';

end Time;

用法:

import Modelica.Utilities.Streams.print

t1 = Time(d=12)  // create record using day constructor
t2 = Time(s=3600*24*2)  // create record using second constructor

print(String(t1, format="s"))  // prints 1036800
print(String(t1, format="d"))  // prints 12
print(String(t2, format="s"))  // prints 172800
print(String(t2, format="d"))  // prints 2

请参阅 Modelica Spec 3.4 第 14 章重载运算符";详情.

See Modelica Spec 3.4 Chapter 14 "Overloaded Operators" for details.

注意:这是用 Dymola 2019 测试的,而不是用 Wolfram SystemModeler

Note: This was tested with Dymola 2019, not with Wolfram SystemModeler

这篇关于如何在 Modelica 中使用模型中的单元属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆