---
url: /dyad/dev/stdlib/FluidComponents/types/Pipes.BaseClasses.PartialTwoPortFlow.md
description: >-
  Dyad Pipes.BaseClasses.PartialTwoPortFlow docs.  Base class for distributed
  fluid flow with finite-volume storage and staggered momentum balances.
---

# `Pipes.BaseClasses.PartialTwoPortFlow` {#Pipes.BaseClasses.PartialTwoPortFlow}

Base class for distributed fluid flow with finite-volume storage and staggered momentum balances.

Compared with Modelica.Fluid, this component inlines the declarations from PartialTwoPort because Dyad does not currently accept the required multiple inheritance together with PartialDistributedVolume. The duplicated declarations preserve the same public port interface and must remain consistent with FluidComponents.Interfaces.PartialTwoPort.

Workaround: Dyad does not currently parse indexed array comprehensions such as `sum([crossAreas[i] for i in 1:iLumped - 1])`, so the Modelica slice sums used by the lumped-pressure branches cannot be written directly. The cumulative arrays below are algebraically equivalent prefix sums used to express those same totals and weighted averages in valid Dyad syntax.

This component extends from [`FluidComponents.Interfaces.PartialDistributedVolume`](./PartialDistributedVolume.md)

## Usage {#Usage}

`FluidComponents.Pipes.BaseClasses.PartialTwoPortFlow(p_a_start, p_b_start=p_a_start, ps_start=n > 1 ? FluidComponents.collect(FluidComponents.range(p_a_start, p_b_start, length = n)) : FluidComponents.fill((p_a_start + p_b_start) / 2, n), T_start, h_start, X_start=FluidComponents.MediaComponents.Interfaces.medium_X_default(medium_data), C_start=FluidComponents.MediaComponents.Interfaces.medium_C_default(medium_data), showDesignFlowDirection=true, nParallel, lengths, crossAreas, dimensions, roughnesses, dheights, m_flow_start, dp_nominal, m_flow_nominal, g=9.80665)`

## Parameters: {#Parameters:}

|                      Name | Description | Units |             Default value |
| -------------------------:| -----------:| -----:| -------------------------:|
|             `medium_data` |             |     – |                           |
|                       `n` |             |     – |                    nNodes |
|            `ThermoStates` |             |     – | FluidCompon...edium_data) |
|             `singleState` |             |     – | FluidCompon...edium_data) |
|                      `nX` |             |     – | FluidCompon...edium_data) |
|                     `nXi` |             |     – | FluidCompon...edium_data) |
|                      `nC` |             |     – | FluidCompon...edium_data) |
|          `energyDynamics` |             |     – |                           |
|            `massDynamics` |             |     – |                           |
|       `substanceDynamics` |             |     – |              massDynamics |
|           `traceDynamics` |             |     – |              massDynamics |
|             `use_T_start` |             |     – |                           |
|            `initialize_p` |             |     – |              !singleState |
|       `allowFlowReversal` |             |     – |                           |
|     `port_a_exposesState` |             |     – | (\_\_dyad_isa...ure.av_vb)) |
|     `port_b_exposesState` |             |     – | (\_\_dyad_isa...ure.av_vb)) |
|                  `nNodes` |             |     – |                           |
|          `modelStructure` |             |     – |                           |
|        `momentumDynamics` |             |     – |                           |
|       `useLumpedPressure` |             |     – |                     false |
|          `nFMDistributed` |             |     – | (\_\_dyad_isa...: (n - 1))) |
|               `nFMLumped` |             |     – | (\_\_dyad_isa...? (2) : (1) |
|                     `nFM` |             |     – | (useLumpedP...istributed) |
|              `nFM_plus_1` |             |     – |                   nFM + 1 |
|                `n_plus_1` |             |     – |                     n + 1 |
|                 `iLumped` |             |     – |             div(n, 2) + 1 |
|  `useInnerPortProperties` |             |     – |                     false |
|                `reducedX` |             |     – | MediaCompon...edium_data) |
|                  `fixedX` |             |     – | MediaCompon...edium_data) |
|               `p_a_start` |             |    Pa |                           |
|               `p_b_start` |             |    Pa |                 p_a_start |
|                 `T_start` |             |     K |                           |
|                 `h_start` |             |  J/kg |                           |
|                 `X_start` |             | kg/kg | FluidCompon...edium_data) |
|                 `C_start` |             |     – | FluidCompon...edium_data) |
| `showDesignFlowDirection` |             |     – |                      true |
|               `nParallel` |             |     – |                           |
|                 `lengths` |             |     m |                           |
|              `crossAreas` |             |    m2 |                           |
|              `dimensions` |             |     m |                           |
|             `roughnesses` |             |     m |                           |
|                `dheights` |             |     m |                           |
|            `m_flow_start` |             |  kg/s |                           |
|              `dp_nominal` |             |    Pa |                           |
|          `m_flow_nominal` |             |  kg/s |                           |
|                       `g` |             |  m/s2 |                   9.80665 |

## Connectors {#Connectors}

* `port_a` - Generic fluid connector at the design inlet. ([`FluidPort_a`](@ref))

* `port_b` - Generic fluid connector at the design outlet. ([`FluidPort_b`](@ref))

## Variables {#Variables}

|                        Name | Description | Units |
| ---------------------------:| -----------:| -----:|
|              `fluidVolumes` |             |    m3 |
|                        `Us` |             |     J |
|                        `ms` |             |    kg |
|                      `mXis` |             |    kg |
|                       `mCs` |             |    kg |
|                `mCs_scaled` |             |    kg |
|                        `Cs` |             |     – |
|                  `mb_flows` |             |  kg/s |
|                `mbXi_flows` |             |  kg/s |
|                 `mbC_flows` |             |  kg/s |
|                  `Hb_flows` |             |     W |
|                  `Qb_flows` |             |     W |
|                  `Wb_flows` |             |     W |
|                   `m_flows` |             |  kg/s |
|                 `mXi_flows` |             |  kg/s |
|                  `mC_flows` |             |  kg/s |
|                   `H_flows` |             |     W |
|                        `vs` |             |   m/s |
|               `pathLengths` |             |     m |
|                `dheightsFM` |             |     m |
|              `crossAreasFM` |             |    m2 |
|                      `vsFM` |             |   m/s |
|              `dimensionsFM` |             |     m |
|             `roughnessesFM` |             |     m |
|         `cumulativeLengths` |             |     m |
|        `cumulativeDheights` |             |     m |
|      `cumulativeCrossAreas` |             |    m2 |
|      `cumulativeDimensions` |             |     m |
|     `cumulativeRoughnesses` |             |     m |
| `cumulativeVelocityLengths` |             |  m2/s |

## Source {#Source}

