Mathematica taking forever to compute
Let's compile the integrand into a Listable
CompiledFunction
:
cintegrand = Block[{x, y, d, p, ζ, t},
With[{code =
N[(ζ (Exp[I 20000 ((d^2 + ζ^2)^(1/2) + ((p^2 + (x - ζ Cos[t])^2 + (y - ζ Sin[t])^2)^(1/2)))]))/(d^2 + ζ^2)^(1/2)]
},
Compile[{{x, _Real}, {y, _Real}, {d, _Real}, {p, _Real}, {ζ, _Real}, {t, _Real}},
code,
CompilationTarget -> "C",
RuntimeAttributes -> {Listable},
Parallelization -> True
]
]
];
Next, pick a Gauss quadrature rule for high order polynomials (the integrand is very smooth but quite oscillatory):
{pts, weights, errweights} = NIntegrate`GaussRuleData[9, $MachinePrecision];
Divide the integration domain into $m \times n$ rectangles and set up the quadrature points and weights:
m = 20;
n = 20;
ζdata = Partition[Subdivide[0., 0.008, m], 2, 1];
tdata = Partition[Subdivide[0., 2 Pi, n], 2, 1];
{ζ, t} = Transpose[Tuples[{Flatten[ζdata.{1. - pts, pts}], Flatten[tdata.{1. - pts, pts}]}]];
ω = Flatten[KroneckerProduct[
Flatten[KroneckerProduct[Differences /@ ζdata, weights]],
Flatten[KroneckerProduct[Differences /@ tdata, weights]]
]];
Define the mapping that parameterizes the graph of OP's function w
:
W = {x, y} \[Function] {x, y, Abs[cintegrand[x, y, 0.004, 20., ζ, t].ω]^2};
Compute the values of W
on a $101 \times 101$ grid:
R = 2.;
data = Outer[W, Subdivide[-R, R, 100], Subdivide[-R, R, 100]]; // AbsoluteTiming // First
28.2668
Plot the result
ListPlot3D[Flatten[data, 1], PlotRange -> All, AxesLabel -> {"x", "y", "w"}]
TIP: In Mathematica use exact values,because have infinty precision.
f[x_?NumericQ, y_?NumericQ, d_?NumericQ, p_?NumericQ] :=
NIntegrate[(ζ*(Exp[I*20000 ((d^2 + ζ^2)^(1/2) + ((p^2 + (x - ζ*Cos[t])^2 +
(y - ζ*Sin[t])^2)^(1/2)))]))/(d^2 + ζ^2)^(1/2), {ζ, 0, 8/1000}, {t, 0, 2*Pi},
Method -> {"LocalAdaptive", "SymbolicProcessing" -> 0}];
g[x_, y_, d_, p_] := Abs[f[x, y, d, p]]^2;
n = 1/6; (*The smaller the value , 3D plot is smoother,but CPU time will increase !!! *)
ListPlot3D[Partition[Flatten[Table[{x, y, g[x, y, 4/1000, 20]},
{x, -2, 2, n}, {y, -2, 2, n}]], 3]]
(*In Range: -2<x<2 and -2<y<2.If you increase range domain CPU time will increase !!! *)
On my a 100$
old laptop calculation time is about 24
minutes.