# Mass Balance
This section focuses on **mass balance**, a fundamental principle in mathematical modeling that applies to any quantity flowing into and out of a system. The **law of conservation of mass** provides the foundation for deriving systems of *differential equations* that describe mass transfer. We will use **constitutive laws**, such as the **law of mass action** in chemical kinetics, to define the transfer rates. By applying *nondimensionalization*, we'll simplify these models and determine the influence of key parameters. Finally, we'll use `SciPy` to find numerical solutions for these systems.
### Big Ideas
* The **law of conservation of mass** is general; it yields differential equations for any quantity—including salt, chemicals, or money—that flows in and out of a system.
* **Constitutive laws** (like the **law of mass action**) define specific components of a model, describing how different quantities interact.
* **Nondimensionalization** is crucial for simplifying models by reducing the number of parameters and identifying their roles in determining system behavior.
* `SciPy` is used to compute **numerical solutions**, allowing us to easily simulate and visualize complex models.
## Conservation of Mass
```python
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as spi
```
### Mass Balance Equation
The **law of conservation of mass** states that the mass of a closed system does not change. For an *open system* (where mass can enter or exit), this implies the *balance equation*:
$$
\frac{dM}{dt} = Q_{in} - Q_{out}
$$
where:
* $M(t)$ is the mass in the system over time $t$.
* $Q_{in}$ is the mass flow rate into the system (dimensions MT<sup>-1</sup>).
* $Q_{out}$ is the mass flow rate out of the system (dimensions MT<sup>-1</sup>)
### Dilution
We model a solution in a tank being diluted by pure water pumped in and out at the same rate $Q$.
| Description | Symbol | Dimensions | Type |
| :---: | :---: | :---: | :---: |
| mass of salt in the tank | $M$ | M | dependent variable |
| time | $t$ | T | independent variable |
| initial mass of salt in the tank | $M_0$ | M | parameter |
| flow rate of fluid in/out of the tank | $Q$ | L<sup>3</sup>T<sup>-1</sup> | parameter |
| volume of solution | $V_0$ | L<sup>3</sup> | parameter |
The mass flow rate of salt leaving the tank is the product of the concentration $\frac{M}{V_0}$ and the flow rate $Q$. Since only pure water enters, $Q_{in}=0$. The mass balance equation is:
$$
\frac{dM}{dt} = -\frac{MQ}{V}, \quad M(0) = M_0
$$
#### Nondimensionalization
Choose $[t] = \frac{V}{Q}$ and $[M] = M_0$. The dimensionless system is:
$$
\frac{dM^*}{dt^*} = -M^*, \quad M^*(0) = 1
$$
The solution is $M(t) = M_0e^{-Qt/V}$.
### Withdrawing from a Pension
The mass balance concept can apply to other conserved quantities, such as the monetary value of a pension. We model a pension earning interest at rate r and subject to constant withdrawals $w$.
| Description | Symbol | Dimensions | Type |
| :---: | :---: | :---: | :---: |
| total value of the pension | $M$ | N | dependent variable |
| time | $t$ | T | independent variable |
| rate of return | $r$ | T<sup>-1</sup> | parameter |
| rate of withdrawal | $w$ | NT<sup>-1</sup> | parameter |
The mass balance equation is:
$$
\frac{dM}{dt} = \underbrace{rM}_{\text{Rate In (Interest)}} - \underbrace{w}_{\text{Rate Out (Withdrawl)}}, \quad M(0) = M_0
$$
#### Nondimensionalization
We choose the scales $[t] = 1/r$ and $[M] = w/r$. The dimensionless system is:
$$
\frac{dM^*}{dt^*} = M^* - 1 \ , \ \ M^*(0) = M_0^*
$$
where the single dimensionless parameter is the normalized initial pension value: $M_0^* = \frac{rM_0}{w}$.
### Two-Tank System
We model the salt concentrations in two connected tanks. Salt water flows into Tank 1 at rate $Q_1$, flows between Tank 1 and Tank 2 at rate $Q_0$, and flows out of Tank 2 at rate $Q_1$. Total volume $V_1 +V_2$ is constant.
| Description | Symbol | Dimensions | Type |
| :---: | :---: | :---: | :---: |
| mass of salt tank 1 | $M_1$ | M | dependent variable |
| mass of salt tank 2 | $M_2$ | M | dependent variable |
| time | $t$ | T | independent variable |
| salt concentration of solution flowing into tank 1 | $C$ | M L<sup>-3</sup> | parameter |
| flow rate of solution into tank 1 | $Q_1$ | L<sup>3</sup>T<sup>-1</sup> | parameter |
| flow rate of solution from tank 1 to tank 2 | $Q_0$ | L<sup>3</sup>T<sup>-1</sup> | parameter |
| volume of solution in tank 1 | $V_1$ | L<sup>3</sup> | parameter |
The mass balance equations are:
\begin{align}
\frac{dM_1}{dt} &= \underbrace{C Q_1}_{\text{In}} + \underbrace{\frac{M_2 Q_0}{V_2}}_{\text{From Tank 2}} - \underbrace{\frac{M_1 Q_0}{V_1}}_{\text{To Tank 2}} \\ \frac{dM_2}{dt} &= \underbrace{\frac{M_1 Q_0}{V_1}}_{\text{From Tank 1}} - \underbrace{\frac{M_2 Q_0}{V_2}}_{\text{To Tank 1}} - \underbrace{\frac{M_2 Q_1}{V_2}}_{\text{Out}}
\end{align}
#### Nondimensionalization
Choosing a time scale proportional to the internal flow and volumes, and mass scales proportional to the steady-state concentrations:
$$
[t] = \frac{\sqrt{V_1 V_2}}{Q_0}, \quad [M_1] = \frac{C Q_1 \sqrt{V_1V_2}}{Q_0}, \quad [M_2] = \frac{C Q_1 V_2}{Q_0}
$$
This yields the dimensionless system controlled by two parameters: $\nu = \sqrt{\frac{V_2}{V_1}}$ (volume ratio) and $\rho = \frac{Q_1}{Q_0}$ (flow ratio).
\begin{align}
\frac{dM_1^*}{dt^*} &= 1 + M_2^* - \nu M_1^* \\
\frac{dM_2^*}{dt^*} &= M_1^* - \left(\frac{1 + \rho}{\nu} \right) M_2^*
\end{align}
```python=
nu = 1
rho = 1
f = lambda u,t: np.array([1 + u[1] - nu*u[0],u[0] - (1+rho)/nu*u[1]])
u0 = [1,1]
t = np.linspace(0,10,100)
u = spi.odeint(f,u0,t)
plt.plot(t,u[:,0],t,u[:,1])
plt.legend(['Tank 1','Tank 2']), plt.grid(True)
plt.show()
```

