# Functions, strings and ToExpression in ParallelDo

I have defined functions:

fluximu[enu_] := -(enu - 5)^2 + 25;
otherfunc[enu_] :=
enu Boole[0 <= enu <= 5] + 5 Boole[5 < enu <= 8] + (13 - enu) Boole[8 < enu <= 10];
fluxie[enu_] := -(enu - 6)^2 + 36;


as well as the variables:

mat = ({{2, 3, 6}, {8, 1, 4}, {9, 2, 6} });
eff1 = 0.6;
eff2 = 0.8;


With these, I define the lists:

numui = {2, "fluximu", eff1};
nuei = {1, "fluxei", eff2};


which are used as nui in a general function:

nev[mat_?ArrayQ, nui_?ListQ, emin_, emax_]:=
Block[{ini, fluxfunc, effval},
(ini = nui[[1]];
fluxfunc = ToExpression[nui[[2]]];
effval = nui[[3]];
NIntegrate[effval mat[[ini,1]] otherfunc[enu] fluxfunc[enu],{enu, emin, emax}])]


This works well if I evaluate it on the main kernel:

nev[mat, numui, 0, 10]


3192.4

However, once I parallelize this, I get errors. For example:

ParallelDo[
numui = {2, "fluximu", effvar}; nev[mat, numui, 0, 10],
{effvar, 0.3, 0.9, 0.05}]


(kernel 8) NIntegrate::inumr: The integrand 2.4 (5 Boole[5 < enu <= 8]+(13 - enu) Boole[8 < enu <= 10] + enu Boole[0 <= enu <= 5]) fluximu[enu] has evaluated to non-numerical values for all sampling points in the region with boundaries {{5, 8}}.

Any hints on what may be wrong?

• Have you DistributeDifinitions ? – happy fish Sep 20 '16 at 6:38
• Please post a complete but minimal example. See sscce.org for guidance. There isn't enough information in your post to reproduce the problem. Do make sure that your example is as small as possible though, otherwise the chance that someone will look at it isn't too great. – Szabolcs Sep 20 '16 at 8:18
• Added example. Not sure how to use DistributeDefinitions in this case... – Stilgar Sep 20 '16 at 13:11
• This seems to be related to NIntegrate (and Vegas, from the CUBA library). I have defined a function:newflux4[enu_?NumericQ, list_?ListQ] := Block[ {func} , ( func = ToExpression[ list[[2]] ]; func[enu])] and it does evaluate in parallel..... – Stilgar Sep 21 '16 at 17:46
• Okay, I managed to make it work. One needs to define the function to be evaluated without NIntegrate, and then carry out the integration externally. It is also crucial to use "DistributeDefinitions" on all other functions that might be called (ie, those that are defined by strings). – Stilgar Sep 21 '16 at 18:05