LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
solver_fluid.m
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)
3
4% Copyright (c) 2012-2026, Imperial College London
5% All rights reserved.
6
7M = sn.nstations; %number of stations
8K = sn.nclasses; %number of classes
9N = sn.nclosedjobs; %population
10Mu = sn.mu;
11Phi = sn.phi;
12PH = sn.proc;
13sched = sn.sched;
14rt = sn.rt;
15S = sn.nservers;
16NK = sn.njobs'; %initial population
17
18match = zeros(M,K); % indicates whether a class is served at a station
19for ist = 1:M
20 for k = 1:K
21 if isnan(Mu{ist}{k})
22 Mu{ist}{k} = [];
23 Phi{ist}{k}=[];
24 end
25 match(ist,k) = sum(rt(:, (ist-1)*K+k)) > 0;
26 end
27 %Set number of servers in delay station = population
28 if isinf(S(ist))
29 S(ist) = N;
30 end
31end
32
33%% initialization
34max_time = Inf;
35Tstart = tic;
36
37%phases = sn.phases;
38phases = zeros(M,K);
39for ist = 1:M
40 for k = 1:K
41 phases(ist,k) = length(Mu{ist}{k});
42 end
43end
44
45slowrate = zeros(M,K);
46for ist = 1:M
47 for k = 1:K
48 if ~isempty(Mu{ist}{k})
49 slowrate(ist,k) = min(Mu{ist}{k}(:)); %service completion (exit) rates in each phase
50 else
51 slowrate(ist,k) = Inf;
52 end
53 end
54end
55
56%NK(isinf(NK))=1e6;
57iters = 0;
58xvec_it = {};
59y0 = [];
60assigned = zeros(1,K); %number of jobs of each class already assigned
61for ist = 1:M
62 for k = 1:K
63 if match(ist,k) > 0 % indicates whether a class is served at a station
64 if isinf(NK(k))
65 if sched(ist)==SchedStrategy.EXT
66 toAssign = 1; % open job pool
67 else
68 toAssign = 0; % set to zero open jobs everywhere
69 end
70 else
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);
74 end
75 end
76 y0 = [y0, toAssign, zeros(1,phases(ist,k)-1)]; % this is just for PS
77 assigned(k) = assigned(k) + toAssign;
78 else
79 y0 = [y0, zeros(1,phases(ist,k))];
80 end
81 end
82end
83
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
87else
88 xvec_it{iters +1} = options.init_sol(:)';
89 ydefault = y0(:)'; % default solution if init_sol fails
90end
91
92%% solve ode
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);
94
95runtime = toc(Tstart);
96% if options.verbose >= 2
97% if iters==1
98% line_printf('Fluid analysis iteration completed in %0.6f sec [%d iteration]\n',runtime,iters);
99% else
100% line_printf('Fluid analysis iteration completed in %0.6f sec [%d iterations]\n',runtime,iters);
101% end
102% end
103
104% this part assumes PS, DPS, GPS scheduling
105QN = zeros(M,K);
106QNt = cell(M,K);
107Qt = cell(M,1);
108UNt = cell(M,K);
109for ist=1:M
110 Qt{ist} = 0;
111 for k = 1:K
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
119 end
120end
121
122for ist=1:M
123 if sn.nservers(ist) > 0 % not INF
124 for k = 1:K
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
127 end
128 else % infinite server
129 for k = 1:K
130 UNt{ist,k} = QNt{ist,k};
131 end
132 end
133end
134return
135end