0
$\begingroup$

I'm working basically with the creation of random numbers that represent the positions of $N$ electrons within a box. Later, I sum all the individual electric fields generated by the electrons, thus yielding the total electric field at one specific point in space (outside the box). After doing that for $N_{re}$ realizations of random electrons position, I build a histogram for the electric field at that specific point.

When I chose $N=10000$ electrons and $N_{re}=10000$, my computer takes a lot of time to perform these calculations -- even using Parallel commands.

Would you guys have any suggestion for improvement on the speed calculation? I'd appreciate anything that might help.

Here is the code, it's actually very short. I should notice that I'm also fitting the histogram with a well known distribution.

MonteCarlo[k_, Nt_, R_, Nre_, z_] := Module[{el, ϵd, nI, Rdip}, ϵd = 9.6 8.8542 10^-12; el = 1.6 10^-19; nI = Nt/R^3; rdop[i_] := {RandomReal[{-R/2, R/2}, {1}][[1]],RandomReal[{-R/2, R/2}, {1}][[1]],RandomReal[{-R/2, R/2}, {1}][[1]]}; Rlistdop = ParallelTable[rdop[i], {i, 1, Nt}]; AbsoluteTiming[Do[Rlistdip[j] = ParallelTable[rdip[i], {i, 1, Nt}], {j, 1,Nre}]]; El[j_, i_]:=el/(4 π ϵd) (Rlistdop[[i]] - rNV)/(Norm[Rlistdop[[i]]-rNV]^3); rNV = {0, 0, -R/2 - z}; Et[j_] := Sum[El[j, i], {i, 1, Nt}]; Elmean[j_] := 1/Nt Sum[El[j, i], {i, 1, Nt}]; dataEz[k]=ParallelTable[If[Abs[Et[j][[3]]] < 1 10^8, Et[j][[3]], Nothing], {j, 1, Nre}]; pEz[k]=Histogram[{dataEz[k]}, {-2 10^7, 2 10^7, 10^4}, PDF,PlotRange -> {{-2 10^7, 2 10^7}, All}, PlotTheme -> "Scientific",ChartLayout -> "Overlapped",LabelStyle ->Directive[Black, {FontFamily -> "Latin Modern Roman", FontSize -> 17}]]; μ0[k]=μ[k]/.FindDistributionParameters[dataEz[k],StudentTDistribution[μ[k],bba[k],cca[k]]][[1]]; b0[k]=bba[k]/.FindDistributionParameters[dataEz[k],StudentTDistribution[μ[k],bba[k],cca[k]]][[2]]; c0[k]=cca[k]/.FindDistributionParameters[dataEz[k],StudentTDistribution[μ[k],bba[k],cca[k]]][[3]]; \[ScriptCapitalD][k] = StudentTDistribution[μ0[k], b0[k], c0[k]]; μ0[k] = Median[\[ScriptCapitalD][k]]; fitd[x_][k] := PDF[\[ScriptCapitalD][k], x]; xhalf1[k]=x/.Solve[PDF[\[ScriptCapitalD][k], μ0[k]]/2 ==PDF[\[ScriptCapitalD][k],x],x][[1]]; xhalf2[k]=x/.Solve[PDF[\[ScriptCapitalD][k],μ0[k]]/2==PDF[\[ScriptCapitalD][k],x],x][[2]]; Ehalf[k] = Abs[xhalf1[k]] + Abs[xhalf2[k]]; Ehalfa[k, z] = Ehalf[k]; ] 

Using the module above, my situation correspond on

Nt = 10000; R = 1. 10^-7; Nre = 10000; MonteCarlo[1, Nt, R, Nre, 2 R] 

Thanks! Best, Denis

$\endgroup$
5
  • 4
    $\begingroup$ 2 brief comments. First, there is no reason to define functions inside of your module, just define them outside the module. Second, RandomReal can directly produce tensors, so just use something like Rlistdop = RandomReal[{-R/2, R/2}, {Nt, 3}]. $\endgroup$ Commented Aug 25, 2020 at 22:30
  • 2
    $\begingroup$ In order to speed up a natural choice is switching symbolic capabilities to numeric ones, e.g. Solve to FindRoot, Sum to NSum or just something like Total. $\endgroup$ Commented Aug 25, 2020 at 22:31
  • $\begingroup$ @CarlWoll I actually have noticed that 99% of the time is spending on building the dataEz[k] tables that defines my Histogram. Any idea on how to improve that? I've alread implemented your command, but just saved me 5%. $\endgroup$ Commented Aug 25, 2020 at 23:26
  • 1
    $\begingroup$ Is the above really the code you're using? What is rdip? Why does the definition of El[j_, i_] not depend on j? $\endgroup$ Commented Aug 25, 2020 at 23:42
  • $\begingroup$ It's actually a slight simplification of my code. El[j_, i_] doesn't depend explicitly on j. However, different values of j yield different values of El[j_,i_], as the latter is generated by other group of random numbers (that is connected to having different j). $\endgroup$ Commented Aug 26, 2020 at 15:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.