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}]

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

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?


Posted 2014-05-14T08:53:24.310

Reputation: 123

1I'm afraid that we would need inputfile.dat to try your code :) – Öskå – 2014-05-14T09:24:31.473

1Unrelated 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). – Sjoerd C. de Vries – 2014-05-14T09:38:38.277

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). – Sjoerd C. de Vries – 2014-05-14T09:44:16.407

Thanks already for the comments. I try to suppress the output with ; now, lets see how it works. How can I upload the input file? – Guiste – 2014-05-14T09:59:19.223

Does not help, still aborting... – Guiste – 2014-05-14T10:10:52.580 here you find the inputfile.dat – Guiste – 2014-05-14T10:34:06.590

1That seems to lead to a .exe file. I'm sorry, but I'm not going to download that. Could you try – Sjoerd C. de Vries – 2014-05-14T10:35:59.553 – Guiste – 2014-05-14T11:07:47.327

please use datatoint = dataimport[[2;;,1]], the first point is not calculatable – Guiste – 2014-05-14T11:12:00.173

1@SjoerdC.deVries, it leads to the right .dat file for me :) – Öskå – 2014-05-14T14:36:47.063

So is it working for you? – Guiste – 2014-05-14T19:29:12.933

@Guiste First you are saying r>0 and then you Integrate from 0 to rho. That's not possible. And secondly: check here for how to pass Assumptions to Integrate.

– Öskå – 2014-05-15T13:43:59.287



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.


Posted 2014-05-14T08:53:24.310

Reputation: 21

1Maybe 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 – 2014-08-09T03:40:09.733