I have developed (of course with a lot of help from experts in this forum) the following code
to speed up the calculations for large digraphs. However, I ended up with much slower calculations compared to Do
loop formulation.
ClearAll[n, d, G, aG, aGsym, Xt, Xt1, alfa, eta, gama, tao, teta, one,
onePlusEta, Aterm, Bterm, Cterm, Dterm, eqs, modelFull];
(* generating a digraph *)
SeedRandom[14];
n = 10;
d = 0.5;
G = RandomGraph[{Round[n], Round[n*(n - 1)*d]}, DirectedEdges -> True];
aG = AdjacencyMatrix[G];
aGsym = BitAnd[aG, Transpose[aG]];
(* preliminaries of matrix inputs for the model *)
Xt = DiagonalMatrix[Array[Subscript[x, #][t] &, n]];
Xt1 = Array[Subscript[x, #]'[t] &, n];
alfa = Transpose@Array[Subscript[\[Alpha], #] &, {n, n}];
eta = Array[Subscript[\[Eta], #] &, n];
gama = Transpose[Array[Subscript[\[Gamma], #] &, {n, n}]]*aG;
tao = Array[Subscript[\[Tau], #] &, {n, n}];
teta = Transpose[Array[Subscript[\[Theta], #] &, {n, n}]*aG];
one = Array[1 &, n];
(* building the model *)
onePlusEta = DiagonalMatrix[one + eta]*Xt; Aterm = (gama + teta).Xt;
Bterm = Xt.((alfa*tao)*aG);
Cterm = ((alfa*alfa)*aGsym)*(Xt.Array[Diagonal[Xt] &, {n}])*aGsym;
Dterm = ((gama*gama)*aGsym)*
Transpose[Array[(Subscript[x, #][t])^2 &, {n, n}]*aGsym];
eqs = Total[onePlusEta - Aterm + Bterm + Cterm - Dterm]//FullSimplify;
modelFull = Thread[Xt1 == eqs - Array[Subscript[x, #][t] &, n]];
(* parameter values for numeric solutions *)
ClearAll[params, parameters, initialVals, sol];
SeedRandom[14];
params = {abs, random, absCost, trsCost, trs} = {
RandomReal[{0.01, 1}, n], RandomReal[{-0.5, 0.5}, n],
RandomReal[{0.01, 0.5}, n], RandomReal[{0.01, 1}, n],
RandomReal[{0.01, 0.5}, n]
};
parameters = Flatten[Thread[#[[1]] -> #[[2]]] & /@Thread[Table[Subscript[#, n], {n, n}] & /@ {\[Alpha], \[Eta], \[Gamma], \[Tau], \[Theta]} -> params]];
initialVals = Array[Subscript[x, #][0] == RandomReal[{0.01, 1}] &, n];
sol = NDSolve[{modelFull /. parameters, initialVals} // Flatten,
Array[Subscript[x, #][t] &, n], {t, 50},
Method-> "StiffnessSwitching"] // Flatten;
Plot[Evaluate[Array[Subscript[x, #][t] &, n] /. sol], {t, 0, 50},
PlotRange -> All]
Is there any specific reason for the slow process?
How can I really improve the performance of the code
with larger digraphs?
OR Am I on the WRONG track for speeding up the calculations?
Please give me some ideas about improving the calculations.
Note:
I can also send the other code
using Do
and If
if somebody is interested in.
code
. Thecode
uses a digraphG
to create a system of ODEs and then solve the systemsol
using the created system. I would appreciate if you run thecode
withn=50
andn=100
and give me the timing for each one. MyMathematica version is 10.0.2.0 and use Windows 7 (with RAM 4). Also tell me on which machine you run the model. Thank you again. $\endgroup$NDSolve
spits outndsz
warning and fails at aboutt==5.57
, tested in v9.0.1 and v11.2. $\endgroup$