0
$\begingroup$

I am evaluating the numerical integration it takes very much long time. Can somebody suggest to improve the code? Code runs smoothly for the first plot given by h1. But when it comes to the numerical integration and summation it takes long hours to run.

\[Gamma]3 = 3;
\[Gamma]2 = 0.05*\[Gamma]3;
Subscript[\[CapitalOmega], TH] = 8*\[Gamma]3;
Subscript[\[CapitalDelta], TH] = 0;
vf = 3;
\[HBar] = 1;
c = 1;
e = 1;

d21 = -(\[Gamma]2/2 + 
     I*(\[CapitalDelta]p - Subscript[\[CapitalDelta], TH]) + 
     2*\[CapitalGamma]);
d31 = -(\[Gamma]3/2 + I*\[CapitalDelta]p);

\[Omega] = 45;
B = 3;

\[Chi][x_] := (-I*d21)/(
  d21*d31 + Abs[Subscript[\[CapitalOmega], TH]]^2*Sin[\[Pi] *x]^2);

\[Omega]c = Sqrt[2]/Sqrt[(c*\[HBar])/(e*B)];
\[Omega]31 = ((Sign[2]*\[HBar]*Sqrt[2] + 
     Sign[1]*\[HBar]*1)*\[Omega]c)/\[HBar];

f = 5;

\[CapitalDelta]p = \[Omega]31/(2*\[Pi]) - \[Omega]/(2*\[Pi]);
\[CapitalLambda] = 4*\[Lambda];

L = 5;
\[Lambda] = 0.25;

t1[x_] := Exp[-(Pi/\[Lambda])*Im[\[Chi][x]]*L]*
  Exp[I*Pi/\[Lambda]*Re[\[Chi][x]]*L]

h1 = DensityPlot[Abs[t1[x]], {x, -5, 5},{\[CapitalGamma], 0, 4},
  ColorFunction->"SunsetColors", PlotRange->All, PlotPoints->80,
  LabelStyle->Directive[Black, Large],Frame->True, PlotLegends->Automatic]

m = 2;    
g1[n_, x_] := 1/\[CapitalLambda]*Abs[t1[x]]*
  Exp[-((I*2*\[Pi]*n*x)/\[CapitalLambda])]

e1[n_] := NIntegrate[g1[n, x], {x, 0, \[CapitalLambda]}, 
  Method -> "MultiPeriodic"]

e3[x1_] := Sum[e1[n]*Exp[-I*m*n^2*\[Pi]]*
  Exp[-I*(2 \[Pi]*n*x1)/\[CapitalLambda]], {n, -10, 10}]

h2 = DensityPlot[Abs[e3[x1]], {x1, -5, 5},
  {\[CapitalGamma], 0, 4}, PlotRange->All,
    ColorFunction->"SunsetColors", PlotPoints->50,
    PlotLegends->Automatic, LabelStyle->Directive[Black, Large]] 
$\endgroup$
10
  • 1
    $\begingroup$ Is the above supposed to be Latex? it is hard to read. Do you happen to have the Mathematica code or just Latex? If just Latex, the above does not render in mathjax as you can see. $\endgroup$
    – Nasser
    Sep 5, 2023 at 18:06
  • $\begingroup$ @Nasser I have edited the question and changed the format. $\endgroup$ Sep 5, 2023 at 18:41
  • $\begingroup$ You do not have "\[CapitalGamma]" defined anywhere. Numerical integration will not work otherwise. Maybe you forgot to copy it. $\endgroup$
    – Nasser
    Sep 5, 2023 at 18:49
  • $\begingroup$ Plotting against the Capital gamma.see h1 and h2 $\endgroup$ Sep 5, 2023 at 18:50
  • $\begingroup$ Yes, I see that, but you are calling e3[x1] from inside the plot, while your function e3 is calling e1 which has no access to the value of Capital gamma. Look carefully at the code and you will see. You should then be passing Capital gamma to e3 and it in turn passes it to e1. $\endgroup$
    – Nasser
    Sep 5, 2023 at 18:52

1 Answer 1

1
$\begingroup$

But it takes 2 hrs to run.

Well, you are calling sum for 20 items, each one does integration. And this is all done for each sampling point. So there is a lot to do.

If you change to PerformanceGoal -> "Speed" it finishes much faster also.

But this finishes in 70 seconds for me

Mathematica graphics

And this in finishes in 354 seconds

Mathematica graphics

Link to notebook.nb

Code with changes made

ClearAll["Global`*"]
γ3=3
γ2 = 0.05*γ3
ΩTH = 8*γ3
ΔTH=0
vf=3
ℏ=1
c=1
e=1
d21[Γ_]:=-(γ2/2+I*(Δp-ΔTH)+2*Γ)
d31=-(γ3/2+I*Δp)
ω=45
B=3
χ[x_?NumericQ,Γ_?NumericQ]:=(-I*d21[Γ])/(d21[Γ]*d31+Abs[ΩTH]^2*Sin[π*x]^2)
ωc=Sqrt[2]/Sqrt[(c*ℏ)/(e*B)]

ω31=((Sign[2]*ℏ*Sqrt[2]+Sign[1]*ℏ*1)*ωc)/ℏ

f=5

Δp=ω31/(2*π)-ω/(2*π)

Λ=4*λ

L=5

λ=0.25

t1[x_?NumericQ,Γ_?NumericQ]:=Exp[-(Pi/λ)*Im[χ[x,Γ]]*L]*Exp[I*Pi/λ*Re[χ[x,Γ]]*L]

h1=DensityPlot[Abs[t1[x,Γ]],{x,-5,5},{Γ,0,4},ColorFunction->"SunsetColors",PlotRange->All,PlotPoints->80,LabelStyle->Directive[Black,Large],Frame->True,PlotLegends->Automatic]

m = 2;

g1[n_?NumericQ, x_?NumericQ, Γ_?NumericQ] := 
 1/Λ*Abs[t1[x, Γ]]*
  Exp[-((I*2*π*n*x)/Λ)]

e1[n_?NumericQ, Γ_?NumericQ] := 
 NIntegrate[g1[n, x, Γ], {x, 0, Λ}, 
  Method -> "MultiPeriodic"]

e3[x1_?NumericQ, Γ_?NumericQ] := 
 Sum[e1[n, Γ]*Exp[-I*m*n^2*π]*
   Exp[-I*(2 π*n*x1)/Λ], {n, -10, 10}]

Timing@DensityPlot[
  Abs[e3[x1, Γ]], {x1, -5, 5}, {Γ, 0, 4}, 
  PlotRange -> All, ColorFunction -> "SunsetColors", 
  PlotLegends -> Automatic, LabelStyle -> Directive[Black, Large], 
  PerformanceGoal -> "Quality"]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.