Advanced Linear Optimization for Refinery Flow Allocation (part 3)

       When building refinery optimization models for refinery digital twins, the problem often goes beyond simply splitting an incoming feed between parallel process lines. Real refinery systems introduce additional constraints, such as blending different feedstocks, process unit capacity limits, upstream tank farm capacity, and dependencies between interconnected process units. Such relationships require specialized equations and linear constraints in optimization that capture the physical and operational behavior of the refinery. This part focuses on these refinery-specific constraints and shows how refinery flow allocation optimization can be implemented using linear programming and mixed-integer linear programming (MILP) models. For the broader relationship between simulation and optimization in refinery digital twins, see Hybrid Simulation and Optimization for Refinery Digital Twins.

       This guide describes refinery process optimization techniques for allocating incoming flows between process units and their individual lines in refinery digital twins. The techniques include:
       1. Refinery Flow Allocation Optimization Across Multiple Process Units
       2. Prioritize Operational Preferences with Penalty Functions
       3. Maintain Material Balance in Flow Allocation
       4. Managing Target Process (Plant) Loading
       5. Mode-Dependent Minimum and Maximum Flow Limits
       6. Evaluating Unit Underloading
       7. Constraining Feed Blending Ratios

When to Recalculate the Optimization Model

       In a refinery digital twin, simulation continuously represents the current refinery state, while optimization determines the best flow allocation under those conditions. The optimization model is recalculated whenever an event can change the optimal solution, such as changes in feed flow, production planning requirements, unit availability, capacity, operating mode, downstream requirements, or operating constraints. The simulation passes the updated state to the predefined LP/MILP model, which recalculates its parameters and generates a new flow allocation. The simulation applies the result and continues until the next relevant event. This keeps the optimization structure stable while its parameters dynamically adapt to changing refinery conditions. This event-driven interaction between simulation and optimization is described in more detail in Hybrid Simulation and Optimization for Refinery Digital Twins.

How to Formulate Equations and Recalculate the Optimization Model

       Once the equations and constraints are defined, they can be implemented in the refinery digital twin using the ojAlgo wrapper. It provides a convenient interface for defining LP/MILP variables, constraints, and objective functions, while ojAlgo performs the optimization. The model structure remains fixed, while current refinery parameters—flows, capacities, operating modes, and tank-farm state—are updated before each optimization run. When a relevant simulation event occurs, the model is recalculated and the resulting flow allocation and operating decisions are returned to the simulation. This separation of model structure and dynamic parameters allows the same optimization model to be continuously reused as refinery conditions change. For implementation details, see ojAlgo Solver for AnyLogic: Linear Programming in Petroleum Refining Library.

1. Refinery Flow Allocation Optimization Across Multiple Process Units

Problem
       Consider a flowing tank farm supplying four condensate stabilization units (CSUs), with two parallel lines at each unit. For more details on the role and placement of flowing tank farms in refinery material-flow models, see Tank Farm Placement in Oil Refinery Material Flow Simulation Models. The CSU lines produce three main product streams: high-pressure vent gas, stabilized condensate, and NGL. Downstream, these products are processed by dedicated processing units whose capacities for the respective products are lower than the corresponding total output from all eight CSU lines. The stabilized-condensate processing unit has a higher processing capacity than the NGL processing unit.Therefore, there is always excess stabilized condensate and NGL that cannot be processed by the downstream units. These excess streams are routed through the pipeline system to another refinery and should be minimized to avoid sending valuable feed outside the facility.
       Each CSU line can operate in three modes (the products remains the same, while product yields, flow destinations, and routing change):
       1. Normal mode. Produces high-pressure vent gas, on-spec stabilized condensate, and NGL, which are routed to their respective downstream processing units.
       2. Stabilized-condensate-only mode. Produces high-pressure vent gas and on-spec stabilized condensate, with the stabilized condensate routed to its downstream processing unit, while off-spec NGL is sent through a separate pipeline for processing at another refinery. This mode allows the stabilized-condensate processing unit to be loaded further without increasing the load on the NGL processing unit.
       3. Remaining flow. If feed still remains after the previous steps, distribute the remaining flow among available CSU lines to maximize the tank-farm unloading rate.

       The refinery flow optimization model must determine how to allocate the incoming flow between CSU lines and which operating mode to assign to each line, while respecting downstream capacities and the available capacity of the upstream tank farm. Since the tank farm is continuously receiving incoming flow, tank farm optimization should also maximize its unloading rate while maintaining the tank level around the target operating level. The operating principles of flowing tank farms and their level-based control are described in Tank Farm Operating Strategy in Refineries.
