1、modelica特点
面向对象
基于等式、无因果关系
只负责建立模型,不负责模型求解
2、优势
开发环境:Dymola(商业软件、贵)、OpenModelica(开源)、MWorks(国产工业软件)
下载地址(win10):
变量声明
Real
, Integer
, Boolean
, String
input
, output
, parameter
public
,protected
算术运算: +
,-
,*
,/
,^
导数运算符:der(x)表示x的一阶导;y1=der(x),y2=der(y1),其中y2表示对x求二阶导
延时运算符:z=delay(x,2),z为对x延时2s后的结果
注释:
// this is a line comment
/* this is a block comment */
Real foo "A special comment";
数组:
x[3]
:一维数组 (3-element vector)x[3, 3]
:二维数组 (3x3 matrix)model demo
Real foo "this is a foo variable";
Integer bar;
Boolean foo2;
input Real foo3;
output Integer bar2;
parameter Real foo4 = 1.0;
parameter Real x[3] = {1,2,3} "A 3 ELEMENT VECTOR";
input Real x1[:]"定义输入为一维数组";
input Real x2[:,:]"定义输入为二维数组";
parameter Real x2[3,3] = [1,2,3;2,3,4;3,4,5]
protected
Integer bar3;
equation
//foo = foo2 + foo3;
/*foo = foo2 * foo3; */
annotation (Icon(coordinateSystem(preserveAspectRatio=false)), Diagram(
coordinateSystem(preserveAspectRatio=false)));
end demo;
and
,or
,not
,<
,>
,<>
,<=
,>=
if <condition> then
...
elseif <condition> then
...
else
...
end if;
for i in 1:10 loop
...
end for;
input
output
:=
algorithm
protected
input Real tol = 1e-5 "Input with default value"
function demo_function "demo function"
input Real in1 "The first argument for demo function";
input Real in2 "The second argument for demo function";
input Real tol = 5 "Tolerance";
output Real out1 "The first output for demo";
output Real out2 "The second output for demo";
/*(out1,out2) = demo(in1,in2)*/
protected
Real p1;
algorithm
p1 := in1 - in2;
out1 := in1 + in2 + p1;
out2 := in1 * in2;
end demo_function;
function <函数名>
input TypeI1 in1;
input TypeI2 in2;
input TypeI3 in3 = <默认值> "注释" annotation(...);
...
output TypeO1 out1;
output TypeO2 out2 = <默认值>;
protected
<局部变量>
...
algorithm
<语句>
...
end <函数名>;
编写一个coo函数将致密矩阵M转换为Coordinate/Triplet储存格式的稀疏矩阵。
例子:
M = [1.0,2.0,3.0;0,4.0,0;5.0,0,0]; "致密矩阵"
// M转换为COO格式的稀疏矩阵
i, j, value
1, 1, 1.0
1, 2, 2.0
1, 3, 3.0
2, 2, 4.0
3, 1, 5.0
function emo
input Real M[:,:];
output Real row[:];
output Real col[:];
output Real value[:];
protected
Integer count;
Real temp_row[size(M,1)*size(M,2)];
Real temp_col[size(M,1)*size(M,2)];
Real temp_value[size(M,1)*size(M,2)];
algorithm
count := 1;
for i in 1 : size(M,1) loop
for j in 1 : size(M,2) loop
if M[i,j]<>0 then
temp_row[count] := i;
temp_col[count] := j;
temp_value[count] := M[i,j];
count := count + 1;
end if;
end for;
end for;
row := temp_row[1:count-1];
col := temp_col[1:count-1];
value := temp_value[1:count-1];
end emo;
parameter Modelica.Units.SI.mass m = 10 "质量";
Modelica.Units.SI.Velocity v "速度"
m*der(v)
m*der(v) = F
Modelica.Mechanics.Translational.Components.Mass
Modelica.Mechanics.Translational.Examples.Accelerate
ModelicaReference.BalancedModel
Modelica Language Specification v3.5 - Section 4.7
model ModelName "模型描述(非必要)"
// 声明状态变量, 参数, 输入/输出变量等
initial equation
// 初始化等式
equation
/* 构建已知未知变量之间的关系的等式 */
end ModelName;
<表达式1> = <表达式2>;
=
符号不表示赋值,不同于其他语言,左侧是表达式而不是变量。m*a = F
是合法的。<表达式1> = <表达式2>;
与 <表达式2> = <表达式1>;
是等效的,且无先后顺序。if a > b then
x = sin(time);
else
x = cos(time);
end if
x = if a > b then sin(time) else cos(time);
if
一定就有else
。...
initial equation
x = 3;
der(y) = 0;
z = p0;
equation
...
input
, parameter
, constant
output
, flow
, Real/Integer/Boolean/Enumeration
) -->Free library from the Modelica Association
to model mechanical (1D/3D), electrical (analog, digital, machines), magnetic, thermal, fluid, control systems and hierarchical state machines. Also numerical functions and functions for strings, files and streams are included.
OpenModelica报错
分析:
Modelica.SIunits
-> Modelica.Units.{SI, NonSI, Conversions}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DHyCyMri-1646963763516)(…/modelica资料/ModelicaPrimer-master/lecture05a/images/LibStructure.png)]
Blocks
(貌似)强因果关系模型Clocked/StateGraph
离散模型/状态机Magnetic/Electrical/Mechanics/Fuild/Thermal
各领域基础物理模型Math/ComplexMath/Utilities/Constants/Units
辅助应用强因果关系
的Block模型。input
和output
连接器信号生成
及信号处理
基于time
变量的各种信号发生器,一般通过更改参数
的形式改变信号的形状
Modelica.Blocks.Sources.Constant
Modelica.Blocks.Sources.Sine/Cosine...
etcModelica.Blocks.Sources.Step/Ramp...
etcModelica.Blocks.Sources.RealExperession
Modelica.Blocks.Sources.CombiTimeTable
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MIS9Pr4T-1646963763517)(…/…/AppData/Roaming/Typora/typora-user-images/image-20211231143541106.png)]
插值: 是一种通过已知的、离散的数据点,在范围内推求新数据点的过程或方法。
举例
一维单变量插值: Modelica.Blocks.Tables.CombiTable1Ds
输入(自变量,Real
): u
输出(插值结果,Real[:]
):y
可以理解为y = {f1(u), f2(u), ... fn(u)}
图像
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SouPrj38-1646963763517)(…/…/AppData/Roaming/Typora/typora-user-images/image-20211231173040802.png)]
table
(Real[:][:]
) : 二维数组,第一列为自变量,且需单调递增!*.txt
,*.mat
)
fileName
(String
类型):数据文件所在位置,注意\\
, NoName
tableName
(String
类型):table名称columns
:决定输出向量的大小smoothness
:插值方式
extrapolation
:外推法
单摆modelica模型
model simplePendulum
parameter Real L=2 "参量";
constant Real g=9.81 "常量";
Real theta"变量";
Real omega;
initial equation
//--------------------------------------------------------------------------
// 设定初始值
//--------------------------------------------------------------------------
omega = 0;
theta = 2.3;
equation
der(theta) = omega;
der(omega) = -(g/L)*Modelica.Math.sin(theta);
end simplePendulum;