I want to simulate the fresnel diffraction through circular aperture.But mathematica is taking forever to calculate.Here is my code.
f[x_?NumericQ, y_?NumericQ, d_?NumericQ, p_?NumericQ, t_?NumericQ] :=
NIntegrate[(\[Zeta]*(Exp[
I*20000 ((d^2 + \[Zeta]^2)^(0.5) + ((p^2 + (x - \[Zeta]*
Cos[t])^2 + (y - \[Zeta]*
Sin[t])^2)^(0.5)))]))/(d^2 + \[Zeta]^2)^(0.5), {\
\[Zeta], 0, 0.008}]
w[x_?NumericQ, y_?NumericQ, d_?NumericQ, p_?NumericQ] :=
NIntegrate[f[x, y, d, p, t], {t, 0, 2*Pi}]
g[x_, y_, d_, p_] := (Abs[w[x, y, d, p]])^2
Plot3D[g[x, y, 0.004, 20], {x, -100, 100}, {y, -100, 100},
PlotRange -> Full]
What's the way out? *Specs:i5 7th gen intel processor,12 gb ram
Answer
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"}]
Comments
Post a Comment