Solution
       Instead of solving all these decisions in a single optimization problem, decompose the task into three sequential steps:
       1. Normal operation. Determine the maximum number of CSU lines that can operate in normal mode and optimize their loading within the downstream capacity limits.
       2. Stabilized-condensate-only mode. If feed remains after Step 1, select an available CSU line and switch it to Stabilized-condensate-only mode to further load the downstream stabilized-condensate processing unit up to its maximum capacity.
       3. Remaining flow. If feed still remains after Step 2, distribute it among available CSU lines, using Transition mode where required, to maximize the tank-farm unloading rate.
       This decomposition reduces the complexity of the optimization model by separating product-quality constraints, downstream capacity limits, operating modes, and tank farm unloading into manageable subproblems. In this formulation, Steps 1 and 3 are optimization problems, while Step 2 is a discrete mode-selection decision.

2. Prioritize Operational Preferences with Penalty Functions

       Complex refinery configurations often involve several competing operational preferences that cannot be satisfied simultaneously. For example, the model may need to maintain a target tank-farm level, minimize off-spec production, prioritize normal operating modes, or reduce flows sent to another refinery. The specific penalty functions depend on the refinery configuration and its operational priorities. Each deviation is assigned a penalty weight according to its operational importance. By adjusting these weights, the optimization model can prioritize critical constraints while allowing less important preferences to be relaxed when necessary.These preferences can be represented in a linear optimization model using penalty functions. Each deviation from a desired condition is assigned a penalty weight:

min

       The penalty weight defines the priority of the corresponding preference. A higher weight makes the model more strongly avoid that deviation, while a lower weight allows it to be sacrificed when higher-priority objectives must be satisfied. By adjusting these weights, complex refinery operations can be managed through a clear hierarchy of priorities without changing the underlying optimization model. This makes penalty functions particularly useful for representing changing operational priorities in refinery digital twins.


Example

       For the configuration described in Example 1, the penalty function can be formulated as a weighted sum of the main undesirable outcomes:

where

        - significant overload of the NGL processing unit, causing part of the valuable NGL flow to be routed to another refinery;

       - significant overload of the stabilized-condensate processing unit, causing part of the valuable stabilized-condensate flow to be routed to another refinery;

        - unused capacity of the stabilized-condensate processing unit; this penalty encourages the optimizer to load the unit up to its maximum capacity;

       - CSU underloading, representing the incoming flow not allocated to the CSU lines;

        - small overload of the NGL processing unit. This is an acceptable trade-off because, in Normal operation, NGL and stabilized condensate are produced together, so a small amount of additional NGL can be sacrificed to increase stabilized-condensate processing;

        - line-specific priority coefficient;

        - flow assigned to CSU line i;

        - dynamic penalty weight for residual flow R, which can be adjusted according to the current tank-farm level (see the detailed explanation below.).


       As shown in the formulas above, the NGL downstream processing unit uses two levels of overload penalties.. This approach has proven effective in practice. A small NGL overload (​) allows more CSU lines to operate in Normal mode and further load the stabilized-condensate processing unit, while accepting a small amount of NGL being routed to another refinery, it provides greater flexibility in managing the overall flow. If the small-penalty threshold is insufficient to satisfy the operating requirements, the model enters the large-penalty range (), making the corresponding operating mode effectively infeasible.

       Penalty weights should be chosen so that, under realistic operating conditions, the maximum possible contribution of a lower-priority penalty remains below the minimum contribution of a higher-priority penalty. This preserves the intended hierarchy of operational priorities. However, excessively large penalties or large gaps between penalty weights can make the optimization numerically unstable. Therefore, penalty coefficients should be calibrated through numerical experiments, taking into account the required priority separation, solver stability, and the capabilities of the selected solver. In Anylogic-based Petroleum Refining Library, linear optimization is performed using the ojAlgo solver.

       In this example, the coefficients are assigned sequentially: . These coefficients do not represent physical process constraints. Their purpose is to introduce a weak preference for the loading sequence of the CSU lines. This prevents the optimizer from treating different line-loading combinations as mathematically equivalent and helps avoid alternative optimal solutions during subsequent optimization runs.

