Speed up NDSolve for a changing equation?
You can speed up the evaluation a good bit by breaking the solution domain so you get rid of the Piecewise
:
gamma = 176;
alphag = 0.01;
alphajConstant = 0.00603;
p = {Cos[Pi/6], 0, Sin[Pi/6]};
current[t_] := 3 + .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, 50}, MaxSteps -> \[Infinity]];
current[t_] := (5 - 3)/(150 - 50) (t - 50) + 3 + .01*Sin[2*Pi*30*t];
m50 = (m /. First@sol1)[50];
LLGS = {m'[t] == cons[t] + tGilbdamp[t] + tSlondamp[t], m[50] == m50};
sol2 = NDSolve[LLGS, {m}, {t, 50, 150}, MaxSteps -> \[Infinity]];
current[t_] := 5 + .01*Sin[2*Pi*30*t];
m150 = (m /. First@sol2)[150];
LLGS = {m'[t] == cons[t] + tGilbdamp[t] + tSlondamp[t],
m[150] == m150};
sol3 = NDSolve[LLGS, {m}, {t, 150, 200}, MaxSteps -> \[Infinity]];
This runs in about 3-4 seconds..
plot the third component using ListPlot
to plot the actual solution points.
Show[{
ListPlot[
Transpose[{Flatten@(m /. sol1[[1]])["Grid"], (m /. sol1[[1]])[
"ValuesOnGrid"][[All, 3]]}]],
ListPlot[
Transpose[{Flatten@(m /. sol2[[1]])["Grid"], (m /. sol2[[1]])[
"ValuesOnGrid"][[All, 3]]}]],
ListPlot[
Transpose[{Flatten@(m /. sol3[[1]])["Grid"], (m /. sol3[[1]])[
"ValuesOnGrid"][[All, 3]]}]]}, PlotRange -> All]
components 1 and 2..
You can also convert the Piecewise[]
into terms of UnitStep[]
, using Simplify`PWToUnitStep
, and get a significant speed-up:
current[t_] :=
Simplify`PWToUnitStep@
Piecewise[{
{3, t <= 50},
{.02 (t - 50) + 3, 50 < t < 150},
{5, t >= 150}}] + .01*Sin[2*Pi*30*t];
LLGS = {m'[t] == cons[t] + tGilbdamp[t] + tSlondamp[t], m[0] == {0, 0, 1}};
sol1 = NDSolve[LLGS, {m}, {t, 0, 200}, MaxSteps -> ∞]; // AbsoluteTiming
(* {2.24653, Null} *)