英文:
Parameter without default value in a Modelica model
问题
Context: Modelica 3.6 "Undefined modification" feature
在Modelica语言版本3.6的发布公告中(请参见Modelica Association Newsletter 2023-01),有一个新的语法Undefined modification:
> 未定义修改 - 这允许将参数设置为未定义,例如,强制模型的用户明确设置它。https://specification.modelica.org/master/inheritance-modification-and-redeclaration.html#removing-modifiers-break
然而,新的语法,如果我理解正确,只是用于在继承模型时(使用extends
关键字)删除参数值的设置。
Question
现在看到这个新的语法,我突然想到 我不知道如何指定模型参数通常不应该具有默认值(即,强制模型的用户指定其值)。
例如,这里是一个值为param
没有被设置的简短模型:
model ParameterDefaultValue "一个具有不应该有默认值的参数的模型"
parameter Real param "不应该有默认值的参数";
equation
end ParameterDefaultValue;
即使没有指定值,这个模型 仍然 可以进行模拟,使用默认值0.0(在OpenModelica 1.20中),尽管会有一个翻译警告:[ParameterDefaultValue: 2:3-2:73]: 参数param没有值,并且在初始化期间被固定(fixed=true),使用可用的起始值(start=0.0)作为默认值。
更具体地说,我想知道是否可能使模拟 失败 而不是发出警告。从某种意义上说,这意味着模型ParameterDefaultValue不能直接进行模拟,只有在被扩展或简单地组合成较大的模型之后才能进行模拟:
model UpperModel
ParameterDefaultValue parameterDefaultValue(param = 1);
equation
end UpperModel;
与部分模型的关系
我发现我的问题类似于partial
关键字的用法(在其他面向对象语言中称为抽象类),不同的是 partial
是:
- 用于不足的模型(不足的方程),而不是缺少参数值
- “部分性”通过扩展模型来解决,而如上所示,值的缺失可以在实例化时提供(而禁止实例化部分模型)。
其他语言中的默认值
另外,与像Python这样的命令式编程语言相比,函数参数的默认值是一个可选特性,而不是默认的:
def f(a, b=1):
return a+b
f(2) # OK → 返回3
f() # 失败,抛出TypeError:f()缺少1个必需的位置参数:“a”
英文:
Context: Modelica 3.6 "Undefined modification" feature
In the release announcement of the Modelica language version 3.6 (see Modelica Association Newsletter 2023-01), there is a new syntax Undefined modification:
> Undefined modification - this allows making a parameter undefined, e.g., to force users of the model to explicitly set it. https://specification.modelica.org/master/inheritance-modification-and-redeclaration.html#removing-modifiers-break
However, the new syntax, if I understand it correctly, is only to remove the parameter value set the superclass when inheriting a model (with the extends
keyword).
Question
Now seeing this new syntax it just occurred to me that I don't know how to specify that a model parameter shouldn't have a default value in general (i.e. force the user of the model to specify its value).
For example, here is a short model where the value of param
is not set:
model ParameterDefaultValue "A model with one parameter which shouldn't have a default value"
parameter Real param "parameter which shouldn't have a default value";
equation
end ParameterDefaultValue;
Even if the value is not specified, this model does simulate, using a default value of 0.0 (in OpenModelica 1.20), albeit with a Translation warning: [ParameterDefaultValue: 2:3-2:73]: Parameter param has no value, and is fixed during initialization (fixed=true), using available start value (start=0.0) as default value.
More specifically, I wonder if it's possible for the simulation to fail rather than emit a warning. In a sense, it means that the model ParameterDefaultValue wouldn't be directly simulable, only after having been extended or simply composed is a larger model:
model UpperModel
ParameterDefaultValue parameterDefaultValue(param = 1);
equation
end UpperModel;
Relation to partial models
I see that my question resembles the use case of the partial
keyword (abstract classes in other object oriented languages), except that partial
is:
- for underspecified models (not enough equations), not the lack of parameter value
- the "partialness" is resolved by extending the model, whereas, as illustrated above, the lack of value can be provided at instantiation (whereas instantiating partial models is forbidden)
Default values in other languages
Also, comparing to an imperative programming languages like Python, the default values of parameters in a function is an optional feature, not a default:
def f(a, b=1):
return a+b
f(2) # OK → returns 3
f() # fails with TypeError: f() missing 1 required positional argument: 'a'
答案1
得分: 3
I believe that OpenModelica version may be relying on another item that was changed in Modelica 3.6 (due to https://github.com/modelica/ModelicaSpecification/pull/3226 ).
Specifically the messages says it uses the available start-value 0.0, and that seems to refer to the earlier definition of Real that did have a start-value of 0.0 https://specification.modelica.org/maint/3.5/class-predefined-types-and-declarations.html#real-type
Technically the start-value should only be used as default if there was a modifier, but that was a bit unclear and the Pull Request removed the default 0.0 and clarified the use. (There might be additional changes related to this.)
Added:
Basically for parameters we can think of three cases:
- Nothing,
parameter Real p;
- Start-default,
parameter Real p(start=1);
- Value,
parameter Real p=1;
The idea is that later ones are "better", but before 3.6 the specification only specified that 2 should give a warning, but didn't really specify anything for 1. I believe that so many relied on the check in Dymola that no-one actually checked whether it was specified.
Note that case 2 was added in 3.3, and updated in https://github.com/modelica/ModelicaSpecification/issues/2136
英文:
I believe that OpenModelica version may be relying on another item that was changed in Modelica 3.6 (due to https://github.com/modelica/ModelicaSpecification/pull/3226 ).
Specifically the messages says it uses the available start-value 0.0, and that seems to refer to the earlier definition of Real that did have a start-value of 0.0 https://specification.modelica.org/maint/3.5/class-predefined-types-and-declarations.html#real-type
Technically the start-value should only be used as default if there was a modifier, but that was a bit unclear and the Pull Request removed the default 0.0 and clarified the use. (There might be additional changes related to this.)
Added:
Basically for parameters we can think of three cases:
- Nothing,
parameter Real p;
- Start-default,
parameter Real p(start=1);
- Value,
parameter Real p=1;
The idea is that later ones are "better", but before 3.6 the specification only specified that 2 should give a warning, but didn't really specify anything for 1. I believe that so many relied on the check in Dymola that no-one actually checked whether it was specified.
Note that case 2 was added in 3.3, and updated in https://github.com/modelica/ModelicaSpecification/issues/2136
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论