3. Maintain Material Balance in Flow Allocation

       Material balance is a fundamental requirement in every feedstock allocation model. All flow-splitting equations must conserve mass at each stage of the process.

       For example, in the configuration above, the incoming flow is distributed between the four CSU units:

,

where ​ is the incoming flow and R is the residual flow.

       Each CSU flow is then distributed between its individual lines. For Example for :

.

       The residual flow requires particular attention. In some configurations, must always equal zero. For example, if a CSU is directly connected to the tank farm and there is no bypass or alternative flow path, a non-zero residual indicates an infeasible solution or an error in the optimization model. In other configurations, a residual flow is physically possible but undesirable. In this case, it can be minimized by adding to the penalty function with an appropriate weight. This reflects the operational principle of accepting as much incoming feed as possible. The weight () assigned to can also be made dynamic. When the tank farm is highly loaded, its penalty can be increased significantly, encouraging the model to increase the withdrawal rate even if this requires sacrificing lower-priority objectives.

4. Managing Target Process (Plant) Loading

       In some optimization models, it is necessary to define a desired loading level for a process (plant) line unit rather than simply impose its nominal capacity as a strict upper bound. Depending on the operating situation, the model may need to allow some underloading, slight overloading, or both. This can be represented by separating the deviation from the target loading into different components:

.


For a plant line with a nominal capacity of :

,

where

        - actual flow to the process unit (t/h).

        - unit underloading relative to its target capacity (and may be penalized when underloading is undesirable);

        - total unit overload;

        - small, acceptable overload subject to a relatively low penalty (for example );

        - overload beyond the acceptable range, subject to a substantially higher penalty.

       The important principle is that underloading and overloading are modeled explicitly as deviations from the desired unit loading, allowing the optimization to balance them according to their assigned penalties. This provides much greater flexibility than treating the nominal unit capacity as a simple hard constraint.

5. Mode-Dependent Minimum and Maximum Flow Limits

       In some refinery configurations, the allowable flow range of a process line depends on its operating mode. Each mode can therefore have its own minimum and maximum flow limits. For example:

,

where​ is a binary variable indicating whether the corresponding mode is selected.

       Additionally, the maximum flow on a line can be limited by a user-defined value. This allows the optimization model to impose an operational limit below the technical maximum of the selected mode. The effective upper bound on the line flow is therefore the lower of the mode-specific maximum and the user-defined maximum. The optimizer determines the actual flow within this allowable range according to the objective function. Thus, the line flow must satisfy both constraints simultaneously: it cannot exceed either the maximum allowed by the operating mode or the user-defined maximum. This provides an additional level of operational control without changing the underlying mode-specific limits.

       A process line can be assigned at most one operating mode at a time. This can be enforced by requiring the sum of the binary variables for all available modes to be no greater than one.For example: . The binary variable can take the value 1 when the mode is available and selected, or 0 when it is not selected. This also allows a line to be made unavailable - for example, when the line is under maintenance - by setting the corresponding availability variable to zero. Thus, the same formulation can simultaneously control mode selection, minimum and maximum loading, and equipment availability.

       The binary mode-selection variables can also be used to determine the active operating mode ID of each process line. By multiplying each binary variable by the corresponding mode ID stored in the database and summing the results, the optimization model returns the ID of the selected mode. For example:

