LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
solver_fluid_analyzer.m
1function [QN, UN, RN, TN, CN, XN, t, QNt, UNt, TNt, xvec, iter, aoiResults] = solver_fluid_analyzer(sn, options)
2% [QN, UN, RN, TN, CN, XN, T, QNT, UNT, TNT, XVEC, iter, aoiResults] = SOLVER_FLUID_ANALYZER(QN, OPTIONS)
3
4% Copyright (c) 2012-2026, Imperial College London
5% All rights reserved.
6%global GlobalConstants.Immediate
7%global GlobalConstants.FineTol
8
9% Initialize aoiResults (will be populated if AoI solver is used)
10aoiResults = [];
11
12M = sn.nstations;
13K = sn.nclasses;
14S = sn.nservers;
15SCV = sn.scv;
16V = cellsum(sn.visits);
17gamma = zeros(M,1);
18sched = sn.sched;
19
20line_debug('Fluid analyzer starting: method=%s, nstations=%d, nclasses=%d', options.method, M, K);
21phases = sn.phases;
22phases_last = sn.phases;
23rates0 = sn.rates;
24
25if isempty(options.init_sol)
26 options.init_sol = solver_fluid_initsol(sn, options);
27end
28
29outer_iters = 1;
30outer_runtime = tic;
31switch options.method
32 case {'matrix','fluid.matrix','default','pnorm','fluid.pnorm'}
33 % pnorm uses matrix method with pstar smoothing parameter
34 if strcmpi(options.method, 'default')
35 line_printf('Default method: using matrix/pnorm fluid method\n');
36 end
37 line_debug('Using matrix/pnorm method, calling solver_fluid_matrix');
38 [QN, UN, RN, TN, xvec_iter, QNt, UNt, TNt, ~, t] = solver_fluid_matrix(sn, options);
39 case {'closing','statedep','softmin','fluid.closing','fluid.statedep','fluid.softmin'}
40 line_debug('Using closing/statedep method, calling solver_fluid_closing');
41 [QN, UN, RN, TN, xvec_iter, QNt, UNt, TNt, ~, t] = solver_fluid_closing(sn, options);
42 case {'diffusion','fluid.diffusion'}
43 % Diffusion approximation using Euler-Maruyama SDE solver (closed networks only)
44 line_debug('Using diffusion method, calling solver_fluid_diffusion');
45 [QN, UN, RN, TN, xvec_iter, QNt, UNt, TNt, ~, t] = solver_fluid_diffusion(sn, options);
46 case {'mfq','fluid.mfq'}
47 % Markovian fluid queue method using BUTools for exact single-queue analysis
48 % Also supports Age of Information (AoI) analysis for valid topologies
49
50 % Check for AoI topology first (more specific than general single-queue)
51 [isAoI, aoiInfo] = aoi_is_aoi(sn);
52
53 if isAoI
54 % Route to AoI solver for Age of Information analysis
55 line_debug('AoI topology detected, calling solver_mfq_aoi');
56 [QN, UN, RN, TN, xvec_iter, QNt, UNt, TNt, ~, t, ~, ~, aoiResults] = solver_mfq_aoi(sn, options);
57 else
58 % Check for general single-queue topology
59 [isSingleQueue, fluidInfo] = fluid_is_single_queue(sn);
60
61 if isSingleQueue
62 line_debug('MFQ topology check passed, calling solver_mfq');
63 [QN, UN, RN, TN, xvec_iter, QNt, UNt, TNt, ~, t] = solver_mfq(sn, options);
64 else
65 % Fallback to matrix method if topology is not suitable
66 line_warning(mfilename, 'MFQ not applicable: %s. Falling back to matrix method.', fluidInfo.errorMsg);
67 options.method = 'matrix';
68 [QN, UN, RN, TN, xvec_iter, QNt, UNt, TNt, ~, t] = solver_fluid_matrix(sn, options);
69 end
70 end
71 case {'rmf','fluid.rmf'}
72 % Refined mean field method for cache analysis
73 % Use cacheqn analyzer for integrated cache+queueing network
74 line_debug('Using refined mean field method, calling solver_fld_cacheqn_analyzer');
75 [QN, UN, RN, TN, CN, XN, t, QNt, UNt, TNt, xvec_iter, cacheHitProb, cacheMissProb, ~, ~] = solver_fld_cacheqn_analyzer(sn, options);
76 otherwise
77 line_error(mfilename,sprintf('The ''%s'' method is unsupported by this solver.',options.method));
78end
79outer_runtime = toc(outer_runtime);
80
81
82switch options.method
83 case {'matrix','closing'}
84 % approximate FCFS nodes as state-independent stations
85 if any(sched==SchedStrategy.FCFS)
86 line_debug('FCFS nodes detected, starting iterative approximation');
87 iter = 0;
88 eta_1 = zeros(1,M);
89 eta = Inf*ones(1,M);
90 tol = GlobalConstants.CoarseTol;
91
92 while max(abs(1-eta./eta_1)) > tol & iter <= options.iter_max %#ok<AND2>
93 iter = iter + 1;
94 eta_1 = eta;
95 for ist=1:M
96 sd = rates0(ist,:)>0;
97 UN(ist,sd) = TN(ist,sd) ./ rates0(ist,sd);
98 end
99 ST0 = 1./rates0;
100 ST0(isinf(ST0)) = GlobalConstants.Immediate;
101 ST0(isnan(ST0)) = GlobalConstants.FineTol;
102
103 XN = zeros(1,K);
104 for k=1:K
105 if sn.refstat(k)>0 % ignore artificial classes
106 XN(k) = TN(sn.refstat(k),k);
107 end
108 end
109 [ST,gamma,~,~,~,~,eta] = npfqn_nonexp_approx(options.config.highvar,sn,ST0,V,SCV,TN,UN,gamma,S);
110
111 rates = 1./ST;
112 rates(isinf(rates)) = GlobalConstants.Immediate;
113 rates(isnan(rates)) = GlobalConstants.FineTol; %#ok<NASGU>
114
115 for ist=1:M
116 switch sn.sched(ist)
117 case SchedStrategy.FCFS
118 for k=1:K
119 if rates(ist,k)>0 && SCV(ist,k)>0
120 [cx,muik,phiik] = Coxian.fitMeanAndSCV(1/rates(ist,k), SCV(ist,k));
121 % we now handle the case that due to either numerical issues
122 % or different relationship between scv and mean the size of
123 % the phase-type representation has changed
124 phases(ist,k) = length(muik);
125 if phases(ist,k) ~= phases_last(ist,k) % if number of phases changed
126 % before we update sn we adjust the initial state
127 isf = sn.stationToStateful(ist);
128 [~, nir, sir] = State.toMarginal(sn, ist, sn.state{isf});
129 end
130 sn.proc{ist}{k} = cx.getProcess;
131 sn.mu{ist}{k} = muik;
132 sn.phi{ist}{k} = phiik;
133 % For Coxian, jobs always start in phase 1 (entry probability 1 on first phase)
134 sn.pie{ist}{k} = [1, zeros(1, length(muik)-1)];
135 sn.phases = phases;
136 sn.phasessz = max(sn.phases,ones(size(sn.phases)));
137 sn.phaseshift = [zeros(size(phases,1),1),cumsum(sn.phasessz,2)];
138 if phases(ist,k) ~= phases_last(ist,k)
139 isf = sn.stationToStateful(ist);
140 % we now initialize the new service process
141 sn.state{isf} = State.fromMarginalAndStarted(sn, ist, nir, sir, options);
142 sn.state{isf} = sn.state{isf}(1,:); % pick one as the marginals won't change
143 end
144 end
145 end
146 end
147
148 % xvec_iter{end} may be shorter than expected due to
149 % state space reduction (keep filter / immediate
150 % elimination) in solver_fluid_matrix. Regenerate
151 % init_sol from current sn to ensure correct size.
152 expected_init_sol = solver_fluid_initsol(sn);
153 if ~isempty(xvec_iter) && numel(xvec_iter{end}) == numel(expected_init_sol)
154 options.init_sol = xvec_iter{end}(:);
155 else
156 options.init_sol = expected_init_sol;
157 end
158 if any(phases_last-phases~=0) % If there is a change of phases reset
159 options.init_sol = solver_fluid_initsol(sn);
160 end
161 end
162 sn.phases = phases;
163 switch options.method
164 case {'matrix'}
165 [~, UN, ~, TN, xvec_iter, ~, ~, ~, ~, ~, inner_iters, inner_runtime] = solver_fluid_matrix(sn, options);
166 case {'closing','statedep'}
167 [~, UN, ~, TN, xvec_iter, ~, ~, ~, ~, ~, inner_iters, inner_runtime] = solver_fluid_closing(sn, options);
168 end
169 phases_last = phases;
170 outer_iters = outer_iters + inner_iters;
171 outer_runtime = outer_runtime + inner_runtime;
172 end % FCFS iteration ends here
173 % The FCFS iteration reinitializes at the solution of the last
174 % iterative step. We now have converged in the substitution of the
175 % model parameters and we rerun everything from the true initial point
176 % so that we get the correct transient.
177 options.init_sol = solver_fluid_initsol(sn, options);
178 switch options.method
179 case {'matrix'}
180 [QN, UN, RN, TN, xvec_iter, QNt, UNt, TNt, ~, t] = solver_fluid_matrix(sn, options);
181 case {'closing','statedep'}
182 [QN, UN, RN, TN, xvec_iter, QNt, UNt, TNt, ~, t] = solver_fluid_closing(sn, options);
183 end
184 end
185 case 'statedep'
186 % do nothing, a single iteration is sufficient
187end
188
189if t(1) == 0
190 t(1) = GlobalConstants.FineTol;
191end
192
193for ist=1:M
194 for k=1:K
195 %Qfull_t{i,k} = cumsum(Qfull_t{i,k}.*[0;diff(t)])./t;
196 %Ufull_t{i,k} = cumsum(Ufull_t{i,k}.*[0;diff(t)])./t;
197 end
198end
199
200Ufull0 = UN;
201for ist=1:M
202 sd = find(QN(ist,:)>0);
203 UN(ist,QN(ist,:)==0)=0;
204 switch sn.sched(ist)
205 case SchedStrategy.INF
206 for k=sd
207 UN(ist,k) = QN(ist,k);
208 UNt{ist,k} = QNt{ist,k};
209 TNt{ist,k} = UNt{ist,k}*sn.rates(ist,k);
210 end
211 case SchedStrategy.DPS
212 %w = sn.schedparam(i,:);
213 %wcorr = w(:)*QN(i,:)/(w(sd)*QN(i,sd)');
214 for k=sd
215 % correct for the real rates, instead of the diffusion
216 % approximation rates
217 UN(ist,k) = min([1,QN(ist,k)/S(ist),sum(Ufull0(ist,sd)) * (TN(ist,k)./(rates0(ist,k)))/sum(TN(ist,sd)./(rates0(ist,sd)))]);
218 TNt{ist,k} = UNt{ist,k}*sn.rates(ist,k)*sn.nservers(ist); % not sure if this is needed
219 end
220 otherwise
221 for k=sd
222 % correct for the real rates, instead of the diffusion
223 % approximation rates
224 UN(ist,k) = min([1,QN(ist,k)/S(ist),sum(Ufull0(ist,sd)) * (TN(ist,k)./rates0(ist,k))/sum(TN(ist,sd)./rates0(ist,sd))]);
225 TNt{ist,k} = UNt{ist,k}*sn.rates(ist,k)*sn.nservers(ist);
226 end
227 end
228end
229UN(isnan(UN))=0;
230
231%switch options.method
232%case {'closing','statedep'}
233% for i=1:M
234% if sn.nservers(i) > 0 % not INF
235% for k = 1:K
236% UNt{i,k} = min(QNt{i,k} / S(i), QNt{i,k} ./ cellsum({QNt{i,:}}) ); % if not an infinite server then this is a number between 0 and 1
237% UNt{i,k}(isnan(UNt{i,k})) = 0; % fix cases where qlen is 0
238% end
239% else % infinite server
240% for k = 1:K
241% UNt{i,k} = QNt{i,k};
242% end
243% end
244% end
245
246for ist=1:M
247 sd = find(QN(ist,:)>0);
248 RN(ist,QN(ist,:)==0)=0;
249 for k=sd
250 switch sn.sched(ist)
251 case SchedStrategy.INF
252 % no-op
253 otherwise
254 RN(ist,k) = QN(ist,k) / TN(ist,k);
255 end
256 end
257end
258RN(isnan(RN))=0;
259%end
260
261XN = zeros(1,K);
262CN = zeros(1,K);
263for k=1:K
264 if sn.refstat(k)>0 % ignore artificial classes
265 XN(k) = TN(sn.refstat(k),k);
266 CN(k) = sn.njobs(k) ./ XN(k);
267 end
268end
269if ~isempty(xvec_iter)
270 xvec.odeStateVec = xvec_iter{end};
271else
272 xvec = []; % Signal failure to caller (runAnalyzer checks isempty)
273end
274xvec.sn = sn;
275if exist('cacheHitProb', 'var')
276 xvec.cacheHitProb = cacheHitProb;
277 xvec.cacheMissProb = cacheMissProb;
278end
279iter = outer_iters;
280end
Definition mmt.m:124