I need to solve a equation by NDSlove, so I write the equation first, but it takes me a lot of time.
The follow is my code
energy = Subdivide[0, 50, 5];
theta0 = Reverse[N[ArcSin[Subdivide[0, 1, 5]]]]
theta = Cos[
ArcSin[(R Sin[theta0])/Sqrt[
t^2 + 2 R t Cos[theta0] + R^2 Cos[theta0]^2 + R^2 Sin[theta0]^2]]];
ni = Length[theta];nj = Length[energy];
Ebare = 10;Ebaree = 15;Ebarx = 24;betae = 0.315;betaee = 0.21;betax = 0.131;R = 10;
Fj = Compile[{{x, _Real}}, (
betae (betae x)^2)/((E^(betae x) + 1) Ebare) + (
betax (betax x)^2)/((E^(betax x) + 1) Ebarx)];
Fjbar = Compile[{{x, _Real}}, ((
betaee (betaee x)^2)/((E^(betaee x) + 1) Ebaree) + (
betax (betax x)^2)/((E^(betax x) + 1) Ebarx)) ];
For[j = 2, j <= nj, j++,
For[i = 1, i <= ni, i++,
P[theta0[[i]], energy[[j]], t] = {Px[theta0[[i]], energy[[j]], t],
Py[theta0[[i]], energy[[j]], t],
Pz[theta0[[i]], energy[[j]], t]}]];
For[j = 2, j <= nj, j++,
For[i = 1, i <= ni, i++,
Pbar[theta0[[i]], energy[[j]],
t] = {Pbarx[theta0[[i]], energy[[j]], t],
Pbary[theta0[[i]], energy[[j]], t],
Pbarz[theta0[[i]], energy[[j]], t]}]];
Clear["i", "j"];
timing = AbsoluteTiming[
MPP = Flatten[
ParallelTable[D[P[theta0[[i]], energy[[j]], t], t] ==
Cross[Sum[(1 - theta[[ii]] theta[[i]]) (theta[[ii]] -
theta[[ii -
1]]) Sum[(Fj[energy[[jj]]] P[theta0[[ii]],
energy[[jj]], t] -
Fjbar[energy[[jj]]] Pbar[theta0[[ii]], energy[[jj]],
t]) (energy[[jj]] - energy[[jj - 1]]), {jj, 2,
nj}], {ii, 2, ni}], P[theta0[[i]], energy[[j]], t]], {i,
1, ni}, {j, 2, nj}]]];
timing[[1]]
I run the former code in a HPC which have 16 kernels, when ni=nj=6, the output is 301..., when ni=6, nj=8 the output is 1229...s, as you can find the time used arise quiet quickly. And what final I want is ni=80, nj=32, I can't accept the time used of this.
So, is there anyway to save time to do Table or Cross. Thanks for any suggestions.
NDSolvein the formD[X[t], t] == F[X[t]]with a functionF[x_?NumericQ]:=that eats a numeric vector and throws out the forcing term. Then you can make use of fast operations likeDotfor matrices and vectors. – Henrik Schumacher Dec 01 '18 at 10:09D[P[t], t]but doD[Px[t]],D[Py[t]],D[Pz[t]]separately? – 袁子奕 Dec 01 '18 at 11:12Crossis supposed to do there. Are thePsupposed to be vector-valued? In total, there is really not enough information for providing a detailed answer. Certainly, your system is very expensive as each degree of freedom seems to couple nontrivially with all the other ones. – Henrik Schumacher Dec 01 '18 at 11:24PandPbar. Now you could just copy my code and it can run to get the time used. I follow your advice doD[P[t]]separately, it almost finished right away, I'm checking my code of the separately definition, and try to make it more clearly. As you can see if it write separately, it will be too long. – 袁子奕 Dec 01 '18 at 11:53Crossin that question won't take such a long time. In other words, I didn't meet this problem in that question. But both question are proposed to solve the same physics problem. I ask this new question is because I did't find any related question mentioned the time used byCrossin this forum. – 袁子奕 Dec 01 '18 at 14:33