For physics research, there is a differential equation that I simulate again and again. It would be wonderful to speed it up. Each time I run it, part of the input function $I(t)$ changes. It takes a few minutes each time, and after running a few hundred iterations per day, it is eating up a bunch of time. Each time I run it, the input function $I(t)$ only changes a little bit, and the output $\textbf{m(t)}$ also only changes a little bit.
The equation is the Landau -Lifshitz-Gilbert-Slonczewski Equation (LLGS) :
$$ \frac{d\textbf{m}}{dt}=-|\gamma|\textbf{m} \times \textbf{B}_\textrm{eff}-|\gamma|\alpha_\textrm{g}\textbf{m}\times[\textbf{m}\times\textbf{B}_\textrm{eff}]+|\gamma|\alpha_\textrm{j}~I(t)~\textbf{m}\times[\textbf{m}\times\textbf{p}] $$
In this equation, I am solving for $\bf{m}=\bf{m}(t)$, which is magnetization (a 3-D vector). $I(t)$ is current, an input parameter that is a function of time, which changes each time the LLGS is solved. $t$ for time, $\gamma$, $\alpha_\textrm{g}$, $\alpha_\textrm{j}$ are scalar constants, and $\textbf{B}_\textrm{eff}$ and $\textbf{p}$ are constant vectors.
Here is my code for solving this equation:
gamma = 176;
alphag = 0.01;
alphajConstant = 0.00603;
p = {Cos[Pi/6], 0, Sin[Pi/6]};
current[t_] := Piecewise[{{3, t <= 50}, {(5-3)/(150-50) (t - 50) + 3, 50 < t < 150}, {5, t >= 150}}] + .01*Sin[2*Pi*30*t];
Beff[t_] := {0, 0, 1.5 - 0.8*(m[t].{0, 0, 1})};
cons[t_] := -gamma*Cross[m[t], Beff[t]];
tGilbdamp[t_] := alphag*Cross[m[t], cons[t]];
tSlondamp[t_] := current[t]*alphajConstant*gamma*Cross[m[t], Cross[m[t], p]];
LLGS = {m'[t] == cons[t] + tGilbdamp[t] + tSlondamp[t], m[0] == {0, 0, 1}};
sol1 = NDSolve[LLGS, {m}, {t, 0, 200}, MaxSteps -> \[Infinity]];
mm[t_] = m[t] /. sol1[[1]] ;
To reiterate, this equation is solving for $\textbf{m(t)}$=m[t]
with a chaging input parameter $I(t)$=current[t]
. What might change? Perhaps a second current & simulation would look like this:
current[t_] := Piecewise[{{3.1, t <= 60}, {(5.1-3.1)/(200-60) (t - 60) + 3.1, 60< t < 200}, {5.1, t >= 200}}] + .05*Sin[2*Pi*31*t];
LLGS = {m'[t] == cons[t] + tGilbdamp[t] + tSlondamp[t], m[0] == {0, 0, 1}};
sol1 = NDSolve[LLGS, {m}, {t, 0, 200}, MaxSteps -> \[Infinity]];
What I am considering:
One thought is to use the functionality for NDSolve Reinitialize . Will this require me to change how my LLGS
equation is formatted? I assume that to use this I will need to change how current[t]
appears in my equation, and have it instead be a set of initial conditions. Is this the best option to speed things up?
Perhaps I should define a method? Perhaps there a way to use previous solutions as a starting point?
Is there a better way to make this go faster? Both individually, and in repetition?
NDSolve`Reinitialize[]
will save significant time only if the time forNDSolve`ProcessEquations[]
is significantly long. I suspect that the time taken forNDSolve`Iterate[]
will be at least a few orders of magnitude greater. In other words, I don't expect reinitialization to save much time. $\endgroup$