5

I have a first order non-homogeneous system of differential equation (100+ equations, so no hope to solve them analytically, due to the Abel–Ruffini theorem). If I solve them using NDSolve, I have to use the form:

NDSove[Join[MyEquations,{y0[0]==y00,y1[0]==y01,...,yN[0]==yN0}],{y0[t],y1[t],...,yN[t]},
    {t,timeStart,timeEnd}]

Now this will give the solution as an interpolation function between timeStart and timeEnd. This is, however, not really what I need. I need the solution of this system at t->Infinity only (in physics it's called the steady state solution).

The question is: How can I get the solution of y0[Infinity] numerically?

The problem with doing it the "easy" way, i.e., by choosing a high value of t, is that it consumes so much memory that the kernel crashes.

Please advise.

Please feel free to ask for any additional details. Thanks.


Update:

Since only help can be provided with an example, I created a simplified system showing the problem. I hate going into details because it's the wrong way to ask a question here, but there seems to be no way around it. Now this system is an atomic system with 3 levels, leading to 9 density matrix equations (3 populations and 6 coherences). This system simulates the famous problem called EIT (Electromagnetically Induced Transparency, and the steady state solution shows the red curve from the Wikipedia page). I replaced all the parameters, and all that's left is the parameter $\Delta$, which represents the detuning (in GHz). The task is: Get the steady state solution of this system for about a 500 values of $\Delta$ to see that red curve. This is equivalent to a scan of light frequency in an experiment. Now this is doable for 3 levels with a simple Table[NDSolve[...],{Δ,...,...}].

Here's the system the full NDSolve call (please just copy/paste to your Mathematica notebook):

MyEquations = {(I Subscript[ρ, {0, 0}]'[t])/(2 π) == -0.5` E^(-2 I π t Δ) Subscript[ρ, {-1, 0}][t] + 0.5` E^(2 I π t Δ) Subscript[ρ, {0, -1}][t] - (0.` + 1.273255460229472` I) Subscript[ρ, {0, 0}][t] + 0.5` E^(2 I π t Δ) Subscript[ρ, {0, 1}][t] - 0.5` E^(-2 I π t Δ) Subscript[ρ, {1, 0}][t], (I Subscript[ρ, {1, 0}]'[t])/(2 π) == -0.5` E^(2 I π t Δ) Subscript[ρ, {0, 0}][t] + 0.5` E^(2 I π t Δ) Subscript[ρ, {1, -1}][t] - (0.` + 0.6366356878618905` I) Subscript[ρ, {1, 0}][t] + Δ Subscript[ρ, {1, 0}][t] + 0.5` E^(2 I π t Δ) Subscript[ρ, {1, 1}][t], (I Subscript[ρ, {-1, 0}]'[t])/(2 π) == 0.5` E^(2 I π t Δ) Subscript[ρ, {-1, -1}][t] - (0.` + 0.6366356878618905` I) Subscript[ρ, {-1, 0}][t] + Δ Subscript[ρ, {-1, 0}][t] + 0.5` E^(2 I π t Δ) Subscript[ρ, {-1, 1}][t] - 0.5` E^(2 I π t Δ) Subscript[ρ, {0, 0}][t], (I Subscript[ρ, {0, 1}]'[t])/(2 π) == -0.5` E^(-2 I π t Δ) Subscript[ρ, {-1, 1}][t] + 0.5` E^(-2 I π t Δ) Subscript[ρ, {0, 0}][t] - (0.` + 0.6366356878618905` I) Subscript[ρ, {0, 1}][t] - Δ Subscript[ρ, {0, 1}][t] - 0.5` E^(-2 I π t Δ) Subscript[ρ, {1, 1}][t], (I Subscript[ρ, {1, 1}]'[t])/(2 π) == (I (0.00005` + 4 Subscript[ρ, {0, 0}][t]))/(2 π) - 0.5` E^(2 I π t Δ) Subscript[ρ, {0, 1}][t] + 0.5` E^(-2 I π t Δ) Subscript[ρ, {1, 0}][t] - (0.` + 0.000015915494309189534` I) Subscript[ρ, {1, 1}][t], (I Subscript[ρ, {-1, 1}]'[t])/(2 π) == 0.5` E^(-2 I π t Δ) Subscript[ρ, {-1, 0}][t] - (0.` + 0.000015915494309189534` I) Subscript[ρ, {-1, 1}][t] - 0.5` E^(2 I π t Δ) Subscript[ρ, {0, 1}][t], (I Subscript[ρ, {0, -1}]'[t])/(2 π) == -0.5` E^(-2 I π t Δ) Subscript[ρ, {-1, -1}][t] - (0.` + 0.6366356878618905` I) Subscript[ρ, {0, -1}][t] - Δ Subscript[ρ, {0, -1}][t] + 0.5` E^(-2 I π t Δ) Subscript[ρ, {0, 0}][t] - 0.5` E^(-2 I π t Δ) Subscript[ρ, {1, -1}][t], (I Subscript[ρ, {1, -1}]'[t])/(2 π) == -0.5` E^(2 I π t Δ) Subscript[ρ, {0, -1}][t] - (0.` + 0.000015915494309189534` I) Subscript[ρ, {1, -1}][t] + 0.5` E^(-2 I π t Δ) Subscript[ρ, {1, 0}][t], (I Subscript[ρ, {-1, -1}]'[t])/(2 π) == (0.` - 0.000015915494309189534` I) Subscript[ρ, {-1, -1}][t] + 0.5` E^(-2 I π t Δ) Subscript[ρ, {-1, 0}][t] - 0.5` E^(2 I π t Δ) Subscript[ρ, {0, -1}][t] + (I (0.00005` + 4 Subscript[ρ, {0, 0}][t]))/(2 π)} /. Δ -> 3;
MyBC = {Subscript[ρ, {0, 0}][0] == 1, Subscript[ρ, {1, 0}][0] == 0, 
        Subscript[ρ, {-1, 0}][0] == 0, Subscript[ρ, {0, 1}][0] == 0, 
        Subscript[ρ, {1, 1}][0] == 0, Subscript[ρ, {-1, 1}][0] == 0, 
        Subscript[ρ, {0, -1}][0] == 0, Subscript[ρ, {1, -1}][0] == 0, 
        Subscript[ρ, {-1, -1}][0] == 0};
MyVariables = {Subscript[ρ, {0, 0}][t], Subscript[ρ, {1, 0}][t], Subscript[ρ, {-1, 0}][t], 
        Subscript[ρ, {0, 1}][t], Subscript[ρ, {1, 1}][t], 
        Subscript[ρ, {-1, 1}][t], Subscript[ρ, {0, -1}][t], 
        Subscript[ρ, {1, -1}][t], Subscript[ρ, {-1, -1}][t]};
NDSolve[{MyEquations, MyBC}, MyVariables, {t, 0, 500}];
Plot[Subscript[ρ, {0, 0}][t] /. s, {t, 0, 500}]

Now as you see, going to time t=500 gives a steady state solution, but:

1- Takes quite a while to solve

2- For my large system, I need t=10^6 in order to reach the steady state solution.

If I just replace 500 with 10^6, not only that solving will take forever, but also the kernel of Mathematica will crash.

What do I need? I probably need some old fashioned Runge-Kutta solver, where I solve this set of differential equations progressively until rho[t]-(rho[t-dt] is comparable to machine precision (or to some predefined precision). I don't need interpolation!

Now if I try to solve this with NSolve:

NSolve[#[[2]] == 0 & /@ MyEquations /. Δ -> 3, MyVariables]

Then:

1- t will still appear on the other side of the equation.

2- This will take forever with a huge system. There's no way to take a limit of t->Infinity before solving the system.

Just for completeness, I would like to point out that these equations for this simplified system can be further simplified using the famous RWA (Rotating Wave Approximation), but this is not possible in my larger system because there's multiple generators of rotations (multiple angular momenta) involved there.

bbgodfrey
  • 61,439
  • 17
  • 89
  • 156
  • 2
    Often, setting derivatives to zero gives a steady-state solution without actually solving the differential equations. Readers probably can provide good answers, if you include a few sample equations. – bbgodfrey Dec 31 '15 at 10:13
  • @bbgodfrey Thank you for the tip, I'm trying that now. It's difficult to provide examples because it's 100+ equations. It's actually the Liouville equations of a density matrix system I'm trying to solve. Hope that helps. – The Quantum Physicist Dec 31 '15 at 10:14
  • @bbgodfrey By setting the derivatives to zero, I still have complex exponentials on the other side of the equations that depend on time. I don't seem to find a way to put a value for them. Mathematica is unable to take the limit of that when t->Infinity, and NSolveing that system is very, very slow... What would you suggest? Is there any other way to get the steady state solution? – The Quantum Physicist Dec 31 '15 at 10:46
  • @bbgodfrey Thank you. I updated my question with a simplified set of equations... hope that provides an exmaple of the problem. – The Quantum Physicist Dec 31 '15 at 17:09
  • Thanks to adaptive step sizes, running to t=10^6 won't take 2000 times as long as running to t=500 if the system does settle down to a steady state. – Chris K Jan 02 '16 at 03:02
  • @ChrisK Actually I understand the concept... but it's taking a very, very, very long time that I'm desperate with it... You know I'm thinking of copying the whole system of equations here... the big one... and see if I can get help with it here. – The Quantum Physicist Jan 02 '16 at 04:09
  • @ChrisK I updated the question with a link to a short notebook. Please let me know if you can solve these equations within less than 1 second. Imagine having to solve these for 500 values of Delta. That's the problem. Here's the link too http://link.afach.de/spinsde – The Quantum Physicist Jan 02 '16 at 05:15
  • I think the notion of steady state is not generally applicable when you have time-dependence on the right hand side (i.e., in general d rho/dt will never equal zero). It seems that for some of your variables, the time-varying terms cancel out as t->Infinity but for others they don't. The time-dependence is quite fast relative to the time you want to solve the system. This is going to make it a difficult problem for any software, not just Mathematica. I think you need to scale back or realize that these equations can't be solved in <1 second. – Chris K Jan 02 '16 at 16:49
  • My only remaining ideas are to start with better initial conditions (closer to the long-term result; if you're varying a parameter, extrapolate from the previous result) or to do some sort of time-averaging approximation to your system. I have no idea about the physics of your problem, so don't know if this is feasible. Good luck! – Chris K Jan 02 '16 at 16:51
  • @bbgodfrey I promise there's nothing in the link except a very simple Mathematica notebook with an NDSolve and a Plot. It's all under my own website, and it's my shortening website too. I thought this is the most convenient way to provide the notebook, and thus I did it through my own website. I hope that makes you feel better about it. If you insist, let me know and I'll provide it through Pastebin. I just don't want you to spend 1 hour cleaning a temporary notebook, because copying and pasting in Mathematica is so not convenient, especially with subscripts. – The Quantum Physicist Jan 02 '16 at 23:58
  • @bbgodfrey Here's the pastebin link, Thanks. http://pastebin.com/eZ2ZfNFY – The Quantum Physicist Jan 03 '16 at 07:22
  • @bbgodfrey I'm so sorry I don't use github... Could you please make an exception and use that link I provided and trust me as a scientist who has a real problem you're seeing here? Do you really think a scientist would be interested in installing bloatware? I mean the link will download a Mathematica notebook, which you could open with some text editor and check for yourself whether it has anything other than NDSolve and Plot before running/opening it with Mathematica... I'm out of solutions! – The Quantum Physicist Jan 03 '16 at 16:28

2 Answers2

7

It is convenient to gather the components of this ODE system into MyEquations, MyBC, and MyVariables, as I did while editing the question to correct a few transcription errors, so that the NDSolve call can be written as

s = NDSolve[{MyEquations, MyBC}, MyVariables, {t, 0, 500}];
Grid[Table[Plot[Evaluate[ReIm[Chop[Subscript[ρ, {i1, i2}][t] /. s]]], 
    {t, 0, 100}], {i1, -1, 1}, {i2, -1, 1}]]

enter image description here

Although four of the solutions remain oscillatory, the oscillations are becoming very small, and Subscript[ρ, {0, 0}] has reached a steady state.

LogPlot[Chop[Subscript[ρ, {0, 0}][t] /. s], {t, 0, 500}]

enter image description here

with a value of

Subscript[ρ, {0, 0}][t] /. s /. t -> 500
(* {0.0000124782 - 1.20307*10^-19 I} *)

It seems plausible to seek a steady state by setting derivatives equal to zero in MyEquations, as I suggested in a comment above.

steady = MyEquations /. Equal[z1_, z2_] -> Equal[0, z2];
ss = Solve[steady, MyVariables] // Simplify // Flatten;
Subscript[ρ, {0, 0}][t] /. ss
(* 0.0000124989 + 2.38692*10^-9 I *)

which agrees well with the numerical result at large t. For this ODE system, at least, the approach just outlined indeed yields the steady state.

Alternative Solution

Only four of the nine dependent variables exhibit rapid oscillation, suggesting that this oscillation can be eliminated by a change of variables. Before doing so, let us eliminate the function Subscript, which often introduces needless difficulties.

eqs = MyEquations /. Subscript[ρ, {i1_, i2_}] -> ρ[i1, i2]
(* {((I/2)*Derivative[1][ρ[0, 0]][t])/Pi == (-0.5*ρ[-1, 0][t])/E^((2*I)*Pi*t*Δ) + 0.5*E^((2*I)*Pi*t*Δ)*ρ[0, -1][t] - 
        (0. + 1.273255460229472*I)*ρ[0, 0][t] + 0.5*E^((2*I)*Pi*t*Δ)*ρ[0, 1][t] - (0.5*ρ[1, 0][t])/E^((2*I)*Pi*t*Δ), 
    ((I/2)*Derivative[1][ρ[1, 0]][t])/Pi == -0.5*E^((2*I)*Pi*t*Δ)*ρ[0, 0][t] + 0.5*E^((2*I)*Pi*t*Δ)*ρ[1, -1][t] - 
        (0. + 0.6366356878618905*I)*ρ[1, 0][t] + Δ*ρ[1, 0][t] + 0.5*E^((2*I)*Pi*t*Δ)*ρ[1, 1][t], 
    ((I/2)*Derivative[1][ρ[-1, 0]][t])/Pi == 0.5*E^((2*I)*Pi*t*Δ)*ρ[-1, -1][t] - (0. + 0.6366356878618905*I)*ρ[-1, 0][t] + Δ*ρ[-1, 0][t] + 
        0.5*E^((2*I)*Pi*t*Δ)*ρ[-1, 1][t] - 0.5*E^((2*I)*Pi*t*Δ)*ρ[0, 0][t], 
    ((I/2)*Derivative[1][ρ[0, 1]][t])/Pi == 
    (-0.5*ρ[-1, 1][t])/E^((2*I)*Pi*t*Δ) + (0.5*ρ[0, 0][t])/E^((2*I)*Pi*t*Δ) - (0. + 0.6366356878618905*I)*ρ[0, 1][t] - Δ*ρ[0, 1][t] - 
        (0.5*ρ[1, 1][t])/E^((2*I)*Pi*t*Δ), 
    ((I/2)*Derivative[1][ρ[1, 1]][t])/Pi == ((I/2)*(0.00005 + 4*ρ[0, 0][t]))/Pi - 
        0.5*E^((2*I)*Pi*t*Δ)*ρ[0, 1][t] + (0.5*ρ[1, 0][t])/E^((2*I)*Pi*t*Δ) - (0. + 0.000015915494309189534*I)*ρ[1, 1][t], 
    ((I/2)*Derivative[1][ρ[-1, 1]][t])/Pi == (0.5*ρ[-1, 0][t])/E^((2*I)*Pi*t*Δ) - (0. + 0.000015915494309189534*I)*ρ[-1, 1][t] - 
        0.5*E^((2*I)*Pi*t*Δ)*ρ[0, 1][t], 
    ((I/2)*Derivative[1][ρ[0, -1]][t])/Pi == (-0.5*ρ[-1, -1][t])/E^((2*I)*Pi*t*Δ) - 
        (0. + 0.6366356878618905*I)*ρ[0, -1][t] - Δ*ρ[0, -1][t] + (0.5*ρ[0, 0][t])/E^((2*I)*Pi*t*Δ) - (0.5*ρ[1, -1][t])/E^((2*I)*Pi*t*Δ), 
    ((I/2)*Derivative[1][ρ[1, -1]][t])/Pi == -0.5*E^((2*I)*Pi*t*Δ)*ρ[0, -1][t] - (0. + 0.000015915494309189534*I)*ρ[1, -1][t] + 
        (0.5*ρ[1, 0][t])/E^((2*I)*Pi*t*Δ), 
    ((I/2)*Derivative[1][ρ[-1, -1]][t])/Pi == (0. - 0.000015915494309189534*I)*ρ[-1, -1][t] + 
        (0.5*ρ[-1, 0][t])/E^((2*I)*Pi*t*Δ) - 0.5*E^((2*I)*Pi*t*Δ)*ρ[0, -1][t] + ((I/2)*(0.00005 + 4*ρ[0, 0][t]))/Pi} *)

bc = MyBC /. Subscript[ρ, {i1_, i2_}] -> ρ[i1, i2]
(* {ρ[0, 0][0] == 1, ρ[1, 0][0] == 0, ρ[-1, 0][0] == 0, ρ[0, 1][0] == 0, ρ[1, 1][0] == 0, 
    ρ[-1, 1][0] == 0, ρ[0, -1][0] == 0, ρ[1, -1][0] == 0, ρ[-1, -1][0] == 0} *)

var = MyVariables /. Subscript[ρ, {i1_, i2_}] -> ρ[i1, i2]
(* {ρ[0, 0][t], ρ[1, 0][t], ρ[-1, 0][t], ρ[0, 1][t], ρ[1, 1][t], ρ[-1, 1][t], 
    ρ[0, -1][t], ρ[1, -1][t], ρ[-1, -1][t]} *)

Note that the value of Δ has not yet been specified in eqs. Next, make the substitution

{ρ[-1, 0][t] -> E^(2 I π t Δ) σ[-1, 0][t], ρ[1, 0][t] -> E^(2 I π t Δ) σ[1, 0][t], 
 ρ[0, -1][t] -> E^(-2 I π t Δ) σ[0, -1][t], ρ[0, 1][t] -> E^(-2 I π t Δ) σ[0, 1][t]}

which after additional manipulations eliminates E^(2 I π t Δ) from the equations.

eqstt = First[#] == Simplify[Last[#] /. 
    {ρ[-1, 0][t] -> E^(2 I π t Δ) σ[-1, 0][t], ρ[1, 0][t] -> E^(2 I π t Δ) σ[1, 0][t], 
     ρ[0, -1][t] -> E^(-2 I π t Δ) σ[0, -1][t], ρ[0, 1][t] -> E^(-2 I π t Δ) σ[0, 1][t]}] &
     /@ eqs;
eqstt[[2]] = Thread[E^(-2 I π t Δ) eqstt[[2]] /. Derivative[1][ρ[1, 0]][t] -> 
    (Unevaluated[D[ρ[1, 0][t], t]] /. ρ[1, 0][t] -> E^(2 I π t Δ) σ[1, 0] [t]), Equal]
    // Expand;
eqstt[[3]] = Thread[E^(-2 I π t Δ) eqstt[[3]] /. Derivative[1][ρ[-1, 0]][t] -> 
    (Unevaluated[D[ρ[-1, 0][t], t]] /. ρ[-1, 0][t] -> E^(2 I π t Δ) σ[-1, 0] [t]), Equal]
    // Expand;
eqstt[[4]] = Thread[E^(2 I π t Δ) eqstt[[4]] /. Derivative[1][ρ[0, 1]][t] -> 
    (Unevaluated[D[ρ[0, 1][t], t]] /. ρ[0, 1][t] -> E^(-2 I π t Δ) σ[0, 1] [t]), Equal]
    // Expand;
eqstt[[7]] = Thread[E^(2 I π t Δ) eqstt[[7]] /. Derivative[1][ρ[0, -1]][t] -> 
    (Unevaluated[D[ρ[0, -1][t], t]] /. ρ[0, -1][t] -> E^(-2 I π t Δ) σ[0, -1] [t]), Equal]
    // Expand;

sstt = NDSolve[{eqstt /. Δ -> 3, bctt}, vartt, {t, 0, 500}];
Grid[Table[Plot[Evaluate[ReIm[ρ[i1, i2][t] /. 
    {ρ[-1, 0][t] -> σ[-1, 0][t], ρ[1, 0][t] -> σ[1, 0][t], ρ[0, -1][t] -> σ[0, -1][t], 
     ρ[0, 1][t] -> σ[0, 1][t]} /. sstt]], {t, 0, 500}], {i1, -1, 1}, {i2, -1, 1}]]

enter image description here

The AbsoluteTiming of this computation is almost two orders of magnitude less than that of the solution provided a few days earlier.

The steady state solution is obtained by setting derivatives to zero.

steady = eqstt /. Δ -> 3 /. {ρ[0, 0]'[t] -> 0, σ[1, 0]'[t] -> 0, σ[-1, 0]'[t] -> 0, 
    σ[0, 1]'[t] -> 0, ρ[1, 1]'[t] -> 0, ρ[-1, 1]'[t] -> 0, σ[0, -1]'[t] -> 0, 
    ρ[1, -1]'[t] -> 0, ρ[-1, -1]'[t] -> 0};
ss = Solve[steady, vartt] // Simplify // Flatten // Chop
(* {ρ[0, 0][t] -> 0.0000124767, σ[1, 0][t] -> -0.0000748591 - 7.94299*10^-6 I, 
    σ[-1, 0][t] -> -0.0000748591 - 7.94299*10^-6 I, σ[0, 1][t] -> .0000748591 + 
        7.94299*10^-6 I, 
    ρ[1, 1][t] -> 0.499994, ρ[-1, 1][t] -> -0.499073, 
    σ[0, -1][t] -> -0.0000748591 + 7.94299*10^-6 I, ρ[1, -1][t] -> -0.499073, 
    ρ[-1, -1][t] -> 0.499994} *)

which agree with the corresponding NDSolve solutions at t -> 500 to at least six significant figures.

A cursory examination of the OP's full 81 x 81 system of equations suggests that the transformation used here will work there as well, although the computations are likely to be one to two orders of magnitude slower. However, there appear to be additional symmetries in the equations that, if taken advantage of, may further decrease computational time.

bbgodfrey
  • 61,439
  • 17
  • 89
  • 156
  • Thank you for the answer. What about going to t=10^6. That's actually the real problem? My huge set of equations oscillate until that time... and using NSolve there is very slow... – The Quantum Physicist Jan 01 '16 at 00:49
  • @TheQuantumPhysicist The point of my answer was to demonstrate that setting derivatives to zero and using Solve on the resulting equations gives the steady-state answer. Presumably, you can do the same for your complete set of equations too. To be sure, using Solve or NSolve is likely to be slow, but that seems inevitable for such a large set of equations. I suggest you try it on some intermediate sized sets of equations to verify that the method used above for 9 equations works for, say, 20 equations, then 50, finally 100. – bbgodfrey Jan 01 '16 at 04:09
  • Thanks for the help. To be honest it's quite disappointing that Mathematica deals with differential equations in this weird way... While it's helpful for some applications, but who says all people require a range to solve their differential equations instead of just points?! This is deliberate wasting of memory and resources for no good reason... – The Quantum Physicist Jan 01 '16 at 10:07
  • 1
    @TheQuantumPhysicist I take it you would like Mathematica to integrate from zero to some very large number, discarding earlier results as it goes in order to conserve memory. Actually, it is possible to do this by using the final result of computing to t1 as initialization for a computation to t2, etc. Also, it is possible to run NDSolve using Runge-Kutta and a fixed time step, allow the check you described in your question. Do you need a steady-state in all variables or only in some? I shall take another look at your equations later today. Best wishes for the New Year. – bbgodfrey Jan 01 '16 at 10:29
  • Thank you very much for caring! Yes exactly. I need the computation up to some very large number. The problem with evaluating just the last number is that while doing it Mathematica assumes that I need all the values in between (thus creating an interpolation function), which gets my kernel to crash because it requires so much memory. I need the steady-state solutions only for diagonal components of the density matrix; i.e., components where the two subscript indices are equal. For the simple example I provided it's 3 components. – The Quantum Physicist Jan 01 '16 at 12:29
  • @TheQuantumPhysicist The coefficients in your sample equations contain just one oscillatory term or its conjugate. Is that true of your full system of equations as well? – bbgodfrey Jan 01 '16 at 15:26
  • There's only one oscillatory form, which is that exponential and its conjugate, yes. – The Quantum Physicist Jan 01 '16 at 16:44
  • @TheQuantumPhysicist My second solution significantly reduces run time for the sample problem and probably will do so for you full problem, which I have examined but not solved. Nonetheless, I believe that the computations will take many seconds each, unless you are able to take advantage of additional symmetries that undoubtedly are in the equations. Best wishes. – bbgodfrey Jan 03 '16 at 18:49
  • @TheQuantumPhysicist I now am confident that, if the transformations in my answer can be performed on your complete problem, each integration can be performed in a second or so. And, I can rewrite my second answer above in just six lines of code. However, your sample equations in the question contain Δ both in the argument of Exp and also as linear terms multiplying some dependent variables. In contrast, in spin.nb, Δ appears only in exponentials. Why is that? – bbgodfrey Jan 04 '16 at 10:52
  • The transformation you used is what's called "the rotating wave approximation" (but just without the approximation). It can be done in certain cases. It's worked on this example cuz there I'm using exponentials instead of cosines/sines (which is probably a mistake that oversimplifies the problem). The transformation you did simply means that you go to a rotating frame, which is valid. But this becomes more difficult if you replace these exponentials (Exp[-I 2 pi (f+Δ)]) with a cosine (Cos[2 pi (f+Δ)]), because that transformation creates a term with twice the frequency, which can be ignored. – The Quantum Physicist Jan 04 '16 at 11:52
  • If you can still do this transformation after replacing Exp with Cos without dropping any terms, I'd be really grateful! I'm trying to do that myself now but it's quite challenging. As to your question, why there's a Delta there; it's just a convenient coincidence because it's an academic simple problem, in contrast to the real system I'm trying to simulate. – The Quantum Physicist Jan 04 '16 at 11:56
  • @TheQuantumPhysicist Can you tell me which terms you expect to oscillate? In the sample problem, only those whose indices sum to an odd integer oscillate. Also, are you saying that spins.nb is wrong because it uses exponentials? If so, what other changes are needed? – bbgodfrey Jan 04 '16 at 12:38
  • Actually I don't know what terms are supposed to oscillate. Sorry about that! I still don't have an intuitive understanding of this system and I'm trying to learn it now. Yes, the file spins.nb has a problem/is wrong (also one more thing I learned now). The changes to be made only to change Exp to Cos and remove the imaginary unit I from the argument of Exp, so that it's an oscillation. I'm so thankful for your time and feel guilty for using it that much. – The Quantum Physicist Jan 04 '16 at 12:50
  • @TheQuantumPhysicist I just ran the case of Δ-> 2.87, which eliminates the exponentials. Nonetheless, some of the solutions terms oscillate with a period of around 300. Is that expected? It slows the computation. – bbgodfrey Jan 04 '16 at 13:06
  • Actually setting Δ-> 2.87 is quite invalid, because 2.87 GHz is the frequency of the light we input to the system, so by setting Δ-> 2.87 you're removing light from the system by setting its frequency to zero, removing the purpose of the whole simulation! Δ is supposed to be small in the range +/- 0.01. Sorry for not making that clear before! About oscillating solutions, the diagonal solutions (solutions with indices of equal values, there are 9, called populations, which are all I care about) are not supposed to oscillate. Actually I'd be surprised if they do. – The Quantum Physicist Jan 04 '16 at 13:15
  • @TheQuantumPhysicist Although the diagonal elements do not oscillate, the off-diagonal elements that do oscillate determine the time required by NDSolve. I suggest that you take time to be sure that you have the right equations. Chasing numerical solutions will not be productive otherwise. Best wishes, – bbgodfrey Jan 04 '16 at 13:52
  • That's a very interesting and good point! The fact that the off-diagonal components always oscillate could be the cause of the problem because they determine the time scale of the steps. Actually the equations are correct and I'm still trying to solve them with the exception of changing Exp to Cos. Anyway, thank you for your help, and I won't blame you if you stop working on the problem. You've helped so much. Thanks again! – The Quantum Physicist Jan 04 '16 at 14:29
  • @TheQuantumPhysicist If Exp is replaced throughout by Cos, then either the diagonal terms also will oscillate, or the sum of terms multiplied by Cos must vanish for the ODEs determining the diagonal terms. You have ruled out the former, and the latter does not seem credible to me. – bbgodfrey Jan 05 '16 at 09:30
  • Did you remove the complex I from the argument? – The Quantum Physicist Jan 05 '16 at 09:41
  • @TheQuantumPhysicist Actually, I have not run your code, unchanged or modified, in the last 20 hours. Instead, I merely note that the equations constitute an inhomogeneous Floquet problem, implying that all terms oscillate with the oscillatory coefficients, unless some special cancellations occur. With Exp the cancellations are fairly obvious. With Cos the cancellation probably would be what I described in my last comment, but such cancellations appears to eliminate all the physics. – bbgodfrey Jan 05 '16 at 09:49
  • So you're saying that it's a complicated system and there's no escape from it taking a very long time to solve. – The Quantum Physicist Jan 05 '16 at 10:01
  • @TheQuantumPhysicist Actually, I am questioning the correctness of the equations with Cos used. (No offence meant.) Try running a sample problem with few unknowns (say 9) to see whether it gives credible answers. In addition, it is a complicated system. – bbgodfrey Jan 05 '16 at 13:25
  • Well using Cos is definitely the right way to go. If you're interested, you could check the source of this information in the following link, where this problem is solved with the rotating wave approximation: https://www.osapublishing.org/oe/abstract.cfm?uri=oe-18-25-25494 – The Quantum Physicist Jan 05 '16 at 13:55
5

To help with the memory problem, you could save only the last value by using

s = NDSolve[{MyEquations, MyBC}, MyVariables, {t, 500, 500}];

and extract the final values using

Table[Subscript[ρ, {i1, i2}][t], {i1, -1, 1}, {i2, -1, 1}] /.s/.t->tmax

As an alternative to NSolve for finding the equilibrium, how about FindRoot?

bbgodfrey
  • 61,439
  • 17
  • 89
  • 156
Chris K
  • 20,207
  • 3
  • 39
  • 74