# 一维热传导¶

## 推导方程¶

$m_i C T_i$

$m_i = \rho V_i$

$V_i = A_i L_i$

$L_i = \frac{L}{n}$

$m = \rho A L_i$

$\rho A L_i C T_i$

$\rho A L_i C \frac{\mathrm{d} T_i}{\mathrm{d}t}$

$q_h = -h A (T_i-T_{amb})$

$q_{k_{i \rightarrow {i-1}}} = -k A \frac{T_i-T_{i-1}}{L_i}$
$q_{k_{i \rightarrow {i+1}}} = -k A \frac{T_i-T_{i+1}}{L_i}$

$\begin{split}\rho A L_i C \frac{\mathrm{d} T_1}{\mathrm{d}t} &= -k A \frac{T_1-T_2}{L_i} - h A (T_1-T_{amb})\end{split}$

$\begin{split}\rho A L_i C \frac{\mathrm{d} T_n}{\mathrm{d}t} &= -k A\frac{T_n-T_{n-1}}{L_i} -h A (T_n-T_{amb})\end{split}$

$\begin{split}\rho A L_i C \frac{\mathrm{d} T_i}{\mathrm{d}t} &= -k A\frac{T_i-T_{i-1}}{L_i} -k A\frac{T_i-T_{i+1}}{L_i} -h A (T_i-T_{amb})\end{split}$

## 实现¶

  type Temperature=Real(unit="K", min=0);
type ConvectionCoefficient=Real(unit="W/K", min=0);
type ConductionCoefficient=Real(unit="W.m-1.K-1", min=0);
type Mass=Real(unit="kg", min=0);
type SpecificHeat=Real(unit="J/(K.kg)", min=0);
type Density=Real(unit="kg/m3", min=0);
type Area=Real(unit="m2");
type Volume=Real(unit="m3");
type Length=Real(unit="m", min=0);


  parameter Integer n=10;
parameter Length L=1.0;
parameter Density rho=2.0;
parameter ConvectionCoefficient h=2.0;
parameter ConductionCoefficient k=10;
parameter SpecificHeat C=10.0;
parameter Temperature Tamb=300 "Ambient temperature";


  parameter Area A = pi*R^2;
parameter Volume V = A*L/n;


  Temperature T[n];


initial equation
T = linspace(200,300,n);


equation
rho*V*C*der(T[1]) = -h*(T[1]-Tamb)-k*A*(T[1]-T[2])/(L/n);
for i in 2:(n-1) loop
rho*V*C*der(T[i]) = -k*A*(T[i]-T[i-1])/(L/n)-k*A*(T[i]-T[i+1])/(L/n);
end for;
rho*V*C*der(T[end]) = -h*(T[end]-Tamb)-k*A*(T[end]-T[end-1])/(L/n);


for <var> in <range> loop
// statements
end for;


for循环用在等式区域时，循环内每个方程都会在for循环每次迭代时产生一个新的方程。因此，在本例对应每个在2n-1之间的i，我们会一共生成$$n-2$$个方程。

model Rod_ForLoop "Modeling heat conduction in a rod using a for loop"
type Temperature=Real(unit="K", min=0);
type ConvectionCoefficient=Real(unit="W/K", min=0);
type ConductionCoefficient=Real(unit="W.m-1.K-1", min=0);
type Mass=Real(unit="kg", min=0);
type SpecificHeat=Real(unit="J/(K.kg)", min=0);
type Density=Real(unit="kg/m3", min=0);
type Area=Real(unit="m2");
type Volume=Real(unit="m3");
type Length=Real(unit="m", min=0);

constant Real pi = 3.14159;

parameter Integer n=10;
parameter Length L=1.0;
parameter Density rho=2.0;
parameter ConvectionCoefficient h=2.0;
parameter ConductionCoefficient k=10;
parameter SpecificHeat C=10.0;
parameter Temperature Tamb=300 "Ambient temperature";

parameter Area A = pi*R^2;
parameter Volume V = A*L/n;

Temperature T[n];
initial equation
T = linspace(200,300,n);
equation
rho*V*C*der(T[1]) = -h*(T[1]-Tamb)-k*A*(T[1]-T[2])/(L/n);
for i in 2:(n-1) loop
rho*V*C*der(T[i]) = -k*A*(T[i]-T[i-1])/(L/n)-k*A*(T[i]-T[i+1])/(L/n);
end for;
rho*V*C*der(T[end]) = -h*(T[end]-Tamb)-k*A*(T[end]-T[end-1])/(L/n);
end Rod_ForLoop;


Note

## 其他可选方法¶

equation
rho*V*C*der(T[1]) = -h*(T[1]-Tamb)-k*A*(T[1]-T[2])/(L/n);
rho*V*C*der(T[2:n-1]) = {-k*A*(T[i]-T[i-1])/(L/n)-k*A*(T[i]-T[i+1])/(L/n) for i in 2:(n-1)};
rho*V*C*der(T[end]) = -h*(T[end]-Tamb)-k*A*(T[end]-T[end-1])/(L/n);


equation
rho*V*C*der(T) = {-h*(T[i]-Tamb)
-(if i==1 then 0 else k*A/(L/n)*(T[i]-T[i-1]))
-(if i==n then 0 else k*A/(L/n)*(T[i]-T[i+1])) for i in 1:n};


equation
rho*V*C*der(T[1]) = -h*(T[1]-Tamb)-k*A*(T[1]-T[2])/(L/n);
rho*V*C*der(T[2:n-1]) = -k*A*(T[2:n-1]-T[1:n-2])/(L/n)-k*A*(T[2:n-1]-T[3:n])/(L/n);
rho*V*C*der(T[end]) = -h*(T[end]-Tamb)-k*A*(T[end]-T[end-1])/(L/n);


  Heat Qleft[n];
Heat Qright[n];


  Qleft = {if i==1 then -h*(T[i]-Tamb) else -k*A*(T[i]-T[i-1])/(L/n) for i in 1:n};
Qright = {if i==n then -h*(T[i]-Tamb) else -k*A*(T[i]-T[i+1])/(L/n) for i in 1:n};


  rho*V*C*der(T) = Qleft+Qright;