Modelica学习笔记

一、使用

1.新建package-》新建modelica类

2.找组件modelica-->mechanics-->translational-->components等;找信号源blocks-->sources等

3.模件进行连接

4.元件设置参数

5.仿真-仿真设置

二、模型类化

打开类看单个构件的建模语言

新建类-->扩展 (选库中类的)-->在扩展类中写方程-->调用到modelica类中使用

三、语法

变量声明:

  1. 类型:Real,Integer, Boolean,String,enumeration
  2. 前缀:input, output, parameter;
  3. 可见性:public, protected;

注释:
1.单行;多行;
2.引号注释:

Real foo " A special comment";

数组:

parameter Real x[3,3]={1,2,3;4,5,6;4,5,6};

标注:annotation

equation
der(x) = 1-x ”Drives value of x toward 1.0”;
annotation(experiment(StartTime=0,StopTime=8));//定义模型仿真开始和结束的时间

标注中包含的信息与模型的属性没有直接联系,只是提供进行模拟仿真的条件

  1. 对变量、语句(方程)、继承:紧跟在一个声明的后面,并且在标示符 ; 的前面
    parameter Real length ”Rod length” annotation(...);
    
  2. 模型标注数据直接在模型自身定义的时候声明
  • 标注中即可以包含对变量的赋值,也可以包含对一些内部变量的修改
  • 标注就是为了方便模型开发者将任意数据添加到模型中而设计的,
  • 可以同时多个标注,
  • 只要标注的名称不同,我们就可以一次加入多个。
    :在加入零件号时,最好要将其放一个特别 的变量内。这个变量最好应该和你的公司或者应用情况相关,而且足够特别
    annotation(XogenyIndustries(PartNumber=”FF78-E4B879”),
    experiment(StartTime=0,StopTime=8));
    

    变量 XogenyIndustries 可以为某个特定组织或目的创建出一个“命名空间”

  • 几种标准标注:Documentation;experiment;Evaluate;HideResult(P37)

逻辑运算符:
and , or, not,<,>,<>,<=, >=

if <condition> then
...
elseif <condition> then
...
else
...
end if;

 循环:

for i in1:10 loop
  ...
​​​​​​​end for;

四、函数语法

分区标志符

        函数内容:alogrithm
        局部变量:protected

参数默认值:

        input Real tol=le-5 "Input with default value"

多个输出变量:
 

package demo
  function demo_fuction
    input Real in1"The first input demo function";
    input Real in2"The second argument";
    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;
     
     out2:=in1*in2;
     
     p1:=in1-in2;
     
  end demo_fuction;
end demo;

调用:

  (out1,out2)=Fuctions.demo_fuction(in1=10,in2=20);

函数测试:右键调用inputs,结果检验;

例题:将致密矩阵M转化为存储格式的稀疏矩阵

function  coo
  input Real M[:,:] "Input dense matrix";
  input Real tol = le-5 "M[i,j]<tol is considered empty. Tolerance value";
  
  output Integer rows[:] "Row indices";
  output Integer cols[:] "Colum indices";
  output Real data[:] "values";
protected
  Integer r_temp[size(M, 1) * size(M, 2)];
  Integer c_temp[size(M, 1) * size(M, 2)];
  Real d_temp[size(M, 1) * size(M, 2)];
  Integer counter;
algorithm
  counter := 0;
  for i in 1:size(M, 1) loop
    for j in 1:size(M, 2) loop
      if abs(M[i, j]) > tol then
        counter := counter + 1;
        r_temp[counter] := i;
        c_temp[counter] := j;
        d_temp[counter] := M[i, j];
      end if;
    end for;
  end for;
  rows := r_temp[1:counter];
  cols := c_temp[1:counter];
  date := d_temp[1:counter];
end coo;

五、等式语法

modelica各元素的组合关系

变量-表达式-等式组件-系统

语法不同于其他语言,掌握核心

核心:通过等式建立起变量之间的关系,并使未知变量数量与等式变量一致

model/block结构

model ModeName"模型描述(非必要)"
  //声明状态变量,参数,输入/输出变量等
initial equation
        //初始化等式
equation
        /*构建已知未知量之间的关系的等式*/
end ModeName;

等式语法-基本等式

构建已知与未知或未知与未知量之间的关系

格式: <表达式1>=<表达式2>

=不表示赋值,无因果关系

条件等式

表达形式1:

x= if a>b then sin(time) else cos(time);

表达形式2:

if a>b then
        x=sin(time);
else
        x=cos(time);
end if

1.方程的平衡形式:
核心是要保持等式数量不变,等式有if一定有else;
变量的数量必须等于方程的数量,而且,在模拟的过程中方程的数量必须是固定的

2.不平衡形式:
​​​​​​​if 和 else 两侧的方程数量是不同的
仅当 :模拟过程中条件表达式的值不能改变 ;方程的数量在模拟过程中不能改变;

..
parameter Boolean steady_state;
initial equation
if steady_state then
der(x) = 0;
der(y) = 0;
..

如果布尔参数 steady_state(稳定状态)为真,那么初始方程是有效的。但是如果参数为假, 它们就无效。这里的条件表达式具有参数级别的可变性是因为,表达式仅仅包含一个变量,而这个变量 是个参数。

初始值等式

initial equation 
        x=3;
        der(y)=0;
        z=p0;

注意:变量定义域;检验等式平衡性

五、MSL

Modelica.Blocks

  • 包含强因果关系的模型
  • 一般包含一个或多个input和output连接器
  • 一般用于信号生成及信号处理
    多用于辅助物理模型,而不是建构

