I want to compile my function that calculates the energy of a particle system. This is what I've tried:
n = 512;
nl = Ceiling[Power[n, (3)^-1]];
r = 1.;
σ = 2 r;
ρ = 0.01/((2 r)^3);
l = Power[n/ρ, (3)^-1];
ε = 1;
εr = .01;
z = 1;
v = Flatten[Table[l ({i, j, k} - .5)/(nl), {i, nl}, {j, nl}, {k, nl}], 2];
c = RandomSample[Join[Table[-1, {n/2}], Table[1, {n/2}]]];
tc = Times[#[[All, 1]], #[[All, 2]]] &@Subsets[c, {2}];
UC =
Compile[{{vv, _Real, 2}},
Total[(4 ε ((σ/#[[1]])^12 - (σ/#[[1]])^6) +
1/(4 π εr) (#[[2]] z)/#[[1]]) & /@
Transpose[{Sqrt[Total[((#[[1]] - #[[2]]) -
l*Round[(#[[1]] - #[[2]])/l])^2]] & /@ Subsets[vv, {2}],
tc
}]
]
, CompilationTarget -> "C",
RuntimeOptions -> {"Speed", "CatchMachineUnderflow" -> True},
CompilationOptions -> {"InlineExternalDefinitions" -> True}]
UC[v]
However, the compilation of UC never finishes and after some time, the kernel becomes unresponsive and has to be killed. So I can't get the speed advantages of compilation. How can I persuade Mathematica to compile my function? I don't think the function is too complex to be compiled, there are only basic operations used.
CompilePrintto look at the compiled code. It calls the main evaluator almost everywhere. – Marius Ladegård Meyer Jun 02 '15 at 12:16Subsetsare not in the list. – Marius Ladegård Meyer Jun 02 '15 at 12:19Subsetsfunction. However, I can still put theSubsetspart outside and the rest should compile. – shrx Jun 02 '15 at 12:22System`SetSystemOptions["CompileOptions" -> "CompileReportExternal" -> True]and then try to compile your function. – Sektor Jun 02 '15 at 13:07