```dyad
"""
Base class for distributed fluid flow with finite-volume storage and staggered momentum balances.

Compared with Modelica.Fluid, this component inlines the declarations from
PartialTwoPort because Dyad does not currently accept the required multiple
inheritance together with PartialDistributedVolume. The duplicated declarations
preserve the same public port interface and must remain consistent with
FluidComponents.Interfaces.PartialTwoPort.

Workaround: Dyad does not currently parse indexed array comprehensions such as
`sum([crossAreas[i] for i in 1:iLumped - 1])`, so the Modelica slice sums used
by the lumped-pressure branches cannot be written directly. The cumulative
arrays below are algebraically equivalent prefix sums used to express those
same totals and weighted averages in valid Dyad syntax.
"""
partial component PartialTwoPortFlow
  extends FluidComponents.Interfaces.PartialDistributedVolume(n = nNodes)
  # Whether flow may reverse from the positive design direction [-]
  structural parameter allowFlowReversal::Boolean
  # Fluid connector at the design inlet [-]
  port_a = FluidComponents.Interfaces.FluidPort_a(medium_data = medium_data) {
    "Dyad": {
      "placement": {
        "icon": {"iconName": "default", "x1": -50, "y1": 450, "x2": 50, "y2": 550, "rot": 0}
      }
    }
  }
  # Fluid connector at the design outlet [-]
  port_b = FluidComponents.Interfaces.FluidPort_b(medium_data = medium_data) {
    "Dyad": {
      "placement": {
        "icon": {"iconName": "default", "x1": 950, "y1": 450, "x2": 1050, "y2": 550, "rot": 0}
      }
    }
  }
  # Whether port_a exposes the state of an internal fluid volume [-]
  final structural parameter port_a_exposesState::Boolean = case(modelStructure, FluidComponents.Types.ModelStructure.av_b) or case(modelStructure, FluidComponents.Types.ModelStructure.av_vb)
  # Whether port_b exposes the state of an internal fluid volume [-]
  final structural parameter port_b_exposesState::Boolean = case(modelStructure, FluidComponents.Types.ModelStructure.a_vb) or case(modelStructure, FluidComponents.Types.ModelStructure.av_vb)
  # Whether to show the positive design-flow direction [-]
  parameter showDesignFlowDirection::Boolean = true
  # Number of discrete fluid volumes [-]
  structural parameter nNodes::Integer(min = 1)
  # Placement of momentum balances relative to the fluid volumes [-]
  structural parameter modelStructure::FluidComponents.Types.ModelStructure
  # Number of identical parallel flow devices [-]
  parameter nParallel::Real(min = 1)
  # Lengths of the fluid-volume segments [m]
  parameter lengths::Length[n]
  # Cross-sectional flow areas of the fluid-volume segments [m2]
  parameter crossAreas::Area[n]
  # Hydraulic diameters of the fluid-volume segments [m]
  parameter dimensions::Length[n]
  # Average surface roughnesses of the fluid-volume segments [m]
  parameter roughnesses::FluidComponents.Types.Roughness[n]
  # Height differences across the fluid-volume segments [m]
  parameter dheights::Length[n]
  # Formulation of the momentum balances [-]
  structural parameter momentumDynamics::FluidComponents.Types.Dynamics
  # Start value of the mass flow rate [kg/s]
  parameter m_flow_start::MediaComponents.Types.MassFlowRate
  # Whether all internal pressure states are lumped together [-]
  structural parameter useLumpedPressure::Boolean = false
  # Number of distributed momentum balances [-]
  final structural parameter nFMDistributed::Integer = case(modelStructure, FluidComponents.Types.ModelStructure.a_v_b) ? n + 1 : ((case(modelStructure, FluidComponents.Types.ModelStructure.a_vb) or case(modelStructure, FluidComponents.Types.ModelStructure.av_b)) ? n : n - 1)
  # Number of lumped momentum balances [-]
  final structural parameter nFMLumped::Integer = case(modelStructure, FluidComponents.Types.ModelStructure.a_v_b) ? 2 : 1
  # Number of momentum balances used by the flow model [-]
  final structural parameter nFM::Integer = useLumpedPressure ? nFMLumped : nFMDistributed
  # Number of thermodynamic states used by the flow model [-]
  final structural parameter nFM_plus_1::Integer = nFM + 1
  # Number of fluid-volume boundaries [-]
  final structural parameter n_plus_1::Integer = n + 1
  # Index of the representative volume for lumped pressure [-]
  final structural parameter iLumped::Integer = div(n, 2) + 1
  # Whether flow-model port states use adjacent internal-volume properties [-]
  structural parameter useInnerPortProperties::Boolean = false
  # Whether the medium mass fractions sum to one [-]
  final structural parameter reducedX::Boolean = MediaComponents.Interfaces.medium_reducedX(medium_data)
  # Whether the medium composition is fixed [-]
  final structural parameter fixedX::Boolean = MediaComponents.Interfaces.medium_fixedX(medium_data)
  # Nominal pressure loss used by the laminar flow model [Pa]
  parameter dp_nominal::MediaComponents.Types.AbsolutePressure
  # Mass flow rate at the nominal pressure loss [kg/s]
  parameter m_flow_nominal::MediaComponents.Types.MassFlowRate
  # Constant gravitational acceleration [m/s2]
  parameter g::Acceleration = 9.80665
  "Thermodynamic state immediately outside port_a."
  state_a = MediaComponents.Interfaces.ThermodynamicState(medium_data = medium_data)
  "Thermodynamic state immediately outside port_b."
  state_b = MediaComponents.Interfaces.ThermodynamicState(medium_data = medium_data)
  "Thermodynamic states supplied to the staggered flow model."
  statesFM = [MediaComponents.Interfaces.ThermodynamicState(medium_data = medium_data) for i in 1:nFM_plus_1]
  "Linear laminar wall-friction, gravity, and momentum model."
  flowModel = FluidComponents.Pipes.BaseClasses.FlowModels.NominalLaminarFlow(medium_data = medium_data, n = nFM_plus_1, momentumDynamics = momentumDynamics, allowFlowReversal = allowFlowReversal, p_a_start = p_a_start, p_b_start = p_b_start, m_flow_start = m_flow_start, nParallel = nParallel, g = g, dp_nominal = dp_nominal, m_flow_nominal = m_flow_nominal)
  # Mass flow rates across the fluid-volume boundaries [kg/s]
  variable m_flows::MediaComponents.Types.MassFlowRate(min = allowFlowReversal ? -Inf : 0)[n_plus_1]
  # Independent-substance mass flow rates across the boundaries [kg/s]
  variable mXi_flows::MediaComponents.Types.MassFlowRate[n_plus_1, nXi] if nXi > 0
  # Trace-substance flow rates across the boundaries [kg/s]
  variable mC_flows::MediaComponents.Types.MassFlowRate[n_plus_1, nC] if nC > 0
  # Enthalpy flow rates across the fluid-volume boundaries [W]
  variable H_flows::EnthalpyFlowRate[n_plus_1]
  # Mean velocities in the fluid-volume segments [m/s]
  variable vs::Velocity[n]
  # Lengths of the flow-model momentum segments [m]
  variable pathLengths::Length[nFM]
  # Height differences across the flow-model momentum segments [m]
  variable dheightsFM::Length[nFM]
  # Cross-sectional areas at the flow-model state locations [m2]
  variable crossAreasFM::Area[nFM_plus_1]
  # Mean velocities at the flow-model state locations [m/s]
  variable vsFM::Velocity[nFM_plus_1]
  # Hydraulic diameters at the flow-model state locations [m]
  variable dimensionsFM::Length[nFM_plus_1]
  # Surface roughnesses at the flow-model state locations [m]
  variable roughnessesFM::FluidComponents.Types.Roughness[nFM_plus_1]
  # Cumulative segment lengths used to scalarize Modelica slice sums [m]
  variable cumulativeLengths::Length[n_plus_1]
  # Cumulative height differences used to scalarize Modelica slice sums [m]
  variable cumulativeDheights::Length[n_plus_1]
  # Cumulative cross-sectional areas used to scalarize Modelica slice sums [m2]
  variable cumulativeCrossAreas::Area[n_plus_1]
  # Cumulative hydraulic diameters used to scalarize Modelica slice sums [m]
  variable cumulativeDimensions::Length[n_plus_1]
  # Cumulative surface roughnesses used to scalarize Modelica slice sums [m]
  variable cumulativeRoughnesses::Length[n_plus_1]
  # Cumulative velocity-length products used for lumped mean velocities [m2/s]
  variable cumulativeVelocityLengths::Real(units = "m2/s")[n_plus_1]
relations
  for i in 1:nFM_plus_1
    flowModel.states[i].p = statesFM[i].p
    flowModel.states[i].T = statesFM[i].T
    flowModel.states[i].d = statesFM[i].d
    flowModel.states[i].h = statesFM[i].h
    for j in 1:nX
      flowModel.states[i].X[j] = statesFM[i].X[j]
    end
    flowModel.vs[i] = vsFM[i]
    flowModel.crossAreas[i] = crossAreasFM[i]
    flowModel.dimensions[i] = dimensionsFM[i]
    flowModel.roughnesses[i] = roughnessesFM[i]
  end
  for i in 1:nFM
    flowModel.pathLengths[i] = pathLengths[i]
    flowModel.dheights[i] = dheightsFM[i]
  end
  cumulativeLengths[1] = 0
  cumulativeDheights[1] = 0
  cumulativeCrossAreas[1] = 0
  cumulativeDimensions[1] = 0
  cumulativeRoughnesses[1] = 0
  cumulativeVelocityLengths[1] = 0
  for i in 1:n
    cumulativeLengths[i + 1] = cumulativeLengths[i] + lengths[i]
    cumulativeDheights[i + 1] = cumulativeDheights[i] + dheights[i]
    cumulativeCrossAreas[i + 1] = cumulativeCrossAreas[i] + crossAreas[i]
    cumulativeDimensions[i + 1] = cumulativeDimensions[i] + dimensions[i]
    cumulativeRoughnesses[i + 1] = cumulativeRoughnesses[i] + roughnesses[i]
    cumulativeVelocityLengths[i + 1] = cumulativeVelocityLengths[i] + vs[i] * lengths[i]
  end
  if useLumpedPressure
    if !case(modelStructure, FluidComponents.Types.ModelStructure.a_v_b)
      pathLengths[1] = cumulativeLengths[n_plus_1]
      dheightsFM[1] = cumulativeDheights[n_plus_1]
      if n == 1
        crossAreasFM[1] = crossAreas[1]
        crossAreasFM[2] = crossAreas[1]
        dimensionsFM[1] = dimensions[1]
        dimensionsFM[2] = dimensions[1]
        roughnessesFM[1] = roughnesses[1]
        roughnessesFM[2] = roughnesses[1]
      else
        crossAreasFM[1] = cumulativeCrossAreas[iLumped] / (iLumped - 1)
        crossAreasFM[2] = (cumulativeCrossAreas[n_plus_1] - cumulativeCrossAreas[iLumped]) / (n - iLumped + 1)
        dimensionsFM[1] = cumulativeDimensions[iLumped] / (iLumped - 1)
        dimensionsFM[2] = (cumulativeDimensions[n_plus_1] - cumulativeDimensions[iLumped]) / (n - iLumped + 1)
        roughnessesFM[1] = cumulativeRoughnesses[iLumped] / (iLumped - 1)
        roughnessesFM[2] = (cumulativeRoughnesses[n_plus_1] - cumulativeRoughnesses[iLumped]) / (n - iLumped + 1)
      end
    else
      if n == 1
        pathLengths[1] = lengths[1] / 2
        pathLengths[2] = lengths[1] / 2
        dheightsFM[1] = dheights[1] / 2
        dheightsFM[2] = dheights[1] / 2
        for i in 1:3
          crossAreasFM[i] = crossAreas[1]
          dimensionsFM[i] = dimensions[1]
          roughnessesFM[i] = roughnesses[1]
        end
      else
        pathLengths[1] = cumulativeLengths[iLumped]
        pathLengths[2] = cumulativeLengths[n_plus_1] - cumulativeLengths[iLumped]
        dheightsFM[1] = cumulativeDheights[iLumped]
        dheightsFM[2] = cumulativeDheights[n_plus_1] - cumulativeDheights[iLumped]
        crossAreasFM[1] = cumulativeCrossAreas[iLumped] / (iLumped - 1)
        crossAreasFM[2] = cumulativeCrossAreas[n_plus_1] / n
        crossAreasFM[3] = (cumulativeCrossAreas[n_plus_1] - cumulativeCrossAreas[iLumped]) / (n - iLumped + 1)
        dimensionsFM[1] = cumulativeDimensions[iLumped] / (iLumped - 1)
        dimensionsFM[2] = cumulativeDimensions[n_plus_1] / n
        dimensionsFM[3] = (cumulativeDimensions[n_plus_1] - cumulativeDimensions[iLumped]) / (n - iLumped + 1)
        roughnessesFM[1] = cumulativeRoughnesses[iLumped] / (iLumped - 1)
        roughnessesFM[2] = cumulativeRoughnesses[n_plus_1] / n
        roughnessesFM[3] = (cumulativeRoughnesses[n_plus_1] - cumulativeRoughnesses[iLumped]) / (n - iLumped + 1)
      end
    end
  else
    if case(modelStructure, FluidComponents.Types.ModelStructure.av_vb)
      if n == 2
        pathLengths[1] = lengths[1] + lengths[2]
        dheightsFM[1] = dheights[1] + dheights[2]
      else
        pathLengths[1] = lengths[1] + 0.5 * lengths[2]
        dheightsFM[1] = dheights[1] + 0.5 * dheights[2]
        for i in 2:n - 2
          pathLengths[i] = 0.5 * (lengths[i] + lengths[i + 1])
          dheightsFM[i] = 0.5 * (dheights[i] + dheights[i + 1])
        end
        pathLengths[n - 1] = 0.5 * lengths[n - 1] + lengths[n]
        dheightsFM[n - 1] = 0.5 * dheights[n - 1] + dheights[n]
      end
      for i in 1:n
        crossAreasFM[i] = crossAreas[i]
        dimensionsFM[i] = dimensions[i]
        roughnessesFM[i] = roughnesses[i]
      end
    elseif case(modelStructure, FluidComponents.Types.ModelStructure.av_b)
      for i in 1:n
        pathLengths[i] = lengths[i]
        dheightsFM[i] = dheights[i]
        crossAreasFM[i] = crossAreas[i]
        dimensionsFM[i] = dimensions[i]
        roughnessesFM[i] = roughnesses[i]
      end
      crossAreasFM[n_plus_1] = crossAreas[n]
      dimensionsFM[n_plus_1] = dimensions[n]
      roughnessesFM[n_plus_1] = roughnesses[n]
    elseif case(modelStructure, FluidComponents.Types.ModelStructure.a_vb)
      crossAreasFM[1] = crossAreas[1]
      dimensionsFM[1] = dimensions[1]
      roughnessesFM[1] = roughnesses[1]
      for i in 1:n
        pathLengths[i] = lengths[i]
        dheightsFM[i] = dheights[i]
        crossAreasFM[i + 1] = crossAreas[i]
        dimensionsFM[i + 1] = dimensions[i]
        roughnessesFM[i + 1] = roughnesses[i]
      end
    else
      pathLengths[1] = 0.5 * lengths[1]
      dheightsFM[1] = 0.5 * dheights[1]
      crossAreasFM[1] = crossAreas[1]
      dimensionsFM[1] = dimensions[1]
      roughnessesFM[1] = roughnesses[1]
      for i in 2:n
        pathLengths[i] = 0.5 * (lengths[i - 1] + lengths[i])
        dheightsFM[i] = 0.5 * (dheights[i - 1] + dheights[i])
        crossAreasFM[i] = crossAreas[i - 1]
        dimensionsFM[i] = dimensions[i - 1]
        roughnessesFM[i] = roughnesses[i - 1]
      end
      pathLengths[n_plus_1] = 0.5 * lengths[n]
      dheightsFM[n_plus_1] = 0.5 * dheights[n]
      crossAreasFM[n_plus_1] = crossAreas[n]
      dimensionsFM[n_plus_1] = dimensions[n]
      roughnessesFM[n_plus_1] = roughnesses[n]
      crossAreasFM[n_plus_1 + 1] = crossAreas[n]
      dimensionsFM[n_plus_1 + 1] = dimensions[n]
      roughnessesFM[n_plus_1 + 1] = roughnesses[n]
    end
  end
  guess m_flows = fill(m_flow_start, n_plus_1)
  state_a.p = port_a.p
  state_b.p = port_b.p
  if useInnerPortProperties
    state_a.h = mediums[1].h
    state_b.h = mediums[n].h
    for j in 1:nX
      state_a.X[j] = mediums[1].X[j]
      state_b.X[j] = mediums[n].X[j]
    end
  else
    state_a.h = instream(port_a.h_outflow)
    state_b.h = instream(port_b.h_outflow)
    if fixedX
      for j in 1:nX
        state_a.X[j] = mediums[1].X[j]
        state_b.X[j] = mediums[n].X[j]
      end
    elseif reducedX
      if nXi > 0
        for j in 1:nXi
          state_a.X[j] = instream(port_a.Xi_outflow[j])
          state_b.X[j] = instream(port_b.Xi_outflow[j])
        end
        state_a.X[nX] = 1 - sum(instream(port_a.Xi_outflow))
        state_b.X[nX] = 1 - sum(instream(port_b.Xi_outflow))
      else
        state_a.X[nX] = 1
        state_b.X[nX] = 1
      end
    else
      for j in 1:nX
        state_a.X[j] = instream(port_a.Xi_outflow[j])
        state_b.X[j] = instream(port_b.Xi_outflow[j])
      end
    end
  end
  state_a.T = MediaComponents.Interfaces.temperature_phX(medium_data, state_a.p, state_a.h, state_a.X)
  state_a.d = MediaComponents.Interfaces.density_phX(medium_data, state_a.p, state_a.h, state_a.X)
  state_b.T = MediaComponents.Interfaces.temperature_phX(medium_data, state_b.p, state_b.h, state_b.X)
  state_b.d = MediaComponents.Interfaces.density_phX(medium_data, state_b.p, state_b.h, state_b.X)
  for i in 1:n
    fluidVolumes[i] = crossAreas[i] * lengths[i] * nParallel
    vs[i] = 0.5 * (m_flows[i] + m_flows[i + 1]) / mediums[i].d / crossAreas[i] / nParallel
    mb_flows[i] = m_flows[i] - m_flows[i + 1]
    Hb_flows[i] = H_flows[i] - H_flows[i + 1]
    if nXi > 0
      for j in 1:nXi
        mbXi_flows[i, j] = mXi_flows[i, j] - mXi_flows[i + 1, j]
      end
    end
    if nC > 0
      for j in 1:nC
        mbC_flows[i, j] = mC_flows[i, j] - mC_flows[i + 1, j]
      end
    end
  end
  for i in 2:n
    H_flows[i] = FluidComponents.semilinear(m_flows[i], mediums[i - 1].h, mediums[i].h)
    if nXi > 0
      for j in 1:nXi
        mXi_flows[i, j] = FluidComponents.semilinear(m_flows[i], mediums[i - 1].Xi[j], mediums[i].Xi[j])
      end
    end
    if nC > 0
      for j in 1:nC
        mC_flows[i, j] = FluidComponents.semilinear(m_flows[i], Cs[i - 1, j], Cs[i, j])
      end
    end
  end
  H_flows[1] = FluidComponents.semilinear(port_a.m_flow, instream(port_a.h_outflow), mediums[1].h)
  H_flows[n_plus_1] = -FluidComponents.semilinear(port_b.m_flow, instream(port_b.h_outflow), mediums[n].h)
  if nXi > 0
    for j in 1:nXi
      mXi_flows[1, j] = FluidComponents.semilinear(port_a.m_flow, instream(port_a.Xi_outflow[j]), mediums[1].Xi[j])
      mXi_flows[n_plus_1, j] = -FluidComponents.semilinear(port_b.m_flow, instream(port_b.Xi_outflow[j]), mediums[n].Xi[j])
    end
  end
  if nC > 0
    for j in 1:nC
      mC_flows[1, j] = FluidComponents.semilinear(port_a.m_flow, instream(port_a.C_outflow[j]), Cs[1, j])
      mC_flows[n_plus_1, j] = -FluidComponents.semilinear(port_b.m_flow, instream(port_b.C_outflow[j]), Cs[n, j])
    end
  end
  port_a.m_flow = m_flows[1]
  port_b.m_flow = -m_flows[n_plus_1]
  port_a.h_outflow = mediums[1].h
  port_b.h_outflow = mediums[n].h
  if nXi > 0
    for j in 1:nXi
      port_a.Xi_outflow[j] = mediums[1].Xi[j]
      port_b.Xi_outflow[j] = mediums[n].Xi[j]
    end
  else
    port_a.Xi_outflow[1] = 0
    port_b.Xi_outflow[1] = 0
  end
  # Trace properties are stored in the volumes and therefore use the same
  # adjacent-volume outflow convention as the independent mass fractions.
  if nC > 0
    for j in 1:nC
      port_a.C_outflow[j] = Cs[1, j]
      port_b.C_outflow[j] = Cs[n, j]
    end
  else
    port_a.C_outflow[1] = 0
    port_b.C_outflow[1] = 0
  end
  if useLumpedPressure
    if !case(modelStructure, FluidComponents.Types.ModelStructure.av_vb)
      for i in 2:n
        mediums[i].p = mediums[1].p
      end
    elseif n > 2
      for i in 2:iLumped - 1
        mediums[i].p = mediums[1].p
      end
      for i in iLumped:n - 1
        mediums[i].p = mediums[n].p
      end
    end
    if case(modelStructure, FluidComponents.Types.ModelStructure.av_vb)
      port_a.p = mediums[1].p
      port_b.p = mediums[n].p
      statesFM[1].p = mediums[1].p
      statesFM[1].T = mediums[1].T
      statesFM[1].d = mediums[1].d
      statesFM[1].h = mediums[1].h
      statesFM[2].p = mediums[n].p
      statesFM[2].T = mediums[n].T
      statesFM[2].d = mediums[n].d
      statesFM[2].h = mediums[n].h
      for j in 1:nX
        statesFM[1].X[j] = mediums[1].X[j]
        statesFM[2].X[j] = mediums[n].X[j]
      end
      m_flows[iLumped] = flowModel.m_flows[1]
      vsFM[1] = cumulativeVelocityLengths[iLumped] / cumulativeLengths[iLumped]
      vsFM[2] = (cumulativeVelocityLengths[n_plus_1] - cumulativeVelocityLengths[iLumped]) / (cumulativeLengths[n_plus_1] - cumulativeLengths[iLumped])
    elseif case(modelStructure, FluidComponents.Types.ModelStructure.av_b)
      port_a.p = mediums[1].p
      statesFM[1].p = mediums[iLumped].p
      statesFM[1].T = mediums[iLumped].T
      statesFM[1].d = mediums[iLumped].d
      statesFM[1].h = mediums[iLumped].h
      statesFM[2].p = state_b.p
      statesFM[2].T = state_b.T
      statesFM[2].d = state_b.d
      statesFM[2].h = state_b.h
      for j in 1:nX
        statesFM[1].X[j] = mediums[iLumped].X[j]
        statesFM[2].X[j] = state_b.X[j]
      end
      m_flows[n_plus_1] = flowModel.m_flows[1]
      vsFM[1] = cumulativeVelocityLengths[n_plus_1] / cumulativeLengths[n_plus_1]
      vsFM[2] = m_flows[n_plus_1] / state_b.d / crossAreas[n] / nParallel
    elseif case(modelStructure, FluidComponents.Types.ModelStructure.a_vb)
      port_b.p = mediums[n].p
      statesFM[1].p = state_a.p
      statesFM[1].T = state_a.T
      statesFM[1].d = state_a.d
      statesFM[1].h = state_a.h
      statesFM[2].p = mediums[iLumped].p
      statesFM[2].T = mediums[iLumped].T
      statesFM[2].d = mediums[iLumped].d
      statesFM[2].h = mediums[iLumped].h
      for j in 1:nX
        statesFM[1].X[j] = state_a.X[j]
        statesFM[2].X[j] = mediums[iLumped].X[j]
      end
      m_flows[1] = flowModel.m_flows[1]
      vsFM[1] = m_flows[1] / state_a.d / crossAreas[1] / nParallel
      vsFM[2] = cumulativeVelocityLengths[n_plus_1] / cumulativeLengths[n_plus_1]
    else
      statesFM[1].p = state_a.p
      statesFM[1].T = state_a.T
      statesFM[1].d = state_a.d
      statesFM[1].h = state_a.h
      statesFM[2].p = mediums[iLumped].p
      statesFM[2].T = mediums[iLumped].T
      statesFM[2].d = mediums[iLumped].d
      statesFM[2].h = mediums[iLumped].h
      statesFM[3].p = state_b.p
      statesFM[3].T = state_b.T
      statesFM[3].d = state_b.d
      statesFM[3].h = state_b.h
      for j in 1:nX
        statesFM[1].X[j] = state_a.X[j]
        statesFM[2].X[j] = mediums[iLumped].X[j]
        statesFM[3].X[j] = state_b.X[j]
      end
      m_flows[1] = flowModel.m_flows[1]
      m_flows[n_plus_1] = flowModel.m_flows[2]
      vsFM[1] = m_flows[1] / state_a.d / crossAreas[1] / nParallel
      vsFM[2] = cumulativeVelocityLengths[n_plus_1] / cumulativeLengths[n_plus_1]
      vsFM[3] = m_flows[n_plus_1] / state_b.d / crossAreas[n] / nParallel
    end
  else
    if case(modelStructure, FluidComponents.Types.ModelStructure.av_vb)
      for i in 1:n
        statesFM[i].p = mediums[i].p
        statesFM[i].T = mediums[i].T
        statesFM[i].d = mediums[i].d
        statesFM[i].h = mediums[i].h
        vsFM[i] = vs[i]
        for j in 1:nX
          statesFM[i].X[j] = mediums[i].X[j]
        end
      end
      for i in 1:n - 1
        m_flows[i + 1] = flowModel.m_flows[i]
      end
      port_a.p = mediums[1].p
      port_b.p = mediums[n].p
    elseif case(modelStructure, FluidComponents.Types.ModelStructure.av_b)
      for i in 1:n
        statesFM[i].p = mediums[i].p
        statesFM[i].T = mediums[i].T
        statesFM[i].d = mediums[i].d
        statesFM[i].h = mediums[i].h
        vsFM[i] = vs[i]
        for j in 1:nX
          statesFM[i].X[j] = mediums[i].X[j]
        end
      end
      statesFM[n_plus_1].p = state_b.p
      statesFM[n_plus_1].T = state_b.T
      statesFM[n_plus_1].d = state_b.d
      statesFM[n_plus_1].h = state_b.h
      for j in 1:nX
        statesFM[n_plus_1].X[j] = state_b.X[j]
      end
      for i in 1:n
        m_flows[i + 1] = flowModel.m_flows[i]
      end
      vsFM[n_plus_1] = m_flows[n_plus_1] / state_b.d / crossAreas[n] / nParallel
      port_a.p = mediums[1].p
    elseif case(modelStructure, FluidComponents.Types.ModelStructure.a_vb)
      statesFM[1].p = state_a.p
      statesFM[1].T = state_a.T
      statesFM[1].d = state_a.d
      statesFM[1].h = state_a.h
      vsFM[1] = m_flows[1] / state_a.d / crossAreas[1] / nParallel
      for j in 1:nX
        statesFM[1].X[j] = state_a.X[j]
      end
      for i in 1:n
        statesFM[i + 1].p = mediums[i].p
        statesFM[i + 1].T = mediums[i].T
        statesFM[i + 1].d = mediums[i].d
        statesFM[i + 1].h = mediums[i].h
        m_flows[i] = flowModel.m_flows[i]
        vsFM[i + 1] = vs[i]
        for j in 1:nX
          statesFM[i + 1].X[j] = mediums[i].X[j]
        end
      end
      port_b.p = mediums[n].p
    else
      statesFM[1].p = state_a.p
      statesFM[1].T = state_a.T
      statesFM[1].d = state_a.d
      statesFM[1].h = state_a.h
      vsFM[1] = m_flows[1] / state_a.d / crossAreas[1] / nParallel
      for j in 1:nX
        statesFM[1].X[j] = state_a.X[j]
      end
      for i in 1:n
        statesFM[i + 1].p = mediums[i].p
        statesFM[i + 1].T = mediums[i].T
        statesFM[i + 1].d = mediums[i].d
        statesFM[i + 1].h = mediums[i].h
        m_flows[i] = flowModel.m_flows[i]
        vsFM[i + 1] = vs[i]
        for j in 1:nX
          statesFM[i + 1].X[j] = mediums[i].X[j]
        end
      end
      m_flows[n_plus_1] = flowModel.m_flows[n_plus_1]
      statesFM[n_plus_1 + 1].p = state_b.p
      statesFM[n_plus_1 + 1].T = state_b.T
      statesFM[n_plus_1 + 1].d = state_b.d
      statesFM[n_plus_1 + 1].h = state_b.h
      vsFM[n_plus_1 + 1] = m_flows[n_plus_1] / state_b.d / crossAreas[n] / nParallel
      for j in 1:nX
        statesFM[n_plus_1 + 1].X[j] = state_b.X[j]
      end
    end
  end
end
```

