## 4 Simulation Code VC-CMV Mode
The patient is simulated using the subroutine \"Patient\" shown in Figure 14. It consists of:
* Two \"Check Valve\" blocks, used to allow the gas to flow through the upper/lower branch during inspiration/expiration, respectively; the cracking pressure, which is the minimum upstream pressure required to open the valve, is set in these blocks;
* Two \"Local Restriction\" blocks, which reproduce the inspiratory/expiratory resistance of the respiratory system, respectively; these blocks model the pressure loss due to a flow area restriction, the value of which can be set inside the blocks. The laminar flow pressure ratio and the cross-sectional area at the inlet/outlet port must also be defined in order to consider the type of flow conditions in the trachea and its dimensions;
* The \"Translational Mechanical Converter\" block (namely, a piston moving inside a cylinder), used to simulate the patient's lungs (12);
* The \"Spring\" block, connected in series with the \"Translational Mechanical Converter\" block and used to simulate the compliance of the respiratory system. The piston diameter and the spring stiffness must be chosen according to the compliance of the respiratory system of the simulated patient. The compliance of the respiratory system (\(\mathrm{C_{RS}}\)) is defined as the change in volume of the lungs (\(\mathrm{\Delta V}\)) that occurs per unit change in the pressure inside the lungs (\(\mathrm{\Delta P}\)):  
\[\mathrm{C_{RS}}=\frac{\mathrm{\Delta V}}{\mathrm{\Delta P}}, \tag{16}\]  
As the lungs are simulated in the same way as a cylinder whose piston is counteracted by a spring, the relation between (\(\mathrm{\Delta P}\)) and (\(\mathrm{\Delta V}\)) is:  
\[\mathrm{\Delta P}=\frac{\mathrm{k}\mathrm{\Delta x}}{\mathrm{A}}=\frac{ \mathrm{k}\mathrm{\Delta V}}{\mathrm{A}^{2}}, \tag{17}\]  
where (A) is the piston area, (\(\mathrm{\Delta x}\)) is the displacement of the piston, and (k) is the stiffness of the spring. Combining Equations (16) and (17), one obtains:  
\[\mathrm{C_{RS}}=\frac{\mathrm{A}^{2}}{\mathrm{k}}, \tag{18}\]  
Equation (18) allows us to calculate (k) once (A) has been fixed (or vice versa) by establishing the value of the simulated patient compliance. Furthermore, once we have established the value of the minimum pressure that we want in the circuit at the beginning of the inspiratory phase, namely the positive end-expiratory pressure (PEEP), the pre-compression of the spring (\(\mathrm{x_{0}}\)) can be evaluated as follows:  
\[\mathrm{x_{0}}=\frac{\mathrm{A}}{\mathrm{k}}\mathrm{PEEP}, \tag{19}\]  
Finally, the following equation (Equation (20)) allows the dead volume of the cylinder (\(\mathrm{V_{0}}\)) to be calculated:  
\[\mathrm{V_{0}}=\mathrm{A}\ \mathrm{x_{0}}, \tag{20}\]  
For contrast, the subroutine reproducing the nebulizer is shown in Figure 15.  
Figure 14: Subroutine reproducing the patient’s trachea and lungs. It consists of:  
* The \"Two-Way Directional Valve\" block, used to simulate the ON/OFF valve (21); the control signal, at port S, being equal to either 0 or 1;
* The \"Pressure Source\" block, which reproduces the compressor (22); this block maintains a constant pressure at its outlet and its value can be set inside the block;
* The \"Orifice ISO 6358\" block, used to reproduce the nebulizer jar (23); again, the valve properties inside the block can be set using the sonic conductance, the flow coefficient, or the restriction area of the corresponding valve. The automatic control of the inspiratory flow and the positive end-expiratory pressure, which is therefore the automatic control of the opening degrees of the inspiratory and expiratory valves, has been obtained through two proportional-integral-derivative (PID) controllers: \"PID Flow\" and \"PID Pressure\". The parameters of the PID controllers have been determined using the Ziegler-Nichols method. The subroutines reproducing these controllers are shown in Figures 16 and 17, respectively.