问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

Modelica建模语言入门:基础语法与实战案例解析

创作时间:
作者:
@小白创作中心

Modelica建模语言入门:基础语法与实战案例解析

引用
CSDN
1.
https://blog.csdn.net/xzb5566/article/details/141064063

Modelica是一种面向对象的物理系统建模语言,广泛应用于多领域复杂工程系统的建模仿真。本文将从Modelica的基础概念、语法特点,到具体的建模实例,全面介绍这一重要技术工具。

Modelica简介

Modelica是由Modelica协会维护、免费开放的物理系统面向对象的统一建模语言规范,为机、电、液、控等多领域复杂工程系统提供了统一的物理建模语言规范。

Modelica作为多学科统一建模仿真的国际标准,被仿真界广泛采用,系统多领域统一建模仿真,已成为世界公认的MBSE和CPS的核心关键支持技术。

Modelica 语法、函数、包、连接器、组件、子系统

https://modelica.org/libraries.html 第三方的库
MSL

Modelica 下载安装:

支持Modelica语言的开源软件有
1) OpenModelica,
2) JModelica(新版本不再开源,但是最后的开源版本仍可下载)
3) Scilab项目中的SCICOS 。

可以使用 openmodelica 软件来进行modelica建模,测试 ,openmodelica 的下载安装 Download Windows。 下载对应的版本 :Index of /omc/builds/windows/releases/1.21/0/64bit

典型商业软件主要有 [2] :

  1. 苏州同元的MWorks(中国),
  2. 伊萨的SimulationX,
  3. 达索的Dymola,
  4. 西门子的AMESim(支持Modelica),
  5. MapleSoft(Maple厂家)的MapleSim,
  6. Wolfram (Mathemetica厂家)的MathModelica。

Modelica 基础语法:

1) 模型定义
model SomeModelName "An optional description"
// By convention, variables are listed at the start
equation
/* And equations are listed at the end */
end SomeModelName;

2) 继承
model SpecializedModelName "An optional description" extends Model1; // No modifications
extends Model2(n=5); // Including modification // By convention, variables are listed at the start
equation /* And equations are listed at the end */
... ...
end SpecializedModelName;

3) 变量
Real x;
Real x, y;
Real alpha “angular acceleration”;

· 内建类型:
4大类型: Real , Integer, Boolean, String (4大内置参数类型)

· 参数: parameter 修饰
能够在变量声明前加入parameter(参数)限定词,以表明该变量是先验已知的。我们可以认为这个参数作为模型的“输入数据”,是不随时间变化的常数。

· 常数: constant 修饰
与parameter(参数)限定符关系密切的是constant(常数)限定词。

· 离散变量: discrete

· 派生类型:
type NewTypeName = BaseTypeName(/* attributes to be modified /);
type Temperature = Real(unit="K"); // Could be a temperature difference
type AbsoluteTemperature = Temperature(min=0); // Must be positive
model BaseTypeName
type BaseTypeName1 = Real(unit="m2");
type NewTypeName = BaseTypeName1;// /
attributes to be modified */;
type Temperature = Real(unit="K"); // Could be a temperature difference type AbsoluteTemperature = Temperature(min=0); // Must be positiveequation
end BaseTypeName;

· 枚举类型:
type AssertionLevel = enumeration(warning, error);
type StateSelect = enumeration(never, avoid, default, prefer, always);

修改语句:
Real x(start=10);
parameter Real x0=10 "Start value of prey population";
Real x(start=x0) "Prey population";
extends QuiescentModelWithInheritance(gamma=0.3, delta=0.01);
extends QuiescentModelWithInheritance(x(start=5));

Record类型定义
record类型可以有自己的变量,但是不允许包含方程
record Vector
"A vector in 3D space" Real x; Real y; Real z;
end Vector;

record Complex "Representation of a complex number"
Real re "Real component";
Real im "Imaginary component";
end Complex;

parameter Vector v(x=1.0, y=2.0, z=0.0);
parameter Vector v = Vector(x=1.0, y=2.0, z=0.0);

Demo展示:

demo1: 模拟一个乒乓球从1米的高度,掉落到地上,并来回弹起来的效果:

  1. 对弹跳球的建模
    model BouncingBall "The 'classic' bouncing ball model"
    type Height = Real(unit = "m");
    type Velocity = Real(unit = "m/s");
    parameter Real e = 0.8 "Coefficient of restitution";
    parameter Height h0 = 1.0 "Initial height";
    Height h;
    Velocity v;
    initial equation
    h = h0;
    equation
    v = der(h);
    der(v) = -9.81;
    when h <= 0 then
    reinit(v, -e*pre(v));
    end when;
    end BouncingBall;

modelica模拟的效果图:、
从上面的模拟图中能发现,当最后球静止的时候,球落在地面以下的位置了。这显然不符合现实世界。我们可以对其改进一番看看。

  1. 改进版本:
    model StableBouncingBall
    "The 'classic' bouncing ball model with numerical tolerances"
    type Height=Real(unit="m");
    type Velocity=Real(unit="m/s");
    parameter Real e=0.8 "Coefficient of restitution";
    parameter Height h0=1.0 "Initial height";
    constant Height eps=1e-3 "Small height";
    Boolean done;
    Height h;
    Velocity v;
    initial equation
    h = h0;
    done = false;
    equation
    v = der(h);
    der(v) = if done then 0 else -9.81;
    when {h<0,h<-eps} then
    done = h<-eps;
    reinit(v, -e*(if h<-eps then 0 else pre(v)));
    end when;
    end StableBouncingBall;

modelica模拟的效果图:

demo2: 带滞回的温度控制

model HysteresisControl "A control strategy that doesn't chatter"
type HeatCapacitance=Real(unit="J/K");
type Temperature=Real(unit="K");
type Heat=Real(unit="W");
type Mass=Real(unit="kg");
type HeatTransferCoefficient=Real(unit="W/K");
Boolean heat(start=false) "Indicates whether heater is on";
parameter HeatCapacitance C=1.0;
parameter HeatTransferCoefficient h=2.0;
parameter Heat Qcapacity=25.0;
parameter Temperature Tamb=285;
parameter Temperature Tbar=295;
Temperature T;
Heat Q;
initial equation
T = Tbar+5;
heat = false;
equation
Q = if heat then Qcapacity else 0;
C*der(T) = Q-h*(T-Tamb);
when {T>Tbar+1,T<Tbar-1} then
heat = T<Tbar;
end when;
end HysteresisControl;

modelica模拟的效果图:

demo3: 带抖动的温度控制

model ChatteringControl "A control strategy that will 'chatter'"
type HeatCapacitance=Real(unit="J/K");
type Temperature=Real(unit="K");
type Heat=Real(unit="W");
type Mass=Real(unit="kg");
type HeatTransferCoefficient=Real(unit="W/K");
Boolean heat "Indicates whether heater is on";
parameter HeatCapacitance C=1.0;
parameter HeatTransferCoefficient h=2.0;
parameter Heat Qcapacity=25.0;
parameter Temperature Tamb=285;
parameter Temperature Tbar=295;
Temperature T;
Heat Q;
initial equation
T = Tbar+5;
equation
heat = T<Tbar;
Q = if heat then Qcapacity else 0;
C*der(T) = Q-h*(T-Tamb);
end ChatteringControl;

modelica模拟的效果图:

电子电气相关的模型仿真

仿真效果:

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号