1

I need to NDSolve a system many times by scanning some of its parameters and do some matrix calculation with the (discretized) solutions. The example is the following code. The system eqs looks formidable, but the concrete form doesn't matter.

Because the computation is quite heavy (I actually need much denser meshes for scanning the parameters, I mean the lists I defined in the middle), I was wondering if any further speed improvement possible. Probably by properly Compile the program? I've heard of great gain by compile to C or machine code. But I somehow got lost in the related documentation. Any suggestion would be appreciated.

Note that the NDSolve part sometimes takes less or comparable time than the matrix calculations followed, depending on how dense we scan particular parameters.

eqs = {I Derivative[1][B1][t] == 
    Abs[B1[t]]^2 (m B1[t] + B3[t] (x - I y + Cos[t] - I Sin[t])) + 
     Abs[B2[t]]^2 (m B1[t] + 
        B3[t] (x - I y + Cos[t] - I Sin[t])) + (B1[t] Conjugate[
          B3[t]] + B2[t] Conjugate[B4[t]]) (-m B3[t] + 
        B1[t] (x + I y + Cos[t] + I Sin[t])), 
   I Derivative[1][B2][t] == 
    Abs[B1[t]]^2 (m B2[t] + B4[t] (x - I y + Cos[t] - I Sin[t])) + 
     Abs[B2[t]]^2 (m B2[t] + 
        B4[t] (x - I y + Cos[t] - I Sin[t])) + (B1[t] Conjugate[
          B3[t]] + B2[t] Conjugate[B4[t]]) (-m B4[t] + 
        B2[t] (x + I y + Cos[t] + I Sin[t])), 
   I Derivative[1][B3][t] == 
    B1[t] (m (B3[t] Conjugate[B1[t]] + 
           B4[t] Conjugate[B2[t]]) + (Abs[B3[t]]^2 + 
           Abs[B4[t]]^2) (x + I y + Cos[t] + I Sin[t])) + 
     B3[t] (-m (Abs[B3[t]]^2 + 
           Abs[B4[t]]^2) + (B3[t] Conjugate[B1[t]] + 
           B4[t] Conjugate[B2[t]]) (x + Cos[t] - I (y + Sin[t]))), 
   I Derivative[1][B4][t] == 
    B2[t] (m (B3[t] Conjugate[B1[t]] + 
           B4[t] Conjugate[B2[t]]) + (Abs[B3[t]]^2 + 
           Abs[B4[t]]^2) (x + I y + Cos[t] + I Sin[t])) + 
     B4[t] (-m (Abs[B3[t]]^2 + 
           Abs[B4[t]]^2) + (B3[t] Conjugate[B1[t]] + 
           B4[t] Conjugate[B2[t]]) (x + Cos[t] - I (y + Sin[t])))};
ic = {B1[-tmax] == 1, B2[-tmax] == 0, B3[-tmax] == 0, B4[-tmax] == 1};
var = {B1, B2, B3, B4};

tmax = 25; m = 0.4; tlist = Table[i, {i, 10, 30, 5}]; ttmax = tmax; ttmesh = ttmax/10; ttlist = Table[tt, {tt, -ttmax, ttmax, ttmesh}]; rmax = 2.0; rmesh = 0.1; rlist = Table[r, {r, -rmax, rmax, rmesh}]; omax = 1.0; omesh = omax/60; olist = Table[o, {o, -omax, omax, omesh}]; smat = Table[Exp[-(t1 - myt)^2/5 + I o t1], {o, olist}, {myt, tlist}, {t1, ttlist}]; G0 = {{x^2, y}, {-y, x y}};

data = ParallelTable[ Module[{sol = Last[#] & /@ (First@NDSolve[{eqs, ic}, var, {t, -tmax, tmax}]), Bsol}, Bsol = Table[Map[#@t1 &, sol, {1}], {t1, ttlist}]; Map[#.G0.ConjugateTranspose[#] &@(ttmesh Partition[#1.Bsol, 2]) &, smat, {2}]], {x, rlist}, {y, rlist}];

xzczd
  • 65,995
  • 9
  • 163
  • 468
xiaohuamao
  • 4,728
  • 18
  • 36
  • 3
  • Given the system isn't that large, I doubt if Compile will help. 2. Why not ParametricNDSolve?
  • – xzczd Feb 14 '21 at 11:52
  • @xzczd 1. I actually need much denser mesh for scanning the parameters, I mean the lists I defined. 2. Thanks for point that out. Maybe that helps. But the NDSolve part actually can take much less time than the matrix calculations followed, depending on how dense we scan particular parameters. So I was wondering if any Compile can give some overall improvement. – xiaohuamao Feb 14 '21 at 12:36
  • 1
    I presume you are already aware of this list? – J. M.'s missing motivation Feb 14 '21 at 12:52