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
18% see _kb/06-solver-catalog.md for rationale
19if isfield(sn,'procid')
20 for ist = 1:M
21 for k = 1:K
22 if sn.procid(ist,k) == ProcessType.NHPP && ~isempty(Mu{ist}{k}) ...
23 && ~isnan(Mu{ist}{k}(1))
24 lam = Mu{ist}{k}(1);
25 PH{ist}{k} = {-lam, lam};
26 Phi{ist}{k} = 1;
27 end
28 end
29 end
30end
31
32match = zeros(M,K); % indicates whether a class is served at a station
33for ist = 1:M
34 for k = 1:K
35 if isnan(Mu{ist}{k})
36 Mu{ist}{k} = [];
37 Phi{ist}{k}=[];
38 end
39 match(ist,k) = sum(rt(:, (ist-1)*K+k)) > 0;
40 end
41 %Set number of servers in delay station = population
42 if isinf(S(ist))
43 S(ist) = N;
44 end
45end
46
47%% initialization
48% Wall-clock time budget (options.timeout, seconds; Inf = no budget). The ODE
49% iteration in solver_fluid_iteration breaks when toc(Tstart) > max_time.
50if isfield(options,'timeout') && ~isempty(options.timeout) && isfinite(options.timeout) && options.timeout > 0
51 max_time = options.timeout;
52else
53 max_time = Inf;
54end
55Tstart = tic;
56
57%phases = sn.phases;
58phases = zeros(M,K);
59for ist = 1:M
60 for k = 1:K
61 phases(ist,k) = length(Mu{ist}{k});
62 end
63end
64
65slowrate = zeros(M,K);
66for ist = 1:M
67 for k = 1:K
68 if ~isempty(Mu{ist}{k})
69 slowrate(ist,k) = min(Mu{ist}{k}(:)); %service completion (exit) rates in each phase
70 else
71 slowrate(ist,k) = Inf;
72 end
73 end
74end
75
76%NK(isinf(NK))=1e6;
77iters = 0;
78xvec_it = {};
79y0 = [];
80assigned = zeros(1,K); %number of jobs of each class already assigned
81for ist = 1:M
82 for k = 1:K
83 if match(ist,k) > 0 % indicates whether a class is served at a station
84 if isinf(NK(k))
85 if sched(ist)==SchedStrategy.EXT
86 toAssign = 1; % open job pool
87 else
88 toAssign = 0; % set to zero open jobs everywhere
89 end
90 else
91 toAssign = floor(NK(k)/sum(match(:,k)));
92 if sum( match(ist+1:end, k) ) == 0 % if this is the last station for this job class
93 toAssign = NK(k) - assigned(k);
94 end
95 end
96 y0 = [y0, toAssign, zeros(1,phases(ist,k)-1)]; % this is just for PS
97 assigned(k) = assigned(k) + toAssign;
98 else
99 y0 = [y0, zeros(1,phases(ist,k))];
100 end
101 end
102end
103
104if isempty(options.init_sol)
105 xvec_it{iters +1} = y0(:)'; % average state embedded at stage change transitions out of e
106 ydefault = y0(:)'; % not used in this case
107else
108 xvec_it{iters +1} = options.init_sol(:)';
109 ydefault = y0(:)'; % default solution if init_sol fails
110end
111
112%% solve ode
113switch options.method
114 case {'tbi','fluid.tbi'}
115 [xvec_it, xvec_t, t, iters] = solver_fluid_tbi_iteration(sn, N, Mu, Phi, PH, rt, S, xvec_it, ydefault, slowrate, Tstart, max_time, options);
116 otherwise
117 [xvec_it, xvec_t, t, iters] = solver_fluid_iteration(sn, N, Mu, Phi, PH, rt, S, xvec_it, ydefault, slowrate, Tstart, max_time, options);
118end
119
120runtime = toc(Tstart);
121% if options.verbose >= 2
122% if iters==1
123% line_printf('Fluid analysis iteration completed in %0.6f sec [%d iteration]\n',runtime,iters);
124% else
125% line_printf('Fluid analysis iteration completed in %0.6f sec [%d iterations]\n',runtime,iters);
126% end
127% end
128
129% this part assumes PS, DPS, GPS scheduling
130QN = zeros(M,K);
131QNt = cell(M,K);
132Qt = cell(M,1);
133UNt = cell(M,K);
134for ist=1:M
135 Qt{ist} = 0;
136 for k = 1:K
137 shift = sum(sum(phases(1:ist-1,:))) + sum( phases(ist,1:k-1) ) + 1;
138 QN(ist,k) = sum(xvec_it{end}(shift:shift+phases(ist,k)-1));
139 QNt{ist,k} = sum(xvec_t(:,shift:shift+phases(ist,k)-1),2);
140 Qt{ist} = Qt{ist} + QNt{ist,k};
141 % computes queue length in each node and stage, summing the total
142 % number in service and waiting in that station
143 % results are weighted with the stat prob of the stage
144 end
145end
146
147for ist=1:M
148 if sn.nservers(ist) > 0 % not INF
149 for k = 1:K
150 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
151 UNt{ist,k}(isnan(UNt{ist,k})) = 0; % fix cases where qlen is 0
152 end
153 else % infinite server
154 for k = 1:K
155 UNt{ist,k} = QNt{ist,k};
156 end
157 end
158end
159return
160end