# 再探冷却¶

## 改变环境条件¶

model NewtonCoolingDynamic
"Cooling example with fluctuating ambient conditions"
// 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 NewtonCoolingDynamic;


## 初始瞬态¶

model NewtonCoolingSteadyThenDynamic
"Dynamic cooling example with steady state conditions"
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);

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";

Temperature T_inf "Ambient temperature";
Temperature T "Temperature";
initial equation
der(T) = 0 "Steady state initial conditions";
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";


## 简洁性¶

if语句的一个问题是，它们可以让相对简单的行为变化看起来相当复杂。我们可以用好几种另外的语法结构以更少的代码获得相同的行为。

equation
T_inf = 298.15 - (if time<0.5 then 0 else 20*(time-0.5));
m*c_p*der(T) = h*A*(T_inf-T) "Newton's law of cooling";


equation
T_inf = 298.15 - max(0, 20*(time-0.5));
m*c_p*der(T) = h*A*(T_inf-T) "Newton's law of cooling";


## 事件¶

Modelica语言有众多能优化仿真进行方式的特性。这是一个其中一个例子。读者可以在关于事件的紧接小节中找到对于事件处理的进一步讨论。在接下来的章节中，我们还可以看到另一种更复杂事件的例子。这种事件会依赖于解中的变量的值。