# 化学系统¶

$\begin{split}A + B &\underset{1}{\rightarrow} X \\ A + B &\underset{2}{\leftarrow} X \\ X + B &\underset{3}{\rightarrow} R + S\end{split}$

$A + 2B \rightarrow R + S$

$\begin{split}\frac{\mathrm{d}[A]}{\mathrm{d}t} &= -k_1 [A] [B] + k_2 [X] \\ \frac{\mathrm{d}[B]}{\mathrm{d}t} &= -k_1 [A] [B] + k_2 [X] -k_3 [B] [X] \\ \frac{\mathrm{d}[X]}{\mathrm{d}t} &= k_1 [A] [B] - k_2 [X] -k_3 [B] [X]\end{split}$

## 不使用数组¶

model Reactions_NoArrays "Modeling a chemical reaction without arrays"
Real cA;
Real cB;
Real cX;
parameter Real k1=0.1;
parameter Real k2=0.1;
parameter Real k3=10;
initial equation
cA = 1;
cB = 1;
cX = 0;
equation
der(cA) = -k1*cA*cB + k2*cX;
der(cB) = -k1*cA*cB + k2*cX - k3*cB*cX;
der(cX) = k1*cA*cB - k2*cX - k3*cB*cX;
end Reactions_NoArrays;


## 使用数组¶

model Reactions_Array "Modeling a chemical reaction with arrays"
Real C[3];
parameter Real k[3] = {0.1, 0.1, 10};
initial equation
C = {1, 1, 0};
equation
der(C) = {-k[1]*C[1]*C[2] + k[2]*C[3],
-k[1]*C[1]*C[2] + k[2]*C[3] - k[3]*C[2]*C[3],
k[1]*C[1]*C[2] - k[2]*C[3] - k[3]*C[2]*C[3]};
end Reactions_Array;


$\begin{split}\frac{\mathrm{d}[A]}{\mathrm{d}t} &= -k_1 [A] [B] + k_2 [X] \\ \frac{\mathrm{d}[B]}{\mathrm{d}t} &= -k_1 [A] [B] + k_2 [X] -k_3 [B] [X] \\ \frac{\mathrm{d}[X]}{\mathrm{d}t} &= k_1 [A] [B] - k_2 [X] -k_3 [B] [X]\end{split}$

$\begin{split}\frac{\mathrm{d}}{\mathrm{d}t} \left\{ \begin{array}{c} [A] \\[0pt] [B] \\[0pt] [X] \end{array} \right\} = \left[ \begin{array}{rrr} -k_1 [B] & 0 & k_2 \\ -k_1 [B] & -k_3 [X] & k_2 \\ k_1 [B] & -k_3 [X] & -k_2 \end{array} \right] \left\{ \begin{array}{c} [A] \\[0pt] [B] \\[0pt] [X] \end{array} \right\}\end{split}$

der(C) = [-k[1]*C[2], 0,          k[2];
-k[1]*C[2], -k[3]*C[3], k[2];
k[1]*C[2],  -k[3]*C[3], -k[2]]*C;


## 使用枚举¶

type Species = enumeration(A, B, X);


Real C[Species];


constant Species A = Species.A;
constant Species B = Species.B;
constant Species X = Species.X;


model Reactions_Enum "Modeling a chemical reaction with enums"
type Species = enumeration(
A,
B,
X);
Real C[Species] "Species concentrations";
parameter Real k[3] = {0.1, 0.1, 10};
constant Species A = Species.A;
constant Species B = Species.B;
constant Species X = Species.X;
initial equation
C[A] = 1.0;
C[B] = 1.0;
C[X] = 0.0;
equation
der(C[A]) = -k[1]*C[A]*C[B] + k[2]*C[X];
der(C[B]) = -k[1]*C[A]*C[B] + k[2]*C[X] - k[3]*C[B]*C[X];
der(C[X]) = k[1]*C[A]*C[B] - k[2]*C[X] - k[3]*C[B]*C[X];
end Reactions_Enum;