1function [QN,xvec_it,QNt,UNt,xvec_t,t,iters,runtime] = solver_fluid(sn, options)
2% [QN,XVEC_IT,QNT,UNT,XVEC_T,T,ITERS,RUNTIME] = SOLVER_FLUID(QN, OPTIONS)
4% Copyright (c) 2012-2026, Imperial College London
7M = sn.nstations; %number of stations
8K = sn.nclasses; %number of
classes
9N = sn.nclosedjobs; %population
16NK = sn.njobs
'; %initial population
18match = zeros(M,K); % indicates whether a class is served at a station
25 match(ist,k) = sum(rt(:, (ist-1)*K+k)) > 0;
27 %Set number of servers in delay station = population
41 phases(ist,k) = length(Mu{ist}{k});
48 if ~isempty(Mu{ist}{k})
49 slowrate(ist,k) = min(Mu{ist}{k}(:)); %service completion (exit) rates in each phase
51 slowrate(ist,k) = Inf;
60assigned = zeros(1,K); %number of jobs of each class already assigned
63 if match(ist,k) > 0 % indicates whether a class is served at a station
65 if sched(ist)==SchedStrategy.EXT
66 toAssign = 1; % open job pool
68 toAssign = 0; % set to zero open jobs everywhere
71 toAssign = floor(NK(k)/sum(match(:,k)));
72 if sum( match(ist+1:end, k) ) == 0 % if this is the last station for this job class
73 toAssign = NK(k) - assigned(k);
76 y0 = [y0, toAssign, zeros(1,phases(ist,k)-1)]; % this is just for PS
77 assigned(k) = assigned(k) + toAssign;
79 y0 = [y0, zeros(1,phases(ist,k))];
84if isempty(options.init_sol)
85 xvec_it{iters +1} = y0(:)'; % average state embedded at stage change transitions out of e
86 ydefault = y0(:)
'; % not used in this case
88 xvec_it{iters +1} = options.init_sol(:)';
89 ydefault = y0(:)
'; % default solution if init_sol fails
93[xvec_it, xvec_t, t, iters] = solver_fluid_iteration(sn, N, Mu, Phi, PH, rt, S, xvec_it, ydefault, slowrate, Tstart, max_time, options);
96% if options.verbose >= 2
98% line_printf('Fluid analysis iteration completed in %0.6f sec [%d iteration]\n
',runtime,iters);
100% line_printf('Fluid analysis iteration completed in %0.6f sec [%d iterations]\n
',runtime,iters);
104% this part assumes PS, DPS, GPS scheduling
112 shift = sum(sum(phases(1:ist-1,:))) + sum( phases(ist,1:k-1) ) + 1;
113 QN(ist,k) = sum(xvec_it{end}(shift:shift+phases(ist,k)-1));
114 QNt{ist,k} = sum(xvec_t(:,shift:shift+phases(ist,k)-1),2);
115 Qt{ist} = Qt{ist} + QNt{ist,k};
116 % computes queue length in each node and stage, summing the total
117 % number in service and waiting in that station
118 % results are weighted with the stat prob of the stage
123 if sn.nservers(ist) > 0 % not INF
125 UNt{ist,k} = min(QNt{ist,k} / S(ist), QNt{ist,k} ./ Qt{ist}); % if not an infinite server then this is a number between 0 and 1
126 UNt{ist,k}(isnan(UNt{ist,k})) = 0; % fix cases where qlen is 0
128 else % infinite server
130 UNt{ist,k} = QNt{ist,k};