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)
4% Copyright (c) 2012-2026, Imperial College London
6%global GlobalConstants.Immediate
7%global GlobalConstants.FineTol
9% Initialize aoiResults (will be populated
if AoI solver
is used)
16V = cellsum(sn.visits);
20line_debug(
'Fluid analyzer starting: method=%s, nstations=%d, nclasses=%d', options.method, M, K);
22phases_last = sn.phases;
25if isempty(options.init_sol)
26 options.init_sol = solver_fluid_initsol(sn, options);
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');
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
50 % Check
for AoI topology first (more specific than general single-queue)
51 [isAoI, aoiInfo] = aoi_is_aoi(sn);
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);
58 % Check
for general single-queue topology
59 [isSingleQueue, fluidInfo] = fluid_is_single_queue(sn);
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);
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);
72 line_error(mfilename,sprintf(
'The ''%s'' method is unsupported by this solver.',options.method));
74outer_runtime = toc(outer_runtime);
78 case {
'matrix',
'closing'}
79 % approximate FCFS
nodes as state-independent stations
80 if any(sched==SchedStrategy.FCFS)
81 line_debug('FCFS
nodes detected, starting iterative approximation');
85 tol = GlobalConstants.CoarseTol;
87 while max(abs(1-eta./eta_1)) > tol & iter <= options.iter_max %
#ok<AND2>
92 UN(ist,sd) = TN(ist,sd) ./ rates0(ist,sd);
95 ST0(isinf(ST0)) = GlobalConstants.Immediate;
96 ST0(isnan(ST0)) = GlobalConstants.FineTol;
100 if sn.refstat(k)>0 % ignore artificial
classes
101 XN(k) = TN(sn.refstat(k),k);
104 [ST,gamma,~,~,~,~,eta] = npfqn_nonexp_approx(options.config.highvar,sn,ST0,V,SCV,TN,UN,gamma,S);
107 rates(isinf(rates)) = GlobalConstants.Immediate;
108 rates(isnan(rates)) = GlobalConstants.FineTol; %#ok<NASGU>
112 case SchedStrategy.FCFS
114 if rates(ist,k)>0 && SCV(ist,k)>0
115 [cx,muik,phiik] = Coxian.fitMeanAndSCV(1/rates(ist,k), SCV(ist,k));
116 % we now handle the
case that due to either numerical issues
117 % or different relationship between scv and mean the size of
118 % the phase-type representation has changed
119 phases(ist,k) = length(muik);
120 if phases(ist,k) ~= phases_last(ist,k) %
if number of phases changed
121 % before we update sn we adjust the initial state
122 isf = sn.stationToStateful(ist);
123 [~, nir, sir] = State.toMarginal(sn, ist, sn.state{isf});
125 sn.proc{ist}{k} = cx.getProcess;
126 sn.mu{ist}{k} = muik;
127 sn.phi{ist}{k} = phiik;
128 % For Coxian, jobs always start in phase 1 (entry probability 1 on first phase)
129 sn.pie{ist}{k} = [1, zeros(1, length(muik)-1)];
131 sn.phasessz = max(sn.phases,ones(size(sn.phases)));
132 sn.phaseshift = [zeros(size(phases,1),1),cumsum(sn.phasessz,2)];
133 if phases(ist,k) ~= phases_last(ist,k)
134 isf = sn.stationToStateful(ist);
135 % we now initialize the
new service process
136 sn.state{isf} = State.fromMarginalAndStarted(sn, ist, nir, sir, options);
137 sn.state{isf} = sn.state{isf}(1,:); % pick one as the marginals won
't change
143 options.init_sol = xvec_iter{end}(:);
144 if any(phases_last-phases~=0) % If there is a change of phases reset
145 options.init_sol = solver_fluid_initsol(sn);
149 switch options.method
151 [~, UN, ~, TN, xvec_iter, ~, ~, ~, ~, ~, inner_iters, inner_runtime] = solver_fluid_matrix(sn, options);
152 case {'closing
','statedep
'}
153 [~, UN, ~, TN, xvec_iter, ~, ~, ~, ~, ~, inner_iters, inner_runtime] = solver_fluid_closing(sn, options);
155 phases_last = phases;
156 outer_iters = outer_iters + inner_iters;
157 outer_runtime = outer_runtime + inner_runtime;
158 end % FCFS iteration ends here
159 % The FCFS iteration reinitializes at the solution of the last
160 % iterative step. We now have converged in the substitution of the
161 % model parameters and we rerun everything from the true initial point
162 % so that we get the correct transient.
163 options.init_sol = solver_fluid_initsol(sn, options);
164 switch options.method
166 [QN, UN, RN, TN, xvec_iter, QNt, UNt, TNt, ~, t] = solver_fluid_matrix(sn, options);
167 case {'closing
','statedep
'}
168 [QN, UN, RN, TN, xvec_iter, QNt, UNt, TNt, ~, t] = solver_fluid_closing(sn, options);
172 % do nothing, a single iteration is sufficient
176 t(1) = GlobalConstants.FineTol;
181 %Qfull_t{i,k} = cumsum(Qfull_t{i,k}.*[0;diff(t)])./t;
182 %Ufull_t{i,k} = cumsum(Ufull_t{i,k}.*[0;diff(t)])./t;
188 sd = find(QN(ist,:)>0);
189 UN(ist,QN(ist,:)==0)=0;
191 case SchedStrategy.INF
193 UN(ist,k) = QN(ist,k);
194 UNt{ist,k} = QNt{ist,k};
195 TNt{ist,k} = UNt{ist,k}*sn.rates(ist,k);
197 case SchedStrategy.DPS
198 %w = sn.schedparam(i,:);
199 %wcorr = w(:)*QN(i,:)/(w(sd)*QN(i,sd)');
201 % correct
for the real rates, instead of the diffusion
202 % approximation rates
203 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)))]);
204 TNt{ist,k} = UNt{ist,k}*sn.rates(ist,k)*sn.nservers(ist); % not sure
if this is needed
208 % correct
for the real rates, instead of the diffusion
209 % approximation rates
210 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))]);
211 TNt{ist,k} = UNt{ist,k}*sn.rates(ist,k)*sn.nservers(ist);
217%
switch options.method
218%
case {
'closing',
'statedep'}
220%
if sn.nservers(i) > 0 % not INF
222% 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
223% UNt{i,k}(isnan(UNt{i,k})) = 0; % fix cases where qlen
is 0
225%
else % infinite server
227% UNt{i,k} = QNt{i,k};
233 sd = find(QN(ist,:)>0);
234 RN(ist,QN(ist,:)==0)=0;
237 case SchedStrategy.INF
240 RN(ist,k) = QN(ist,k) / TN(ist,k);
250 if sn.refstat(k)>0 % ignore artificial
classes
251 XN(k) = TN(sn.refstat(k),k);
252 CN(k) = sn.njobs(k) ./ XN(k);
255xvec.odeStateVec = xvec_iter{end};