modelica学习

一、基础知识

1、modelica特点

  • 面向对象

    • 模拟人类思维方法、高效表达模型物理属性
    • 极大促进模型复用
  • 基于等式、无因果关系

    • 等式不是其他编程语言的赋值
  • 只负责建立模型,不负责模型求解

2、优势

  • 高效:面向对象、基于等式、跨学科
  • 灵活:支持多种形式的模型构建
    • 连续模型
    • 离散模型
    • 混合模型
  • 自由:丰富且高质量的开源和商用模型库
    • 开源库:Modelica standard library、linearSystems2
    • 商用库:Modelon,claytex
    • 语言开源

开发环境:Dymola(商业软件、贵)、OpenModelica(开源)、MWorks(国产工业软件)

下载地址(win10):

二、Modelica模型搭建流程

建模准备流程

  • 积累基本物理规律
    • 上课认真听讲,多读Paper,看视频教程。
  • 确立模型用途
    • 影响搭建过程,工具选用及模型本身(做什么:系统分析、模型控制、实时仿真)
  • 分析并确定模型复杂度
    • 不要太高,也不要太低,满足要求最好。

四、基本语法

按照模型类型学习语法

  • 通用语法
  • 函数语法
  • 连续模型语法
  • 离散及混合模型语法 (跳过)

4.1 通用语法 (部分)

  • 变量声明

    • 类型: Real, Integer, BooleanString
    • 前缀: 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;

4.2 函数语法

  • 参数声明: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;

4.3 等式语法(equation)

Modelica各元素的组成关系

  • 变量:
    • parameter Modelica.Units.SI.mass m = 10 "质量";
    • Modelica.Units.SI.Velocity v "速度"
  • 表达式expression: m*der(v)
  • 等式equation: m*der(v) = F
  • 组件component: Modelica.Mechanics.Translational.Components.Mass
  • 系统system: Modelica.Mechanics.Translational.Examples.Accelerate

等式语法的核心 - 构建平衡模型

  • 语法不同于其他语言,但只要掌握核心便可加速理解。
  • 核心:通过等式建立变量之间的关系,并使未知变量数量与等式数量一致
  • 详细信息可参见:
    • ModelicaReference.BalancedModel
    • Modelica Language Specification v3.5 - Section 4.7

model/block 结构

  • Modelica模型中最常见的单位
  • 以模型名称开头(CamelCase),以同样的模型名称结尾。
model ModelName "模型描述(非必要)"
  // 声明状态变量, 参数, 输入/输出变量等
initial equation
  // 初始化等式
equation
  /* 构建已知未知变量之间的关系的等式 */
end ModelName;

等式语法 - 基本等式

  • 作用: 用于构建已知与未知,或未知与未知变量之间的关系。
  • 格式: <表达式1> = <表达式2>;
  • =符号不表示赋值,不同于其他语言,左侧是表达式而不是变量。m*a = F是合法的。
  • 无因果关系: <表达式1> = <表达式2>;<表达式2> = <表达式1>; 是等效的,且无先后顺序。

等式语法 - 条件等式

  • 作用: 用于表达模型根据条件变化发生行为的改变。
  • 格式1:
if a > b then
    x = sin(time);
else
    x = cos(time);
end if
  • 格式2:
x = if a > b then sin(time) else cos(time);
  • 核心是要保持等式数量不变等式语句中有if一定就有else

等式语法 - 初始化等式

  • 作用: 用于设置状态变量,或状态变量导数的初始值(稳态分析)。
  • 格式:
...
initial equation
    x = 3;
    der(y) = 0;
    z = p0;
equation
...

等式语法 - 注意事项

  • 时刻检测等式平衡性
  • 注意变量的定义域,例如log(x), sqrt(x), 1/x

变量数量及等式数量计算

  • 已知变量:input, parameter, constant
  • 未知变量:其他变量都是未知(output, flow, Real/Integer/Boolean/Enumeration) -->

5、标准库 Modelica Standard Library

Modelica Standard Libary (MSL) 介绍

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.

  • Modelica Association (权威性)
  • BSD 3-Clause License (开放性)
  • https://github.com/modelica/ModelicaStandardLibrary
  • 最新版本: v4.0.0 (2020-06-04)

MSL 常识

  • Modelica编译器,Modelica语言,MSL标准库三者之间的关系
    • 相互独立又相互依赖。
      • Dymola: 2022 | 语言: 3.5 | MSL: 4.0.0
    • 举例 - OpenModelica报错分析:
      • Modelica.SIunits -> Modelica.Units.{SI, NonSI, Conversions}
  • 版本管理

MSL 概述

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DHyCyMri-1646963763516)(…/modelica资料/ModelicaPrimer-master/lecture05a/images/LibStructure.png)]

  • Blocks(貌似)强因果关系模型
  • Clocked/StateGraph 离散模型/状态机
  • Magnetic/Electrical/Mechanics/Fuild/Thermal 各领域基础物理模型
  • Math/ComplexMath/Utilities/Constants/Units 辅助应用

Modelica.Blocks

  • 主要包含一些强因果关系的Block模型。
  • 一般都包含一个或多个inputoutput连接器
  • 一般被用于信号生成信号处理
  • 建议:多用于辅助物理模型,而不是构建物理模型。

Modelica.Blocks.Sources

基于time变量的各种信号发生器,一般通过更改参数的形式改变信号的形状

  • Modelica.Blocks.Sources.Constant
  • Modelica.Blocks.Sources.Sine/Cosine... etc
  • Modelica.Blocks.Sources.Step/Ramp... etc
  • Modelica.Blocks.Sources.RealExperession
  • Modelica.Blocks.Sources.CombiTimeTable

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MIS9Pr4T-1646963763517)(…/…/AppData/Roaming/Typora/typora-user-images/image-20211231143541106.png)]

5、标准库MSL - CombiTable插值模块

功能介绍

  • 插值: 是一种通过已知的、离散的数据点,在范围内推求新数据点的过程或方法。

  • 举例

  • 一维单变量插值: 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:插值方式
    • Linear
    • Piecewise Constant
  • extrapolation:外推法
    • Hold first/last table point
    • Extrapolate using last two point derivative

openmodelica

单摆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;
  • 15
    点赞
  • 98
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hellobigorange

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值