## Upgrading Custom Hydraulic Blocks to Use the Isothermal Liquid Domain

If your model contains custom blocks with hydraulic ports, you can rewrite the underlying component source to adapt them to using the isothermal liquid domain.

This change may lead to numerical changes in the block behavior. Using mass flow rate, instead of volumetric flow rate, as the Through variable reduces the potential for small errors in mass conservation to accumulate over time due to the conversion between mass and volumetric quantities, which results in increased accuracy.

To rewrite the component source, follow these steps:

1. Replace the nodes of type `foundation.hydraulic.hydraulic` with `foundation.isothermal_liquid.isothermal_liquid`.

2. In the `variables` section, replace the Through variable `q` with `mdot`. `q` represents volumetric flow rate and has units of volume over time, such as `m^3/s`. `mdot` represents mass flow rate and has units of mass over time, such as `kg/s`.

3. Add an intermediate, `rho`, to represent fluid density. Use the provided library function to calculate density based on pressure at the port, for blocks with a single fluid port, or based on average port pressure, for blocks with two or more fluid ports. To view the source file of this function, at the MATLAB® command prompt, type:

`open([matlabroot '/toolbox/physmod/simscape/library/m/+foundation/+isothermal_liquid/mixture_density.ssc'])`
4. Rewrite the equations by replacing `q` with `mdot/rho`.

For example, consider this custom component, which models a hydraulic linear resistance.

```component custom_linear_resistance % Custom Linear Hydraulic Resistance % This block represents a hydraulic resistance where pressure loss % is directly proportional to flow rate. % % Connections A and B are conserving hydraulic ports associated % with the block inlet and outlet, respectively. The block positive % direction is from port A to port B. This means that the flow rate is % positive if fluid flows from A to B, and the pressure loss is determined % as p = p_A - p_B. % Copyright 2005-2023 The MathWorks, Inc. nodes A = foundation.hydraulic.hydraulic; % A:left B = foundation.hydraulic.hydraulic; % B:right end variables (Access = protected) q = { 1e-3 , 'm^3/s' }; % Flow rate p = { 0 , 'Pa' }; % Pressure differential end branches q : A.q -> B.q; end parameters resistance = { 1, 'GPa/(m^3/s)' }; % Resistance end equations % Assertion assert(resistance >= 0) p == A.p - B.p; p == resistance * q; end end ```

To adapt this component to use the isothermal liquid domain:

1. Declare nodes `A` and `B` as ``` foundation.isothermal_liquid.isothermal_liquid```.

2. Under `variables`, replace `q` with `mdot`.

3. Add the `rho_avg` intermediate, which calculates density based on average port pressure. The density calculation uses the Foundation library function `foundation.isothermal_liquid.mixture_density`.

4. Rewrite the equation `p == resistance * q;` by replacing `q` with `mdot/rho_avg`.

The new component, `custom_linear_resistance_il`, now models an isothermal liquid linear resistance.

```component custom_linear_resistance_il % Custom Linear Resistance (IL) : % This block represents a hydraulic resistance where pressure loss % is directly proportional to flow rate. % % Connections A and B are conserving isothermal liquid ports associated % with the block inlet and outlet, respectively. The block positive % direction is from port A to port B. This means that the flow rate is % positive if fluid flows from A to B, and the pressure loss is determined % as p = p_A - p_B. % Copyright 2005-2023 The MathWorks, Inc. nodes A = foundation.isothermal_liquid.isothermal_liquid; % A:left B = foundation.isothermal_liquid.isothermal_liquid; % B:right end variables (Access = protected) mdot = { 0.1 , 'kg/s' }; % Mass flow rate p = { 0 , 'Pa' }; % Pressure differential end branches mdot : A.mdot -> B.mdot; end parameters resistance = { 1, 'GPa/(m^3/s)' }; % Resistance end % For logging intermediates (Access = private) rho_avg = foundation.isothermal_liquid.mixture_density((A.p + B.p)/2, ... A.bulk_modulus_model, A.air_dissolution_model, A.rho_L_atm, A.beta_L_atm, ... A.beta_gain, A.air_fraction, A.rho_g_atm, A.polytropic_index, A.p_atm, ... A.p_crit, A.p_min); % Average liquid density end equations % Assertion assert(resistance >= 0) p == A.p - B.p; p == resistance * mdot/rho_avg; end end ```