# Momentum balance implementation in Dymola

Written by: Maura Gallarotti – Project Engineer

When starting to work on fluids in Dymola, it is of paramount importance to build base classes where the momentum, energy and mass balances are performed, in order to have a common base class for several models and a strong foundation to build your own library.

A good starting point is to understand how the momentum, energy and mass balances are performed in the Modelica Standard Library Modelica.Fluid. Doing so, one will then be able to build his/her own models, making them compatible with the ones in MSL.

Let us examine a fairly simple model in MSL, the DynamicPipe: Figure 1: Screenshot of the component browser for the model DynamicPipe.

Looking at the Component Browser, one can see that the Model DynamicPipe extends from PartialStraightPipe and PartialTwoPortFlow. PartialTwoPortFlow extends from PartialTwoPort and PartialDistributedVolume.

The mass and energy balances are performed in PartialDistributedVolume, while the momentum balance is determined by the FlowModel component.

In this way, the momentum balance is performed using a staggered grid approach (See ‘How to avoid computationally expensive fluid networks in Dymola‘ Blog Post).

The total volume of the pipe is split into nNodes segments along the flow path and m=n-1 momentum balances are performed between n fluid volumes using an upwind discretization.

Let us have a look at the flow model DetailedPipeFlow: the model extends from PartialGenericPipeFlow, which in turn extends from PartialStaggeredFlowModel. Then, the model PartialStaggeredFlowModel extends from PartialDistributedFlow.

Basically, the model DetailedPipeFlow defines the complete regime of wall friction from laminar to turbulent calculating the fiction coefficient used in the formula .

PartialGenericPipeFlow defines a common interface for m=n-1 flow models between n device segments. The flow models provide a steady-state or dynamic momentum balance using an upwind discretization scheme per default.

The equations of the momentum balance can be found in PartialGenericPipeFlow and in the base class PartialDistributedFlow. Figure 2: Screenshot of the component browser for the model DetailedPipeFlow.

At first sight, the momentum balance equations implemented in Modelica.Fluid can seem a bit tricky, so let us start from a simplified case.

Here is a simplified version of the set of equations that one can find in PartialGenericPipeFlow and PartialDistributedFlow to ease understanding.

The analysed case is a pipe with constant cross sectional area, when the number of nodes is reduced to 2 (inlet and outlet).

(…)

`// Parameters`
```parameter SI.Length length "Pipe length";
parameter Integer nParallel "Number of identical parallel flow devices";
parameter SI.Length height_ab "Height(port_b) - Height(port_a)";
parameter Modelica.SIunits.Area crossArea "Inner cross-sectional area (constant)";```
```final parameter Modelica.SIunits.Acceleration g=system.g "Constant acceleration of gravity";

// Variables for momentum balance

(…)

```
```SI.MassFlowRate m_flow "Mass flow rate through the component";
SI.Pressure dp_fg "pressure drop due to friction + gravity forces";
SI.Momentum I "Momentum";
SI.Force Ib_flow "Flow of momentum across boudaries";
SI.Force F_p "Pressure force";
SI.Force F_fg "Friction and gravity force";

equation

(…)

```
```I = m_flow*length;

der(I) = Ib_flow - F_p - F_fg;

Ib_flow = 0

F_p = nParallel*crossArea*(port_b.p – port_a.p);

dp_fg = F_fg/nParallel/crossArea;

dp_fg = g*height_ab*rho + dp_friction;

(…)```

Now let us try to understand each term of the momentum balance, starting from Navier Stokes equations.

Newton’s second law F = m . a can be rewritten for a fluid particle as: In the 1D case, the balance is performed only in the x direction.

The total derivative for a discretization in the x direction can be expressed as: The right hand side of eq. (1) can be expressed as: In the inviscid case, the surface forces are only perpendicular to the surface of the element, so in this case the surface forces are those applied by pressure on the faces of the element normal to x. They can be expressed as: . Eq. 1 can be rewritten as: Multiplying all the terms by one obtains: Integrating along x between inlet (a) and outlet (b), one obtains: Where the friction losses are modelled as a volume momentum source homogeneous in the fluid element, coherently with the 1D approximation adopted (the momentum source is .

Eq. 2 reproduces exactly the equations used in PartialStaggeredFlowModel where Where contains both the pressure head and the friction losses: The Claytex.Fluid library has been built using the same approach as in Modelica.Fluid, in order to have a library fully compatible with the Modelica Standard Library components.

Several pressure drop models have been developed in the Claytex.Fluid library, such as:

• A model solving the unsteady Euler equations using the functions Swamee and Jain and Colebrook-White
• A model performing a quadratic interpolation around nominal conditions
• A model performing a linear interpolation around nominal conditions
• A model using a constant pressure loss coefficient
• A model using a constant pressure drop

Feel free to contact sales@claytex.com to get more information on the Claytex.Fluid library.

Please get in touch if you have any questions or have got a topic in mind that you would like us to write about. You can submit your questions / topics via: Tech Blog Questions / Topic Suggestion.