,

where 29, 30, and 33 are mode IDs from the database, and ​, ​, and ​ are binary mode-selection variables. The resulting ​ is the ID of the active mode for CSU1 line 1.

6. Evaluating Unit Underloading

       Underloading can be calculated separately for each process unit by summing the unused capacity of its individual lines. This makes it possible to track underloading at both the unit level and the overall system level.
       For example, for three process units:

The binary variable ensures that only the capacity of the selected operating mode contributes to the underloading calculation.

       The total underloading can then be expressed as:

       This approach allows the model to evaluate how much available capacity remains unused at each unit and across the entire processing scheme.

7. Constraining Feed Blending Ratios

       Some process units can operate only with specific feedstock blends. For example, consider two incoming feed streams: light feed and heavy feed. A process unit may operate either on 100% light feed or on a blend containing up to 40% heavy feed, i.e. 60% light / 40% heavy. This type of operating constraint can be represented directly in the linear optimization model through a set of blending equations. The equations ensure that the selected unit receives a feed composition within the technically permitted range while allowing the optimizer to determine the actual flow allocation.
       For example, if Flight​ and Fheavy​ are the respective feed flows: or equivalently: This constraint allows pure light feed as well as a 60/40 light-to-heavy blend, while preventing the heavy-feed fraction from exceeding the unit's operating limit.

Conclusions

       Refinery flow allocation optimization requires more than distributing feed between parallel process lines. A practical LP/MILP model must also represent material balances, operating modes, target loading, feed blending, downstream capacities, and competing operational priorities through penalty functions. These techniques allow complex refinery configurations to be represented within a structured optimization model while keeping the model adaptable to changing operating conditions.
The key principle is to keep the optimization structure stable while dynamically updating its parameters as feed flows, capacities, equipment availability, tank-farm conditions, and operational priorities change. This makes the same optimization model reusable throughout the refinery simulation and suitable for integration into a refinery digital twin.

FAQ

1. What is refinery flow allocation optimization?
Refinery flow allocation optimization determines how incoming feed is distributed between multiple process units and their individual lines while considering capacities, operating modes, material balances, tank-farm conditions, and downstream constraints.

2. Why are penalty functions used in refinery optimization?
Penalty functions allow the optimization model to prioritize competing operational requirements, such as minimizing unit overload, underloading, residual flow, or deviations from a preferred line-loading sequence.

3. Can penalty weights change dynamically?
Yes. Penalty weights can be updated according to the current refinery state. For example, the penalty for residual flow can increase when the tank farm becomes highly loaded, encouraging the optimizer to increase the withdrawal rate.

4. How are operating modes represented in an LP/MILP model?
Operating modes can be represented using binary variables. Each mode can have its own minimum and maximum flow limits, while additional constraints ensure that no more than one mode is selected for a process line.

5. Why is material balance important in flow allocation?
Material balance ensures that flow is conserved at every allocation stage. A residual flow can either be explicitly allowed and minimized or, when no alternative flow path exists, indicate an infeasible solution or an error in the optimization model.

6. Can process units have different feed blending requirements?
Yes. Feed blending constraints can restrict the allowable ratio of different feedstocks. For example, a process unit can be limited to 100% light feed or a blend containing up to 40% heavy feed.

7. How can process unit underloading be included in optimization?
Underloading can be calculated as unused capacity relative to the target loading of the selected process lines. It can then be included in the objective function when maximizing unit utilization is an operational priority.

8. When should a refinery optimization model be recalculated?
The model should be recalculated whenever a change can affect the optimal allocation, including changes in feed flow, production period, unit availability, capacity, operating mode, downstream requirements, or operating constraints.

9. How is the optimization model implemented in Petroleum Refining Library?
The LP/MILP equations can be implemented using the Petroleum Refining Library ojAlgo wrapper, while ojAlgo performs the underlying optimization. The model structure remains stable while current refinery parameters are updated before each optimization run.