Mathematica crashes after some iterations

I wrote a Mathematica script, which calculates an integral value at a list of points and writes it to a file:

ϵ :=78.36;
z := 1;
bohr := 0.52917721;
boltzmann = 3.16681535*10^-6;
c := 10 * 6.02214129*10^23*1.0*10^-27*bohr^3;
κ := Sqrt[2.*4*π*c*z];
kBT := boltzmann * 300;

λ = κ/Sqrt[ϵ]*Sqrt[z/(kBT)]

funcin =
1/(ϵ*π)*Exp[-(2*r + λ*Sqrt[r^2 + ρ^2 - 2*r* ρ*Cos[θ]])]*
r^2*Sin[θ]*1/Sqrt[ρ^2 + r^2 - 2*r*ρ*Cos[θ]]

intfuncinang =
Assuming[r > 0 && ρ > 0 && ϵ > 0 & &λ > 0, Integrate[funcin, {θ,0,π}, {ϕ,0, 2π}]]

Assuming[ρ > 0 && ϵ > 0 && λ > 0,
funcins =
Integrate[intfuncinang, {r,0,ρ}] + Integrate[intfuncinang,{r, ρ, Infinity}]
]//Simplify

dataimport = Import["inputfile.dat", "Table"];
datatoint = dataimport[[1 ;;, 1]]

Print[Length[datatoint]]
int = Array[#&, Length[datatoint]];
Do[ρ = datatoint[[i]];
int[[i]] = N[funcins];
points = {datatoint[[i]], int[[i]]};
points >>> "outputfile.dat"
, {i, Length[datatoint]}]


I run this on the console with math -nopromt -run "<<script.m". This works always for some values, but crashes after some time. I am using Mathematica 8. Does anyone have an idea why I am getting crashes?

• I'm afraid that we would need inputfile.dat to try your code :)
– Öskå
May 14 '14 at 9:24
• Unrelated to your problem, but Array[#&,Length[datatoint]] is just Range[Length[datatoint]]. Perhaps points>>>"outputfile.dat" could use a ; to suppress output to the console (I can imagine that a large amount of that may choke the console). May 14 '14 at 9:38
• Another thing: your assignment of funcins within the Assuming/Simplify combo doesn't mean funcins contains the simplified result. You have to assign it to the result of the simplification (so outside of the call). May 14 '14 at 9:44
• That seems to lead to a .exe file. I'm sorry, but I'm not going to download that. Could you try pastebin.com? May 14 '14 at 10:35
• @SjoerdC.deVries, it leads to the right .dat file for me :)
– Öskå
May 14 '14 at 14:36

I have experienced kernel crashes like that of Mathematica 9.0.1 on OS X in scripts where there is a repeated output to text files during iterations over cycles. Curiously, same scripts run like a charm in Windows, so it is specifically the OS X issue.

Diagnosing this problem is hard, since the kernel just crashes, flushes all variables, and the only message appearing after choosing Why the Beep? from the Help menu is "kernel has quit during an evaluation". The crash is somewhat random, i.e. it does not happen at the same points in cycles or for the same data.

I managed to trace the problem (by naive Print debugging) to calls of Export. Specifically to the variant with Export["file.txt", data, "Table"]. Replacing this with Export["file.txt", data] fixes the problem. So it may be a bug in Export.

You get differently formatted output after the above mention change, with {} braces around lists - much like InputForm representation . When importing such files, you just do Import["file.txt", "List"] // ToExpression instead of Import["file.txt", "Table"].

I'm not sure what platform you working in, but if it is OS X you may be dealing with the same issue. Exporting with the file extension ".dat" produces "Table" format by default, I believe. Try using the extension ".txt" -- easiest of fixes, if it helps.

• Maybe you can get more information by looking at the crash log, or at least sending it to Wolfram support. To find this file, next time a crash occurs open Utilities > Console and check the last couple of lines written there. That usually tells you the path and name of the crash log.
– Jens
Aug 9 '14 at 3:40