# Computer freezes during NIntegrate[]

I have a notebook that freezes the computer every time I run it (I mean the whole computer becomes unresponsive and do not react to ctrl-shift-esc and ctr-alt-delete as well as alt-tab and windows-tab). The weird thing is it used to work without any big differences in the code as far as I can remember. I have made sure that it is indeed the notebook that freezes the computer. Here is the code:

t1 = 1.696222702113381;
p1 = 5.604730078805160;
t2 = 1.439432831934896;
p2 = 2.373727848568306;

psiN2[a_, b_, t1_, p1_, t2_, p2_, k_, n_, twoS_] :=
Cos[a/2]^k*Sin[a/2]^(n - 1 - k)*
Exp[I*(k - (n - 1)/2)*
b]*((Cos[a/2]*Cos[t1/2]*Exp[I*(p1 - b)/2] +
Sin[a/2]*Sin[t1/2]*Exp[I*(b - p1)/2])^
twoS + (Cos[a/2]*Cos[t2/2]*Exp[I*(p2 - b)/2] +
Sin[a/2]*Sin[t2/2]*Exp[I*(b - p2)/2])^twoS);

PsiN2[a1_, b1_, a2_, b2_, t1_, p1_, t2_, p2_, k1_, k2_, n_, twoS_] :=
Det[{{psiN2[a1, b1, t1, p1, t2, p2, k1, n, twoS],
psiN2[a2, b2, t1, p1, t2, p2, k1, n, twoS]}, {psiN2[a1, b1, t1,
p1, t2, p2, k2, n, twoS],
psiN2[a2, b2, t1, p1, t2, p2, k2, n, twoS]}}];

NIntegrate[
Abs[PsiN2[a1, b1, a2, b2, t1, p1, t2, p2, 0, 1, 1, 3]]^2*Sin[a1]*
Sin[a2], {a1, 0, Pi}, {b1, 0, 2*Pi}, {a2, 0, Pi}, {b2, 0, 2*Pi},
WorkingPrecision -> 15]


It's the numerical integration above that causes the freeze. I realize that it is a big calculation, but as mentioned it used to work (although it took a long time, around 30-60 minutes).

My specs are: Mathematica 10 on windows 7 with 4 cores and 20 GB of memory. Any idea what's going on?

• When you say it used to take a long time, give us a clue? A minute, an hour or a day :) – Ymareth Oct 13 '14 at 16:35
• Around 30-60 minutes I'd say. – jorgen Oct 13 '14 at 16:50
• When you say your computer freezes, you mean other applications become unresponsive and you have to reboot? OR just that Mathematica itself is busy? – Ymareth Oct 14 '14 at 8:00
• You're probably going to say sometime after when your machine chewed all the way through its 20Gb memory. I have 16Gb on my machine and it became sluggish when the kernel passed 12Gb though I have other stuff going on too. So the question "why does this calculation burn through so much memory?" remains. – Ymareth Oct 14 '14 at 12:55
• I have had similar experience! I use task manager to keep track of this. Sometimes, Mathematica uses memory suddenly jump from 200MB to 3.9GB in less than 30 seconds. That freezed my laptop as well. I have to force the kernel to close immediately, to avoid system crush. Mine specs: i7 940X, 4G, Win 7 X64, MMA 10 X64. – Chen Stats Yu Oct 14 '14 at 14:53

## 1 Answer

The integrand goes to infinity along a1 == 0 and a2 == 0:

Manipulate[
Plot3D[
Abs[PsiN2[a1, b1, a2, b2, t1, p1, t2, p2, 0, 1, 1, 3]]^2 * Sin[a1] * Sin[a2],
{a1, 0, Pi}, {a2, 0, Pi}],
{b1, 0, 2*Pi}, {b2, 0, 2*Pi}] One can see the divergence of the integral by controlling MaxRecursion:

Table[Quiet@
NIntegrate[
Abs[PsiN2[a1, b1, a2, b2, t1, p1, t2, p2, 0, 1, 1, 3]]^2*Sin[a1]*
Sin[a2], {a1, 0, Pi}, {b1, 0, 2*Pi}, {a2, 0, Pi}, {b2, 0, 2*Pi},
MaxRecursion -> r],
{r, 2, 5}]
(*
{6.67053*10^6 + 0. I,
1.06724*10^8 + 0. I,
1.70758*10^9 + 0. I,
2.73213*10^10 + 0. I}
*)


The kind of recursive subdivision that NIntegrate does quickly explodes the number of function evaluations in a high-dimensional integral, which in this case approaches a 16-fold increase in memory usage at each subdivision:

Table[MaxMemoryUsed[
Quiet@NIntegrate[
Abs[PsiN2[a1, b1, a2, b2, t1, p1, t2, p2, 0, 1, 1, 3]]^2*Sin[a1]*
Sin[a2], {a1, 0, Pi}, {b1, 0, 2*Pi}, {a2, 0, Pi}, {b2, 0, 2*Pi},
MaxRecursion -> r]
],
{r, 2, 5}]
N @ Ratios[%]
(*
{618704, 4407384, 64871808, 1032170480}
{7.12357, 14.7189, 15.9109}
*)