## Chemical Kinetics
```python
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as spi
```
### Stoichiometry
**Stoichiometry** is the quantitative study of *reactants* and *products* in chemical reactions. A **stoichiometric equation** describes the number of molecules involved in a reaction using *stoichiometric coefficients*.
For example, the combustion of methane:
$$
CH_4 + 2 O_2 \rightarrow CO_2 + 2 H_2O
$$
This reaction is balanced according to the **law of conservation of mass** because the total number of atoms of each element is the same on both sides.
### Concentration and Reaction Rate
**Molar concentration** (or simply *concentration*) is the amount of a substance per unit volume (dimensions NL<sup>-3</sup>). The **reaction rate** is the change in reactant concentration per unit time (dimensions NL<sup>-3</sup>T<sup>-1</sup>).
### Law of Mass Action
The **law of mass action** states that the reaction rate is directly proportional to the product of the concentrations of the reactants. For a reaction:
$$
c_1 R_1 + \cdots + c_n R_n \rightarrow P
$$
The reaction rate is $k R_1^{c_1} \cdots R_n^{c_n}$, where $k$ is the *reaction coefficient*. The law gives rise to a differential equation for each reactant $R_i$:
$$
\frac{dR_i}{dt} = c_i k R_1^{c_1} \cdots R_n^{c_n}, \quad i=1,\dots,n
$$
### Enzyme Kinetics
[**Enzymes**]((https://en.wikipedia.org/wiki/Enzyme)) are proteins that catalyze (accelerate) chemical reactions. We model a basic enzyme $E$ reaction where a substrate $S$ is transformed into a product $P$ via an intermediate complex $C$:
$$
E + S \underset{k_{-1}}{\overset{k_1}{\rightleftharpoons}} C \overset{k_2}{\rightarrow} E + P
$$
Applying the law of mass action yields the system for the concentrations of each molecule:
\begin{align}
\frac{dE}{dt} &= - k_1 ES + (k_{-1} + k_2) C \\
\frac{dS}{dt} &= -k_1 ES + k_{-1} C \\
\frac{dC}{dt} &= k_1 ES - (k_{-1} + k_2) C\\
\frac{dP}{dt} &= k_2 C
\end{align}
#### Nondimensionalization
Choosing scaling factors that normalize the reaction rates:
$$
[t] = \frac{1}{k_{-1} + k_2}, \quad [E] = [S] = [C] = \frac{k_{-1} + k_2}{k_1}, \quad [P] = \frac{k_2}{k_1}
$$
This yields the dimensionless system, controlled by the parameter $\kappa = \frac{k_{-1}}{k_{-1} + k_2}$:
\begin{align}
\frac{dE^*}{dt^*} &= -E^*S^* + C^* \\
\frac{dS^*}{dt^*} &= -E^*S^* + \kappa , C^* \\
\frac{dC^*}{dt^*} &= E^*S^* - C^* \\
\frac{dP^*}{dt^*} &= C^*
\end{align}
```python=
k = 1/2
# E = u[0], S = u[1], C = u[2], P = u[3]
f = lambda u,t: np.array([-u[0]*u[1] + u[2],-u[0]*u[1] + k*u[2],u[0]*u[1] - u[2],u[2]])
u0 = [1,1,0,0]
t = np.linspace(0,20,500)
u = spi.odeint(f,u0,t)
plt.figure(figsize=(10,4))
plt.plot(t,u[:,0],t,u[:,1],t,u[:,2],t,u[:,3])
plt.legend(['Enzyme E','Substrate S','Compositie C','Product P']), plt.grid(True)
plt.show()
```
