I am trying to integrate a two-dimensional function purely numerically. For one coordinate the integrand can oscillate rapidly while for the other coordinate the integrand is not behaving in a special way. Since Mathematica features special integration routines for oscillatory integrands only in 1D I figured out that it is a good idea to nest two NIntegrates. As a minimal example we consider the function f[x, y, a]
that is integrated over a square with edge length max
:
f[x_, y_, a_] := Exp[-y^2] Sin[a x]
fint[a_, max_] := NIntegrate[NIntegrate[f[x, y, a], {x, 0, max}, Method -> "LocalAdaptive"], {y, 0, max}]
For my calculation it is important to leave SymbolicProcessing activated since it speeds up my calculation by a factor of approximately 100. Now, if I try to integrate f[x, y, a]
for different values of a
then the following code does the trick:
max0 := 10
alist := N@Subdivide[1, 1000, 50]
result := Table[fint[a, max0], {a, alist}]
result
WaitAll[result]
In my case a single integration can take several minutes up to an hour. Over time the memory fills up until the kernels crash since there is no more free memory. As far as I understand Mathematica does not clear the cache of previous (finished) calculations automatically since it is expecting to use the results later on again. Apparently, the best solution to this issue would be if I could get Mathematica to close each subkernel after each evaluation in Table and start a fresh subkernel for the next one. However, I am not very experienced in Mathematica and I don't know how to do it.
I would be very grateful if somebody could help me and show me how it is done for my particular problem.
Edit 1: I think the problem is still not really clear so let me elaborate further. If you perform an integration with NIntegrate and you leave "SymbolicProcessing" on, NIntegrate will try to do some symbolic "magic" first to speed up the integration. Symbolic processing costs, at least to my knowledge, a lot of memory. Now, if you are using Table to calculate the integral for different parameters all the intermediate results of the symbolic processing etc. are stored in the memory but never deleted. Thus, if the computation takes a long time (and hence a lot of memory) you run out of memory eventually.