# 同步系统¶

Modelica语言3.3版引入了一个新功能以解决有关非确定性离散行为的问题[Elmqvist] 。本节将先介绍这些问题在3.3版本之前的表现。然后，我们将用例子展示这些新特性能如何帮助解决这类问题。

model IndependentSampling "Sampling independently"
Real x "Sampled at 10Hz via one method";
Real y "Sampled at 10Hz via another method";
Real e "Error between x and y";
Real next_time "Next sample for y";
equation
when sample(0,0.1) then
x = time;
end when;

when {initial(), time>pre(next_time)} then
y = time;
next_time = pre(next_time)+0.1;
end when;
e = x-y;
end IndependentSampling;


model SynchronizedSampling "A simple way to synchronize sampling"
Integer tick "A clock counter";
Real x, y;
Real e "Error between x and y";
equation
when sample(0,0.1) then
tick = pre(tick)+1;
end when;

when change(tick) then
x = time;
end when;

when change(tick) then
y = time;
end when;

e = x-y;
end SynchronizedSampling;


model SubsamplingWithIntegers "Use integers to implement subsampling"
Integer tick "Clock counter";
Real x, y, z;
equation
when sample(0,0.1) then
tick = pre(tick)+1;
end when;

when change(tick) then
x = time;
end when;

when change(tick) then
y = time;
end when;

when mod(tick-1,2)==0 then
z = time;
end when;
end SubsamplingWithIntegers;


model SamplingWithClocks "Using clocks to sub and super sample"
Real x, y, z, w;
equation
x = sample(time, Clock(1,10));
y = sample(time, Clock(1,10));
z = subSample(x, 2);
w = superSample(x, 3);
end SamplingWithClocks;


  x = sample(time, Clock(1,10));


  y = sample(time, Clock(1,10));


  z = subSample(x, 2);


Modelica语言编译器可以在幕后对这些时钟间的关系进行推理。编译器知道每隔$$\frac{1}{10}$$秒时钟x就会触发。因此Modelica语言编译器可以使用subSample运算符所提供的信息推论出，每隔$$\frac{2}{10}$$秒时钟z就会触发。理论上，这意味着z也可以被定义为：

z = sample(time, Clock(2,10));


  w = superSample(x, 3);


w = sample(time, Clock(1,30));


Modelica语言的同步时钟特性是相对较新。因此，并非所有的Modelica语言编译器都支持这些特性。要了解更多有关这些同步功能及其应用，可以参考[Elmqvist]以及／或者3.3版以后的Modelica规范。

 [Elmqvist] (1, 2) “Fundamentals of Synchronous Control in Modelica”, Hilding Elmqvist, Martin Otter and Sven-Erik Mattsson http://www.ep.liu.se/ecp/076/001/ecp12076001.pdf