```dyad
"""
Base class for distributed fluid flow with finite-volume storage and staggered momentum balances.

Compared with Modelica.Fluid, this component inlines the declarations from
PartialTwoPort because Dyad does not currently accept the required multiple
inheritance together with PartialDistributedVolume. The duplicated declarations
preserve the same public port interface and must remain consistent with
FluidComponents.Interfaces.PartialTwoPort.

Workaround: Dyad does not currently parse indexed array comprehensions such as
`sum([crossAreas[i] for i in 1:iLumped - 1])`, so the Modelica slice sums used
by the lumped-pressure branches cannot be written directly. The cumulative
arrays below are algebraically equivalent prefix sums used to express those
same totals and weighted averages in valid Dyad syntax.
"""
partial component PartialTwoPortFlow
  # Immutable medium definition selected at model construction [-]
  structural parameter medium_data::MediaComponents.Interfaces.AbstractPartialMediumData
  # Number of discrete fluid volumes [-]
  structural parameter n::Integer(min = 1)
  # Choice of independent thermodynamic variables [-]
  final structural parameter ThermoStates::MediaComponents.Interfaces.Choices.IndependentVariables = MediaComponents.Interfaces.medium_thermo_states(medium_data)
  # Whether internal energy and density are independent of pressure [-]
  final structural parameter singleState::Boolean = MediaComponents.Interfaces.medium_single_state(medium_data)
  # Number of mass fractions [-]
  final structural parameter nX::Integer = MediaComponents.Interfaces.medium_nX(medium_data)
  # Number of independent mass fractions [-]
  final structural parameter nXi::Integer = MediaComponents.Interfaces.medium_nXi(medium_data)
  # Number of extra transported properties [-]
  final structural parameter nC::Integer = MediaComponents.Interfaces.medium_nC(medium_data)
  # Formulation of the energy balances [-]
  structural parameter energyDynamics::FluidComponents.Types.Dynamics
  # Formulation of the mass balances [-]
  structural parameter massDynamics::FluidComponents.Types.Dynamics
  # Formulation of the substance balances [-]
  final structural parameter substanceDynamics::FluidComponents.Types.Dynamics = massDynamics
  # Formulation of the trace-substance balances [-]
  final structural parameter traceDynamics::FluidComponents.Types.Dynamics = massDynamics
  # Start value of pressure at port a [Pa]
  parameter p_a_start::MediaComponents.Types.AbsolutePressure
  # Start value of pressure at port b [Pa]
  parameter p_b_start::MediaComponents.Types.AbsolutePressure = p_a_start
  # Start values of pressure across the fluid volumes [Pa]
  final parameter ps_start::MediaComponents.Types.AbsolutePressure[n] = n > 1 ? collect(range(p_a_start, p_b_start, length = n)) : fill((p_a_start + p_b_start) / 2, n)
  # Whether temperature rather than specific enthalpy supplied the start state [-]
  structural parameter use_T_start::Boolean
  # Start value of temperature [K]
  parameter T_start::MediaComponents.Types.Temperature
  # Start value of specific enthalpy [J/kg]
  parameter h_start::MediaComponents.Types.SpecificEnthalpy
  # Start values of mass fractions [kg/kg]
  parameter X_start::MediaComponents.Types.MassFraction[nX] = MediaComponents.Interfaces.medium_X_default(medium_data)
  # Start values of trace substances [-]
  parameter C_start::MediaComponents.Types.ExtraProperty[nC] = MediaComponents.Interfaces.medium_C_default(medium_data) if nC > 0
  # Discretized fluid volumes supplied by the extending component [m3]
  variable fluidVolumes::Volume[n]
  # Internal energy stored in each fluid volume [J]
  variable Us::Energy[n]
  # Fluid mass stored in each fluid volume [kg]
  variable ms::Mass[n]
  # Independent-substance mass stored in each fluid volume [kg]
  variable mXis::Mass[n, nXi] if nXi > 0
  # Trace-substance mass stored in each fluid volume [kg]
  variable mCs::Mass[n, nC] if nC > 0
  # Scaled trace-substance mass state [kg]
  variable mCs_scaled::Mass[n, nC] if nC > 0
  # Trace-substance mixture content [-]
  variable Cs::MediaComponents.Types.ExtraProperty[n, nC] if nC > 0
  "Thermodynamic properties for each fluid volume."
  mediums = [MediaComponents.Interfaces.BaseProperties(medium_data = medium_data, preferredMediumStates = true) for i in 1:n]
  # Mass source flow for each fluid volume [kg/s]
  variable mb_flows::MediaComponents.Types.MassFlowRate[n]
  # Independent-substance source flow for each fluid volume [kg/s]
  variable mbXi_flows::MediaComponents.Types.MassFlowRate[n, nXi] if nXi > 0
  # Trace-substance source flow for each fluid volume [kg/s]
  variable mbC_flows::MediaComponents.Types.ExtraPropertyFlowRate[n, nC] if nC > 0
  # Enthalpy source flow for each fluid volume [W]
  variable Hb_flows::EnthalpyFlowRate[n]
  # Heat source flow for each fluid volume [W]
  variable Qb_flows::HeatFlowRate[n]
  # Mechanical power supplied to each fluid volume [W]
  variable Wb_flows::Power[n]
  # Whether pressure requires an explicit initialization equation [-]
  final structural parameter initialize_p::Boolean = !singleState
  # Whether flow may reverse from the positive design direction [-]
  structural parameter allowFlowReversal::Boolean
  # Fluid connector at the design inlet [-]
  port_a = FluidComponents.Interfaces.FluidPort_a(medium_data = medium_data) {
    "Dyad": {
      "placement": {
        "icon": {"iconName": "default", "x1": -50, "y1": 450, "x2": 50, "y2": 550, "rot": 0}
      }
    }
  }
  # Fluid connector at the design outlet [-]
  port_b = FluidComponents.Interfaces.FluidPort_b(medium_data = medium_data) {
    "Dyad": {
      "placement": {
        "icon": {"iconName": "default", "x1": 950, "y1": 450, "x2": 1050, "y2": 550, "rot": 0}
      }
    }
  }
  # Whether port_a exposes the state of an internal fluid volume [-]
  final structural parameter port_a_exposesState::Boolean = case(modelStructure, FluidComponents.Types.ModelStructure.av_b) or case(modelStructure, FluidComponents.Types.ModelStructure.av_vb)
  # Whether port_b exposes the state of an internal fluid volume [-]
  final structural parameter port_b_exposesState::Boolean = case(modelStructure, FluidComponents.Types.ModelStructure.a_vb) or case(modelStructure, FluidComponents.Types.ModelStructure.av_vb)
  # Whether to show the positive design-flow direction [-]
  parameter showDesignFlowDirection::Boolean = true
  # Number of discrete fluid volumes [-]
  structural parameter nNodes::Integer(min = 1)
  # Placement of momentum balances relative to the fluid volumes [-]
  structural parameter modelStructure::FluidComponents.Types.ModelStructure
  # Number of identical parallel flow devices [-]
  parameter nParallel::Real(min = 1)
  # Lengths of the fluid-volume segments [m]
  parameter lengths::Length[n]
  # Cross-sectional flow areas of the fluid-volume segments [m2]
  parameter crossAreas::Area[n]
  # Hydraulic diameters of the fluid-volume segments [m]
  parameter dimensions::Length[n]
  # Average surface roughnesses of the fluid-volume segments [m]
  parameter roughnesses::FluidComponents.Types.Roughness[n]
  # Height differences across the fluid-volume segments [m]
  parameter dheights::Length[n]
  # Formulation of the momentum balances [-]
  structural parameter momentumDynamics::FluidComponents.Types.Dynamics
  # Start value of the mass flow rate [kg/s]
  parameter m_flow_start::MediaComponents.Types.MassFlowRate
  # Whether all internal pressure states are lumped together [-]
  structural parameter useLumpedPressure::Boolean = false
  # Number of distributed momentum balances [-]
  final structural parameter nFMDistributed::Integer = case(modelStructure, FluidComponents.Types.ModelStructure.a_v_b) ? n + 1 : ((case(modelStructure, FluidComponents.Types.ModelStructure.a_vb) or case(modelStructure, FluidComponents.Types.ModelStructure.av_b)) ? n : n - 1)
  # Number of lumped momentum balances [-]
  final structural parameter nFMLumped::Integer = case(modelStructure, FluidComponents.Types.ModelStructure.a_v_b) ? 2 : 1
  # Number of momentum balances used by the flow model [-]
  final structural parameter nFM::Integer = useLumpedPressure ? nFMLumped : nFMDistributed
  # Number of thermodynamic states used by the flow model [-]
  final structural parameter nFM_plus_1::Integer = nFM + 1
  # Number of fluid-volume boundaries [-]
  final structural parameter n_plus_1::Integer = n + 1
  # Index of the representative volume for lumped pressure [-]
  final structural parameter iLumped::Integer = div(n, 2) + 1
  # Whether flow-model port states use adjacent internal-volume properties [-]
  structural parameter useInnerPortProperties::Boolean = false
  # Whether the medium mass fractions sum to one [-]
  final structural parameter reducedX::Boolean = MediaComponents.Interfaces.medium_reducedX(medium_data)
  # Whether the medium composition is fixed [-]
  final structural parameter fixedX::Boolean = MediaComponents.Interfaces.medium_fixedX(medium_data)
  # Nominal pressure loss used by the laminar flow model [Pa]
  parameter dp_nominal::MediaComponents.Types.AbsolutePressure
  # Mass flow rate at the nominal pressure loss [kg/s]
  parameter m_flow_nominal::MediaComponents.Types.MassFlowRate
  # Constant gravitational acceleration [m/s2]
  parameter g::Acceleration = 9.80665
  "Thermodynamic state immediately outside port_a."
  state_a = MediaComponents.Interfaces.ThermodynamicState(medium_data = medium_data)
  "Thermodynamic state immediately outside port_b."
  state_b = MediaComponents.Interfaces.ThermodynamicState(medium_data = medium_data)
  "Thermodynamic states supplied to the staggered flow model."
  statesFM = [MediaComponents.Interfaces.ThermodynamicState(medium_data = medium_data) for i in 1:nFM_plus_1]
  "Linear laminar wall-friction, gravity, and momentum model."
  flowModel = FluidComponents.Pipes.BaseClasses.FlowModels.NominalLaminarFlow(medium_data = medium_data, n = nFM_plus_1, momentumDynamics = momentumDynamics, allowFlowReversal = allowFlowReversal, p_a_start = p_a_start, p_b_start = p_b_start, m_flow_start = m_flow_start, nParallel = nParallel, g = g, dp_nominal = dp_nominal, m_flow_nominal = m_flow_nominal)
  # Mass flow rates across the fluid-volume boundaries [kg/s]
  variable m_flows::MediaComponents.Types.MassFlowRate(min = allowFlowReversal ? -Inf : 0)[n_plus_1]
  # Independent-substance mass flow rates across the boundaries [kg/s]
  variable mXi_flows::MediaComponents.Types.MassFlowRate[n_plus_1, nXi] if nXi > 0
  # Trace-substance flow rates across the boundaries [kg/s]
  variable mC_flows::MediaComponents.Types.MassFlowRate[n_plus_1, nC] if nC > 0
  # Enthalpy flow rates across the fluid-volume boundaries [W]
  variable H_flows::EnthalpyFlowRate[n_plus_1]
  # Mean velocities in the fluid-volume segments [m/s]
  variable vs::Velocity[n]
  # Lengths of the flow-model momentum segments [m]
  variable pathLengths::Length[nFM]
  # Height differences across the flow-model momentum segments [m]
  variable dheightsFM::Length[nFM]
  # Cross-sectional areas at the flow-model state locations [m2]
  variable crossAreasFM::Area[nFM_plus_1]
  # Mean velocities at the flow-model state locations [m/s]
  variable vsFM::Velocity[nFM_plus_1]
  # Hydraulic diameters at the flow-model state locations [m]
  variable dimensionsFM::Length[nFM_plus_1]
  # Surface roughnesses at the flow-model state locations [m]
  variable roughnessesFM::FluidComponents.Types.Roughness[nFM_plus_1]
  # Cumulative segment lengths used to scalarize Modelica slice sums [m]
  variable cumulativeLengths::Length[n_plus_1]
  # Cumulative height differences used to scalarize Modelica slice sums [m]
  variable cumulativeDheights::Length[n_plus_1]
  # Cumulative cross-sectional areas used to scalarize Modelica slice sums [m2]
  variable cumulativeCrossAreas::Area[n_plus_1]
  # Cumulative hydraulic diameters used to scalarize Modelica slice sums [m]
  variable cumulativeDimensions::Length[n_plus_1]
  # Cumulative surface roughnesses used to scalarize Modelica slice sums [m]
  variable cumulativeRoughnesses::Length[n_plus_1]
  # Cumulative velocity-length products used for lumped mean velocities [m2/s]
  variable cumulativeVelocityLengths::Real(units = "m2/s")[n_plus_1]
relations
  for i in 1:n
    guess mediums[i].p = ps_start[i]
    guess mediums[i].h = h_start
    guess mediums[i].T = T_start
    ms[i] = fluidVolumes[i] * mediums[i].d
    Us[i] = ms[i] * mediums[i].u
    if nXi > 0
      for j in 1:nXi
        guess mediums[i].Xi[j] = X_start[j]
        mXis[i, j] = ms[i] * mediums[i].Xi[j]
      end
    end
    if nC > 0
      for j in 1:nC
        mCs[i, j] = ms[i] * Cs[i, j]
      end
    end
  end
  if case(energyDynamics, FluidComponents.Types.Dynamics.SteadyState)
    for i in 1:n
      0 = Hb_flows[i] + Wb_flows[i] + Qb_flows[i]
    end
  else
    for i in 1:n
      der(Us[i]) = Hb_flows[i] + Wb_flows[i] + Qb_flows[i]
    end
  end
  if case(massDynamics, FluidComponents.Types.Dynamics.SteadyState)
    for i in 1:n
      0 = mb_flows[i]
    end
  else
    for i in 1:n
      der(ms[i]) = mb_flows[i]
    end
  end
  if nXi > 0
    if case(substanceDynamics, FluidComponents.Types.Dynamics.SteadyState)
      for i in 1:n
        for j in 1:nXi
          0 = mbXi_flows[i, j]
        end
      end
    else
      for i in 1:n
        for j in 1:nXi
          der(mXis[i, j]) = mbXi_flows[i, j]
        end
      end
    end
  end
  if nC > 0
    if case(traceDynamics, FluidComponents.Types.Dynamics.SteadyState)
      for i in 1:n
        for j in 1:nC
          0 = mbC_flows[i, j]
        end
      end
    else
      for i in 1:n
        for j in 1:nC
          der(mCs_scaled[i, j]) = mbC_flows[i, j] / MediaComponents.Interfaces.medium_C_nominal_at(medium_data, j)
          mCs[i, j] = mCs_scaled[i, j] * MediaComponents.Interfaces.medium_C_nominal_at(medium_data, j)
        end
      end
    end
  end
  if case(energyDynamics, FluidComponents.Types.Dynamics.FixedInitial)
    if case(ThermoStates, MediaComponents.Interfaces.Choices.IndependentVariables.ph) or case(ThermoStates, MediaComponents.Interfaces.Choices.IndependentVariables.phX)
      for i in 1:n
        initial mediums[i].h = h_start
      end
    else
      for i in 1:n
        initial mediums[i].T = T_start
      end
    end
  end
  if case(energyDynamics, FluidComponents.Types.Dynamics.SteadyStateInitial)
    if case(ThermoStates, MediaComponents.Interfaces.Choices.IndependentVariables.ph) or case(ThermoStates, MediaComponents.Interfaces.Choices.IndependentVariables.phX)
      for i in 1:n
        initial der(mediums[i].h) = 0
      end
    else
      for i in 1:n
        initial der(mediums[i].T) = 0
      end
    end
  end
  if initialize_p
    if case(massDynamics, FluidComponents.Types.Dynamics.FixedInitial)
      for i in 1:n
        initial mediums[i].p = ps_start[i]
      end
    end
    if case(massDynamics, FluidComponents.Types.Dynamics.SteadyStateInitial)
      for i in 1:n
        initial der(mediums[i].p) = 0
      end
    end
  end
  if nXi > 0
    if case(substanceDynamics, FluidComponents.Types.Dynamics.FixedInitial)
      for i in 1:n
        for j in 1:nXi
          initial mediums[i].Xi[j] = X_start[j]
        end
      end
    end
    if case(substanceDynamics, FluidComponents.Types.Dynamics.SteadyStateInitial)
      for i in 1:n
        for j in 1:nXi
          initial der(mediums[i].Xi[j]) = 0
        end
      end
    end
  end
  if nC > 0
    if case(traceDynamics, FluidComponents.Types.Dynamics.FixedInitial)
      for i in 1:n
        for j in 1:nC
          initial Cs[i, j] = C_start[j]
        end
      end
    end
    if case(traceDynamics, FluidComponents.Types.Dynamics.SteadyStateInitial)
      for i in 1:n
        for j in 1:nC
          initial der(mCs[i, j]) = 0
        end
      end
    end
  end
  for i in 1:nFM_plus_1
    flowModel.states[i].p = statesFM[i].p
    flowModel.states[i].T = statesFM[i].T
    flowModel.states[i].d = statesFM[i].d
    flowModel.states[i].h = statesFM[i].h
    for j in 1:nX
      flowModel.states[i].X[j] = statesFM[i].X[j]
    end
    flowModel.vs[i] = vsFM[i]
    flowModel.crossAreas[i] = crossAreasFM[i]
    flowModel.dimensions[i] = dimensionsFM[i]
    flowModel.roughnesses[i] = roughnessesFM[i]
  end
  for i in 1:nFM
    flowModel.pathLengths[i] = pathLengths[i]
    flowModel.dheights[i] = dheightsFM[i]
  end
  cumulativeLengths[1] = 0
  cumulativeDheights[1] = 0
  cumulativeCrossAreas[1] = 0
  cumulativeDimensions[1] = 0
  cumulativeRoughnesses[1] = 0
  cumulativeVelocityLengths[1] = 0
  for i in 1:n
    cumulativeLengths[i + 1] = cumulativeLengths[i] + lengths[i]
    cumulativeDheights[i + 1] = cumulativeDheights[i] + dheights[i]
    cumulativeCrossAreas[i + 1] = cumulativeCrossAreas[i] + crossAreas[i]
    cumulativeDimensions[i + 1] = cumulativeDimensions[i] + dimensions[i]
    cumulativeRoughnesses[i + 1] = cumulativeRoughnesses[i] + roughnesses[i]
    cumulativeVelocityLengths[i + 1] = cumulativeVelocityLengths[i] + vs[i] * lengths[i]
  end
  if useLumpedPressure
    if !case(modelStructure, FluidComponents.Types.ModelStructure.a_v_b)
      pathLengths[1] = cumulativeLengths[n_plus_1]
      dheightsFM[1] = cumulativeDheights[n_plus_1]
      if n == 1
        crossAreasFM[1] = crossAreas[1]
        crossAreasFM[2] = crossAreas[1]
        dimensionsFM[1] = dimensions[1]
        dimensionsFM[2] = dimensions[1]
        roughnessesFM[1] = roughnesses[1]
        roughnessesFM[2] = roughnesses[1]
      else
        crossAreasFM[1] = cumulativeCrossAreas[iLumped] / (iLumped - 1)
        crossAreasFM[2] = (cumulativeCrossAreas[n_plus_1] - cumulativeCrossAreas[iLumped]) / (n - iLumped + 1)
        dimensionsFM[1] = cumulativeDimensions[iLumped] / (iLumped - 1)
        dimensionsFM[2] = (cumulativeDimensions[n_plus_1] - cumulativeDimensions[iLumped]) / (n - iLumped + 1)
        roughnessesFM[1] = cumulativeRoughnesses[iLumped] / (iLumped - 1)
        roughnessesFM[2] = (cumulativeRoughnesses[n_plus_1] - cumulativeRoughnesses[iLumped]) / (n - iLumped + 1)
      end
    else
      if n == 1
        pathLengths[1] = lengths[1] / 2
        pathLengths[2] = lengths[1] / 2
        dheightsFM[1] = dheights[1] / 2
        dheightsFM[2] = dheights[1] / 2
        for i in 1:3
          crossAreasFM[i] = crossAreas[1]
          dimensionsFM[i] = dimensions[1]
          roughnessesFM[i] = roughnesses[1]
        end
      else
        pathLengths[1] = cumulativeLengths[iLumped]
        pathLengths[2] = cumulativeLengths[n_plus_1] - cumulativeLengths[iLumped]
        dheightsFM[1] = cumulativeDheights[iLumped]
        dheightsFM[2] = cumulativeDheights[n_plus_1] - cumulativeDheights[iLumped]
        crossAreasFM[1] = cumulativeCrossAreas[iLumped] / (iLumped - 1)
        crossAreasFM[2] = cumulativeCrossAreas[n_plus_1] / n
        crossAreasFM[3] = (cumulativeCrossAreas[n_plus_1] - cumulativeCrossAreas[iLumped]) / (n - iLumped + 1)
        dimensionsFM[1] = cumulativeDimensions[iLumped] / (iLumped - 1)
        dimensionsFM[2] = cumulativeDimensions[n_plus_1] / n
        dimensionsFM[3] = (cumulativeDimensions[n_plus_1] - cumulativeDimensions[iLumped]) / (n - iLumped + 1)
        roughnessesFM[1] = cumulativeRoughnesses[iLumped] / (iLumped - 1)
        roughnessesFM[2] = cumulativeRoughnesses[n_plus_1] / n
        roughnessesFM[3] = (cumulativeRoughnesses[n_plus_1] - cumulativeRoughnesses[iLumped]) / (n - iLumped + 1)
      end
    end
  else
    if case(modelStructure, FluidComponents.Types.ModelStructure.av_vb)
      if n == 2
        pathLengths[1] = lengths[1] + lengths[2]
        dheightsFM[1] = dheights[1] + dheights[2]
      else
        pathLengths[1] = lengths[1] + 0.5 * lengths[2]
        dheightsFM[1] = dheights[1] + 0.5 * dheights[2]
        for i in 2:n - 2
          pathLengths[i] = 0.5 * (lengths[i] + lengths[i + 1])
          dheightsFM[i] = 0.5 * (dheights[i] + dheights[i + 1])
        end
        pathLengths[n - 1] = 0.5 * lengths[n - 1] + lengths[n]
        dheightsFM[n - 1] = 0.5 * dheights[n - 1] + dheights[n]
      end
      for i in 1:n
        crossAreasFM[i] = crossAreas[i]
        dimensionsFM[i] = dimensions[i]
        roughnessesFM[i] = roughnesses[i]
      end
    elseif case(modelStructure, FluidComponents.Types.ModelStructure.av_b)
      for i in 1:n
        pathLengths[i] = lengths[i]
        dheightsFM[i] = dheights[i]
        crossAreasFM[i] = crossAreas[i]
        dimensionsFM[i] = dimensions[i]
        roughnessesFM[i] = roughnesses[i]
      end
      crossAreasFM[n_plus_1] = crossAreas[n]
      dimensionsFM[n_plus_1] = dimensions[n]
      roughnessesFM[n_plus_1] = roughnesses[n]
    elseif case(modelStructure, FluidComponents.Types.ModelStructure.a_vb)
      crossAreasFM[1] = crossAreas[1]
      dimensionsFM[1] = dimensions[1]
      roughnessesFM[1] = roughnesses[1]
      for i in 1:n
        pathLengths[i] = lengths[i]
        dheightsFM[i] = dheights[i]
        crossAreasFM[i + 1] = crossAreas[i]
        dimensionsFM[i + 1] = dimensions[i]
        roughnessesFM[i + 1] = roughnesses[i]
      end
    else
      pathLengths[1] = 0.5 * lengths[1]
      dheightsFM[1] = 0.5 * dheights[1]
      crossAreasFM[1] = crossAreas[1]
      dimensionsFM[1] = dimensions[1]
      roughnessesFM[1] = roughnesses[1]
      for i in 2:n
        pathLengths[i] = 0.5 * (lengths[i - 1] + lengths[i])
        dheightsFM[i] = 0.5 * (dheights[i - 1] + dheights[i])
        crossAreasFM[i] = crossAreas[i - 1]
        dimensionsFM[i] = dimensions[i - 1]
        roughnessesFM[i] = roughnesses[i - 1]
      end
      pathLengths[n_plus_1] = 0.5 * lengths[n]
      dheightsFM[n_plus_1] = 0.5 * dheights[n]
      crossAreasFM[n_plus_1] = crossAreas[n]
      dimensionsFM[n_plus_1] = dimensions[n]
      roughnessesFM[n_plus_1] = roughnesses[n]
      crossAreasFM[n_plus_1 + 1] = crossAreas[n]
      dimensionsFM[n_plus_1 + 1] = dimensions[n]
      roughnessesFM[n_plus_1 + 1] = roughnesses[n]
    end
  end
  guess m_flows = fill(m_flow_start, n_plus_1)
  state_a.p = port_a.p
  state_b.p = port_b.p
  if useInnerPortProperties
    state_a.h = mediums[1].h
    state_b.h = mediums[n].h
    for j in 1:nX
      state_a.X[j] = mediums[1].X[j]
      state_b.X[j] = mediums[n].X[j]
    end
  else
    state_a.h = instream(port_a.h_outflow)
    state_b.h = instream(port_b.h_outflow)
    if fixedX
      for j in 1:nX
        state_a.X[j] = mediums[1].X[j]
        state_b.X[j] = mediums[n].X[j]
      end
    elseif reducedX
      if nXi > 0
        for j in 1:nXi
          state_a.X[j] = instream(port_a.Xi_outflow[j])
          state_b.X[j] = instream(port_b.Xi_outflow[j])
        end
        state_a.X[nX] = 1 - sum(instream(port_a.Xi_outflow))
        state_b.X[nX] = 1 - sum(instream(port_b.Xi_outflow))
      else
        state_a.X[nX] = 1
        state_b.X[nX] = 1
      end
    else
      for j in 1:nX
        state_a.X[j] = instream(port_a.Xi_outflow[j])
        state_b.X[j] = instream(port_b.Xi_outflow[j])
      end
    end
  end
  state_a.T = MediaComponents.Interfaces.temperature_phX(medium_data, state_a.p, state_a.h, state_a.X)
  state_a.d = MediaComponents.Interfaces.density_phX(medium_data, state_a.p, state_a.h, state_a.X)
  state_b.T = MediaComponents.Interfaces.temperature_phX(medium_data, state_b.p, state_b.h, state_b.X)
  state_b.d = MediaComponents.Interfaces.density_phX(medium_data, state_b.p, state_b.h, state_b.X)
  for i in 1:n
    fluidVolumes[i] = crossAreas[i] * lengths[i] * nParallel
    vs[i] = 0.5 * (m_flows[i] + m_flows[i + 1]) / mediums[i].d / crossAreas[i] / nParallel
    mb_flows[i] = m_flows[i] - m_flows[i + 1]
    Hb_flows[i] = H_flows[i] - H_flows[i + 1]
    if nXi > 0
      for j in 1:nXi
        mbXi_flows[i, j] = mXi_flows[i, j] - mXi_flows[i + 1, j]
      end
    end
    if nC > 0
      for j in 1:nC
        mbC_flows[i, j] = mC_flows[i, j] - mC_flows[i + 1, j]
      end
    end
  end
  for i in 2:n
    H_flows[i] = FluidComponents.semilinear(m_flows[i], mediums[i - 1].h, mediums[i].h)
    if nXi > 0
      for j in 1:nXi
        mXi_flows[i, j] = FluidComponents.semilinear(m_flows[i], mediums[i - 1].Xi[j], mediums[i].Xi[j])
      end
    end
    if nC > 0
      for j in 1:nC
        mC_flows[i, j] = FluidComponents.semilinear(m_flows[i], Cs[i - 1, j], Cs[i, j])
      end
    end
  end
  H_flows[1] = FluidComponents.semilinear(port_a.m_flow, instream(port_a.h_outflow), mediums[1].h)
  H_flows[n_plus_1] = -FluidComponents.semilinear(port_b.m_flow, instream(port_b.h_outflow), mediums[n].h)
  if nXi > 0
    for j in 1:nXi
      mXi_flows[1, j] = FluidComponents.semilinear(port_a.m_flow, instream(port_a.Xi_outflow[j]), mediums[1].Xi[j])
      mXi_flows[n_plus_1, j] = -FluidComponents.semilinear(port_b.m_flow, instream(port_b.Xi_outflow[j]), mediums[n].Xi[j])
    end
  end
  if nC > 0
    for j in 1:nC
      mC_flows[1, j] = FluidComponents.semilinear(port_a.m_flow, instream(port_a.C_outflow[j]), Cs[1, j])
      mC_flows[n_plus_1, j] = -FluidComponents.semilinear(port_b.m_flow, instream(port_b.C_outflow[j]), Cs[n, j])
    end
  end
  port_a.m_flow = m_flows[1]
  port_b.m_flow = -m_flows[n_plus_1]
  port_a.h_outflow = mediums[1].h
  port_b.h_outflow = mediums[n].h
  if nXi > 0
    for j in 1:nXi
      port_a.Xi_outflow[j] = mediums[1].Xi[j]
      port_b.Xi_outflow[j] = mediums[n].Xi[j]
    end
  else
    port_a.Xi_outflow[1] = 0
    port_b.Xi_outflow[1] = 0
  end
  # Trace properties are stored in the volumes and therefore use the same
  # adjacent-volume outflow convention as the independent mass fractions.
  if nC > 0
    for j in 1:nC
      port_a.C_outflow[j] = Cs[1, j]
      port_b.C_outflow[j] = Cs[n, j]
    end
  else
    port_a.C_outflow[1] = 0
    port_b.C_outflow[1] = 0
  end
  if useLumpedPressure
    if !case(modelStructure, FluidComponents.Types.ModelStructure.av_vb)
      for i in 2:n
        mediums[i].p = mediums[1].p
      end
    elseif n > 2
      for i in 2:iLumped - 1
        mediums[i].p = mediums[1].p
      end
      for i in iLumped:n - 1
        mediums[i].p = mediums[n].p
      end
    end
    if case(modelStructure, FluidComponents.Types.ModelStructure.av_vb)
      port_a.p = mediums[1].p
      port_b.p = mediums[n].p
      statesFM[1].p = mediums[1].p
      statesFM[1].T = mediums[1].T
      statesFM[1].d = mediums[1].d
      statesFM[1].h = mediums[1].h
      statesFM[2].p = mediums[n].p
      statesFM[2].T = mediums[n].T
      statesFM[2].d = mediums[n].d
      statesFM[2].h = mediums[n].h
      for j in 1:nX
        statesFM[1].X[j] = mediums[1].X[j]
        statesFM[2].X[j] = mediums[n].X[j]
      end
      m_flows[iLumped] = flowModel.m_flows[1]
      vsFM[1] = cumulativeVelocityLengths[iLumped] / cumulativeLengths[iLumped]
      vsFM[2] = (cumulativeVelocityLengths[n_plus_1] - cumulativeVelocityLengths[iLumped]) / (cumulativeLengths[n_plus_1] - cumulativeLengths[iLumped])
    elseif case(modelStructure, FluidComponents.Types.ModelStructure.av_b)
      port_a.p = mediums[1].p
      statesFM[1].p = mediums[iLumped].p
      statesFM[1].T = mediums[iLumped].T
      statesFM[1].d = mediums[iLumped].d
      statesFM[1].h = mediums[iLumped].h
      statesFM[2].p = state_b.p
      statesFM[2].T = state_b.T
      statesFM[2].d = state_b.d
      statesFM[2].h = state_b.h
      for j in 1:nX
        statesFM[1].X[j] = mediums[iLumped].X[j]
        statesFM[2].X[j] = state_b.X[j]
      end
      m_flows[n_plus_1] = flowModel.m_flows[1]
      vsFM[1] = cumulativeVelocityLengths[n_plus_1] / cumulativeLengths[n_plus_1]
      vsFM[2] = m_flows[n_plus_1] / state_b.d / crossAreas[n] / nParallel
    elseif case(modelStructure, FluidComponents.Types.ModelStructure.a_vb)
      port_b.p = mediums[n].p
      statesFM[1].p = state_a.p
      statesFM[1].T = state_a.T
      statesFM[1].d = state_a.d
      statesFM[1].h = state_a.h
      statesFM[2].p = mediums[iLumped].p
      statesFM[2].T = mediums[iLumped].T
      statesFM[2].d = mediums[iLumped].d
      statesFM[2].h = mediums[iLumped].h
      for j in 1:nX
        statesFM[1].X[j] = state_a.X[j]
        statesFM[2].X[j] = mediums[iLumped].X[j]
      end
      m_flows[1] = flowModel.m_flows[1]
      vsFM[1] = m_flows[1] / state_a.d / crossAreas[1] / nParallel
      vsFM[2] = cumulativeVelocityLengths[n_plus_1] / cumulativeLengths[n_plus_1]
    else
      statesFM[1].p = state_a.p
      statesFM[1].T = state_a.T
      statesFM[1].d = state_a.d
      statesFM[1].h = state_a.h
      statesFM[2].p = mediums[iLumped].p
      statesFM[2].T = mediums[iLumped].T
      statesFM[2].d = mediums[iLumped].d
      statesFM[2].h = mediums[iLumped].h
      statesFM[3].p = state_b.p
      statesFM[3].T = state_b.T
      statesFM[3].d = state_b.d
      statesFM[3].h = state_b.h
      for j in 1:nX
        statesFM[1].X[j] = state_a.X[j]
        statesFM[2].X[j] = mediums[iLumped].X[j]
        statesFM[3].X[j] = state_b.X[j]
      end
      m_flows[1] = flowModel.m_flows[1]
      m_flows[n_plus_1] = flowModel.m_flows[2]
      vsFM[1] = m_flows[1] / state_a.d / crossAreas[1] / nParallel
      vsFM[2] = cumulativeVelocityLengths[n_plus_1] / cumulativeLengths[n_plus_1]
      vsFM[3] = m_flows[n_plus_1] / state_b.d / crossAreas[n] / nParallel
    end
  else
    if case(modelStructure, FluidComponents.Types.ModelStructure.av_vb)
      for i in 1:n
        statesFM[i].p = mediums[i].p
        statesFM[i].T = mediums[i].T
        statesFM[i].d = mediums[i].d
        statesFM[i].h = mediums[i].h
        vsFM[i] = vs[i]
        for j in 1:nX
          statesFM[i].X[j] = mediums[i].X[j]
        end
      end
      for i in 1:n - 1
        m_flows[i + 1] = flowModel.m_flows[i]
      end
      port_a.p = mediums[1].p
      port_b.p = mediums[n].p
    elseif case(modelStructure, FluidComponents.Types.ModelStructure.av_b)
      for i in 1:n
        statesFM[i].p = mediums[i].p
        statesFM[i].T = mediums[i].T
        statesFM[i].d = mediums[i].d
        statesFM[i].h = mediums[i].h
        vsFM[i] = vs[i]
        for j in 1:nX
          statesFM[i].X[j] = mediums[i].X[j]
        end
      end
      statesFM[n_plus_1].p = state_b.p
      statesFM[n_plus_1].T = state_b.T
      statesFM[n_plus_1].d = state_b.d
      statesFM[n_plus_1].h = state_b.h
      for j in 1:nX
        statesFM[n_plus_1].X[j] = state_b.X[j]
      end
      for i in 1:n
        m_flows[i + 1] = flowModel.m_flows[i]
      end
      vsFM[n_plus_1] = m_flows[n_plus_1] / state_b.d / crossAreas[n] / nParallel
      port_a.p = mediums[1].p
    elseif case(modelStructure, FluidComponents.Types.ModelStructure.a_vb)
      statesFM[1].p = state_a.p
      statesFM[1].T = state_a.T
      statesFM[1].d = state_a.d
      statesFM[1].h = state_a.h
      vsFM[1] = m_flows[1] / state_a.d / crossAreas[1] / nParallel
      for j in 1:nX
        statesFM[1].X[j] = state_a.X[j]
      end
      for i in 1:n
        statesFM[i + 1].p = mediums[i].p
        statesFM[i + 1].T = mediums[i].T
        statesFM[i + 1].d = mediums[i].d
        statesFM[i + 1].h = mediums[i].h
        m_flows[i] = flowModel.m_flows[i]
        vsFM[i + 1] = vs[i]
        for j in 1:nX
          statesFM[i + 1].X[j] = mediums[i].X[j]
        end
      end
      port_b.p = mediums[n].p
    else
      statesFM[1].p = state_a.p
      statesFM[1].T = state_a.T
      statesFM[1].d = state_a.d
      statesFM[1].h = state_a.h
      vsFM[1] = m_flows[1] / state_a.d / crossAreas[1] / nParallel
      for j in 1:nX
        statesFM[1].X[j] = state_a.X[j]
      end
      for i in 1:n
        statesFM[i + 1].p = mediums[i].p
        statesFM[i + 1].T = mediums[i].T
        statesFM[i + 1].d = mediums[i].d
        statesFM[i + 1].h = mediums[i].h
        m_flows[i] = flowModel.m_flows[i]
        vsFM[i + 1] = vs[i]
        for j in 1:nX
          statesFM[i + 1].X[j] = mediums[i].X[j]
        end
      end
      m_flows[n_plus_1] = flowModel.m_flows[n_plus_1]
      statesFM[n_plus_1 + 1].p = state_b.p
      statesFM[n_plus_1 + 1].T = state_b.T
      statesFM[n_plus_1 + 1].d = state_b.d
      statesFM[n_plus_1 + 1].h = state_b.h
      vsFM[n_plus_1 + 1] = m_flows[n_plus_1] / state_b.d / crossAreas[n] / nParallel
      for j in 1:nX
        statesFM[n_plus_1 + 1].X[j] = state_b.X[j]
      end
    end
  end
metadata {}
end
```

## Test Cases {#Test-Cases}

No test cases defined.

## Related {#Related}

* Examples

* Experiments

* Analyses