Modelica.Blocks.Sources
信号源输出

差值模块
Modelica.Blocks.Tables.CombiTable1Ds一维单变量差值

  • 用作信号源
  • 数据模型

**************************************************************************************************************

一、基本方程

parameter Real T_inf(unit=”K”)=298.15 ”Ambient temperature”;


1.关键字parameter 表明变量的 值是先验已知的(即仿真开始之前)
2.每个变量的声明部分都包含与变量相关联的物理单元文本 (unit=”...”):  ”1” 代表值没有物理 单位,。另一方面””(默认没有给定值)表明物理单位不确定。

model NewtonCoolingWithTypes ”Cooling example with physical types”
// Types
type Temperature=Real(unit=”K”, min=0);
type ConvectionCoefficient=Real(unit=”W/(m2.K)”, min=0);
type Area=Real(unit=”m2”, min=0);
type Mass=Real(unit=”kg”, min=0);
type SpecificHeat=Real(unit=”J/(K.kg)”, min=0);
// Parameters
parameter Temperature T_inf=298.15 ”Ambient temperature”;
parameter Temperature T0=363.15 ”Initial temperature”;
parameter ConvectionCoefficient h=0.7 ”Convective cooling coefficient”;
parameter Area A=1.0 ”Surface area”;
parameter Mass m=0.1 ”Mass of thermal capacitance”;
parameter SpecificHeat c_p=1.2 ”Specific heat”;
// Variables
Temperature T ”Temperature”;
initial equation
T = T0 ”Specify initial value for T”;
equation
m*c_p*der(T) = h*A*(T_inf-T) ”Newton’s law of cooling”;
end NewtonCoolingWithTypes;

一旦定义了一个物理类型比如 Temperature,我们可以使用它为多 个变量(例如 T、T_inf 和 T0)进行声明
 

Real x(start=5, fixed=true);
  1. start 属性的主要目的是(正如在初始化 ( 31) 那节里广泛讨 论过的)为状态变量提供“备用”的初始状态。
  2. start 属性的也可以用于变量,是迭代变量时的初始假想值。
  3. 最后,如果一个 parameter 没有明确指定的值,那么 start 属性的值可以作为 parameter 的默认值。

在声明变量的 start 属性时直接指 定其初始条件;变量的 fixed 属性被 用来通知编译器 start 属性必须作为初始条件来使用

start 属性的其中一个作用是提供初始化猜想值:因为该变量必须通过系统的非线性方程组来 求解。这也就意味着,我们要对变量 x 和 y 的 start 属性值进行指定,以尽量“避开”系统的零解(或 者说至少接近我们期望的非零解)

继承:

model QuiescentModelWithInheritance ”Steady state model with inheritance”
extends ClassicModel;
initial equation
der(x) = 0;
der(y) = 0;
end QuiescentModelWithInheritance;

从 ClassicModel 模型中复制(或“继承”) 其包含的所有内容,而无需重复定义。因此,除了新加入的初始化方程外,QuiescentModelWithInheritance 模型和 ClassicModel 模型其他部分完全一样

model QuiescentModelWithModifications ”Steady state model with modifications”
extends QuiescentModelWithInheritance(gamma=0.3, delta=0.01);
end QuiescentModelWithModifications;

允许对模型加入相应的“修改语句”

  • 按照惯例,extends 子句通常列在模型定义最上方,在任何变量之前

派生类型

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

BaseTypeName(基本类型名称)一般为内置类型(比如 Real(实数))。但是它也可以是另外一种派生 类型。这意味着多层次的限定也是支持的。

Record 类型

record 类型可以有自己的变量,但是不允许包含方程。record 类型主要用于数据的分组

定义record;创建record:记录构造函数输入record类型内部定义匹配的变量

record Vector ”A vector in 3D space”
Real x;
Real y;
Real z;
end Vector;

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

二、离散行为

事件是任何在系统内触发了某种不连续性的东西:
“时间事件”:发生在某个特定时间的事件是最简单的一类事件;
“状态事件”:等到某信号越过特定阈值

time 是一个内置在所有 Modelica 模型内的变量

 T_inf = 298.15 - (if time<0.5 then 0 else 20*(time-0.5));
 T_inf = 298.15 - max(0, 20*(time-0.5));//用max表示环境温度的变化

model NewtonCooling
//Types
type Temperature=Real(unit="K",min=0);
type ConvectionCoefficient=Real(unit="W/(m2.k)",min=0);
type Area=Real(unit="m2",min=0);
type Mass=Real(unit="kg",min=0);
type SpecificHeat=Real(unit="J/(K.kg)",min=0);

//Parameters
parameter Temperature T0=363.15"Initial temperature";
parameter ConvectionCoefficient h=0.7"Convective cooling coefficient";
parameter Area A=1.0"Surface area";
parameter Mass m=0.1"Mass of thermal capacitance";
parameter SpecificHeat c_p=1.2"Specific heat";

//Variables
Temperature T_inf"Ambient temperature";
Temperature T "Temperature";

initial equation
  T=T0"Specify initial value for T";
equation
  if time<=0.5 then
    T_inf=298.15"Constant temperature when time <=0.5";
  else
    T_inf=298.15-20*(time-0.5)"Otherwise,increasing";
  end if;
  m*c_p*der(T)=h*A*(T_inf-T)"Newton's law of cooling";  

end NewtonCooling;

问题:0x000002错误

另:可以用初始方程,方程从平衡态开始

if表达式代替if语句

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值